From bd5e4b922efc2249f567ca7d80b18a7fa35483db Mon Sep 17 00:00:00 2001 From: kremsy Date: Fri, 31 Mar 2017 22:07:00 +0200 Subject: [PATCH] updated statisticcollector --- .../OnHitNearMissArmorEmptyBaseStructure.php | 2 +- core/Statistics/StatisticCollector.php | 189 ++++++++++++++---- .../DedicatedServer/Connection.php | 2 +- 3 files changed, 154 insertions(+), 39 deletions(-) diff --git a/core/Callbacks/Structures/ShootMania/OnHitNearMissArmorEmptyBaseStructure.php b/core/Callbacks/Structures/ShootMania/OnHitNearMissArmorEmptyBaseStructure.php index 3b7e24b9..d2786476 100644 --- a/core/Callbacks/Structures/ShootMania/OnHitNearMissArmorEmptyBaseStructure.php +++ b/core/Callbacks/Structures/ShootMania/OnHitNearMissArmorEmptyBaseStructure.php @@ -72,7 +72,7 @@ class OnHitNearMissArmorEmptyBaseStructure extends BaseStructure { * @return int */ public function getWeapon() { - return $this->weapon; + return intval($this->weapon); } /** diff --git a/core/Statistics/StatisticCollector.php b/core/Statistics/StatisticCollector.php index d368d1ad..b3701733 100644 --- a/core/Statistics/StatisticCollector.php +++ b/core/Statistics/StatisticCollector.php @@ -5,6 +5,13 @@ namespace ManiaControl\Statistics; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\Callbacks; +use ManiaControl\Callbacks\Structures\ShootMania\Models\Weapons; +use ManiaControl\Callbacks\Structures\ShootMania\OnArmorEmptyStructure; +use ManiaControl\Callbacks\Structures\ShootMania\OnCaptureStructure; +use ManiaControl\Callbacks\Structures\ShootMania\OnHitStructure; +use ManiaControl\Callbacks\Structures\ShootMania\OnNearMissStructure; +use ManiaControl\Callbacks\Structures\ShootMania\OnPlayerRequestRespawnStructure; +use ManiaControl\Callbacks\Structures\ShootMania\OnShootStructure; use ManiaControl\ManiaControl; use ManiaControl\Players\Player; use ManiaControl\Players\PlayerManager; @@ -16,7 +23,7 @@ use ManiaControl\Players\PlayerManager; * @copyright 2014-2017 ManiaControl Team * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 */ -class StatisticCollector implements CallbackListener { +class StatisticCollector implements CallbackListener { //TODO remove old callbacks later /* * Constants */ @@ -47,11 +54,6 @@ class StatisticCollector implements CallbackListener { 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 */ @@ -73,6 +75,11 @@ class StatisticCollector implements CallbackListener { $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONINIT, $this, 'onInit'); $this->maniaControl->getCallbackManager()->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECT, $this, 'onPlayerDisconnect'); + $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::SM_ONHIT, $this, 'onHitCallback'); + $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::SM_ONSHOOT, $this, 'onShootCallback'); + $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::SM_ONNEARMISS, $this, 'onNearMissCallback'); + $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::SM_ONCAPTURE, $this, 'onCaptureCallback'); + // Settings $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_COLLECT_STATS_ENABLED, true); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_COLLECT_STATS_MINPLAYERS, 4); @@ -104,6 +111,118 @@ class StatisticCollector implements CallbackListener { $this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_ARROW_SHOT); } + /** + * Checks if the Collecting is Enabled + * + * @return boolean + */ + public function isCollectingEnabled() { + return $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_COLLECT_STATS_ENABLED); + } + + + /** + * Check for the Minimum Amount of Players to collect + * + * @return bool + */ + public function checkForMinimumPlayers() { + // Check for Minimum PlayerCount + return ($this->maniaControl->getPlayerManager()->getPlayerCount() < $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_COLLECT_STATS_MINPLAYERS)); + } + + /** + * Handles the onHitCallback + * + * @param \ManiaControl\Callbacks\Structures\ShootMania\OnHitStructure $structure + */ + public function onHitCallback(OnHitStructure $structure) { + if (!$this->isCollectingEnabled() || !$this->checkForMinimumPlayers()) { + return; + } + + if ($structure->getShooter()) { + $this->maniaControl->getStatisticManager()->incrementStat($this->getWeaponStat($structure->getWeapon(), false), $structure->getShooter()); + $this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_HIT, $structure->getShooter()); + } + if ($structure->getVictim()) { + $this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_GOT_HIT, $structure->getVictim()); + } + } + + /** + * Handles the onShoot Callback + * + * @param \ManiaControl\Callbacks\Structures\ShootMania\OnShootStructure $structure + */ + public function onShootCallback(OnShootStructure $structure) { + if (!$this->isCollectingEnabled() || !$this->checkForMinimumPlayers()) { + return; + } + + $this->handleOnShoot($structure->getShooter()->login, $structure->getWeapon()); + } + + /** + * Handles the OnNearMiss Callback + * + * @param \ManiaControl\Callbacks\Structures\ShootMania\OnNearMissStructure $structure + */ + public function onNearMissCallback(OnNearMissStructure $structure) { + if (!$this->isCollectingEnabled() || !$this->checkForMinimumPlayers()) { + return; + } + + $this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_NEARMISS, $structure->getShooter()); + } + + + /** + * Handles the OnCapture Callback + * + * @param \ManiaControl\Callbacks\Structures\ShootMania\OnCaptureStructure $structure + */ + public function onCaptureCallback(OnCaptureStructure $structure) { + if (!$this->isCollectingEnabled() || !$this->checkForMinimumPlayers()) { + return; + } + + foreach ($structure->getPlayerArray() as $player) { + $this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_CAPTURE, $player); + } + } + + /** + * Handles the OnArmorEmpty Callback + * + * @param \ManiaControl\Callbacks\Structures\ShootMania\OnArmorEmptyStructure $structure + */ + public function onArmorEmptyCallback(OnArmorEmptyStructure $structure) { + if (!$this->isCollectingEnabled() || !$this->checkForMinimumPlayers()) { + return; + } + if ($structure->getShooter()) { + $this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_KILL, $structure->getShooter()); + } + if ($structure->getVictim()) { + $this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_DEATH, $structure->getVictim()); + } + } + + /** + * Handles the OnPlayerRequestRespawn Callback + * + * @param \ManiaControl\Callbacks\Structures\ShootMania\OnPlayerRequestRespawnStructure $structure + */ + public function onPlayerRequestRespawnCallback(OnPlayerRequestRespawnStructure $structure) { + if (!$this->isCollectingEnabled() || !$this->checkForMinimumPlayers()) { + return; + } + + $this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_PLAYER_REQUEST_RESPAWN, $structure->getPlayer()); + } + + /** * Handle EndMap * @@ -111,8 +230,7 @@ class StatisticCollector implements CallbackListener { */ public function onEndMap(array $callback) { //Check for Minimum PlayerCount - if ($this->maniaControl->getPlayerManager()->getPlayerCount() < $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_COLLECT_STATS_MINPLAYERS) - ) { + if ($this->maniaControl->getPlayerManager()->getPlayerCount() < $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_COLLECT_STATS_MINPLAYERS)) { return; } @@ -131,8 +249,7 @@ class StatisticCollector implements CallbackListener { */ public function onPlayerDisconnect(Player $player) { // Check if Stat Collecting is enabled - if (!$this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_COLLECT_STATS_ENABLED) - ) { + if (!$this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_COLLECT_STATS_ENABLED)) { return; } @@ -153,19 +270,18 @@ class StatisticCollector implements CallbackListener { public function handleCallbacks(array $callback) { //TODO survivals // Check if Stat Collecting is enabled - if (!$this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_COLLECT_STATS_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) - ) { + if ($this->maniaControl->getPlayerManager()->getPlayerCount() < $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_COLLECT_STATS_MINPLAYERS)) { return; } $callbackName = $callback[1][0]; + //TODO remove later switch ($callbackName) { case 'LibXmlRpc_OnShoot': $this->handleOnShoot($callback[1][1][0], $callback[1][1][1]); @@ -236,7 +352,7 @@ class StatisticCollector implements CallbackListener { case 'OnHit': $paramsObject = json_decode($callback[1][1]); if ($paramsObject && isset($paramsObject->Event)) { - $weapon = (int)$paramsObject->Event->WeaponNum; + $weapon = (int) $paramsObject->Event->WeaponNum; if (isset($paramsObject->Event->Shooter)) { $shooter = $this->maniaControl->getPlayerManager()->getPlayer($paramsObject->Event->Shooter->Login); if ($shooter) { @@ -262,7 +378,6 @@ class StatisticCollector implements CallbackListener { if ($shooter) { $this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_KILL, $shooter); } - $this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_KILL, $shooter); } } break; @@ -273,10 +388,10 @@ class StatisticCollector implements CallbackListener { $this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_PLAYER_REQUEST_RESPAWN, $player); } break; - case 'EndTurn': //TODO make it for other modes working + case 'EndTurn': //TODO make it for other modes working //TODO also not available in MP4 atm $paramsObject = json_decode($callback[1][1]); if ($paramsObject && is_array($paramsObject->ScoresTable)) { - $durationTime = (int)(($paramsObject->EndTime - $paramsObject->StartTime) / 1000); + $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); @@ -293,8 +408,9 @@ class StatisticCollector implements CallbackListener { * @param int $weaponNumber */ private function handleOnShoot($login, $weaponNumber) { + //TODO update to player in MP4 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); + $this->onShootArray[$login] = array(Weapons::ROCKET => 0, Weapons::ARROW => 0, Weapons::NUCLEUS => 0, Weapons::LASER => 0); } if (!isset($this->onShootArray[$login][$weaponNumber])) { $this->onShootArray[$login][$weaponNumber] = 0; @@ -302,30 +418,29 @@ class StatisticCollector implements CallbackListener { $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) - ) { + 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]; + $rocketShots = $this->onShootArray[$login][Weapons::ROCKET]; + $laserShots = $this->onShootArray[$login][Weapons::LASER]; + $arrowShots = $this->onShootArray[$login][Weapons::ARROW]; + $nucleusShots = $this->onShootArray[$login][Weapons::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; + $this->onShootArray[$login][Weapons::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; + $this->onShootArray[$login][Weapons::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; + $this->onShootArray[$login][Weapons::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->onShootArray[$login][Weapons::NUCLEUS] = 0; } $this->maniaControl->getStatisticManager()->insertStat(self::STAT_ON_SHOOT, $player, $this->maniaControl->getServer()->index, $rocketShots + $laserShots + $arrowShots + $nucleusShots); @@ -342,26 +457,26 @@ class StatisticCollector implements CallbackListener { private function getWeaponStat($weaponNumber, $shot = true) { if ($shot) { switch ($weaponNumber) { - case self::WEAPON_ROCKET: + case Weapons::ROCKET: return self::STAT_ROCKET_SHOT; - case self::WEAPON_LASER: + case Weapons::LASER: return self::STAT_LASER_SHOT; - case self::WEAPON_ARROW: + case Weapons::ARROW: return self::STAT_ARROW_SHOT; - case self::WEAPON_NUCLEUS: + case Weapons::NUCLEUS: return self::STAT_NUCLEUS_SHOT; default: return -1; } } else { switch ($weaponNumber) { - case self::WEAPON_ROCKET: + case Weapons::ROCKET: return self::STAT_ROCKET_HIT; - case self::WEAPON_LASER: + case Weapons::LASER: return self::STAT_LASER_HIT; - case self::WEAPON_ARROW: + case Weapons::ARROW: return self::STAT_ARROW_HIT; - case self::WEAPON_NUCLEUS: + case Weapons::NUCLEUS: return self::STAT_NUCLEUS_HIT; default: return -1; diff --git a/libs/Maniaplanet/DedicatedServer/Connection.php b/libs/Maniaplanet/DedicatedServer/Connection.php index c2a3d53e..00adf71b 100755 --- a/libs/Maniaplanet/DedicatedServer/Connection.php +++ b/libs/Maniaplanet/DedicatedServer/Connection.php @@ -763,7 +763,7 @@ class Connection return $this->execute(ucfirst(__FUNCTION__), array($link), $multicall); } - + /** * Kick the player with the specified login, with an optional message. * Only available to Admin.