TrackManiaControl/application/core/Statistics/StatisticManager.php

308 lines
7.2 KiB
PHP
Raw Normal View History

2013-12-31 17:31:05 +01:00
<?php
2014-01-01 18:37:32 +01:00
namespace ManiaControl\Statistics;
use ManiaControl\ManiaControl;
2014-01-01 18:29:28 +01:00
use ManiaControl\Players\Player;
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
*/
2014-01-01 18:29:28 +01:00
//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';
2014-01-01 18:29:28 +01:00
const TABLE_STATISTICS = 'mc_statistics';
2014-01-03 19:14:07 +01:00
const STAT_TYPE_INT = '0';
const STAT_TYPE_TIME = '1';
2014-01-01 18:29:28 +01:00
/**
* Public Properties
*/
public $statisticCollector = null;
2013-12-31 17:59:01 +01:00
/**
* Private Properties
*/
private $maniaControl = null;
2014-01-01 18:29:28 +01:00
private $stats = array();
2013-12-31 17:59:01 +01:00
/**
* Construct player manager
*
2014-01-01 18:29:28 +01:00
* @param \ManiaControl\ManiaControl $maniaControl
2013-12-31 17:59:01 +01:00
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->initTables();
2014-01-01 18:29:28 +01:00
$this->statisticCollector = new StatisticCollector($maniaControl);
//Store Stats MetaData
$this->storeStatMetaData();
}
/**
* Get the value of an statistic
*
* @param $statName
* @param $playerId
2014-01-03 21:06:24 +01:00
* @param int $serverId
2014-01-01 18:29:28 +01:00
* @return int
*/
2014-01-03 21:06:24 +01:00
public function getStatisticData($statName, $playerId, $serverId = -1) {
$mysqli = $this->maniaControl->database->mysqli;
$statId = $this->getStatId($statName);
2014-01-01 18:29:28 +01:00
if($statId == null) {
return -1;
}
2014-01-03 21:06:24 +01:00
if($serverId == -1) {
2014-01-01 18:29:28 +01:00
$query = "SELECT SUM(value) as value FROM `" . self::TABLE_STATISTICS . "` WHERE `statId` = " . $statId . " AND `playerId` = " . $playerId . ";";
} else {
2014-01-03 18:37:51 +01:00
$query = "SELECT value FROM `" . self::TABLE_STATISTICS . "` WHERE `statId` = " . $statId . " AND `playerId` = " . $playerId . " AND `serverLogin` = '" . $serverId . "';";
2014-01-01 18:29:28 +01:00
}
$result = $mysqli->query($query);
2014-01-01 18:29:28 +01:00
if(!$result) {
trigger_error($mysqli->error);
2014-01-01 18:29:28 +01:00
}
$row = $result->fetch_object();
$result->close();
return $row->value;
}
/**
* Store Stats Meta Data from the Database
*/
private function storeStatMetaData() {
$mysqli = $this->maniaControl->database->mysqli;
2014-01-01 18:29:28 +01:00
$query = "SELECT * FROM `" . self::TABLE_STATMETADATA . "`;";
$result = $mysqli->query($query);
2014-01-01 18:29:28 +01:00
if(!$result) {
trigger_error($mysqli->error);
2014-01-01 18:29:28 +01:00
}
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];
2014-01-02 15:13:59 +01:00
return (int)$stat->index;
2014-01-01 18:29:28 +01:00
} else {
return null;
}
}
2014-01-03 18:37:51 +01:00
/**
* Get all statistics of a certain palyer
*
* @param Player $player
2014-01-03 21:06:24 +01:00
* @param int $serverId
2014-01-03 18:37:51 +01:00
* @return array
*/
2014-01-03 21:06:24 +01:00
public function getAllPlayerStats(Player $player, $serverId = -1) {
2014-01-03 18:37:51 +01:00
$playerStats = array(); //TODO improve performence
foreach($this->stats as $stat) {
2014-01-03 21:06:24 +01:00
$value = $this->getStatisticData($stat->name, $player->index, $serverId);
2014-01-03 18:37:51 +01:00
$playerStats[$stat->name] = array($stat, $value);
2014-01-03 17:36:10 +01:00
}
return $playerStats;
}
2014-01-03 18:37:51 +01:00
2014-01-01 18:29:28 +01:00
/**
* Inserts a Stat into the database
*
* @param $statName
* @param Player $player
2014-01-03 21:06:24 +01:00
* @param int $serverId
2014-01-01 18:29:28 +01:00
* @param $value , value to Add
* @param string $statType
2014-01-03 21:06:24 +01:00
* @internal param string $serverLogin
* @return bool
2014-01-01 18:29:28 +01:00
*/
2014-01-03 21:06:24 +01:00
public function insertStat($statName, $player, $serverId = -1, $value, $statType = self::STAT_TYPE_INT) {
$statId = $this->getStatId($statName);
2014-01-01 18:29:28 +01:00
2014-01-02 18:48:27 +01:00
if($player == null) {
return false;
}
2014-01-01 18:29:28 +01:00
if($statId == null) {
return false;
}
if($player->isFakePlayer()) {
return true;
}
2014-01-03 21:06:24 +01:00
if($serverId == -1) {
$serverId = $this->maniaControl->server->getServerId();
}
2014-01-02 14:31:30 +01:00
2014-01-02 15:13:59 +01:00
$mysqli = $this->maniaControl->database->mysqli;
2014-01-02 14:31:30 +01:00
2014-01-01 18:29:28 +01:00
$query = "INSERT INTO `" . self::TABLE_STATISTICS . "` (
2014-01-03 18:37:51 +01:00
`serverId`,
2014-01-01 18:29:28 +01:00
`playerId`,
`statId`,
`value`
) VALUES (
?, ?, ?, ?
) ON DUPLICATE KEY UPDATE
`value` = `value` + VALUES(`value`);";
$statement = $mysqli->prepare($query);
if($mysqli->error) {
trigger_error($mysqli->error);
2014-01-01 18:29:28 +01:00
return false;
}
2014-01-03 19:31:30 +01:00
2014-01-03 18:37:51 +01:00
$statement->bind_param('iiii', $serverId, $player->index, $statId, $value);
2014-01-01 18:29:28 +01:00
$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 \ManiaControl\Players\Player $player
2014-01-03 21:06:24 +01:00
* @param int $serverId
* @internal param string $serverLogin
* @internal param \ManiaControl\Players\Player $playerId
2014-01-01 18:29:28 +01:00
* @return bool
*/
2014-01-03 21:06:24 +01:00
public function incrementStat($statName, $player, $serverId = -1) {
return $this->insertStat($statName, $player, $serverId, 1);
2013-12-31 17:59:01 +01:00
}
2014-01-03 19:14:07 +01:00
2013-12-31 18:15:45 +01:00
/**
* Defines a Stat
*
2014-01-03 19:14:07 +01:00
* @param $statName
2014-01-03 18:37:51 +01:00
* @param string $type
2014-01-03 19:14:07 +01:00
* @param string $statDescription
* @return bool
2013-12-31 18:15:45 +01:00
*/
2014-01-03 19:14:07 +01:00
public function defineStatMetaData($statName, $type = self::STAT_TYPE_INT, $statDescription = '') {
$mysqli = $this->maniaControl->database->mysqli;
2014-01-03 19:14:07 +01:00
$query = "INSERT INTO `" . self::TABLE_STATMETADATA . "` (
2014-01-01 18:29:28 +01:00
`name`,
2014-01-03 18:37:51 +01:00
`type`,
2014-01-01 18:29:28 +01:00
`description`
2013-12-31 18:15:45 +01:00
) VALUES (
2014-01-03 18:37:51 +01:00
?, ?, ?
2014-01-03 19:14:07 +01:00
) ON DUPLICATE KEY UPDATE
`type` = VALUES(`type`),
`description` = VALUES(`description`);";
$statement = $mysqli->prepare($query);
if($mysqli->error) {
trigger_error($mysqli->error);
2013-12-31 18:15:45 +01:00
return false;
}
2014-01-03 19:14:07 +01:00
$statement->bind_param('sis', $statName, $type, $statDescription);
2013-12-31 18:15:45 +01:00
$statement->execute();
2014-01-01 18:29:28 +01:00
if($statement->error) {
2013-12-31 18:15:45 +01:00
trigger_error($statement->error);
$statement->close();
return false;
}
2014-01-01 18:29:28 +01:00
2013-12-31 18:15:45 +01:00
$statement->close();
return true;
2013-12-31 18:15:45 +01:00
}
2014-01-03 17:36:10 +01:00
2013-12-31 17:59:01 +01:00
/**
2014-01-01 18:29:28 +01:00
* Initialize necessary database tables
2013-12-31 17:59:01 +01:00
*
* @return bool
*/
private function initTables() {
$mysqli = $this->maniaControl->database->mysqli;
$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,
2014-01-03 19:14:07 +01:00
`type` int(5) 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;";
2014-01-01 18:29:28 +01:00
$statement = $mysqli->prepare($query);
if($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
2014-01-01 18:29:28 +01:00
2013-12-31 17:59:01 +01:00
return false;
}
$statement->execute();
2014-01-01 18:29:28 +01:00
if($statement->error) {
2013-12-31 17:59:01 +01:00
trigger_error($statement->error, E_USER_ERROR);
2014-01-01 18:29:28 +01:00
2013-12-31 17:59:01 +01:00
return false;
}
$statement->close();
2014-01-01 18:29:28 +01:00
2013-12-31 18:15:45 +01:00
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_STATISTICS . "` (
2014-01-01 18:29:28 +01:00
`index` int(11) NOT NULL AUTO_INCREMENT,
2014-01-03 18:37:51 +01:00
`serverId` int(11) NOT NULL,
2013-12-31 17:59:01 +01:00
`playerId` int(11) NOT NULL,
2014-01-01 18:29:28 +01:00
`statId` int(11) NOT NULL,
2013-12-31 17:59:01 +01:00
`value` int(20) COLLATE utf8_unicode_ci NOT NULL,
2014-01-01 18:29:28 +01:00
PRIMARY KEY (`index`),
2014-01-03 18:37:51 +01:00
UNIQUE KEY `unique` (`statId`,`playerId`,`serverId`)
2014-01-01 18:29:28 +01:00
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics' AUTO_INCREMENT=1;";
$statement = $mysqli->prepare($query);
if($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
2014-01-01 18:29:28 +01:00
2013-12-31 17:59:01 +01:00
return false;
}
$statement->execute();
2014-01-01 18:29:28 +01:00
if($statement->error) {
2013-12-31 17:59:01 +01:00
trigger_error($statement->error, E_USER_ERROR);
2014-01-01 18:29:28 +01:00
2013-12-31 17:59:01 +01:00
return false;
}
$statement->close();
return true;
2013-12-31 17:59:01 +01:00
}
2014-01-01 18:29:28 +01:00
}