updated and improved statistic collector

This commit is contained in:
kremsy 2017-04-13 19:24:01 +02:00
parent fce74ed928
commit a2542de916
2 changed files with 101 additions and 3 deletions

View File

@ -14,6 +14,7 @@ use ManiaControl\ManiaControl;
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/ */
class BasePlayerTimeStructure extends BaseTimeStructure { class BasePlayerTimeStructure extends BaseTimeStructure {
protected $login;
/** /**
* Construct a new On Hit Structure * Construct a new On Hit Structure
@ -23,6 +24,8 @@ class BasePlayerTimeStructure extends BaseTimeStructure {
*/ */
public function __construct(ManiaControl $maniaControl, $data) { public function __construct(ManiaControl $maniaControl, $data) {
parent::__construct($maniaControl, $data); parent::__construct($maniaControl, $data);
$this->login = $this->getPlainJsonObject()->login;
} }
/** /**
@ -32,7 +35,17 @@ class BasePlayerTimeStructure extends BaseTimeStructure {
* @return \ManiaControl\Players\Player * @return \ManiaControl\Players\Player
*/ */
public function getPlayer() { public function getPlayer() {
return $this->maniaControl->getPlayerManager()->getPlayer($this->getPlainJsonObject()->login); return $this->maniaControl->getPlayerManager()->getPlayer($this->login);
}
/**
* Returns the Login of the Player
*
* @api
* @return string
*/
public function getLogin() {
return $this->login;
} }

View File

@ -5,6 +5,8 @@ namespace ManiaControl\Statistics;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\Callbacks; use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Callbacks\Structures\Common\BasePlayerTimeStructure;
use ManiaControl\Callbacks\Structures\ManiaPlanet\StartEndStructure;
use ManiaControl\Callbacks\Structures\ShootMania\Models\Weapons; use ManiaControl\Callbacks\Structures\ShootMania\Models\Weapons;
use ManiaControl\Callbacks\Structures\ShootMania\OnArmorEmptyStructure; use ManiaControl\Callbacks\Structures\ShootMania\OnArmorEmptyStructure;
use ManiaControl\Callbacks\Structures\ShootMania\OnCaptureStructure; use ManiaControl\Callbacks\Structures\ShootMania\OnCaptureStructure;
@ -61,6 +63,8 @@ class StatisticCollector implements CallbackListener { //TODO remove old callbac
private $maniaControl = null; private $maniaControl = null;
private $onShootArray = array(); private $onShootArray = array();
private $startPlayLoopTime = -1;
/** /**
* Construct a new statistic collector instance * Construct a new statistic collector instance
* *
@ -80,6 +84,14 @@ class StatisticCollector implements CallbackListener { //TODO remove old callbac
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::SM_ONNEARMISS, $this, 'onNearMissCallback'); $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::SM_ONNEARMISS, $this, 'onNearMissCallback');
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::SM_ONCAPTURE, $this, 'onCaptureCallback'); $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::SM_ONCAPTURE, $this, 'onCaptureCallback');
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::MP_STARTPLAYLOOP, $this, 'onStartPlayLoop');
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::MP_ENDPLAYLOOP, $this, 'onEndPlayLoop');
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::SM_ONPLAYERREMOVED, $this, 'onPlayerRemoved');
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONPLAYERREMOVED, $this, 'onPlayerRemoved');
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::SM_ONCAPTURE, $this, 'onCaptureCallback');
// Settings // Settings
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_COLLECT_STATS_ENABLED, true); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_COLLECT_STATS_ENABLED, true);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_COLLECT_STATS_MINPLAYERS, 4); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_COLLECT_STATS_MINPLAYERS, 4);
@ -88,6 +100,8 @@ class StatisticCollector implements CallbackListener { //TODO remove old callbac
/** /**
* Handle ManiaControl OnInit Callback * Handle ManiaControl OnInit Callback
*
* @internal
*/ */
public function onInit() { public function onInit() {
// Define Stats MetaData // Define Stats MetaData
@ -114,6 +128,7 @@ class StatisticCollector implements CallbackListener { //TODO remove old callbac
/** /**
* Checks if the Collecting is Enabled * Checks if the Collecting is Enabled
* *
* @api
* @return boolean * @return boolean
*/ */
public function isCollectingEnabled() { public function isCollectingEnabled() {
@ -124,6 +139,7 @@ class StatisticCollector implements CallbackListener { //TODO remove old callbac
/** /**
* Check for the Minimum Amount of Players to collect * Check for the Minimum Amount of Players to collect
* *
* @api
* @return bool * @return bool
*/ */
public function checkForMinimumPlayers() { public function checkForMinimumPlayers() {
@ -134,6 +150,7 @@ class StatisticCollector implements CallbackListener { //TODO remove old callbac
/** /**
* Handles the onHitCallback * Handles the onHitCallback
* *
* @internal
* @param \ManiaControl\Callbacks\Structures\ShootMania\OnHitStructure $structure * @param \ManiaControl\Callbacks\Structures\ShootMania\OnHitStructure $structure
*/ */
public function onHitCallback(OnHitStructure $structure) { public function onHitCallback(OnHitStructure $structure) {
@ -153,6 +170,7 @@ class StatisticCollector implements CallbackListener { //TODO remove old callbac
/** /**
* Handles the onShoot Callback * Handles the onShoot Callback
* *
* @internal
* @param \ManiaControl\Callbacks\Structures\ShootMania\OnShootStructure $structure * @param \ManiaControl\Callbacks\Structures\ShootMania\OnShootStructure $structure
*/ */
public function onShootCallback(OnShootStructure $structure) { public function onShootCallback(OnShootStructure $structure) {
@ -166,6 +184,7 @@ class StatisticCollector implements CallbackListener { //TODO remove old callbac
/** /**
* Handles the OnNearMiss Callback * Handles the OnNearMiss Callback
* *
* @internal
* @param \ManiaControl\Callbacks\Structures\ShootMania\OnNearMissStructure $structure * @param \ManiaControl\Callbacks\Structures\ShootMania\OnNearMissStructure $structure
*/ */
public function onNearMissCallback(OnNearMissStructure $structure) { public function onNearMissCallback(OnNearMissStructure $structure) {
@ -180,6 +199,7 @@ class StatisticCollector implements CallbackListener { //TODO remove old callbac
/** /**
* Handles the OnCapture Callback * Handles the OnCapture Callback
* *
* @internal
* @param \ManiaControl\Callbacks\Structures\ShootMania\OnCaptureStructure $structure * @param \ManiaControl\Callbacks\Structures\ShootMania\OnCaptureStructure $structure
*/ */
public function onCaptureCallback(OnCaptureStructure $structure) { public function onCaptureCallback(OnCaptureStructure $structure) {
@ -195,6 +215,7 @@ class StatisticCollector implements CallbackListener { //TODO remove old callbac
/** /**
* Handles the OnArmorEmpty Callback * Handles the OnArmorEmpty Callback
* *
* @internal
* @param \ManiaControl\Callbacks\Structures\ShootMania\OnArmorEmptyStructure $structure * @param \ManiaControl\Callbacks\Structures\ShootMania\OnArmorEmptyStructure $structure
*/ */
public function onArmorEmptyCallback(OnArmorEmptyStructure $structure) { public function onArmorEmptyCallback(OnArmorEmptyStructure $structure) {
@ -212,6 +233,7 @@ class StatisticCollector implements CallbackListener { //TODO remove old callbac
/** /**
* Handles the OnPlayerRequestRespawn Callback * Handles the OnPlayerRequestRespawn Callback
* *
* @internal
* @param \ManiaControl\Callbacks\Structures\ShootMania\OnPlayerRequestRespawnStructure $structure * @param \ManiaControl\Callbacks\Structures\ShootMania\OnPlayerRequestRespawnStructure $structure
*/ */
public function onPlayerRequestRespawnCallback(OnPlayerRequestRespawnStructure $structure) { public function onPlayerRequestRespawnCallback(OnPlayerRequestRespawnStructure $structure) {
@ -226,6 +248,7 @@ class StatisticCollector implements CallbackListener { //TODO remove old callbac
/** /**
* Handle EndMap * Handle EndMap
* *
* @internal
* @param array $callback * @param array $callback
*/ */
public function onEndMap(array $callback) { public function onEndMap(array $callback) {
@ -245,11 +268,11 @@ class StatisticCollector implements CallbackListener { //TODO remove old callbac
/** /**
* Insert OnShoot Statistic when a player leaves * Insert OnShoot Statistic when a player leaves
* *
* @internal
* @param Player $player * @param Player $player
*/ */
public function onPlayerDisconnect(Player $player) { public function onPlayerDisconnect(Player $player) {
// Check if Stat Collecting is enabled if (!$this->isCollectingEnabled() || !$this->checkForMinimumPlayers()) {
if (!$this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_COLLECT_STATS_ENABLED)) {
return; return;
} }
@ -260,14 +283,76 @@ class StatisticCollector implements CallbackListener { //TODO remove old callbac
} }
unset($this->onShootArray[$player->login]); unset($this->onShootArray[$player->login]);
} }
}
/**
* Update PlayTime if the Player leaves
*
* @internal
* @param \ManiaControl\Callbacks\Structures\Common\BasePlayerTimeStructure $structure
*/
public function onPlayerRemoved(BasePlayerTimeStructure $structure) {
if (!$this->isCollectingEnabled() || !$this->checkForMinimumPlayers()) {
return;
}
//Check if in a PlayLoop actually has been started
if ($this->startPlayLoopTime < 0) {
return;
}
$durationTime = ($structure->getTime() - $this->startPlayLoopTime) / 1000;
$this->maniaControl->getStatisticManager()->insertStat(self::STAT_PLAYTIME, $structure->getPlayer(), $this->maniaControl->getServer()->index, $durationTime);
}
/**
* Handles the PlayerTime on Join
*
* @internal
* @param \ManiaControl\Callbacks\Structures\ManiaPlanet\StartEndStructure $structure
*/
public function onStartPlayLoop(StartEndStructure $structure) {
$this->startPlayLoopTime = $structure->getTime();
}
/**
* Handles The Playtime Statistic on EndPlayerLoop
*
* @internal
* @param \ManiaControl\Callbacks\Structures\ManiaPlanet\StartEndStructure $structure
*/
public function onEndPlayLoop(StartEndStructure $structure) {
// Check if Stat Collecting is enabled
if (!$this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_COLLECT_STATS_ENABLED)) {
return;
}
//Check if in a PlayLoop actually has been started
if ($this->startPlayLoopTime < 0) {
return;
}
$durationTime = ($structure->getTime() - $this->startPlayLoopTime) / 1000;
foreach ($this->maniaControl->getPlayerManager()->getPlayers() as $player) {
$this->maniaControl->getStatisticManager()->insertStat(self::STAT_PLAYTIME, $player, $this->maniaControl->getServer()->index, $durationTime);
}
$this->startPlayLoopTime = -1;
} }
/** /**
* Handle stats on callbacks * Handle stats on callbacks
* *
* @deprecated
* @internal
* @param array $callback * @param array $callback
*/ */
public function handleCallbacks(array $callback) { public function handleCallbacks(array $callback) {
//TODO remove later, only used for MP3
//TODO survivals //TODO survivals
// Check if Stat Collecting is enabled // Check if Stat Collecting is enabled
if (!$this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_COLLECT_STATS_ENABLED)) { if (!$this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_COLLECT_STATS_ENABLED)) {