From 950ad1098b5c26e88b78d2c4eaf4aa4c2c1e8cd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Sun, 10 Nov 2013 17:23:00 +0100 Subject: [PATCH] - improved player and playerHandler - logs subfolder --- application/ManiaControl.php | 7 +- application/core/manialinkIdHandler.php | 49 ++++------ application/core/player.php | 120 ++++++++++++++---------- application/core/playerHandler.php | 118 +++++++++++++++++++++-- 4 files changed, 207 insertions(+), 87 deletions(-) diff --git a/application/ManiaControl.php b/application/ManiaControl.php index 207d5774..ec5e7951 100644 --- a/application/ManiaControl.php +++ b/application/ManiaControl.php @@ -13,10 +13,13 @@ if (function_exists('date_default_timezone_get') && function_exists('date_defaul // Error handling ini_set('log_errors', 1); ini_set('error_reporting', -1); -ini_set('error_log', 'ManiaControl_' . getmypid() . '.log'); +if (!is_dir('logs')) { + mkdir('logs'); +} +ini_set('error_log', 'logs/ManiaControl_' . getmypid() . '.log'); // Load ManiaControl class -require_once __DIR__ . '/core/maniaControlClass.php'; +require_once __DIR__ . '/core/maniaControl.php'; // Start ManiaControl error_log('Loading ManiaControl v' . ManiaControl::VERSION . '...'); diff --git a/application/core/manialinkIdHandler.php b/application/core/manialinkIdHandler.php index 167a9599..dff7ce76 100644 --- a/application/core/manialinkIdHandler.php +++ b/application/core/manialinkIdHandler.php @@ -3,37 +3,28 @@ namespace ManiaControl; /** - * Class manialinkidHandler handles manialink id's + * Handler for manialink ids * - * @author Lukas Kremsmayr and steeffeen + * @author kremsy & steeffeen */ - - - class ManialinkIdHandler { - /** - * Private properties - */ - private $maniaLinkIdCount; - - public function __construct(){ - $maniaLinkIdCount = 0; - } - - /** - * Reservses manialinks for a plugin - * - * @param int $count - * @return array with manialink Ids +class ManialinkIdHandler { + /** + * Private properties */ - - public function reserveManiaLinkIds($count){ - $mlIds = array(); - for($i = 0; $i < $count; $i++){ - $mlIds[0] = $i + $this->maniaLinkIdCount; - } - $this->maniaLinkIdCount += $count; - return $mlIds; - } + private $maniaLinkIdCount = 0; - } + /** + * Reserve manialink ids + * + * @param int $count + * @return array with manialink Ids + */ + public function reserveManiaLinkIds($count) { + $mlIds = array(); + for ($i = 0; $i < $count; $i++) { + array_push($mlIds, ++$this->maniaLinkIdCount); + } + return $mlIds; + } +} ?> \ No newline at end of file diff --git a/application/core/player.php b/application/core/player.php index d0d9b798..712f8574 100644 --- a/application/core/player.php +++ b/application/core/player.php @@ -5,7 +5,7 @@ namespace ManiaControl; /** * Class representing players * - * @author Kremsy & Steff + * @author kremsy & steeffeen */ class Player { /** @@ -15,66 +15,92 @@ class Player { public $pid = -1; public $login = ''; public $nickname = ''; - public $isFakePlayer = false; - public $teamName = ''; - public $ip = ''; - public $ipFull = ''; - public $clientVersion = ''; - public $zone = ''; - public $continent = ''; - public $nation = ''; - public $isSpectator = false; - public $isOfficial = false; + public $path = ''; + public $joinCount = 0; + public $totalPlayed = 0; public $language = ''; public $avatar = ''; - public $teamId; // TODO: default value - public $unlocked; // TODO: default value + public $allies = array(); + public $clubLink = ''; + public $teamId = -1; + public $isSpectator = false; + public $isOfficial = false; + public $isReferee = false; + public $ladderScore = -1.; public $ladderRank = -1; - public $ladderScore = -1; - public $created = -1; - public $rightLevel = 0; - - // TODO: usefull construct player without rpc info? - // TODO: check isFakePlayer (probably server itself also "fakeplayer") - // TODO: add all attributes like, allies, clublink ... just make vardump on rpc infos - // TODO: READ ADDITIONAL INFOS FROM DATABASE + public $joinTime = -1; + /** - * Construct a player + * Construct a player from XmlRpc data * * @param array $rpcInfos */ - public function __construct($rpcInfos) { - $this->created = time(); + public function __construct(array $rpcInfos) { if (!$rpcInfos) { return; } - $this->login = $rpcInfos['Login']; - $this->isFakePlayer = (stripos($this->login, '*') !== false); - $this->nickname = $rpcInfos['NickName']; $this->pid = $rpcInfos['PlayerId']; - $this->teamId = $rpcInfos['TeamId']; - $this->ipFull = $rpcInfos['IPAddress']; - $this->ip = preg_replace('/:\d+/', '', $this->ipFull); - $this->isSpectator = $rpcInfos['IsSpectator']; - $this->isOfficial = $rpcInfos['IsInOfficialMode']; - $this->teamName = $rpcInfos['LadderStats']['TeamName']; - $this->zone = substr($rpcInfos['Path'], 6); - $zones = explode('|', $rpcInfos['Path']); - if (isset($zones[1])) { - if (isset($zones[2])) { - $this->continent = $zones[1]; - $this->nation = $zones[2]; - } - else { - $this->nation = $zones[1]; - } - } - $this->ladderRank = $rpcInfos['LadderStats']['PlayerRankings'][0]['Ranking']; - $this->ladderScore = round($rpcInfos['LadderStats']['PlayerRankings'][0]['Score'], 2); - $this->clientVersion = $rpcInfos['ClientVersion']; + $this->login = $rpcInfos['Login']; + $this->nickname = $rpcInfos['NickName']; + $this->path = $rpcInfos['Path']; $this->language = $rpcInfos['Language']; $this->avatar = $rpcInfos['Avatar']['FileName']; + + $this->allies = $rpcInfos['Allies']; + $this->clubLink = $rpcInfos['ClubLink']; + $this->teamId = $rpcInfos['TeamId']; + $this->isSpectator = $rpcInfos['IsSpectator']; + $this->isOfficial = $rpcInfos['IsInOfficialMode']; + $this->isReferee = $rpcInfos['IsReferee']; + $this->ladderScore = $rpcInfos['LadderStats']['PlayerRankings'][0]['Score']; + $this->ladderRank = $rpcInfos['LadderStats']['PlayerRankings'][0]['Ranking']; + + $this->joinTime = time(); + } + + /** + * Check if player is not a real player + * + * @return bool + */ + public function isFakePlayer() { + return ($this->pid <= 0); + } + + /** + * Get country + * + * @return string + */ + public function getCountry() { + $pathParts = explode('|', $this->path); + if (isset($pathParts[2])) { + return $pathParts[2]; + } + if (isset($pathParts[1])) { + return $pathParts[1]; + } + if (isset($pathParts[0])) { + return $pathParts[0]; + } + return $this->path; + } + + /** + * Get continent + * + * @return string + */ + public function getContinent() { + $pathParts = explode('|', $this->path); + if (isset($pathParts[1])) { + return $pathParts[1]; + } + if (isset($pathParts[0])) { + return $pathParts[0]; + } + return $this->path; } } diff --git a/application/core/playerHandler.php b/application/core/playerHandler.php index 3486f428..d266d1b2 100644 --- a/application/core/playerHandler.php +++ b/application/core/playerHandler.php @@ -7,9 +7,9 @@ require_once __DIR__ . '/player.php'; /** * Class managing players * - * @package ManiaControl + * @author kremsy & steeffeen */ -class playerHandler { +class PlayerHandler { /** * Constants */ @@ -50,13 +50,10 @@ class playerHandler { $mysqli = $this->maniaControl->database->mysqli; $playerTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_PLAYERS . "` ( `index` int(11) NOT NULL AUTO_INCREMENT, - `pid` int(11) NOT NULL DEFAULT '-1', `login` varchar(100) COLLATE utf8_unicode_ci NOT NULL, - `ipFull` varchar(50) COLLATE utf8_unicode_ci NOT NULL, - `clientVersion` varchar(50) COLLATE utf8_unicode_ci NOT NULL, - `zone` varchar(150) COLLATE utf8_unicode_ci NOT NULL, - `language` varchar(50) COLLATE utf8_unicode_ci NOT NULL, - `avatar` varchar(100) COLLATE utf8_unicode_ci NOT NULL, + `nickname` varchar(150) COLLATE utf8_unicode_ci NOT NULL, + `path` varchar(100) COLLATE utf8_unicode_ci NOT NULL, + `totalPlayed` int(11) NOT NULL DEFAULT '0' COMMENT 'Seconds', `changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`index`), UNIQUE KEY `login` (`login`) @@ -84,6 +81,9 @@ class playerHandler { $this->maniaControl->client->query('GetPlayerList', 300, 0, 2); $playerList = $this->maniaControl->client->getResponse(); foreach ($playerList as $player) { + if ($player['PlayerId'] <= 0) { + continue; + } $callback = array(Callbacks::CB_MP_PLAYERCONNECT, array($player['Login'])); $this->playerConnect($callback); } @@ -135,6 +135,7 @@ class playerHandler { if (!$player) { return false; } + $this->savePlayer($player); $this->playerList[$player->login] = $player; return true; } @@ -143,14 +144,113 @@ class playerHandler { * Remove a Player from the PlayerList * * @param string $login + * @param bool $savePlayedTime * @return Player $player */ - private function removePlayer($login) { + private function removePlayer($login, $savePlayedTime = true) { if (!isset($this->playerList[$login])) { return null; } $player = $this->playerList[$login]; unset($this->playerList[$login]); + if ($savePlayedTime) { + $this->updatePlayedTime($player); + } return $player; } + + /** + * Save player in database and fill up object properties + * + * @param Player $player + * @param int $joinCount + * @return bool + */ + private function savePlayer(Player &$player, $joinCount = 1) { + if (!$player) { + return false; + } + $mysqli = $this->maniaControl->database->mysqli; + + // Save player + $playerQuery = "INSERT INTO `" . self::TABLE_PLAYERS . "` ( + `login`, + `nickname`, + `path`, + `joinCount` + ) VALUES ( + ?, ?, ?, ? + ) ON DUPLICATE KEY UPDATE + `index` = LAST_INSERT_ID(`index`), + `nickname` = VALUES(`nickname`), + `path` = VALUES(`path`), + `joinCount` = `joinCount` + VALUES(`joinCount`);"; + $playerStatement = $mysqli->prepare($playerQuery); + if ($mysqli->error) { + trigger_error($mysqli->error); + return false; + } + $playerStatement->bind_param('sssi', $player->login, $player->nickname, $player->path, $joinCount); + $playerStatement->execute(); + if ($playerStatement->error) { + trigger_error($playerStatement->error); + $playerStatement->close(); + return false; + } + $player->index = $playerStatement->insert_id; + $playerStatement->close(); + + // Fill up properties + $playerQuery = "SELECT `joinCount`, `totalPlayed` FROM `" . self::TABLE_PLAYERS . "` + WHERE `index` = ?;"; + $playerStatement = $mysqli->prepare($playerQuery); + if ($mysqli->error) { + trigger_error($mysqli->error); + return false; + } + $playerStatement->bind_param('i', $player->index); + $playerStatement->execute(); + if ($playerStatement->error) { + trigger_error($playerStatement->error); + $playerStatement->close(); + return false; + } + $playerStatement->store_result(); + $playerStatement->bind_result($player->joinCount, $player->totalPlayed); + $playerStatement->fetch(); + $playerStatement->free_result(); + $playerStatement->close(); + + return true; + } + + /** + * Update total played time of the player + * + * @param Player $player + * @return bool + */ + private function updatePlayedTime(Player $player) { + if (!$player) { + return false; + } + $playedTime = time() - $player->joinTime; + $mysqli = $this->maniaControl->database->mysqli; + $playedQuery = "UPDATE `" . self::TABLE_PLAYERS . "` + SET `totalPlayed` = `totalPlayed` + ?;"; + $playedStatement = $mysqli->prepare($playedQuery); + if ($mysqli->error) { + trigger_error($mysqli->error); + return false; + } + $playedStatement->bind_param('i', $playedTime); + $playedStatement->execute(); + if ($playedStatement->error) { + trigger_error($playedStatement->error); + $playedStatement->close(); + return false; + } + $playedStatement->close(); + return true; + } } \ No newline at end of file