2014-04-21 02:01:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl\Players;
|
|
|
|
|
|
|
|
|
|
|
|
use ManiaControl\ManiaControl;
|
2014-05-13 16:15:46 +02:00
|
|
|
use ManiaControl\Utils\ClassUtil;
|
2014-04-21 02:01:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Player Data Manager
|
|
|
|
*
|
2014-05-02 17:50:30 +02:00
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
|
|
|
* @copyright 2014 ManiaControl Team
|
2014-04-21 02:01:30 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
|
|
|
*/
|
|
|
|
class PlayerDataManager {
|
|
|
|
const TABLE_PLAYERDATAMETADATA = 'mc_playerdata_metadata';
|
|
|
|
const TABLE_PLAYERDATA = 'mc_playerdata';
|
|
|
|
const TYPE_STRING = 'string';
|
|
|
|
const TYPE_INT = 'int';
|
|
|
|
const TYPE_REAL = 'real';
|
|
|
|
const TYPE_BOOL = 'bool';
|
|
|
|
const TYPE_ARRAY = 'array';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Private Properties
|
|
|
|
*/
|
|
|
|
private $maniaControl = null;
|
|
|
|
private $arrayDelimiter = ';;';
|
|
|
|
private $metaData = array();
|
2014-04-22 22:42:34 +02:00
|
|
|
private $storedData = array();
|
2014-04-21 02:01:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct player manager
|
|
|
|
*
|
|
|
|
* @param \ManiaControl\ManiaControl $maniaControl
|
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
$this->initTables();
|
|
|
|
|
|
|
|
// Store Stats MetaData
|
2014-04-22 22:42:34 +02:00
|
|
|
$this->storeMetaData();
|
|
|
|
}
|
|
|
|
|
2014-05-02 17:50:30 +02:00
|
|
|
/**
|
|
|
|
* Initialize necessary database tables
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function initTables() {
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
$defaultType = "'" . self::TYPE_STRING . "'";
|
|
|
|
$typeSet = $defaultType . ",'" . self::TYPE_INT . "','" . self::TYPE_REAL . "','" . self::TYPE_BOOL . "','" . self::TYPE_ARRAY . "'";
|
|
|
|
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_PLAYERDATAMETADATA . "` (
|
|
|
|
`dataId` int(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
`class` varchar(100) NOT NULL,
|
|
|
|
`dataName` varchar(100) NOT NULL,
|
|
|
|
`type` set({$typeSet}) NOT NULL DEFAULT {$defaultType},
|
|
|
|
`defaultValue` varchar(150) NOT NULL,
|
|
|
|
`description` varchar(150) NOT NULL,
|
|
|
|
PRIMARY KEY (`dataId`),
|
|
|
|
UNIQUE KEY `name` (`class`, `dataName`)
|
|
|
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Player-Data MetaData' AUTO_INCREMENT=1;";
|
|
|
|
$statement = $mysqli->prepare($query);
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$statement->execute();
|
|
|
|
if ($statement->error) {
|
|
|
|
trigger_error($statement->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$statement->close();
|
|
|
|
|
|
|
|
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_PLAYERDATA . "` (
|
|
|
|
`index` int(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
`serverIndex` int(11) NOT NULL,
|
|
|
|
`playerId` int(11) NOT NULL,
|
|
|
|
`dataId` int(11) NOT NULL,
|
|
|
|
`value` varchar(150) NOT NULL,
|
|
|
|
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
|
|
PRIMARY KEY (`index`),
|
|
|
|
UNIQUE KEY `unique` (`dataId`,`playerId`,`serverIndex`)
|
|
|
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Player Data' AUTO_INCREMENT=1;";
|
|
|
|
$statement = $mysqli->prepare($query);
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$statement->execute();
|
|
|
|
if ($statement->error) {
|
|
|
|
trigger_error($statement->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$statement->close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store Meta Data from the Database in the Ram
|
|
|
|
*/
|
|
|
|
private function storeMetaData() {
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
|
|
|
|
$query = "SELECT * FROM `" . self::TABLE_PLAYERDATAMETADATA . "`;";
|
|
|
|
$result = $mysqli->query($query);
|
|
|
|
if (!$result) {
|
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ($row = $result->fetch_object()) {
|
|
|
|
$this->metaData[$row->class . $row->dataName] = $row;
|
|
|
|
}
|
2014-05-18 23:34:47 +02:00
|
|
|
$result->free();
|
2014-05-02 17:50:30 +02:00
|
|
|
}
|
|
|
|
|
2014-04-22 22:42:34 +02:00
|
|
|
/**
|
|
|
|
* Destroys the stored PlayerData (Method get called by PlayerManager, so don't call it anywhere else)
|
|
|
|
*
|
|
|
|
* @param Player $player
|
|
|
|
*/
|
|
|
|
public function destroyPlayerData(Player $player) {
|
|
|
|
unset($this->storedData[$player->index]);
|
2014-04-21 02:01:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the Player-Data MetaData
|
|
|
|
*
|
2014-05-13 16:40:05 +02:00
|
|
|
* @param mixed $object
|
|
|
|
* @param string $dataName
|
|
|
|
* @param mixed $default
|
|
|
|
* @param string $dataDescription (optional)
|
2014-04-21 02:01:30 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function defineMetaData($object, $dataName, $default, $dataDescription = '') {
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2014-05-13 16:15:46 +02:00
|
|
|
$className = ClassUtil::getClass($object);
|
2014-04-21 02:01:30 +02:00
|
|
|
|
|
|
|
$query = "INSERT INTO `" . self::TABLE_PLAYERDATAMETADATA . "` (
|
|
|
|
`class`,
|
|
|
|
`dataName`,
|
|
|
|
`type`,
|
|
|
|
`defaultValue`,
|
|
|
|
`description`
|
|
|
|
) VALUES (
|
|
|
|
?, ?, ?, ?, ?
|
|
|
|
) ON DUPLICATE KEY UPDATE
|
|
|
|
`type` = VALUES(`type`),
|
|
|
|
`defaultValue` = VALUES(`defaultValue`),
|
|
|
|
`description` = VALUES(`description`);";
|
|
|
|
$statement = $mysqli->prepare($query);
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$type = $this->getType($default);
|
|
|
|
|
|
|
|
$statement->bind_param('sssss', $className, $dataName, $type, $default, $dataDescription);
|
|
|
|
$statement->execute();
|
|
|
|
if ($statement->error) {
|
|
|
|
trigger_error($statement->error);
|
|
|
|
$statement->close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$statement->close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-02 17:50:30 +02:00
|
|
|
/**
|
|
|
|
* Get Type of a Parameter
|
|
|
|
*
|
|
|
|
* @param mixed $param
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getType($param) {
|
|
|
|
if (is_int($param)) {
|
|
|
|
return self::TYPE_INT;
|
2014-04-21 02:01:30 +02:00
|
|
|
}
|
2014-05-02 17:50:30 +02:00
|
|
|
if (is_real($param)) {
|
|
|
|
return self::TYPE_REAL;
|
|
|
|
}
|
|
|
|
if (is_bool($param)) {
|
|
|
|
return self::TYPE_BOOL;
|
|
|
|
}
|
|
|
|
if (is_string($param)) {
|
|
|
|
return self::TYPE_STRING;
|
|
|
|
}
|
|
|
|
if (is_array($param)) {
|
|
|
|
return self::TYPE_ARRAY;
|
|
|
|
}
|
2014-05-09 13:00:04 +02:00
|
|
|
trigger_error('Unsupported data type. ' . print_r($param, true));
|
2014-05-02 17:50:30 +02:00
|
|
|
return null;
|
2014-04-21 02:01:30 +02:00
|
|
|
}
|
|
|
|
|
2014-04-22 22:42:34 +02:00
|
|
|
/**
|
|
|
|
* Gets the Player Data
|
|
|
|
*
|
2014-05-13 16:40:05 +02:00
|
|
|
* @param mixed $object
|
|
|
|
* @param string $dataName
|
|
|
|
* @param Player $player
|
|
|
|
* @param int $serverIndex
|
|
|
|
* @return mixed
|
2014-04-22 22:42:34 +02:00
|
|
|
*/
|
2014-05-02 16:13:45 +02:00
|
|
|
public function getPlayerData($object, $dataName, Player $player, $serverIndex = -1) {
|
2014-05-13 16:15:46 +02:00
|
|
|
$className = ClassUtil::getClass($object);
|
2014-04-22 22:42:34 +02:00
|
|
|
|
2014-05-02 16:13:45 +02:00
|
|
|
$meta = $this->metaData[$className . $dataName];
|
2014-04-22 22:42:34 +02:00
|
|
|
|
2014-07-29 00:05:27 +02:00
|
|
|
// Check if data is already in the ram
|
|
|
|
if (isset($this->storedData[$player->index]) && isset($this->storedData[$player->index][$meta->dataId])) {
|
|
|
|
return $this->storedData[$player->index][$meta->dataId];
|
2014-04-22 22:42:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
$dataQuery = "SELECT `value` FROM `" . self::TABLE_PLAYERDATA . "`
|
|
|
|
WHERE `dataId` = ?
|
|
|
|
AND `playerId` = ?
|
|
|
|
AND `serverIndex` = ?;";
|
|
|
|
$dataStatement = $mysqli->prepare($dataQuery);
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$dataStatement->bind_param('iii', $meta->dataId, $player->index, $serverIndex);
|
|
|
|
$dataStatement->execute();
|
|
|
|
if ($dataStatement->error) {
|
|
|
|
trigger_error($dataStatement->error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$dataStatement->store_result();
|
|
|
|
if ($dataStatement->num_rows <= 0) {
|
2014-05-02 16:13:45 +02:00
|
|
|
$this->setPlayerData($object, $dataName, $player, $meta->defaultValue, $serverIndex);
|
2014-04-22 22:42:34 +02:00
|
|
|
return $meta->default;
|
|
|
|
}
|
|
|
|
$dataStatement->bind_result($value);
|
|
|
|
$dataStatement->fetch();
|
|
|
|
$dataStatement->free_result();
|
|
|
|
$dataStatement->close();
|
|
|
|
$data = $this->castSetting($meta->type, $value);
|
|
|
|
|
|
|
|
//Store setting in the ram
|
|
|
|
if (!isset($this->storedData[$player->index])) {
|
|
|
|
$this->storedData[$player->index] = array();
|
|
|
|
}
|
|
|
|
$this->storedData[$player->index][$meta->dataId] = $data;
|
|
|
|
return $data;
|
|
|
|
}
|
2014-04-21 02:01:30 +02:00
|
|
|
|
|
|
|
/**
|
2014-04-22 22:42:34 +02:00
|
|
|
* Set a PlayerData to a specific defined statMetaData
|
2014-04-21 02:01:30 +02:00
|
|
|
*
|
2014-05-13 16:40:05 +02:00
|
|
|
* @param mixed $object
|
|
|
|
* @param string $dataName
|
|
|
|
* @param Player $player
|
|
|
|
* @param mixed $value
|
|
|
|
* @param int $serverIndex (empty if it's global)
|
2014-04-21 02:01:30 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-04-22 22:42:34 +02:00
|
|
|
public function setPlayerData($object, $dataName, Player $player, $value, $serverIndex = -1) {
|
2014-05-13 16:15:46 +02:00
|
|
|
$className = ClassUtil::getClass($object);
|
2014-04-21 02:01:30 +02:00
|
|
|
if (!$player) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-22 22:42:34 +02:00
|
|
|
$dataId = $this->getMetaDataId($className, $dataName);
|
2014-04-21 02:01:30 +02:00
|
|
|
if (!$dataId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
$query = "INSERT INTO `" . self::TABLE_PLAYERDATA . "` (
|
|
|
|
`serverIndex`,
|
|
|
|
`playerId`,
|
|
|
|
`dataId`,
|
|
|
|
`value`
|
|
|
|
) VALUES (
|
|
|
|
?, ?, ?, ?
|
|
|
|
) ON DUPLICATE KEY UPDATE
|
|
|
|
`value` = VALUES(`value`);";
|
|
|
|
$statement = $mysqli->prepare($query);
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$statement->bind_param('iiis', $serverIndex, $player->index, $dataId, $value);
|
|
|
|
$statement->execute();
|
|
|
|
if ($statement->error) {
|
|
|
|
trigger_error($statement->error);
|
|
|
|
$statement->close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$statement->close();
|
2014-04-22 22:42:34 +02:00
|
|
|
|
2014-07-29 00:05:27 +02:00
|
|
|
// Store changed value
|
|
|
|
if (!isset($this->storedData[$player->index])) {
|
|
|
|
$this->storedData[$player->index] = array();
|
2014-04-22 22:42:34 +02:00
|
|
|
}
|
2014-07-29 00:05:27 +02:00
|
|
|
$this->storedData[$player->index][$dataId] = $value;
|
2014-04-22 22:42:34 +02:00
|
|
|
|
2014-04-21 02:01:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the Id of the MetaData
|
|
|
|
*
|
2014-05-02 16:13:45 +02:00
|
|
|
* @param string $className
|
|
|
|
* @param string $statName
|
2014-04-21 02:01:30 +02:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function getMetaDataId($className, $statName) {
|
|
|
|
if (isset($this->metaData[$className . $statName])) {
|
|
|
|
$stat = $this->metaData[$className . $statName];
|
|
|
|
return (int)$stat->dataId;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cast a Setting to the given Type
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param mixed $value
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
private function castSetting($type, $value) {
|
|
|
|
if ($type === self::TYPE_INT) {
|
|
|
|
return (int)$value;
|
|
|
|
}
|
|
|
|
if ($type === self::TYPE_REAL) {
|
|
|
|
return (float)$value;
|
|
|
|
}
|
|
|
|
if ($type === self::TYPE_BOOL) {
|
|
|
|
return (bool)$value;
|
|
|
|
}
|
|
|
|
if ($type === self::TYPE_STRING) {
|
|
|
|
return (string)$value;
|
|
|
|
}
|
|
|
|
if ($type === self::TYPE_ARRAY) {
|
|
|
|
return explode($this->arrayDelimiter, $value);
|
|
|
|
}
|
2014-05-09 13:00:04 +02:00
|
|
|
trigger_error('Unsupported data type. ' . print_r($type, true));
|
2014-04-21 02:01:30 +02:00
|
|
|
return $value;
|
|
|
|
}
|
2014-07-29 00:05:27 +02:00
|
|
|
}
|