2013-12-31 17:31:05 +01:00
|
|
|
<?php
|
2014-01-01 18:37:32 +01:00
|
|
|
|
|
|
|
namespace ManiaControl\Statistics;
|
|
|
|
|
2014-01-01 17:59:29 +01:00
|
|
|
use ManiaControl\ManiaControl;
|
2014-01-01 18:29:28 +01:00
|
|
|
use ManiaControl\Players\Player;
|
|
|
|
|
2013-12-31 17:31:05 +01:00
|
|
|
/**
|
|
|
|
* Statistic Manager Class
|
|
|
|
*
|
2014-05-02 17:50:30 +02:00
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
|
|
|
* @copyright 2014 ManiaControl Team
|
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2013-12-31 17:31:05 +01:00
|
|
|
*/
|
|
|
|
class StatisticManager {
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2013-12-31 17:59:01 +01:00
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
const TABLE_STATMETADATA = 'mc_statmetadata';
|
2014-01-19 00:09:15 +01:00
|
|
|
const TABLE_STATISTICS = 'mc_statistics';
|
|
|
|
const STAT_TYPE_INT = '0';
|
|
|
|
const STAT_TYPE_TIME = '1';
|
2014-01-29 09:29:02 +01:00
|
|
|
const STAT_TYPE_FLOAT = '2';
|
2014-01-28 22:59:18 +01:00
|
|
|
|
|
|
|
const SPECIAL_STAT_KD_RATIO = 'Kill Death Ratio'; //TODO dynamic later
|
2014-01-29 20:51:53 +01:00
|
|
|
const SPECIAL_STAT_HITS_PH = 'Hits Per Hour';
|
|
|
|
const SPECIAL_STAT_LASER_ACC = 'Laser Accuracy';
|
|
|
|
const SPECIAL_STAT_NUCLEUS_ACC = 'Nucleus Accuracy';
|
|
|
|
const SPECIAL_STAT_ROCKET_ACC = 'Rocket Accuracy';
|
|
|
|
const SPECIAL_STAT_ARROW_ACC = 'Arrow Accuracy';
|
2014-01-19 00:09:15 +01:00
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-08-02 22:31:46 +02:00
|
|
|
* Public properties
|
2014-01-01 18:29:28 +01:00
|
|
|
*/
|
2014-08-02 22:31:46 +02:00
|
|
|
/** @var StatisticCollector $statisticCollector */
|
|
|
|
/** @deprecated see getStatisticCollector() */
|
2014-01-01 18:29:28 +01:00
|
|
|
public $statisticCollector = null;
|
2014-08-02 22:31:46 +02:00
|
|
|
/** @var SimpleStatsList $simpleStatsList */
|
|
|
|
/** @deprecated see getSimpleStatsList() */
|
2014-01-19 00:09:15 +01:00
|
|
|
public $simpleStatsList = null;
|
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-08-02 22:31:46 +02:00
|
|
|
* Private properties
|
2013-12-31 17:59:01 +01:00
|
|
|
*/
|
2014-08-02 22:31:46 +02:00
|
|
|
/** @var ManiaControl $maniaControl */
|
2013-12-31 17:59:01 +01:00
|
|
|
private $maniaControl = null;
|
2014-01-01 18:29:28 +01:00
|
|
|
private $stats = array();
|
2014-01-28 22:59:18 +01:00
|
|
|
private $specialStats = array();
|
2013-12-31 17:59:01 +01:00
|
|
|
|
|
|
|
/**
|
2014-08-02 22:31:46 +02:00
|
|
|
* Construct a new statistic manager instance
|
2013-12-31 17:59:01 +01:00
|
|
|
*
|
2014-08-02 22:31:46 +02:00
|
|
|
* @param ManiaControl $maniaControl
|
2013-12-31 17:59:01 +01:00
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
$this->initTables();
|
2014-01-19 00:09:15 +01:00
|
|
|
|
2014-01-01 18:29:28 +01:00
|
|
|
$this->statisticCollector = new StatisticCollector($maniaControl);
|
2014-01-19 00:09:15 +01:00
|
|
|
$this->simpleStatsList = new SimpleStatsList($maniaControl);
|
|
|
|
|
2014-01-05 14:41:19 +01:00
|
|
|
// Store Stats MetaData
|
2014-01-01 18:29:28 +01:00
|
|
|
$this->storeStatMetaData();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-02 17:50:30 +02:00
|
|
|
* Initialize necessary database tables
|
2014-01-01 18:29:28 +01:00
|
|
|
*
|
2014-05-02 17:50:30 +02:00
|
|
|
* @return bool
|
2014-01-01 18:29:28 +01:00
|
|
|
*/
|
2014-05-02 17:50:30 +02:00
|
|
|
private function initTables() {
|
2014-08-03 01:34:18 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-05-02 17:50:30 +02:00
|
|
|
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_STATMETADATA . "` (
|
|
|
|
`index` int(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
`name` varchar(100) NOT NULL,
|
|
|
|
`type` int(5) NOT NULL,
|
|
|
|
`description` varchar(150) NOT NULL,
|
|
|
|
PRIMARY KEY (`index`),
|
|
|
|
UNIQUE KEY `name` (`name`)
|
|
|
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics Meta Data' AUTO_INCREMENT=1;";
|
|
|
|
$statement = $mysqli->prepare($query);
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
return false;
|
2014-02-06 21:18:25 +01:00
|
|
|
}
|
2014-05-02 17:50:30 +02:00
|
|
|
$statement->execute();
|
|
|
|
if ($statement->error) {
|
|
|
|
trigger_error($statement->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$statement->close();
|
2014-02-06 21:18:25 +01:00
|
|
|
|
2014-05-02 17:50:30 +02:00
|
|
|
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_STATISTICS . "` (
|
|
|
|
`index` int(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
`serverIndex` int(11) NOT NULL,
|
|
|
|
`playerId` int(11) NOT NULL,
|
|
|
|
`statId` int(11) NOT NULL,
|
|
|
|
`value` int(20) NOT NULL DEFAULT '0',
|
|
|
|
PRIMARY KEY (`index`),
|
|
|
|
UNIQUE KEY `unique` (`statId`,`playerId`,`serverIndex`)
|
|
|
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics' AUTO_INCREMENT=1;";
|
|
|
|
$statement = $mysqli->prepare($query);
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$statement->execute();
|
|
|
|
if ($statement->error) {
|
|
|
|
trigger_error($statement->error, E_USER_ERROR);
|
|
|
|
return false;
|
2014-01-05 14:41:19 +01:00
|
|
|
}
|
2014-05-02 17:50:30 +02:00
|
|
|
$statement->close();
|
|
|
|
return true;
|
|
|
|
}
|
2014-01-19 00:09:15 +01:00
|
|
|
|
2014-05-02 17:50:30 +02:00
|
|
|
/**
|
|
|
|
* Store Stats Meta Data from the Database
|
|
|
|
*/
|
|
|
|
private function storeStatMetaData() {
|
2014-08-03 01:34:18 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-05-02 17:50:30 +02:00
|
|
|
|
|
|
|
$query = "SELECT * FROM `" . self::TABLE_STATMETADATA . "`;";
|
2014-01-02 11:19:26 +01:00
|
|
|
$result = $mysqli->query($query);
|
2014-01-28 22:59:18 +01:00
|
|
|
if (!$result) {
|
2014-01-02 11:19:26 +01:00
|
|
|
trigger_error($mysqli->error);
|
2014-05-02 17:50:30 +02:00
|
|
|
return;
|
2014-01-01 18:29:28 +01:00
|
|
|
}
|
2014-01-19 00:09:15 +01:00
|
|
|
|
2014-05-02 17:50:30 +02:00
|
|
|
while ($row = $result->fetch_object()) {
|
|
|
|
$this->stats[$row->name] = $row;
|
|
|
|
}
|
2014-05-18 23:34:47 +02:00
|
|
|
$result->free();
|
2014-05-02 17:50:30 +02:00
|
|
|
|
|
|
|
// TODO: own model class
|
|
|
|
|
|
|
|
//Define Special Stat Kill / Death Ratio
|
|
|
|
$stat = new \stdClass();
|
|
|
|
$stat->name = self::SPECIAL_STAT_KD_RATIO;
|
|
|
|
$stat->type = self::STAT_TYPE_FLOAT;
|
|
|
|
$this->specialStats[self::SPECIAL_STAT_KD_RATIO] = $stat;
|
|
|
|
|
|
|
|
//Hits Per Hour
|
|
|
|
$stat = new \stdClass();
|
|
|
|
$stat->name = self::SPECIAL_STAT_HITS_PH;
|
|
|
|
$stat->type = self::STAT_TYPE_FLOAT;
|
|
|
|
$this->specialStats[self::SPECIAL_STAT_HITS_PH] = $stat;
|
|
|
|
|
|
|
|
//Laser Accuracy
|
|
|
|
$stat = new \stdClass();
|
|
|
|
$stat->name = self::SPECIAL_STAT_LASER_ACC;
|
|
|
|
$stat->type = self::STAT_TYPE_FLOAT;
|
|
|
|
$this->specialStats[self::SPECIAL_STAT_LASER_ACC] = $stat;
|
|
|
|
|
|
|
|
//Nucleus Accuracy
|
|
|
|
$stat = new \stdClass();
|
|
|
|
$stat->name = self::SPECIAL_STAT_NUCLEUS_ACC;
|
|
|
|
$stat->type = self::STAT_TYPE_FLOAT;
|
|
|
|
$this->specialStats[self::SPECIAL_STAT_NUCLEUS_ACC] = $stat;
|
|
|
|
|
|
|
|
//Arrow Accuracy
|
|
|
|
$stat = new \stdClass();
|
|
|
|
$stat->name = self::SPECIAL_STAT_ARROW_ACC;
|
|
|
|
$stat->type = self::STAT_TYPE_FLOAT;
|
|
|
|
$this->specialStats[self::SPECIAL_STAT_ARROW_ACC] = $stat;
|
|
|
|
|
|
|
|
//Rocket Accuracy
|
|
|
|
$stat = new \stdClass();
|
|
|
|
$stat->name = self::SPECIAL_STAT_ROCKET_ACC;
|
|
|
|
$stat->type = self::STAT_TYPE_FLOAT;
|
|
|
|
$this->specialStats[self::SPECIAL_STAT_ROCKET_ACC] = $stat;
|
2014-01-01 18:29:28 +01:00
|
|
|
}
|
|
|
|
|
2014-08-02 22:31:46 +02:00
|
|
|
/**
|
|
|
|
* Return the statistic collector
|
|
|
|
*
|
|
|
|
* @return StatisticCollector
|
|
|
|
*/
|
|
|
|
public function getStatisticCollector() {
|
|
|
|
return $this->statisticCollector;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the simple stats list
|
|
|
|
*
|
|
|
|
* @return SimpleStatsList
|
|
|
|
*/
|
|
|
|
public function getSimpleStatsList() {
|
|
|
|
return $this->simpleStatsList;
|
|
|
|
}
|
|
|
|
|
2014-01-21 21:58:39 +01:00
|
|
|
/**
|
2014-02-06 14:35:54 +01:00
|
|
|
* Get All statistics ordered by an given name
|
2014-01-21 21:58:39 +01:00
|
|
|
*
|
2014-02-06 14:35:54 +01:00
|
|
|
* @param string $statName
|
|
|
|
* @param $serverIndex
|
|
|
|
* @param $minValue
|
|
|
|
* @internal param $orderedBy
|
2014-04-20 16:13:24 +02:00
|
|
|
* @return array
|
2014-01-21 21:58:39 +01:00
|
|
|
*/
|
2014-02-06 14:35:54 +01:00
|
|
|
public function getStatsRanking($statName = '', $serverIndex = -1, $minValue = -1) {
|
2014-01-29 20:51:53 +01:00
|
|
|
if (isset($this->specialStats[$statName])) {
|
|
|
|
return $this->getStatsRankingOfSpecialStat($statName, $serverIndex);
|
|
|
|
}
|
|
|
|
|
2014-08-03 01:34:18 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-01-21 21:58:39 +01:00
|
|
|
$statId = $this->getStatId($statName);
|
|
|
|
|
2014-08-03 01:34:18 +02:00
|
|
|
$query = "SELECT `playerId`, `serverIndex`, `value` FROM `" . self::TABLE_STATISTICS . "`
|
|
|
|
WHERE `statId` = {$statId} ";
|
2014-06-14 14:32:29 +02:00
|
|
|
if ($minValue >= 0) {
|
2014-08-03 01:34:18 +02:00
|
|
|
$query .= "AND `value` >= {$minValue} ";
|
2014-02-06 14:35:54 +01:00
|
|
|
}
|
2014-08-03 01:34:18 +02:00
|
|
|
$query .= "ORDER BY `value` DESC;";
|
2014-01-21 21:58:39 +01:00
|
|
|
|
|
|
|
$result = $mysqli->query($query);
|
2014-01-28 22:59:18 +01:00
|
|
|
if (!$result) {
|
2014-01-21 21:58:39 +01:00
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$stats = array();
|
2014-05-02 17:50:30 +02:00
|
|
|
while ($row = $result->fetch_object()) {
|
2014-06-14 14:32:29 +02:00
|
|
|
if ($serverIndex < 0) {
|
2014-01-28 22:59:18 +01:00
|
|
|
if (!isset($stats[$row->playerId])) {
|
2014-01-21 21:58:39 +01:00
|
|
|
$stats[$row->playerId] = $row->value;
|
|
|
|
} else {
|
|
|
|
$stats[$row->playerId] += $row->value;
|
|
|
|
}
|
2014-01-28 22:59:18 +01:00
|
|
|
} else if ($serverIndex == $row->serverIndex) {
|
2014-01-21 21:58:39 +01:00
|
|
|
$stats[$row->playerId] = $row->value;
|
|
|
|
}
|
|
|
|
}
|
2014-06-14 14:32:29 +02:00
|
|
|
$result->free();
|
2014-01-21 21:58:39 +01:00
|
|
|
|
2014-01-31 19:32:50 +01:00
|
|
|
arsort($stats);
|
2014-01-21 21:58:39 +01:00
|
|
|
return $stats;
|
|
|
|
}
|
|
|
|
|
2014-01-28 22:59:18 +01:00
|
|
|
/**
|
|
|
|
* Gets The Ranking of an Special Stat
|
|
|
|
*
|
|
|
|
* @param string $statName
|
2014-01-29 20:51:53 +01:00
|
|
|
* @param $serverIndex
|
|
|
|
* @return array
|
2014-01-28 22:59:18 +01:00
|
|
|
*/
|
2014-01-29 20:51:53 +01:00
|
|
|
public function getStatsRankingOfSpecialStat($statName = '', $serverIndex = -1) {
|
2014-01-28 22:59:18 +01:00
|
|
|
$statsArray = array();
|
2014-05-02 17:50:30 +02:00
|
|
|
switch ($statName) {
|
2014-01-28 22:59:18 +01:00
|
|
|
case self::SPECIAL_STAT_KD_RATIO:
|
2014-01-29 20:51:53 +01:00
|
|
|
$kills = $this->getStatsRanking(StatisticCollector::STAT_ON_KILL, $serverIndex);
|
|
|
|
$deaths = $this->getStatsRanking(StatisticCollector::STAT_ON_DEATH, $serverIndex);
|
2014-05-02 17:50:30 +02:00
|
|
|
if (!$kills || !$deaths) {
|
2014-03-13 18:47:40 +01:00
|
|
|
return array();
|
|
|
|
}
|
2014-05-02 17:50:30 +02:00
|
|
|
foreach ($deaths as $key => $death) {
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$death || !isset($kills[$key])) {
|
2014-01-28 22:59:18 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$statsArray[$key] = intval($kills[$key]) / intval($death);
|
|
|
|
}
|
2014-01-31 15:41:01 +01:00
|
|
|
arsort($statsArray);
|
2014-01-29 20:51:53 +01:00
|
|
|
break;
|
|
|
|
case self::SPECIAL_STAT_HITS_PH:
|
|
|
|
$hits = $this->getStatsRanking(StatisticCollector::STAT_ON_HIT, $serverIndex);
|
|
|
|
$times = $this->getStatsRanking(StatisticCollector::STAT_PLAYTIME, $serverIndex);
|
2014-05-02 17:50:30 +02:00
|
|
|
if (!$hits || !$times) {
|
2014-03-13 18:47:40 +01:00
|
|
|
return array();
|
|
|
|
}
|
2014-05-02 17:50:30 +02:00
|
|
|
foreach ($times as $key => $time) {
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$time || !isset($hits[$key])) {
|
2014-01-29 20:51:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$statsArray[$key] = intval($hits[$key]) / (intval($time) / 3600);
|
|
|
|
}
|
2014-01-31 15:41:01 +01:00
|
|
|
arsort($statsArray);
|
2014-01-29 20:51:53 +01:00
|
|
|
break;
|
|
|
|
case self::SPECIAL_STAT_ARROW_ACC:
|
|
|
|
$hits = $this->getStatsRanking(StatisticCollector::STAT_ARROW_HIT, $serverIndex);
|
|
|
|
$shots = $this->getStatsRanking(StatisticCollector::STAT_ARROW_SHOT, $serverIndex);
|
2014-05-02 17:50:30 +02:00
|
|
|
if (!$hits || !$shots) {
|
2014-03-13 18:47:40 +01:00
|
|
|
return array();
|
|
|
|
}
|
2014-05-02 17:50:30 +02:00
|
|
|
foreach ($shots as $key => $shot) {
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$shot || !isset($hits[$key])) {
|
2014-01-29 20:51:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$statsArray[$key] = intval($hits[$key]) / (intval($shot));
|
|
|
|
}
|
2014-01-31 15:41:01 +01:00
|
|
|
arsort($statsArray);
|
2014-01-29 20:51:53 +01:00
|
|
|
break;
|
|
|
|
case self::SPECIAL_STAT_LASER_ACC:
|
|
|
|
$hits = $this->getStatsRanking(StatisticCollector::STAT_LASER_HIT, $serverIndex);
|
|
|
|
$shots = $this->getStatsRanking(StatisticCollector::STAT_LASER_SHOT, $serverIndex);
|
2014-05-02 17:50:30 +02:00
|
|
|
if (!$hits || !$shots) {
|
2014-03-13 18:47:40 +01:00
|
|
|
return array();
|
|
|
|
}
|
2014-05-02 17:50:30 +02:00
|
|
|
foreach ($shots as $key => $shot) {
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$shot || !isset($hits[$key])) {
|
2014-01-29 20:51:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$statsArray[$key] = intval($hits[$key]) / (intval($shot));
|
|
|
|
}
|
2014-01-31 15:41:01 +01:00
|
|
|
arsort($statsArray);
|
2014-01-29 20:51:53 +01:00
|
|
|
break;
|
|
|
|
case self::SPECIAL_STAT_ROCKET_ACC:
|
|
|
|
$hits = $this->getStatsRanking(StatisticCollector::STAT_ROCKET_HIT, $serverIndex);
|
|
|
|
$shots = $this->getStatsRanking(StatisticCollector::STAT_ROCKET_SHOT, $serverIndex);
|
2014-05-02 17:50:30 +02:00
|
|
|
if (!$hits || !$shots) {
|
2014-03-13 18:47:40 +01:00
|
|
|
return array();
|
|
|
|
}
|
2014-05-02 17:50:30 +02:00
|
|
|
foreach ($shots as $key => $shot) {
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$shot || !isset($hits[$key])) {
|
2014-01-29 20:51:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$statsArray[$key] = intval($hits[$key]) / (intval($shot));
|
|
|
|
}
|
2014-01-31 15:41:01 +01:00
|
|
|
arsort($statsArray);
|
2014-01-29 20:51:53 +01:00
|
|
|
break;
|
|
|
|
case self::SPECIAL_STAT_NUCLEUS_ACC:
|
|
|
|
$hits = $this->getStatsRanking(StatisticCollector::STAT_NUCLEUS_HIT, $serverIndex);
|
|
|
|
$shots = $this->getStatsRanking(StatisticCollector::STAT_NUCLEUS_SHOT, $serverIndex);
|
2014-05-02 17:50:30 +02:00
|
|
|
if (!$hits || !$shots) {
|
2014-03-13 18:47:40 +01:00
|
|
|
return array();
|
|
|
|
}
|
2014-05-02 17:50:30 +02:00
|
|
|
foreach ($shots as $key => $shot) {
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$shot || !isset($hits[$key])) {
|
2014-01-29 20:51:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$statsArray[$key] = intval($hits[$key]) / (intval($shot));
|
|
|
|
}
|
2014-01-31 15:41:01 +01:00
|
|
|
arsort($statsArray);
|
2014-01-29 20:51:53 +01:00
|
|
|
break;
|
2014-01-28 22:59:18 +01:00
|
|
|
}
|
|
|
|
return $statsArray;
|
|
|
|
}
|
|
|
|
|
2014-01-01 18:29:28 +01:00
|
|
|
/**
|
2014-01-05 19:15:27 +01:00
|
|
|
* Return the Stat Id
|
2014-01-01 18:29:28 +01:00
|
|
|
*
|
2014-05-13 16:40:05 +02:00
|
|
|
* @param string $statName
|
2014-01-01 18:29:28 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function getStatId($statName) {
|
2014-01-28 22:59:18 +01:00
|
|
|
if (isset($this->stats[$statName])) {
|
2014-01-01 18:29:28 +01:00
|
|
|
$stat = $this->stats[$statName];
|
2014-01-19 00:09:15 +01:00
|
|
|
return (int)$stat->index;
|
2014-01-05 14:41:19 +01:00
|
|
|
}
|
2014-01-05 19:15:27 +01:00
|
|
|
return null;
|
2014-01-01 18:29:28 +01:00
|
|
|
}
|
|
|
|
|
2014-01-03 18:37:51 +01:00
|
|
|
/**
|
2014-01-29 20:51:53 +01:00
|
|
|
* Get all statistics of a certain player
|
2014-01-03 18:37:51 +01:00
|
|
|
*
|
|
|
|
* @param Player $player
|
2014-01-19 00:09:15 +01:00
|
|
|
* @param int $serverIndex
|
2014-01-03 18:37:51 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2014-01-05 14:41:19 +01:00
|
|
|
public function getAllPlayerStats(Player $player, $serverIndex = -1) {
|
2014-01-29 20:51:53 +01:00
|
|
|
// TODO improve performance of the foreach
|
2014-01-05 19:15:27 +01:00
|
|
|
$playerStats = array();
|
2014-05-02 17:50:30 +02:00
|
|
|
foreach ($this->stats as $stat) {
|
2014-01-19 00:09:15 +01:00
|
|
|
$value = $this->getStatisticData($stat->name, $player->index, $serverIndex);
|
2014-01-03 18:37:51 +01:00
|
|
|
$playerStats[$stat->name] = array($stat, $value);
|
2014-01-03 17:36:10 +01:00
|
|
|
}
|
2014-01-19 00:09:15 +01:00
|
|
|
|
2014-05-02 17:50:30 +02:00
|
|
|
foreach ($this->specialStats as $stat) {
|
|
|
|
switch ($stat->name) {
|
2014-01-28 22:59:18 +01:00
|
|
|
case self::SPECIAL_STAT_KD_RATIO:
|
2014-01-29 09:29:02 +01:00
|
|
|
if (!isset($playerStats[StatisticCollector::STAT_ON_KILL]) || !isset($playerStats[StatisticCollector::STAT_ON_DEATH])) {
|
2014-01-28 22:59:18 +01:00
|
|
|
continue;
|
2014-01-29 09:29:02 +01:00
|
|
|
}
|
2014-01-29 20:51:53 +01:00
|
|
|
$kills = intval($playerStats[StatisticCollector::STAT_ON_KILL][1]);
|
|
|
|
$deaths = intval($playerStats[StatisticCollector::STAT_ON_DEATH][1]);
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$deaths) {
|
2014-01-28 22:59:18 +01:00
|
|
|
continue;
|
2014-01-29 09:29:02 +01:00
|
|
|
}
|
2014-01-28 22:59:18 +01:00
|
|
|
$playerStats[$stat->name] = array($stat, $kills / $deaths);
|
2014-01-29 20:51:53 +01:00
|
|
|
break;
|
|
|
|
case self::SPECIAL_STAT_HITS_PH:
|
|
|
|
if (!isset($playerStats[StatisticCollector::STAT_PLAYTIME]) || !isset($playerStats[StatisticCollector::STAT_ON_HIT])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$hits = intval($playerStats[StatisticCollector::STAT_ON_HIT][1]);
|
|
|
|
$time = intval($playerStats[StatisticCollector::STAT_PLAYTIME][1]);
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$time) {
|
2014-01-29 20:51:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$playerStats[$stat->name] = array($stat, $hits / ($time / 3600));
|
|
|
|
break;
|
|
|
|
case self::SPECIAL_STAT_ARROW_ACC:
|
|
|
|
if (!isset($playerStats[StatisticCollector::STAT_ARROW_HIT]) || !isset($playerStats[StatisticCollector::STAT_ARROW_SHOT])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$hits = intval($playerStats[StatisticCollector::STAT_ARROW_HIT][1]);
|
|
|
|
$shots = intval($playerStats[StatisticCollector::STAT_ARROW_SHOT][1]);
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$shots) {
|
2014-01-29 20:51:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$playerStats[$stat->name] = array($stat, $hits / $shots);
|
|
|
|
break;
|
|
|
|
case self::SPECIAL_STAT_LASER_ACC:
|
|
|
|
if (!isset($playerStats[StatisticCollector::STAT_LASER_HIT]) || !isset($playerStats[StatisticCollector::STAT_LASER_SHOT])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$hits = intval($playerStats[StatisticCollector::STAT_LASER_HIT][1]);
|
|
|
|
$shots = intval($playerStats[StatisticCollector::STAT_LASER_SHOT][1]);
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$shots) {
|
2014-01-29 20:51:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$playerStats[$stat->name] = array($stat, $hits / $shots);
|
|
|
|
break;
|
|
|
|
case self::SPECIAL_STAT_ROCKET_ACC:
|
|
|
|
if (!isset($playerStats[StatisticCollector::STAT_ROCKET_HIT]) || !isset($playerStats[StatisticCollector::STAT_ROCKET_SHOT])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$hits = intval($playerStats[StatisticCollector::STAT_ROCKET_HIT][1]);
|
|
|
|
$shots = intval($playerStats[StatisticCollector::STAT_ROCKET_SHOT][1]);
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$shots) {
|
2014-01-29 20:51:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$playerStats[$stat->name] = array($stat, $hits / $shots);
|
|
|
|
break;
|
|
|
|
case self::SPECIAL_STAT_NUCLEUS_ACC:
|
|
|
|
if (!isset($playerStats[StatisticCollector::STAT_NUCLEUS_HIT]) || !isset($playerStats[StatisticCollector::STAT_NUCLEUS_SHOT])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$hits = intval($playerStats[StatisticCollector::STAT_NUCLEUS_HIT][1]);
|
|
|
|
$shots = intval($playerStats[StatisticCollector::STAT_NUCLEUS_SHOT][1]);
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$shots) {
|
2014-01-29 20:51:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$playerStats[$stat->name] = array($stat, (float)($hits / $shots));
|
|
|
|
break;
|
2014-01-28 22:59:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $playerStats;
|
|
|
|
}
|
|
|
|
|
2014-05-02 17:50:30 +02:00
|
|
|
/**
|
|
|
|
* Get the value of an statistic
|
|
|
|
*
|
|
|
|
* @param $statName
|
|
|
|
* @param $playerId
|
|
|
|
* @param int $serverIndex
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getStatisticData($statName, $playerId, $serverIndex = -1) {
|
|
|
|
//Handle Special Stats
|
|
|
|
switch ($statName) {
|
|
|
|
case self::SPECIAL_STAT_KD_RATIO:
|
|
|
|
$kills = $this->getStatisticData(StatisticCollector::STAT_ON_KILL, $playerId, $serverIndex);
|
|
|
|
$deaths = $this->getStatisticData(StatisticCollector::STAT_ON_DEATH, $playerId, $serverIndex);
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$deaths) {
|
2014-05-02 17:50:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return intval($kills) / intval($deaths);
|
|
|
|
case self::SPECIAL_STAT_HITS_PH:
|
|
|
|
$hits = $this->getStatisticData(StatisticCollector::STAT_ON_HIT, $playerId, $serverIndex);
|
|
|
|
$time = $this->getStatisticData(StatisticCollector::STAT_PLAYTIME, $playerId, $serverIndex);
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$time) {
|
2014-05-02 17:50:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return intval($hits) / (intval($time) / 3600);
|
|
|
|
case self::SPECIAL_STAT_ARROW_ACC:
|
|
|
|
$hits = $this->getStatisticData(StatisticCollector::STAT_ARROW_HIT, $playerId, $serverIndex);
|
|
|
|
$shots = $this->getStatisticData(StatisticCollector::STAT_ARROW_SHOT, $playerId, $serverIndex);
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$shots) {
|
2014-05-02 17:50:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return intval($hits) / intval($shots);
|
|
|
|
case self::SPECIAL_STAT_LASER_ACC:
|
|
|
|
$hits = $this->getStatisticData(StatisticCollector::STAT_LASER_HIT, $playerId, $serverIndex);
|
|
|
|
$shots = $this->getStatisticData(StatisticCollector::STAT_LASER_SHOT, $playerId, $serverIndex);
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$shots) {
|
2014-05-02 17:50:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return intval($hits) / intval($shots);
|
|
|
|
case self::SPECIAL_STAT_NUCLEUS_ACC:
|
|
|
|
$hits = $this->getStatisticData(StatisticCollector::STAT_NUCLEUS_HIT, $playerId, $serverIndex);
|
|
|
|
$shots = $this->getStatisticData(StatisticCollector::STAT_NUCLEUS_SHOT, $playerId, $serverIndex);
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$shots) {
|
2014-05-02 17:50:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return intval($hits) / intval($shots);
|
|
|
|
case self::SPECIAL_STAT_ROCKET_ACC:
|
|
|
|
$hits = $this->getStatisticData(StatisticCollector::STAT_ROCKET_HIT, $playerId, $serverIndex);
|
|
|
|
$shots = $this->getStatisticData(StatisticCollector::STAT_ROCKET_SHOT, $playerId, $serverIndex);
|
2014-06-14 14:32:29 +02:00
|
|
|
if (!$shots) {
|
2014-05-02 17:50:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return intval($hits) / intval($shots);
|
|
|
|
}
|
|
|
|
|
2014-08-03 01:34:18 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-05-02 17:50:30 +02:00
|
|
|
$statId = $this->getStatId($statName);
|
|
|
|
|
|
|
|
if (!$statId) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-06-14 14:32:29 +02:00
|
|
|
if ($serverIndex < 0) {
|
2014-08-03 01:34:18 +02:00
|
|
|
$query = "SELECT SUM(`value`) as `value` FROM `" . self::TABLE_STATISTICS . "`
|
|
|
|
WHERE `statId` = {$statId}
|
|
|
|
AND `playerId` = {$playerId};";
|
2014-05-02 17:50:30 +02:00
|
|
|
} else {
|
2014-08-03 01:34:18 +02:00
|
|
|
$query = "SELECT `value` FROM `" . self::TABLE_STATISTICS . "`
|
|
|
|
WHERE `statId` = {$statId}
|
|
|
|
AND `playerId` = {$playerId}
|
|
|
|
AND `serverIndex` = {$serverIndex};";
|
2014-05-02 17:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$result = $mysqli->query($query);
|
|
|
|
if (!$result) {
|
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$row = $result->fetch_object();
|
|
|
|
|
2014-05-18 23:34:47 +02:00
|
|
|
$result->free();
|
2014-05-02 17:50:30 +02:00
|
|
|
return $row->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increments a Statistic by one
|
|
|
|
*
|
|
|
|
* @param string $statName
|
|
|
|
* @param Player $player
|
|
|
|
* @param int $serverIndex
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function incrementStat($statName, Player $player, $serverIndex = -1) {
|
|
|
|
return $this->insertStat($statName, $player, $serverIndex, 1);
|
|
|
|
}
|
|
|
|
|
2014-01-01 18:29:28 +01:00
|
|
|
/**
|
|
|
|
* Inserts a Stat into the database
|
|
|
|
*
|
2014-01-05 14:41:19 +01:00
|
|
|
* @param string $statName
|
2014-01-01 18:29:28 +01:00
|
|
|
* @param Player $player
|
2014-01-19 00:09:15 +01:00
|
|
|
* @param int $serverIndex
|
|
|
|
* @param mixed $value , value to Add
|
2014-01-01 18:29:28 +01:00
|
|
|
* @param string $statType
|
2014-01-02 11:19:26 +01:00
|
|
|
* @return bool
|
2014-01-01 18:29:28 +01:00
|
|
|
*/
|
2014-01-05 19:15:27 +01:00
|
|
|
public function insertStat($statName, Player $player, $serverIndex = -1, $value, $statType = self::STAT_TYPE_INT) {
|
2014-06-14 15:48:27 +02:00
|
|
|
// TODO: statType isn't used
|
2014-01-28 22:59:18 +01:00
|
|
|
if (!$player) {
|
2014-01-19 00:09:15 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-28 22:59:18 +01:00
|
|
|
if ($player->isFakePlayer()) {
|
2014-01-19 00:09:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-01-03 21:06:24 +01:00
|
|
|
$statId = $this->getStatId($statName);
|
2014-01-28 22:59:18 +01:00
|
|
|
if (!$statId) {
|
2014-01-19 00:09:15 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-02-01 17:47:05 +01:00
|
|
|
if ($value < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-19 00:09:15 +01:00
|
|
|
|
2014-06-14 14:32:29 +02:00
|
|
|
if ($serverIndex) {
|
2014-08-03 01:34:18 +02:00
|
|
|
$serverIndex = $this->maniaControl->getServer()->index;
|
2014-01-02 11:19:26 +01:00
|
|
|
}
|
2014-01-19 00:09:15 +01:00
|
|
|
|
2014-08-03 01:34:18 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-01-19 00:09:15 +01:00
|
|
|
$query = "INSERT INTO `" . self::TABLE_STATISTICS . "` (
|
2014-01-05 19:15:27 +01:00
|
|
|
`serverIndex`,
|
|
|
|
`playerId`,
|
|
|
|
`statId`,
|
|
|
|
`value`
|
2014-01-01 18:29:28 +01:00
|
|
|
) VALUES (
|
|
|
|
?, ?, ?, ?
|
|
|
|
) ON DUPLICATE KEY UPDATE
|
|
|
|
`value` = `value` + VALUES(`value`);";
|
2014-01-02 11:19:26 +01:00
|
|
|
$statement = $mysqli->prepare($query);
|
2014-01-28 22:59:18 +01:00
|
|
|
if ($mysqli->error) {
|
2014-01-02 11:19:26 +01:00
|
|
|
trigger_error($mysqli->error);
|
2014-01-01 18:29:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-05 14:41:19 +01:00
|
|
|
$statement->bind_param('iiii', $serverIndex, $player->index, $statId, $value);
|
2014-01-01 18:29:28 +01:00
|
|
|
$statement->execute();
|
2014-01-28 22:59:18 +01:00
|
|
|
if ($statement->error) {
|
2014-01-01 18:29:28 +01:00
|
|
|
trigger_error($statement->error);
|
|
|
|
$statement->close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$statement->close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-31 18:15:45 +01:00
|
|
|
/**
|
|
|
|
* Defines a Stat
|
|
|
|
*
|
2014-01-19 00:09:15 +01:00
|
|
|
* @param $statName
|
2014-01-03 18:37:51 +01:00
|
|
|
* @param string $type
|
2014-01-03 19:14:07 +01:00
|
|
|
* @param string $statDescription
|
2014-01-02 11:19:26 +01:00
|
|
|
* @return bool
|
2013-12-31 18:15:45 +01:00
|
|
|
*/
|
2014-01-03 19:14:07 +01:00
|
|
|
public function defineStatMetaData($statName, $type = self::STAT_TYPE_INT, $statDescription = '') {
|
2014-08-03 01:34:18 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-01-19 00:09:15 +01:00
|
|
|
$query = "INSERT INTO `" . self::TABLE_STATMETADATA . "` (
|
2014-01-05 19:15:27 +01:00
|
|
|
`name`,
|
|
|
|
`type`,
|
|
|
|
`description`
|
2013-12-31 18:15:45 +01:00
|
|
|
) VALUES (
|
2014-01-05 19:15:27 +01:00
|
|
|
?, ?, ?
|
2014-01-03 19:14:07 +01:00
|
|
|
) ON DUPLICATE KEY UPDATE
|
|
|
|
`type` = VALUES(`type`),
|
|
|
|
`description` = VALUES(`description`);";
|
2014-01-02 11:19:26 +01:00
|
|
|
$statement = $mysqli->prepare($query);
|
2014-01-28 22:59:18 +01:00
|
|
|
if ($mysqli->error) {
|
2014-01-02 11:19:26 +01:00
|
|
|
trigger_error($mysqli->error);
|
2013-12-31 18:15:45 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-03 19:14:07 +01:00
|
|
|
$statement->bind_param('sis', $statName, $type, $statDescription);
|
2013-12-31 18:15:45 +01:00
|
|
|
$statement->execute();
|
2014-01-28 22:59:18 +01:00
|
|
|
if ($statement->error) {
|
2013-12-31 18:15:45 +01:00
|
|
|
trigger_error($statement->error);
|
|
|
|
$statement->close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$statement->close();
|
2014-01-02 11:19:26 +01:00
|
|
|
return true;
|
2013-12-31 18:15:45 +01:00
|
|
|
}
|
2014-01-05 19:15:27 +01:00
|
|
|
}
|