added playertime and joincount to the new stastiticmanager
This commit is contained in:
parent
0c8572b3b8
commit
d6ed139265
@ -5,11 +5,10 @@ namespace ManiaControl\Players;
|
||||
require_once __DIR__ . '/Player.php';
|
||||
require_once __DIR__ . '/PlayerCommands.php';
|
||||
|
||||
use FML\Controls\Quad;
|
||||
use ManiaControl\Formatter;
|
||||
use ManiaControl\ManiaControl;
|
||||
use ManiaControl\Callbacks\CallbackListener;
|
||||
use ManiaControl\Callbacks\CallbackManager;
|
||||
use ManiaControl\Formatter;
|
||||
use ManiaControl\ManiaControl;
|
||||
|
||||
/**
|
||||
* Class managing players
|
||||
@ -20,13 +19,16 @@ 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 = 'joinCount';
|
||||
const STAT_PLAYTIME = 'playTime';
|
||||
|
||||
/**
|
||||
* Private properties
|
||||
*/
|
||||
@ -49,7 +51,7 @@ class PlayerManager implements CallbackListener {
|
||||
$this->initTables();
|
||||
|
||||
$this->playerCommands = new PlayerCommands($maniaControl);
|
||||
$this->playerActions = new PlayerActions($maniaControl);
|
||||
$this->playerActions = new PlayerActions($maniaControl);
|
||||
|
||||
// Init settings
|
||||
$this->maniaControl->settingManager->initSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES, true);
|
||||
@ -57,10 +59,11 @@ class PlayerManager implements CallbackListener {
|
||||
// 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');
|
||||
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERDISCONNECT, $this, 'playerDisconnect');
|
||||
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERINFOCHANGED, $this, 'playerInfoChanged');
|
||||
|
||||
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_JOIN_COUNT);
|
||||
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_PLAYTIME);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,8 +72,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,
|
||||
@ -83,12 +86,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;
|
||||
}
|
||||
@ -105,13 +108,13 @@ class PlayerManager implements CallbackListener {
|
||||
// Add all players
|
||||
$this->maniaControl->client->query('GetPlayerList', 300, 0, 2);
|
||||
$playerList = $this->maniaControl->client->getResponse();
|
||||
foreach ($playerList as $playerItem) {
|
||||
if ($playerItem['PlayerId'] <= 0) {
|
||||
foreach($playerList 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);
|
||||
}
|
||||
|
||||
@ -128,20 +131,18 @@ 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
|
||||
@ -154,19 +155,20 @@ 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()) return;
|
||||
if($player == null || $player->isFakePlayer()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$played = Formatter::formatTimeH(time() - $player->joinTime);
|
||||
$this->maniaControl->log(
|
||||
"Player left: " . $player->login . " / " . Formatter::stripCodes($player->nickname) . " Playtime: " . $played);
|
||||
$this->maniaControl->log("Player left: " . $player->login . " / " . Formatter::stripCodes($player->nickname) . " Playtime: " . $played);
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
@ -178,11 +180,12 @@ class PlayerManager implements CallbackListener {
|
||||
*/
|
||||
public function playerInfoChanged(array $callback) {
|
||||
$player = $this->getPlayer($callback[1][0]['Login']);
|
||||
if ($player == null) return;
|
||||
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));
|
||||
@ -204,7 +207,7 @@ class PlayerManager implements CallbackListener {
|
||||
* @return Player
|
||||
*/
|
||||
public function getPlayer($login) {
|
||||
if (!isset($this->playerList[$login])) {
|
||||
if(!isset($this->playerList[$login])) {
|
||||
return null;
|
||||
}
|
||||
return $this->playerList[$login];
|
||||
@ -217,7 +220,7 @@ class PlayerManager implements CallbackListener {
|
||||
* @return bool
|
||||
*/
|
||||
private function addPlayer(Player $player) {
|
||||
if (!$player) {
|
||||
if(!$player) {
|
||||
return false;
|
||||
}
|
||||
$this->savePlayer($player);
|
||||
@ -229,16 +232,16 @@ class PlayerManager implements CallbackListener {
|
||||
* Remove a Player from the PlayerList
|
||||
*
|
||||
* @param string $login
|
||||
* @param bool $savePlayedTime
|
||||
* @param bool $savePlayedTime
|
||||
* @return Player $player
|
||||
*/
|
||||
private function removePlayer($login, $savePlayedTime = true) {
|
||||
if (!isset($this->playerList[$login])) {
|
||||
if(!isset($this->playerList[$login])) {
|
||||
return null;
|
||||
}
|
||||
$player = $this->playerList[$login];
|
||||
unset($this->playerList[$login]);
|
||||
if ($savePlayedTime) {
|
||||
if($savePlayedTime) {
|
||||
$this->updatePlayedTime($player);
|
||||
}
|
||||
return $player;
|
||||
@ -248,17 +251,20 @@ class PlayerManager implements CallbackListener {
|
||||
* Save player in database and fill up object properties
|
||||
*
|
||||
* @param Player $player
|
||||
* @param int $joinCount
|
||||
* @param int $joinCount
|
||||
* @return bool
|
||||
*/
|
||||
private function savePlayer(Player &$player, $joinCount = 1) {
|
||||
if (!$player) {
|
||||
if(!$player) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
$mysqli = $this->maniaControl->database->mysqli;
|
||||
|
||||
//TODO delete joincount from the following things
|
||||
// Save player
|
||||
$playerQuery = "INSERT INTO `" . self::TABLE_PLAYERS . "` (
|
||||
$playerQuery = "INSERT INTO `" . self::TABLE_PLAYERS . "` (
|
||||
`login`,
|
||||
`nickname`,
|
||||
`path`,
|
||||
@ -271,13 +277,13 @@ class PlayerManager implements CallbackListener {
|
||||
`path` = VALUES(`path`),
|
||||
`joinCount` = `joinCount` + VALUES(`joinCount`);";
|
||||
$playerStatement = $mysqli->prepare($playerQuery);
|
||||
if ($mysqli->error) {
|
||||
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) {
|
||||
if($playerStatement->error) {
|
||||
trigger_error($playerStatement->error);
|
||||
$playerStatement->close();
|
||||
return false;
|
||||
@ -286,16 +292,16 @@ class PlayerManager implements CallbackListener {
|
||||
$playerStatement->close();
|
||||
|
||||
// Fill up properties
|
||||
$playerQuery = "SELECT `authLevel`, `joinCount`, `totalPlayed` FROM `" . self::TABLE_PLAYERS . "`
|
||||
$playerQuery = "SELECT `authLevel`, `joinCount`, `totalPlayed` 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,6 +312,13 @@ class PlayerManager implements CallbackListener {
|
||||
$playerStatement->free_result();
|
||||
$playerStatement->close();
|
||||
|
||||
|
||||
//Increment the Player Join Count
|
||||
$success = $this->maniaControl->statisticManager->incrementStat(self::STAT_JOIN_COUNT, $player, $this->maniaControl->server->getLogin());
|
||||
|
||||
if(!$success)
|
||||
trigger_error("Error while setting the JoinCount");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -316,26 +329,11 @@ class PlayerManager implements CallbackListener {
|
||||
* @return bool
|
||||
*/
|
||||
private function updatePlayedTime(Player $player) {
|
||||
if (!$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;
|
||||
|
||||
return $this->maniaControl->statisticManager->insertStat(self::STAT_PLAYTIME, $player, $this->maniaControl->server->getLogin(), $playedTime);
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,14 @@
|
||||
<?php
|
||||
use ManiaControl\ManiaControl;
|
||||
use ManiaControl\Players\Player;
|
||||
|
||||
/**
|
||||
* Statistic Manager Class
|
||||
*
|
||||
* @author steeffeen & kremsy
|
||||
*/
|
||||
//TODO db reference between player index and statsitics playerId
|
||||
//TODO db reference between metadata statId and statistics statId
|
||||
class StatisticManager {
|
||||
/**
|
||||
* Constants
|
||||
@ -104,16 +107,20 @@ class StatisticManager {
|
||||
* Inserts a Stat into the database
|
||||
*
|
||||
* @param $statName
|
||||
* @param $playerId
|
||||
* @param Player $player
|
||||
* @param string $serverLogin
|
||||
* @param $value , value to Add
|
||||
* @param string $statType
|
||||
*/
|
||||
public function insertStat($statName, $playerId, $serverLogin, $value, $statType = self::STAT_TYPE_INT) {
|
||||
public function insertStat($statName, Player $player, $serverLogin, $value, $statType = self::STAT_TYPE_INT) {
|
||||
$statId = $this->getStatId($statName);
|
||||
|
||||
if($statId == null) {
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
if($player->isFakePlayer()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$query = "INSERT INTO `" . self::TABLE_STATISTICS . "` (
|
||||
@ -126,13 +133,12 @@ class StatisticManager {
|
||||
) ON DUPLICATE KEY UPDATE
|
||||
`value` = `value` + VALUES(`value`);";
|
||||
|
||||
var_dump($query);
|
||||
$statement = $this->mysqli->prepare($query);
|
||||
if($this->mysqli->error) {
|
||||
trigger_error($this->mysqli->error);
|
||||
return false;
|
||||
}
|
||||
$statement->bind_param('siii', $serverLogin, $playerId, $statId, $value);
|
||||
$statement->bind_param('siii', $serverLogin, $player->index, $statId, $value);
|
||||
$statement->execute();
|
||||
if($statement->error) {
|
||||
trigger_error($statement->error);
|
||||
@ -141,17 +147,19 @@ class StatisticManager {
|
||||
}
|
||||
|
||||
$statement->close();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments a Statistic by one
|
||||
*
|
||||
* @param $statName
|
||||
* @param $playerId
|
||||
* @param $serverLogin
|
||||
* @param $statName
|
||||
* @param Player $playerId
|
||||
* @param $serverLogin
|
||||
* @return bool
|
||||
*/
|
||||
public function incrementStat($statName, $playerId, $serverLogin) {
|
||||
$this->insertStat($statName, $playerId, $serverLogin, 1);
|
||||
public function incrementStat($statName, Player $player, $serverLogin) {
|
||||
return $this->insertStat($statName, $player, $serverLogin, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user