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-05-24 16:39:12 +02:00
|
|
|
use ManiaControl\Callbacks\Callbacks;
|
2015-06-19 01:37:31 +02:00
|
|
|
use ManiaControl\Callbacks\EchoListener;
|
2014-03-02 11:25:47 +01:00
|
|
|
use ManiaControl\Callbacks\TimerListener;
|
2014-08-05 01:49:13 +02:00
|
|
|
use ManiaControl\Logger;
|
2013-12-31 20:10:40 +01:00
|
|
|
use ManiaControl\ManiaControl;
|
2014-01-03 19:14:07 +01:00
|
|
|
use ManiaControl\Statistics\StatisticManager;
|
2014-05-13 17:59:37 +02:00
|
|
|
use ManiaControl\Utils\Formatter;
|
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException;
|
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-05-02 17:50:30 +02:00
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2015-01-19 11:06:20 +01:00
|
|
|
* @copyright 2014-2015 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
|
|
|
*/
|
2015-06-19 01:37:31 +02:00
|
|
|
class PlayerManager implements CallbackListener, TimerListener, EchoListener {
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2013-11-10 02:55:08 +01:00
|
|
|
* Constants
|
|
|
|
*/
|
2014-08-14 18:55:45 +02:00
|
|
|
const CB_PLAYERCONNECT = 'PlayerManagerCallback.PlayerConnect';
|
|
|
|
const CB_PLAYERDISCONNECT = 'PlayerManagerCallback.PlayerDisconnect';
|
|
|
|
const CB_PLAYERINFOCHANGED = 'PlayerManagerCallback.PlayerInfoChanged';
|
|
|
|
const CB_SERVER_EMPTY = 'PlayerManagerCallback.ServerEmpty';
|
|
|
|
const TABLE_PLAYERS = 'mc_players';
|
|
|
|
const SETTING_JOIN_LEAVE_MESSAGES = 'Enable Join & Leave Messages';
|
|
|
|
const SETTING_JOIN_LEAVE_MESSAGES_SPECTATOR = 'Enable Join & Leave Messages for Spectators';
|
|
|
|
const STAT_JOIN_COUNT = 'Joins';
|
|
|
|
const STAT_SERVERTIME = 'Servertime';
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2015-06-19 01:37:31 +02:00
|
|
|
const ECHO_WARN_PLAYER = 'ManiaControl.PlayerManager.WarnPlayer';
|
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-08-02 22:31:46 +02:00
|
|
|
* Public properties
|
2013-11-10 02:55:08 +01:00
|
|
|
*/
|
2014-10-24 20:08:21 +02:00
|
|
|
/** @var PlayerActions $playerActions
|
|
|
|
* @deprecated
|
|
|
|
* @see getPlayerActions()
|
|
|
|
*/
|
2014-07-19 23:40:16 +02:00
|
|
|
public $playerActions = null;
|
2014-10-24 20:08:21 +02:00
|
|
|
/** @var PlayerCommands $playerCommands
|
|
|
|
* @deprecated
|
|
|
|
* @see getPlayerCommands()
|
|
|
|
*/
|
2014-01-01 18:53:19 +01:00
|
|
|
public $playerCommands = null;
|
2014-10-24 20:08:21 +02:00
|
|
|
/** @var PlayerDetailed $playerDetailed
|
|
|
|
* @deprecated
|
|
|
|
* @see getPlayerDetailed()
|
|
|
|
*/
|
2014-01-03 16:24:35 +01:00
|
|
|
public $playerDetailed = null;
|
2014-10-24 20:08:21 +02:00
|
|
|
/** @var PlayerDataManager $playerDataManager
|
|
|
|
* @deprecated
|
|
|
|
* @see getPlayerDataManager()
|
|
|
|
*/
|
2014-08-02 22:31:46 +02:00
|
|
|
public $playerDataManager = null;
|
2014-10-24 20:08:21 +02:00
|
|
|
/** @var PlayerList $playerList
|
|
|
|
* @deprecated
|
|
|
|
* @see getPlayerList()
|
|
|
|
*/
|
2014-01-06 16:14:49 +01:00
|
|
|
public $playerList = null;
|
2014-10-24 20:08:21 +02:00
|
|
|
/** @var AdminLists $adminLists
|
|
|
|
* @deprecated
|
|
|
|
* @see getAdminLists()
|
|
|
|
*/
|
2014-01-31 13:40:07 +01:00
|
|
|
public $adminLists = null;
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-08-02 22:31:46 +02:00
|
|
|
* Private properties
|
2013-12-28 19:48:06 +01:00
|
|
|
*/
|
2014-08-02 22:31:46 +02:00
|
|
|
/** @var ManiaControl $maniaControl */
|
2014-01-01 18:53:19 +01:00
|
|
|
private $maniaControl = null;
|
2014-07-17 20:01:31 +02:00
|
|
|
/** @var Player[] $players */
|
|
|
|
private $players = array();
|
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
|
|
|
*
|
2014-05-04 16:45:59 +02:00
|
|
|
* @param 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);
|
2014-07-19 23:40:16 +02:00
|
|
|
$this->playerActions = new PlayerActions($maniaControl);
|
2014-04-21 02:01:30 +02:00
|
|
|
$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
|
|
|
|
2014-08-03 01:34:18 +02:00
|
|
|
// Settings
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES, true);
|
2014-08-14 18:55:45 +02:00
|
|
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES_SPECTATOR, true);
|
2014-08-03 01:34:18 +02:00
|
|
|
|
|
|
|
// Callbacks
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONINIT, $this, 'onInit');
|
|
|
|
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERCONNECT, $this, 'playerConnect');
|
|
|
|
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERDISCONNECT, $this, 'playerDisconnect');
|
|
|
|
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERINFOCHANGED, $this, 'playerInfoChanged');
|
2014-08-03 01:34:18 +02:00
|
|
|
|
|
|
|
// Player stats
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_JOIN_COUNT);
|
|
|
|
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_SERVERTIME, StatisticManager::STAT_TYPE_TIME);
|
2015-06-19 01:37:31 +02:00
|
|
|
|
|
|
|
// Echo Warn Command (Usage: sendEcho json_encode("player" => "loginName")
|
|
|
|
$this->maniaControl->getEchoManager()->registerEchoListener(self::ECHO_WARN_PLAYER, $this, function ($params) {
|
2015-06-19 18:30:44 +02:00
|
|
|
$this->playerActions->warnPlayer(null, $params->player, false);
|
2015-06-21 20:45:48 +02:00
|
|
|
|
|
|
|
return "abcdef";
|
2015-06-19 01:37:31 +02:00
|
|
|
});
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|
2013-11-09 23:01:54 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
2014-08-03 01:34:18 +02:00
|
|
|
* Initialize necessary database tables
|
2013-11-10 02:55:08 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function initTables() {
|
2014-08-13 11:05:52 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-01-16 00:24:24 +01:00
|
|
|
$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);
|
2014-05-04 16:45:59 +02:00
|
|
|
$playerTableStatement->close();
|
2013-11-10 02:55:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$playerTableStatement->close();
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-09 20:18:49 +01:00
|
|
|
|
2014-08-02 22:31:46 +02:00
|
|
|
/**
|
2014-08-03 01:34:18 +02:00
|
|
|
* Return the player actions
|
2014-08-02 22:31:46 +02:00
|
|
|
*
|
|
|
|
* @return PlayerActions
|
|
|
|
*/
|
|
|
|
public function getPlayerActions() {
|
|
|
|
return $this->playerActions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-03 01:34:18 +02:00
|
|
|
* Return the player commands
|
2014-08-02 22:31:46 +02:00
|
|
|
*
|
|
|
|
* @return PlayerCommands
|
|
|
|
*/
|
|
|
|
public function getPlayerCommands() {
|
|
|
|
return $this->playerCommands;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-03 01:34:18 +02:00
|
|
|
* Return the player detailed
|
2014-08-02 22:31:46 +02:00
|
|
|
*
|
|
|
|
* @return PlayerDetailed
|
|
|
|
*/
|
|
|
|
public function getPlayerDetailed() {
|
|
|
|
return $this->playerDetailed;
|
|
|
|
}
|
|
|
|
|
2014-08-03 01:34:18 +02:00
|
|
|
/**
|
|
|
|
* Return the player list
|
2014-08-02 22:31:46 +02:00
|
|
|
*
|
|
|
|
* @return PlayerList
|
|
|
|
*/
|
|
|
|
public function getPlayerList() {
|
|
|
|
return $this->playerList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-03 01:34:18 +02:00
|
|
|
* Return the admin lists
|
2014-08-02 22:31:46 +02:00
|
|
|
*
|
|
|
|
* @return AdminLists
|
|
|
|
*/
|
|
|
|
public function getAdminLists() {
|
|
|
|
return $this->adminLists;
|
|
|
|
}
|
|
|
|
|
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-08-13 11:05:52 +02:00
|
|
|
$players = $this->maniaControl->getClient()->getPlayerList(300, 0, 2);
|
2014-05-02 17:50:30 +02: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-06-14 18:07:18 +02:00
|
|
|
try {
|
2014-08-13 11:05:52 +02:00
|
|
|
$detailedPlayerInfo = $this->maniaControl->getClient()->getDetailedPlayerInfo($playerItem->login);
|
2014-06-14 18:07:18 +02:00
|
|
|
} catch (UnknownPlayerException $exception) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-01-25 11:40:34 +01:00
|
|
|
|
2014-06-14 18:07:18 +02:00
|
|
|
// Check if the Player is in a Team, to notify if its a TeamMode or not
|
2014-06-14 14:32:29 +02:00
|
|
|
if ($playerItem->teamId >= 0) {
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getServer()->setTeamMode(true);
|
2014-02-01 19:06:21 +01:00
|
|
|
}
|
|
|
|
|
2014-05-09 17:30:43 +02:00
|
|
|
$player = new Player($this->maniaControl, true);
|
2014-05-03 19:53:34 +02:00
|
|
|
$player->setInfo($playerItem);
|
|
|
|
$player->setDetailedInfo($detailedPlayerInfo);
|
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
|
|
|
|
2014-05-02 17:50:30 +02:00
|
|
|
/**
|
|
|
|
* Add a player
|
|
|
|
*
|
|
|
|
* @param Player $player
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function addPlayer(Player $player) {
|
|
|
|
$this->savePlayer($player);
|
|
|
|
$this->players[$player->login] = $player;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-02 22:31:46 +02:00
|
|
|
* Save player in database and fill up properties
|
2014-05-02 17:50:30 +02:00
|
|
|
*
|
|
|
|
* @param Player $player
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function savePlayer(Player &$player) {
|
2014-08-13 11:05:52 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-05-02 17:50:30 +02:00
|
|
|
|
|
|
|
// Save player
|
|
|
|
$playerQuery = "INSERT INTO `" . self::TABLE_PLAYERS . "` (
|
|
|
|
`login`,
|
|
|
|
`nickname`,
|
|
|
|
`path`
|
|
|
|
) VALUES (
|
|
|
|
?, ?, ?
|
|
|
|
) ON DUPLICATE KEY UPDATE
|
|
|
|
`index` = LAST_INSERT_ID(`index`),
|
|
|
|
`nickname` = VALUES(`nickname`),
|
|
|
|
`path` = VALUES(`path`);";
|
|
|
|
$playerStatement = $mysqli->prepare($playerQuery);
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$playerStatement->bind_param('sss', $player->login, $player->rawNickname, $player->path);
|
|
|
|
$playerStatement->execute();
|
|
|
|
if ($playerStatement->error) {
|
|
|
|
trigger_error($playerStatement->error);
|
|
|
|
$playerStatement->close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$player->index = $playerStatement->insert_id;
|
|
|
|
$playerStatement->close();
|
|
|
|
|
|
|
|
// Get Player Auth Level from DB
|
|
|
|
$playerQuery = "SELECT `authLevel` 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->authLevel);
|
|
|
|
$playerStatement->fetch();
|
|
|
|
$playerStatement->free_result();
|
|
|
|
$playerStatement->close();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
2014-05-04 16:45:59 +02:00
|
|
|
* Handle PlayerConnect Callback
|
2013-11-10 02:55:08 +01:00
|
|
|
*
|
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 {
|
2014-08-13 11:05:52 +02:00
|
|
|
$playerInfo = $this->maniaControl->getClient()->getDetailedPlayerInfo($login);
|
2014-05-09 17:30:43 +02:00
|
|
|
$player = new Player($this->maniaControl, true);
|
2014-05-03 19:53:34 +02:00
|
|
|
$player->setDetailedInfo($playerInfo);
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2014-03-13 18:25:29 +01:00
|
|
|
$this->addPlayer($player);
|
2014-05-13 17:59:37 +02:00
|
|
|
} catch (UnknownPlayerException $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
|
|
|
/**
|
2014-05-04 16:45:59 +02:00
|
|
|
* Handle PlayerDisconnect callback
|
2013-11-10 02:55:08 +01:00
|
|
|
*
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-04 16:45:59 +02:00
|
|
|
// Trigger own callbacks
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_PLAYERDISCONNECT, $player);
|
2014-05-04 16:45:59 +02:00
|
|
|
if ($this->getPlayerCount(false) <= 0) {
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_SERVER_EMPTY);
|
2014-05-04 16:45:59 +02:00
|
|
|
}
|
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}";
|
2014-08-05 02:08:41 +02:00
|
|
|
Logger::logInfo($logMessage, true);
|
2014-01-16 00:24:24 +01:00
|
|
|
|
2014-08-14 18:55:45 +02:00
|
|
|
if (!$player->isSpectator && $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_JOIN_LEAVE_MESSAGES) && !$player->isFakePlayer()
|
|
|
|
|| $player->isSpectator && $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_JOIN_LEAVE_MESSAGES_SPECTATOR)
|
2014-08-05 02:17:41 +02:00
|
|
|
) {
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getChat()->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
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->getPlayerDataManager()->destroyPlayerData($player);
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|
2013-11-09 22:08:06 +01:00
|
|
|
|
2014-05-02 17:50:30 +02:00
|
|
|
/**
|
|
|
|
* Remove a Player
|
|
|
|
*
|
|
|
|
* @param string $login
|
|
|
|
* @param bool $savePlayedTime
|
|
|
|
* @return Player $player
|
|
|
|
*/
|
|
|
|
private function removePlayer($login, $savePlayedTime = true) {
|
|
|
|
if (!isset($this->players[$login])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$player = $this->players[$login];
|
|
|
|
unset($this->players[$login]);
|
|
|
|
if ($savePlayedTime) {
|
|
|
|
$this->updatePlayedTime($player);
|
|
|
|
}
|
|
|
|
return $player;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2014-08-13 11:05:52 +02:00
|
|
|
return $this->maniaControl->getStatisticManager()->insertStat(self::STAT_SERVERTIME, $player, $this->maniaControl->getServer()->index, $playedTime);
|
2014-05-02 17:50:30 +02:00
|
|
|
}
|
|
|
|
|
2014-05-04 16:45:59 +02:00
|
|
|
/**
|
2014-08-02 22:31:46 +02:00
|
|
|
* Get the count of all Players
|
2014-05-04 16:45:59 +02:00
|
|
|
*
|
|
|
|
* @param bool $withoutSpectators
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getPlayerCount($withoutSpectators = true) {
|
2014-05-27 22:44:22 +02:00
|
|
|
if (!$withoutSpectators) {
|
|
|
|
return count($this->players);
|
|
|
|
}
|
2014-05-04 16:45:59 +02:00
|
|
|
$count = 0;
|
|
|
|
foreach ($this->players as $player) {
|
2014-05-27 22:44:22 +02:00
|
|
|
if (!$player->isSpectator) {
|
2014-05-04 16:45:59 +02:00
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
2014-10-24 20:08:21 +02:00
|
|
|
/**
|
|
|
|
* Return the player data manager
|
|
|
|
*
|
|
|
|
* @return PlayerDataManager
|
|
|
|
*/
|
|
|
|
public function getPlayerDataManager() {
|
|
|
|
return $this->playerDataManager;
|
|
|
|
}
|
|
|
|
|
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
|
2014-06-14 14:32:29 +02:00
|
|
|
if ($player->teamId >= 0) {
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getServer()->setTeamMode(true);
|
2014-02-01 19:06:21 +01:00
|
|
|
}
|
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) {
|
|
|
|
|
2014-08-14 18:55:45 +02:00
|
|
|
if (!$player->isSpectator && $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_JOIN_LEAVE_MESSAGES) && !$player->isFakePlayer()
|
2014-08-05 02:17:41 +02:00
|
|
|
) {
|
2014-03-02 14:18:10 +01:00
|
|
|
$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-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getChat()->sendChat($chatMessage);
|
2014-08-14 18:55:45 +02:00
|
|
|
} else if ($player->isSpectator && $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_JOIN_LEAVE_MESSAGES_SPECTATOR)) {
|
|
|
|
$string = array(0 => '$0f0Player', 1 => '$0f0Moderator', 2 => '$0f0Admin', 3 => '$0f0SuperAdmin', 4 => '$0f0MasterAdmin');
|
|
|
|
$chatMessage = '$0f0' . $string[$player->authLevel] . ' $<$fff' . $player->nickname . '$> Nation: $<$fff' . $player->getCountry() . '$> joined as Spectator!';
|
|
|
|
$this->maniaControl->getChat()->sendChat($chatMessage);
|
2014-03-02 14:18:10 +01:00
|
|
|
}
|
|
|
|
|
2014-08-14 18:55:45 +02:00
|
|
|
$this->maniaControl->getChat()->sendInformation('This server uses ManiaControl v' . ManiaControl::VERSION . '!', $player->login);
|
|
|
|
|
2014-08-05 01:49:13 +02:00
|
|
|
$logMessage = "Player joined: {$player->login} / {$player->nickname} Nation: " . $player->getCountry() . " IP: {$player->ipAddress}";
|
2014-08-05 02:08:41 +02:00
|
|
|
Logger::logInfo($logMessage, true);
|
2014-03-02 14:18:10 +01:00
|
|
|
|
|
|
|
// Increment the Player Join Count
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getStatisticManager()->incrementStat(self::STAT_JOIN_COUNT, $player, $this->maniaControl->getServer()->index);
|
2014-03-02 14:18:10 +01:00
|
|
|
|
|
|
|
// Trigger own PlayerJoined callback
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getCallbackManager()->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-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_PLAYERINFOCHANGED, $player);
|
2013-12-19 21:59:10 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 17:50:30 +02:00
|
|
|
/**
|
2014-08-02 22:31:46 +02:00
|
|
|
* Get a Player by login
|
2014-05-02 17:50:30 +02:00
|
|
|
*
|
2014-05-15 19:30:44 +02:00
|
|
|
* @param mixed $login
|
|
|
|
* @param bool $connectedPlayersOnly
|
2014-05-02 17:50:30 +02:00
|
|
|
* @return Player
|
|
|
|
*/
|
|
|
|
public function getPlayer($login, $connectedPlayersOnly = false) {
|
2014-05-15 19:30:44 +02:00
|
|
|
if ($login instanceof Player) {
|
|
|
|
return $login;
|
|
|
|
}
|
2014-05-02 17:50:30 +02:00
|
|
|
if (!isset($this->players[$login])) {
|
|
|
|
if ($connectedPlayersOnly) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->getPlayerFromDatabaseByLogin($login);
|
|
|
|
}
|
|
|
|
return $this->players[$login];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-02 22:31:46 +02:00
|
|
|
* Get a Player from the database
|
2014-05-02 17:50:30 +02:00
|
|
|
*
|
|
|
|
* @param string $playerLogin
|
|
|
|
* @return Player
|
|
|
|
*/
|
|
|
|
private function getPlayerFromDatabaseByLogin($playerLogin) {
|
2014-08-13 11:05:52 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-05-02 17:50:30 +02:00
|
|
|
|
2014-05-27 22:17:07 +02:00
|
|
|
$query = "SELECT * FROM `" . self::TABLE_PLAYERS . "`
|
|
|
|
WHERE `login` LIKE '" . $mysqli->escape_string($playerLogin) . "';";
|
2014-05-02 17:50:30 +02:00
|
|
|
$result = $mysqli->query($query);
|
|
|
|
if (!$result) {
|
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$row = $result->fetch_object();
|
2014-05-18 23:34:47 +02:00
|
|
|
$result->free();
|
2014-05-02 17:50:30 +02:00
|
|
|
|
|
|
|
if (!isset($row)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-05-09 17:30:43 +02:00
|
|
|
$player = new Player($this->maniaControl, false);
|
2014-05-02 17:50:30 +02: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;
|
|
|
|
|
|
|
|
return $player;
|
|
|
|
}
|
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
|
|
|
*
|
2014-05-27 22:44:22 +02:00
|
|
|
* @return Player[]
|
2013-11-28 01:12:52 +01:00
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
2014-08-02 22:31:46 +02:00
|
|
|
* Get 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-05-02 17:50:30 +02:00
|
|
|
foreach ($this->players as $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
|
|
|
/**
|
2014-08-02 22:31:46 +02:00
|
|
|
* Get a Player by index
|
2014-01-16 00:24:24 +01:00
|
|
|
*
|
2014-05-04 16:45:59 +02:00
|
|
|
* @param int $index
|
2014-04-20 19:59:01 +02:00
|
|
|
* @param bool $connectedPlayersOnly
|
2014-05-27 22:44:22 +02:00
|
|
|
* @return Player
|
2014-01-16 00:24:24 +01:00
|
|
|
*/
|
2014-04-20 19:59:01 +02:00
|
|
|
public function getPlayerByIndex($index, $connectedPlayersOnly = false) {
|
2014-05-02 17:50:30 +02:00
|
|
|
foreach ($this->players as $player) {
|
2014-06-14 14:32:29 +02:00
|
|
|
if ($player->index === $index) {
|
2014-01-16 00:24:24 +01:00
|
|
|
return $player;
|
|
|
|
}
|
|
|
|
}
|
2014-04-20 19:59:01 +02:00
|
|
|
|
2014-08-02 22:31:46 +02:00
|
|
|
// Player is not online - Get Player from Database
|
|
|
|
if (!$connectedPlayersOnly) {
|
|
|
|
return $this->getPlayerFromDatabaseByIndex($index);
|
2014-04-20 19:59:01 +02:00
|
|
|
}
|
2014-08-02 22:31:46 +02:00
|
|
|
|
|
|
|
return null;
|
2014-01-16 00:24:24 +01:00
|
|
|
}
|
|
|
|
|
2014-01-21 21:58:39 +01:00
|
|
|
/**
|
2014-05-04 16:45:59 +02:00
|
|
|
* Get a Player out of the database
|
2014-01-21 21:58:39 +01:00
|
|
|
*
|
2014-05-04 16:45:59 +02:00
|
|
|
* @param int $playerIndex
|
2014-08-02 22:31:46 +02:00
|
|
|
* @return Player
|
2014-01-21 21:58:39 +01:00
|
|
|
*/
|
|
|
|
private function getPlayerFromDatabaseByIndex($playerIndex) {
|
2014-01-28 17:12:23 +01:00
|
|
|
if (!is_numeric($playerIndex)) {
|
2014-01-21 21:58:39 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-08-13 11:05:52 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-08-02 22:31:46 +02:00
|
|
|
$query = "SELECT * FROM `" . self::TABLE_PLAYERS . "`
|
|
|
|
WHERE `index` = {$playerIndex};";
|
2014-01-21 21:58:39 +01:00
|
|
|
$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();
|
2014-05-18 23:34:47 +02:00
|
|
|
$result->free();
|
2014-01-21 21:58:39 +01:00
|
|
|
|
2014-08-02 22:31:46 +02:00
|
|
|
if (!$row) {
|
2014-01-31 14:19:39 +01:00
|
|
|
return null;
|
|
|
|
}
|
2014-02-01 19:06:21 +01:00
|
|
|
|
2014-05-09 17:30:43 +02:00
|
|
|
$player = new Player($this->maniaControl, false);
|
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;
|
|
|
|
}
|
2013-11-12 15:48:25 +01:00
|
|
|
}
|