TrackManiaControl/application/core/Statistics/StatisticCollector.php

348 lines
14 KiB
PHP
Raw Normal View History

2013-12-31 21:41:52 +01:00
<?php
namespace ManiaControl\Statistics;
2013-12-31 21:41:52 +01:00
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\Callbacks;
2013-12-31 21:41:52 +01:00
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
2014-03-02 10:27:55 +01:00
use ManiaControl\Players\PlayerManager;
2013-12-31 21:41:52 +01:00
/**
* Statistic Collector Class
*
2014-05-02 17:50:30 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
2013-12-31 21:41:52 +01:00
class StatisticCollector implements CallbackListener {
/*
2013-12-31 21:41:52 +01:00
* Constants
*/
const SETTING_COLLECT_STATS_ENABLED = 'Collect Stats Enabled';
2014-05-13 18:26:38 +02:00
const SETTING_COLLECT_STATS_MINPLAYERS = 'Minimum Player Count for Collecting Stats';
const SETTING_ON_SHOOT_PRESTORE = 'Prestore Shots before Insert into Database';
2013-12-31 21:41:52 +01:00
/*
* Statistics
*/
2014-01-11 18:24:44 +01:00
const STAT_PLAYTIME = 'Play Time';
2014-02-06 21:18:25 +01:00
const STAT_MAP_WINS = 'Map Wins';
2014-01-04 00:11:12 +01:00
const STAT_ON_SHOOT = 'Shots';
2014-01-03 18:37:51 +01:00
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';
2014-01-28 22:27:33 +01:00
const STAT_LASER_HIT = 'Laser Hits';
const STAT_ROCKET_SHOT = 'Rocket Shots';
2014-01-28 22:27:33 +01:00
const STAT_ROCKET_HIT = 'Rocket Hits';
const STAT_ARROW_SHOT = 'Arrow Shots';
2014-01-28 22:27:33 +01:00
const STAT_ARROW_HIT = 'Arrow Hits';
const STAT_NUCLEUS_SHOT = 'Nucleus Shots';
2014-01-28 22:27:33 +01:00
const STAT_NUCLEUS_HIT = 'Nucleus Hits';
2014-01-03 19:14:07 +01:00
const SPECIAL_STAT_KILL_DEATH_RATIO = 'Kill / Death';
const WEAPON_LASER = 1;
const WEAPON_ROCKET = 2;
const WEAPON_NUCLEUS = 3;
const WEAPON_ARROW = 5;
/*
2013-12-31 21:41:52 +01:00
* Private Properties
*/
private $maniaControl = null;
private $onShootArray = array();
2014-01-02 13:35:52 +01:00
2013-12-31 21:41:52 +01:00
/**
* Construct player manager
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
2014-03-01 11:11:50 +01:00
public function __construct(ManiaControl $maniaControl) {
2013-12-31 21:41:52 +01:00
$this->maniaControl = $maniaControl;
//Register Callbacks
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_MODESCRIPTCALLBACK, $this, 'handleCallbacks');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_MODESCRIPTCALLBACKARRAY, $this, 'handleCallbacks');
$this->maniaControl->callbackManager->registerCallbackListener(Callbacks::ONINIT, $this, 'onInit');
2014-02-19 10:43:37 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECT, $this, 'onPlayerDisconnect');
2013-12-31 21:41:52 +01:00
//Initialize Settings
$this->maniaControl->settingManager->initSetting($this, self::SETTING_COLLECT_STATS_ENABLED, true);
2014-03-02 10:27:55 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_COLLECT_STATS_MINPLAYERS, 4);
2014-01-29 20:55:22 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_ON_SHOOT_PRESTORE, 10);
2014-01-01 18:27:56 +01:00
}
2013-12-31 21:41:52 +01:00
2014-01-01 18:27:56 +01:00
/**
* Handle ManiaControl OnInit Callback
2014-01-01 18:27:56 +01:00
*/
public function onInit() {
2013-12-31 21:41:52 +01:00
//Define Stats MetaData
2014-01-11 18:24:44 +01:00
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_PLAYTIME, StatisticManager::STAT_TYPE_TIME);
2014-02-06 21:18:25 +01:00
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_MAP_WINS);
2013-12-31 21:41:52 +01:00
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_ON_SHOOT);
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_ON_NEARMISS);
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_ON_CAPTURE);
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_ON_HIT);
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_ON_GOT_HIT);
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_ON_DEATH);
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_ON_PLAYER_REQUEST_RESPAWN);
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_ON_KILL);
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_LASER_HIT);
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_LASER_SHOT);
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_NUCLEUS_HIT);
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_NUCLEUS_SHOT);
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_ROCKET_HIT);
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_ROCKET_SHOT);
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_ARROW_HIT);
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_ARROW_SHOT);
2013-12-31 21:41:52 +01:00
}
2014-02-06 21:18:25 +01:00
/**
* Handle EndMap
*
* @param array $callback
*/
public function onEndMap(array $callback) {
//Check for Minimum PlayerCount
2014-05-27 22:44:22 +02:00
if ($this->maniaControl->playerManager->getPlayerCount() < $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_COLLECT_STATS_MINPLAYERS)) {
2014-02-06 21:18:25 +01:00
return;
}
$leaders = $this->maniaControl->server->rankingManager->getLeaders();
2014-05-02 17:50:30 +02:00
foreach ($leaders as $leaderLogin) {
2014-02-06 21:18:25 +01:00
$leader = $this->maniaControl->playerManager->getPlayer($leaderLogin);
$this->maniaControl->statisticManager->incrementStat(self::STAT_MAP_WINS, $leader);
}
}
/**
* Insert OnShoot Statistic when a player leaves
2014-01-02 13:35:52 +01:00
*
* @param Player $player
*/
public function onPlayerDisconnect(Player $player) {
// Check if Stat Collecting is enabled
2014-05-13 16:03:26 +02:00
if (!$this->maniaControl->settingManager->getSettingValue($this, self::SETTING_COLLECT_STATS_ENABLED)) {
return;
}
// Insert Data into Database, and destroy player
2014-01-28 22:27:33 +01:00
if (isset($this->onShootArray[$player->login])) {
if ($this->onShootArray[$player->login] > 0) {
$this->maniaControl->statisticManager->insertStat(self::STAT_ON_SHOOT, $player, $this->maniaControl->server->index, $this->onShootArray[$player->login]);
}
unset($this->onShootArray[$player->login]);
}
}
2013-12-31 21:41:52 +01:00
/**
* Handle stats on callbacks
*
2013-12-31 21:41:52 +01:00
* @param array $callback
*/
2014-05-13 16:03:26 +02:00
public function handleCallbacks(array $callback) {
//TODO survivals
2014-05-27 22:44:22 +02:00
// Check if Stat Collecting is enabled
2014-05-13 16:03:26 +02:00
if (!$this->maniaControl->settingManager->getSettingValue($this, self::SETTING_COLLECT_STATS_ENABLED)) {
2013-12-31 21:41:52 +01:00
return;
}
2014-05-27 22:44:22 +02:00
// Check for Minimum PlayerCount
if ($this->maniaControl->playerManager->getPlayerCount() < $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_COLLECT_STATS_MINPLAYERS)) {
2013-12-31 21:41:52 +01:00
return;
}
$callbackName = $callback[1][0];
2014-05-02 17:50:30 +02:00
switch ($callbackName) {
case 'LibXmlRpc_OnShoot':
2014-01-31 19:00:01 +01:00
$this->handleOnShoot($callback[1][1][0], $callback[1][1][1]);
break;
case 'LibXmlRpc_OnHit':
2014-01-01 20:11:58 +01:00
$shooter = $this->maniaControl->playerManager->getPlayer($callback[1][1][0]);
$victim = $this->maniaControl->playerManager->getPlayer($callback[1][1][1]);
2014-01-24 22:34:35 +01:00
$weapon = $callback[1][1][3];
2014-04-09 11:11:22 +02:00
if ($shooter) {
$this->maniaControl->statisticManager->incrementStat($this->getWeaponStat(intval($weapon), false), $shooter);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_HIT, $shooter);
}
if ($victim) {
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_GOT_HIT, $victim);
}
break;
case 'LibXmlRpc_OnNearMiss':
2014-01-01 20:11:58 +01:00
$player = $this->maniaControl->playerManager->getPlayer($callback[1][1][0]);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_NEARMISS, $player);
break;
case 'LibXmlRpc_OnCapture':
2014-01-05 19:15:27 +01:00
$logins = $callback[1][1][0];
$logins = explode(';', $logins);
2014-05-02 17:50:30 +02:00
foreach ($logins as $login) {
2014-01-05 19:15:27 +01:00
$player = $this->maniaControl->playerManager->getPlayer($login);
2014-01-28 22:27:33 +01:00
if (!$player) {
2014-01-11 18:24:44 +01:00
continue;
}
2014-01-05 19:15:27 +01:00
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_CAPTURE, $player);
}
break;
case 'LibXmlRpc_OnArmorEmpty':
2014-02-13 20:46:40 +01:00
$victim = $this->maniaControl->playerManager->getPlayer($callback[1][1][1]);
if (isset($callback[1][1][0])) {
$shooter = $this->maniaControl->playerManager->getPlayer($callback[1][1][0]);
if ($shooter) {
2014-02-15 16:42:18 +01:00
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_KILL, $shooter);
}
2014-01-11 18:24:44 +01:00
}
2014-03-02 10:27:55 +01:00
if ($victim) {
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_DEATH, $victim);
}
break;
case 'LibXmlRpc_OnPlayerRequestRespawn':
2014-01-01 20:11:58 +01:00
$player = $this->maniaControl->playerManager->getPlayer($callback[1][1][0]);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_PLAYER_REQUEST_RESPAWN, $player);
break;
case 'OnShoot':
$paramsObject = json_decode($callback[1][1]);
$this->handleOnShoot($paramsObject->Event->Shooter->Login, $paramsObject->Event->WeaponNum);
break;
case 'OnNearMiss':
$paramsObject = json_decode($callback[1][1]);
$player = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Shooter->Login);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_NEARMISS, $player);
break;
case 'OnCapture':
$paramsObject = json_decode($callback[1][1]);
$player = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Player->Login);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_CAPTURE, $player);
break;
case 'OnHit':
$paramsObject = json_decode($callback[1][1]);
$shooter = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Shooter->Login);
2014-01-11 18:24:44 +01:00
$victim = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Victim->Login);
2014-01-24 22:34:35 +01:00
$weapon = $paramsObject->Event->WeaponNum;
2014-04-09 11:11:22 +02:00
if ($shooter) {
$this->maniaControl->statisticManager->incrementStat($this->getWeaponStat(intval($weapon), false), $shooter);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_HIT, $shooter);
}
if ($victim) {
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_GOT_HIT, $victim);
}
break;
case 'OnArmorEmpty':
$paramsObject = json_decode($callback[1][1]);
$victim = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Victim->Login);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_DEATH, $victim);
2014-02-13 20:45:38 +01:00
if (isset($paramsObject->Event->Shooter->Login)) {
$shooter = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Shooter->Login);
if ($shooter) {
2014-02-15 16:42:18 +01:00
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_KILL, $shooter);
}
2014-01-09 21:32:17 +01:00
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_KILL, $shooter);
2014-01-11 18:24:44 +01:00
}
break;
case 'OnRequestRespawn':
$paramsObject = json_decode($callback[1][1]);
$player = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Player->Login);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_PLAYER_REQUEST_RESPAWN, $player);
break;
2014-01-11 18:24:44 +01:00
case 'EndTurn': //TODO make it for other modes working
$paramsObject = json_decode($callback[1][1]);
$durationTime = (int)(($paramsObject->EndTime - $paramsObject->StartTime) / 1000);
$scoresTable = $paramsObject->ScoresTable;
2014-05-02 17:50:30 +02:00
foreach ($scoresTable as $score) {
2014-01-11 18:24:44 +01:00
$player = $this->maniaControl->playerManager->getPlayer($score->Login);
$this->maniaControl->statisticManager->insertStat(self::STAT_PLAYTIME, $player, -1, $durationTime);
}
break;
}
2013-12-31 21:41:52 +01:00
}
2014-05-02 17:50:30 +02:00
/**
* 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);
}
2014-06-14 16:44:49 +02:00
if (!isset($this->onShootArray[$login][$weaponNumber])) {
$this->onShootArray[$login][$weaponNumber] = 0;
}
$this->onShootArray[$login][$weaponNumber]++;
2014-05-02 17:50:30 +02:00
//Write Shoot Data into database
2014-05-13 16:03:26 +02:00
if (array_sum($this->onShootArray[$login]) > $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_ON_SHOOT_PRESTORE)) {
2014-05-02 17:50:30 +02:00
$player = $this->maniaControl->playerManager->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->statisticManager->insertStat(self::STAT_ROCKET_SHOT, $player, $this->maniaControl->server->index, $rocketShots);
$this->onShootArray[$login][self::WEAPON_ROCKET] = 0;
}
if ($laserShots > 0) {
$this->maniaControl->statisticManager->insertStat(self::STAT_LASER_SHOT, $player, $this->maniaControl->server->index, $laserShots);
$this->onShootArray[$login][self::WEAPON_LASER] = 0;
}
if ($arrowShots > 0) {
$this->maniaControl->statisticManager->insertStat(self::STAT_ARROW_SHOT, $player, $this->maniaControl->server->index, $arrowShots);
$this->onShootArray[$login][self::WEAPON_ARROW] = 0;
}
if ($nucleusShots > 0) {
$this->maniaControl->statisticManager->insertStat(self::STAT_NUCLEUS_SHOT, $player, $this->maniaControl->server->index, $nucleusShots);
$this->onShootArray[$login][self::WEAPON_NUCLEUS] = 0;
}
$this->maniaControl->statisticManager->insertStat(self::STAT_ON_SHOOT, $player, $this->maniaControl->server->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;
}
}
}
2013-12-31 21:41:52 +01:00
}