removed 'application' folder to have everything in the root directory
This commit is contained in:
307
core/Statistics/SimpleStatsList.php
Normal file
307
core/Statistics/SimpleStatsList.php
Normal file
@ -0,0 +1,307 @@
|
||||
<?php
|
||||
|
||||
namespace ManiaControl\Statistics;
|
||||
|
||||
use FML\Controls\Frame;
|
||||
use FML\Controls\Label;
|
||||
use FML\Controls\Labels\Label_Text;
|
||||
use FML\Controls\Quad;
|
||||
use FML\Controls\Quads\Quad_BgsPlayerCard;
|
||||
use FML\Controls\Quads\Quad_Icons64x64_1;
|
||||
use FML\Controls\Quads\Quad_UIConstruction_Buttons;
|
||||
use FML\ManiaLink;
|
||||
use ManiaControl\Callbacks\CallbackListener;
|
||||
use ManiaControl\Callbacks\CallbackManager;
|
||||
use ManiaControl\Callbacks\Callbacks;
|
||||
use ManiaControl\Commands\CommandListener;
|
||||
use ManiaControl\ManiaControl;
|
||||
use ManiaControl\Manialinks\ManialinkManager;
|
||||
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
|
||||
use ManiaControl\Players\Player;
|
||||
use ManiaControl\Players\PlayerManager;
|
||||
use ManiaControl\Utils\Formatter;
|
||||
|
||||
/**
|
||||
* Simple Stats List Class
|
||||
*
|
||||
* @author ManiaControl Team <mail@maniacontrol.com>
|
||||
* @copyright 2014 ManiaControl Team
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
class SimpleStatsList implements ManialinkPageAnswerListener, CallbackListener, CommandListener {
|
||||
/*
|
||||
* Constants
|
||||
*/
|
||||
const ACTION_OPEN_STATSLIST = 'SimpleStatsList.OpenStatsList';
|
||||
const ACTION_SORT_STATS = 'SimpleStatsList.SortStats';
|
||||
|
||||
/*
|
||||
* Private properties
|
||||
*/
|
||||
/** @var ManiaControl $maniaControl */
|
||||
private $maniaControl = null;
|
||||
private $statArray = array();
|
||||
private $statsWidth = 0;
|
||||
|
||||
/**
|
||||
* Construct a new simple stats list instance
|
||||
*
|
||||
* @param ManiaControl $maniaControl
|
||||
*/
|
||||
public function __construct(ManiaControl $maniaControl) {
|
||||
$this->maniaControl = $maniaControl;
|
||||
|
||||
// Callbacks
|
||||
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
|
||||
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONINIT, $this, 'handleOnInit');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the menu entry
|
||||
*/
|
||||
public function handleOnInit() {
|
||||
$this->maniaControl->getCommandManager()->registerCommandListener('stats', $this, 'command_ShowStatsList', false, 'Shows statistics.');
|
||||
|
||||
// Action Open StatsList
|
||||
$this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::ACTION_OPEN_STATSLIST, $this, 'command_ShowStatsList');
|
||||
|
||||
$itemQuad = new Quad_UIConstruction_Buttons();
|
||||
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_Stats);
|
||||
$itemQuad->setAction(self::ACTION_OPEN_STATSLIST);
|
||||
$this->maniaControl->getActionsMenu()->addMenuItem($itemQuad, true, 14, 'Open Statistics');
|
||||
|
||||
//TODO settings if a stat get shown
|
||||
$this->registerStat(PlayerManager::STAT_SERVERTIME, 10, "ST", 20, StatisticManager::STAT_TYPE_TIME);
|
||||
$this->registerStat(StatisticCollector::STAT_ON_HIT, 20, "H");
|
||||
$this->registerStat(StatisticCollector::STAT_ON_NEARMISS, 30, "NM");
|
||||
$this->registerStat(StatisticCollector::STAT_ON_KILL, 40, "K");
|
||||
$this->registerStat(StatisticCollector::STAT_ON_DEATH, 50, "D");
|
||||
$this->registerStat(StatisticCollector::STAT_ON_CAPTURE, 60, "C");
|
||||
|
||||
$this->registerStat(StatisticManager::SPECIAL_STAT_KD_RATIO, 70, "K/D", 12, StatisticManager::STAT_TYPE_FLOAT);
|
||||
$this->registerStat(StatisticManager::SPECIAL_STAT_LASER_ACC, 80, "LAcc", 15, StatisticManager::STAT_TYPE_FLOAT);
|
||||
$this->registerStat(StatisticManager::SPECIAL_STAT_HITS_PH, 85, "H/h", 15, StatisticManager::STAT_TYPE_FLOAT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a Certain Stat
|
||||
*
|
||||
* @param string $statName
|
||||
* @param int $order
|
||||
* @param string $headShortCut
|
||||
* @param int $width
|
||||
* @param string $format
|
||||
*/
|
||||
public function registerStat($statName, $order, $headShortCut, $width = 10, $format = StatisticManager::STAT_TYPE_INT) {
|
||||
// TODO: use own model class
|
||||
$this->statArray[$order] = array();
|
||||
$this->statArray[$order]["Name"] = $statName;
|
||||
$this->statArray[$order]["HeadShortCut"] = '$o' . $headShortCut;
|
||||
$this->statArray[$order]["Width"] = $width;
|
||||
$this->statArray[$order]["Format"] = $format;
|
||||
$this->statsWidth += $width;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the stat List
|
||||
*
|
||||
* @param array $callback
|
||||
* @param Player $player
|
||||
*/
|
||||
public function command_ShowStatsList(array $callback, Player $player) {
|
||||
$this->showStatsList($player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the StatsList Widget to the Player
|
||||
*
|
||||
* @param Player $player
|
||||
* @param string $order
|
||||
*/
|
||||
public function showStatsList(Player $player, $order = PlayerManager::STAT_SERVERTIME) {
|
||||
$height = $this->maniaControl->getManialinkManager()->getStyleManager()->getListWidgetsHeight();
|
||||
$quadStyle = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultMainWindowStyle();
|
||||
$quadSubstyle = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultMainWindowSubStyle();
|
||||
|
||||
|
||||
$maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID);
|
||||
$width = $this->statsWidth + 60;
|
||||
//TODO handle size when stats are empty
|
||||
|
||||
// Main frame
|
||||
$frame = new Frame();
|
||||
$maniaLink->add($frame);
|
||||
$frame->setSize($width, $height);
|
||||
$frame->setPosition(0, 0, 10);
|
||||
|
||||
// Background
|
||||
$backgroundQuad = new Quad();
|
||||
$frame->add($backgroundQuad);
|
||||
$backgroundQuad->setSize($width, $height);
|
||||
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
|
||||
|
||||
// Close Quad (X)
|
||||
$closeQuad = new Quad_Icons64x64_1();
|
||||
$frame->add($closeQuad);
|
||||
$closeQuad->setPosition($width * 0.483, $height * 0.467, 3);
|
||||
$closeQuad->setSize(6, 6);
|
||||
$closeQuad->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_QuitRace);
|
||||
$closeQuad->setAction(ManialinkManager::ACTION_CLOSEWIDGET);
|
||||
|
||||
// Start offsets
|
||||
$xStart = -$width / 2;
|
||||
$posY = $height / 2;
|
||||
|
||||
// Predefine Description Label
|
||||
$descriptionLabel = new Label();
|
||||
$frame->add($descriptionLabel);
|
||||
$descriptionLabel->setAlign($descriptionLabel::LEFT, $descriptionLabel::TOP);
|
||||
$descriptionLabel->setPosition($xStart + 10, -$height / 2 + 5);
|
||||
$descriptionLabel->setSize($width * 0.7, 4);
|
||||
$descriptionLabel->setTextSize(2);
|
||||
$descriptionLabel->setVisible(false);
|
||||
|
||||
// Headline
|
||||
$headFrame = new Frame();
|
||||
$frame->add($headFrame);
|
||||
$headFrame->setY($posY - 5);
|
||||
|
||||
$posX = $xStart;
|
||||
$array['$oId'] = $posX + 5;
|
||||
$array['$oNickname'] = $posX + 14;
|
||||
|
||||
// Headline
|
||||
$posX = $xStart + 55;
|
||||
$statRankings = array();
|
||||
foreach ($this->statArray as $key => $stat) {
|
||||
$ranking = $this->maniaControl->getStatisticManager()->getStatsRanking($stat["Name"]);
|
||||
if (!empty($ranking)) {
|
||||
$statRankings[$stat["Name"]] = $ranking;
|
||||
$array[$stat['HeadShortCut']] = $posX;
|
||||
$posX += $stat["Width"];
|
||||
} else {
|
||||
unset($this->statArray[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
$labels = $this->maniaControl->getManialinkManager()->labelLine($headFrame, $array);
|
||||
|
||||
// Description Label
|
||||
$index = 2;
|
||||
foreach ($this->statArray as $statArray) {
|
||||
if (!isset($labels[$index])) {
|
||||
break;
|
||||
}
|
||||
|
||||
/** @var Label_Text $label */
|
||||
$label = $labels[$index];
|
||||
|
||||
$label->setAction(self::ACTION_SORT_STATS . '.' . $statArray["Name"]);
|
||||
$label->addTooltipLabelFeature($descriptionLabel, '$o ' . $statArray["Name"]);
|
||||
$index++;
|
||||
}
|
||||
|
||||
// define standard properties
|
||||
$textSize = 1.5;
|
||||
$textColor = 'fff';
|
||||
$index = 1;
|
||||
$posY -= 10;
|
||||
|
||||
if (!isset($statRankings[$order])) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($statRankings[$order] as $playerId => $value) {
|
||||
$listPlayer = $this->maniaControl->getPlayerManager()->getPlayerByIndex($playerId);
|
||||
if (!$listPlayer) {
|
||||
continue;
|
||||
}
|
||||
if ($index === 15) {
|
||||
break;
|
||||
}
|
||||
|
||||
$playerFrame = new Frame();
|
||||
$frame->add($playerFrame);
|
||||
|
||||
// Show current Player Arrow
|
||||
if ($playerId == $player->index) {
|
||||
$currentQuad = new Quad_Icons64x64_1();
|
||||
$playerFrame->add($currentQuad);
|
||||
$currentQuad->setX($xStart + 3.5);
|
||||
$currentQuad->setZ(0.2);
|
||||
$currentQuad->setSize(4, 4);
|
||||
$currentQuad->setSubStyle($currentQuad::SUBSTYLE_ArrowBlue);
|
||||
}
|
||||
|
||||
$displayArray = array();
|
||||
|
||||
foreach ($this->statArray as $stat) {
|
||||
$statValue = 0;
|
||||
if (isset($statRankings[$stat['Name']][$playerId])) {
|
||||
$statValue = $statRankings[$stat['Name']][$playerId];
|
||||
if ($stat['Format'] == StatisticManager::STAT_TYPE_TIME) {
|
||||
$statValue = Formatter::formatTimeH($statValue);
|
||||
} else if ($stat['Format'] == StatisticManager::STAT_TYPE_FLOAT) {
|
||||
$statValue = round(floatval($statValue), 2);
|
||||
}
|
||||
}
|
||||
$displayArray[$stat['Name']] = array('Value' => strval($statValue), 'Width' => $stat['Width']);
|
||||
}
|
||||
|
||||
$array = array($index => $xStart + 5, $listPlayer->nickname => $xStart + 14);
|
||||
$this->maniaControl->getManialinkManager()->labelLine($playerFrame, $array);
|
||||
|
||||
$posX = $xStart + 55;
|
||||
foreach ($displayArray as $key => $array) {
|
||||
$label = new Label_Text();
|
||||
$playerFrame->add($label);
|
||||
$label->setHAlign($label::LEFT);
|
||||
$label->setX($posX);
|
||||
$label->setStyle($label::STYLE_TextCardSmall);
|
||||
$label->setTextSize($textSize);
|
||||
$label->setText($array['Value']);
|
||||
$label->setTextColor($textColor);
|
||||
$label->addTooltipLabelFeature($descriptionLabel, '$o ' . $key);
|
||||
$posX += $array['Width'];
|
||||
}
|
||||
|
||||
$playerFrame->setY($posY);
|
||||
|
||||
if ($index % 2 !== 0) {
|
||||
$lineQuad = new Quad_BgsPlayerCard();
|
||||
$playerFrame->add($lineQuad);
|
||||
$lineQuad->setSize($width, 4);
|
||||
$lineQuad->setSubStyle($lineQuad::SUBSTYLE_BgPlayerCardBig);
|
||||
$lineQuad->setZ(0.001);
|
||||
}
|
||||
|
||||
$index++;
|
||||
$posY -= 4;
|
||||
}
|
||||
|
||||
$this->maniaControl->getManialinkManager()->displayWidget($maniaLink, $player, 'SimpleStatsList');
|
||||
}
|
||||
|
||||
/**
|
||||
* Called on ManialinkPageAnswer
|
||||
*
|
||||
* @param array $callback
|
||||
*/
|
||||
public function handleManialinkPageAnswer(array $callback) {
|
||||
$actionId = $callback[1][2];
|
||||
$actionArray = explode('.', $actionId, 3);
|
||||
if (count($actionArray) <= 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
$action = $actionArray[0] . '.' . $actionArray[1];
|
||||
|
||||
switch ($action) {
|
||||
case self::ACTION_SORT_STATS:
|
||||
$playerLogin = $callback[1][1];
|
||||
$player = $this->maniaControl->getPlayerManager()->getPlayer($playerLogin);
|
||||
$this->showStatsList($player, $actionArray[2]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
371
core/Statistics/StatisticCollector.php
Normal file
371
core/Statistics/StatisticCollector.php
Normal file
@ -0,0 +1,371 @@
|
||||
<?php
|
||||
|
||||
namespace ManiaControl\Statistics;
|
||||
|
||||
use ManiaControl\Callbacks\CallbackListener;
|
||||
use ManiaControl\Callbacks\CallbackManager;
|
||||
use ManiaControl\Callbacks\Callbacks;
|
||||
use ManiaControl\ManiaControl;
|
||||
use ManiaControl\Players\Player;
|
||||
use ManiaControl\Players\PlayerManager;
|
||||
|
||||
/**
|
||||
* Statistic Collector Class
|
||||
*
|
||||
* @author ManiaControl Team <mail@maniacontrol.com>
|
||||
* @copyright 2014 ManiaControl Team
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
class StatisticCollector implements CallbackListener {
|
||||
/*
|
||||
* Constants
|
||||
*/
|
||||
const SETTING_COLLECT_STATS_ENABLED = 'Collect Stats Enabled';
|
||||
const SETTING_COLLECT_STATS_MINPLAYERS = 'Minimum Player Count for Collecting Stats';
|
||||
const SETTING_ON_SHOOT_PRESTORE = 'Prestore Shots before Insert into Database';
|
||||
/*
|
||||
* Statistics
|
||||
*/
|
||||
const STAT_PLAYTIME = 'Play Time';
|
||||
const STAT_MAP_WINS = 'Map Wins';
|
||||
const STAT_ON_SHOOT = 'Shots';
|
||||
const STAT_ON_NEARMISS = 'Near Misses';
|
||||
const STAT_ON_CAPTURE = 'Captures';
|
||||
const STAT_ON_HIT = 'Hits';
|
||||
const STAT_ON_GOT_HIT = 'Got Hits';
|
||||
const STAT_ON_DEATH = 'Deaths';
|
||||
const STAT_ON_PLAYER_REQUEST_RESPAWN = 'Respawns';
|
||||
const STAT_ON_KILL = 'Kills';
|
||||
const STAT_LASER_SHOT = 'Laser Shots';
|
||||
const STAT_LASER_HIT = 'Laser Hits';
|
||||
const STAT_ROCKET_SHOT = 'Rocket Shots';
|
||||
const STAT_ROCKET_HIT = 'Rocket Hits';
|
||||
const STAT_ARROW_SHOT = 'Arrow Shots';
|
||||
const STAT_ARROW_HIT = 'Arrow Hits';
|
||||
const STAT_NUCLEUS_SHOT = 'Nucleus Shots';
|
||||
const STAT_NUCLEUS_HIT = 'Nucleus Hits';
|
||||
|
||||
const SPECIAL_STAT_KILL_DEATH_RATIO = 'Kill / Death';
|
||||
|
||||
const WEAPON_LASER = 1;
|
||||
const WEAPON_ROCKET = 2;
|
||||
const WEAPON_NUCLEUS = 3;
|
||||
const WEAPON_ARROW = 5;
|
||||
|
||||
/*
|
||||
* Private properties
|
||||
*/
|
||||
/** @var ManiaControl $maniaControl */
|
||||
private $maniaControl = null;
|
||||
private $onShootArray = array();
|
||||
|
||||
/**
|
||||
* Construct a new statistic collector instance
|
||||
*
|
||||
* @param ManiaControl $maniaControl
|
||||
*/
|
||||
public function __construct(ManiaControl $maniaControl) {
|
||||
$this->maniaControl = $maniaControl;
|
||||
|
||||
// Callbacks
|
||||
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_MODESCRIPTCALLBACK, $this, 'handleCallbacks');
|
||||
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_MODESCRIPTCALLBACKARRAY, $this, 'handleCallbacks');
|
||||
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONINIT, $this, 'onInit');
|
||||
$this->maniaControl->getCallbackManager()->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECT, $this, 'onPlayerDisconnect');
|
||||
|
||||
// Settings
|
||||
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_COLLECT_STATS_ENABLED, true);
|
||||
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_COLLECT_STATS_MINPLAYERS, 4);
|
||||
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_ON_SHOOT_PRESTORE, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle ManiaControl OnInit Callback
|
||||
*/
|
||||
public function onInit() {
|
||||
// Define Stats MetaData
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_PLAYTIME, StatisticManager::STAT_TYPE_TIME);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_MAP_WINS);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_ON_SHOOT);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_ON_NEARMISS);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_ON_CAPTURE);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_ON_HIT);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_ON_GOT_HIT);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_ON_DEATH);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_ON_PLAYER_REQUEST_RESPAWN);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_ON_KILL);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_LASER_HIT);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_LASER_SHOT);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_NUCLEUS_HIT);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_NUCLEUS_SHOT);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_ROCKET_HIT);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_ROCKET_SHOT);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_ARROW_HIT);
|
||||
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_ARROW_SHOT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle EndMap
|
||||
*
|
||||
* @param array $callback
|
||||
*/
|
||||
public function onEndMap(array $callback) {
|
||||
//Check for Minimum PlayerCount
|
||||
if ($this->maniaControl->getPlayerManager()->getPlayerCount() < $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_COLLECT_STATS_MINPLAYERS)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$leaders = $this->maniaControl->getServer()->getRankingManager()->getLeaders();
|
||||
|
||||
foreach ($leaders as $leaderLogin) {
|
||||
$leader = $this->maniaControl->getPlayerManager()->getPlayer($leaderLogin);
|
||||
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_MAP_WINS, $leader);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert OnShoot Statistic when a player leaves
|
||||
*
|
||||
* @param Player $player
|
||||
*/
|
||||
public function onPlayerDisconnect(Player $player) {
|
||||
// Check if Stat Collecting is enabled
|
||||
if (!$this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_COLLECT_STATS_ENABLED)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Insert Data into Database, and destroy player
|
||||
if (isset($this->onShootArray[$player->login])) {
|
||||
if ($this->onShootArray[$player->login] > 0) {
|
||||
$this->maniaControl->getStatisticManager()->insertStat(self::STAT_ON_SHOOT, $player, $this->maniaControl->getServer()->index, $this->onShootArray[$player->login]);
|
||||
}
|
||||
unset($this->onShootArray[$player->login]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle stats on callbacks
|
||||
*
|
||||
* @param array $callback
|
||||
*/
|
||||
public function handleCallbacks(array $callback) {
|
||||
//TODO survivals
|
||||
// Check if Stat Collecting is enabled
|
||||
if (!$this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_COLLECT_STATS_ENABLED)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for Minimum PlayerCount
|
||||
if ($this->maniaControl->getPlayerManager()->getPlayerCount() < $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_COLLECT_STATS_MINPLAYERS)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$callbackName = $callback[1][0];
|
||||
|
||||
switch ($callbackName) {
|
||||
case 'LibXmlRpc_OnShoot':
|
||||
$this->handleOnShoot($callback[1][1][0], $callback[1][1][1]);
|
||||
break;
|
||||
case 'LibXmlRpc_OnHit':
|
||||
$shooter = $this->maniaControl->getPlayerManager()->getPlayer($callback[1][1][0]);
|
||||
$victim = $this->maniaControl->getPlayerManager()->getPlayer($callback[1][1][1]);
|
||||
$weapon = $callback[1][1][3];
|
||||
if ($shooter) {
|
||||
$this->maniaControl->getStatisticManager()->incrementStat($this->getWeaponStat(intval($weapon), false), $shooter);
|
||||
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_HIT, $shooter);
|
||||
}
|
||||
if ($victim) {
|
||||
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_GOT_HIT, $victim);
|
||||
}
|
||||
break;
|
||||
case 'LibXmlRpc_OnNearMiss':
|
||||
$player = $this->maniaControl->getPlayerManager()->getPlayer($callback[1][1][0]);
|
||||
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_NEARMISS, $player);
|
||||
break;
|
||||
case 'LibXmlRpc_OnCapture':
|
||||
$logins = $callback[1][1][0];
|
||||
$logins = explode(';', $logins);
|
||||
foreach ($logins as $login) {
|
||||
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
|
||||
if (!$player) {
|
||||
continue;
|
||||
}
|
||||
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_CAPTURE, $player);
|
||||
}
|
||||
break;
|
||||
case 'LibXmlRpc_OnArmorEmpty':
|
||||
$victim = $this->maniaControl->getPlayerManager()->getPlayer($callback[1][1][1]);
|
||||
if (isset($callback[1][1][0])) {
|
||||
$shooter = $this->maniaControl->getPlayerManager()->getPlayer($callback[1][1][0]);
|
||||
if ($shooter) {
|
||||
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_KILL, $shooter);
|
||||
}
|
||||
}
|
||||
if ($victim) {
|
||||
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_DEATH, $victim);
|
||||
}
|
||||
break;
|
||||
case 'LibXmlRpc_OnPlayerRequestRespawn':
|
||||
$player = $this->maniaControl->getPlayerManager()->getPlayer($callback[1][1][0]);
|
||||
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_PLAYER_REQUEST_RESPAWN, $player);
|
||||
break;
|
||||
case 'OnShoot':
|
||||
$paramsObject = json_decode($callback[1][1]);
|
||||
if ($paramsObject && isset($paramsObject->Event)) {
|
||||
$this->handleOnShoot($paramsObject->Event->Shooter->Login, $paramsObject->Event->WeaponNum);
|
||||
}
|
||||
break;
|
||||
case 'OnNearMiss':
|
||||
$paramsObject = json_decode($callback[1][1]);
|
||||
if ($paramsObject && isset($paramsObject->Event)) {
|
||||
$player = $this->maniaControl->getPlayerManager()->getPlayer($paramsObject->Event->Shooter->Login);
|
||||
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_NEARMISS, $player);
|
||||
}
|
||||
break;
|
||||
case 'OnCapture':
|
||||
$paramsObject = json_decode($callback[1][1]);
|
||||
if ($paramsObject && isset($paramsObject->Event)) {
|
||||
$player = $this->maniaControl->getPlayerManager()->getPlayer($paramsObject->Event->Player->Login);
|
||||
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_CAPTURE, $player);
|
||||
}
|
||||
break;
|
||||
case 'OnHit':
|
||||
$paramsObject = json_decode($callback[1][1]);
|
||||
if ($paramsObject && isset($paramsObject->Event)) {
|
||||
$weapon = (int)$paramsObject->Event->WeaponNum;
|
||||
if (isset($paramsObject->Event->Shooter)) {
|
||||
$shooter = $this->maniaControl->getPlayerManager()->getPlayer($paramsObject->Event->Shooter->Login);
|
||||
if ($shooter) {
|
||||
$this->maniaControl->getStatisticManager()->incrementStat($this->getWeaponStat($weapon, false), $shooter);
|
||||
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_HIT, $shooter);
|
||||
}
|
||||
}
|
||||
if (isset($paramsObject->Event->Victim)) {
|
||||
$victim = $this->maniaControl->getPlayerManager()->getPlayer($paramsObject->Event->Victim->Login);
|
||||
if ($victim) {
|
||||
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_GOT_HIT, $victim);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'OnArmorEmpty':
|
||||
$paramsObject = json_decode($callback[1][1]);
|
||||
if ($paramsObject && isset($paramsObject->Event)) {
|
||||
$victim = $this->maniaControl->getPlayerManager()->getPlayer($paramsObject->Event->Victim->Login);
|
||||
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_DEATH, $victim);
|
||||
if (isset($paramsObject->Event->Shooter->Login)) {
|
||||
$shooter = $this->maniaControl->getPlayerManager()->getPlayer($paramsObject->Event->Shooter->Login);
|
||||
if ($shooter) {
|
||||
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_KILL, $shooter);
|
||||
}
|
||||
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_KILL, $shooter);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'OnRequestRespawn':
|
||||
$paramsObject = json_decode($callback[1][1]);
|
||||
if ($paramsObject && isset($paramsObject->Event)) {
|
||||
$player = $this->maniaControl->getPlayerManager()->getPlayer($paramsObject->Event->Player->Login);
|
||||
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_PLAYER_REQUEST_RESPAWN, $player);
|
||||
}
|
||||
break;
|
||||
case 'EndTurn': //TODO make it for other modes working
|
||||
$paramsObject = json_decode($callback[1][1]);
|
||||
if ($paramsObject && is_array($paramsObject->ScoresTable)) {
|
||||
$durationTime = (int)(($paramsObject->EndTime - $paramsObject->StartTime) / 1000);
|
||||
foreach ($paramsObject->ScoresTable as $score) {
|
||||
$player = $this->maniaControl->getPlayerManager()->getPlayer($score->Login);
|
||||
$this->maniaControl->getStatisticManager()->insertStat(self::STAT_PLAYTIME, $player, -1, $durationTime);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle Player Shots
|
||||
*
|
||||
* @param string $login
|
||||
* @param int $weaponNumber
|
||||
*/
|
||||
private function handleOnShoot($login, $weaponNumber) {
|
||||
if (!isset($this->onShootArray[$login])) {
|
||||
$this->onShootArray[$login] = array(self::WEAPON_ROCKET => 0, self::WEAPON_ARROW => 0, self::WEAPON_NUCLEUS => 0, self::WEAPON_LASER => 0);
|
||||
}
|
||||
if (!isset($this->onShootArray[$login][$weaponNumber])) {
|
||||
$this->onShootArray[$login][$weaponNumber] = 0;
|
||||
}
|
||||
$this->onShootArray[$login][$weaponNumber]++;
|
||||
|
||||
//Write Shoot Data into database
|
||||
if (array_sum($this->onShootArray[$login]) > $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_ON_SHOOT_PRESTORE)
|
||||
) {
|
||||
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
|
||||
|
||||
$rocketShots = $this->onShootArray[$login][self::WEAPON_ROCKET];
|
||||
$laserShots = $this->onShootArray[$login][self::WEAPON_LASER];
|
||||
$arrowShots = $this->onShootArray[$login][self::WEAPON_ARROW];
|
||||
$nucleusShots = $this->onShootArray[$login][self::WEAPON_NUCLEUS];
|
||||
|
||||
if ($rocketShots > 0) {
|
||||
$this->maniaControl->getStatisticManager()->insertStat(self::STAT_ROCKET_SHOT, $player, $this->maniaControl->getServer()->index, $rocketShots);
|
||||
$this->onShootArray[$login][self::WEAPON_ROCKET] = 0;
|
||||
}
|
||||
if ($laserShots > 0) {
|
||||
$this->maniaControl->getStatisticManager()->insertStat(self::STAT_LASER_SHOT, $player, $this->maniaControl->getServer()->index, $laserShots);
|
||||
$this->onShootArray[$login][self::WEAPON_LASER] = 0;
|
||||
}
|
||||
if ($arrowShots > 0) {
|
||||
$this->maniaControl->getStatisticManager()->insertStat(self::STAT_ARROW_SHOT, $player, $this->maniaControl->getServer()->index, $arrowShots);
|
||||
$this->onShootArray[$login][self::WEAPON_ARROW] = 0;
|
||||
}
|
||||
if ($nucleusShots > 0) {
|
||||
$this->maniaControl->getStatisticManager()->insertStat(self::STAT_NUCLEUS_SHOT, $player, $this->maniaControl->getServer()->index, $nucleusShots);
|
||||
$this->onShootArray[$login][self::WEAPON_NUCLEUS] = 0;
|
||||
}
|
||||
|
||||
$this->maniaControl->getStatisticManager()->insertStat(self::STAT_ON_SHOOT, $player, $this->maniaControl->getServer()->index, $rocketShots + $laserShots + $arrowShots + $nucleusShots);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Weapon stat
|
||||
*
|
||||
* @param int $weaponNumber
|
||||
* @param bool $shot
|
||||
* @return string
|
||||
*/
|
||||
private function getWeaponStat($weaponNumber, $shot = true) {
|
||||
if ($shot) {
|
||||
switch ($weaponNumber) {
|
||||
case self::WEAPON_ROCKET:
|
||||
return self::STAT_ROCKET_SHOT;
|
||||
case self::WEAPON_LASER:
|
||||
return self::STAT_LASER_SHOT;
|
||||
case self::WEAPON_ARROW:
|
||||
return self::STAT_ARROW_SHOT;
|
||||
case self::WEAPON_NUCLEUS:
|
||||
return self::STAT_NUCLEUS_SHOT;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
switch ($weaponNumber) {
|
||||
case self::WEAPON_ROCKET:
|
||||
return self::STAT_ROCKET_HIT;
|
||||
case self::WEAPON_LASER:
|
||||
return self::STAT_LASER_HIT;
|
||||
case self::WEAPON_ARROW:
|
||||
return self::STAT_ARROW_HIT;
|
||||
case self::WEAPON_NUCLEUS:
|
||||
return self::STAT_NUCLEUS_HIT;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
626
core/Statistics/StatisticManager.php
Normal file
626
core/Statistics/StatisticManager.php
Normal file
@ -0,0 +1,626 @@
|
||||
<?php
|
||||
|
||||
namespace ManiaControl\Statistics;
|
||||
|
||||
use ManiaControl\ManiaControl;
|
||||
use ManiaControl\Players\Player;
|
||||
|
||||
/**
|
||||
* Statistic Manager Class
|
||||
*
|
||||
* @author ManiaControl Team <mail@maniacontrol.com>
|
||||
* @copyright 2014 ManiaControl Team
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
class StatisticManager {
|
||||
/*
|
||||
* Constants
|
||||
*/
|
||||
const TABLE_STATMETADATA = 'mc_statmetadata';
|
||||
const TABLE_STATISTICS = 'mc_statistics';
|
||||
const STAT_TYPE_INT = '0';
|
||||
const STAT_TYPE_TIME = '1';
|
||||
const STAT_TYPE_FLOAT = '2';
|
||||
|
||||
const SPECIAL_STAT_KD_RATIO = 'Kill Death Ratio'; //TODO dynamic later
|
||||
const SPECIAL_STAT_HITS_PH = 'Hits Per Hour';
|
||||
const SPECIAL_STAT_LASER_ACC = 'Laser Accuracy';
|
||||
const SPECIAL_STAT_NUCLEUS_ACC = 'Nucleus Accuracy';
|
||||
const SPECIAL_STAT_ROCKET_ACC = 'Rocket Accuracy';
|
||||
const SPECIAL_STAT_ARROW_ACC = 'Arrow Accuracy';
|
||||
|
||||
/*
|
||||
* Public properties
|
||||
*/
|
||||
/** @var StatisticCollector $statisticCollector */
|
||||
/** @deprecated see getStatisticCollector() */
|
||||
public $statisticCollector = null;
|
||||
/** @var SimpleStatsList $simpleStatsList */
|
||||
/** @deprecated see getSimpleStatsList() */
|
||||
public $simpleStatsList = null;
|
||||
|
||||
/*
|
||||
* Private properties
|
||||
*/
|
||||
/** @var ManiaControl $maniaControl */
|
||||
private $maniaControl = null;
|
||||
private $stats = array();
|
||||
private $specialStats = array();
|
||||
|
||||
/**
|
||||
* Construct a new statistic manager instance
|
||||
*
|
||||
* @param ManiaControl $maniaControl
|
||||
*/
|
||||
public function __construct(ManiaControl $maniaControl) {
|
||||
$this->maniaControl = $maniaControl;
|
||||
$this->initTables();
|
||||
|
||||
$this->statisticCollector = new StatisticCollector($maniaControl);
|
||||
$this->simpleStatsList = new SimpleStatsList($maniaControl);
|
||||
|
||||
// Store Stats MetaData
|
||||
$this->storeStatMetaData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize necessary database tables
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function initTables() {
|
||||
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
||||
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_STATMETADATA . "` (
|
||||
`index` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(100) NOT NULL,
|
||||
`type` int(5) NOT NULL,
|
||||
`description` varchar(150) NOT NULL,
|
||||
PRIMARY KEY (`index`),
|
||||
UNIQUE KEY `name` (`name`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics Meta Data' AUTO_INCREMENT=1;";
|
||||
$statement = $mysqli->prepare($query);
|
||||
if ($mysqli->error) {
|
||||
trigger_error($mysqli->error, E_USER_ERROR);
|
||||
return false;
|
||||
}
|
||||
$statement->execute();
|
||||
if ($statement->error) {
|
||||
trigger_error($statement->error, E_USER_ERROR);
|
||||
return false;
|
||||
}
|
||||
$statement->close();
|
||||
|
||||
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_STATISTICS . "` (
|
||||
`index` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`serverIndex` int(11) NOT NULL,
|
||||
`playerId` int(11) NOT NULL,
|
||||
`statId` int(11) NOT NULL,
|
||||
`value` int(20) NOT NULL DEFAULT '0',
|
||||
PRIMARY KEY (`index`),
|
||||
UNIQUE KEY `unique` (`statId`,`playerId`,`serverIndex`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics' AUTO_INCREMENT=1;";
|
||||
$statement = $mysqli->prepare($query);
|
||||
if ($mysqli->error) {
|
||||
trigger_error($mysqli->error, E_USER_ERROR);
|
||||
return false;
|
||||
}
|
||||
$statement->execute();
|
||||
if ($statement->error) {
|
||||
trigger_error($statement->error, E_USER_ERROR);
|
||||
return false;
|
||||
}
|
||||
$statement->close();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store Stats Meta Data from the Database
|
||||
*/
|
||||
private function storeStatMetaData() {
|
||||
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
||||
|
||||
$query = "SELECT * FROM `" . self::TABLE_STATMETADATA . "`;";
|
||||
$result = $mysqli->query($query);
|
||||
if (!$result) {
|
||||
trigger_error($mysqli->error);
|
||||
return;
|
||||
}
|
||||
|
||||
while ($row = $result->fetch_object()) {
|
||||
$this->stats[$row->name] = $row;
|
||||
}
|
||||
$result->free();
|
||||
|
||||
// TODO: own model class
|
||||
|
||||
//Define Special Stat Kill / Death Ratio
|
||||
$stat = new \stdClass();
|
||||
$stat->name = self::SPECIAL_STAT_KD_RATIO;
|
||||
$stat->type = self::STAT_TYPE_FLOAT;
|
||||
$this->specialStats[self::SPECIAL_STAT_KD_RATIO] = $stat;
|
||||
|
||||
//Hits Per Hour
|
||||
$stat = new \stdClass();
|
||||
$stat->name = self::SPECIAL_STAT_HITS_PH;
|
||||
$stat->type = self::STAT_TYPE_FLOAT;
|
||||
$this->specialStats[self::SPECIAL_STAT_HITS_PH] = $stat;
|
||||
|
||||
//Laser Accuracy
|
||||
$stat = new \stdClass();
|
||||
$stat->name = self::SPECIAL_STAT_LASER_ACC;
|
||||
$stat->type = self::STAT_TYPE_FLOAT;
|
||||
$this->specialStats[self::SPECIAL_STAT_LASER_ACC] = $stat;
|
||||
|
||||
//Nucleus Accuracy
|
||||
$stat = new \stdClass();
|
||||
$stat->name = self::SPECIAL_STAT_NUCLEUS_ACC;
|
||||
$stat->type = self::STAT_TYPE_FLOAT;
|
||||
$this->specialStats[self::SPECIAL_STAT_NUCLEUS_ACC] = $stat;
|
||||
|
||||
//Arrow Accuracy
|
||||
$stat = new \stdClass();
|
||||
$stat->name = self::SPECIAL_STAT_ARROW_ACC;
|
||||
$stat->type = self::STAT_TYPE_FLOAT;
|
||||
$this->specialStats[self::SPECIAL_STAT_ARROW_ACC] = $stat;
|
||||
|
||||
//Rocket Accuracy
|
||||
$stat = new \stdClass();
|
||||
$stat->name = self::SPECIAL_STAT_ROCKET_ACC;
|
||||
$stat->type = self::STAT_TYPE_FLOAT;
|
||||
$this->specialStats[self::SPECIAL_STAT_ROCKET_ACC] = $stat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the statistic collector
|
||||
*
|
||||
* @return StatisticCollector
|
||||
*/
|
||||
public function getStatisticCollector() {
|
||||
return $this->statisticCollector;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the simple stats list
|
||||
*
|
||||
* @return SimpleStatsList
|
||||
*/
|
||||
public function getSimpleStatsList() {
|
||||
return $this->simpleStatsList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get All statistics ordered by an given name
|
||||
*
|
||||
* @param string $statName
|
||||
* @param $serverIndex
|
||||
* @param $minValue
|
||||
* @internal param $orderedBy
|
||||
* @return array
|
||||
*/
|
||||
public function getStatsRanking($statName = '', $serverIndex = -1, $minValue = -1) {
|
||||
if (isset($this->specialStats[$statName])) {
|
||||
return $this->getStatsRankingOfSpecialStat($statName, $serverIndex);
|
||||
}
|
||||
|
||||
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
||||
$statId = $this->getStatId($statName);
|
||||
|
||||
$query = "SELECT `playerId`, `serverIndex`, `value` FROM `" . self::TABLE_STATISTICS . "`
|
||||
WHERE `statId` = {$statId} ";
|
||||
if ($minValue >= 0) {
|
||||
$query .= "AND `value` >= {$minValue} ";
|
||||
}
|
||||
$query .= "ORDER BY `value` DESC;";
|
||||
|
||||
$result = $mysqli->query($query);
|
||||
if (!$result) {
|
||||
trigger_error($mysqli->error);
|
||||
return null;
|
||||
}
|
||||
|
||||
$stats = array();
|
||||
while ($row = $result->fetch_object()) {
|
||||
if ($serverIndex < 0) {
|
||||
if (!isset($stats[$row->playerId])) {
|
||||
$stats[$row->playerId] = $row->value;
|
||||
} else {
|
||||
$stats[$row->playerId] += $row->value;
|
||||
}
|
||||
} else if ($serverIndex == $row->serverIndex) {
|
||||
$stats[$row->playerId] = $row->value;
|
||||
}
|
||||
}
|
||||
$result->free();
|
||||
|
||||
arsort($stats);
|
||||
return $stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets The Ranking of an Special Stat
|
||||
*
|
||||
* @param string $statName
|
||||
* @param $serverIndex
|
||||
* @return array
|
||||
*/
|
||||
public function getStatsRankingOfSpecialStat($statName = '', $serverIndex = -1) {
|
||||
$statsArray = array();
|
||||
switch ($statName) {
|
||||
case self::SPECIAL_STAT_KD_RATIO:
|
||||
$kills = $this->getStatsRanking(StatisticCollector::STAT_ON_KILL, $serverIndex);
|
||||
$deaths = $this->getStatsRanking(StatisticCollector::STAT_ON_DEATH, $serverIndex);
|
||||
if (!$kills || !$deaths) {
|
||||
return array();
|
||||
}
|
||||
foreach ($deaths as $key => $death) {
|
||||
if (!$death || !isset($kills[$key])) {
|
||||
continue;
|
||||
}
|
||||
$statsArray[$key] = intval($kills[$key]) / intval($death);
|
||||
}
|
||||
arsort($statsArray);
|
||||
break;
|
||||
case self::SPECIAL_STAT_HITS_PH:
|
||||
$hits = $this->getStatsRanking(StatisticCollector::STAT_ON_HIT, $serverIndex);
|
||||
$times = $this->getStatsRanking(StatisticCollector::STAT_PLAYTIME, $serverIndex);
|
||||
if (!$hits || !$times) {
|
||||
return array();
|
||||
}
|
||||
foreach ($times as $key => $time) {
|
||||
if (!$time || !isset($hits[$key])) {
|
||||
continue;
|
||||
}
|
||||
$statsArray[$key] = intval($hits[$key]) / (intval($time) / 3600);
|
||||
}
|
||||
arsort($statsArray);
|
||||
break;
|
||||
case self::SPECIAL_STAT_ARROW_ACC:
|
||||
$hits = $this->getStatsRanking(StatisticCollector::STAT_ARROW_HIT, $serverIndex);
|
||||
$shots = $this->getStatsRanking(StatisticCollector::STAT_ARROW_SHOT, $serverIndex);
|
||||
if (!$hits || !$shots) {
|
||||
return array();
|
||||
}
|
||||
foreach ($shots as $key => $shot) {
|
||||
if (!$shot || !isset($hits[$key])) {
|
||||
continue;
|
||||
}
|
||||
$statsArray[$key] = intval($hits[$key]) / (intval($shot));
|
||||
}
|
||||
arsort($statsArray);
|
||||
break;
|
||||
case self::SPECIAL_STAT_LASER_ACC:
|
||||
$hits = $this->getStatsRanking(StatisticCollector::STAT_LASER_HIT, $serverIndex);
|
||||
$shots = $this->getStatsRanking(StatisticCollector::STAT_LASER_SHOT, $serverIndex);
|
||||
if (!$hits || !$shots) {
|
||||
return array();
|
||||
}
|
||||
foreach ($shots as $key => $shot) {
|
||||
if (!$shot || !isset($hits[$key])) {
|
||||
continue;
|
||||
}
|
||||
$statsArray[$key] = intval($hits[$key]) / (intval($shot));
|
||||
}
|
||||
arsort($statsArray);
|
||||
break;
|
||||
case self::SPECIAL_STAT_ROCKET_ACC:
|
||||
$hits = $this->getStatsRanking(StatisticCollector::STAT_ROCKET_HIT, $serverIndex);
|
||||
$shots = $this->getStatsRanking(StatisticCollector::STAT_ROCKET_SHOT, $serverIndex);
|
||||
if (!$hits || !$shots) {
|
||||
return array();
|
||||
}
|
||||
foreach ($shots as $key => $shot) {
|
||||
if (!$shot || !isset($hits[$key])) {
|
||||
continue;
|
||||
}
|
||||
$statsArray[$key] = intval($hits[$key]) / (intval($shot));
|
||||
}
|
||||
arsort($statsArray);
|
||||
break;
|
||||
case self::SPECIAL_STAT_NUCLEUS_ACC:
|
||||
$hits = $this->getStatsRanking(StatisticCollector::STAT_NUCLEUS_HIT, $serverIndex);
|
||||
$shots = $this->getStatsRanking(StatisticCollector::STAT_NUCLEUS_SHOT, $serverIndex);
|
||||
if (!$hits || !$shots) {
|
||||
return array();
|
||||
}
|
||||
foreach ($shots as $key => $shot) {
|
||||
if (!$shot || !isset($hits[$key])) {
|
||||
continue;
|
||||
}
|
||||
$statsArray[$key] = intval($hits[$key]) / (intval($shot));
|
||||
}
|
||||
arsort($statsArray);
|
||||
break;
|
||||
}
|
||||
return $statsArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Stat Id
|
||||
*
|
||||
* @param string $statName
|
||||
* @return int
|
||||
*/
|
||||
private function getStatId($statName) {
|
||||
if (isset($this->stats[$statName])) {
|
||||
$stat = $this->stats[$statName];
|
||||
return (int)$stat->index;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all statistics of a certain player
|
||||
*
|
||||
* @param Player $player
|
||||
* @param int $serverIndex
|
||||
* @return array
|
||||
*/
|
||||
public function getAllPlayerStats(Player $player, $serverIndex = -1) {
|
||||
// TODO improve performance of the foreach
|
||||
$playerStats = array();
|
||||
foreach ($this->stats as $stat) {
|
||||
$value = $this->getStatisticData($stat->name, $player->index, $serverIndex);
|
||||
$playerStats[$stat->name] = array($stat, $value);
|
||||
}
|
||||
|
||||
foreach ($this->specialStats as $stat) {
|
||||
switch ($stat->name) {
|
||||
case self::SPECIAL_STAT_KD_RATIO:
|
||||
if (!isset($playerStats[StatisticCollector::STAT_ON_KILL]) || !isset($playerStats[StatisticCollector::STAT_ON_DEATH])) {
|
||||
continue;
|
||||
}
|
||||
$kills = intval($playerStats[StatisticCollector::STAT_ON_KILL][1]);
|
||||
$deaths = intval($playerStats[StatisticCollector::STAT_ON_DEATH][1]);
|
||||
if (!$deaths) {
|
||||
continue;
|
||||
}
|
||||
$playerStats[$stat->name] = array($stat, $kills / $deaths);
|
||||
break;
|
||||
case self::SPECIAL_STAT_HITS_PH:
|
||||
if (!isset($playerStats[StatisticCollector::STAT_PLAYTIME]) || !isset($playerStats[StatisticCollector::STAT_ON_HIT])) {
|
||||
continue;
|
||||
}
|
||||
$hits = intval($playerStats[StatisticCollector::STAT_ON_HIT][1]);
|
||||
$time = intval($playerStats[StatisticCollector::STAT_PLAYTIME][1]);
|
||||
if (!$time) {
|
||||
continue;
|
||||
}
|
||||
$playerStats[$stat->name] = array($stat, $hits / ($time / 3600));
|
||||
break;
|
||||
case self::SPECIAL_STAT_ARROW_ACC:
|
||||
if (!isset($playerStats[StatisticCollector::STAT_ARROW_HIT]) || !isset($playerStats[StatisticCollector::STAT_ARROW_SHOT])) {
|
||||
continue;
|
||||
}
|
||||
$hits = intval($playerStats[StatisticCollector::STAT_ARROW_HIT][1]);
|
||||
$shots = intval($playerStats[StatisticCollector::STAT_ARROW_SHOT][1]);
|
||||
if (!$shots) {
|
||||
continue;
|
||||
}
|
||||
$playerStats[$stat->name] = array($stat, $hits / $shots);
|
||||
break;
|
||||
case self::SPECIAL_STAT_LASER_ACC:
|
||||
if (!isset($playerStats[StatisticCollector::STAT_LASER_HIT]) || !isset($playerStats[StatisticCollector::STAT_LASER_SHOT])) {
|
||||
continue;
|
||||
}
|
||||
$hits = intval($playerStats[StatisticCollector::STAT_LASER_HIT][1]);
|
||||
$shots = intval($playerStats[StatisticCollector::STAT_LASER_SHOT][1]);
|
||||
if (!$shots) {
|
||||
continue;
|
||||
}
|
||||
$playerStats[$stat->name] = array($stat, $hits / $shots);
|
||||
break;
|
||||
case self::SPECIAL_STAT_ROCKET_ACC:
|
||||
if (!isset($playerStats[StatisticCollector::STAT_ROCKET_HIT]) || !isset($playerStats[StatisticCollector::STAT_ROCKET_SHOT])) {
|
||||
continue;
|
||||
}
|
||||
$hits = intval($playerStats[StatisticCollector::STAT_ROCKET_HIT][1]);
|
||||
$shots = intval($playerStats[StatisticCollector::STAT_ROCKET_SHOT][1]);
|
||||
if (!$shots) {
|
||||
continue;
|
||||
}
|
||||
$playerStats[$stat->name] = array($stat, $hits / $shots);
|
||||
break;
|
||||
case self::SPECIAL_STAT_NUCLEUS_ACC:
|
||||
if (!isset($playerStats[StatisticCollector::STAT_NUCLEUS_HIT]) || !isset($playerStats[StatisticCollector::STAT_NUCLEUS_SHOT])) {
|
||||
continue;
|
||||
}
|
||||
$hits = intval($playerStats[StatisticCollector::STAT_NUCLEUS_HIT][1]);
|
||||
$shots = intval($playerStats[StatisticCollector::STAT_NUCLEUS_SHOT][1]);
|
||||
if (!$shots) {
|
||||
continue;
|
||||
}
|
||||
$playerStats[$stat->name] = array($stat, (float)($hits / $shots));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $playerStats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of an statistic
|
||||
*
|
||||
* @param $statName
|
||||
* @param $playerId
|
||||
* @param int $serverIndex
|
||||
* @return int
|
||||
*/
|
||||
public function getStatisticData($statName, $playerId, $serverIndex = -1) {
|
||||
//Handle Special Stats
|
||||
switch ($statName) {
|
||||
case self::SPECIAL_STAT_KD_RATIO:
|
||||
$kills = $this->getStatisticData(StatisticCollector::STAT_ON_KILL, $playerId, $serverIndex);
|
||||
$deaths = $this->getStatisticData(StatisticCollector::STAT_ON_DEATH, $playerId, $serverIndex);
|
||||
if (!$deaths) {
|
||||
return -1;
|
||||
}
|
||||
return intval($kills) / intval($deaths);
|
||||
case self::SPECIAL_STAT_HITS_PH:
|
||||
$hits = $this->getStatisticData(StatisticCollector::STAT_ON_HIT, $playerId, $serverIndex);
|
||||
$time = $this->getStatisticData(StatisticCollector::STAT_PLAYTIME, $playerId, $serverIndex);
|
||||
if (!$time) {
|
||||
return -1;
|
||||
}
|
||||
return intval($hits) / (intval($time) / 3600);
|
||||
case self::SPECIAL_STAT_ARROW_ACC:
|
||||
$hits = $this->getStatisticData(StatisticCollector::STAT_ARROW_HIT, $playerId, $serverIndex);
|
||||
$shots = $this->getStatisticData(StatisticCollector::STAT_ARROW_SHOT, $playerId, $serverIndex);
|
||||
if (!$shots) {
|
||||
return -1;
|
||||
}
|
||||
return intval($hits) / intval($shots);
|
||||
case self::SPECIAL_STAT_LASER_ACC:
|
||||
$hits = $this->getStatisticData(StatisticCollector::STAT_LASER_HIT, $playerId, $serverIndex);
|
||||
$shots = $this->getStatisticData(StatisticCollector::STAT_LASER_SHOT, $playerId, $serverIndex);
|
||||
if (!$shots) {
|
||||
return -1;
|
||||
}
|
||||
return intval($hits) / intval($shots);
|
||||
case self::SPECIAL_STAT_NUCLEUS_ACC:
|
||||
$hits = $this->getStatisticData(StatisticCollector::STAT_NUCLEUS_HIT, $playerId, $serverIndex);
|
||||
$shots = $this->getStatisticData(StatisticCollector::STAT_NUCLEUS_SHOT, $playerId, $serverIndex);
|
||||
if (!$shots) {
|
||||
return -1;
|
||||
}
|
||||
return intval($hits) / intval($shots);
|
||||
case self::SPECIAL_STAT_ROCKET_ACC:
|
||||
$hits = $this->getStatisticData(StatisticCollector::STAT_ROCKET_HIT, $playerId, $serverIndex);
|
||||
$shots = $this->getStatisticData(StatisticCollector::STAT_ROCKET_SHOT, $playerId, $serverIndex);
|
||||
if (!$shots) {
|
||||
return -1;
|
||||
}
|
||||
return intval($hits) / intval($shots);
|
||||
}
|
||||
|
||||
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
||||
$statId = $this->getStatId($statName);
|
||||
|
||||
if (!$statId) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ($serverIndex < 0) {
|
||||
$query = "SELECT SUM(`value`) as `value` FROM `" . self::TABLE_STATISTICS . "`
|
||||
WHERE `statId` = {$statId}
|
||||
AND `playerId` = {$playerId};";
|
||||
} else {
|
||||
$query = "SELECT `value` FROM `" . self::TABLE_STATISTICS . "`
|
||||
WHERE `statId` = {$statId}
|
||||
AND `playerId` = {$playerId}
|
||||
AND `serverIndex` = {$serverIndex};";
|
||||
}
|
||||
|
||||
$result = $mysqli->query($query);
|
||||
if (!$result) {
|
||||
trigger_error($mysqli->error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
$row = $result->fetch_object();
|
||||
|
||||
$result->free();
|
||||
return $row->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments a Statistic by one
|
||||
*
|
||||
* @param string $statName
|
||||
* @param Player $player
|
||||
* @param int $serverIndex
|
||||
* @return bool
|
||||
*/
|
||||
public function incrementStat($statName, Player $player, $serverIndex = -1) {
|
||||
return $this->insertStat($statName, $player, $serverIndex, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a Stat into the database
|
||||
*
|
||||
* @param string $statName
|
||||
* @param Player $player
|
||||
* @param int $serverIndex
|
||||
* @param mixed $value , value to Add
|
||||
* @param string $statType
|
||||
* @return bool
|
||||
*/
|
||||
public function insertStat($statName, Player $player, $serverIndex = -1, $value, $statType = self::STAT_TYPE_INT) {
|
||||
// TODO: statType isn't used
|
||||
if (!$player) {
|
||||
return false;
|
||||
}
|
||||
if ($player->isFakePlayer()) {
|
||||
return true;
|
||||
}
|
||||
$statId = $this->getStatId($statName);
|
||||
if (!$statId) {
|
||||
return false;
|
||||
}
|
||||
if ($value < 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($serverIndex) {
|
||||
$serverIndex = $this->maniaControl->getServer()->index;
|
||||
}
|
||||
|
||||
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
||||
$query = "INSERT INTO `" . self::TABLE_STATISTICS . "` (
|
||||
`serverIndex`,
|
||||
`playerId`,
|
||||
`statId`,
|
||||
`value`
|
||||
) VALUES (
|
||||
?, ?, ?, ?
|
||||
) ON DUPLICATE KEY UPDATE
|
||||
`value` = `value` + VALUES(`value`);";
|
||||
$statement = $mysqli->prepare($query);
|
||||
if ($mysqli->error) {
|
||||
trigger_error($mysqli->error);
|
||||
return false;
|
||||
}
|
||||
$statement->bind_param('iiii', $serverIndex, $player->index, $statId, $value);
|
||||
$statement->execute();
|
||||
if ($statement->error) {
|
||||
trigger_error($statement->error);
|
||||
$statement->close();
|
||||
return false;
|
||||
}
|
||||
$statement->close();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines a Stat
|
||||
*
|
||||
* @param $statName
|
||||
* @param string $type
|
||||
* @param string $statDescription
|
||||
* @return bool
|
||||
*/
|
||||
public function defineStatMetaData($statName, $type = self::STAT_TYPE_INT, $statDescription = '') {
|
||||
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
||||
$query = "INSERT INTO `" . self::TABLE_STATMETADATA . "` (
|
||||
`name`,
|
||||
`type`,
|
||||
`description`
|
||||
) VALUES (
|
||||
?, ?, ?
|
||||
) ON DUPLICATE KEY UPDATE
|
||||
`type` = VALUES(`type`),
|
||||
`description` = VALUES(`description`);";
|
||||
$statement = $mysqli->prepare($query);
|
||||
if ($mysqli->error) {
|
||||
trigger_error($mysqli->error);
|
||||
return false;
|
||||
}
|
||||
$statement->bind_param('sis', $statName, $type, $statDescription);
|
||||
$statement->execute();
|
||||
if ($statement->error) {
|
||||
trigger_error($statement->error);
|
||||
$statement->close();
|
||||
return false;
|
||||
}
|
||||
$statement->close();
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user