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();
/**
@ -89,6 +90,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}
/**
*
* @see \ManiaControl\Plugins\Plugin::load()
*/
public function load(ManiaControl $maniaControl) {
@ -130,6 +132,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}
/**
*
* @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() {
@ -201,7 +209,8 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
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'];
@ -212,9 +221,13 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$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,7 +235,8 @@ 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('-');
@ -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,
@ -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();
@ -665,13 +685,15 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
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
$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
$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
$self->getMxKarmaVotes();
} else {
}
else {
$self->maniaControl->log("Error while authenticating on Mania-Exchange Karma " . $data->data->message);
// 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 {
}
else {
// TODO remove temp trigger
$self->maniaControl->errorHandler->triggerDebugNotice("Error while authenticating on Mania-Exchange Karma " . $error);
$self->maniaControl->log($error);
@ -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;
}
@ -752,10 +776,13 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$properties['getvotesonly'] = false;
$properties['playerlogins'] = array();
foreach ($this->maniaControl->playerManager->getPlayers() as $plyr) {
/** @var Player $player */
/**
* @var Player $player
*/
$properties['playerlogins'][] = $plyr->login;
}
} else {
}
else {
$properties['getvotesonly'] = true;
$properties['playerlogins'] = array($player->login);
}
@ -782,12 +809,14 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$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
$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');
@ -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'];
}
@ -924,12 +955,14 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$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
$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');