dedimania coding improvements

This commit is contained in:
Steffen Schröder 2014-05-24 20:19:41 +02:00
parent e8c649664a
commit ddc3f98715
4 changed files with 32 additions and 46 deletions

View File

@ -105,7 +105,6 @@ class DedimaniaData {
* @param DedimaniaPlayer $player
*/
public function addPlayer(DedimaniaPlayer $player) {
/** @var DedimaniaPlayer $player */
$this->players[$player->login] = $player;
}

View File

@ -3,7 +3,7 @@
namespace MCTeam\Dedimania;
/**
* ManiaControl Dedimania-Plugin Player DataStructure
* ManiaControl Dedimania Plugin Player Data Structure
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
@ -21,28 +21,16 @@ class DedimaniaPlayer {
/**
* Construct a new Dedimania Player Model
*
* @param mixed $player
*/
public function __construct($player) {
if (!$player) {
return;
}
$this->login = $player['Login'];
$this->maxRank = $player['MaxRank'];
if (isset($player['Banned'])) {
$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;
}
}

View File

@ -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) {
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);
}

View File

@ -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 <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team