TrackManiaControl/application/core/Statistics/StatisticManager.php

256 lines
6.1 KiB
PHP
Raw Normal View History

2013-12-31 17:31:05 +01:00
<?php
2013-12-31 17:59:01 +01:00
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
2013-12-31 21:41:52 +01:00
use ManiaControl\Statistics\StatisticCollector;
require_once __DIR__ . '/StatisticCollector.php';
2013-12-31 17:59:01 +01:00
2013-12-31 17:31:05 +01:00
/**
* Statistic Manager Class
*
* @author steeffeen & kremsy
*/
//TODO db reference between player index and statsitics playerId
//TODO db reference between metadata statId and statistics statId
2013-12-31 17:31:05 +01:00
class StatisticManager {
2013-12-31 17:59:01 +01:00
/**
* Constants
*/
const TABLE_STATMETADATA = 'mc_statmetadata';
2013-12-31 18:15:45 +01:00
const TABLE_STATISTICS = 'mc_statistics';
2013-12-31 17:59:01 +01:00
const STAT_TYPE_INT = '0';
2013-12-31 21:41:52 +01:00
/**
* Public Properties
*/
public $statisticCollector = null;
2013-12-31 17:59:01 +01:00
/**
* Private Properties
*/
private $maniaControl = null;
2013-12-31 18:15:45 +01:00
private $mysqli = null;
private $stats = array();
2013-12-31 17:59:01 +01:00
/**
* Construct player manager
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2013-12-31 18:15:45 +01:00
$this->mysqli = $this->maniaControl->database->mysqli;
2013-12-31 17:59:01 +01:00
$this->initTables();
2013-12-31 21:41:52 +01:00
$this->statisticCollector = new StatisticCollector($maniaControl);
//Store Stats MetaData
$this->storeStatMetaData();
}
/**
* Get the value of an statistic
*
* @param $statName
* @param $playerId
* @param bool $serverLogin
* @return int
*/
public function getStatisticData($statName, $playerId, $serverLogin = false) {
$statId = $this->getStatId($statName);
if($statId == null) {
return -1;
}
if(!$serverLogin) {
$query = "SELECT SUM(value) as value FROM `" . self::TABLE_STATISTICS . "` WHERE `statId` = " . $statId . " AND `playerId` = " . $playerId . ";";
} else {
$query = "SELECT value FROM `" . self::TABLE_STATISTICS . "` WHERE `statId` = " . $statId . " AND `playerId` = " . $playerId . " AND `serverLogin` = '" . $serverLogin . "';";
}
$result = $this->mysqli->query($query);
if(!$result) {
trigger_error($this->mysqli->error);
}
$row = $result->fetch_object();
$result->close();
return $row->value;
}
/**
* Store Stats Meta Data from the Database
*/
private function storeStatMetaData() {
$query = "SELECT * FROM `" . self::TABLE_STATMETADATA . "`;";
$result = $this->mysqli->query($query);
if(!$result) {
trigger_error($this->mysqli->error);
}
while($row = $result->fetch_object()) {
$this->stats[$row->name] = $row;
}
$result->close();
}
/**
* Returns the Stats Id
*
* @param $statName
* @return int
*/
private function getStatId($statName) {
if(isset($this->stats[$statName])) {
$stat = $this->stats[$statName];
return $stat->index;
} else {
return null;
}
}
/**
* Inserts a Stat into the database
*
* @param $statName
* @param Player $player
* @param string $serverLogin
* @param $value , value to Add
* @param string $statType
*/
public function insertStat($statName, Player $player, $serverLogin, $value, $statType = self::STAT_TYPE_INT) {
$statId = $this->getStatId($statName);
if($statId == null) {
return false;
}
if($player->isFakePlayer()) {
return true;
}
$query = "INSERT INTO `" . self::TABLE_STATISTICS . "` (
`serverLogin`,
`playerId`,
`statId`,
`value`
) VALUES (
?, ?, ?, ?
) ON DUPLICATE KEY UPDATE
`value` = `value` + VALUES(`value`);";
$statement = $this->mysqli->prepare($query);
if($this->mysqli->error) {
trigger_error($this->mysqli->error);
return false;
}
$statement->bind_param('siii', $serverLogin, $player->index, $statId, $value);
$statement->execute();
if($statement->error) {
trigger_error($statement->error);
$statement->close();
return false;
}
$statement->close();
return true;
}
/**
* Increments a Statistic by one
*
* @param $statName
* @param Player $playerId
* @param $serverLogin
* @return bool
*/
public function incrementStat($statName, Player $player, $serverLogin) {
return $this->insertStat($statName, $player, $serverLogin, 1);
2013-12-31 17:59:01 +01:00
}
2013-12-31 18:15:45 +01:00
/**
* Defines a Stat
*
* @param string $statName
* @param string $statDescription
*/
public function defineStatMetaData($statName, $statDescription = '') {
$query = "INSERT IGNORE INTO `" . self::TABLE_STATMETADATA . "` (
`name`,
`description`
) VALUES (
?, ?
);";
$statement = $this->mysqli->prepare($query);
if($this->mysqli->error) {
trigger_error($this->mysqli->error);
return false;
}
$statement->bind_param('ss', $statName, $statDescription);
$statement->execute();
if($statement->error) {
trigger_error($statement->error);
$statement->close();
return false;
}
$statement->close();
}
2013-12-31 17:59:01 +01:00
/**
* Initialize necessary database tables
*
* @return bool
*/
private function initTables() {
2013-12-31 18:15:45 +01:00
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_STATMETADATA . "` (
2013-12-31 17:59:01 +01:00
`index` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
2013-12-31 18:15:45 +01:00
`description` varchar(150) COLLATE utf8_unicode_ci,
2013-12-31 17:59:01 +01:00
PRIMARY KEY (`index`),
UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics Meta Data' AUTO_INCREMENT=1;";
2013-12-31 18:15:45 +01:00
$statement = $this->mysqli->prepare($query);
if($this->mysqli->error) {
trigger_error($this->mysqli->error, E_USER_ERROR);
2013-12-31 17:59:01 +01:00
return false;
}
$statement->execute();
2013-12-31 18:15:45 +01:00
if($statement->error) {
2013-12-31 17:59:01 +01:00
trigger_error($statement->error, E_USER_ERROR);
2013-12-31 18:15:45 +01:00
2013-12-31 17:59:01 +01:00
return false;
}
$statement->close();
2013-12-31 18:15:45 +01:00
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_STATISTICS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
`serverLogin` varchar(50) NOT NULL,
2013-12-31 17:59:01 +01:00
`playerId` int(11) NOT NULL,
`statId` int(11) NOT NULL,
2013-12-31 17:59:01 +01:00
`value` int(20) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`index`),
UNIQUE KEY `unique` (`statId`,`playerId`,`serverLogin`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics' AUTO_INCREMENT=1;";
2013-12-31 17:31:05 +01:00
2013-12-31 18:15:45 +01:00
$statement = $this->mysqli->prepare($query);
if($this->mysqli->error) {
trigger_error($this->mysqli->error, E_USER_ERROR);
2013-12-31 17:59:01 +01:00
return false;
}
$statement->execute();
2013-12-31 18:15:45 +01:00
if($statement->error) {
2013-12-31 17:59:01 +01:00
trigger_error($statement->error, E_USER_ERROR);
2013-12-31 18:15:45 +01:00
2013-12-31 17:59:01 +01:00
return false;
}
$statement->close();
}
2013-12-31 17:31:05 +01:00
}