2013-11-09 20:18:49 +01:00
|
|
|
<?php
|
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
namespace ManiaControl\Players;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/Player.php';
|
2013-12-05 00:30:10 +01:00
|
|
|
require_once __DIR__ . '/PlayerCommands.php';
|
2013-11-12 15:48:25 +01:00
|
|
|
|
2013-11-12 19:33:25 +01:00
|
|
|
use ManiaControl\Formatter;
|
2013-11-12 15:48:25 +01:00
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
2013-11-09 20:18:49 +01:00
|
|
|
|
|
|
|
/**
|
2013-11-10 02:55:08 +01:00
|
|
|
* Class managing players
|
|
|
|
*
|
2013-11-10 17:23:00 +01:00
|
|
|
* @author kremsy & steeffeen
|
2013-11-09 20:18:49 +01:00
|
|
|
*/
|
2013-11-12 15:48:25 +01:00
|
|
|
class PlayerManager implements CallbackListener {
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
2013-11-28 03:47:08 +01:00
|
|
|
const CB_PLAYERJOINED = 'PlayerManagerCallback.PlayerJoined';
|
|
|
|
const CB_ONINIT = 'PlayerManagerCallback.OnInit';
|
2013-11-10 02:55:08 +01:00
|
|
|
const TABLE_PLAYERS = 'mc_players';
|
2013-11-12 19:33:25 +01:00
|
|
|
const SETTING_JOIN_LEAVE_MESSAGES = 'Enable Join & Leave Messages';
|
2013-11-10 02:55:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Private properties
|
|
|
|
*/
|
|
|
|
private $maniaControl = null;
|
2013-12-05 00:30:10 +01:00
|
|
|
private $playerCommands = null;
|
2013-11-10 02:55:08 +01:00
|
|
|
private $playerList = array();
|
2013-11-09 23:01:54 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
2013-11-12 19:33:25 +01:00
|
|
|
* Construct player manager
|
2013-11-10 02:55:08 +01:00
|
|
|
*
|
2013-11-12 15:48: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();
|
|
|
|
|
2013-12-05 00:30:10 +01:00
|
|
|
$this->playerCommands = new PlayerCommands($maniaControl);
|
|
|
|
|
2013-11-28 03:47:08 +01:00
|
|
|
// Init settings
|
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES, false);
|
|
|
|
|
|
|
|
// Register for callbacks
|
2013-11-12 15:48:25 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'onInit');
|
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCONNECT, $this, 'playerConnect');
|
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERDISCONNECT, $this,
|
|
|
|
'playerDisconnect');
|
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() {
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
$playerTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_PLAYERS . "` (
|
|
|
|
`index` int(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
`login` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
|
2013-11-10 17:23:00 +01:00
|
|
|
`nickname` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
|
|
|
|
`path` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
|
2013-11-10 19:30:14 +01:00
|
|
|
`authLevel` int(11) NOT NULL DEFAULT '0',
|
|
|
|
`joinCount` int(11) NOT NULL DEFAULT '0',
|
2013-11-10 17:23:00 +01:00
|
|
|
`totalPlayed` int(11) NOT NULL DEFAULT '0' COMMENT 'Seconds',
|
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`)
|
|
|
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Player data' AUTO_INCREMENT=1;";
|
|
|
|
$playerTableStatement = $mysqli->prepare($playerTableQuery);
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$playerTableStatement->execute();
|
|
|
|
if ($playerTableStatement->error) {
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param array $callback
|
|
|
|
*/
|
|
|
|
public function onInit(array $callback) {
|
2013-11-12 19:33:25 +01:00
|
|
|
// Add all players
|
2013-11-10 02:55:08 +01:00
|
|
|
$this->maniaControl->client->query('GetPlayerList', 300, 0, 2);
|
|
|
|
$playerList = $this->maniaControl->client->getResponse();
|
2013-11-12 15:48:25 +01:00
|
|
|
foreach ($playerList as $playerItem) {
|
|
|
|
if ($playerItem['PlayerId'] <= 0) {
|
2013-11-10 17:23:00 +01:00
|
|
|
continue;
|
|
|
|
}
|
2013-11-12 15:48:25 +01:00
|
|
|
$this->maniaControl->client->query('GetDetailedPlayerInfo', $playerItem['Login']);
|
|
|
|
$playerInfo = $this->maniaControl->client->getResponse();
|
|
|
|
$player = new Player($playerInfo);
|
|
|
|
$this->addPlayer($player);
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|
2013-11-28 03:47:08 +01:00
|
|
|
|
|
|
|
// Trigger own callback
|
|
|
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_ONINIT, array(self::CB_ONINIT));
|
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
|
|
|
|
*
|
|
|
|
* @param array $callback
|
|
|
|
*/
|
|
|
|
public function playerConnect(array $callback) {
|
|
|
|
$login = $callback[1][0];
|
|
|
|
$this->maniaControl->client->query('GetDetailedPlayerInfo', $login);
|
|
|
|
$playerInfo = $this->maniaControl->client->getResponse();
|
|
|
|
$player = new Player($playerInfo);
|
|
|
|
$this->addPlayer($player);
|
2013-11-12 15:48:25 +01:00
|
|
|
|
2013-11-28 03:47:08 +01:00
|
|
|
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES)) {
|
|
|
|
// TODO: improve styling?
|
|
|
|
$string = array(0 => 'New Player', 1 => '$0f0Operator', 2 => '$0f0Admin', 3 => '$0f0MasterAdmin', 4 => '$0f0MasterAdmin');
|
|
|
|
$nickname = Formatter::stripCodes($player->nickname);
|
|
|
|
$this->maniaControl->chat->sendChat(
|
|
|
|
'$ff0' . $string[$player->authLevel] . ': ' . $nickname . '$z $ff0Nation:$fff ' . $player->getCountry() .
|
|
|
|
' $ff0Ladder: $fff' . $player->ladderRank);
|
2013-11-10 22:08:43 +01:00
|
|
|
}
|
2013-11-28 03:47:08 +01:00
|
|
|
|
|
|
|
// Trigger own callback
|
|
|
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERJOINED, array(self::CB_PLAYERJOINED, $player));
|
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
|
|
|
|
*
|
|
|
|
* @param array $callback
|
|
|
|
*/
|
|
|
|
public function playerDisconnect(array $callback) {
|
|
|
|
$login = $callback[1][0];
|
|
|
|
$player = $this->removePlayer($login);
|
2013-11-12 15:48:25 +01:00
|
|
|
|
2013-11-12 19:33:25 +01:00
|
|
|
if (!$this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES)) {
|
|
|
|
return;
|
2013-11-10 22:08:43 +01:00
|
|
|
}
|
2013-11-12 19:33:25 +01:00
|
|
|
$played = Formatter::formatTimeH(time() - $player->joinTime);
|
2013-11-19 20:29:37 +01:00
|
|
|
$this->maniaControl->chat->sendChat('$<' . $player->nickname . '$> $ff0has left the game. Played:$fff ' . $played);
|
2013-11-10 02:55:08 +01:00
|
|
|
}
|
2013-11-09 22:08:06 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
/**
|
|
|
|
* Get the complete PlayerList
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getPlayers() {
|
|
|
|
return $this->playerList;
|
|
|
|
}
|
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
|
|
|
* Get a Player from the PlayerList
|
|
|
|
*
|
|
|
|
* @param string $login
|
|
|
|
* @return Player
|
|
|
|
*/
|
|
|
|
public function getPlayer($login) {
|
|
|
|
if (!isset($this->playerList[$login])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->playerList[$login];
|
|
|
|
}
|
2013-11-09 22:08:06 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
|
|
|
* Add a player to the PlayerList
|
|
|
|
*
|
|
|
|
* @param Player $player
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function addPlayer(Player $player) {
|
|
|
|
if (!$player) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-10 17:23:00 +01:00
|
|
|
$this->savePlayer($player);
|
2013-11-10 02:55:08 +01:00
|
|
|
$this->playerList[$player->login] = $player;
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-09 22:08:06 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
/**
|
|
|
|
* Remove a Player from the PlayerList
|
|
|
|
*
|
|
|
|
* @param string $login
|
2013-11-10 17:23:00 +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) {
|
2013-11-10 02:55:08 +01:00
|
|
|
if (!isset($this->playerList[$login])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$player = $this->playerList[$login];
|
|
|
|
unset($this->playerList[$login]);
|
2013-11-10 17:23:00 +01:00
|
|
|
if ($savePlayedTime) {
|
|
|
|
$this->updatePlayedTime($player);
|
|
|
|
}
|
2013-11-10 02:55:08 +01:00
|
|
|
return $player;
|
|
|
|
}
|
2013-11-10 17:23:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2013-11-10 19:30:14 +01:00
|
|
|
$playerQuery = "SELECT `authLevel`, `joinCount`, `totalPlayed` FROM `" . self::TABLE_PLAYERS . "`
|
2013-11-10 17:23:00 +01:00
|
|
|
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();
|
2013-11-10 19:30:14 +01:00
|
|
|
$playerStatement->bind_result($player->authLevel, $player->joinCount, $player->totalPlayed);
|
2013-11-10 17:23:00 +01:00
|
|
|
$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;
|
|
|
|
}
|
2013-11-12 15:48:25 +01:00
|
|
|
}
|