code refactoring

- improved comparing & checking
- improved string composition
This commit is contained in:
Steffen Schröder
2014-06-14 14:32:29 +02:00
parent bd6e0b5151
commit 9985b814d2
41 changed files with 337 additions and 318 deletions

View File

@ -153,7 +153,7 @@ class ChatMessagePlugin implements CommandListener, Plugin {
}
}
if ($player && $login == 'lj') {
if ($player && strtolower($login) === 'lj') {
return $player->nickname;
}
@ -417,7 +417,7 @@ class ChatMessagePlugin implements CommandListener, Plugin {
try {
$this->maniaControl->client->spectatorReleasePlayerSlot($player->login);
} catch (Exception $e) {
if ($e->getMessage() != 'The player is not a spectator') {
if ($e->getMessage() !== 'The player is not a spectator') {
$this->maniaControl->errorHandler->triggerDebugNotice("ChatMessagePlugin Debug Line " . $e->getLine() . ": " . $e->getMessage());
// TODO: only possible valid exception should be "wrong login" - throw others (like connection error)
//to nothing

View File

@ -195,7 +195,7 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
* @param float $neededRatio
*/
public function defineVote($voteIndex, $voteName, $idBased = false, $startText = '', $neededRatio = -1) {
if ($neededRatio == -1) {
if ($neededRatio < 0) {
$neededRatio = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_DEFAULT_RATIO);
}
$voteCommand = new VoteCommand($voteIndex, $voteName, $idBased, $neededRatio);
@ -221,7 +221,7 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
$pauseExists = false;
foreach ($scriptInfos->commandDescs as $param) {
if ($param->name == "Command_ForceWarmUp") {
if ($param->name === "Command_ForceWarmUp") {
$pauseExists = true;
break;
}
@ -286,7 +286,7 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
$itemMarginFactorY = 1.2;
//If game is shootmania lower the icons position by 20
if ($this->maniaControl->mapManager->getCurrentMap()->getGame() == 'sm') {
if ($this->maniaControl->mapManager->getCurrentMap()->getGame() === 'sm') {
$posY -= $shootManiaOffset;
}
@ -463,7 +463,7 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
$this->currentVote->neededPlayerRatio = floatval($this->maniaControl->settingManager->getSettingValue($this, self::SETTING_DEFAULT_PLAYER_RATIO));
$this->currentVote->function = $function;
if ($this->currentVote->voteCommand->startText != '') {
if ($this->currentVote->voteCommand->startText) {
$message = $this->currentVote->voteCommand->startText;
} else {
$message = '$fff$<' . $player->nickname . '$>$s$f8f started a $fff$<' . $this->currentVote->voteCommand->name . '$>$f8f!';

View File

@ -61,7 +61,7 @@ class DedimaniaData {
public function toArray() {
$array = array();
foreach (get_object_vars($this) as $key => $value) {
if ($key == 'records' || $key == 'sessionId' || $key == 'directoryAccessChecked' || $key == 'serverMaxRank' || $key == 'players') {
if ($key === 'records' || $key === 'sessionId' || $key === 'directoryAccessChecked' || $key === 'serverMaxRank' || $key === 'players') {
continue;
}
$array[ucfirst($key)] = $value;

View File

@ -153,7 +153,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$serverInfo = $this->maniaControl->server->getInfo();
$serverVersion = $this->maniaControl->client->getVersion();
$packMask = $this->maniaControl->server->titleId;
if ($packMask != 'Trackmania_2@nadeolabs') {
if ($packMask !== 'Trackmania_2@nadeolabs') {
$packMask = substr($this->maniaControl->server->titleId, 2);
}
@ -240,7 +240,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
* @return bool
*/
private function fetchDedimaniaRecords($reset = true) {
if (!$this->dedimaniaData || $this->dedimaniaData->sessionId == '') {
if (!$this->dedimaniaData || !$this->dedimaniaData->sessionId) {
return false;
}
@ -263,7 +263,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$self = $this;
$this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) use (&$self) {
if ($error != '') {
if ($error) {
$self->maniaControl->log("Dedimania Error: " . $error);
}
@ -321,7 +321,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
private function getPlayerList() {
$players = $this->maniaControl->playerManager->getPlayers();
if (count($players) == 0) {
if (empty($players)) {
return null;
}
$playerInfo = array();
@ -352,25 +352,31 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
}
/**
* Get Dedimania string representation of the current game mode
* Get Dedimania String Representation of the current Game Mode
*
* @return String
*/
private function getGameModeString() {
$gameMode = $this->maniaControl->server->getGameMode();
$scriptNameResponse = $this->maniaControl->client->getScriptName();
$scriptName = str_replace('.Script.txt', '', $scriptNameResponse["CurrentValue"]);
$gameMode = $this->maniaControl->server->getGameMode();
if ($gameMode === null) {
trigger_error("Couldn't retrieve game mode. ");
trigger_error("Couldn't retrieve game mode.");
return null;
}
switch ($gameMode) {
case 0:
{
if ($scriptName == 'Rounds' || $scriptName == 'Cup' || $scriptName == 'Team') {
return 'Rounds';
} else if ($scriptName == 'TimeAttack' || $scriptName == 'Laps' || $scriptName == 'TeamAttack' || $scriptName == 'TimeAttackPlus') {
return 'TA';
$scriptNameResponse = $this->maniaControl->client->getScriptName();
$scriptName = str_replace('.Script.txt', '', $scriptNameResponse['CurrentValue']);
switch ($scriptName) {
case 'Rounds':
case 'Cup':
case 'Team':
return 'Rounds';
case 'TimeAttack':
case 'Laps':
case 'TeamAttack':
case 'TimeAttackPlus':
return 'TA';
}
break;
}
@ -390,16 +396,12 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
}
/**
* Handle 1Second callback
* Handle 1 Second Callback
*/
public function updateEverySecond($time) {
if (!$this->updateManialink) {
public function updateEverySecond() {
if (!$this->updateManialink || !$this->dedimaniaData->records) {
return;
}
if (!$this->dedimaniaData->records) {
return;
}
$this->updateManialink = false;
if ($this->maniaControl->settingManager->getSettingValue($this, self::SETTING_WIDGET_ENABLE)) {
@ -526,7 +528,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$self = $this;
$this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) use (&$self) {
if ($error != '') {
if ($error) {
$self->maniaControl->log("Dedimania Error: " . $error);
}
@ -609,7 +611,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$self = $this;
$this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) use (&$self) {
if ($error != '') {
if ($error) {
$self->maniaControl->log("Dedimania Error: " . $error);
}
@ -656,7 +658,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
break;
}
if ($record->newRecord == false) {
if (!$record->newRecord) {
continue;
}
array_push($times, array('Login' => $record->login, 'Best' => $record->best, 'Checks' => $record->checkpoints));
@ -683,7 +685,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$self = $this;
$maniaControl = $this->maniaControl;
$this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) use (&$self, &$maniaControl) {
if ($error != '') {
if ($error) {
$maniaControl->log("Dedimania Error: " . $error);
}
@ -714,7 +716,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$serverInfo = $this->getServerInfo();
$playerList = $this->getPlayerList();
$votesInfo = $this->getVotesInfo();
if (!$serverInfo || !$votesInfo || !$playerList || !isset($this->dedimaniaData) || $this->dedimaniaData->sessionId == '') {
if (!$serverInfo || !$votesInfo || !$playerList || !isset($this->dedimaniaData) || !$this->dedimaniaData->sessionId) {
return;
}
@ -724,7 +726,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$self = $this;
$this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) use (&$self) {
if ($error != '') {
if ($error) {
$self->maniaControl->log("Dedimania Error: " . $error);
}
@ -971,7 +973,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
* Update the sorting and the ranks of all dedimania records
*/
private function updateDedimaniaRecordRanks() {
if ($this->dedimaniaData->getRecordCount() == 0) {
if ($this->dedimaniaData->getRecordCount() === 0) {
$this->maniaControl->callbackManager->triggerCallback(self::CB_DEDIMANIA_UPDATED, $this->dedimaniaData->records);
return;
}
@ -1031,7 +1033,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$login = $callback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login);
if ($actionId == self::ACTION_SHOW_DEDIRECORDSLIST) {
if ($actionId === self::ACTION_SHOW_DEDIRECORDSLIST) {
$this->showDediRecordsList(array(), $player);
}
}
@ -1093,7 +1095,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$recordFrame = new Frame();
$pageFrame->add($recordFrame);
if ($i % 2 != 0) {
if ($i % 2 !== 0) {
$lineQuad = new Quad_BgsPlayerCard();
$recordFrame->add($lineQuad);
$lineQuad->setSize($width, 4);

View File

@ -160,7 +160,7 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
$itemMarginFactorY = 1.2;
//If game is shootmania lower the icons position by 20
if ($this->maniaControl->mapManager->getCurrentMap()->getGame() == 'sm') {
if ($this->maniaControl->mapManager->getCurrentMap()->getGame() === 'sm') {
$posY -= $shootManiaOffset;
}
@ -500,7 +500,7 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
$pageFrame->add($playerFrame);
$playerFrame->setY($y);
if ($i % 2 != 0) {
if ($i % 2 !== 0) {
$lineQuad = new Quad_BgsPlayerCard();
$playerFrame->add($lineQuad);
$lineQuad->setSize($width, 4);

View File

@ -99,7 +99,7 @@ class DynamicPointLimitPlugin implements CallbackListener, CommandListener, Plug
$this->maniaControl = $maniaControl;
$allowOthers = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_ACCEPT_OTHER_MODES);
if (!$allowOthers && $this->maniaControl->server->titleId != 'SMStormRoyal@nadeolabs') {
if (!$allowOthers && $this->maniaControl->server->titleId !== 'SMStormRoyal@nadeolabs') {
$error = 'This plugin only supports Royal (check Settings)!';
throw new \Exception($error);
}

View File

@ -238,7 +238,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$karmaSettingName = self::buildKarmaSettingName($serverLogin);
$mxKarmaCode = $this->maniaControl->settingManager->getSettingValue($this, $karmaSettingName);
if ($mxKarmaCode == '') {
if (!$mxKarmaCode) {
return;
}
@ -297,7 +297,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
// Fetch the Mx Karma Votes
$self->getMxKarmaVotes();
} else {
if ($data->data->message == "invalid hash") {
if ($data->data->message === "invalid hash") {
$permission = $self->maniaControl->settingManager->getSettingValue($this->maniaControl->authenticationManager, PluginMenu::SETTING_PERMISSION_CHANGE_PLUGIN_SETTINGS);
$self->maniaControl->chat->sendErrorToAdmins("Invalid Mania-Exchange Karma code in Karma Plugin specified!", $permission);
} else {
@ -348,7 +348,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$properties = array();
$gameMode = $this->maniaControl->server->getGameMode(true);
if ($gameMode == 'Script') {
if ($gameMode === 'Script') {
$scriptName = $this->maniaControl->client->getScriptName();
$properties['gamemode'] = $scriptName["CurrentValue"];
} else {
@ -393,7 +393,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$self->maniaControl->log("MX-Karma Votes successfully fetched");
} else {
$self->maniaControl->log("Error while fetching votes: " . $data->data->message);
if ($data->data->message == "invalid session") {
if ($data->data->message === 'invalid session') {
unset($this->mxKarma['session']);
return;
}
@ -455,12 +455,12 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$gameMode = $this->maniaControl->server->getGameMode(true);
if (count($votes) == 0) {
if (empty($votes)) {
return;
}
$properties = array();
if ($gameMode == 'Script') {
if ($gameMode === 'Script') {
$scriptName = $this->maniaControl->client->getScriptName();
$properties['gamemode'] = $scriptName["CurrentValue"];
} else {
@ -490,7 +490,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$self->maniaControl->log("Votes successfully submitted");
} else {
$self->maniaControl->log("Error while updating votes: " . $data->data->message);
if ($data->data->message == "invalid session") {
if ($data->data->message === "invalid session") {
unset($this->mxKarma['session']);
return;
}
@ -967,7 +967,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}
$vote = $result->fetch_object();
if ($result->field_count == 0 || !$vote) {
if (!$result->field_count || !$vote) {
$query = "SELECT vote, login, nickname FROM `" . self::TABLE_KARMA . "` k LEFT JOIN `" . PlayerManager::TABLE_PLAYERS . "` p ON (k.playerIndex=p.index) WHERE mapIndex = {$map->index}";
$result2 = $mysqli->query($query);
if ($mysqli->error) {
@ -1011,7 +1011,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
return;
}
if (!isset($this->mxKarma['votes']) || count($this->mxKarma['votes']) == 0) {
if (!isset($this->mxKarma['votes']) || empty($this->mxKarma['votes'])) {
return;
}

View File

@ -308,7 +308,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
*/
public function getLocalRecords(Map $map, $limit = -1) {
$mysqli = $this->maniaControl->database->mysqli;
$limit = ($limit > 0 ? "LIMIT " . $limit : "");
$limit = ($limit > 0 ? 'LIMIT ' . $limit : '');
$query = "SELECT * FROM (
SELECT recs.*, @rank := @rank + 1 as `rank` FROM `" . self::TABLE_RECORDS . "` recs, (SELECT @rank := 0) ra
WHERE recs.`mapIndex` = {$map->index}
@ -435,30 +435,34 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$this->updateManialink = true;
// Announce record
$newRecord = $this->getLocalRecord($map, $callback->player);
$newRecord = $this->getLocalRecord($map, $callback->player);
$improvedRank = (!$oldRecord || $newRecord->rank < $oldRecord->rank);
$notifyOnlyDriver = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_NOTIFY_ONLY_DRIVER);
$notifyOnlyBestRecords = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_NOTIFY_BEST_RECORDS);
if ($notifyOnlyDriver || $notifyOnlyBestRecords > 0 && $newRecord->rank > $notifyOnlyBestRecords) {
$improvement = ((!$oldRecord || $newRecord->rank < $oldRecord->rank) ? 'gained the' : 'improved your');
$message = 'You ' . $improvement . ' $<$ff0' . $newRecord->rank . '.$> Local Record: $<$fff' . Formatter::formatTime($newRecord->time) . '$>';
if ($oldRecord) {
$oldRank = ($improvement == 'improved your') ? '' : $oldRecord->rank . '. ';
}
if ($oldRecord) {
$message .= ' ($<$ff0' . $oldRank . '$>$<$fff-' . Formatter::formatTime(($oldRecord->time - $newRecord->time)) . '$>!';
}
$this->maniaControl->chat->sendInformation('$3c0' . $message . '!', $callback->player->login);
$message = '$3c0';
if ($notifyOnlyDriver) {
$message .= 'You';
} else {
$improvement = ((!$oldRecord || $newRecord->rank < $oldRecord->rank) ? 'gained the' : 'improved the');
$message = '$<$fff' . $callback->player->nickname . '$> ' . $improvement . ' $<$ff0' . $newRecord->rank . '.$> Local Record: $<$fff' . Formatter::formatTime($newRecord->time) . '$>';
if ($oldRecord) {
$oldRank = ($improvement == 'improved the') ? '' : $oldRecord->rank . '. ';
$message .= '$<$fff' . $callback->player->nickname . '$>';
}
$message .= ' ' . ($improvedRank ? 'gained' : 'improved') . ' the';
$message .= ' $<$ff0' . $newRecord->rank . '.$> Local Record:';
$message .= ' $<$fff' . Formatter::formatTime($newRecord->time) . '$>!';
if ($oldRecord) {
$message .= ' (';
if ($improvedRank) {
$message .= '$<$ff0' . $oldRecord->rank . '.$> ';
}
if ($oldRecord) {
$message .= ' ($<$ff0' . $oldRank . '$>$<$fff-' . Formatter::formatTime(($oldRecord->time - $newRecord->time)) . '$>)';
}
$this->maniaControl->chat->sendInformation('$3c0' . $message . '!');
$timeDiff = $oldRecord->time - $newRecord->time;
$message .= '$<$fff-' . Formatter::formatTime($timeDiff) . '$>)';
}
if ($notifyOnlyDriver) {
$this->maniaControl->chat->sendInformation($message, $callback->player);
} else if (!$notifyOnlyBestRecords || $newRecord->rank <= $notifyOnlyBestRecords) {
$this->maniaControl->chat->sendInformation($message);
}
$this->maniaControl->callbackManager->triggerCallback(self::CB_LOCALRECORDS_CHANGED, $newRecord);
@ -596,7 +600,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$recordFrame = new Frame();
$pageFrame->add($recordFrame);
if ($i % 2 != 0) {
if ($i % 2 !== 0) {
$lineQuad = new Quad_BgsPlayerCard();
$recordFrame->add($lineQuad);
$lineQuad->setSize($width, 4);
@ -647,7 +651,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
}
$mysqli = $this->maniaControl->database->mysqli;
$query = "DELETE FROM `" . self::TABLE_RECORDS . "` WHERE `mapIndex` = " . $currentMap->index . " AND `playerIndex` = " . $player->index . "";
$query = "DELETE FROM `" . self::TABLE_RECORDS . "` WHERE `mapIndex` = {$currentMap->index} AND `playerIndex` = {$player->index};";
$mysqli->query($query);
if ($mysqli->error) {
trigger_error($mysqli->error);

View File

@ -27,7 +27,7 @@ use Maniaplanet\DedicatedServer\Structures\AbstractStructure;
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class ServerRankingPlugin implements Plugin, CallbackListener, CommandListener {
/**
/*
* Constants
*/
const PLUGIN_ID = 6;
@ -109,10 +109,10 @@ class ServerRankingPlugin implements Plugin, CallbackListener, CommandListener {
$script = $this->maniaControl->client->getScriptName();
if ($this->maniaControl->mapManager->getCurrentMap()->getGame() == 'tm') {
if ($this->maniaControl->mapManager->getCurrentMap()->getGame() === 'tm') {
//TODO also add obstacle here as default
$maniaControl->settingManager->initSetting($this, self::SETTING_MIN_RANKING_TYPE, self::RANKING_TYPE_RECORDS);
} else if ($script["CurrentValue"] == "InstaDM.Script.txt") {
} else if ($script["CurrentValue"] === 'InstaDM.Script.txt') {
$maniaControl->settingManager->initSetting($this, self::SETTING_MIN_RANKING_TYPE, self::RANKING_TYPE_RATIOS);
} else {
$maniaControl->settingManager->initSetting($this, self::SETTING_MIN_RANKING_TYPE, self::RANKING_TYPE_POINTS);
@ -120,7 +120,7 @@ class ServerRankingPlugin implements Plugin, CallbackListener, CommandListener {
//Check if the type is Correct
$type = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_MIN_RANKING_TYPE);
if ($type != self::RANKING_TYPE_RECORDS && $type != self::RANKING_TYPE_POINTS && $type != self::RANKING_TYPE_RATIOS) {
if (!$this->isValidRankingType($type)) {
$error = 'Ranking Type is not correct, possible values(' . self::RANKING_TYPE_RATIOS . ', ' . self::RANKING_TYPE_POINTS . ', ' . self::RANKING_TYPE_POINTS . ')';
throw new \Exception($error);
}
@ -134,7 +134,8 @@ class ServerRankingPlugin implements Plugin, CallbackListener, CommandListener {
$this->maniaControl->commandManager->registerCommandListener('nextrank', $this, 'command_nextRank', false, 'Shows the person in front of you in the ServerRanking.');
$this->maniaControl->commandManager->registerCommandListener(array('topranks', 'top100'), $this, 'command_topRanks', false, 'Shows an overview of the best-ranked 100 players.');
$this->resetRanks(); //TODO only update records count
// TODO: only update records count
$this->resetRanks();
}
/**
@ -502,7 +503,7 @@ class ServerRankingPlugin implements Plugin, CallbackListener, CommandListener {
$pageFrame->add($playerFrame);
$playerFrame->setY($y);
if ($i % 2 != 0) {
if ($i % 2 !== 0) {
$lineQuad = new Quad_BgsPlayerCard();
$playerFrame->add($lineQuad);
$lineQuad->setSize($width, 4);
@ -521,6 +522,25 @@ class ServerRankingPlugin implements Plugin, CallbackListener, CommandListener {
// Render and display xml
$this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, 'TopRanks');
}
/**
* Get the possible Ranking Types
*
* @return string[]
*/
private function getRankingTypes() {
return array(self::RANKING_TYPE_POINTS, self::RANKING_TYPE_RATIOS, self::RANKING_TYPE_RECORDS);
}
/**
* Check if the given Ranking Type is valid
*
* @param string $rankingType
* @return bool
*/
private function isValidRankingType($rankingType) {
return in_array($rankingType, $this->getRankingTypes());
}
}
/**