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() {
@ -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;
@ -356,6 +367,12 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
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;
} }
@ -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) {