- improved player and playerHandler
- logs subfolder
This commit is contained in:
parent
bc5b1842c2
commit
950ad1098b
@ -13,10 +13,13 @@ if (function_exists('date_default_timezone_get') && function_exists('date_defaul
|
|||||||
// Error handling
|
// Error handling
|
||||||
ini_set('log_errors', 1);
|
ini_set('log_errors', 1);
|
||||||
ini_set('error_reporting', -1);
|
ini_set('error_reporting', -1);
|
||||||
ini_set('error_log', 'ManiaControl_' . getmypid() . '.log');
|
if (!is_dir('logs')) {
|
||||||
|
mkdir('logs');
|
||||||
|
}
|
||||||
|
ini_set('error_log', 'logs/ManiaControl_' . getmypid() . '.log');
|
||||||
|
|
||||||
// Load ManiaControl class
|
// Load ManiaControl class
|
||||||
require_once __DIR__ . '/core/maniaControlClass.php';
|
require_once __DIR__ . '/core/maniaControl.php';
|
||||||
|
|
||||||
// Start ManiaControl
|
// Start ManiaControl
|
||||||
error_log('Loading ManiaControl v' . ManiaControl::VERSION . '...');
|
error_log('Loading ManiaControl v' . ManiaControl::VERSION . '...');
|
||||||
|
@ -3,37 +3,28 @@
|
|||||||
namespace ManiaControl;
|
namespace ManiaControl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class manialinkidHandler handles manialink id's
|
* Handler for manialink ids
|
||||||
*
|
*
|
||||||
* @author Lukas Kremsmayr and steeffeen
|
* @author kremsy & steeffeen
|
||||||
*/
|
*/
|
||||||
|
class ManialinkIdHandler {
|
||||||
|
/**
|
||||||
class ManialinkIdHandler {
|
* Private properties
|
||||||
/**
|
|
||||||
* Private properties
|
|
||||||
*/
|
|
||||||
private $maniaLinkIdCount;
|
|
||||||
|
|
||||||
public function __construct(){
|
|
||||||
$maniaLinkIdCount = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reservses manialinks for a plugin
|
|
||||||
*
|
|
||||||
* @param int $count
|
|
||||||
* @return array with manialink Ids
|
|
||||||
*/
|
*/
|
||||||
|
private $maniaLinkIdCount = 0;
|
||||||
public function reserveManiaLinkIds($count){
|
|
||||||
$mlIds = array();
|
|
||||||
for($i = 0; $i < $count; $i++){
|
|
||||||
$mlIds[0] = $i + $this->maniaLinkIdCount;
|
|
||||||
}
|
|
||||||
$this->maniaLinkIdCount += $count;
|
|
||||||
return $mlIds;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
/**
|
||||||
|
* Reserve manialink ids
|
||||||
|
*
|
||||||
|
* @param int $count
|
||||||
|
* @return array with manialink Ids
|
||||||
|
*/
|
||||||
|
public function reserveManiaLinkIds($count) {
|
||||||
|
$mlIds = array();
|
||||||
|
for ($i = 0; $i < $count; $i++) {
|
||||||
|
array_push($mlIds, ++$this->maniaLinkIdCount);
|
||||||
|
}
|
||||||
|
return $mlIds;
|
||||||
|
}
|
||||||
|
}
|
||||||
?>
|
?>
|
@ -5,7 +5,7 @@ namespace ManiaControl;
|
|||||||
/**
|
/**
|
||||||
* Class representing players
|
* Class representing players
|
||||||
*
|
*
|
||||||
* @author Kremsy & Steff
|
* @author kremsy & steeffeen
|
||||||
*/
|
*/
|
||||||
class Player {
|
class Player {
|
||||||
/**
|
/**
|
||||||
@ -15,66 +15,92 @@ class Player {
|
|||||||
public $pid = -1;
|
public $pid = -1;
|
||||||
public $login = '';
|
public $login = '';
|
||||||
public $nickname = '';
|
public $nickname = '';
|
||||||
public $isFakePlayer = false;
|
public $path = '';
|
||||||
public $teamName = '';
|
public $joinCount = 0;
|
||||||
public $ip = '';
|
public $totalPlayed = 0;
|
||||||
public $ipFull = '';
|
|
||||||
public $clientVersion = '';
|
|
||||||
public $zone = '';
|
|
||||||
public $continent = '';
|
|
||||||
public $nation = '';
|
|
||||||
public $isSpectator = false;
|
|
||||||
public $isOfficial = false;
|
|
||||||
public $language = '';
|
public $language = '';
|
||||||
public $avatar = '';
|
public $avatar = '';
|
||||||
public $teamId; // TODO: default value
|
public $allies = array();
|
||||||
public $unlocked; // TODO: default value
|
public $clubLink = '';
|
||||||
|
public $teamId = -1;
|
||||||
|
public $isSpectator = false;
|
||||||
|
public $isOfficial = false;
|
||||||
|
public $isReferee = false;
|
||||||
|
public $ladderScore = -1.;
|
||||||
public $ladderRank = -1;
|
public $ladderRank = -1;
|
||||||
public $ladderScore = -1;
|
public $joinTime = -1;
|
||||||
public $created = -1;
|
|
||||||
public $rightLevel = 0;
|
|
||||||
|
|
||||||
// TODO: usefull construct player without rpc info?
|
|
||||||
// TODO: check isFakePlayer (probably server itself also "fakeplayer")
|
|
||||||
// TODO: add all attributes like, allies, clublink ... just make vardump on rpc infos
|
|
||||||
// TODO: READ ADDITIONAL INFOS FROM DATABASE
|
|
||||||
/**
|
/**
|
||||||
* Construct a player
|
* Construct a player from XmlRpc data
|
||||||
*
|
*
|
||||||
* @param array $rpcInfos
|
* @param array $rpcInfos
|
||||||
*/
|
*/
|
||||||
public function __construct($rpcInfos) {
|
public function __construct(array $rpcInfos) {
|
||||||
$this->created = time();
|
|
||||||
if (!$rpcInfos) {
|
if (!$rpcInfos) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->login = $rpcInfos['Login'];
|
|
||||||
$this->isFakePlayer = (stripos($this->login, '*') !== false);
|
|
||||||
$this->nickname = $rpcInfos['NickName'];
|
|
||||||
$this->pid = $rpcInfos['PlayerId'];
|
$this->pid = $rpcInfos['PlayerId'];
|
||||||
$this->teamId = $rpcInfos['TeamId'];
|
$this->login = $rpcInfos['Login'];
|
||||||
$this->ipFull = $rpcInfos['IPAddress'];
|
$this->nickname = $rpcInfos['NickName'];
|
||||||
$this->ip = preg_replace('/:\d+/', '', $this->ipFull);
|
$this->path = $rpcInfos['Path'];
|
||||||
$this->isSpectator = $rpcInfos['IsSpectator'];
|
|
||||||
$this->isOfficial = $rpcInfos['IsInOfficialMode'];
|
|
||||||
$this->teamName = $rpcInfos['LadderStats']['TeamName'];
|
|
||||||
$this->zone = substr($rpcInfos['Path'], 6);
|
|
||||||
$zones = explode('|', $rpcInfos['Path']);
|
|
||||||
if (isset($zones[1])) {
|
|
||||||
if (isset($zones[2])) {
|
|
||||||
$this->continent = $zones[1];
|
|
||||||
$this->nation = $zones[2];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$this->nation = $zones[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$this->ladderRank = $rpcInfos['LadderStats']['PlayerRankings'][0]['Ranking'];
|
|
||||||
$this->ladderScore = round($rpcInfos['LadderStats']['PlayerRankings'][0]['Score'], 2);
|
|
||||||
$this->clientVersion = $rpcInfos['ClientVersion'];
|
|
||||||
$this->language = $rpcInfos['Language'];
|
$this->language = $rpcInfos['Language'];
|
||||||
$this->avatar = $rpcInfos['Avatar']['FileName'];
|
$this->avatar = $rpcInfos['Avatar']['FileName'];
|
||||||
|
|
||||||
|
$this->allies = $rpcInfos['Allies'];
|
||||||
|
$this->clubLink = $rpcInfos['ClubLink'];
|
||||||
|
$this->teamId = $rpcInfos['TeamId'];
|
||||||
|
$this->isSpectator = $rpcInfos['IsSpectator'];
|
||||||
|
$this->isOfficial = $rpcInfos['IsInOfficialMode'];
|
||||||
|
$this->isReferee = $rpcInfos['IsReferee'];
|
||||||
|
$this->ladderScore = $rpcInfos['LadderStats']['PlayerRankings'][0]['Score'];
|
||||||
|
$this->ladderRank = $rpcInfos['LadderStats']['PlayerRankings'][0]['Ranking'];
|
||||||
|
|
||||||
|
$this->joinTime = time();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if player is not a real player
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isFakePlayer() {
|
||||||
|
return ($this->pid <= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get country
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getCountry() {
|
||||||
|
$pathParts = explode('|', $this->path);
|
||||||
|
if (isset($pathParts[2])) {
|
||||||
|
return $pathParts[2];
|
||||||
|
}
|
||||||
|
if (isset($pathParts[1])) {
|
||||||
|
return $pathParts[1];
|
||||||
|
}
|
||||||
|
if (isset($pathParts[0])) {
|
||||||
|
return $pathParts[0];
|
||||||
|
}
|
||||||
|
return $this->path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get continent
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getContinent() {
|
||||||
|
$pathParts = explode('|', $this->path);
|
||||||
|
if (isset($pathParts[1])) {
|
||||||
|
return $pathParts[1];
|
||||||
|
}
|
||||||
|
if (isset($pathParts[0])) {
|
||||||
|
return $pathParts[0];
|
||||||
|
}
|
||||||
|
return $this->path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,9 +7,9 @@ require_once __DIR__ . '/player.php';
|
|||||||
/**
|
/**
|
||||||
* Class managing players
|
* Class managing players
|
||||||
*
|
*
|
||||||
* @package ManiaControl
|
* @author kremsy & steeffeen
|
||||||
*/
|
*/
|
||||||
class playerHandler {
|
class PlayerHandler {
|
||||||
/**
|
/**
|
||||||
* Constants
|
* Constants
|
||||||
*/
|
*/
|
||||||
@ -50,13 +50,10 @@ class playerHandler {
|
|||||||
$mysqli = $this->maniaControl->database->mysqli;
|
$mysqli = $this->maniaControl->database->mysqli;
|
||||||
$playerTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_PLAYERS . "` (
|
$playerTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_PLAYERS . "` (
|
||||||
`index` int(11) NOT NULL AUTO_INCREMENT,
|
`index` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
`pid` int(11) NOT NULL DEFAULT '-1',
|
|
||||||
`login` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
|
`login` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
|
||||||
`ipFull` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
|
`nickname` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
|
||||||
`clientVersion` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
|
`path` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
|
||||||
`zone` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
|
`totalPlayed` int(11) NOT NULL DEFAULT '0' COMMENT 'Seconds',
|
||||||
`language` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
|
|
||||||
`avatar` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
|
|
||||||
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
PRIMARY KEY (`index`),
|
PRIMARY KEY (`index`),
|
||||||
UNIQUE KEY `login` (`login`)
|
UNIQUE KEY `login` (`login`)
|
||||||
@ -84,6 +81,9 @@ class playerHandler {
|
|||||||
$this->maniaControl->client->query('GetPlayerList', 300, 0, 2);
|
$this->maniaControl->client->query('GetPlayerList', 300, 0, 2);
|
||||||
$playerList = $this->maniaControl->client->getResponse();
|
$playerList = $this->maniaControl->client->getResponse();
|
||||||
foreach ($playerList as $player) {
|
foreach ($playerList as $player) {
|
||||||
|
if ($player['PlayerId'] <= 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
$callback = array(Callbacks::CB_MP_PLAYERCONNECT, array($player['Login']));
|
$callback = array(Callbacks::CB_MP_PLAYERCONNECT, array($player['Login']));
|
||||||
$this->playerConnect($callback);
|
$this->playerConnect($callback);
|
||||||
}
|
}
|
||||||
@ -135,6 +135,7 @@ class playerHandler {
|
|||||||
if (!$player) {
|
if (!$player) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
$this->savePlayer($player);
|
||||||
$this->playerList[$player->login] = $player;
|
$this->playerList[$player->login] = $player;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -143,14 +144,113 @@ class playerHandler {
|
|||||||
* Remove a Player from the PlayerList
|
* Remove a Player from the PlayerList
|
||||||
*
|
*
|
||||||
* @param string $login
|
* @param string $login
|
||||||
|
* @param bool $savePlayedTime
|
||||||
* @return Player $player
|
* @return Player $player
|
||||||
*/
|
*/
|
||||||
private function removePlayer($login) {
|
private function removePlayer($login, $savePlayedTime = true) {
|
||||||
if (!isset($this->playerList[$login])) {
|
if (!isset($this->playerList[$login])) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$player = $this->playerList[$login];
|
$player = $this->playerList[$login];
|
||||||
unset($this->playerList[$login]);
|
unset($this->playerList[$login]);
|
||||||
|
if ($savePlayedTime) {
|
||||||
|
$this->updatePlayedTime($player);
|
||||||
|
}
|
||||||
return $player;
|
return $player;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save player in database and fill up object properties
|
||||||
|
*
|
||||||
|
* @param Player $player
|
||||||
|
* @param int $joinCount
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function savePlayer(Player &$player, $joinCount = 1) {
|
||||||
|
if (!$player) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$mysqli = $this->maniaControl->database->mysqli;
|
||||||
|
|
||||||
|
// Save player
|
||||||
|
$playerQuery = "INSERT INTO `" . self::TABLE_PLAYERS . "` (
|
||||||
|
`login`,
|
||||||
|
`nickname`,
|
||||||
|
`path`,
|
||||||
|
`joinCount`
|
||||||
|
) VALUES (
|
||||||
|
?, ?, ?, ?
|
||||||
|
) ON DUPLICATE KEY UPDATE
|
||||||
|
`index` = LAST_INSERT_ID(`index`),
|
||||||
|
`nickname` = VALUES(`nickname`),
|
||||||
|
`path` = VALUES(`path`),
|
||||||
|
`joinCount` = `joinCount` + VALUES(`joinCount`);";
|
||||||
|
$playerStatement = $mysqli->prepare($playerQuery);
|
||||||
|
if ($mysqli->error) {
|
||||||
|
trigger_error($mysqli->error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$playerStatement->bind_param('sssi', $player->login, $player->nickname, $player->path, $joinCount);
|
||||||
|
$playerStatement->execute();
|
||||||
|
if ($playerStatement->error) {
|
||||||
|
trigger_error($playerStatement->error);
|
||||||
|
$playerStatement->close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$player->index = $playerStatement->insert_id;
|
||||||
|
$playerStatement->close();
|
||||||
|
|
||||||
|
// Fill up properties
|
||||||
|
$playerQuery = "SELECT `joinCount`, `totalPlayed` FROM `" . self::TABLE_PLAYERS . "`
|
||||||
|
WHERE `index` = ?;";
|
||||||
|
$playerStatement = $mysqli->prepare($playerQuery);
|
||||||
|
if ($mysqli->error) {
|
||||||
|
trigger_error($mysqli->error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$playerStatement->bind_param('i', $player->index);
|
||||||
|
$playerStatement->execute();
|
||||||
|
if ($playerStatement->error) {
|
||||||
|
trigger_error($playerStatement->error);
|
||||||
|
$playerStatement->close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$playerStatement->store_result();
|
||||||
|
$playerStatement->bind_result($player->joinCount, $player->totalPlayed);
|
||||||
|
$playerStatement->fetch();
|
||||||
|
$playerStatement->free_result();
|
||||||
|
$playerStatement->close();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update total played time of the player
|
||||||
|
*
|
||||||
|
* @param Player $player
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function updatePlayedTime(Player $player) {
|
||||||
|
if (!$player) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$playedTime = time() - $player->joinTime;
|
||||||
|
$mysqli = $this->maniaControl->database->mysqli;
|
||||||
|
$playedQuery = "UPDATE `" . self::TABLE_PLAYERS . "`
|
||||||
|
SET `totalPlayed` = `totalPlayed` + ?;";
|
||||||
|
$playedStatement = $mysqli->prepare($playedQuery);
|
||||||
|
if ($mysqli->error) {
|
||||||
|
trigger_error($mysqli->error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$playedStatement->bind_param('i', $playedTime);
|
||||||
|
$playedStatement->execute();
|
||||||
|
if ($playedStatement->error) {
|
||||||
|
trigger_error($playedStatement->error);
|
||||||
|
$playedStatement->close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$playedStatement->close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user