Removed stats from Player Class

This commit is contained in:
Steffen Schröder 2014-01-01 18:53:19 +01:00
parent 31fdecafa5
commit 6c56938ddd
2 changed files with 99 additions and 107 deletions

View File

@ -1,10 +1,11 @@
<?php <?php
namespace ManiaControl\Players; namespace ManiaControl\Players;
use ManiaControl\Formatter; use ManiaControl\Formatter;
/** /**
* Class representing players * Player Model Class
* *
* @author kremsy & steeffeen * @author kremsy & steeffeen
*/ */
@ -18,8 +19,6 @@ class Player {
public $nickname = ''; public $nickname = '';
public $path = ''; public $path = '';
public $authLevel = 0; public $authLevel = 0;
public $joinCount = 0;
public $totalPlayed = 0;
public $language = ''; public $language = '';
public $avatar = ''; public $avatar = '';
public $allies = array(); public $allies = array();
@ -32,10 +31,11 @@ class Player {
public $ladderRank = -1; public $ladderRank = -1;
public $joinTime = -1; public $joinTime = -1;
public $ipAddress = ''; public $ipAddress = '';
/** /**
* Construct a player from XmlRpc data * Construct a player from XmlRpc data
* *
* @param array $rpcInfos * @param array $rpcInfos
*/ */
public function __construct(array $rpcInfos) { public function __construct(array $rpcInfos) {
if (!$rpcInfos) { if (!$rpcInfos) {
@ -55,14 +55,12 @@ class Player {
$this->isReferee = $rpcInfos['IsReferee']; $this->isReferee = $rpcInfos['IsReferee'];
$this->ladderScore = $rpcInfos['LadderStats']['PlayerRankings'][0]['Score']; $this->ladderScore = $rpcInfos['LadderStats']['PlayerRankings'][0]['Score'];
$this->ladderRank = $rpcInfos['LadderStats']['PlayerRankings'][0]['Ranking']; $this->ladderRank = $rpcInfos['LadderStats']['PlayerRankings'][0]['Ranking'];
$this->ipAddress = $rpcInfos['IPAddress']; $this->ipAddress = $rpcInfos['IPAddress'];
$this->joinTime = time(); $this->joinTime = time();
} }
/** /**
* Check if player is not a real player * Check if player is not a real player
* *
@ -72,7 +70,6 @@ class Player {
return ($this->pid <= 0 || $this->path == ""); return ($this->pid <= 0 || $this->path == "");
} }
/** /**
* Get province * Get province
* *

View File

@ -19,49 +19,51 @@ class PlayerManager implements CallbackListener {
/** /**
* Constants * Constants
*/ */
const CB_PLAYERJOINED = 'PlayerManagerCallback.PlayerJoined'; const CB_PLAYERJOINED = 'PlayerManagerCallback.PlayerJoined';
const CB_PLAYERDISCONNECTED = 'PlayerManagerCallback.PlayerDisconnected'; const CB_PLAYERDISCONNECTED = 'PlayerManagerCallback.PlayerDisconnected';
const CB_ONINIT = 'PlayerManagerCallback.OnInit'; const CB_ONINIT = 'PlayerManagerCallback.OnInit';
const CB_PLAYERINFOCHANGED = 'PlayerManagerCallback.PlayerInfoChanged'; const CB_PLAYERINFOCHANGED = 'PlayerManagerCallback.PlayerInfoChanged';
const TABLE_PLAYERS = 'mc_players'; const TABLE_PLAYERS = 'mc_players';
const SETTING_JOIN_LEAVE_MESSAGES = 'Enable Join & Leave Messages'; const SETTING_JOIN_LEAVE_MESSAGES = 'Enable Join & Leave Messages';
const STAT_JOIN_COUNT = 'joinCount'; const STAT_JOIN_COUNT = 'joinCount';
const STAT_PLAYTIME = 'playTime'; const STAT_PLAYTIME = 'playTime';
/** /**
* Private properties * Public Properties
*/
private $maniaControl = null;
private $playerCommands = null;
private $playerList = array();
/**
* Public properties
*/ */
public $playerActions = null; public $playerActions = null;
public $playerCommands = null;
public $playerList = array();
/**
* Private Properties
*/
private $maniaControl = null;
/** /**
* Construct player manager * Construct a new Player Manager
* *
* @param \ManiaControl\ManiaControl $maniaControl * @param \ManiaControl\ManiaControl $maniaControl
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->initTables(); $this->initTables();
$this->playerCommands = new PlayerCommands($maniaControl); $this->playerCommands = new PlayerCommands($maniaControl);
$this->playerActions = new PlayerActions($maniaControl); $this->playerActions = new PlayerActions($maniaControl);
// Init settings // Init settings
$this->maniaControl->settingManager->initSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES, true); $this->maniaControl->settingManager->initSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES, true);
// Register for callbacks // Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'onInit'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'onInit');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCONNECT, $this, 'playerConnect'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCONNECT, $this, 'playerConnect');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERDISCONNECT, $this, 'playerDisconnect'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERDISCONNECT, $this,
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERINFOCHANGED, $this, 'playerInfoChanged'); 'playerDisconnect');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERINFOCHANGED, $this,
'playerInfoChanged');
// Define player stats
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_JOIN_COUNT); $this->maniaControl->statisticManager->defineStatMetaData(self::STAT_JOIN_COUNT);
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_PLAYTIME); $this->maniaControl->statisticManager->defineStatMetaData(self::STAT_PLAYTIME);
} }
@ -72,26 +74,24 @@ class PlayerManager implements CallbackListener {
* @return bool * @return bool
*/ */
private function initTables() { private function initTables() {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$playerTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_PLAYERS . "` ( $playerTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_PLAYERS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT, `index` int(11) NOT NULL AUTO_INCREMENT,
`login` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `login` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`nickname` varchar(150) COLLATE utf8_unicode_ci NOT NULL, `nickname` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
`path` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `path` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`authLevel` int(11) NOT NULL DEFAULT '0', `authLevel` int(11) NOT NULL DEFAULT '0',
`joinCount` int(11) NOT NULL DEFAULT '0',
`totalPlayed` int(11) NOT NULL DEFAULT '0' COMMENT 'Seconds',
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`index`), PRIMARY KEY (`index`),
UNIQUE KEY `login` (`login`) UNIQUE KEY `login` (`login`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Player data' AUTO_INCREMENT=1;"; ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Player Data' AUTO_INCREMENT=1;";
$playerTableStatement = $mysqli->prepare($playerTableQuery); $playerTableStatement = $mysqli->prepare($playerTableQuery);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR); trigger_error($mysqli->error, E_USER_ERROR);
return false; return false;
} }
$playerTableStatement->execute(); $playerTableStatement->execute();
if($playerTableStatement->error) { if ($playerTableStatement->error) {
trigger_error($playerTableStatement->error, E_USER_ERROR); trigger_error($playerTableStatement->error, E_USER_ERROR);
return false; return false;
} }
@ -108,16 +108,16 @@ class PlayerManager implements CallbackListener {
// Add all players // Add all players
$this->maniaControl->client->query('GetPlayerList', 300, 0, 2); $this->maniaControl->client->query('GetPlayerList', 300, 0, 2);
$playerList = $this->maniaControl->client->getResponse(); $playerList = $this->maniaControl->client->getResponse();
foreach($playerList as $playerItem) { foreach ($playerList as $playerItem) {
if($playerItem['PlayerId'] <= 0) { if ($playerItem['PlayerId'] <= 0) {
continue; continue;
} }
$this->maniaControl->client->query('GetDetailedPlayerInfo', $playerItem['Login']); $this->maniaControl->client->query('GetDetailedPlayerInfo', $playerItem['Login']);
$playerInfo = $this->maniaControl->client->getResponse(); $playerInfo = $this->maniaControl->client->getResponse();
$player = new Player($playerInfo); $player = new Player($playerInfo);
$this->addPlayer($player); $this->addPlayer($player);
} }
// Trigger own callback // Trigger own callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_ONINIT, array(self::CB_ONINIT)); $this->maniaControl->callbackManager->triggerCallback(self::CB_ONINIT, array(self::CB_ONINIT));
} }
@ -131,20 +131,22 @@ class PlayerManager implements CallbackListener {
$login = $callback[1][0]; $login = $callback[1][0];
$this->maniaControl->client->query('GetDetailedPlayerInfo', $login); $this->maniaControl->client->query('GetDetailedPlayerInfo', $login);
$playerInfo = $this->maniaControl->client->getResponse(); $playerInfo = $this->maniaControl->client->getResponse();
$player = new Player($playerInfo); $player = new Player($playerInfo);
$this->addPlayer($player); $this->addPlayer($player);
if($this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES) && !$player->isFakePlayer()) { if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES) && !$player->isFakePlayer()) {
$string = array(0 => '$0f0Player', 1 => '$0f0Moderator', 2 => '$0f0Admin', 3 => '$0f0MasterAdmin', 4 => '$0f0MasterAdmin'); $string = array(0 => '$0f0Player', 1 => '$0f0Moderator', 2 => '$0f0Admin', 3 => '$0f0MasterAdmin', 4 => '$0f0MasterAdmin');
$chatMessage = '$s$0f0' . $string[$player->authLevel] . ' $fff' . $player->nickname . '$z$s$0f0 Nation:$fff ' . $player->getCountry() . ' $z$s$0f0joined!'; $chatMessage = '$s$0f0' . $string[$player->authLevel] . ' $fff' . $player->nickname . '$z$s$0f0 Nation:$fff ' .
$player->getCountry() . ' $z$s$0f0joined!';
$this->maniaControl->chat->sendChat($chatMessage); $this->maniaControl->chat->sendChat($chatMessage);
$this->maniaControl->chat->sendInformation('This server uses ManiaControl v' . ManiaControl::VERSION . '!', $player->login); $this->maniaControl->chat->sendInformation('This server uses ManiaControl v' . ManiaControl::VERSION . '!', $player->login);
} }
$logMessage = "Player joined: {$player->login} / " . Formatter::stripCodes($player->nickname) . " Nation: " . $player->getCountry() . " IP: {$player->ipAddress}"; $logMessage = "Player joined: {$player->login} / " . Formatter::stripCodes($player->nickname) . " Nation: " .
$player->getCountry() . " IP: {$player->ipAddress}";
$this->maniaControl->log($logMessage); $this->maniaControl->log($logMessage);
// Trigger own PlayerJoined callback // Trigger own PlayerJoined callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERJOINED, array(self::CB_PLAYERJOINED, $player)); $this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERJOINED, array(self::CB_PLAYERJOINED, $player));
} }
@ -155,20 +157,21 @@ class PlayerManager implements CallbackListener {
* @param array $callback * @param array $callback
*/ */
public function playerDisconnect(array $callback) { public function playerDisconnect(array $callback) {
$login = $callback[1][0]; $login = $callback[1][0];
$player = $this->removePlayer($login); $player = $this->removePlayer($login);
// Trigger own callback // Trigger own callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERDISCONNECTED, array(self::CB_PLAYERDISCONNECTED, $player)); $this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERDISCONNECTED, array(self::CB_PLAYERDISCONNECTED, $player));
if($player == null || $player->isFakePlayer()) { if ($player == null || $player->isFakePlayer()) {
return; return;
} }
$played = Formatter::formatTimeH(time() - $player->joinTime); $played = Formatter::formatTimeH(time() - $player->joinTime);
$this->maniaControl->log("Player left: " . $player->login . " / " . Formatter::stripCodes($player->nickname) . " Playtime: " . $played); $logMessage = "Player left: {$player->login} / {$player->nickname} Playtime: {$played}";
$this->maniaControl->log(Formatter::stripCodes($logMessage));
if($this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES)) {
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES)) {
$this->maniaControl->chat->sendChat('$<' . $player->nickname . '$> $s$0f0has left the game'); $this->maniaControl->chat->sendChat('$<' . $player->nickname . '$> $s$0f0has left the game');
} }
} }
@ -180,13 +183,12 @@ class PlayerManager implements CallbackListener {
*/ */
public function playerInfoChanged(array $callback) { public function playerInfoChanged(array $callback) {
$player = $this->getPlayer($callback[1][0]['Login']); $player = $this->getPlayer($callback[1][0]['Login']);
if($player == null) if ($player == null) return;
return;
$player->teamId = $callback[1][0]["TeamId"];
$player->teamId = $callback[1][0]["TeamId"];
$player->isSpectator = $callback[1][0]["SpectatorStatus"]; $player->isSpectator = $callback[1][0]["SpectatorStatus"];
$player->ladderRank = $callback[1][0]["LadderRanking"]; $player->ladderRank = $callback[1][0]["LadderRanking"];
// Trigger own callback // Trigger own callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERINFOCHANGED, array(self::CB_PLAYERINFOCHANGED)); $this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERINFOCHANGED, array(self::CB_PLAYERINFOCHANGED));
} }
@ -207,7 +209,7 @@ class PlayerManager implements CallbackListener {
* @return Player * @return Player
*/ */
public function getPlayer($login) { public function getPlayer($login) {
if(!isset($this->playerList[$login])) { if (!isset($this->playerList[$login])) {
return null; return null;
} }
return $this->playerList[$login]; return $this->playerList[$login];
@ -220,7 +222,7 @@ class PlayerManager implements CallbackListener {
* @return bool * @return bool
*/ */
private function addPlayer(Player $player) { private function addPlayer(Player $player) {
if(!$player) { if (!$player) {
return false; return false;
} }
$this->savePlayer($player); $this->savePlayer($player);
@ -232,16 +234,16 @@ class PlayerManager implements CallbackListener {
* Remove a Player from the PlayerList * Remove a Player from the PlayerList
* *
* @param string $login * @param string $login
* @param bool $savePlayedTime * @param bool $savePlayedTime
* @return Player $player * @return Player $player
*/ */
private function removePlayer($login, $savePlayedTime = true) { private function removePlayer($login, $savePlayedTime = true) {
if(!isset($this->playerList[$login])) { if (!isset($this->playerList[$login])) {
return null; return null;
} }
$player = $this->playerList[$login]; $player = $this->playerList[$login];
unset($this->playerList[$login]); unset($this->playerList[$login]);
if($savePlayedTime) { if ($savePlayedTime) {
$this->updatePlayedTime($player); $this->updatePlayedTime($player);
} }
return $player; return $player;
@ -251,74 +253,66 @@ class PlayerManager implements CallbackListener {
* Save player in database and fill up object properties * Save player in database and fill up object properties
* *
* @param Player $player * @param Player $player
* @param int $joinCount * @param int $joinCount
* @return bool * @return bool
*/ */
private function savePlayer(Player &$player, $joinCount = 1) { private function savePlayer(Player &$player) {
if(!$player) {
return false;
}
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
//TODO delete joincount from the following things
// Save player // Save player
$playerQuery = "INSERT INTO `" . self::TABLE_PLAYERS . "` ( $playerQuery = "INSERT INTO `" . self::TABLE_PLAYERS . "` (
`login`, `login`,
`nickname`, `nickname`,
`path`, `path`
`joinCount`
) VALUES ( ) VALUES (
?, ?, ?, ? ?, ?, ?
) ON DUPLICATE KEY UPDATE ) ON DUPLICATE KEY UPDATE
`index` = LAST_INSERT_ID(`index`), `index` = LAST_INSERT_ID(`index`),
`nickname` = VALUES(`nickname`), `nickname` = VALUES(`nickname`),
`path` = VALUES(`path`), `path` = VALUES(`path`);";
`joinCount` = `joinCount` + VALUES(`joinCount`);";
$playerStatement = $mysqli->prepare($playerQuery); $playerStatement = $mysqli->prepare($playerQuery);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
$playerStatement->bind_param('sssi', $player->login, $player->nickname, $player->path, $joinCount); $playerStatement->bind_param('sss', $player->login, $player->nickname, $player->path);
$playerStatement->execute(); $playerStatement->execute();
if($playerStatement->error) { if ($playerStatement->error) {
trigger_error($playerStatement->error); trigger_error($playerStatement->error);
$playerStatement->close(); $playerStatement->close();
return false; return false;
} }
$player->index = $playerStatement->insert_id; $player->index = $playerStatement->insert_id;
$playerStatement->close(); $playerStatement->close();
// Fill up properties // Get Player Auth Level from DB
$playerQuery = "SELECT `authLevel`, `joinCount`, `totalPlayed` FROM `" . self::TABLE_PLAYERS . "` $playerQuery = "SELECT `authLevel` FROM `" . self::TABLE_PLAYERS . "` WHERE `index` = ?;";
WHERE `index` = ?;";
$playerStatement = $mysqli->prepare($playerQuery); $playerStatement = $mysqli->prepare($playerQuery);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
$playerStatement->bind_param('i', $player->index); $playerStatement->bind_param('i', $player->index);
$playerStatement->execute(); $playerStatement->execute();
if($playerStatement->error) { if ($playerStatement->error) {
trigger_error($playerStatement->error); trigger_error($playerStatement->error);
$playerStatement->close(); $playerStatement->close();
return false; return false;
} }
$playerStatement->store_result(); $playerStatement->store_result();
$playerStatement->bind_result($player->authLevel, $player->joinCount, $player->totalPlayed); $playerStatement->bind_result($player->authLevel);
$playerStatement->fetch(); $playerStatement->fetch();
$playerStatement->free_result(); $playerStatement->free_result();
$playerStatement->close(); $playerStatement->close();
// Increment the Player Join Count
//Increment the Player Join Count $success = $this->maniaControl->statisticManager->incrementStat(self::STAT_JOIN_COUNT, $player,
$success = $this->maniaControl->statisticManager->incrementStat(self::STAT_JOIN_COUNT, $player, $this->maniaControl->server->getLogin()); $this->maniaControl->server->getLogin());
if(!$success) if (!$success) {
trigger_error("Error while setting the JoinCount"); trigger_error("Error while setting the JoinCount");
}
return true; return true;
} }
@ -329,11 +323,12 @@ class PlayerManager implements CallbackListener {
* @return bool * @return bool
*/ */
private function updatePlayedTime(Player $player) { private function updatePlayedTime(Player $player) {
if(!$player) { if (!$player) {
return false; return false;
} }
$playedTime = time() - $player->joinTime; $playedTime = time() - $player->joinTime;
return $this->maniaControl->statisticManager->insertStat(self::STAT_PLAYTIME, $player, $this->maniaControl->server->getLogin(), $playedTime); return $this->maniaControl->statisticManager->insertStat(self::STAT_PLAYTIME, $player, $this->maniaControl->server->getLogin(),
$playedTime);
} }
} }