code improvements

This commit is contained in:
Steffen Schröder 2013-12-31 15:27:25 +01:00
parent 42384ebd75
commit 32624e0946

View File

@ -26,14 +26,14 @@ class PlayerManager implements CallbackListener {
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';
/** /**
* Private properties * Private properties
*/ */
private $maniaControl = null; private $maniaControl = null;
private $playerCommands = null; private $playerCommands = null;
private $playerList = array(); private $playerList = array();
/** /**
* Public properties * Public properties
*/ */
@ -42,7 +42,7 @@ class PlayerManager implements CallbackListener {
/** /**
* Construct player manager * Construct 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;
@ -50,16 +50,17 @@ class PlayerManager implements CallbackListener {
$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');
} }
/** /**
@ -98,7 +99,7 @@ class PlayerManager implements CallbackListener {
/** /**
* Handle OnInit callback * Handle OnInit callback
* *
* @param array $callback * @param array $callback
*/ */
public function onInit(array $callback) { public function onInit(array $callback) {
// Add all players // Add all players
@ -113,7 +114,7 @@ class PlayerManager implements CallbackListener {
$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));
} }
@ -121,53 +122,50 @@ class PlayerManager implements CallbackListener {
/** /**
* Handle playerConnect callback * Handle playerConnect callback
* *
* @param array $callback * @param array $callback
*/ */
public function playerConnect(array $callback) { public function playerConnect(array $callback) {
$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');
//$nickname = Formatter::stripCodes($player->nickname); // TODO: strip codes without colour codes like in serverviewer $chatMessage = '$s$0f0' . $string[$player->authLevel] . ' $fff' . $player->nickname . '$z$s$0f0 Nation:$fff ' .
$player->getCountry() . ' $z$s$0f0joined!';
//TODO standart notification colour from settings or something $this->maniaControl->chat->sendChat($chatMessage);
$this->maniaControl->chat->sendInformation('This server uses ManiaControl v' . ManiaControl::VERSION . '!', $player->login);
$this->maniaControl->chat->sendChat(
'$s$0f0' . $string[$player->authLevel] . ' $fff' . $player->nickname . '$z$s$0f0 Nation:$fff ' . $player->getCountry() . ' $z$s$0f0joined');
$this->maniaControl->chat->sendInformation('This server uses ManiaControl v' . ManiaControl::VERSION,$player->login);
} }
$this->maniaControl->log('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}";
// Trigger own callback $this->maniaControl->log($logMessage);
// 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));
} }
/** /**
* Handle playerDisconnect callback * Handle playerDisconnect callback
* *
* @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); $this->maniaControl->log(
"Player left: " . $player->login . " / " . Formatter::stripCodes($player->nickname) . " Playtime: " . $played);
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');
} }
@ -175,17 +173,17 @@ class PlayerManager implements CallbackListener {
/** /**
* Update PlayerInfo * Update PlayerInfo
*
* @param array $callback * @param array $callback
*/ */
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));
} }
@ -202,7 +200,7 @@ class PlayerManager implements CallbackListener {
/** /**
* Get a Player from the PlayerList * Get a Player from the PlayerList
* *
* @param string $login * @param string $login
* @return Player * @return Player
*/ */
public function getPlayer($login) { public function getPlayer($login) {
@ -215,7 +213,7 @@ class PlayerManager implements CallbackListener {
/** /**
* Add a player to the PlayerList * Add a player to the PlayerList
* *
* @param Player $player * @param Player $player
* @return bool * @return bool
*/ */
private function addPlayer(Player $player) { private function addPlayer(Player $player) {
@ -230,8 +228,8 @@ 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) {
@ -249,8 +247,8 @@ 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, $joinCount = 1) {
@ -314,7 +312,7 @@ class PlayerManager implements CallbackListener {
/** /**
* Update total played time of the player * Update total played time of the player
* *
* @param Player $player * @param Player $player
* @return bool * @return bool
*/ */
private function updatePlayedTime(Player $player) { private function updatePlayedTime(Player $player) {