karma plugin settings migration

This commit is contained in:
Steffen Schröder 2014-04-27 21:31:55 +02:00
parent e07ac29d13
commit 8bf32d7157
4 changed files with 317 additions and 203 deletions

View File

@ -1,8 +1,9 @@
<?php
namespace ManiaControl;
namespace ManiaControl\Database;
use ManiaControl\Callbacks\TimerListener;
use ManiaControl\ManiaControl;
/**
* Database Connection Class
@ -16,6 +17,7 @@ class Database implements TimerListener {
* Public Properties
*/
public $mysqli = null;
public $migrationHelper = null;
/*
* Private Properties
@ -59,6 +61,9 @@ class Database implements TimerListener {
// Register Method which checks the Database Connection every 5 seconds
$this->maniaControl->timerManager->registerTimerListening($this, 'checkConnection', 5000);
// Create migration helper
$this->migrationHelper = new MigrationHelper($maniaControl);
}
/**

View File

@ -0,0 +1,75 @@
<?php
namespace ManiaControl\Database;
use ManiaControl\Settings\SettingManager;
use ManiaControl\ManiaControl;
/**
* Database Migration Assistant
*
* @author steeffeen
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class MigrationHelper {
/*
* Private Properties
*/
private $maniaControl = null;
/**
* Construct Migration Helper
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
/**
* Transfer the Settings of the given Class to a new One
*
* @param mixed $sourceClass
* @param mixed $targetClass
* @return bool
*/
public function transferSettings($sourceClass, $targetClass) {
$sourceClass = $this->getClass($sourceClass);
$targetClass = $this->getClass($targetClass);
var_dump($sourceClass, $targetClass);
$mysqli = $this->maniaControl->database->mysqli;
$query = "INSERT INTO `" . SettingManager::TABLE_SETTINGS . "` (`class`, `setting`, `type`, `value`, `default`)
SELECT ?, `setting`, `type`, `value`, `default` FROM `" . SettingManager::TABLE_SETTINGS . "` WHERE `class` = ?;";
$statement = $mysqli->prepare($query);
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$statement->bind_param('ss', $targetClass, $sourceClass);
if ($statement->error) {
trigger_error($statement->error);
$statement->close();
return false;
}
$success = $statement->execute();
$statement->close();
return $success;
}
/**
* Get the Class of the given Object
*
* @param mixed $class
* @return string
*/
private function getClass($class) {
if (is_object($class)) {
return get_class($class);
}
return (string) $class;
}
}

View File

@ -26,6 +26,7 @@ use Maniaplanet\DedicatedServer\Connection;
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
use Maniaplanet\DedicatedServer\Xmlrpc\NotInScriptModeException;
use Maniaplanet\DedicatedServer\Xmlrpc\TransportException;
use ManiaControl\Database\Database;
require_once __DIR__ . '/Libs/Maniaplanet/DedicatedServer/Connection.php';
require_once __DIR__ . '/Libs/GbxDataFetcher/gbxdatafetcher.inc.php';

View File

@ -43,7 +43,6 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
const SETTING_WIDGET_POSY = 'Widget-Position: Y';
const SETTING_WIDGET_WIDTH = 'Widget-Size: Width';
const SETTING_WIDGET_HEIGHT = 'Widget-Size: Height';
const STAT_PLAYER_MAPVOTES = 'Voted Maps';
/**
@ -63,13 +62,15 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
* Private properties
*/
/**
*
* @var maniaControl $maniaControl
*/
private $maniaControl = null;
private $updateManialink = false;
/** @var ManiaLink $manialink */
/**
* @var ManiaLink $manialink
*/
private $manialink = null;
private $mxKarma = array();
/**
@ -83,12 +84,13 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$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();
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', '');
}
}
/**
*
* @see \ManiaControl\Plugins\Plugin::load()
*/
public function load(ManiaControl $maniaControl) {
@ -123,13 +125,14 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->updateManialink = true;
//Open MX-Karma Session
// Open MX-Karma Session
$this->mxKarmaOpenSession();
$this->mxKarma['startTime'] = time();
return true;
}
/**
*
* @see \ManiaControl\Plugins\Plugin::unload()
*/
public function unload() {
@ -141,6 +144,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}
/**
*
* @see \ManiaControl\Plugins\Plugin::getId()
*/
public static function getId() {
@ -148,6 +152,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}
/**
*
* @see \ManiaControl\Plugins\Plugin::getName()
*/
public static function getName() {
@ -155,6 +160,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}
/**
*
* @see \ManiaControl\Plugins\Plugin::getVersion()
*/
public static function getVersion() {
@ -162,6 +168,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}
/**
*
* @see \ManiaControl\Plugins\Plugin::getAuthor()
*/
public static function getAuthor() {
@ -169,6 +176,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}
/**
*
* @see \ManiaControl\Plugins\Plugin::getDescription()
*/
public static function getDescription() {
@ -197,24 +205,29 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
// Get map karma
$map = $this->maniaControl->mapManager->getCurrentMap();
//Display the mx Karma if the setting is choosen and the MX session is available
// 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 {
}
else {
$karma = $this->getMapKarma($map);
$votes = $this->getMapVotes($map);
$voteCount = $votes['count'];
}
if($this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_ENABLE)) {
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_ENABLE)) {
// Build karma manialink
$this->buildManialink();
// Update karma gauge & label
/** @var Gauge $karmaGauge */
/**
* @var Gauge $karmaGauge
*/
$karmaGauge = $this->manialink->karmaGauge;
/** @var Label $karmaLabel */
/**
* @var Label $karmaLabel
*/
$karmaLabel = $this->manialink->karmaLabel;
if (is_numeric($karma) && $voteCount > 0) {
$karma = floatval($karma);
@ -222,19 +235,20 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$karmaColor = ColorUtil::floatToStatusColor($karma);
$karmaGauge->setColor($karmaColor . '7');
$karmaLabel->setText(' ' . round($karma * 100.) . '% (' . $voteCount . ')');
} else {
}
else {
$karmaGauge->setRatio(0.);
$karmaGauge->setColor('00fb');
$karmaLabel->setText('-');
}
// Loop players
foreach($players as $login => $player) {
foreach ($players as $login => $player) {
// Get player vote
// TODO: show the player his own vote in some way
//$vote = $this->getPlayerVote($player, $map);
//$votesFrame = $this->manialink->votesFrame;
//$votesFrame->removeChildren();
// $vote = $this->getPlayerVote($player, $map);
// $votesFrame = $this->manialink->votesFrame;
// $votesFrame->removeChildren();
// Send manialink
$this->maniaControl->manialinkManager->sendManialink($this->manialink, $login);
@ -248,10 +262,10 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
* @param Map $map
*/
public function handleBeginMap(Map $map) {
//send Map Karma to MX from previous Map
// send Map Karma to MX from previous Map
if (isset($this->mxKarma['map'])) {
$votes = array();
foreach($this->mxKarma['votes'] as $login => $value) {
foreach ($this->mxKarma['votes'] as $login => $value) {
$player = $this->maniaControl->playerManager->getPlayer($login);
array_push($votes, array("login" => $login, "nickname" => $player->rawNickname, "vote" => $value));
}
@ -263,7 +277,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->mxKarma['startTime'] = time();
$this->updateManialink = true;
//Get Karma votes at begin of map
// Get Karma votes at begin of map
$this->getMxKarmaVotes();
}
@ -278,7 +292,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}
$this->queryManialinkUpdateFor($player);
//Get Mx Karma Vote for Player
// Get Mx Karma Vote for Player
$this->getMxKarmaVotes($player);
}
@ -342,7 +356,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
// Save vote
$map = $this->maniaControl->mapManager->getCurrentMap();
//Update vote in MX karma array
// 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;
@ -350,7 +364,8 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$modeSum = $this->mxKarma["modeVoteCount"] * $this->mxKarma["modeVoteAverage"] + $vote * 100;
$this->mxKarma["modeVoteCount"]++;
} else {
}
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;
@ -361,7 +376,6 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->mxKarma["votes"][$player->login] = $vote * 100;
}
$voted = $this->getPlayerVote($player, $map);
if (!$voted) {
$this->maniaControl->statisticManager->incrementStat(self::STAT_PLAYER_MAPVOTES, $player, $this->maniaControl->server->index);
@ -396,6 +410,8 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
*/
private function initTables() {
$mysqli = $this->maniaControl->database->mysqli;
// Create local table
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_KARMA . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
`mapIndex` int(11) NOT NULL,
@ -410,10 +426,14 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
trigger_error($mysqli->error, E_USER_ERROR);
}
// Migrate settings
$this->maniaControl->database->migrationHelper->transferSettings('ManiaControl\Chat', $this);
if (!$this->maniaControl->settingManager->getSetting($this, self::SETTING_MX_KARMA_ACTIVATED)) {
return;
}
// Create mx table
$query = "CREATE TABLE IF NOT EXISTS `" . self::MX_IMPORT_TABLE . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
`mapIndex` int(11) NOT NULL,
@ -533,7 +553,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}
$votes = array();
$count = 0;
while($vote = $result->fetch_object()) {
while ($vote = $result->fetch_object()) {
$votes[$vote->vote] = $vote;
$count += $vote->count;
}
@ -604,7 +624,6 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->manialink = $manialink;
}
/**
* Update Settings
*
@ -625,7 +644,8 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
if ($settingName == 'Enable Karma Widget' && $value == true) {
$this->updateManialink = true;
$this->handle1Second(time());
} elseif ($settingName == 'Enable Karma Widget' && $value == false) {
}
elseif ($settingName == 'Enable Karma Widget' && $value == false) {
$this->updateManialink = false;
$ml = new ManiaLink(self::MLID_KARMA);
$mltext = $ml->render()->saveXML();
@ -659,21 +679,23 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->mxKarma['connectionInProgress'] = true;
$self = $this;
$this->maniaControl->fileReader->loadFile($query, function ($data, $error) use (&$self, $mxKarmaCode) {
$this->maniaControl->fileReader->loadFile($query, function ($data, $error) use(&$self, $mxKarmaCode) {
if (!$error) {
$data = json_decode($data);
if ($data->success) {
$self->mxKarma['session'] = $data->data;
$self->activateSession($mxKarmaCode);
} else {
}
else {
$self->maniaControl->log("Error while authenticating on Mania-Exchange Karma");
//TODO remove temp trigger
// TODO remove temp trigger
$self->maniaControl->errorHandler->triggerDebugNotice("Error while authenticating on Mania-Exchange Karma " . $data->data->message);
$self->mxKarma['connectionInProgress'] = false;
}
} else {
}
else {
$self->maniaControl->log($error);
//TODO remove temp trigger
// TODO remove temp trigger
$self->maniaControl->errorHandler->triggerDebugNotice("Error while authenticating on Mania-Exchange Karma " . $error);
$self->mxKarma['connectionInProgress'] = false;
}
@ -693,23 +715,25 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$query .= '&activationHash=' . urlencode($hash);
$self = $this;
$this->maniaControl->fileReader->loadFile($query, function ($data, $error) use (&$self, $query) {
$this->maniaControl->fileReader->loadFile($query, function ($data, $error) use(&$self, $query) {
if (!$error) {
$data = json_decode($data);
if ($data->success && $data->data->activated) {
$self->maniaControl->log("Successfully authenticated on Mania-Exchange Karma");
$self->mxKarma['connectionInProgress'] = false;
//Fetch the Mx Karma Votes
// Fetch the Mx Karma Votes
$self->getMxKarmaVotes();
} else {
}
else {
$self->maniaControl->log("Error while authenticating on Mania-Exchange Karma " . $data->data->message);
//TODO remove temp trigger
// TODO remove temp trigger
$self->maniaControl->errorHandler->triggerDebugNotice("Error while authenticating on Mania-Exchange Karma " . $data->data->message . " url Query " . $query);
$self->mxKarma['connectionInProgress'] = false;
}
} else {
//TODO remove temp trigger
}
else {
// TODO remove temp trigger
$self->maniaControl->errorHandler->triggerDebugNotice("Error while authenticating on Mania-Exchange Karma " . $error);
$self->maniaControl->log($error);
$self->mxKarma['connectionInProgress'] = false;
@ -717,7 +741,6 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}, "application/json", 1000);
}
/**
* Fetch the mxKarmaVotes for the current map
*/
@ -741,7 +764,8 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
if ($gameMode == 'Script') {
$scriptName = $this->maniaControl->client->getScriptName();
$properties['gamemode'] = $scriptName["CurrentValue"];
} else {
}
else {
$properties['gamemode'] = $gameMode;
}
@ -751,23 +775,26 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
if (!$player) {
$properties['getvotesonly'] = false;
$properties['playerlogins'] = array();
foreach($this->maniaControl->playerManager->getPlayers() as $plyr) {
/** @var Player $player */
foreach ($this->maniaControl->playerManager->getPlayers() as $plyr) {
/**
* @var Player $player
*/
$properties['playerlogins'][] = $plyr->login;
}
} else {
}
else {
$properties['getvotesonly'] = true;
$properties['playerlogins'] = array($player->login);
}
$content = json_encode($properties);
$self = $this;
$this->maniaControl->fileReader->postData(self::MX_KARMA_URL . self::MX_KARMA_GETMAPRATING . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey), function ($data, $error) use (&$self, &$player) {
$this->maniaControl->fileReader->postData(self::MX_KARMA_URL . self::MX_KARMA_GETMAPRATING . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey), function ($data, $error) use(&$self, &$player) {
if (!$error) {
$data = json_decode($data);
if ($data->success) {
//Fetch averages if its for the whole server
// Fetch averages if its for the whole server
if (!$player) {
$self->mxKarma["voteCount"] = $data->data->votecount;
$self->mxKarma["voteAverage"] = $data->data->voteaverage;
@ -775,19 +802,21 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$self->mxKarma["modeVoteAverage"] = $data->data->modevoteaverage;
}
foreach($data->data->votes as $votes) {
foreach ($data->data->votes as $votes) {
$self->mxKarma["votes"][$votes->login] = $votes->vote;
}
$self->updateManialink = true;
$self->maniaControl->callbackManager->triggerCallback($self::CB_KARMA_MXUPDATED, $self->mxKarma);
$self->maniaControl->log("MX-Karma Votes successfully fetched");
} else {
}
else {
$self->maniaControl->log("Error while fetching votes: " . $data->data->message);
//TODO remove temp trigger
// TODO remove temp trigger
$self->maniaControl->errorHandler->triggerDebugNotice("Error while fetching votes: " . $data->data->message . " " . KarmaPlugin::MX_KARMA_URL . KarmaPlugin::MX_KARMA_SAVEVOTES . "?sessionKey=" . urlencode($self->mxKarma['session']->sessionKey));
}
} else {
}
else {
$self->maniaControl->log($error);
}
}, $content, false, 'application/json');
@ -832,13 +861,13 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}
$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));
}
$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;";
$mysqli->query($query);
if ($mysqli->error) {
@ -899,13 +928,15 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
if ($gameMode == 'Script') {
$scriptName = $this->maniaControl->client->getScriptName();
$properties['gamemode'] = $scriptName["CurrentValue"];
} else {
}
else {
$properties['gamemode'] = $gameMode;
}
if ($import) {
$properties['maptime'] = 0;
} else {
}
else {
$properties['maptime'] = time() - $this->mxKarma['startTime'];
}
@ -919,17 +950,19 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$content = json_encode($properties);
$self = $this;
$this->maniaControl->fileReader->postData(self::MX_KARMA_URL . self::MX_KARMA_SAVEVOTES . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey), function ($data, $error) use(&$self){
$this->maniaControl->fileReader->postData(self::MX_KARMA_URL . self::MX_KARMA_SAVEVOTES . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey), function ($data, $error) use(&$self) {
if (!$error) {
$data = json_decode($data);
if ($data->success) {
$self->maniaControl->log("Votes successfully permitted");
} else {
}
else {
$self->maniaControl->log("Error while updating votes: " . $data->data->message);
//TODO remove temp trigger
// TODO remove temp trigger
$self->maniaControl->errorHandler->triggerDebugNotice("Error while updating votes: " . $data->data->message . " " . KarmaPlugin::MX_KARMA_URL . $self::MX_KARMA_SAVEVOTES . "?sessionKey=" . urlencode($self->mxKarma['session']->sessionKey));
}
} else {
}
else {
$self->maniaControl->log($error);
}
}, $content, false, 'application/json');