2014-05-04 00:36:20 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MCTeam\Dedimania;
|
|
|
|
|
|
|
|
use ManiaControl\ManiaControl;
|
2014-05-13 16:40:05 +02:00
|
|
|
use ManiaControl\Players\Player;
|
2014-05-04 00:36:20 +02:00
|
|
|
use Maniaplanet\DedicatedServer\Structures\Version;
|
|
|
|
|
|
|
|
/**
|
2014-05-24 20:19:41 +02:00
|
|
|
* ManiaControl Dedimania Plugin Data Structure
|
2014-05-04 00:36:20 +02:00
|
|
|
*
|
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2017-02-04 11:49:23 +01:00
|
|
|
* @copyright 2014-2017 ManiaControl Team
|
2014-05-04 00:36:20 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
|
|
|
*/
|
|
|
|
class DedimaniaData {
|
|
|
|
/*
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
public $game;
|
|
|
|
public $path;
|
|
|
|
public $packmask;
|
|
|
|
public $serverVersion;
|
|
|
|
public $serverBuild;
|
|
|
|
public $tool;
|
|
|
|
public $version;
|
|
|
|
public $login;
|
|
|
|
public $code;
|
|
|
|
public $sessionId = '';
|
2014-07-20 00:19:54 +02:00
|
|
|
/** @var RecordData[] $records */
|
2014-05-04 00:36:20 +02:00
|
|
|
public $records = array();
|
2014-07-20 00:19:54 +02:00
|
|
|
/** @var DedimaniaPlayer[] $players */
|
2014-10-24 19:59:20 +02:00
|
|
|
public $players = array();
|
2014-05-04 00:36:20 +02:00
|
|
|
public $directoryAccessChecked = false;
|
2014-10-24 19:59:20 +02:00
|
|
|
public $serverMaxRank = 30;
|
2014-05-04 00:36:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new Dedimania Data Model
|
|
|
|
*
|
|
|
|
* @param string $serverLogin
|
|
|
|
* @param string $dedimaniaCode
|
|
|
|
* @param string $path
|
|
|
|
* @param string $packmask
|
|
|
|
* @param Version $serverVersion
|
|
|
|
*/
|
|
|
|
public function __construct($serverLogin, $dedimaniaCode, $path, $packmask, Version $serverVersion) {
|
2014-10-24 19:59:20 +02:00
|
|
|
$this->game = 'TM2';
|
2014-05-04 00:36:20 +02:00
|
|
|
$this->login = $serverLogin;
|
|
|
|
$this->code = $dedimaniaCode;
|
|
|
|
$this->version = ManiaControl::VERSION;
|
2014-10-24 19:59:20 +02:00
|
|
|
$this->tool = 'ManiaControl';
|
2014-05-04 00:36:20 +02:00
|
|
|
$this->path = $path;
|
|
|
|
$this->packmask = $packmask;
|
|
|
|
$this->serverVersion = $serverVersion->version;
|
|
|
|
$this->serverBuild = $serverVersion->build;
|
|
|
|
}
|
|
|
|
|
2014-06-14 15:48:27 +02:00
|
|
|
/**
|
|
|
|
* Sort the Records
|
|
|
|
*/
|
|
|
|
public function sortRecords() {
|
|
|
|
usort($this->records, function (RecordData $first, RecordData $second) {
|
|
|
|
if ($first->best == $second->best) {
|
|
|
|
return ($first->rank - $second->rank);
|
|
|
|
}
|
|
|
|
return ($first->best - $second->best);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-19 23:53:21 +02:00
|
|
|
/**
|
|
|
|
* Update the Record Ranks
|
|
|
|
*/
|
|
|
|
public function updateRanks() {
|
|
|
|
$rank = 1;
|
|
|
|
foreach ($this->records as $record) {
|
|
|
|
$record->rank = $rank;
|
|
|
|
$rank++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 16:40:05 +02:00
|
|
|
/**
|
|
|
|
* Build the Data Array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2014-05-04 00:36:20 +02:00
|
|
|
public function toArray() {
|
|
|
|
$array = array();
|
|
|
|
foreach (get_object_vars($this) as $key => $value) {
|
2014-06-14 14:32:29 +02:00
|
|
|
if ($key === 'records' || $key === 'sessionId' || $key === 'directoryAccessChecked' || $key === 'serverMaxRank' || $key === 'players') {
|
2014-05-04 00:36:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$array[ucfirst($key)] = $value;
|
|
|
|
}
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2014-05-13 16:40:05 +02:00
|
|
|
/**
|
|
|
|
* Get the Number of Records
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2014-05-04 00:36:20 +02:00
|
|
|
public function getRecordCount() {
|
|
|
|
return count($this->records);
|
|
|
|
}
|
|
|
|
|
2017-05-19 23:53:21 +02:00
|
|
|
/**
|
|
|
|
* Checks if a Record is in the array
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function recordsExisting() {
|
|
|
|
return $this->getRecordCount() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsets all existing Records
|
|
|
|
*/
|
|
|
|
public function unsetRecords() {
|
|
|
|
unset($this->records);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a Record by its Index
|
|
|
|
*
|
|
|
|
* @param $index
|
|
|
|
*/
|
|
|
|
public function deleteRecordByIndex($index) {
|
|
|
|
unset($this->records[$index]);
|
|
|
|
}
|
|
|
|
|
2014-05-04 00:36:20 +02:00
|
|
|
/**
|
|
|
|
* Get Max Rank for a certain Player
|
|
|
|
*
|
2014-05-13 16:40:05 +02:00
|
|
|
* @param mixed $login
|
2014-05-04 00:36:20 +02:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getPlayerMaxRank($login) {
|
2014-05-13 16:40:05 +02:00
|
|
|
$login = Player::parseLogin($login);
|
2014-05-04 00:36:20 +02:00
|
|
|
$maxRank = $this->serverMaxRank;
|
|
|
|
foreach ($this->players as $player) {
|
|
|
|
if ($player->login === $login) {
|
|
|
|
if ($player->maxRank > $maxRank) {
|
|
|
|
$maxRank = $player->maxRank;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $maxRank;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-09 17:31:29 +02:00
|
|
|
* Add a Player to the Players array
|
2014-05-04 00:36:20 +02:00
|
|
|
*
|
|
|
|
* @param DedimaniaPlayer $player
|
|
|
|
*/
|
|
|
|
public function addPlayer(DedimaniaPlayer $player) {
|
|
|
|
$this->players[$player->login] = $player;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-09 17:31:29 +02:00
|
|
|
* Remove a Dedimania Player by its login
|
2014-05-04 00:36:20 +02:00
|
|
|
*
|
2014-05-09 17:31:29 +02:00
|
|
|
* @param string $login
|
2014-05-04 00:36:20 +02:00
|
|
|
*/
|
|
|
|
public function removePlayer($login) {
|
|
|
|
unset($this->players[$login]);
|
|
|
|
}
|
2014-10-24 19:59:20 +02:00
|
|
|
}
|