code refactoring

improved error handling + reporting
This commit is contained in:
Steffen Schröder 2014-07-28 14:39:12 +02:00
parent 494139bacb
commit 4f66c8e54f

View File

@ -209,10 +209,6 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
trigger_error($mysqli->error, E_USER_ERROR); trigger_error($mysqli->error, E_USER_ERROR);
} }
if (!$this->maniaControl->settingManager->getSettingValue($this, self::SETTING_MX_KARMA_ACTIVATED)) {
return;
}
// Create mx table // Create mx table
$query = "CREATE TABLE IF NOT EXISTS `" . self::MX_IMPORT_TABLE . "` ( $query = "CREATE TABLE IF NOT EXISTS `" . self::MX_IMPORT_TABLE . "` (
`index` int(11) NOT NULL AUTO_INCREMENT, `index` int(11) NOT NULL AUTO_INCREMENT,
@ -244,32 +240,34 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
return; return;
} }
$applicationIdentifier = 'ManiaControl v' . ManiaControl::VERSION; $appIdentifier = 'ManiaControl v' . ManiaControl::VERSION;
$testMode = 'true'; $testMode = 'true';
$query = self::MX_KARMA_URL . self::MX_KARMA_START_SESSION; $query = self::MX_KARMA_URL . self::MX_KARMA_START_SESSION;
$query .= '?serverLogin=' . $serverLogin; $query .= '?serverLogin=' . $serverLogin;
$query .= '&applicationIdentifier=' . urlencode($applicationIdentifier); $query .= '&applicationIdentifier=' . urlencode($appIdentifier);
$query .= '&testMode=' . $testMode; $query .= '&testMode=' . $testMode;
$this->mxKarma['connectionInProgress'] = true; $this->mxKarma['connectionInProgress'] = true;
$this->maniaControl->fileReader->loadFile($query, function ($data, $error) use ($mxKarmaCode) { $this->maniaControl->fileReader->loadFile($query, function ($json, $error) use ($mxKarmaCode) {
if (!$error) { $this->mxKarma['connectionInProgress'] = false;
$data = json_decode($data); if ($error) {
if ($data->success) { $this->maniaControl->errorHandler->triggerDebugNotice('mx karma error: ' . $error);
$this->mxKarma['session'] = $data->data; return;
$this->activateSession($mxKarmaCode); }
} else { $data = json_decode($json);
$this->maniaControl->log("Error while authenticating on Mania-Exchange Karma"); if (!$data) {
// TODO remove temp trigger $this->maniaControl->errorHandler->triggerDebugNotice('auth error', $json, $data);
$this->maniaControl->errorHandler->triggerDebugNotice("Error while authenticating on Mania-Exchange Karma " . $data->data->message); return;
$this->mxKarma['connectionInProgress'] = false; }
} if ($data->success) {
$this->mxKarma['session'] = $data->data;
$this->activateSession($mxKarmaCode);
} else { } else {
$this->maniaControl->log($error); $this->maniaControl->log("Error while authenticating on Mania-Exchange Karma");
// TODO remove temp trigger // TODO remove temp trigger
$this->maniaControl->errorHandler->triggerDebugNotice("Error while authenticating on Mania-Exchange Karma " . $error); $this->maniaControl->errorHandler->triggerDebugNotice('auth error', $data->data->message);
$this->mxKarma['connectionInProgress'] = false; $this->mxKarma['connectionInProgress'] = false;
} }
}, AsynchronousFileReader::CONTENT_TYPE_JSON, 1000); }, AsynchronousFileReader::CONTENT_TYPE_JSON, 1000);
@ -281,39 +279,37 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
* @param string $mxKarmaCode * @param string $mxKarmaCode
*/ */
private function activateSession($mxKarmaCode) { private function activateSession($mxKarmaCode) {
// TODO: unused private method! remove?
$hash = $this->buildActivationHash($this->mxKarma['session']->sessionSeed, $mxKarmaCode); $hash = $this->buildActivationHash($this->mxKarma['session']->sessionSeed, $mxKarmaCode);
$query = self::MX_KARMA_URL . self::MX_KARMA_ACTIVATE_SESSION; $query = self::MX_KARMA_URL . self::MX_KARMA_ACTIVATE_SESSION;
$query .= '?sessionKey=' . urlencode($this->mxKarma['session']->sessionKey); $query .= '?sessionKey=' . urlencode($this->mxKarma['session']->sessionKey);
$query .= '&activationHash=' . urlencode($hash); $query .= '&activationHash=' . urlencode($hash);
$this->maniaControl->fileReader->loadFile($query, function ($data, $error) use ($query) { $this->maniaControl->fileReader->loadFile($query, function ($json, $error) use ($query) {
if (!$error) { $this->mxKarma['connectionInProgress'] = false;
$data = json_decode($data); if ($error) {
if ($data->success && $data->data->activated) { $this->maniaControl->errorHandler->triggerDebugNotice('mx karma error', $error);
$this->maniaControl->log("Successfully authenticated on Mania-Exchange Karma"); return;
$this->mxKarma['connectionInProgress'] = false; }
$data = json_decode($json);
if (!$data) {
$this->maniaControl->errorHandler->triggerDebugNotice('parse error', $json, $data);
return;
}
if ($data->success && $data->data->activated) {
$this->maniaControl->log('Successfully authenticated on Mania-Exchange Karma');
// Fetch the Mx Karma Votes // Fetch the Mx Karma Votes
$this->getMxKarmaVotes(); $this->getMxKarmaVotes();
} else {
if ($data->data->message === "invalid hash") {
$permission = $this->maniaControl->settingManager->getSettingValue($this->maniaControl->authenticationManager, PluginMenu::SETTING_PERMISSION_CHANGE_PLUGIN_SETTINGS);
$this->maniaControl->chat->sendErrorToAdmins("Invalid Mania-Exchange Karma code in Karma Plugin specified!", $permission);
} else {
// TODO remove temp trigger
$this->maniaControl->errorHandler->triggerDebugNotice("Error while authenticating on Mania-Exchange Karma " . $data->data->message . " url Query " . $query);
}
$this->maniaControl->log("Error while activating Mania-Exchange Karma Session: " . $data->data->message);
$this->mxKarma['connectionInProgress'] = false;
unset($this->mxKarma['session']);
}
} else { } else {
// TODO remove temp trigger if ($data->data->message === 'invalid hash') {
$this->maniaControl->errorHandler->triggerDebugNotice("Error while activating Mania-Exchange Karma Session: " . $error); $permission = $this->maniaControl->settingManager->getSettingValue($this->maniaControl->authenticationManager, PluginMenu::SETTING_PERMISSION_CHANGE_PLUGIN_SETTINGS);
$this->maniaControl->log($error); $this->maniaControl->chat->sendErrorToAdmins("Invalid Mania-Exchange Karma code in Karma Plugin specified!", $permission);
$this->mxKarma['connectionInProgress'] = false; } else {
$this->maniaControl->errorHandler->triggerDebugNotice('auth error', $data->data->message, $query);
}
$this->maniaControl->log("Error while activating Mania-Exchange Karma Session: " . $data->data->message);
unset($this->mxKarma['session']);
} }
}, AsynchronousFileReader::CONTENT_TYPE_JSON, 1000); }, AsynchronousFileReader::CONTENT_TYPE_JSON, 1000);
} }
@ -351,7 +347,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$gameMode = $this->maniaControl->server->getGameMode(true); $gameMode = $this->maniaControl->server->getGameMode(true);
if ($gameMode === 'Script') { if ($gameMode === 'Script') {
$scriptName = $this->maniaControl->client->getScriptName(); $scriptName = $this->maniaControl->client->getScriptName();
$properties['gamemode'] = $scriptName["CurrentValue"]; $properties['gamemode'] = $scriptName['CurrentValue'];
} else { } else {
$properties['gamemode'] = $gameMode; $properties['gamemode'] = $gameMode;
} }
@ -371,41 +367,45 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
$content = json_encode($properties); $content = json_encode($properties);
$this->maniaControl->fileReader->postData(self::MX_KARMA_URL . self::MX_KARMA_GET_MAP_RATING . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey), function ($data, $this->maniaControl->fileReader->postData(self::MX_KARMA_URL . self::MX_KARMA_GET_MAP_RATING . '?sessionKey=' . urlencode($this->mxKarma['session']->sessionKey), function ($json,
$error) use $error) use
( (
&$player &$player
) { ) {
if (!$error) { if ($error) {
$data = json_decode($data); $this->maniaControl->errorHandler->triggerDebugNotice('mx karma error', $error);
if ($data->success) { return;
}
$data = json_decode($json);
if (!$data) {
$this->maniaControl->errorHandler->triggerDebugNotice('parse error', $json, $data);
return;
}
// Fetch averages if its for the whole server if ($data->success) {
if (!$player) { // Fetch averages if it's for the whole server
$this->mxKarma['voteCount'] = $data->data->votecount; if (!$player) {
$this->mxKarma['voteAverage'] = $data->data->voteaverage; $this->mxKarma['voteCount'] = $data->data->votecount;
$this->mxKarma['modeVoteCount'] = $data->data->modevotecount; $this->mxKarma['voteAverage'] = $data->data->voteaverage;
$this->mxKarma['modeVoteAverage'] = $data->data->modevoteaverage; $this->mxKarma['modeVoteCount'] = $data->data->modevotecount;
} $this->mxKarma['modeVoteAverage'] = $data->data->modevoteaverage;
foreach ($data->data->votes as $votes) {
$this->mxKarma["votes"][$votes->login] = $votes->vote;
}
$this->updateManialink = true;
$this->maniaControl->callbackManager->triggerCallback(self::CB_KARMA_MXUPDATED, $this->mxKarma);
$this->maniaControl->log("MX-Karma Votes successfully fetched");
} else {
$this->maniaControl->log("Error while fetching votes: " . $data->data->message);
if ($data->data->message === 'invalid session') {
unset($this->mxKarma['session']);
return;
}
// TODO remove temp trigger
$this->maniaControl->errorHandler->triggerDebugNotice("Error while fetching votes: '{$data->data->message}' " . KarmaPlugin::MX_KARMA_URL . KarmaPlugin::MX_KARMA_SAVE_VOTES . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey));
} }
foreach ($data->data->votes as $votes) {
$this->mxKarma['votes'][$votes->login] = $votes->vote;
}
$this->updateManialink = true;
$this->maniaControl->callbackManager->triggerCallback(self::CB_KARMA_MXUPDATED, $this->mxKarma);
$this->maniaControl->log('MX-Karma Votes successfully fetched!');
} else { } else {
$this->maniaControl->log($error); // Problem occurred
$this->maniaControl->log('Error while fetching votes: ' . $data->data->message);
if ($data->data->message === 'invalid session') {
unset($this->mxKarma['session']);
} else {
$this->maniaControl->errorHandler->triggerDebugNotice('fetch error', $data->data->message, self::MX_KARMA_GET_MAP_RATING, $this->mxKarma['session']);
}
} }
}, $content, false, AsynchronousFileReader::CONTENT_TYPE_JSON); }, $content, false, AsynchronousFileReader::CONTENT_TYPE_JSON);
} }
@ -426,7 +426,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$votes = array(); $votes = array();
foreach ($this->mxKarma['votes'] as $login => $value) { foreach ($this->mxKarma['votes'] as $login => $value) {
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
array_push($votes, array("login" => $login, "nickname" => $player->rawNickname, "vote" => $value)); array_push($votes, array('login' => $login, 'nickname' => $player->rawNickname, 'vote' => $value));
} }
$this->postKarmaVotes($this->mxKarma['map'], $votes); $this->postKarmaVotes($this->mxKarma['map'], $votes);
unset($this->mxKarma['map']); unset($this->mxKarma['map']);
@ -464,7 +464,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$properties = array(); $properties = array();
if ($gameMode === 'Script') { if ($gameMode === 'Script') {
$scriptName = $this->maniaControl->client->getScriptName(); $scriptName = $this->maniaControl->client->getScriptName();
$properties['gamemode'] = $scriptName["CurrentValue"]; $properties['gamemode'] = $scriptName['CurrentValue'];
} else { } else {
$properties['gamemode'] = $gameMode; $properties['gamemode'] = $gameMode;
} }
@ -484,23 +484,27 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$content = json_encode($properties); $content = json_encode($properties);
$this->maniaControl->fileReader->postData(self::MX_KARMA_URL . self::MX_KARMA_SAVE_VOTES . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey), function ($data, $this->maniaControl->fileReader->postData(self::MX_KARMA_URL . self::MX_KARMA_SAVE_VOTES . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey), function ($json,
$error) { $error) {
if (!$error) { if ($error) {
$data = json_decode($data); $this->maniaControl->errorHandler->triggerDebugNotice('mx karma error', $error);
if ($data->success) { return;
$this->maniaControl->log("Votes successfully submitted"); }
} else { $data = json_decode($json);
$this->maniaControl->log("Error while updating votes: " . $data->data->message); if (!$data) {
if ($data->data->message === "invalid session") { $this->maniaControl->errorHandler->triggerDebugNotice('parse error', $json, $data);
unset($this->mxKarma['session']); return;
return; }
} if ($data->success) {
// TODO remove temp trigger $this->maniaControl->log('Votes successfully submitted!');
$this->maniaControl->errorHandler->triggerDebugNotice("Error while updating votes: " . $data->data->message . " " . KarmaPlugin::MX_KARMA_URL . self::MX_KARMA_SAVE_VOTES . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey));
}
} else { } else {
$this->maniaControl->log($error); // Problem occurred
$this->maniaControl->log("Error while updating votes: '{$data->data->message}'");
if ($data->data->message === "invalid session") {
unset($this->mxKarma['session']);
} else {
$this->maniaControl->errorHandler->triggerDebugNotice('saving error', $data->data->message, self::MX_KARMA_SAVE_VOTES, $this->mxKarma['session']);
}
} }
}, $content, false, AsynchronousFileReader::CONTENT_TYPE_JSON); }, $content, false, AsynchronousFileReader::CONTENT_TYPE_JSON);
} }
@ -508,7 +512,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
/** /**
* Handle PlayerConnect callback * Handle PlayerConnect callback
* *
* @param \ManiaControl\Players\Player $player * @param Player $player
*/ */
public function handlePlayerConnect(Player $player) { public function handlePlayerConnect(Player $player) {
if (!$player) { if (!$player) {
@ -974,7 +978,8 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$query = "SELECT mapImported FROM `" . self::MX_IMPORT_TABLE . "` WHERE `mapIndex` = {$map->index};"; $query = "SELECT `mapImported` FROM `" . self::MX_IMPORT_TABLE . "`
WHERE `mapIndex` = {$map->index};";
$result = $mysqli->query($query); $result = $mysqli->query($query);
if ($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
@ -983,7 +988,11 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$vote = $result->fetch_object(); $vote = $result->fetch_object();
if (!$result->field_count || !$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}"; $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); $result2 = $mysqli->query($query);
if ($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
@ -992,13 +1001,20 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$votes = array(); $votes = array();
while ($row = $result2->fetch_object()) { while ($row = $result2->fetch_object()) {
array_push($votes, array("login" => $row->login, "nickname" => $row->nickname, "vote" => $row->vote * 100)); array_push($votes, array('login' => $row->login, 'nickname' => $row->nickname, 'vote' => $row->vote * 100));
} }
$this->postKarmaVotes($map, $votes, true); $this->postKarmaVotes($map, $votes, true);
// Flag Map as Imported in database if it is a import // Flag Map as Imported in database if it is a import
$query = "INSERT INTO `" . self::MX_IMPORT_TABLE . "` (`mapIndex`,`mapImported`) VALUES ({$map->index},true) ON DUPLICATE KEY UPDATE `mapImported` = true;"; $query = "INSERT INTO `" . self::MX_IMPORT_TABLE . "` (
`mapIndex`,
`mapImported`
) VALUES (
{$map->index},
1
) ON DUPLICATE KEY UPDATE
`mapImported` = 1;";
$mysqli->query($query); $mysqli->query($query);
if ($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
@ -1006,7 +1022,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$result2->free(); $result2->free();
} }
$result->free_result(); $result->free();
return; return;
} }