TrackManiaControl/application/core/Statistics/StatisticManager.php

112 lines
2.8 KiB
PHP
Raw Normal View History

2013-12-31 17:31:05 +01:00
<?php
2013-12-31 21:41:52 +01:00
namespace ManiaControl\Statistics;
use ManiaControl\ManiaControl;
2013-12-31 17:59:01 +01:00
2013-12-31 17:31:05 +01:00
/**
* Statistic Manager Class
*
* @author steeffeen & kremsy
*/
class StatisticManager {
2013-12-31 17:59:01 +01:00
/**
* Constants
*/
const TABLE_STATMETADATA = 'mc_statmetadata';
const TABLE_STATISTICS = 'mc_statistics';
2013-12-31 17:59:01 +01:00
/**
* Private Properties
*/
private $maniaControl = null;
/**
* Construct player manager
*
* @param ManiaControl $maniaControl
2013-12-31 17:59:01 +01:00
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->initTables();
}
2013-12-31 18:15:45 +01:00
/**
* Defines a Stat
*
* @param string $statName
* @param string $statDescription
* @return bool
2013-12-31 18:15:45 +01:00
*/
public function defineStatMetaData($statName, $statDescription = '') {
$mysqli = $this->maniaControl->database->mysqli;
$query = "INSERT IGNORE INTO `" . self::TABLE_STATMETADATA . "` (
`name`,
`description`
2013-12-31 18:15:45 +01:00
) VALUES (
?, ?
2013-12-31 18:15:45 +01:00
);";
$statement = $mysqli->prepare($query);
if ($mysqli->error) {
trigger_error($mysqli->error);
2013-12-31 18:15:45 +01:00
return false;
}
$statement->bind_param('ss', $statName, $statDescription);
$statement->execute();
if ($statement->error) {
2013-12-31 18:15:45 +01:00
trigger_error($statement->error);
$statement->close();
return false;
}
$statement->close();
return true;
2013-12-31 18:15:45 +01:00
}
2013-12-31 17:59:01 +01:00
/**
* Initialize necessary Database Tables
2013-12-31 17:59:01 +01:00
*
* @return bool
*/
private function initTables() {
$mysqli = $this->maniaControl->database->mysqli;
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;";
$statement = $mysqli->prepare($query);
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
2013-12-31 17:59:01 +01:00
return false;
}
$statement->execute();
if ($statement->error) {
2013-12-31 17:59:01 +01:00
trigger_error($statement->error, E_USER_ERROR);
return false;
}
$statement->close();
2013-12-31 18:15:45 +01:00
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_STATISTICS . "` (
`statId` int(11) NOT NULL AUTO_INCREMENT,
2013-12-31 17:59:01 +01:00
`playerId` int(11) NOT NULL,
`serverId` int(11) NOT NULL,
2013-12-31 17:59:01 +01:00
`value` int(20) COLLATE utf8_unicode_ci NOT NULL,
UNIQUE KEY `unique` (`statId`,`playerId`,`serverId`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics Data' AUTO_INCREMENT=1;";
$statement = $mysqli->prepare($query);
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
2013-12-31 17:59:01 +01:00
return false;
}
$statement->execute();
if ($statement->error) {
2013-12-31 17:59:01 +01:00
trigger_error($statement->error, E_USER_ERROR);
return false;
}
$statement->close();
return true;
2013-12-31 17:59:01 +01:00
}
}