2013-12-31 21:41:52 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Statistic Collector Class
|
|
|
|
*
|
|
|
|
* @author steeffeen & kremsy
|
|
|
|
*/
|
|
|
|
namespace ManiaControl\Statistics;
|
|
|
|
|
|
|
|
|
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
|
|
|
use ManiaControl\ManiaControl;
|
2014-01-02 11:19:26 +01:00
|
|
|
use ManiaControl\Players\PlayerManager;
|
2013-12-31 21:41:52 +01:00
|
|
|
|
|
|
|
class StatisticCollector implements CallbackListener {
|
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
const SETTING_COLLECT_STATS_ENABLED = 'Collect Stats Enabled';
|
|
|
|
const SETTING_COLLECT_STATS_MINPLAYERS = 'Minimum Playercount for Collecting Stats';
|
2014-01-02 13:35:52 +01:00
|
|
|
const SETTING_ON_SHOOT_PRESTORE = 'Prestore Shoots before insert into Database';
|
2013-12-31 21:41:52 +01:00
|
|
|
/*
|
|
|
|
* Statistics
|
|
|
|
*/
|
2014-01-03 18:37:51 +01:00
|
|
|
const STAT_ON_SHOOT = 'Shoots';
|
|
|
|
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';
|
2014-01-03 19:14:07 +01:00
|
|
|
|
|
|
|
const SPECIAL_STAT_KILL_DEATH_RATIO = 'Kill / Death';
|
|
|
|
|
2013-12-31 21:41:52 +01:00
|
|
|
/**
|
|
|
|
* Private Properties
|
|
|
|
*/
|
|
|
|
private $maniaControl = null;
|
2014-01-02 11:19:26 +01:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$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');
|
2014-01-01 18:27:56 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'onInit');
|
2014-01-02 11:19:26 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECTED, $this, 'onPlayerDisconnect');
|
2013-12-31 21:41:52 +01:00
|
|
|
|
|
|
|
//Initialize Settings
|
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_COLLECT_STATS_ENABLED, true);
|
2014-01-03 18:37:51 +01:00
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_COLLECT_STATS_MINPLAYERS, 1); //TODO TEMP on 1, normally 3 or 4
|
2014-01-02 15:15:15 +01:00
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_ON_SHOOT_PRESTORE, 30);
|
2014-01-01 18:27:56 +01:00
|
|
|
}
|
2013-12-31 21:41:52 +01:00
|
|
|
|
2014-01-01 18:27:56 +01:00
|
|
|
/**
|
|
|
|
* onInit
|
2014-01-01 19:37:15 +01:00
|
|
|
*
|
2014-01-01 18:27:56 +01:00
|
|
|
* @param array $callback
|
|
|
|
*/
|
2014-01-01 19:37:15 +01:00
|
|
|
public function onInit(array $callback) {
|
2013-12-31 21:41:52 +01:00
|
|
|
//Define Stats MetaData
|
|
|
|
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_ON_SHOOT);
|
2014-01-01 19:37:15 +01:00
|
|
|
$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);
|
2013-12-31 21:41:52 +01:00
|
|
|
}
|
|
|
|
|
2014-01-02 11:19:26 +01:00
|
|
|
/**
|
|
|
|
* Handle Player Shoots
|
2014-01-02 13:35:52 +01:00
|
|
|
*
|
2014-01-02 11:19:26 +01:00
|
|
|
* @param $login
|
|
|
|
*/
|
2014-01-02 13:35:52 +01:00
|
|
|
private function handleOnShoot($login) {
|
|
|
|
if(!isset($this->onShootArray[$login])) {
|
2014-01-02 11:19:26 +01:00
|
|
|
$this->onShootArray[$login] = 1;
|
2014-01-02 13:35:52 +01:00
|
|
|
} else {
|
2014-01-02 11:19:26 +01:00
|
|
|
$this->onShootArray[$login]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Write Shoot Data into database
|
2014-01-02 14:31:30 +01:00
|
|
|
if($this->onShootArray[$login] > $this->maniaControl->settingManager->getSetting($this, self::SETTING_ON_SHOOT_PRESTORE)) {
|
2014-01-02 11:19:26 +01:00
|
|
|
$serverLogin = $this->maniaControl->server->getLogin();
|
2014-01-02 13:35:52 +01:00
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($login);
|
2014-01-02 11:19:26 +01:00
|
|
|
$this->maniaControl->statisticManager->insertStat(self::STAT_ON_SHOOT, $player, $serverLogin, $this->onShootArray[$login]);
|
|
|
|
$this->onShootArray[$login] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert OnShoot Statistic when a player leaves
|
2014-01-02 13:35:52 +01:00
|
|
|
*
|
|
|
|
* @param array $callback
|
2014-01-02 11:19:26 +01:00
|
|
|
*/
|
2014-01-02 13:35:52 +01:00
|
|
|
public function onPlayerDisconnect(array $callback) {
|
|
|
|
$player = $callback[1];
|
|
|
|
|
2014-01-02 11:19:26 +01:00
|
|
|
//Check if Stat Collecting is enabled
|
|
|
|
if(!$this->maniaControl->settingManager->getSetting($this, self::SETTING_COLLECT_STATS_ENABLED)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Insert Data into Database, and destroy player
|
2014-01-02 13:35:52 +01:00
|
|
|
if(isset($this->onShootArray[$player->login])) {
|
|
|
|
if($this->onShootArray[$player->login] > 0) {
|
2014-01-02 11:19:26 +01:00
|
|
|
$serverLogin = $this->maniaControl->server->getLogin();
|
|
|
|
$this->maniaControl->statisticManager->insertStat(self::STAT_ON_SHOOT, $player, $serverLogin, $this->onShootArray[$player->login]);
|
|
|
|
}
|
|
|
|
unset($this->onShootArray[$player->login]);
|
|
|
|
}
|
|
|
|
}
|
2013-12-31 21:41:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle stats on callbacks
|
2014-01-01 19:37:15 +01:00
|
|
|
*
|
2013-12-31 21:41:52 +01:00
|
|
|
* @param array $callback
|
|
|
|
*/
|
|
|
|
public function handleCallbacks(array $callback) {
|
|
|
|
|
|
|
|
//Check if Stat Collecting is enabled
|
|
|
|
if(!$this->maniaControl->settingManager->getSetting($this, self::SETTING_COLLECT_STATS_ENABLED)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check for Minplayer
|
|
|
|
if(count($this->maniaControl->playerManager->getPlayers()) < $this->maniaControl->settingManager->getSetting($this, self::SETTING_COLLECT_STATS_MINPLAYERS)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-01 19:37:15 +01:00
|
|
|
$callbackName = $callback[1][0];
|
|
|
|
|
|
|
|
switch($callbackName) {
|
2014-01-02 11:19:26 +01:00
|
|
|
case 'LibXmlRpc_OnShoot':
|
|
|
|
$this->handleOnShoot($callback[1][1][0]);
|
2014-01-01 19:37:15 +01:00
|
|
|
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-02 11:19:26 +01:00
|
|
|
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_HIT, $shooter);
|
|
|
|
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_GOT_HIT, $victim);
|
2014-01-01 19:37:15 +01:00
|
|
|
break;
|
|
|
|
case 'LibXmlRpc_OnNearMiss':
|
2014-01-01 20:11:58 +01:00
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($callback[1][1][0]);
|
2014-01-02 11:19:26 +01:00
|
|
|
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_NEARMISS, $player);
|
2014-01-01 19:37:15 +01:00
|
|
|
break;
|
|
|
|
case 'LibXmlRpc_OnCapture':
|
2014-01-01 20:11:58 +01:00
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($callback[1][1][0]);
|
2014-01-02 11:19:26 +01:00
|
|
|
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_CAPTURE, $player);
|
2014-01-01 19:37:15 +01:00
|
|
|
break;
|
|
|
|
case 'LibXmlRpc_OnArmorEmpty':
|
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-02 11:19:26 +01:00
|
|
|
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_KILL, $shooter);
|
|
|
|
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_DEATH, $victim);
|
2014-01-01 19:37:15 +01:00
|
|
|
break;
|
|
|
|
case 'LibXmlRpc_OnPlayerRequestRespawn':
|
2014-01-01 20:11:58 +01:00
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($callback[1][1][0]);
|
2014-01-02 11:19:26 +01:00
|
|
|
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_PLAYER_REQUEST_RESPAWN, $player);
|
2014-01-01 19:37:15 +01:00
|
|
|
break;
|
2014-01-02 11:19:26 +01:00
|
|
|
case 'OnShoot':
|
|
|
|
$paramsObject = json_decode($callback[1][1]);
|
2014-01-02 14:31:30 +01:00
|
|
|
$this->handleOnShoot($paramsObject->Event->Shooter->Login);
|
2014-01-01 19:37:15 +01:00
|
|
|
break;
|
|
|
|
case 'OnNearMiss':
|
|
|
|
$paramsObject = json_decode($callback[1][1]);
|
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Shooter->Login);
|
2014-01-02 11:19:26 +01:00
|
|
|
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_NEARMISS, $player);
|
2014-01-01 19:37:15 +01:00
|
|
|
break;
|
|
|
|
case 'OnCapture':
|
|
|
|
$paramsObject = json_decode($callback[1][1]);
|
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Player->Login);
|
2014-01-02 11:19:26 +01:00
|
|
|
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_CAPTURE, $player);
|
2014-01-01 19:37:15 +01:00
|
|
|
break;
|
|
|
|
case 'OnHit':
|
|
|
|
$paramsObject = json_decode($callback[1][1]);
|
|
|
|
$shooter = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Shooter->Login);
|
2014-01-02 11:19:26 +01:00
|
|
|
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_HIT, $shooter);
|
2014-01-01 19:37:15 +01:00
|
|
|
$victim = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Victim->Login);
|
2014-01-02 11:19:26 +01:00
|
|
|
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_GOT_HIT, $victim);
|
2014-01-01 19:37:15 +01:00
|
|
|
break;
|
|
|
|
case 'OnArmorEmpty':
|
|
|
|
$paramsObject = json_decode($callback[1][1]);
|
|
|
|
$victim = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Victim->Login);
|
2014-01-02 11:19:26 +01:00
|
|
|
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_DEATH, $victim);
|
2014-01-01 19:37:15 +01:00
|
|
|
$shooter = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Shooter->Login);
|
2014-01-02 11:19:26 +01:00
|
|
|
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_KILL, $shooter);
|
2014-01-01 19:37:15 +01:00
|
|
|
break;
|
|
|
|
case 'OnRequestRespawn':
|
|
|
|
$paramsObject = json_decode($callback[1][1]);
|
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Player->Login);
|
2014-01-02 11:19:26 +01:00
|
|
|
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_PLAYER_REQUEST_RESPAWN, $player);
|
2014-01-01 19:37:15 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-12-31 21:41:52 +01:00
|
|
|
}
|
|
|
|
}
|