From ddc3f9871550a4bebccfa187d133918012261a60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Sat, 24 May 2014 20:19:41 +0200 Subject: [PATCH] dedimania coding improvements --- .../MCTeam/Dedimania/DedimaniaData.php | 3 +- .../MCTeam/Dedimania/DedimaniaPlayer.php | 28 ++++-------- .../MCTeam/Dedimania/DedimaniaPlugin.php | 45 +++++++++---------- .../plugins/MCTeam/Dedimania/RecordData.php | 2 +- 4 files changed, 32 insertions(+), 46 deletions(-) diff --git a/application/plugins/MCTeam/Dedimania/DedimaniaData.php b/application/plugins/MCTeam/Dedimania/DedimaniaData.php index 43c2bfdc..7ed293fa 100644 --- a/application/plugins/MCTeam/Dedimania/DedimaniaData.php +++ b/application/plugins/MCTeam/Dedimania/DedimaniaData.php @@ -7,7 +7,7 @@ use ManiaControl\Players\Player; use Maniaplanet\DedicatedServer\Structures\Version; /** - * ManiaControl Dedimania Plugin DataStructure + * ManiaControl Dedimania Plugin Data Structure * * @author ManiaControl Team * @copyright 2014 ManiaControl Team @@ -105,7 +105,6 @@ class DedimaniaData { * @param DedimaniaPlayer $player */ public function addPlayer(DedimaniaPlayer $player) { - /** @var DedimaniaPlayer $player */ $this->players[$player->login] = $player; } diff --git a/application/plugins/MCTeam/Dedimania/DedimaniaPlayer.php b/application/plugins/MCTeam/Dedimania/DedimaniaPlayer.php index 9046527d..584afaa4 100644 --- a/application/plugins/MCTeam/Dedimania/DedimaniaPlayer.php +++ b/application/plugins/MCTeam/Dedimania/DedimaniaPlayer.php @@ -3,7 +3,7 @@ namespace MCTeam\Dedimania; /** - * ManiaControl Dedimania-Plugin Player DataStructure + * ManiaControl Dedimania Plugin Player Data Structure * * @author ManiaControl Team * @copyright 2014 ManiaControl Team @@ -21,28 +21,16 @@ class DedimaniaPlayer { /** * Construct a new Dedimania Player Model - * @param mixed$player + * + * @param mixed $player */ public function __construct($player) { - if (!$player) { - return; - } - $this->login = $player['Login']; $this->maxRank = $player['MaxRank']; - $this->banned = $player['Banned']; - $this->optionsEnabled = $player['OptionsEnabled']; - $this->options = $player['Options']; - } - - /** - * Construct a new Player by its login and maxRank - * - * @param string $login - * @param int $maxRank - */ - public function constructNewPlayer($login, $maxRank) { - $this->login = $login; - $this->maxRank = $maxRank; + if (isset($player['Banned'])) { + $this->banned = $player['Banned']; + $this->optionsEnabled = $player['OptionsEnabled']; + $this->options = $player['Options']; + } } } \ No newline at end of file diff --git a/application/plugins/MCTeam/Dedimania/DedimaniaPlugin.php b/application/plugins/MCTeam/Dedimania/DedimaniaPlugin.php index b6140700..63484b7f 100644 --- a/application/plugins/MCTeam/Dedimania/DedimaniaPlugin.php +++ b/application/plugins/MCTeam/Dedimania/DedimaniaPlugin.php @@ -112,7 +112,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene * @see \ManiaControl\Plugins\Plugin::getDescription() */ public static function getDescription() { - return "Dedimania Plugin for Trackmania"; + return "Dedimania Plugin for TrackMania"; } /** @@ -143,6 +143,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene $this->maniaControl->timerManager->registerTimerListening($this, 'updateEverySecond', 1000); $this->maniaControl->timerManager->registerTimerListening($this, 'handleEveryMinute', 1000 * 60); $this->maniaControl->timerManager->registerTimerListening($this, 'updatePlayerList', 1000 * 60 * 3); + $this->maniaControl->commandManager->registerCommandListener(array('dedirecs', 'dedirecords'), $this, 'showDediRecordsList', false, 'Shows a list of Dedimania records of the current map.'); // Open session @@ -154,7 +155,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene } $dedimaniaCode = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_DEDIMANIA_CODE . $serverInfo->login . '$l'); - if ($dedimaniaCode == '') { + if (!$dedimaniaCode) { throw new \Exception("No Dedimania Code Specified, check the settings!"); } @@ -169,28 +170,27 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene private function openDedimaniaSession() { $content = $this->encode_request(self::DEDIMANIA_OPENSESSION, array($this->dedimaniaData->toArray())); - $self = $this; - $this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) use (&$self) { - $self->maniaControl->log("Try to connect on Dedimania"); + $this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) { + $this->maniaControl->log("Try to connect on Dedimania"); if (!$data || $error) { - $self->maniaControl->log("Dedimania Error: '{$error}'"); + $this->maniaControl->log("Dedimania Error: '{$error}'"); } - $data = $self->decode($data); + $data = $this->decode($data); if (is_array($data)) { foreach ($data as $index => $methodResponse) { if (xmlrpc_is_fault($methodResponse)) { - $self->handleXmlRpcFault($methodResponse, self::DEDIMANIA_OPENSESSION); + $this->handleXmlRpcFault($methodResponse, self::DEDIMANIA_OPENSESSION); } else if ($index <= 0) { $responseData = $methodResponse[0]; - $self->dedimaniaData->sessionId = $responseData['SessionId']; - if ($self->dedimaniaData->sessionId != '') { - $self->maniaControl->log("Dedimania connection successfully established."); - $self->fetchDedimaniaRecords(); - $self->init = true; + $this->dedimaniaData->sessionId = $responseData['SessionId']; + if ($this->dedimaniaData->sessionId) { + $this->maniaControl->log("Dedimania connection successfully established."); + $this->fetchDedimaniaRecords(); + $this->init = true; } else { - $self->maniaControl->log("Error while opening Dedimania Connection"); + $this->maniaControl->log("Error while opening Dedimania Connection"); } } } @@ -277,8 +277,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene $self->dedimaniaData->serverMaxRank = $responseData['ServerMaxRank']; foreach ($responseData['Players'] as $player) { - $dediPlayer = new DedimaniaPlayer(null); - $dediPlayer->constructNewPlayer($player['Login'], $player['MaxRank']); + $dediPlayer = new DedimaniaPlayer($player); $self->dedimaniaData->addPlayer($dediPlayer); } foreach ($responseData['Records'] as $key => $record) { @@ -517,7 +516,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene * Checks If a Dedimania Session exists, if not create a new oen */ private function checkDedimaniaSession() { - if ($this->dedimaniaData->sessionId == '') { + if (!$this->dedimaniaData->sessionId) { $this->openDedimaniaSession(); return; } @@ -552,7 +551,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene /** * Handle PlayerConnect callback * - * @param \ManiaControl\Players\Player $player + * @param Player $player */ public function handlePlayerConnect(Player $player) { // Send Dedimania request @@ -561,7 +560,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene $self = $this; $this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) use (&$self, &$player) { - if ($error != '') { + if ($error) { $self->maniaControl->log("Dedimania Error: " . $error); } @@ -572,10 +571,11 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene $self->handleXmlRpcFault($methodResponse, self::DEDIMANIA_PLAYERCONNECT); } else if ($index <= 0) { $responseData = $methodResponse[0]; - $self->dedimaniaData->addPlayer(new DedimaniaPlayer($responseData)); + $dediPlayer = new DedimaniaPlayer($responseData); + $self->dedimaniaData->addPlayer($dediPlayer); - //Fetch records if he is the first who joined the server - if (count($self->maniaControl->playerManager->getPlayers()) == 1) { + // Fetch records if he is the first who joined the server + if ($self->maniaControl->playerManager->getPlayerCount(false) === 1) { $self->fetchDedimaniaRecords(true); } } @@ -853,7 +853,6 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene return $record; } } - return new RecordData(null); } diff --git a/application/plugins/MCTeam/Dedimania/RecordData.php b/application/plugins/MCTeam/Dedimania/RecordData.php index 5ce69368..a585c1be 100644 --- a/application/plugins/MCTeam/Dedimania/RecordData.php +++ b/application/plugins/MCTeam/Dedimania/RecordData.php @@ -5,7 +5,7 @@ namespace MCTeam\Dedimania; use ManiaControl\Utils\Formatter; /** - * ManiaControl Dedimania-Plugin Record DataStructure + * ManiaControl Dedimania Plugin Record Data Structure * * @author ManiaControl Team * @copyright 2014 ManiaControl Team