statistic collector improvement, simple stats list continue work

This commit is contained in:
kremsy 2014-01-23 21:55:40 +01:00 committed by Steffen Schröder
parent c4f3cad350
commit 0bdb1a7826
2 changed files with 178 additions and 55 deletions

View File

@ -14,7 +14,6 @@ use FML\Controls\Quads\Quad_UIConstruction_Buttons;
use FML\ManiaLink;
use FML\Script\Script;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Formatter;
use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\ManialinkManager;
@ -32,6 +31,8 @@ class SimpleStatsList implements ManialinkPageAnswerListener, CallbackListener {
* Private Properties
*/
private $maniaControl = null;
private $statArray = array();
private $statsWidth = 0;
/**
* Create a PlayerList Instance
@ -42,7 +43,7 @@ class SimpleStatsList implements ManialinkPageAnswerListener, CallbackListener {
$this->maniaControl = $maniaControl;
//$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'handleOnInit');
}
}
/**
* Add the menu entry
@ -58,6 +59,24 @@ class SimpleStatsList implements ManialinkPageAnswerListener, CallbackListener {
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_Stats);
$itemQuad->setAction(self::ACTION_OPEN_STATSLIST);
$this->maniaControl->actionsMenu->addMenuItem($itemQuad, true, 14, 'Open Statistics');
//TODO setting if a stat get shown
//TODO sort out stats where no player have a point
$this->registerStat(PlayerManager::STAT_SERVERTIME, 10, "ST", 20, StatisticManager::STAT_TYPE_TIME);
$this->registerStat(StatisticCollector::STAT_ON_HIT, 20, "H");
$this->registerStat(StatisticCollector::STAT_ON_NEARMISS, 30, "NM");
$this->registerStat(StatisticCollector::STAT_ON_KILL, 40, "K");
$this->registerStat(StatisticCollector::STAT_ON_DEATH, 50, "D");
$this->registerStat(StatisticCollector::STAT_ON_CAPTURE, 60, "C");
//TODO register from classes:
if($this->maniaControl->pluginManager->getPlugin('DonationPlugin')) {
$this->registerStat(\DonationPlugin::STAT_PLAYER_DONATIONS, 70, "D", 20);
}
if($this->maniaControl->pluginManager->getPlugin('KarmaPlugin')) {
$this->registerStat(\DonationPlugin::STAT_PLAYER_DONATIONS, 80, "VM");
}
}
/**
@ -70,20 +89,32 @@ class SimpleStatsList implements ManialinkPageAnswerListener, CallbackListener {
$this->showStatsList($player);
}
public function registerStat($statName, $order, $headShortCut, $width = 8, $format = StatisticManager::STAT_TYPE_INT) {
$this->statArray[$order] = array();
$this->statArray[$order]["Name"] = $statName;
$this->statArray[$order]["HeadShortCut"] = '$o' . $headShortCut;
$this->statArray[$order]["Width"] = $width;
$this->statArray[$order]["Format"] = $format;
$this->statsWidth += $width;
}
/**
* Show the PlayerList Widget to the Player
*
* @param Player $player
*/
public function showStatsList(Player $player) {
$width = $this->maniaControl->manialinkManager->styleManager->getListWidgetsWidth();
$height = $this->maniaControl->manialinkManager->styleManager->getListWidgetsHeight();
$quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowStyle();
$quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowSubStyle();
$width = $width * 1.4; //TODO setting
$maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID);
$width = $this->statsWidth + 80;
// Create script and features
$script = new Script();
$maniaLink->setScript($script);
@ -127,22 +158,29 @@ class SimpleStatsList implements ManialinkPageAnswerListener, CallbackListener {
$headFrame->setY($y - 5);
// get Players by Main-Order
//$players = $this->maniaControl->playerManager->getPlayers();
$time = $this->maniaControl->statisticManager->getStatsRanking(PlayerManager::STAT_SERVERTIME);
$kills = $this->maniaControl->statisticManager->getStatsRanking(StatisticCollector::STAT_ON_KILL);
$deaths = $this->maniaControl->statisticManager->getStatsRanking(StatisticCollector::STAT_ON_DEATH);
$playTime = $this->maniaControl->statisticManager->getStatsRanking(StatisticCollector::STAT_PLAYTIME);
$shots = $this->maniaControl->statisticManager->getStatsRanking(StatisticCollector::STAT_ON_SHOOT);
$x = $xStart;
$array['$oId'] = $x + 5;
$array['$oNickname'] = $x + 14;
$array['$oS'] = $x += 55;
$array['$oK'] = $x += 20;
$array['$oD'] = $x += 8;
$array['$oK/D'] = $x += 8;
//$array = array("Id" => $xStart + 5, "Nickname" => $xStart + 14, "K" => $xStart + 50, "D" => $xStart + 58);
$x = $xStart + 55;
$statRankings = array();
foreach($this->statArray as $key => $stat) {
$statRankings[$stat["Name"]] = $this->maniaControl->statisticManager->getStatsRanking($stat["Name"]);
$array[$stat['HeadShortCut']] = $x;
$x += $stat["Width"];
}
$standardWidth = 10;
$array['$oK/D'] = $x;
$x += $standardWidth;
$array['$oH/h'] = $x;
$order = PlayerManager::STAT_SERVERTIME;
$this->maniaControl->manialinkManager->labelLine($headFrame, $array);
// define standard properties
@ -152,44 +190,45 @@ class SimpleStatsList implements ManialinkPageAnswerListener, CallbackListener {
$textColor = 'FFF';
$i = 1;
$y -= 10;
foreach($time as $playerId => $value) {
foreach($statRankings[$order] as $playerId => $value) {
$listPlayer = $this->maniaControl->playerManager->getPlayerByIndex($playerId);
//var_dump($listPlayer);
/**
*
* @var Player $listPlayer
*/
if($i == 15) {
break;
}
/** @var Player $listPlayer * */
$playerFrame = new Frame();
$frame->add($playerFrame);
//$this->maniaControl->statisticManager->get
$displayArray = array();
if(!isset($kills[$playerId])) {
$killStat = 0;
} else {
$killStat = $kills[$playerId];
foreach($this->statArray as $stat) {
$statValue = 0;
if(isset($statRankings[$stat['Name']][$playerId])) {
$statValue = $statRankings[$stat['Name']][$playerId];
if($stat['Format'] == StatisticManager::STAT_TYPE_TIME) {
$statValue = Formatter::formatTimeH($statValue);
}
}
$displayArray[$stat['Name']] = array("Value" => strval($statValue), "Width" => $stat['Width']);
}
if(!isset($deaths[$playerId])) {
$deathStat = 0;
isset($playTime[$playerId]) ? $playTimeStat = $playTime[$playerId] : $playTimeStat = 0;
if(isset($statRankings[StatisticCollector::STAT_ON_DEATH][$playerId])) {
$deathStat = $statRankings[StatisticCollector::STAT_ON_DEATH][$playerId];
$killStat = $statRankings[StatisticCollector::STAT_ON_KILL][$playerId];
$displayArray['Kill-Death Ratio'] = array("Value" => round($killStat / $deathStat, 2), "Width" => $standardWidth);
} else {
$deathStat = $deaths[$playerId];
$displayArray['Kill-Death Ratio'] = array("Value" => "-", "Width" => $standardWidth);
}
$displayArray['Server Time'] = Formatter::formatTimeH($value);
var_dump($value);
$displayArray['Kills'] = strval($killStat);
//var_dump($deaths);
$displayArray['Deaths'] = strval($deathStat);
// var_dump($displayArray);
if($deathStat == 0) {
$displayArray['Kill-Death Ratio'] = '-';
if($playTimeStat == 0) {
$displayArray['Hits per Hour'] = array("Value" => "-", "Width" => $standardWidth);
} else {
$displayArray['Kill-Death Ratio'] = strval(round($killStat / $deathStat, 2));
$hitStat = $statRankings[StatisticCollector::STAT_ON_HIT][$playerId];
$displayArray['Hits per Hour'] = array("Value" => strval(round(intval($hitStat) / (intval($playTimeStat) / 3600), 1)), "Width" => $standardWidth);
}
$array = array($i => $xStart + 5, $listPlayer->nickname => $xStart + 14);
@ -203,13 +242,10 @@ class SimpleStatsList implements ManialinkPageAnswerListener, CallbackListener {
$label->setX($x);
$label->setStyle($style);
$label->setTextSize($textSize);
$label->setText($array);
$label->setText($array['Value']);
$label->setTextColor($textColor);
$script->addTooltip($label, $descriptionLabel, array(Script::OPTION_TOOLTIP_TEXT => '$o ' . $key));
if($x == $xStart + 55) {
$x += 10; //TODO improve
}
$x += 8;
$x += $array['Width'];
}

View File

@ -31,9 +31,22 @@ class StatisticCollector implements CallbackListener {
const STAT_ON_DEATH = 'Deaths';
const STAT_ON_PLAYER_REQUEST_RESPAWN = 'Respawns';
const STAT_ON_KILL = 'Kills';
const STAT_LASER_SHOT = 'Laser Shots';
const STAT_LASER_HIT = 'Laser Hit';
const STAT_ROCKET_SHOT = 'Rocket Shots';
const STAT_ROCKET_HIT = 'Rocket Hit';
const STAT_ARROW_SHOT = 'Arrow Shots';
const STAT_ARROW_HIT = 'Arrow Hit';
const STAT_NUCLEUS_SHOT = 'Nucleus Shots';
const STAT_NUCLEUS_HIT = 'Nucleus Hit';
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
*/
@ -56,8 +69,8 @@ class StatisticCollector implements CallbackListener {
//Initialize Settings
$this->maniaControl->settingManager->initSetting($this, self::SETTING_COLLECT_STATS_ENABLED, true);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_COLLECT_STATS_MINPLAYERS, 3);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_ON_SHOOT_PRESTORE, 30);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_COLLECT_STATS_MINPLAYERS, 2); //TODO just temp on 2
$this->maniaControl->settingManager->initSetting($this, self::SETTING_ON_SHOOT_PRESTORE, 20);
}
/**
@ -76,6 +89,14 @@ class StatisticCollector implements CallbackListener {
$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);
}
/**
@ -83,22 +104,84 @@ class StatisticCollector implements CallbackListener {
*
* @param $login
*/
private function handleOnShoot($login) {
private function handleOnShoot($login, $weaponNumber) {
if(!isset($this->onShootArray[$login])) {
$this->onShootArray[$login] = 1;
$this->onShootArray[$login] = array(self::WEAPON_ROCKET => 0, self::WEAPON_ARROW => 0, self::WEAPON_NUCLEUS => 0, self::WEAPON_LASER => 0);
$this->onShootArray[$login][$weaponNumber]++;
} else {
$this->onShootArray[$login]++;
$this->onShootArray[$login][$weaponNumber]++;
}
//Write Shoot Data into database
if($this->onShootArray[$login] > $this->maniaControl->settingManager->getSetting($this, self::SETTING_ON_SHOOT_PRESTORE)) {
if(array_sum($this->onShootArray[$login]) > $this->maniaControl->settingManager->getSetting($this, self::SETTING_ON_SHOOT_PRESTORE)) {
$player = $this->maniaControl->playerManager->getPlayer($login);
$this->maniaControl->statisticManager->insertStat(self::STAT_ON_SHOOT, $player, $this->maniaControl->server->index, $this->onShootArray[$login]);
$this->onShootArray[$login] = 0;
$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);
}
}
/**
* Gets the Weapon stat
*
* @param $weaponNumber
* @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;
}
}
}
/**
* Insert OnShoot Statistic when a player leaves
*
@ -141,11 +224,13 @@ class StatisticCollector implements CallbackListener {
switch($callbackName) {
case 'LibXmlRpc_OnShoot':
$this->handleOnShoot($callback[1][1][0]);
$this->handleOnShoot($callback[1][1][0], $callback[1][1][1]);
break;
case 'LibXmlRpc_OnHit':
$shooter = $this->maniaControl->playerManager->getPlayer($callback[1][1][0]);
$victim = $this->maniaControl->playerManager->getPlayer($callback[1][1][1]);
$weapon = $this->maniaControl->playerManager->getPlayer($callback[1][1][3]);
$this->maniaControl->statisticManager->incrementStat($this->getWeaponStat($weapon, false), $shooter);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_HIT, $shooter);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_GOT_HIT, $victim);
break;
@ -178,7 +263,7 @@ class StatisticCollector implements CallbackListener {
break;
case 'OnShoot':
$paramsObject = json_decode($callback[1][1]);
$this->handleOnShoot($paramsObject->Event->Shooter->Login);
$this->handleOnShoot($paramsObject->Event->Shooter->Login, $paramsObject->Event->WeaponNum);
break;
case 'OnNearMiss':
$paramsObject = json_decode($callback[1][1]);
@ -194,6 +279,8 @@ class StatisticCollector implements CallbackListener {
$paramsObject = json_decode($callback[1][1]);
$shooter = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Shooter->Login);
$victim = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->Victim->Login);
$weapon = $this->maniaControl->playerManager->getPlayer($paramsObject->Event->WeaponNum);
$this->maniaControl->statisticManager->incrementStat($this->getWeaponStat($weapon, false), $shooter);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_HIT, $shooter);
$this->maniaControl->statisticManager->incrementStat(self::STAT_ON_GOT_HIT, $victim);
break;