updated statisticcollector

This commit is contained in:
kremsy 2017-03-31 22:07:00 +02:00
parent 7b6c6af73e
commit bd5e4b922e
3 changed files with 154 additions and 39 deletions

View File

@ -72,7 +72,7 @@ class OnHitNearMissArmorEmptyBaseStructure extends BaseStructure {
* @return int * @return int
*/ */
public function getWeapon() { public function getWeapon() {
return $this->weapon; return intval($this->weapon);
} }
/** /**

View File

@ -5,6 +5,13 @@ namespace ManiaControl\Statistics;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\Callbacks; 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\ManiaControl;
use ManiaControl\Players\Player; use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager; use ManiaControl\Players\PlayerManager;
@ -16,7 +23,7 @@ use ManiaControl\Players\PlayerManager;
* @copyright 2014-2017 ManiaControl Team * @copyright 2014-2017 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @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 * Constants
*/ */
@ -47,11 +54,6 @@ class StatisticCollector implements CallbackListener {
const SPECIAL_STAT_KILL_DEATH_RATIO = 'Kill / Death'; 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 * Private properties
*/ */
@ -73,6 +75,11 @@ class StatisticCollector implements CallbackListener {
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONINIT, $this, 'onInit'); $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONINIT, $this, 'onInit');
$this->maniaControl->getCallbackManager()->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECT, $this, 'onPlayerDisconnect'); $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 // Settings
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_COLLECT_STATS_ENABLED, true); $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_COLLECT_STATS_MINPLAYERS, 4);
@ -104,6 +111,118 @@ class StatisticCollector implements CallbackListener {
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_ARROW_SHOT); $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 * Handle EndMap
* *
@ -111,8 +230,7 @@ class StatisticCollector implements CallbackListener {
*/ */
public function onEndMap(array $callback) { public function onEndMap(array $callback) {
//Check for Minimum PlayerCount //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; return;
} }
@ -131,8 +249,7 @@ class StatisticCollector implements CallbackListener {
*/ */
public function onPlayerDisconnect(Player $player) { public function onPlayerDisconnect(Player $player) {
// Check if Stat Collecting is enabled // 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; return;
} }
@ -153,19 +270,18 @@ class StatisticCollector implements CallbackListener {
public function handleCallbacks(array $callback) { public function handleCallbacks(array $callback) {
//TODO survivals //TODO survivals
// Check if Stat Collecting is enabled // 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; return;
} }
// Check for Minimum PlayerCount // 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; return;
} }
$callbackName = $callback[1][0]; $callbackName = $callback[1][0];
//TODO remove later
switch ($callbackName) { switch ($callbackName) {
case 'LibXmlRpc_OnShoot': case 'LibXmlRpc_OnShoot':
$this->handleOnShoot($callback[1][1][0], $callback[1][1][1]); $this->handleOnShoot($callback[1][1][0], $callback[1][1][1]);
@ -262,7 +378,6 @@ class StatisticCollector implements CallbackListener {
if ($shooter) { if ($shooter) {
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_KILL, $shooter); $this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_KILL, $shooter);
} }
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_KILL, $shooter);
} }
} }
break; break;
@ -273,7 +388,7 @@ class StatisticCollector implements CallbackListener {
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_PLAYER_REQUEST_RESPAWN, $player); $this->maniaControl->getStatisticManager()->incrementStat(self::STAT_ON_PLAYER_REQUEST_RESPAWN, $player);
} }
break; 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]); $paramsObject = json_decode($callback[1][1]);
if ($paramsObject && is_array($paramsObject->ScoresTable)) { if ($paramsObject && is_array($paramsObject->ScoresTable)) {
$durationTime = (int) (($paramsObject->EndTime - $paramsObject->StartTime) / 1000); $durationTime = (int) (($paramsObject->EndTime - $paramsObject->StartTime) / 1000);
@ -293,8 +408,9 @@ class StatisticCollector implements CallbackListener {
* @param int $weaponNumber * @param int $weaponNumber
*/ */
private function handleOnShoot($login, $weaponNumber) { private function handleOnShoot($login, $weaponNumber) {
//TODO update to player in MP4
if (!isset($this->onShootArray[$login])) { 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])) { if (!isset($this->onShootArray[$login][$weaponNumber])) {
$this->onShootArray[$login][$weaponNumber] = 0; $this->onShootArray[$login][$weaponNumber] = 0;
@ -302,30 +418,29 @@ class StatisticCollector implements CallbackListener {
$this->onShootArray[$login][$weaponNumber]++; $this->onShootArray[$login][$weaponNumber]++;
//Write Shoot Data into database //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); $player = $this->maniaControl->getPlayerManager()->getPlayer($login);
$rocketShots = $this->onShootArray[$login][self::WEAPON_ROCKET]; $rocketShots = $this->onShootArray[$login][Weapons::ROCKET];
$laserShots = $this->onShootArray[$login][self::WEAPON_LASER]; $laserShots = $this->onShootArray[$login][Weapons::LASER];
$arrowShots = $this->onShootArray[$login][self::WEAPON_ARROW]; $arrowShots = $this->onShootArray[$login][Weapons::ARROW];
$nucleusShots = $this->onShootArray[$login][self::WEAPON_NUCLEUS]; $nucleusShots = $this->onShootArray[$login][Weapons::NUCLEUS];
if ($rocketShots > 0) { if ($rocketShots > 0) {
$this->maniaControl->getStatisticManager()->insertStat(self::STAT_ROCKET_SHOT, $player, $this->maniaControl->getServer()->index, $rocketShots); $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) { if ($laserShots > 0) {
$this->maniaControl->getStatisticManager()->insertStat(self::STAT_LASER_SHOT, $player, $this->maniaControl->getServer()->index, $laserShots); $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) { if ($arrowShots > 0) {
$this->maniaControl->getStatisticManager()->insertStat(self::STAT_ARROW_SHOT, $player, $this->maniaControl->getServer()->index, $arrowShots); $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) { if ($nucleusShots > 0) {
$this->maniaControl->getStatisticManager()->insertStat(self::STAT_NUCLEUS_SHOT, $player, $this->maniaControl->getServer()->index, $nucleusShots); $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); $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) { private function getWeaponStat($weaponNumber, $shot = true) {
if ($shot) { if ($shot) {
switch ($weaponNumber) { switch ($weaponNumber) {
case self::WEAPON_ROCKET: case Weapons::ROCKET:
return self::STAT_ROCKET_SHOT; return self::STAT_ROCKET_SHOT;
case self::WEAPON_LASER: case Weapons::LASER:
return self::STAT_LASER_SHOT; return self::STAT_LASER_SHOT;
case self::WEAPON_ARROW: case Weapons::ARROW:
return self::STAT_ARROW_SHOT; return self::STAT_ARROW_SHOT;
case self::WEAPON_NUCLEUS: case Weapons::NUCLEUS:
return self::STAT_NUCLEUS_SHOT; return self::STAT_NUCLEUS_SHOT;
default: default:
return -1; return -1;
} }
} else { } else {
switch ($weaponNumber) { switch ($weaponNumber) {
case self::WEAPON_ROCKET: case Weapons::ROCKET:
return self::STAT_ROCKET_HIT; return self::STAT_ROCKET_HIT;
case self::WEAPON_LASER: case Weapons::LASER:
return self::STAT_LASER_HIT; return self::STAT_LASER_HIT;
case self::WEAPON_ARROW: case Weapons::ARROW:
return self::STAT_ARROW_HIT; return self::STAT_ARROW_HIT;
case self::WEAPON_NUCLEUS: case Weapons::NUCLEUS:
return self::STAT_NUCLEUS_HIT; return self::STAT_NUCLEUS_HIT;
default: default:
return -1; return -1;