2013-11-09 20:18:49 +01:00
|
|
|
<?php
|
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
namespace ManiaControl\Players;
|
|
|
|
|
2014-01-31 13:40:07 +01:00
|
|
|
use ManiaControl\Admin\AdminLists;
|
2013-11-12 15:48:25 +01:00
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
2014-03-02 11:25:47 +01:00
|
|
|
use ManiaControl\Callbacks\TimerListener;
|
2013-12-31 20:10:40 +01:00
|
|
|
use ManiaControl\Formatter;
|
|
|
|
use ManiaControl\ManiaControl;
|
2014-01-03 19:14:07 +01:00
|
|
|
use ManiaControl\Statistics\StatisticManager;
|
2014-04-20 19:59:01 +02:00
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\LoginUnknownException;
|
2013-11-09 20:18:49 +01:00
|
|
|
|
|
|
|
/**
|
2014-01-06 16:14:49 +01:00
|
|
|
* Class managing Players
|
2013-11-10 02:55:08 +01:00
|
|
|
*
|
2014-04-20 19:59:01 +02:00
|
|
|
* @author kremsy & steeffeen
|
2014-04-12 12:14:37 +02:00
|
|
|
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
|
2014-04-20 19:59:01 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2013-11-09 20:18:49 +01:00
|
|
|
*/
|
2014-03-02 11:25:47 +01:00
|
|
|
class PlayerManager implements CallbackListener, TimerListener {
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2013-11-10 02:55:08 +01:00
|
|
|
* Constants
|
|
|
|
*/
|
2014-02-19 10:43:37 +01:00
|
|
|
const CB_PLAYERCONNECT = 'PlayerManagerCallback.PlayerConnect';
|
|
|
|
const CB_PLAYERDISCONNECT = 'PlayerManagerCallback.PlayerDisconnect';
|
2014-01-16 00:24:24 +01:00
|
|
|
const CB_PLAYERINFOCHANGED = 'PlayerManagerCallback.PlayerInfoChanged';
|
|
|
|
const TABLE_PLAYERS = 'mc_players';
|
2013-11-12 19:33:25 +01:00
|
|
|
const SETTING_JOIN_LEAVE_MESSAGES = 'Enable Join & Leave Messages';
|
2014-01-16 00:24:24 +01:00
|
|
|
const STAT_JOIN_COUNT = 'Joins';
|
|
|
|
const STAT_SERVERTIME = 'Servertime';
|
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-01-01 18:53:19 +01:00
|
|
|
* Public Properties
|
2013-11-10 02:55:08 +01:00
|
|
|
*/
|
2014-01-01 18:53:19 +01:00
|
|
|
public $playerActions = null;
|
|
|
|
public $playerCommands = null;
|
2014-01-03 16:24:35 +01:00
|
|
|
public $playerDetailed = null;
|
2014-01-06 16:14:49 +01:00
|
|
|
public $playerList = null;
|
2014-01-31 13:40:07 +01:00
|
|
|
public $adminLists = null;
|
2014-01-06 16:14:49 +01:00
|
|
|
public $players = array();
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-01-01 18:53:19 +01:00
|
|
|
* Private Properties
|
2013-12-28 19:48:06 +01:00
|
|
|
*/
|
2014-01-01 18:53:19 +01:00
|
|
|
private $maniaControl = null;
|
2013-12-28 19:48:06 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
2014-01-01 18:53:19 +01:00
|
|
|
* Construct a new Player Manager
|
2013-11-10 02:55:08 +01:00
|
|
|
*
|
2013-12-31 15:27:25 +01:00
|
|
|
* @param \ManiaControl\ManiaControl $maniaControl
|
2013-11-10 02:55:08 +01:00
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
$this->initTables();
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2014-04-21 02:01:30 +02:00
|
|
|
$this->playerCommands = new PlayerCommands($maniaControl);
|
|
|
|
$this->playerActions = new PlayerActions($maniaControl);
|
|
|
|
$this->playerDetailed = new PlayerDetailed($maniaControl);
|
|
|
|
$this->playerDataManager = new PlayerDataManager($maniaControl);
|
|
|
|
$this->playerList = new PlayerList($maniaControl);
|
|
|
|
$this->adminLists = new AdminLists($maniaControl);
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2013-11-28 03:47:08 +01:00
|
|
|
// Init settings
|
2013-12-08 22:38:20 +01:00
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES, true);
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2013-11-28 03:47:08 +01:00
|
|
|
// Register for callbacks
|
2014-02-19 12:53:06 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_ONINIT, $this, 'onInit');
|
2013-11-12 15:48:25 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCONNECT, $this, 'playerConnect');
|
2014-01-02 13:35:52 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERDISCONNECT, $this, 'playerDisconnect');
|
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERINFOCHANGED, $this, 'playerInfoChanged');
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2014-01-01 18:53:19 +01:00
|
|
|
// Define player stats
|
2013-12-31 20:10:40 +01:00
|
|
|
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_JOIN_COUNT);
|
2014-01-03 21:11:13 +01:00
|
|
|
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_SERVERTIME, StatisticManager::STAT_TYPE_TIME);
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|
2013-11-09 23:01:54 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
2013-11-10 20:21:12 +01:00
|
|
|
* Initialize necessary database tables
|
2013-11-10 02:55:08 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function initTables() {
|
2014-01-16 00:24:24 +01:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
$playerTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_PLAYERS . "` (
|
2013-11-10 02:55:08 +01:00
|
|
|
`index` int(11) NOT NULL AUTO_INCREMENT,
|
2014-02-13 14:58:04 +01:00
|
|
|
`login` varchar(100) NOT NULL,
|
|
|
|
`nickname` varchar(150) NOT NULL,
|
|
|
|
`path` varchar(100) NOT NULL,
|
2013-11-10 19:30:14 +01:00
|
|
|
`authLevel` int(11) NOT NULL DEFAULT '0',
|
2013-11-10 02:55:08 +01:00
|
|
|
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
|
|
PRIMARY KEY (`index`),
|
|
|
|
UNIQUE KEY `login` (`login`)
|
2014-01-01 18:53:19 +01:00
|
|
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Player Data' AUTO_INCREMENT=1;";
|
2013-11-10 02:55:08 +01:00
|
|
|
$playerTableStatement = $mysqli->prepare($playerTableQuery);
|
2014-01-28 17:12:23 +01:00
|
|
|
if ($mysqli->error) {
|
2013-11-10 02:55:08 +01:00
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$playerTableStatement->execute();
|
2014-01-28 17:12:23 +01:00
|
|
|
if ($playerTableStatement->error) {
|
2013-11-10 02:55:08 +01:00
|
|
|
trigger_error($playerTableStatement->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$playerTableStatement->close();
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-09 20:18:49 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
|
|
|
* Handle OnInit callback
|
|
|
|
*/
|
2014-02-19 15:44:00 +01:00
|
|
|
public function onInit() {
|
2013-11-12 19:33:25 +01:00
|
|
|
// Add all players
|
2014-01-16 18:08:32 +01:00
|
|
|
$players = $this->maniaControl->client->getPlayerList(300, 0, 2);
|
2014-01-16 00:24:24 +01:00
|
|
|
foreach($players as $playerItem) {
|
2014-01-28 17:12:23 +01:00
|
|
|
if ($playerItem->playerId <= 0) {
|
2013-11-10 17:23:00 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-24 16:31:16 +01:00
|
|
|
|
2014-01-24 22:34:35 +01:00
|
|
|
$detailedPlayerInfo = $this->maniaControl->client->getDetailedPlayerInfo($playerItem->login);
|
2014-01-25 11:40:34 +01:00
|
|
|
$playerItem->path = $detailedPlayerInfo->path;
|
|
|
|
$playerItem->language = $detailedPlayerInfo->language;
|
|
|
|
$playerItem->clientVersion = $detailedPlayerInfo->clientVersion;
|
|
|
|
$playerItem->iPAddress = $detailedPlayerInfo->iPAddress;
|
|
|
|
$playerItem->isSpectator = $detailedPlayerInfo->isSpectator;
|
|
|
|
$playerItem->avatar = $detailedPlayerInfo->avatar;
|
|
|
|
$playerItem->ladderStats = $detailedPlayerInfo->ladderStats;
|
2014-01-28 17:12:23 +01:00
|
|
|
$playerItem->downloadRate = $detailedPlayerInfo->downloadRate;
|
|
|
|
$playerItem->uploadRate = $detailedPlayerInfo->uploadRate;
|
|
|
|
|
2014-01-25 13:22:56 +01:00
|
|
|
$playerItem->hoursSinceZoneInscription = $detailedPlayerInfo->hoursSinceZoneInscription;
|
2014-01-25 11:40:34 +01:00
|
|
|
|
2014-02-01 19:06:21 +01:00
|
|
|
//Check if the Player is in a Team, to notify if its a TeamMode or not
|
|
|
|
if ($playerItem->teamId != -1) {
|
|
|
|
$this->maniaControl->server->setTeamMode(true);
|
|
|
|
}
|
|
|
|
|
2014-01-25 11:40:34 +01:00
|
|
|
$player = new Player($playerItem);
|
2014-01-23 16:15:41 +01:00
|
|
|
$player->hasJoinedGame = true;
|
2013-11-12 15:48:25 +01:00
|
|
|
$this->addPlayer($player);
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-09 20:18:49 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
|
|
|
* Handle playerConnect callback
|
|
|
|
*
|
2013-12-31 15:27:25 +01:00
|
|
|
* @param array $callback
|
2013-11-10 02:55:08 +01:00
|
|
|
*/
|
|
|
|
public function playerConnect(array $callback) {
|
2014-03-13 18:25:29 +01:00
|
|
|
$login = $callback[1][0];
|
|
|
|
try {
|
|
|
|
$playerInfo = $this->maniaControl->client->getDetailedPlayerInfo($login);
|
|
|
|
$player = new Player($playerInfo);
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2014-03-13 18:25:29 +01:00
|
|
|
$this->addPlayer($player);
|
2014-04-20 19:59:01 +02:00
|
|
|
} catch(LoginUnknownException $e) {
|
2014-03-13 18:25:29 +01:00
|
|
|
}
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|
2013-11-09 22:08:06 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
|
|
|
* Handle playerDisconnect callback
|
|
|
|
*
|
2013-12-31 15:27:25 +01:00
|
|
|
* @param array $callback
|
2013-11-10 02:55:08 +01:00
|
|
|
*/
|
|
|
|
public function playerDisconnect(array $callback) {
|
2014-01-16 00:24:24 +01:00
|
|
|
$login = $callback[1][0];
|
2013-11-10 02:55:08 +01:00
|
|
|
$player = $this->removePlayer($login);
|
2014-04-20 19:59:01 +02:00
|
|
|
if (!$player) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-29 13:18:27 +01:00
|
|
|
// Trigger own callback
|
2014-02-19 15:44:00 +01:00
|
|
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERDISCONNECT, $player);
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2014-04-21 02:01:30 +02:00
|
|
|
if ($player->isFakePlayer()) {
|
2014-04-20 19:59:01 +02:00
|
|
|
return;
|
2014-04-21 02:01:30 +02:00
|
|
|
}
|
2014-01-16 00:24:24 +01:00
|
|
|
|
|
|
|
$played = Formatter::formatTimeH(time() - $player->joinTime);
|
2014-01-01 18:53:19 +01:00
|
|
|
$logMessage = "Player left: {$player->login} / {$player->nickname} Playtime: {$played}";
|
|
|
|
$this->maniaControl->log(Formatter::stripCodes($logMessage));
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2014-01-28 17:12:23 +01:00
|
|
|
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES)) {
|
2014-04-28 18:25:45 +02:00
|
|
|
$this->maniaControl->chat->sendChat('$0f0$<$fff' . $player->nickname . '$> has left the game');
|
2013-12-09 19:31:55 +01:00
|
|
|
}
|
2014-04-22 22:42:34 +02:00
|
|
|
|
|
|
|
//Destroys stored PlayerData, after all Disconnect Callbacks got Handled
|
|
|
|
$this->playerDataManager->destroyPlayerData($player);
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|
2013-11-09 22:08:06 +01:00
|
|
|
|
2013-12-19 21:59:10 +01:00
|
|
|
/**
|
|
|
|
* Update PlayerInfo
|
2013-12-31 15:27:25 +01:00
|
|
|
*
|
2013-12-19 21:59:10 +01:00
|
|
|
* @param array $callback
|
|
|
|
*/
|
2013-12-31 15:27:25 +01:00
|
|
|
public function playerInfoChanged(array $callback) {
|
2013-12-19 21:59:10 +01:00
|
|
|
$player = $this->getPlayer($callback[1][0]['Login']);
|
2014-04-22 22:42:34 +02:00
|
|
|
if (!$player) {
|
2014-04-20 19:59:01 +02:00
|
|
|
return;
|
2014-04-22 22:42:34 +02:00
|
|
|
}
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2014-01-24 22:34:35 +01:00
|
|
|
$player->ladderRank = $callback[1][0]["LadderRanking"];
|
2014-02-01 19:06:21 +01:00
|
|
|
$player->teamId = $callback[1][0]["TeamId"];
|
|
|
|
|
|
|
|
//Check if the Player is in a Team, to notify if its a TeamMode or not
|
|
|
|
if ($player->teamId != -1) {
|
|
|
|
$this->maniaControl->server->setTeamMode(true);
|
|
|
|
}
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2014-01-23 16:15:41 +01:00
|
|
|
$prevJoinState = $player->hasJoinedGame;
|
|
|
|
|
|
|
|
$player->updatePlayerFlags($callback[1][0]["Flags"]);
|
|
|
|
$player->updateSpectatorStatus($callback[1][0]["SpectatorStatus"]);
|
|
|
|
|
|
|
|
//Check if Player finished joining the game
|
2014-03-02 14:18:10 +01:00
|
|
|
if ($player->hasJoinedGame && !$prevJoinState) {
|
|
|
|
|
|
|
|
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES) && !$player->isFakePlayer()) {
|
|
|
|
$string = array(0 => '$0f0Player', 1 => '$0f0Moderator', 2 => '$0f0Admin', 3 => '$0f0SuperAdmin', 4 => '$0f0MasterAdmin');
|
2014-04-28 18:25:45 +02:00
|
|
|
$chatMessage = '$0f0' . $string[$player->authLevel] . ' $<$fff' . $player->nickname . '$> Nation: $<$fff' . $player->getCountry() . '$> joined!';
|
2014-03-02 14:18:10 +01:00
|
|
|
$this->maniaControl->chat->sendChat($chatMessage);
|
|
|
|
$this->maniaControl->chat->sendInformation('This server uses ManiaControl v' . ManiaControl::VERSION . '!', $player->login);
|
|
|
|
}
|
|
|
|
|
|
|
|
$logMessage = "Player joined: {$player->login} / " . Formatter::stripCodes($player->nickname) . " Nation: " . $player->getCountry() . " IP: {$player->ipAddress}";
|
|
|
|
$this->maniaControl->log($logMessage);
|
|
|
|
|
|
|
|
// Increment the Player Join Count
|
|
|
|
$this->maniaControl->statisticManager->incrementStat(self::STAT_JOIN_COUNT, $player, $this->maniaControl->server->index);
|
|
|
|
|
|
|
|
// Trigger own PlayerJoined callback
|
|
|
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERCONNECT, $player);
|
|
|
|
|
2014-01-23 16:15:41 +01:00
|
|
|
}
|
2014-01-24 15:32:34 +01:00
|
|
|
|
2013-12-19 21:59:10 +01:00
|
|
|
// Trigger own callback
|
2014-02-19 15:44:00 +01:00
|
|
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERINFOCHANGED, $player);
|
2013-12-19 21:59:10 +01:00
|
|
|
}
|
|
|
|
|
2014-01-23 16:15:41 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
/**
|
2014-01-06 16:14:49 +01:00
|
|
|
* Get all Players
|
2013-11-28 01:12:52 +01:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getPlayers() {
|
2014-01-06 16:14:49 +01:00
|
|
|
return $this->players;
|
2013-11-28 01:12:52 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 20:29:50 +01:00
|
|
|
/**
|
|
|
|
* Gets the Count of all Player
|
2014-02-15 23:08:51 +01:00
|
|
|
*
|
2014-02-08 20:29:50 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
2014-02-15 23:08:51 +01:00
|
|
|
public function getPlayerCount() {
|
2014-02-08 20:29:50 +01:00
|
|
|
$count = 0;
|
2014-02-15 23:08:51 +01:00
|
|
|
foreach($this->players as $player) {
|
2014-02-08 20:29:50 +01:00
|
|
|
/** @var Player $player */
|
2014-02-15 23:08:51 +01:00
|
|
|
if (!$player->isSpectator) {
|
2014-02-08 20:29:50 +01:00
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the Count of all Spectators
|
2014-02-15 23:08:51 +01:00
|
|
|
*
|
2014-02-08 20:29:50 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
2014-02-15 23:08:51 +01:00
|
|
|
public function getSpectatorCount() {
|
2014-02-08 20:29:50 +01:00
|
|
|
$count = 0;
|
2014-02-15 23:08:51 +01:00
|
|
|
foreach($this->players as $player) {
|
2014-02-08 20:29:50 +01:00
|
|
|
/** @var Player $player */
|
2014-02-15 23:08:51 +01:00
|
|
|
if ($player->isSpectator) {
|
2014-02-08 20:29:50 +01:00
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
2014-01-16 00:24:24 +01:00
|
|
|
/**
|
|
|
|
* Gets a Player by his index
|
|
|
|
*
|
2014-04-20 19:59:01 +02:00
|
|
|
* @param $index
|
|
|
|
* @param bool $connectedPlayersOnly
|
2014-01-16 00:24:24 +01:00
|
|
|
* @return Player|null
|
|
|
|
*/
|
2014-04-20 19:59:01 +02:00
|
|
|
public function getPlayerByIndex($index, $connectedPlayersOnly = false) {
|
2014-01-16 00:24:24 +01:00
|
|
|
foreach($this->players as $player) {
|
|
|
|
/** @var Player $player */
|
2014-01-28 17:12:23 +01:00
|
|
|
if ($player->index == $index) {
|
2014-01-16 00:24:24 +01:00
|
|
|
return $player;
|
|
|
|
}
|
|
|
|
}
|
2014-04-20 19:59:01 +02:00
|
|
|
|
|
|
|
if ($connectedPlayersOnly) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-01-21 21:58:39 +01:00
|
|
|
//Player is not online -> get Player from Database
|
|
|
|
return $this->getPlayerFromDatabaseByIndex($index);
|
2014-01-16 00:24:24 +01:00
|
|
|
}
|
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
2014-01-06 16:14:49 +01:00
|
|
|
* Get a Player by Login
|
2013-11-10 02:55:08 +01:00
|
|
|
*
|
2013-12-31 15:27:25 +01:00
|
|
|
* @param string $login
|
2014-04-20 19:59:01 +02:00
|
|
|
* @param bool $connectedPlayersOnly
|
|
|
|
* @return Player|null
|
2013-11-10 02:55:08 +01:00
|
|
|
*/
|
2014-04-20 19:59:01 +02:00
|
|
|
public function getPlayer($login, $connectedPlayersOnly = false) {
|
2014-01-28 17:12:23 +01:00
|
|
|
if (!isset($this->players[$login])) {
|
2014-04-20 19:59:01 +02:00
|
|
|
if ($connectedPlayersOnly) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-01-31 14:19:39 +01:00
|
|
|
return $this->getPlayerFromDatabaseByLogin($login);
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|
2014-01-06 16:14:49 +01:00
|
|
|
return $this->players[$login];
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|
2013-11-09 22:08:06 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
2014-01-06 16:14:49 +01:00
|
|
|
* Add a player
|
2013-11-10 02:55:08 +01:00
|
|
|
*
|
2013-12-31 15:27:25 +01:00
|
|
|
* @param Player $player
|
2013-11-10 02:55:08 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function addPlayer(Player $player) {
|
2013-11-10 17:23:00 +01:00
|
|
|
$this->savePlayer($player);
|
2014-01-06 16:14:49 +01:00
|
|
|
$this->players[$player->login] = $player;
|
2013-11-10 02:55:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
2013-11-09 22:08:06 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
2014-01-06 16:14:49 +01:00
|
|
|
* Remove a Player
|
2013-11-10 02:55:08 +01:00
|
|
|
*
|
2013-12-31 15:27:25 +01:00
|
|
|
* @param string $login
|
2014-01-16 00:24:24 +01:00
|
|
|
* @param bool $savePlayedTime
|
2013-11-10 02:55:08 +01:00
|
|
|
* @return Player $player
|
|
|
|
*/
|
2013-11-10 17:23:00 +01:00
|
|
|
private function removePlayer($login, $savePlayedTime = true) {
|
2014-01-28 17:12:23 +01:00
|
|
|
if (!isset($this->players[$login])) {
|
2014-01-16 00:24:24 +01:00
|
|
|
return null;
|
|
|
|
}
|
2014-01-06 16:14:49 +01:00
|
|
|
$player = $this->players[$login];
|
|
|
|
unset($this->players[$login]);
|
2014-01-28 17:12:23 +01:00
|
|
|
if ($savePlayedTime) {
|
2013-11-10 17:23:00 +01:00
|
|
|
$this->updatePlayedTime($player);
|
|
|
|
}
|
2013-11-10 02:55:08 +01:00
|
|
|
return $player;
|
|
|
|
}
|
2013-11-10 17:23:00 +01:00
|
|
|
|
2014-01-21 21:58:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get's a Player out of the database
|
|
|
|
*
|
|
|
|
* @param $playerIndex
|
|
|
|
* @return Player $player
|
|
|
|
*/
|
|
|
|
private function getPlayerFromDatabaseByIndex($playerIndex) {
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
|
2014-01-28 17:12:23 +01:00
|
|
|
if (!is_numeric($playerIndex)) {
|
2014-01-21 21:58:39 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = "SELECT * FROM `" . self::TABLE_PLAYERS . "` WHERE `index` = " . $playerIndex . ";";
|
|
|
|
$result = $mysqli->query($query);
|
2014-01-28 17:12:23 +01:00
|
|
|
if (!$result) {
|
2014-01-21 21:58:39 +01:00
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$row = $result->fetch_object();
|
|
|
|
$result->close();
|
|
|
|
|
2014-01-31 14:19:39 +01:00
|
|
|
if (!isset($row)) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-02-01 19:06:21 +01:00
|
|
|
|
2014-04-27 15:12:09 +02:00
|
|
|
$player = new Player(null);
|
2014-02-24 20:20:17 +01:00
|
|
|
$player->index = $playerIndex;
|
|
|
|
$player->login = $row->login;
|
|
|
|
$player->rawNickname = $row->nickname;
|
|
|
|
$player->nickname = Formatter::stripDirtyCodes($player->rawNickname);
|
|
|
|
$player->path = $row->path;
|
|
|
|
$player->authLevel = $row->authLevel;
|
2014-01-21 21:58:39 +01:00
|
|
|
|
|
|
|
return $player;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-31 14:19:39 +01:00
|
|
|
/**
|
|
|
|
* Get's a Player out of the database
|
|
|
|
*
|
|
|
|
* @param $playerIndex
|
|
|
|
* @return Player $player
|
|
|
|
*/
|
|
|
|
private function getPlayerFromDatabaseByLogin($playerLogin) {
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
|
|
|
|
$query = "SELECT * FROM `" . self::TABLE_PLAYERS . "` WHERE `login` LIKE '" . $mysqli->escape_string($playerLogin) . "';";
|
|
|
|
$result = $mysqli->query($query);
|
|
|
|
if (!$result) {
|
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$row = $result->fetch_object();
|
|
|
|
$result->close();
|
|
|
|
|
|
|
|
if (!isset($row)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-04-27 15:12:09 +02:00
|
|
|
$player = new Player(null);
|
2014-02-24 20:20:17 +01:00
|
|
|
$player->index = $row->index;
|
|
|
|
$player->login = $row->login;
|
|
|
|
$player->rawNickname = $row->nickname;
|
|
|
|
$player->nickname = Formatter::stripDirtyCodes($player->rawNickname);
|
|
|
|
$player->path = $row->path;
|
|
|
|
$player->authLevel = $row->authLevel;
|
2014-01-31 14:19:39 +01:00
|
|
|
|
|
|
|
return $player;
|
|
|
|
}
|
|
|
|
|
2013-11-10 17:23:00 +01:00
|
|
|
/**
|
2014-01-06 16:14:49 +01:00
|
|
|
* Save player in Database and fill up Object Properties
|
2013-11-10 17:23:00 +01:00
|
|
|
*
|
2013-12-31 15:27:25 +01:00
|
|
|
* @param Player $player
|
2013-11-10 17:23:00 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-01-01 18:53:19 +01:00
|
|
|
private function savePlayer(Player &$player) {
|
2013-11-10 17:23:00 +01:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2013-11-10 17:23:00 +01:00
|
|
|
// Save player
|
2014-01-16 00:24:24 +01:00
|
|
|
$playerQuery = "INSERT INTO `" . self::TABLE_PLAYERS . "` (
|
2013-11-10 17:23:00 +01:00
|
|
|
`login`,
|
|
|
|
`nickname`,
|
2014-01-01 18:53:19 +01:00
|
|
|
`path`
|
2013-11-10 17:23:00 +01:00
|
|
|
) VALUES (
|
2014-01-01 18:53:19 +01:00
|
|
|
?, ?, ?
|
2013-11-10 17:23:00 +01:00
|
|
|
) ON DUPLICATE KEY UPDATE
|
|
|
|
`index` = LAST_INSERT_ID(`index`),
|
|
|
|
`nickname` = VALUES(`nickname`),
|
2014-01-01 18:53:19 +01:00
|
|
|
`path` = VALUES(`path`);";
|
2013-11-10 17:23:00 +01:00
|
|
|
$playerStatement = $mysqli->prepare($playerQuery);
|
2014-01-28 17:12:23 +01:00
|
|
|
if ($mysqli->error) {
|
2013-11-10 17:23:00 +01:00
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-24 20:20:17 +01:00
|
|
|
$playerStatement->bind_param('sss', $player->login, $player->rawNickname, $player->path);
|
2013-11-10 17:23:00 +01:00
|
|
|
$playerStatement->execute();
|
2014-01-28 17:12:23 +01:00
|
|
|
if ($playerStatement->error) {
|
2013-11-10 17:23:00 +01:00
|
|
|
trigger_error($playerStatement->error);
|
|
|
|
$playerStatement->close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$player->index = $playerStatement->insert_id;
|
|
|
|
$playerStatement->close();
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2014-01-01 18:53:19 +01:00
|
|
|
// Get Player Auth Level from DB
|
2014-01-16 00:24:24 +01:00
|
|
|
$playerQuery = "SELECT `authLevel` FROM `" . self::TABLE_PLAYERS . "` WHERE `index` = ?;";
|
2013-11-10 17:23:00 +01:00
|
|
|
$playerStatement = $mysqli->prepare($playerQuery);
|
2014-01-28 17:12:23 +01:00
|
|
|
if ($mysqli->error) {
|
2013-11-10 17:23:00 +01:00
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$playerStatement->bind_param('i', $player->index);
|
|
|
|
$playerStatement->execute();
|
2014-01-28 17:12:23 +01:00
|
|
|
if ($playerStatement->error) {
|
2013-11-10 17:23:00 +01:00
|
|
|
trigger_error($playerStatement->error);
|
|
|
|
$playerStatement->close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$playerStatement->store_result();
|
2014-01-01 18:53:19 +01:00
|
|
|
$playerStatement->bind_result($player->authLevel);
|
2013-11-10 17:23:00 +01:00
|
|
|
$playerStatement->fetch();
|
|
|
|
$playerStatement->free_result();
|
|
|
|
$playerStatement->close();
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2013-11-10 17:23:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update total played time of the player
|
|
|
|
*
|
2013-12-31 15:27:25 +01:00
|
|
|
* @param Player $player
|
2013-11-10 17:23:00 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function updatePlayedTime(Player $player) {
|
2014-01-28 17:12:23 +01:00
|
|
|
if (!$player) {
|
2013-11-10 17:23:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$playedTime = time() - $player->joinTime;
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2014-01-06 16:14:49 +01:00
|
|
|
return $this->maniaControl->statisticManager->insertStat(self::STAT_SERVERTIME, $player, $this->maniaControl->server->index, $playedTime);
|
2013-11-10 17:23:00 +01:00
|
|
|
}
|
2013-11-12 15:48:25 +01:00
|
|
|
}
|