mx karma finished ;)

This commit is contained in:
kremsy 2014-04-13 14:51:18 +02:00 committed by Steffen Schröder
parent 01b3d2346d
commit 644113966b

View File

@ -19,9 +19,9 @@ use ManiaControl\Settings\SettingManager;
/** /**
* ManiaControl Karma Plugin * ManiaControl Karma Plugin
* *
* @author kremsy and steeffeen * @author kremsy and steeffeen
* @copyright ManiaControl Copyright © 2014 ManiaControl Team * @copyright ManiaControl Copyright © 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/ */
class KarmaPlugin implements CallbackListener, TimerListener, Plugin { class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
/** /**
@ -43,6 +43,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
/** /**
* Constants MX Karma * Constants MX Karma
*/ */
const SETTING_WIDGET_DISPLAY_MX = 'Display MX-Karma in Widget';
const SETTING_MX_KARMA_ACTIVATED = 'Activate MX-Karma'; const SETTING_MX_KARMA_ACTIVATED = 'Activate MX-Karma';
const SETTING_MX_KARMA_IMPORTING = 'Import old MX-Karmas'; const SETTING_MX_KARMA_IMPORTING = 'Import old MX-Karmas';
const MX_IMPORT_TABLE = 'mc_karma_mximport'; const MX_IMPORT_TABLE = 'mc_karma_mximport';
@ -50,6 +51,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
const MX_KARMA_STARTSESSION = 'startSession'; const MX_KARMA_STARTSESSION = 'startSession';
const MX_KARMA_ACTIVATESESSION = 'activateSession'; const MX_KARMA_ACTIVATESESSION = 'activateSession';
const MX_KARMA_SAVEVOTES = 'saveVotes'; const MX_KARMA_SAVEVOTES = 'saveVotes';
const MX_KARMA_GETMAPRATING = 'getMapRating';
/** /**
* Private properties * Private properties
@ -73,6 +75,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
public static function prepare(ManiaControl $maniaControl) { public static function prepare(ManiaControl $maniaControl) {
$maniaControl->settingManager->initSetting(get_class(), self::SETTING_MX_KARMA_ACTIVATED, true); $maniaControl->settingManager->initSetting(get_class(), self::SETTING_MX_KARMA_ACTIVATED, true);
$maniaControl->settingManager->initSetting(get_class(), self::SETTING_MX_KARMA_IMPORTING, true); $maniaControl->settingManager->initSetting(get_class(), self::SETTING_MX_KARMA_IMPORTING, true);
$maniaControl->settingManager->initSetting(get_class(), self::SETTING_WIDGET_DISPLAY_MX, true);
$servers = $maniaControl->server->getAllServers(); $servers = $maniaControl->server->getAllServers();
foreach($servers as $server) { foreach($servers as $server) {
$maniaControl->settingManager->initSetting(get_class(), '$l[http://karma.mania-exchange.com/auth/getapikey?server=' . $server->login . ']MX Karma Code for ' . $server->login . '$l', ''); $maniaControl->settingManager->initSetting(get_class(), '$l[http://karma.mania-exchange.com/auth/getapikey?server=' . $server->login . ']MX Karma Code for ' . $server->login . '$l', '');
@ -113,6 +116,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->updateManialink = true; $this->updateManialink = true;
//Open MX-Karma Session
$this->mxKarmaOpenSession(); $this->mxKarmaOpenSession();
$this->mxKarma['startTime'] = time(); $this->mxKarma['startTime'] = time();
return true; return true;
@ -174,6 +178,8 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
return; return;
} }
$displayMxKarma = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_DISPLAY_MX);
// Get players // Get players
$players = $this->updateManialink; $players = $this->updateManialink;
if ($players === true) { if ($players === true) {
@ -182,9 +188,17 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->updateManialink = false; $this->updateManialink = false;
// Get map karma // Get map karma
$map = $this->maniaControl->mapManager->getCurrentMap(); $map = $this->maniaControl->mapManager->getCurrentMap();
$karma = $this->getMapKarma($map);
$votes = $this->getMapVotes($map); //Display the mx Karma if the setting is choosen and the MX session is available
if ($displayMxKarma && isset($this->mxKarma['session']) && isset($this->mxKarma['voteCount'])) {
$karma = $this->mxKarma['modeVoteAverage'] / 100;
$voteCount = $this->mxKarma['modeVoteCount'];
} else {
$karma = $this->getMapKarma($map);
$votes = $this->getMapVotes($map);
$voteCount = $votes['count'];
}
// Build karma manialink // Build karma manialink
$this->buildManialink(); $this->buildManialink();
@ -192,12 +206,12 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
// Update karma gauge & label // Update karma gauge & label
$karmaGauge = $this->manialink->karmaGauge; $karmaGauge = $this->manialink->karmaGauge;
$karmaLabel = $this->manialink->karmaLabel; $karmaLabel = $this->manialink->karmaLabel;
if (is_numeric($karma)) { if (is_numeric($karma) && $voteCount > 0) {
$karma = floatval($karma); $karma = floatval($karma);
$karmaGauge->setRatio($karma + 0.15 - $karma * 0.15); $karmaGauge->setRatio($karma + 0.15 - $karma * 0.15);
$karmaColor = ColorUtil::floatToStatusColor($karma); $karmaColor = ColorUtil::floatToStatusColor($karma);
$karmaGauge->setColor($karmaColor . '7'); $karmaGauge->setColor($karmaColor . '7');
$karmaLabel->setText(' ' . round($karma * 100.) . '% (' . $votes['count'] . ')'); $karmaLabel->setText(' ' . round($karma * 100.) . '% (' . $voteCount . ')');
} else { } else {
$karma = 0.; $karma = 0.;
$karmaGauge->setRatio(0.); $karmaGauge->setRatio(0.);
@ -239,6 +253,9 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
unset($this->mxKarma['votes']); unset($this->mxKarma['votes']);
$this->mxKarma['startTime'] = time(); $this->mxKarma['startTime'] = time();
$this->updateManialink = true; $this->updateManialink = true;
//Get Karma votes at begin of map
$this->getMxKarmaVotes();
} }
/** /**
@ -251,6 +268,9 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
return; return;
} }
$this->queryManialinkUpdateFor($player); $this->queryManialinkUpdateFor($player);
//Get Mx Karma Vote for Player
$this->getMxKarmaVotes($player);
} }
/** /**
@ -313,6 +333,26 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
// Save vote // Save vote
$map = $this->maniaControl->mapManager->getCurrentMap(); $map = $this->maniaControl->mapManager->getCurrentMap();
//Update vote in MX karma array
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_MX_KARMA_ACTIVATED) && isset($this->mxKarma["session"])) {
if (!isset($this->mxKarma["votes"][$player->login])) {
$sum = $this->mxKarma["voteCount"] * $this->mxKarma["voteAverage"] + $vote * 100;
$this->mxKarma["voteCount"]++;
$modeSum = $this->mxKarma["modeVoteCount"] * $this->mxKarma["modeVoteAverage"] + $vote * 100;
$this->mxKarma["modeVoteCount"]++;
} else {
$oldVote = $this->mxKarma["votes"][$player->login];
$sum = $this->mxKarma["voteCount"] * $this->mxKarma["voteAverage"] - $oldVote + $vote * 100;
$modeSum = $this->mxKarma["modeVoteCount"] * $this->mxKarma["modeVoteAverage"] - $oldVote + $vote * 100;
}
$this->mxKarma["voteAverage"] = $sum / $this->mxKarma["voteCount"];
$this->mxKarma["modeVoteAverage"] = $modeSum / $this->mxKarma["modeVoteCount"];
$this->mxKarma["votes"][$player->login] = $vote * 100;
}
$voted = $this->getPlayerVote($player, $map); $voted = $this->getPlayerVote($player, $map);
if (!$voted) { if (!$voted) {
$this->maniaControl->statisticManager->incrementStat(self::STAT_PLAYER_MAPVOTES, $player, $this->maniaControl->server->index); $this->maniaControl->statisticManager->incrementStat(self::STAT_PLAYER_MAPVOTES, $player, $this->maniaControl->server->index);
@ -404,11 +444,6 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
return false; return false;
} }
//Update vote in MX karma array
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_MX_KARMA_ACTIVATED)) {
$this->mxKarma["votes"][$player->login] = $vote * 100;
}
return $result; return $result;
} }
@ -642,6 +677,9 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
if ($data->success && $data->data->activated) { if ($data->success && $data->data->activated) {
$this->maniaControl->log("Successfully authenticated on Mania-Exchange Karma"); $this->maniaControl->log("Successfully authenticated on Mania-Exchange Karma");
$this->mxKarma['connectionInProgress'] = false; $this->mxKarma['connectionInProgress'] = false;
//Fetch the Mx Karma Votes
$this->getMxKarmaVotes();
} else { } else {
$this->maniaControl->log("Error while authenticating on Mania-Exchange Karma " . $data->data->message); $this->maniaControl->log("Error while authenticating on Mania-Exchange Karma " . $data->data->message);
//TODO remove temp trigger //TODO remove temp trigger
@ -657,6 +695,81 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}, "application/json", 1000); }, "application/json", 1000);
} }
/**
* Fetch the mxKarmaVotes for the current map
*/
public function getMxKarmaVotes(Player $player = null) {
if (!$this->maniaControl->settingManager->getSetting($this, self::SETTING_MX_KARMA_ACTIVATED)) {
return;
}
if (!isset($this->mxKarma['session'])) {
if (!isset($this->mxKarma['connectionInProgress']) || !$this->mxKarma['connectionInProgress']) {
$this->mxKarmaOpenSession();
}
return;
}
$map = $this->maniaControl->mapManager->getCurrentMap();
$properties = array();
$gameMode = $this->maniaControl->server->getGameMode(true);
if ($gameMode == 'Script') {
$scriptName = $this->maniaControl->client->getScriptName();
$properties['gamemode'] = $scriptName["CurrentValue"];
} else {
$properties['gamemode'] = $gameMode;
}
$properties['titleid'] = $this->maniaControl->server->titleId;
$properties['mapuid'] = $map->uid;
if (!$player) {
$properties['getvotesonly'] = false;
$properties['playerlogins'] = array();
foreach($this->maniaControl->playerManager->getPlayers() as $plyr) {
/** @var Player $player */
$properties['playerlogins'][] = $plyr->login;
}
} else {
$properties['getvotesonly'] = true;
$properties['playerlogins'] = array($player->login);
}
$content = json_encode($properties);
$this->maniaControl->fileReader->postData(self::MX_KARMA_URL . self::MX_KARMA_GETMAPRATING . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey), function ($data, $error) use (&$player) {
if (!$error) {
$data = json_decode($data);
if ($data->success) {
//Fetch averages if its for the whole server
if (!$player) {
$this->mxKarma["voteCount"] = $data->data->votecount;
$this->mxKarma["voteAverage"] = $data->data->voteaverage;
$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->log("MX-Karma Votes successfully fetched");
} else {
$this->maniaControl->log("Error while fetching votes: " . $data->data->message);
//TODO remove temp trigger
$this->maniaControl->errorHandler->triggerDebugNotice("Error while fetching votes: " . $data->data->message . " " . self::MX_KARMA_URL . self::MX_KARMA_SAVEVOTES . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey));
}
} else {
$this->maniaControl->log($error);
}
}, $content, false, 'application/json');
}
/** /**
* Import old Karma votes to Mania-Exchange Karma * Import old Karma votes to Mania-Exchange Karma
* *
@ -671,8 +784,10 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
return; return;
} }
if (!isset($this->mxKarma['session']) && !(isset($this->mxKarma['connectionInProgress']) && $this->mxKarma['connectionInProgress'])) { if (!isset($this->mxKarma['session'])) {
$this->mxKarmaOpenSession(); if (!isset($this->mxKarma['connectionInProgress']) || !$this->mxKarma['connectionInProgress']) {
$this->mxKarmaOpenSession();
}
return; return;
} }
@ -722,8 +837,10 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
return; return;
} }
if (!isset($this->mxKarma['session']) && !(isset($this->mxKarma['connectionInProgress']) && $this->mxKarma['connectionInProgress'])) { if (!isset($this->mxKarma['session'])) {
$this->mxKarmaOpenSession(); if (!isset($this->mxKarma['connectionInProgress']) || !$this->mxKarma['connectionInProgress']) {
$this->mxKarmaOpenSession();
}
return; return;
} }
@ -742,8 +859,10 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
* @param bool $import * @param bool $import
*/ */
private function postKarmaVotes(Map $map, array $votes, $import = false) { private function postKarmaVotes(Map $map, array $votes, $import = false) {
if (!isset($this->mxKarma['session']) && !(isset($this->mxKarma['connectionInProgress']) && $this->mxKarma['connectionInProgress'])) { if (!isset($this->mxKarma['session'])) {
$this->mxKarmaOpenSession(); if (!isset($this->mxKarma['connectionInProgress']) || !$this->mxKarma['connectionInProgress']) {
$this->mxKarmaOpenSession();
}
return; return;
} }
@ -783,8 +902,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} else { } else {
$this->maniaControl->log("Error while updating votes: " . $data->data->message); $this->maniaControl->log("Error while updating votes: " . $data->data->message);
//TODO remove temp trigger //TODO remove temp trigger
$this->maniaControl->errorHandler->triggerDebugNotice("Error while updating votes: " . $data->data->message . " " . $this->maniaControl->errorHandler->triggerDebugNotice("Error while updating votes: " . $data->data->message . " " . self::MX_KARMA_URL . self::MX_KARMA_SAVEVOTES . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey));
self::MX_KARMA_URL . self::MX_KARMA_SAVEVOTES . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey));
} }
} else { } else {
$this->maniaControl->log($error); $this->maniaControl->log($error);