improved database converter

This commit is contained in:
kremsy 2014-02-06 15:39:20 +01:00 committed by Steffen Schröder
parent 2bcc9029bc
commit 6ccb5117ae

View File

@ -4,20 +4,30 @@
* 2014 by Lukas Kremsmayr * 2014 by Lukas Kremsmayr
**/ **/
//Settings Begin
//Connection Settings
$host = "localhost"; $host = "localhost";
$port = 3306; $port = 3306;
$targetUser = "smparagon"; //Settings of Target Database
$targetDb = "maniacontrol_1337"; $targetUser = "maniacontrol";
$targetDb = "convert_test";
$targetPass = ""; $targetPass = "";
$user = "smesc1"; //Settings of Source Database
$dbname = "smesc2"; $sourceUser = "maniacontrol";
$pass = ""; $sourceDb = "smesc12";
$sourcePass = "";
//Convert Hits to Kills (means each Hit gets Converted to one Kill
$convertHitsToKills = true;
//Settings END
$converter = new DatabaseConverter($host, $port, $targetUser, $targetPass, $targetDb); $converter = new DatabaseConverter($host, $port, $targetUser, $targetPass, $targetDb);
$converter->connectToSourceDB($host, $port, $user, $pass, $dbname); $converter->connectToSourceDB($host, $port, $sourceUser, $sourcePass, $sourceDb);
$converter->convertHitsToKills = $convertHitsToKills;
$test1 = $converter->convertPlayersAndStatistics(); $test1 = $converter->convertPlayersAndStatistics();
$test2 = $converter->convertMapsAndKarma(); $test2 = $converter->convertMapsAndKarma();
unset($converter); unset($converter);
@ -40,13 +50,17 @@ class DatabaseConverter {
/** /**
* Public properties * Public properties
*/ */
public $convertHitsToKills = true;
/**
* Private Properties
*/
private $mysqli = null; private $mysqli = null;
/** @var \mysqli $sourceMysqli */ /** @var \mysqli $sourceMysqli */
private $sourceMysqli = null; private $sourceMysqli = null;
private $targetDatabase = ""; private $targetDatabase = "";
public function __construct($host, $port, $user, $pass, $dbname) { public function __construct($host, $port, $user, $pass, $dbname) {
$host = (string)$host; $host = (string)$host;
$port = (int)$port; $port = (int)$port;
@ -137,7 +151,12 @@ class DatabaseConverter {
return $this->convertKarma(); return $this->convertKarma();
} }
private function convertKarma() { /**
* Converts the Map Karma
* @param $serverIndex
* @return bool
*/
private function convertKarma($serverIndex = -1) {
$success = $this->initKarmaTable(); $success = $this->initKarmaTable();
if (!$success) { if (!$success) {
return false; return false;
@ -152,7 +171,6 @@ class DatabaseConverter {
return false; return false;
} }
$mapvector = array(); $mapvector = array();
while($row = $result->fetch_object()) { while($row = $result->fetch_object()) {
$mapvector[$row->uid] = $row->index; $mapvector[$row->uid] = $row->index;
@ -217,9 +235,54 @@ class DatabaseConverter {
$result->free_result(); $result->free_result();
$statement->close(); $statement->close();
//SELECT PlayerIndex, COUNT(*) FROM `mc_karma` GROUP BY PlayerIndex
$votedMapsQuery = "SELECT PlayerIndex, COUNT(*) as cnt FROM `".self::MC_TABLE_KARMA. "` GROUP BY PlayerIndex;";
$result = $this->mysqli->query($votedMapsQuery);
if (!$result) {
return false;
}
$statisticQuery = "INSERT IGNORE INTO `" . self::MC_TABLE_STATISTICS . "`
(`playerId`, `statId`, `value`, `serverIndex`)
VALUES
(?,?,?,?);";
$statement = $this->mysqli->prepare($statisticQuery);
if ($this->mysqli->error) {
trigger_error($this->mysqli->error);
return false;
}
//Loop through all the players and Insert the Voted Maps Stat
while($row = $result->fetch_object()) {
if ($row->cnt == 0) {
continue;
}
$statId = 15;
$statement->bind_param('iiii', $row->PlayerIndex, $statId, $row->cnt, $serverIndex);
$statement->execute();
if ($statement->error) {
trigger_error($statement->error);
return false;
}
}
$result->free_result();
return true; return true;
} }
/**
* Converts the Players and Statistics Table
* @param $serverIndex
* @return bool
*/
public function convertPlayersAndStatistics($serverIndex = -1) { public function convertPlayersAndStatistics($serverIndex = -1) {
$success = $this->initPlayerTable(); $success = $this->initPlayerTable();
if (!$success) { if (!$success) {
@ -275,9 +338,8 @@ class DatabaseConverter {
return false; return false;
} }
if ($row->Joins > 0) { if ($row->Joins > 0) {
$statId = 0; $statId = 1;
$statStatement->bind_param('iiii', $row->Id, $statId, $row->Joins, $serverIndex); $statStatement->bind_param('iiii', $row->Id, $statId, $row->Joins, $serverIndex);
$statStatement->execute(); $statStatement->execute();
} }
@ -316,6 +378,12 @@ class DatabaseConverter {
$statId = 8; $statId = 8;
$statStatement->bind_param('iiii', $row->Id, $statId, $row->Hits, $serverIndex); $statStatement->bind_param('iiii', $row->Id, $statId, $row->Hits, $serverIndex);
$statStatement->execute(); $statStatement->execute();
if($this->convertHitsToKills){
$statId = 12;
$statStatement->bind_param('iiii', $row->Id, $statId, $row->Hits, $serverIndex);
$statStatement->execute();
}
} }
if ($row->GotHits > 0) { if ($row->GotHits > 0) {
@ -347,9 +415,6 @@ class DatabaseConverter {
$statStatement->bind_param('iiii', $row->Id, $statId, $row->attackerWon, $serverIndex); $statStatement->bind_param('iiii', $row->Id, $statId, $row->attackerWon, $serverIndex);
$statStatement->execute(); $statStatement->execute();
} }
/*
(12, 'Kills', 0, ''),
*/
} }
$statement->close(); $statement->close();
@ -358,6 +423,11 @@ class DatabaseConverter {
return $this->convertPlayersExtra($serverIndex); return $this->convertPlayersExtra($serverIndex);
} }
/**
* Konvert the Players Extra (Donations)
* @param $serverIndex
* @return bool
*/
private function convertPlayersExtra($serverIndex) { //TODO dont rely on playerId from Aseco table private function convertPlayersExtra($serverIndex) { //TODO dont rely on playerId from Aseco table
$databaseQuery = "SELECT * FROM `" . self::AS_TABLE_PLAYERS_EXTRA . "`;"; $databaseQuery = "SELECT * FROM `" . self::AS_TABLE_PLAYERS_EXTRA . "`;";
@ -551,8 +621,9 @@ class DatabaseConverter {
(10, 'Deaths', 0, ''), (10, 'Deaths', 0, ''),
(11, 'Respawns', 0, ''), (11, 'Respawns', 0, ''),
(12, 'Kills', 0, ''), (12, 'Kills', 0, ''),
(13, 'Shots', 0, ''); (13, 'Shots', 0, ''),
(14, 'Won Attacks', 0, '')"; (14, 'Won Attacks', 0, ''),
(15, 'Voted Maps', 0, '');";
$statement = $mysqli->prepare($query); $statement = $mysqli->prepare($query);
if ($mysqli->error) { if ($mysqli->error) {