minor improvements, begin simplestats

This commit is contained in:
kremsy 2014-01-21 21:58:39 +01:00 committed by Steffen Schröder
parent 11abd5ee6e
commit df202393ba
4 changed files with 192 additions and 28 deletions

View File

@ -63,7 +63,7 @@ abstract class Formatter {
* @return string
*/
public static function formatTimeH($seconds) {
return gmdate("H:i:s", $seconds);
return date("H:i:s", $seconds);
}
/**

View File

@ -219,7 +219,8 @@ class PlayerManager implements CallbackListener {
return $player;
}
}
return null;
//Player is not online -> get Player from Database
return $this->getPlayerFromDatabaseByIndex($index);
}
/**
@ -266,6 +267,41 @@ class PlayerManager implements CallbackListener {
return $player;
}
/**
* Get's a Player out of the database
*
* @param $playerIndex
* @return Player $player
*/
private function getPlayerFromDatabaseByIndex($playerIndex) {
$mysqli = $this->maniaControl->database->mysqli;
if(!is_numeric($playerIndex)) {
return null;
}
$query = "SELECT * FROM `" . self::TABLE_PLAYERS . "` WHERE `index` = " . $playerIndex . ";";
$result = $mysqli->query($query);
if(!$result) {
trigger_error($mysqli->error);
return null;
}
$row = $result->fetch_object();
$result->close();
$player = new Player(false);
$player->index = $playerIndex;
$player->login = $row->login;
$player->nickname = $row->nickname;
$player->path = $row->path;
$player->authLevel = $row->authLevel;
return $player;
}
/**
* Save player in Database and fill up Object Properties
*

View File

@ -6,17 +6,23 @@ namespace ManiaControl\Statistics;
use FML\Controls\Control;
use FML\Controls\Frame;
use FML\Controls\Label;
use FML\Controls\Labels\Label_Text;
use FML\Controls\Quad;
use FML\Controls\Quads\Quad_BgsPlayerCard;
use FML\Controls\Quads\Quad_Icons64x64_1;
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;
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
class SimpleStatsList implements ManialinkPageAnswerListener{
class SimpleStatsList implements ManialinkPageAnswerListener, CallbackListener {
/**
* Constants
*/
@ -35,19 +41,34 @@ class SimpleStatsList implements ManialinkPageAnswerListener{
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
//$this->maniaControl->callbackManager->registerCallbackListener(ManialinkManager::CB_MAIN_WINDOW_CLOSED, $this, 'closeWidget');
//$this->maniaControl->callbackManager->registerCallbackListener(ManialinkManager::CB_MAIN_WINDOW_OPENED, $this, 'handleWidgetOpened');
//$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
//$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'handleOnInit');
}
/**
* Add the menu entry
*
* @param array $callback
*/
public function handleOnInit(array $callback) {
// Action Open StatsList
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_OPEN_STATSLIST, $this, 'command_ShowStatsList');
//TODO command
// Action Open Playerlist
//$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_OPEN_STATSLIST, $this, 'command_playerList');
$itemQuad = new Quad_UIConstruction_Buttons();
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_Author);
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_Stats);
$itemQuad->setAction(self::ACTION_OPEN_STATSLIST);
//$this->maniaControl->actionsMenu->addMenuItem($itemQuad, true, 14, 'Open Statistics');
$this->maniaControl->actionsMenu->addMenuItem($itemQuad, true, 14, 'Open Statistics');
}
/**
* Show the stat List
*
* @param array $callback
* @param Player $player
*/
public function command_ShowStatsList(array $callback, Player $player) {
$this->showStatsList($player);
}
/**
* Show the PlayerList Widget to the Player
@ -60,8 +81,12 @@ class SimpleStatsList implements ManialinkPageAnswerListener{
$quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowStyle();
$quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowSubStyle();
$width = $width * 1.4; //TODO setting
$maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID);
$script = $maniaLink->getScript();
// Create script and features
$script = new Script();
$maniaLink->setScript($script);
// Main frame
$frame = new Frame();
@ -84,14 +109,14 @@ class SimpleStatsList implements ManialinkPageAnswerListener{
$closeQuad->setAction(ManialinkManager::ACTION_CLOSEWIDGET);
// Start offsets
$x = -$width / 2;
$y = $height / 2;
$xStart = -$width / 2;
$y = $height / 2;
// Predefine Description Label
$descriptionLabel = new Label();
$frame->add($descriptionLabel);
$descriptionLabel->setAlign(Control::LEFT, Control::TOP);
$descriptionLabel->setPosition($x + 10, -$height / 2 + 5);
$descriptionLabel->setPosition($xStart + 10, -$height / 2 + 5);
$descriptionLabel->setSize($width * 0.7, 4);
$descriptionLabel->setTextSize(2);
$descriptionLabel->setVisible(false);
@ -101,24 +126,95 @@ class SimpleStatsList implements ManialinkPageAnswerListener{
$frame->add($headFrame);
$headFrame->setY($y - 5);
$array = array("Id" => $x + 5, "Nickname" => $x + 18, "Login" => $x + 70, "Location" => $x + 101);
// 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);
$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);
$this->maniaControl->manialinkManager->labelLine($headFrame, $array);
// get PlayerList
$players = $this->maniaControl->playerManager->getPlayers();
$i = 1;
// define standard properties
$hAlign = Control::LEFT;
$style = Label_Text::STYLE_TextCardSmall;
$textSize = 1.5;
$textColor = 'FFF';
$i = 1;
$y -= 10;
foreach($players as $listPlayer) {
foreach($time as $playerId => $value) {
$listPlayer = $this->maniaControl->playerManager->getPlayerByIndex($playerId);
//var_dump($listPlayer);
/**
*
* @var Player $listPlayer
*/
$path = $listPlayer->getProvince();
$playerFrame = new Frame();
$frame->add($playerFrame);
//$this->maniaControl->statisticManager->get
$displayArray = array();
if(!isset($kills[$playerId])) {
$killStat = 0;
} else {
$killStat = $kills[$playerId];
}
if(!isset($deaths[$playerId])) {
$deathStat = 0;
} else {
$deathStat = $deaths[$playerId];
}
$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'] = '-';
} else {
$displayArray['Kill-Death Ratio'] = strval(round($killStat / $deathStat, 2));
}
$array = array($i => $xStart + 5, $listPlayer->nickname => $xStart + 14);
$this->maniaControl->manialinkManager->labelLine($playerFrame, $array);
$x = $xStart + 55;
foreach($displayArray as $key => $array) {
$label = new Label_Text();
$playerFrame->add($label);
$label->setHAlign($hAlign);
$label->setX($x);
$label->setStyle($style);
$label->setTextSize($textSize);
$label->setText($array);
$label->setTextColor($textColor);
$script->addTooltip($label, $descriptionLabel, array(Script::OPTION_TOOLTIP_TEXT => '$o ' . $key));
if($x == $xStart + 55) {
$x += 10; //TODO improve
}
$x += 8;
}
$playerFrame->setY($y);
if($i % 2 != 0) {
$lineQuad = new Quad_BgsPlayerCard();
$playerFrame->add($lineQuad);
@ -127,11 +223,6 @@ class SimpleStatsList implements ManialinkPageAnswerListener{
$lineQuad->setZ(0.001);
}
$array = array($i => $x + 5, $listPlayer->nickname => $x + 18, $listPlayer->login => $x + 70, $path => $x + 101);
$this->maniaControl->manialinkManager->labelLine($playerFrame, $array);
$playerFrame->setY($y);
$i++;
$y -= 4;

View File

@ -86,6 +86,42 @@ class StatisticManager {
return $row->value;
}
/**
* Get All statistics orderd by an given name
*
* @param $orderedBy
* @param $serverIndex
* @return object
*/
public function getStatsRanking($statName = '', $serverIndex = -1) {
$mysqli = $this->maniaControl->database->mysqli;
$statId = $this->getStatId($statName);
$query = "SELECT playerId, serverIndex, value FROM `" . self::TABLE_STATISTICS . "` WHERE statId = " . $statId . " ORDER BY value DESC;";
$result = $mysqli->query($query);
if(!$result) {
trigger_error($mysqli->error);
return null;
}
$stats = array();
while($row = $result->fetch_object()) {
if($serverIndex == -1) {
if(!isset($stats[$row->playerId])) {
$stats[$row->playerId] = $row->value;
} else {
$stats[$row->playerId] += $row->value;
}
} else if($serverIndex == $row->serverIndex) {
$stats[$row->playerId] = $row->value;
}
}
$result->close();
return $stats;
}
/**
* Store Stats Meta Data from the Database
*/
@ -137,6 +173,7 @@ class StatisticManager {
return $playerStats;
}
/**
* Inserts a Stat into the database
*
@ -268,7 +305,7 @@ class StatisticManager {
`serverIndex` int(11) NOT NULL,
`playerId` int(11) NOT NULL,
`statId` int(11) NOT NULL,
`value` int(20) COLLATE utf8_unicode_ci NOT NULL,
`value` int(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
PRIMARY KEY (`index`),
UNIQUE KEY `unique` (`statId`,`playerId`,`serverIndex`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics' AUTO_INCREMENT=1;";