added method to playermanager

This commit is contained in:
kremsy 2014-01-16 00:24:24 +01:00 committed by Steffen Schröder
parent 02a60ac875
commit d8d52251c9

View File

@ -23,14 +23,14 @@ class PlayerManager implements CallbackListener {
/** /**
* Constants * Constants
*/ */
const CB_PLAYERJOINED = 'PlayerManagerCallback.PlayerJoined'; const CB_PLAYERJOINED = 'PlayerManagerCallback.PlayerJoined';
const CB_PLAYERDISCONNECTED = 'PlayerManagerCallback.PlayerDisconnected'; const CB_PLAYERDISCONNECTED = 'PlayerManagerCallback.PlayerDisconnected';
const CB_ONINIT = 'PlayerManagerCallback.OnInit'; const CB_ONINIT = 'PlayerManagerCallback.OnInit';
const CB_PLAYERINFOCHANGED = 'PlayerManagerCallback.PlayerInfoChanged'; const CB_PLAYERINFOCHANGED = 'PlayerManagerCallback.PlayerInfoChanged';
const TABLE_PLAYERS = 'mc_players'; const TABLE_PLAYERS = 'mc_players';
const SETTING_JOIN_LEAVE_MESSAGES = 'Enable Join & Leave Messages'; const SETTING_JOIN_LEAVE_MESSAGES = 'Enable Join & Leave Messages';
const STAT_JOIN_COUNT = 'Joins'; const STAT_JOIN_COUNT = 'Joins';
const STAT_SERVERTIME = 'Servertime'; const STAT_SERVERTIME = 'Servertime';
/** /**
* Public Properties * Public Properties
@ -56,9 +56,9 @@ class PlayerManager implements CallbackListener {
$this->initTables(); $this->initTables();
$this->playerCommands = new PlayerCommands($maniaControl); $this->playerCommands = new PlayerCommands($maniaControl);
$this->playerActions = new PlayerActions($maniaControl); $this->playerActions = new PlayerActions($maniaControl);
$this->playerDetailed = new PlayerDetailed($maniaControl); $this->playerDetailed = new PlayerDetailed($maniaControl);
$this->playerList = new PlayerList($this->maniaControl); $this->playerList = new PlayerList($this->maniaControl);
// Init settings // Init settings
$this->maniaControl->settingManager->initSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES, true); $this->maniaControl->settingManager->initSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES, true);
@ -80,8 +80,8 @@ class PlayerManager implements CallbackListener {
* @return bool * @return bool
*/ */
private function initTables() { private function initTables() {
$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,
`login` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `login` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`nickname` varchar(150) COLLATE utf8_unicode_ci NOT NULL, `nickname` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
@ -92,12 +92,12 @@ class PlayerManager implements CallbackListener {
UNIQUE KEY `login` (`login`) UNIQUE KEY `login` (`login`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Player Data' AUTO_INCREMENT=1;"; ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Player Data' AUTO_INCREMENT=1;";
$playerTableStatement = $mysqli->prepare($playerTableQuery); $playerTableStatement = $mysqli->prepare($playerTableQuery);
if ($mysqli->error) { if($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR); trigger_error($mysqli->error, E_USER_ERROR);
return false; return false;
} }
$playerTableStatement->execute(); $playerTableStatement->execute();
if ($playerTableStatement->error) { if($playerTableStatement->error) {
trigger_error($playerTableStatement->error, E_USER_ERROR); trigger_error($playerTableStatement->error, E_USER_ERROR);
return false; return false;
} }
@ -114,13 +114,13 @@ class PlayerManager implements CallbackListener {
// Add all players // Add all players
$this->maniaControl->client->query('GetPlayerList', 300, 0, 2); $this->maniaControl->client->query('GetPlayerList', 300, 0, 2);
$players = $this->maniaControl->client->getResponse(); $players = $this->maniaControl->client->getResponse();
foreach ($players as $playerItem) { foreach($players as $playerItem) {
if ($playerItem['PlayerId'] <= 0) { if($playerItem['PlayerId'] <= 0) {
continue; continue;
} }
$this->maniaControl->client->query('GetDetailedPlayerInfo', $playerItem['Login']); $this->maniaControl->client->query('GetDetailedPlayerInfo', $playerItem['Login']);
$playerInfo = $this->maniaControl->client->getResponse(); $playerInfo = $this->maniaControl->client->getResponse();
$player = new Player($playerInfo); $player = new Player($playerInfo);
$this->addPlayer($player); $this->addPlayer($player);
} }
@ -137,20 +137,18 @@ class PlayerManager implements CallbackListener {
$login = $callback[1][0]; $login = $callback[1][0];
$this->maniaControl->client->query('GetDetailedPlayerInfo', $login); $this->maniaControl->client->query('GetDetailedPlayerInfo', $login);
$playerInfo = $this->maniaControl->client->getResponse(); $playerInfo = $this->maniaControl->client->getResponse();
$player = new Player($playerInfo); $player = new Player($playerInfo);
$this->addPlayer($player); $this->addPlayer($player);
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES) && !$player->isFakePlayer()) { if($this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES) && !$player->isFakePlayer()) {
$string = array(0 => '$0f0Player', 1 => '$0f0Moderator', 2 => '$0f0Admin', 3 => '$0f0MasterAdmin', 4 => '$0f0MasterAdmin'); $string = array(0 => '$0f0Player', 1 => '$0f0Moderator', 2 => '$0f0Admin', 3 => '$0f0MasterAdmin', 4 => '$0f0MasterAdmin');
$chatMessage = '$s$0f0' . $string[$player->authLevel] . ' $fff' . $player->nickname . '$z$s$0f0 Nation:$fff ' . $player->getCountry() . $chatMessage = '$s$0f0' . $string[$player->authLevel] . ' $fff' . $player->nickname . '$z$s$0f0 Nation:$fff ' . $player->getCountry() . ' $z$s$0f0joined!';
' $z$s$0f0joined!';
$this->maniaControl->chat->sendChat($chatMessage); $this->maniaControl->chat->sendChat($chatMessage);
$this->maniaControl->chat->sendInformation('This server uses ManiaControl v' . ManiaControl::VERSION . '!', $player->login); $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() . $logMessage = "Player joined: {$player->login} / " . Formatter::stripCodes($player->nickname) . " Nation: " . $player->getCountry() . " IP: {$player->ipAddress}";
" IP: {$player->ipAddress}";
$this->maniaControl->log($logMessage); $this->maniaControl->log($logMessage);
// Trigger own PlayerJoined callback // Trigger own PlayerJoined callback
@ -163,21 +161,21 @@ class PlayerManager implements CallbackListener {
* @param array $callback * @param array $callback
*/ */
public function playerDisconnect(array $callback) { public function playerDisconnect(array $callback) {
$login = $callback[1][0]; $login = $callback[1][0];
$player = $this->removePlayer($login); $player = $this->removePlayer($login);
// Trigger own callback // Trigger own callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERDISCONNECTED, array(self::CB_PLAYERDISCONNECTED, $player)); $this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERDISCONNECTED, array(self::CB_PLAYERDISCONNECTED, $player));
if ($player == null || $player->isFakePlayer()) { if($player == null || $player->isFakePlayer()) {
return; return;
} }
$played = Formatter::formatTimeH(time() - $player->joinTime); $played = Formatter::formatTimeH(time() - $player->joinTime);
$logMessage = "Player left: {$player->login} / {$player->nickname} Playtime: {$played}"; $logMessage = "Player left: {$player->login} / {$player->nickname} Playtime: {$played}";
$this->maniaControl->log(Formatter::stripCodes($logMessage)); $this->maniaControl->log(Formatter::stripCodes($logMessage));
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES)) { if($this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES)) {
$this->maniaControl->chat->sendChat('$<' . $player->nickname . '$> $s$0f0has left the game'); $this->maniaControl->chat->sendChat('$<' . $player->nickname . '$> $s$0f0has left the game');
} }
} }
@ -189,13 +187,13 @@ class PlayerManager implements CallbackListener {
*/ */
public function playerInfoChanged(array $callback) { public function playerInfoChanged(array $callback) {
$player = $this->getPlayer($callback[1][0]['Login']); $player = $this->getPlayer($callback[1][0]['Login']);
if ($player == null) { if($player == null) {
return; return;
} }
$player->teamId = $callback[1][0]["TeamId"]; $player->teamId = $callback[1][0]["TeamId"];
$player->isSpectator = $callback[1][0]["SpectatorStatus"]; $player->isSpectator = $callback[1][0]["SpectatorStatus"];
$player->ladderRank = $callback[1][0]["LadderRanking"]; $player->ladderRank = $callback[1][0]["LadderRanking"];
// Trigger own callback // Trigger own callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERINFOCHANGED, array(self::CB_PLAYERINFOCHANGED)); $this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERINFOCHANGED, array(self::CB_PLAYERINFOCHANGED));
@ -210,6 +208,24 @@ class PlayerManager implements CallbackListener {
return $this->players; return $this->players;
} }
/**
* Gets a Player by his index
*
* @param $index
* @return Player|null
*/
public function getPlayerByIndex($index) {
foreach($this->players as $player) {
/** @var Player $player */
if($player->index == $index) {
return $player;
} else {
return null;
}
}
}
/** /**
* Get a Player by Login * Get a Player by Login
* *
@ -217,7 +233,7 @@ class PlayerManager implements CallbackListener {
* @return \ManiaControl\Players\Player * @return \ManiaControl\Players\Player
*/ */
public function getPlayer($login) { public function getPlayer($login) {
if (!isset($this->players[$login])) { if(!isset($this->players[$login])) {
return null; return null;
} }
return $this->players[$login]; return $this->players[$login];
@ -239,14 +255,16 @@ class PlayerManager implements CallbackListener {
* Remove a Player * Remove a Player
* *
* @param string $login * @param string $login
* @param bool $savePlayedTime * @param bool $savePlayedTime
* @return Player $player * @return Player $player
*/ */
private function removePlayer($login, $savePlayedTime = true) { private function removePlayer($login, $savePlayedTime = true) {
if (!isset($this->players[$login])) return null; if(!isset($this->players[$login])) {
return null;
}
$player = $this->players[$login]; $player = $this->players[$login];
unset($this->players[$login]); unset($this->players[$login]);
if ($savePlayedTime) { if($savePlayedTime) {
$this->updatePlayedTime($player); $this->updatePlayedTime($player);
} }
return $player; return $player;
@ -262,7 +280,7 @@ class PlayerManager implements CallbackListener {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
// Save player // Save player
$playerQuery = "INSERT INTO `" . self::TABLE_PLAYERS . "` ( $playerQuery = "INSERT INTO `" . self::TABLE_PLAYERS . "` (
`login`, `login`,
`nickname`, `nickname`,
`path` `path`
@ -273,13 +291,13 @@ class PlayerManager implements CallbackListener {
`nickname` = VALUES(`nickname`), `nickname` = VALUES(`nickname`),
`path` = VALUES(`path`);"; `path` = VALUES(`path`);";
$playerStatement = $mysqli->prepare($playerQuery); $playerStatement = $mysqli->prepare($playerQuery);
if ($mysqli->error) { if($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
$playerStatement->bind_param('sss', $player->login, $player->nickname, $player->path); $playerStatement->bind_param('sss', $player->login, $player->nickname, $player->path);
$playerStatement->execute(); $playerStatement->execute();
if ($playerStatement->error) { if($playerStatement->error) {
trigger_error($playerStatement->error); trigger_error($playerStatement->error);
$playerStatement->close(); $playerStatement->close();
return false; return false;
@ -288,15 +306,15 @@ class PlayerManager implements CallbackListener {
$playerStatement->close(); $playerStatement->close();
// Get Player Auth Level from DB // Get Player Auth Level from DB
$playerQuery = "SELECT `authLevel` FROM `" . self::TABLE_PLAYERS . "` WHERE `index` = ?;"; $playerQuery = "SELECT `authLevel` FROM `" . self::TABLE_PLAYERS . "` WHERE `index` = ?;";
$playerStatement = $mysqli->prepare($playerQuery); $playerStatement = $mysqli->prepare($playerQuery);
if ($mysqli->error) { if($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
$playerStatement->bind_param('i', $player->index); $playerStatement->bind_param('i', $player->index);
$playerStatement->execute(); $playerStatement->execute();
if ($playerStatement->error) { if($playerStatement->error) {
trigger_error($playerStatement->error); trigger_error($playerStatement->error);
$playerStatement->close(); $playerStatement->close();
return false; return false;
@ -320,7 +338,7 @@ class PlayerManager implements CallbackListener {
* @return bool * @return bool
*/ */
private function updatePlayedTime(Player $player) { private function updatePlayedTime(Player $player) {
if (!$player) { if(!$player) {
return false; return false;
} }
$playedTime = time() - $player->joinTime; $playedTime = time() - $player->joinTime;