- updates for new server properties
- refactored various imports
This commit is contained in:
		| @@ -2,18 +2,20 @@ | ||||
|  | ||||
| namespace ManiaControl\Players; | ||||
|  | ||||
| require_once __DIR__ . '/Player.php'; | ||||
| require_once __DIR__ . '/PlayerCommands.php'; | ||||
| require_once __DIR__ . '/PlayerDetailed.php'; | ||||
|  | ||||
| use ManiaControl\Callbacks\CallbackListener; | ||||
| use ManiaControl\Callbacks\CallbackManager; | ||||
| use ManiaControl\Formatter; | ||||
| use ManiaControl\ManiaControl; | ||||
| use ManiaControl\Statistics\StatisticManager; | ||||
|  | ||||
| require_once __DIR__ . '/Player.php'; | ||||
| require_once __DIR__ . '/PlayerActions.php'; | ||||
| require_once __DIR__ . '/PlayerCommands.php'; | ||||
| require_once __DIR__ . '/PlayerDetailed.php'; | ||||
| require_once __DIR__ . '/PlayerList.php'; | ||||
|  | ||||
| /** | ||||
|  * Class managing players | ||||
|  * Class managing Players | ||||
|  * | ||||
|  * @author kremsy & steeffeen | ||||
|  */ | ||||
| @@ -21,23 +23,24 @@ class PlayerManager implements CallbackListener { | ||||
| 	/** | ||||
| 	 * Constants | ||||
| 	 */ | ||||
| 	const CB_PLAYERJOINED             = 'PlayerManagerCallback.PlayerJoined'; | ||||
| 	const CB_PLAYERDISCONNECTED       = 'PlayerManagerCallback.PlayerDisconnected'; | ||||
| 	const CB_ONINIT                   = 'PlayerManagerCallback.OnInit'; | ||||
| 	const CB_PLAYERINFOCHANGED        = 'PlayerManagerCallback.PlayerInfoChanged'; | ||||
| 	const TABLE_PLAYERS               = 'mc_players'; | ||||
| 	const CB_PLAYERJOINED = 'PlayerManagerCallback.PlayerJoined'; | ||||
| 	const CB_PLAYERDISCONNECTED = 'PlayerManagerCallback.PlayerDisconnected'; | ||||
| 	const CB_ONINIT = 'PlayerManagerCallback.OnInit'; | ||||
| 	const CB_PLAYERINFOCHANGED = 'PlayerManagerCallback.PlayerInfoChanged'; | ||||
| 	const TABLE_PLAYERS = 'mc_players'; | ||||
| 	const SETTING_JOIN_LEAVE_MESSAGES = 'Enable Join & Leave Messages'; | ||||
| 	const STAT_JOIN_COUNT             = 'Joins'; | ||||
| 	const STAT_SERVERTIME               = 'Servertime'; | ||||
|  | ||||
| 	const STAT_JOIN_COUNT = 'Joins'; | ||||
| 	const STAT_SERVERTIME = 'Servertime'; | ||||
| 	 | ||||
| 	/** | ||||
| 	 * Public Properties | ||||
| 	 */ | ||||
| 	public $playerActions = null; | ||||
| 	public $playerCommands = null; | ||||
| 	public $playerDetailed = null; | ||||
| 	public $playerList = array(); | ||||
|  | ||||
| 	public $playerList = null; | ||||
| 	public $players = array(); | ||||
| 	 | ||||
| 	/** | ||||
| 	 * Private Properties | ||||
| 	 */ | ||||
| @@ -51,20 +54,21 @@ class PlayerManager implements CallbackListener { | ||||
| 	public function __construct(ManiaControl $maniaControl) { | ||||
| 		$this->maniaControl = $maniaControl; | ||||
| 		$this->initTables(); | ||||
|  | ||||
| 		 | ||||
| 		$this->playerCommands = new PlayerCommands($maniaControl); | ||||
| 		$this->playerActions  = new PlayerActions($maniaControl); | ||||
| 		$this->playerDetailed  = new PlayerDetailed($maniaControl); | ||||
|  | ||||
| 		$this->playerActions = new PlayerActions($maniaControl); | ||||
| 		$this->playerDetailed = new PlayerDetailed($maniaControl); | ||||
| 		$this->playerList = new PlayerList($this->maniaControl); | ||||
| 		 | ||||
| 		// Init settings | ||||
| 		$this->maniaControl->settingManager->initSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES, true); | ||||
|  | ||||
| 		 | ||||
| 		// Register for callbacks | ||||
| 		$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'); | ||||
| 		$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERINFOCHANGED, $this, 'playerInfoChanged'); | ||||
|  | ||||
| 		 | ||||
| 		// Define player stats | ||||
| 		$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_JOIN_COUNT); | ||||
| 		$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_SERVERTIME, StatisticManager::STAT_TYPE_TIME); | ||||
| @@ -76,8 +80,8 @@ class PlayerManager implements CallbackListener { | ||||
| 	 * @return bool | ||||
| 	 */ | ||||
| 	private function initTables() { | ||||
| 		$mysqli               = $this->maniaControl->database->mysqli; | ||||
| 		$playerTableQuery     = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_PLAYERS . "` ( | ||||
| 		$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, | ||||
| 				`nickname` varchar(150) COLLATE utf8_unicode_ci NOT NULL, | ||||
| @@ -88,12 +92,12 @@ class PlayerManager implements CallbackListener { | ||||
| 				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) { | ||||
| 		if ($mysqli->error) { | ||||
| 			trigger_error($mysqli->error, E_USER_ERROR); | ||||
| 			return false; | ||||
| 		} | ||||
| 		$playerTableStatement->execute(); | ||||
| 		if($playerTableStatement->error) { | ||||
| 		if ($playerTableStatement->error) { | ||||
| 			trigger_error($playerTableStatement->error, E_USER_ERROR); | ||||
| 			return false; | ||||
| 		} | ||||
| @@ -109,17 +113,17 @@ class PlayerManager implements CallbackListener { | ||||
| 	public function onInit(array $callback) { | ||||
| 		// Add all players | ||||
| 		$this->maniaControl->client->query('GetPlayerList', 300, 0, 2); | ||||
| 		$playerList = $this->maniaControl->client->getResponse(); | ||||
| 		foreach($playerList as $playerItem) { | ||||
| 			if($playerItem['PlayerId'] <= 0) { | ||||
| 		$players = $this->maniaControl->client->getResponse(); | ||||
| 		foreach ($players as $playerItem) { | ||||
| 			if ($playerItem['PlayerId'] <= 0) { | ||||
| 				continue; | ||||
| 			} | ||||
| 			$this->maniaControl->client->query('GetDetailedPlayerInfo', $playerItem['Login']); | ||||
| 			$playerInfo = $this->maniaControl->client->getResponse(); | ||||
| 			$player     = new Player($playerInfo); | ||||
| 			$player = new Player($playerInfo); | ||||
| 			$this->addPlayer($player); | ||||
| 		} | ||||
|  | ||||
| 		 | ||||
| 		// Trigger own callback | ||||
| 		$this->maniaControl->callbackManager->triggerCallback(self::CB_ONINIT, array(self::CB_ONINIT)); | ||||
| 	} | ||||
| @@ -133,20 +137,22 @@ class PlayerManager implements CallbackListener { | ||||
| 		$login = $callback[1][0]; | ||||
| 		$this->maniaControl->client->query('GetDetailedPlayerInfo', $login); | ||||
| 		$playerInfo = $this->maniaControl->client->getResponse(); | ||||
| 		$player     = new Player($playerInfo); | ||||
|  | ||||
| 		$player = new Player($playerInfo); | ||||
| 		 | ||||
| 		$this->addPlayer($player); | ||||
|  | ||||
| 		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'); | ||||
| 			$chatMessage = '$s$0f0' . $string[$player->authLevel] . ' $fff' . $player->nickname . '$z$s$0f0 Nation:$fff ' . $player->getCountry() . ' $z$s$0f0joined!'; | ||||
| 		 | ||||
| 		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'); | ||||
| 			$chatMessage = '$s$0f0' . $string[$player->authLevel] . ' $fff' . $player->nickname . '$z$s$0f0 Nation:$fff ' . $player->getCountry() . | ||||
| 					 ' $z$s$0f0joined!'; | ||||
| 			$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}"; | ||||
| 		 | ||||
| 		$logMessage = "Player joined: {$player->login} / " . Formatter::stripCodes($player->nickname) . " Nation: " . $player->getCountry() . | ||||
| 				 " IP: {$player->ipAddress}"; | ||||
| 		$this->maniaControl->log($logMessage); | ||||
|  | ||||
| 		 | ||||
| 		// Trigger own PlayerJoined callback | ||||
| 		$this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERJOINED, array(self::CB_PLAYERJOINED, $player)); | ||||
| 	} | ||||
| @@ -157,21 +163,21 @@ class PlayerManager implements CallbackListener { | ||||
| 	 * @param array $callback | ||||
| 	 */ | ||||
| 	public function playerDisconnect(array $callback) { | ||||
| 		$login  = $callback[1][0]; | ||||
| 		$login = $callback[1][0]; | ||||
| 		$player = $this->removePlayer($login); | ||||
|  | ||||
| 		 | ||||
| 		// Trigger own callback | ||||
| 		$this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERDISCONNECTED, array(self::CB_PLAYERDISCONNECTED, $player)); | ||||
|  | ||||
| 		if($player == null || $player->isFakePlayer()) { | ||||
| 		 | ||||
| 		if ($player == null || $player->isFakePlayer()) { | ||||
| 			return; | ||||
| 		} | ||||
|  | ||||
| 		$played     = Formatter::formatTimeH(time() - $player->joinTime); | ||||
| 		 | ||||
| 		$played = Formatter::formatTimeH(time() - $player->joinTime); | ||||
| 		$logMessage = "Player left: {$player->login} / {$player->nickname} Playtime: {$played}"; | ||||
| 		$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'); | ||||
| 		} | ||||
| 	} | ||||
| @@ -183,86 +189,80 @@ class PlayerManager implements CallbackListener { | ||||
| 	 */ | ||||
| 	public function playerInfoChanged(array $callback) { | ||||
| 		$player = $this->getPlayer($callback[1][0]['Login']); | ||||
| 		if($player == null) { | ||||
| 		if ($player == null) { | ||||
| 			return; | ||||
| 		} | ||||
|  | ||||
| 		$player->teamId      = $callback[1][0]["TeamId"]; | ||||
| 		 | ||||
| 		$player->teamId = $callback[1][0]["TeamId"]; | ||||
| 		$player->isSpectator = $callback[1][0]["SpectatorStatus"]; | ||||
| 		$player->ladderRank  = $callback[1][0]["LadderRanking"]; | ||||
|  | ||||
| 		$player->ladderRank = $callback[1][0]["LadderRanking"]; | ||||
| 		 | ||||
| 		// Trigger own callback | ||||
| 		$this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERINFOCHANGED, array(self::CB_PLAYERINFOCHANGED)); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Get the complete PlayerList | ||||
| 	 * Get all Players | ||||
| 	 * | ||||
| 	 * @return array | ||||
| 	 */ | ||||
| 	public function getPlayers() { | ||||
| 		return $this->playerList; | ||||
| 		return $this->players; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Get a Player from the PlayerList | ||||
| 	 * Get a Player by Login | ||||
| 	 * | ||||
| 	 * @param string $login | ||||
| 	 * @return Player | ||||
| 	 */ | ||||
| 	public function getPlayer($login) { | ||||
| 		if(!isset($this->playerList[$login])) { | ||||
| 		if (!isset($this->players[$login])) { | ||||
| 			return null; | ||||
| 		} | ||||
| 		return $this->playerList[$login]; | ||||
| 		return $this->players[$login]; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Add a player to the PlayerList | ||||
| 	 * Add a player | ||||
| 	 * | ||||
| 	 * @param Player $player | ||||
| 	 * @return bool | ||||
| 	 */ | ||||
| 	private function addPlayer(Player $player) { | ||||
| 		if(!$player) { | ||||
| 			return false; | ||||
| 		} | ||||
| 		$this->savePlayer($player); | ||||
| 		$this->playerList[$player->login] = $player; | ||||
| 		$this->players[$player->login] = $player; | ||||
| 		return true; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Remove a Player from the PlayerList | ||||
| 	 * Remove a Player | ||||
| 	 * | ||||
| 	 * @param string $login | ||||
| 	 * @param bool   $savePlayedTime | ||||
| 	 * @param bool $savePlayedTime | ||||
| 	 * @return Player $player | ||||
| 	 */ | ||||
| 	private function removePlayer($login, $savePlayedTime = true) { | ||||
| 		if(!isset($this->playerList[$login])) { | ||||
| 			return null; | ||||
| 		} | ||||
| 		$player = $this->playerList[$login]; | ||||
| 		unset($this->playerList[$login]); | ||||
| 		if($savePlayedTime) { | ||||
| 		if (!isset($this->players[$login])) return null; | ||||
| 		$player = $this->players[$login]; | ||||
| 		unset($this->players[$login]); | ||||
| 		if ($savePlayedTime) { | ||||
| 			$this->updatePlayedTime($player); | ||||
| 		} | ||||
| 		return $player; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Save player in database and fill up object properties | ||||
| 	 * Save player in Database and fill up Object Properties | ||||
| 	 * | ||||
| 	 * @param Player $player | ||||
| 	 * @internal param int $joinCount | ||||
| 	 * @return bool | ||||
| 	 */ | ||||
| 	private function savePlayer(Player &$player) { | ||||
| 		$mysqli = $this->maniaControl->database->mysqli; | ||||
|  | ||||
| 		 | ||||
| 		// Save player | ||||
| 		$playerQuery     = "INSERT INTO `" . self::TABLE_PLAYERS . "` ( | ||||
| 		$playerQuery = "INSERT INTO `" . self::TABLE_PLAYERS . "` ( | ||||
| 				`login`, | ||||
| 				`nickname`, | ||||
| 				`path` | ||||
| @@ -273,30 +273,30 @@ class PlayerManager implements CallbackListener { | ||||
| 				`nickname` = VALUES(`nickname`), | ||||
| 				`path` = VALUES(`path`);"; | ||||
| 		$playerStatement = $mysqli->prepare($playerQuery); | ||||
| 		if($mysqli->error) { | ||||
| 		if ($mysqli->error) { | ||||
| 			trigger_error($mysqli->error); | ||||
| 			return false; | ||||
| 		} | ||||
| 		$playerStatement->bind_param('sss', $player->login, $player->nickname, $player->path); | ||||
| 		$playerStatement->execute(); | ||||
| 		if($playerStatement->error) { | ||||
| 		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` = ?;"; | ||||
| 		$playerQuery = "SELECT `authLevel` FROM `" . self::TABLE_PLAYERS . "` WHERE `index` = ?;"; | ||||
| 		$playerStatement = $mysqli->prepare($playerQuery); | ||||
| 		if($mysqli->error) { | ||||
| 		if ($mysqli->error) { | ||||
| 			trigger_error($mysqli->error); | ||||
| 			return false; | ||||
| 		} | ||||
| 		$playerStatement->bind_param('i', $player->index); | ||||
| 		$playerStatement->execute(); | ||||
| 		if($playerStatement->error) { | ||||
| 		if ($playerStatement->error) { | ||||
| 			trigger_error($playerStatement->error); | ||||
| 			$playerStatement->close(); | ||||
| 			return false; | ||||
| @@ -306,10 +306,10 @@ class PlayerManager implements CallbackListener { | ||||
| 		$playerStatement->fetch(); | ||||
| 		$playerStatement->free_result(); | ||||
| 		$playerStatement->close(); | ||||
|  | ||||
| 		 | ||||
| 		// Increment the Player Join Count | ||||
| 		$this->maniaControl->statisticManager->incrementStat(self::STAT_JOIN_COUNT, $player, $this->maniaControl->server->index); | ||||
|  | ||||
| 		 | ||||
| 		return true; | ||||
| 	} | ||||
|  | ||||
| @@ -320,12 +320,11 @@ class PlayerManager implements CallbackListener { | ||||
| 	 * @return bool | ||||
| 	 */ | ||||
| 	private function updatePlayedTime(Player $player) { | ||||
| 		if(!$player) { | ||||
| 		if (!$player) { | ||||
| 			return false; | ||||
| 		} | ||||
| 		$playedTime = time() - $player->joinTime; | ||||
|  | ||||
| 		$serverIndex = $this->maniaControl->server->getIndex(); | ||||
| 		return $this->maniaControl->statisticManager->insertStat(self::STAT_SERVERTIME, $player, $serverIndex, $playedTime); | ||||
| 		 | ||||
| 		return $this->maniaControl->statisticManager->insertStat(self::STAT_SERVERTIME, $player, $this->maniaControl->server->index, $playedTime); | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user