improved statstic collector, nothing tested yet

This commit is contained in:
kremsy 2014-01-02 11:19:26 +01:00
parent 442b6d1f7b
commit 3ca2879fd1
3 changed files with 107 additions and 45 deletions

View File

@ -400,6 +400,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
$script->addTooltip($switchToQuad, $descriptionLabel);
}
//Display Karma bar
if($karmaPlugin != null) {
$karma = $karmaPlugin->getMapKarma($map);

View File

@ -10,6 +10,8 @@ namespace ManiaControl\Statistics;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
class StatisticCollector implements CallbackListener {
/**
@ -17,7 +19,7 @@ class StatisticCollector implements CallbackListener {
*/
const SETTING_COLLECT_STATS_ENABLED = 'Collect Stats Enabled';
const SETTING_COLLECT_STATS_MINPLAYERS = 'Minimum Playercount for Collecting Stats';
const SETTING_ON_SHOOT_PRESTORE = 'Prestore Shoots before insert into Database';
/*
* Statistics
*/
@ -33,7 +35,7 @@ class StatisticCollector implements CallbackListener {
* Private Properties
*/
private $maniaControl = null;
private $onShootArray = array();
/**
* Construct player manager
*
@ -46,10 +48,12 @@ class StatisticCollector implements CallbackListener {
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_MODESCRIPTCALLBACK, $this, 'handleCallbacks');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_MODESCRIPTCALLBACKARRAY, $this, 'handleCallbacks');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'onInit');
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECTED, $this, 'onPlayerDisconnect');
//Initialize Settings
$this->maniaControl->settingManager->initSetting($this, self::SETTING_COLLECT_STATS_ENABLED, true);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_COLLECT_STATS_MINPLAYERS, 4);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_ON_SHOOT_PRESTORE, 30);
}
/**
@ -69,6 +73,47 @@ class StatisticCollector implements CallbackListener {
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_ON_KILL);
}
/**
* Handle Player Shoots
* @param $login
*/
private function handleOnShoot($login){
if(!isset($this->onShootArray[$login])){
$this->onShootArray[$login] = 1;
}else{
$this->onShootArray[$login]++;
}
//Write Shoot Data into database
if($this->onShootArray[$login] > self::SETTING_ON_SHOOT_PRESTORE){
$serverLogin = $this->maniaControl->server->getLogin();
$player = $this->maniaControl->playerManager->getPlayer($login);
$this->maniaControl->statisticManager->insertStat(self::STAT_ON_SHOOT, $player, $serverLogin, $this->onShootArray[$login]);
$this->onShootArray[$login] = 0;
}
}
/**
* Insert OnShoot Statistic when a player leaves
* @param array $callback
* @param Player $player
*/
public function onPlayerDisconnect(array $callback, Player $player) {
//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
if(isset($this->onShootArray[$player->login])){
if($this->onShootArray[$player->login] > 0){
$serverLogin = $this->maniaControl->server->getLogin();
$this->maniaControl->statisticManager->insertStat(self::STAT_ON_SHOOT, $player, $serverLogin, $this->onShootArray[$player->login]);
}
unset($this->onShootArray[$player->login]);
}
}
/**
* Handle stats on callbacks
@ -88,66 +133,67 @@ class StatisticCollector implements CallbackListener {
}
$callbackName = $callback[1][0];
$serverLogin = $this->maniaControl->server->getLogin();
switch($callbackName) {
case 'LibXmlRpc_OnShoot': //TODO
case 'LibXmlRpc_OnShoot':
$this->handleOnShoot($callback[1][1][0]);
break;
case 'LibXmlRpc_OnHit':
$shooter = $this->maniaControl->playerManager->getPlayer($callback[1][1][0]);
$victim = $this->maniaControl->playerManager->getPlayer($callback[1][1][1]);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_HIT, $shooter, $serverLogin);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_GOT_HIT, $victim, $serverLogin);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_HIT, $shooter);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_GOT_HIT, $victim);
break;
case 'LibXmlRpc_OnNearMiss':
$player = $this->maniaControl->playerManager->getPlayer($callback[1][1][0]);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_NEARMISS, $player, $serverLogin);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_NEARMISS, $player);
break;
case 'LibXmlRpc_OnCapture':
$player = $this->maniaControl->playerManager->getPlayer($callback[1][1][0]);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_CAPTURE, $player, $serverLogin);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_CAPTURE, $player);
break;
case 'LibXmlRpc_OnArmorEmpty':
$shooter = $this->maniaControl->playerManager->getPlayer($callback[1][1][0]);
$victim = $this->maniaControl->playerManager->getPlayer($callback[1][1][1]);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_KILL, $shooter, $serverLogin);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_DEATH, $victim, $serverLogin);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_KILL, $shooter);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_DEATH, $victim);
break;
case 'LibXmlRpc_OnPlayerRequestRespawn':
$player = $this->maniaControl->playerManager->getPlayer($callback[1][1][0]);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_PLAYER_REQUEST_RESPAWN, $player, $serverLogin);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_PLAYER_REQUEST_RESPAWN, $player);
break;
case 'OnShoot': //TODO
case 'OnShoot':
$paramsObject = json_decode($callback[1][1]);
$this->handleOnShoot($paramsObject->Event->Player->Login);
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, $serverLogin);
$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, $serverLogin);
$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);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_HIT, $shooter, $serverLogin);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_HIT, $shooter);
$victim = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Victim->Login);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_GOT_HIT, $victim, $serverLogin);
$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, $serverLogin);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_DEATH, $victim);
$shooter = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Shooter->Login);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_KILL, $shooter, $serverLogin);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_KILL, $shooter);
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, $serverLogin);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_PLAYER_REQUEST_RESPAWN, $player);
break;
}
}

View File

@ -4,7 +4,6 @@ namespace ManiaControl\Statistics;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use ManiaControl\Statistics\StatisticCollector;
require_once __DIR__ . '/StatisticCollector.php';
@ -33,7 +32,6 @@ class StatisticManager {
* Private Properties
*/
private $maniaControl = null;
private $mysqli = null;
private $stats = array();
/**
@ -43,7 +41,6 @@ class StatisticManager {
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->mysqli = $this->maniaControl->database->mysqli;
$this->initTables();
$this->statisticCollector = new StatisticCollector($maniaControl);
@ -61,6 +58,7 @@ class StatisticManager {
* @return int
*/
public function getStatisticData($statName, $playerId, $serverLogin = false) {
$mysqli = $this->maniaControl->database->mysqli;
$statId = $this->getStatId($statName);
if($statId == null) {
@ -73,9 +71,9 @@ class StatisticManager {
$query = "SELECT value FROM `" . self::TABLE_STATISTICS . "` WHERE `statId` = " . $statId . " AND `playerId` = " . $playerId . " AND `serverLogin` = '" . $serverLogin . "';";
}
$result = $this->mysqli->query($query);
$result = $mysqli->query($query);
if(!$result) {
trigger_error($this->mysqli->error);
trigger_error($mysqli->error);
}
$row = $result->fetch_object();
@ -88,10 +86,12 @@ class StatisticManager {
* Store Stats Meta Data from the Database
*/
private function storeStatMetaData() {
$mysqli = $this->maniaControl->database->mysqli;
$query = "SELECT * FROM `" . self::TABLE_STATMETADATA . "`;";
$result = $this->mysqli->query($query);
$result = $mysqli->query($query);
if(!$result) {
trigger_error($this->mysqli->error);
trigger_error($mysqli->error);
}
while($row = $result->fetch_object()) {
@ -124,8 +124,9 @@ class StatisticManager {
* @param string $serverLogin
* @param $value , value to Add
* @param string $statType
* @return bool
*/
public function insertStat($statName, Player $player, $serverLogin, $value, $statType = self::STAT_TYPE_INT) {
public function insertStat($statName, Player $player, $serverLogin = '', $value, $statType = self::STAT_TYPE_INT) {
$statId = $this->getStatId($statName);
if($statId == null) {
@ -136,6 +137,12 @@ class StatisticManager {
return true;
}
if($serverLogin == '') {
$serverLogin = $this->maniaControl->server->getLogin();
}
$mysqli = $this->maniaControl->database->mysqli;
$query = "INSERT INTO `" . self::TABLE_STATISTICS . "` (
`serverLogin`,
`playerId`,
@ -146,9 +153,9 @@ class StatisticManager {
) ON DUPLICATE KEY UPDATE
`value` = `value` + VALUES(`value`);";
$statement = $this->mysqli->prepare($query);
if($this->mysqli->error) {
trigger_error($this->mysqli->error);
$statement = $mysqli->prepare($query);
if($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$statement->bind_param('siii', $serverLogin, $player->index, $statId, $value);
@ -167,11 +174,12 @@ class StatisticManager {
* Increments a Statistic by one
*
* @param $statName
* @param Player $playerId
* @param $serverLogin
* @param \ManiaControl\Players\Player $player
* @param string $serverLogin
* @internal param \ManiaControl\Players\Player $playerId
* @return bool
*/
public function incrementStat($statName, Player $player, $serverLogin) {
public function incrementStat($statName, Player $player, $serverLogin = '') {
return $this->insertStat($statName, $player, $serverLogin, 1);
}
@ -180,17 +188,19 @@ class StatisticManager {
*
* @param string $statName
* @param string $statDescription
* @return bool
*/
public function defineStatMetaData($statName, $statDescription = '') {
$mysqli = $this->maniaControl->database->mysqli;
$query = "INSERT IGNORE INTO `" . self::TABLE_STATMETADATA . "` (
`name`,
`description`
) VALUES (
?, ?
);";
$statement = $this->mysqli->prepare($query);
if($this->mysqli->error) {
trigger_error($this->mysqli->error);
$statement = $mysqli->prepare($query);
if($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$statement->bind_param('ss', $statName, $statDescription);
@ -202,6 +212,8 @@ class StatisticManager {
}
$statement->close();
return true;
}
/**
@ -210,6 +222,7 @@ class StatisticManager {
* @return bool
*/
private function initTables() {
$mysqli = $this->maniaControl->database->mysqli;
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_STATMETADATA . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
@ -218,9 +231,9 @@ class StatisticManager {
UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics Meta Data' AUTO_INCREMENT=1;";
$statement = $this->mysqli->prepare($query);
if($this->mysqli->error) {
trigger_error($this->mysqli->error, E_USER_ERROR);
$statement = $mysqli->prepare($query);
if($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
@ -242,9 +255,9 @@ class StatisticManager {
UNIQUE KEY `unique` (`statId`,`playerId`,`serverLogin`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics' AUTO_INCREMENT=1;";
$statement = $this->mysqli->prepare($query);
if($this->mysqli->error) {
trigger_error($this->mysqli->error, E_USER_ERROR);
$statement = $mysqli->prepare($query);
if($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
@ -255,5 +268,7 @@ class StatisticManager {
return false;
}
$statement->close();
return true;
}
}