mx karma beginn

This commit is contained in:
kremsy 2014-02-23 23:27:30 +01:00 committed by Steffen Schröder
parent 5382cca615
commit 663c5633f5
3 changed files with 187 additions and 47 deletions

View File

@ -85,6 +85,10 @@ class RankingManager implements CallbackListener {
* @param $data * @param $data
*/ */
private function updateRankings($data) { private function updateRankings($data) {
if (!is_string($data)) {
return;
}
$scores = explode(';', $data); $scores = explode(';', $data);
foreach($scores as $player) { foreach($scores as $player) {
if (strpos($player, ':') !== false) { if (strpos($player, ':') !== false) {

View File

@ -10,9 +10,9 @@ use ManiaControl\Callbacks\TimerListener;
use ManiaControl\ColorUtil; use ManiaControl\ColorUtil;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\Maps\Map; use ManiaControl\Maps\Map;
use ManiaControl\Maps\MapManager;
use ManiaControl\Players\Player; use ManiaControl\Players\Player;
use ManiaControl\Plugins\Plugin; use ManiaControl\Plugins\Plugin;
use ManiaControl\Maps\MapManager;
/** /**
* ManiaControl Karma Plugin * ManiaControl Karma Plugin
@ -36,17 +36,28 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
const STAT_PLAYER_MAPVOTES = 'Voted Maps'; const STAT_PLAYER_MAPVOTES = 'Voted Maps';
/**
* Constants MX Karma
*/
const SETTING_MX_KARMA_AKTIVATED = 'Aktivate MX-Karma';
const MX_KARMA_SETTING_CODE = 'MX Karma Code for ';
const MX_KARMA_URL = 'http://karma.mania-exchange.com/api2/';
const MX_KARMA_STARTSESSION = 'startSession';
const MX_KARMA_ACTIVATESESSION = 'activateSession';
const MX_KARMA_SAVEVOTES = 'saveVotes';
/** /**
* Private properties * Private properties
*/ */
/** /**
*
* @var maniaControl $maniaControl * @var maniaControl $maniaControl
*/ */
private $maniaControl = null; private $maniaControl = null;
private $updateManialink = false; private $updateManialink = false;
private $manialink = null; private $manialink = null;
private $mxKarma = array();
/** /**
* Prepares the Plugin * Prepares the Plugin
* *
@ -54,11 +65,14 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
* @return mixed * @return mixed
*/ */
public static function prepare(ManiaControl $maniaControl) { public static function prepare(ManiaControl $maniaControl) {
//do nothing $maniaControl->settingManager->initSetting(get_class(), self::SETTING_MX_KARMA_AKTIVATED, true);
$servers = $maniaControl->server->getAllServers();
foreach($servers as $server) {
$maniaControl->settingManager->initSetting(get_class(), self::MX_KARMA_SETTING_CODE . $server->login, '');
}
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::load() * @see \ManiaControl\Plugins\Plugin::load()
*/ */
public function load(ManiaControl $maniaControl) { public function load(ManiaControl $maniaControl) {
@ -78,6 +92,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
// Register for callbacks // Register for callbacks
$this->maniaControl->timerManager->registerTimerListening($this, 'handle1Second', 1000); $this->maniaControl->timerManager->registerTimerListening($this, 'handle1Second', 1000);
$this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_BEGINMAP, $this, 'handleBeginMap'); $this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_BEGINMAP, $this, 'handleBeginMap');
$this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_ENDMAP, $this, 'saveMxKarmaVotes');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCONNECT, $this, 'handlePlayerConnect'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCONNECT, $this, 'handlePlayerConnect');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handlePlayerChat'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handlePlayerChat');
@ -88,11 +103,12 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->maniaControl->statisticManager->simpleStatsList->registerStat(self::STAT_PLAYER_MAPVOTES, 100, "VM"); $this->maniaControl->statisticManager->simpleStatsList->registerStat(self::STAT_PLAYER_MAPVOTES, 100, "VM");
$this->updateManialink = true; $this->updateManialink = true;
$this->mxKarmaOpenSession();
return true; return true;
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::unload() * @see \ManiaControl\Plugins\Plugin::unload()
*/ */
public function unload() { public function unload() {
@ -105,7 +121,6 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getId() * @see \ManiaControl\Plugins\Plugin::getId()
*/ */
public static function getId() { public static function getId() {
@ -113,7 +128,6 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getName() * @see \ManiaControl\Plugins\Plugin::getName()
*/ */
public static function getName() { public static function getName() {
@ -121,7 +135,6 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getVersion() * @see \ManiaControl\Plugins\Plugin::getVersion()
*/ */
public static function getVersion() { public static function getVersion() {
@ -129,7 +142,6 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getAuthor() * @see \ManiaControl\Plugins\Plugin::getAuthor()
*/ */
public static function getAuthor() { public static function getAuthor() {
@ -137,7 +149,6 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getDescription() * @see \ManiaControl\Plugins\Plugin::getDescription()
*/ */
public static function getDescription() { public static function getDescription() {
@ -150,13 +161,13 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
* @param $time * @param $time
*/ */
public function handle1Second($time) { public function handle1Second($time) {
if(!$this->updateManialink) { if (!$this->updateManialink) {
return; return;
} }
// Get players // Get players
$players = $this->updateManialink; $players = $this->updateManialink;
if($players === true) { if ($players === true) {
$players = $this->maniaControl->playerManager->getPlayers(); $players = $this->maniaControl->playerManager->getPlayers();
} }
$this->updateManialink = false; $this->updateManialink = false;
@ -172,7 +183,7 @@ 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)) {
$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);
@ -188,7 +199,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
// Loop players // Loop players
foreach($players as $login => $player) { foreach($players as $login => $player) {
// Get player vote // Get player vote
$vote = $this->getPlayerVote($player, $map); $vote = $this->getPlayerVote($player, $map); //TODO what is this for, vote nowhere used?
// Adjust manialink for player's vote // Adjust manialink for player's vote
$votesFrame = $this->manialink->votesFrame; $votesFrame = $this->manialink->votesFrame;
@ -217,7 +228,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
public function handlePlayerConnect(array $callback) { public function handlePlayerConnect(array $callback) {
$login = $callback[1][0]; $login = $callback[1][0];
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
if(!$player) { if (!$player) {
return; return;
} }
$this->queryManialinkUpdateFor($player); $this->queryManialinkUpdateFor($player);
@ -231,24 +242,24 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
public function handlePlayerChat(array $chatCallback) { public function handlePlayerChat(array $chatCallback) {
$login = $chatCallback[1][1]; $login = $chatCallback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
if(!$player) { if (!$player) {
return; return;
} }
$message = $chatCallback[1][2]; $message = $chatCallback[1][2];
if($chatCallback[1][3]) { if ($chatCallback[1][3]) {
$message = substr($message, 1); $message = substr($message, 1);
} }
if(preg_match('/[^+-]/', $message)) { if (preg_match('/[^+-]/', $message)) {
return; return;
} }
$countPositive = substr_count($message, '+'); $countPositive = substr_count($message, '+');
$countNegative = substr_count($message, '-'); $countNegative = substr_count($message, '-');
if($countPositive <= 0 && $countNegative <= 0) { if ($countPositive <= 0 && $countNegative <= 0) {
return; return;
} }
$vote = $countPositive - $countNegative; $vote = $countPositive - $countNegative;
$success = $this->handleVote($player, $vote); $success = $this->handleVote($player, $vote);
if(!$success) { if (!$success) {
$this->maniaControl->chat->sendError('Error occurred.', $player->login); $this->maniaControl->chat->sendError('Error occurred.', $player->login);
return; return;
} }
@ -268,10 +279,10 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$votes = explode(',', $votesSetting); $votes = explode(',', $votesSetting);
$voteLow = intval($votes[0]); $voteLow = intval($votes[0]);
$voteHigh = $voteLow + 2; $voteHigh = $voteLow + 2;
if(isset($votes[1])) { if (isset($votes[1])) {
$voteHigh = intval($votes[1]); $voteHigh = intval($votes[1]);
} }
if($vote < $voteLow || $vote > $voteHigh) { if ($vote < $voteLow || $vote > $voteHigh) {
return false; return false;
} }
@ -284,12 +295,12 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$map = $this->maniaControl->mapManager->getCurrentMap(); $map = $this->maniaControl->mapManager->getCurrentMap();
$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);
} }
$success = $this->savePlayerVote($player, $map, $vote); $success = $this->savePlayerVote($player, $map, $vote);
if(!$success) { if (!$success) {
return false; return false;
} }
$this->updateManialink = true; $this->updateManialink = true;
@ -302,10 +313,10 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
* @param Player $player * @param Player $player
*/ */
private function queryManialinkUpdateFor(Player $player) { private function queryManialinkUpdateFor(Player $player) {
if($this->updateManialink === true) { if ($this->updateManialink === true) {
return; return;
} }
if(!is_array($this->updateManialink)) { if (!is_array($this->updateManialink)) {
$this->updateManialink = array(); $this->updateManialink = array();
} }
$this->updateManialink[$player->login] = $player; $this->updateManialink[$player->login] = $player;
@ -326,7 +337,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
UNIQUE KEY `player_map_vote` (`mapIndex`, `playerIndex`) UNIQUE KEY `player_map_vote` (`mapIndex`, `playerIndex`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Save players map votes' AUTO_INCREMENT=1;"; ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Save players map votes' AUTO_INCREMENT=1;";
$mysqli->query($query); $mysqli->query($query);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR); trigger_error($mysqli->error, E_USER_ERROR);
} }
} }
@ -352,10 +363,16 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
) ON DUPLICATE KEY UPDATE ) ON DUPLICATE KEY UPDATE
`vote` = VALUES(`vote`);"; `vote` = VALUES(`vote`);";
$result = $mysqli->query($query); $result = $mysqli->query($query);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
//Update vote in MX karma array
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_MX_KARMA_AKTIVATED)) {
$this->mxKarma["votes"][$player->login] = $vote * 100;
}
return $result; return $result;
} }
@ -373,11 +390,11 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
AND `mapIndex` = {$map->index} AND `mapIndex` = {$map->index}
AND `vote` >= 0;"; AND `vote` >= 0;";
$result = $mysqli->query($query); $result = $mysqli->query($query);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
if($result->num_rows <= 0) { if ($result->num_rows <= 0) {
$result->free(); $result->free();
return false; return false;
} }
@ -399,18 +416,18 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
WHERE `mapIndex` = {$map->index} WHERE `mapIndex` = {$map->index}
AND `vote` >= 0;"; AND `vote` >= 0;";
$result = $mysqli->query($query); $result = $mysqli->query($query);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
if($result->num_rows <= 0) { if ($result->num_rows <= 0) {
$result->free(); $result->free();
return false; return false;
} }
$item = $result->fetch_object(); $item = $result->fetch_object();
$result->free(); $result->free();
$karma = $item->karma; $karma = $item->karma;
if($karma === null) { if ($karma === null) {
return false; return false;
} }
return floatval($karma); return floatval($karma);
@ -429,7 +446,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
AND `vote` >= 0 AND `vote` >= 0
GROUP BY `vote`;"; GROUP BY `vote`;";
$result = $mysqli->query($query); $result = $mysqli->query($query);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
@ -450,7 +467,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
* @param bool $forceBuild * @param bool $forceBuild
*/ */
private function buildManialink($forceBuild = false) { private function buildManialink($forceBuild = false) {
if(is_object($this->manialink) && !$forceBuild) { if (is_object($this->manialink) && !$forceBuild) {
return; return;
} }
@ -505,4 +522,129 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->manialink = $manialink; $this->manialink = $manialink;
} }
/**
* Open a Mx Karma Session
*/
private function mxKarmaOpenSession() {
if (!$this->maniaControl->settingManager->getSetting($this, self::SETTING_MX_KARMA_AKTIVATED)) {
return;
}
$serverLogin = $this->maniaControl->server->login;
$mxKarmaCode = $this->maniaControl->settingManager->getSetting($this, self::MX_KARMA_SETTING_CODE . $serverLogin);
if ($mxKarmaCode == '') {
return;
}
$applicationIdentifier = 'ManiaControl v' . ManiaControl::VERSION;
$testMode = 'true';
$query = self::MX_KARMA_URL . self::MX_KARMA_STARTSESSION;
$query .= '?serverLogin=' . $serverLogin;
$query .= '&applicationIdentifier=' . urlencode($applicationIdentifier);
$query .= '&game=sm';
$query .= '&testMode=' . $testMode;
$this->maniaControl->fileReader->loadFile($query, function ($data, $error) use($mxKarmaCode){
if (!$error) {
$data = json_decode($data);
if ($data->success) {
$this->mxKarma['session'] = $data->data;
$this->activateSession($mxKarmaCode);
} else {
$this->maniaControl->log("Error while authenticating on Mania-Exchange Karma");
}
} else {
$this->maniaControl->log($error);
}
}, "application/json", 1000);
}
/**
* Activates the MX-Karma Session
* @param $mxKarmaCode
*/
private function activateSession($mxKarmaCode) {
$hash = $this->buildActivationHash($this->mxKarma['session']->sessionSeed, $mxKarmaCode);
$query = self::MX_KARMA_URL . self::MX_KARMA_ACTIVATESESSION;
$query .= '?sessionKey=' . urlencode($this->mxKarma['session']->sessionKey);
$query .= '&activationHash=' . urlencode($hash);
$this->maniaControl->fileReader->loadFile($query, function ($data, $error){
if (!$error) {
$data = json_decode($data);
if ($data->success && $data->data->activated) {
$this->maniaControl->log("Successfully authenticated on Mania-Exchange Karma");
} else {
$this->maniaControl->log("Error while authenticating on Mania-Exchange Karma");
}
} else {
$this->maniaControl->log($error);
}
}, "application/json", 1000);
}
/**
* Save Mx Karma Votes at Mapend
*/
public function saveMxKarmaVotes(Map $map){
if (!$this->maniaControl->settingManager->getSetting($this, self::SETTING_MX_KARMA_AKTIVATED)) {
return;
}
if(!isset($this->mxKarma['session'])){
$this->mxKarmaOpenSession();
return;
}
$gameMode = $this->maniaControl->server->getGameMode(true);
$properties = array();
if($gameMode == 'Script'){
$scriptName = $this->maniaControl->client->getScriptName();
$properties['gamemode'] = $scriptName["CurrentValue"];
}else{
$properties['gamemode'] = $gameMode;
}
$properties['titleid'] = $this->maniaControl->server->titleId;
$properties['mapname'] = $map->name;
$properties['mapuid'] = $map->uid;
$properties['mapauthor'] = $map->authorLogin;
$properties['votes'] = array();
foreach($this->mxKarma['votes'] as $login => $value){
$player = $this->maniaControl->playerManager->getPlayer($login);
array_push($properties['votes'], array("login" => $login, "nickname" => $player->nickname, "vote" => $value));
}
$content = json_encode($properties);
$this->maniaControl->fileReader->postData(self::MX_KARMA_URL.self::MX_KARMA_SAVEVOTES . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey) , function ($data, $error){
if (!$error) {
$data = json_decode($data);
var_dump($data);
if ($data->success) {
$this->maniaControl->log("Votes successfully permitted");
} else {
$this->maniaControl->log("Error while updating votes");
}
} else {
$this->maniaControl->log($error);
}
}, $content, false, 'application/json');
}
/**
* Builds a sha512 activation Hash for the MX-Karma
* @param $sessionSeed
* @param $mxKey
* @return string
*/
private function buildActivationHash($sessionSeed, $mxKey) {
return hash('sha512', $mxKey . $sessionSeed);
}
} }

View File

@ -111,28 +111,22 @@ class KarmaTest implements Plugin {
$properties['mapname'] = $map->name; $properties['mapname'] = $map->name;
$properties['mapuid'] = $map->uid; $properties['mapuid'] = $map->uid;
$properties['mapauthor'] = $map->authorLogin; $properties['mapauthor'] = $map->authorLogin;
$properties['votes'] = array(); $properties['votes'] = array(array("login" => "kremsy", "nickname" => "test123", "vote" => 52));
$content = json_encode($properties); $content = json_encode($properties);
var_dump($content);
$this->maniaControl->fileReader->postData(self::MX_KARMA_URL.self::MX_KARMA_SAVEVOTES . "?sessionKey=" . $sessionKey , function ($data, $error) use ($sessionKey) { $this->maniaControl->fileReader->postData(self::MX_KARMA_URL.self::MX_KARMA_SAVEVOTES . "?sessionKey=" . urlencode($sessionKey) , function ($data, $error) use ($sessionKey) {
if (!$error) { if (!$error) {
$data = json_decode($data); $data = json_decode($data);
var_dump($data); var_dump($data);
if ($data->success && $data->data->activated) { if ($data->success) {
$this->maniaControl->log("Successfully authenticated on Mania-Exchange Karma"); $this->maniaControl->log("Votes successfully permitted");
$this->saveVotes($sessionKey);
} else { } else {
$this->maniaControl->log("Error while authenticating on Mania-Exchange Karma"); $this->maniaControl->log("Error while updating votes");
} }
} else { } else {
$this->maniaControl->log($error); $this->maniaControl->log($error);
} }
}, $content, false, 'application/json'); }, $content, false, 'application/json');
} }
private function buildActivationHash($sessionSeed, $mxKey) { private function buildActivationHash($sessionSeed, $mxKey) {