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 <?php
namespace ManiaControl; namespace ManiaControl\Database;
use ManiaControl\Callbacks\TimerListener; use ManiaControl\Callbacks\TimerListener;
use ManiaControl\ManiaControl;
/** /**
* Database Connection Class * Database Connection Class
@ -16,6 +17,7 @@ class Database implements TimerListener {
* Public Properties * Public Properties
*/ */
public $mysqli = null; public $mysqli = null;
public $migrationHelper = null;
/* /*
* Private Properties * Private Properties
@ -59,6 +61,9 @@ class Database implements TimerListener {
// Register Method which checks the Database Connection every 5 seconds // Register Method which checks the Database Connection every 5 seconds
$this->maniaControl->timerManager->registerTimerListening($this, 'checkConnection', 5000); $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\Exception;
use Maniaplanet\DedicatedServer\Xmlrpc\NotInScriptModeException; use Maniaplanet\DedicatedServer\Xmlrpc\NotInScriptModeException;
use Maniaplanet\DedicatedServer\Xmlrpc\TransportException; use Maniaplanet\DedicatedServer\Xmlrpc\TransportException;
use ManiaControl\Database\Database;
require_once __DIR__ . '/Libs/Maniaplanet/DedicatedServer/Connection.php'; require_once __DIR__ . '/Libs/Maniaplanet/DedicatedServer/Connection.php';
require_once __DIR__ . '/Libs/GbxDataFetcher/gbxdatafetcher.inc.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_POSY = 'Widget-Position: Y';
const SETTING_WIDGET_WIDTH = 'Widget-Size: Width'; const SETTING_WIDGET_WIDTH = 'Widget-Size: Width';
const SETTING_WIDGET_HEIGHT = 'Widget-Size: Height'; const SETTING_WIDGET_HEIGHT = 'Widget-Size: Height';
const STAT_PLAYER_MAPVOTES = 'Voted Maps'; const STAT_PLAYER_MAPVOTES = 'Voted Maps';
/** /**
@ -63,13 +62,15 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
* Private properties * Private properties
*/ */
/** /**
*
* @var maniaControl $maniaControl * @var maniaControl $maniaControl
*/ */
private $maniaControl = null; private $maniaControl = null;
private $updateManialink = false; private $updateManialink = false;
/** @var ManiaLink $manialink */ /**
* @var ManiaLink $manialink
*/
private $manialink = null; private $manialink = null;
private $mxKarma = array(); private $mxKarma = array();
/** /**
@ -89,6 +90,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::load() * @see \ManiaControl\Plugins\Plugin::load()
*/ */
public function load(ManiaControl $maniaControl) { public function load(ManiaControl $maniaControl) {
@ -130,6 +132,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::unload() * @see \ManiaControl\Plugins\Plugin::unload()
*/ */
public function unload() { public function unload() {
@ -141,6 +144,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getId() * @see \ManiaControl\Plugins\Plugin::getId()
*/ */
public static function getId() { public static function getId() {
@ -148,6 +152,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getName() * @see \ManiaControl\Plugins\Plugin::getName()
*/ */
public static function getName() { public static function getName() {
@ -155,6 +160,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getVersion() * @see \ManiaControl\Plugins\Plugin::getVersion()
*/ */
public static function getVersion() { public static function getVersion() {
@ -162,6 +168,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getAuthor() * @see \ManiaControl\Plugins\Plugin::getAuthor()
*/ */
public static function getAuthor() { public static function getAuthor() {
@ -169,6 +176,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getDescription() * @see \ManiaControl\Plugins\Plugin::getDescription()
*/ */
public static function getDescription() { public static function getDescription() {
@ -201,7 +209,8 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
if ($displayMxKarma && isset($this->mxKarma['session']) && isset($this->mxKarma['voteCount'])) { if ($displayMxKarma && isset($this->mxKarma['session']) && isset($this->mxKarma['voteCount'])) {
$karma = $this->mxKarma['modeVoteAverage'] / 100; $karma = $this->mxKarma['modeVoteAverage'] / 100;
$voteCount = $this->mxKarma['modeVoteCount']; $voteCount = $this->mxKarma['modeVoteCount'];
} else { }
else {
$karma = $this->getMapKarma($map); $karma = $this->getMapKarma($map);
$votes = $this->getMapVotes($map); $votes = $this->getMapVotes($map);
$voteCount = $votes['count']; $voteCount = $votes['count'];
@ -212,9 +221,13 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->buildManialink(); $this->buildManialink();
// Update karma gauge & label // Update karma gauge & label
/** @var Gauge $karmaGauge */ /**
* @var Gauge $karmaGauge
*/
$karmaGauge = $this->manialink->karmaGauge; $karmaGauge = $this->manialink->karmaGauge;
/** @var Label $karmaLabel */ /**
* @var Label $karmaLabel
*/
$karmaLabel = $this->manialink->karmaLabel; $karmaLabel = $this->manialink->karmaLabel;
if (is_numeric($karma) && $voteCount > 0) { if (is_numeric($karma) && $voteCount > 0) {
$karma = floatval($karma); $karma = floatval($karma);
@ -222,7 +235,8 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$karmaColor = ColorUtil::floatToStatusColor($karma); $karmaColor = ColorUtil::floatToStatusColor($karma);
$karmaGauge->setColor($karmaColor . '7'); $karmaGauge->setColor($karmaColor . '7');
$karmaLabel->setText(' ' . round($karma * 100.) . '% (' . $voteCount . ')'); $karmaLabel->setText(' ' . round($karma * 100.) . '% (' . $voteCount . ')');
} else { }
else {
$karmaGauge->setRatio(0.); $karmaGauge->setRatio(0.);
$karmaGauge->setColor('00fb'); $karmaGauge->setColor('00fb');
$karmaLabel->setText('-'); $karmaLabel->setText('-');
@ -350,7 +364,8 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$modeSum = $this->mxKarma["modeVoteCount"] * $this->mxKarma["modeVoteAverage"] + $vote * 100; $modeSum = $this->mxKarma["modeVoteCount"] * $this->mxKarma["modeVoteAverage"] + $vote * 100;
$this->mxKarma["modeVoteCount"]++; $this->mxKarma["modeVoteCount"]++;
} else { }
else {
$oldVote = $this->mxKarma["votes"][$player->login]; $oldVote = $this->mxKarma["votes"][$player->login];
$sum = $this->mxKarma["voteCount"] * $this->mxKarma["voteAverage"] - $oldVote + $vote * 100; $sum = $this->mxKarma["voteCount"] * $this->mxKarma["voteAverage"] - $oldVote + $vote * 100;
$modeSum = $this->mxKarma["modeVoteCount"] * $this->mxKarma["modeVoteAverage"] - $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; $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);
@ -396,6 +410,8 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
*/ */
private function initTables() { private function initTables() {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
// Create local table
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_KARMA . "` ( $query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_KARMA . "` (
`index` int(11) NOT NULL AUTO_INCREMENT, `index` int(11) NOT NULL AUTO_INCREMENT,
`mapIndex` int(11) NOT NULL, `mapIndex` int(11) NOT NULL,
@ -410,10 +426,14 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
trigger_error($mysqli->error, E_USER_ERROR); 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)) { if (!$this->maniaControl->settingManager->getSetting($this, self::SETTING_MX_KARMA_ACTIVATED)) {
return; return;
} }
// 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,
`mapIndex` int(11) NOT NULL, `mapIndex` int(11) NOT NULL,
@ -604,7 +624,6 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->manialink = $manialink; $this->manialink = $manialink;
} }
/** /**
* Update Settings * Update Settings
* *
@ -625,7 +644,8 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
if ($settingName == 'Enable Karma Widget' && $value == true) { if ($settingName == 'Enable Karma Widget' && $value == true) {
$this->updateManialink = true; $this->updateManialink = true;
$this->handle1Second(time()); $this->handle1Second(time());
} elseif ($settingName == 'Enable Karma Widget' && $value == false) { }
elseif ($settingName == 'Enable Karma Widget' && $value == false) {
$this->updateManialink = false; $this->updateManialink = false;
$ml = new ManiaLink(self::MLID_KARMA); $ml = new ManiaLink(self::MLID_KARMA);
$mltext = $ml->render()->saveXML(); $mltext = $ml->render()->saveXML();
@ -665,13 +685,15 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
if ($data->success) { if ($data->success) {
$self->mxKarma['session'] = $data->data; $self->mxKarma['session'] = $data->data;
$self->activateSession($mxKarmaCode); $self->activateSession($mxKarmaCode);
} else { }
else {
$self->maniaControl->log("Error while authenticating on Mania-Exchange Karma"); $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->maniaControl->errorHandler->triggerDebugNotice("Error while authenticating on Mania-Exchange Karma " . $data->data->message);
$self->mxKarma['connectionInProgress'] = false; $self->mxKarma['connectionInProgress'] = false;
} }
} else { }
else {
$self->maniaControl->log($error); $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->maniaControl->errorHandler->triggerDebugNotice("Error while authenticating on Mania-Exchange Karma " . $error);
@ -702,13 +724,15 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
// Fetch the Mx Karma Votes // Fetch the Mx Karma Votes
$self->getMxKarmaVotes(); $self->getMxKarmaVotes();
} else { }
else {
$self->maniaControl->log("Error while authenticating on Mania-Exchange Karma " . $data->data->message); $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->maniaControl->errorHandler->triggerDebugNotice("Error while authenticating on Mania-Exchange Karma " . $data->data->message . " url Query " . $query);
$self->mxKarma['connectionInProgress'] = false; $self->mxKarma['connectionInProgress'] = false;
} }
} else { }
else {
// TODO remove temp trigger // TODO remove temp trigger
$self->maniaControl->errorHandler->triggerDebugNotice("Error while authenticating on Mania-Exchange Karma " . $error); $self->maniaControl->errorHandler->triggerDebugNotice("Error while authenticating on Mania-Exchange Karma " . $error);
$self->maniaControl->log($error); $self->maniaControl->log($error);
@ -717,7 +741,6 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}, "application/json", 1000); }, "application/json", 1000);
} }
/** /**
* Fetch the mxKarmaVotes for the current map * Fetch the mxKarmaVotes for the current map
*/ */
@ -741,7 +764,8 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
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;
} }
@ -752,10 +776,13 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$properties['getvotesonly'] = false; $properties['getvotesonly'] = false;
$properties['playerlogins'] = array(); $properties['playerlogins'] = array();
foreach ($this->maniaControl->playerManager->getPlayers() as $plyr) { foreach ($this->maniaControl->playerManager->getPlayers() as $plyr) {
/** @var Player $player */ /**
* @var Player $player
*/
$properties['playerlogins'][] = $plyr->login; $properties['playerlogins'][] = $plyr->login;
} }
} else { }
else {
$properties['getvotesonly'] = true; $properties['getvotesonly'] = true;
$properties['playerlogins'] = array($player->login); $properties['playerlogins'] = array($player->login);
} }
@ -782,12 +809,14 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$self->updateManialink = true; $self->updateManialink = true;
$self->maniaControl->callbackManager->triggerCallback($self::CB_KARMA_MXUPDATED, $self->mxKarma); $self->maniaControl->callbackManager->triggerCallback($self::CB_KARMA_MXUPDATED, $self->mxKarma);
$self->maniaControl->log("MX-Karma Votes successfully fetched"); $self->maniaControl->log("MX-Karma Votes successfully fetched");
} else { }
else {
$self->maniaControl->log("Error while fetching votes: " . $data->data->message); $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)); $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); $self->maniaControl->log($error);
} }
}, $content, false, 'application/json'); }, $content, false, 'application/json');
@ -899,13 +928,15 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
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;
} }
if ($import) { if ($import) {
$properties['maptime'] = 0; $properties['maptime'] = 0;
} else { }
else {
$properties['maptime'] = time() - $this->mxKarma['startTime']; $properties['maptime'] = time() - $this->mxKarma['startTime'];
} }
@ -924,12 +955,14 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$data = json_decode($data); $data = json_decode($data);
if ($data->success) { if ($data->success) {
$self->maniaControl->log("Votes successfully permitted"); $self->maniaControl->log("Votes successfully permitted");
} else { }
else {
$self->maniaControl->log("Error while updating votes: " . $data->data->message); $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)); $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); $self->maniaControl->log($error);
} }
}, $content, false, 'application/json'); }, $content, false, 'application/json');