TrackManiaControl/plugins/MCTeam/Dedimania/DedimaniaData.php

173 lines
3.6 KiB
PHP
Raw Normal View History

<?php
namespace MCTeam\Dedimania;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use Maniaplanet\DedicatedServer\Structures\Version;
/**
2014-05-24 20:19:41 +02:00
* ManiaControl Dedimania Plugin Data Structure
*
* @author ManiaControl Team <mail@maniacontrol.com>
2017-02-04 11:49:23 +01:00
* @copyright 2014-2017 ManiaControl Team
* @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 */
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();
public $directoryAccessChecked = false;
2014-10-24 19:59:20 +02:00
public $serverMaxRank = 30;
/**
* 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';
$this->login = $serverLogin;
$this->code = $dedimaniaCode;
$this->version = ManiaControl::VERSION;
2014-10-24 19:59:20 +02:00
$this->tool = 'ManiaControl';
$this->path = $path;
$this->packmask = $packmask;
$this->serverVersion = $serverVersion->version;
$this->serverBuild = $serverVersion->build;
}
2017-05-22 16:31:09 +02:00
public function sessionIdSet(){
return $this->sessionId != '';
}
/**
* 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++;
}
}
/**
* Build the Data Array
*
* @return array
*/
public function toArray() {
$array = array();
foreach (get_object_vars($this) as $key => $value) {
if ($key === 'records' || $key === 'sessionId' || $key === 'directoryAccessChecked' || $key === 'serverMaxRank' || $key === 'players') {
continue;
}
$array[ucfirst($key)] = $value;
}
return $array;
}
/**
* Get the Number of Records
*
* @return int
*/
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() {
2017-05-20 10:10:39 +02:00
$this->records = array();
2017-05-19 23:53:21 +02:00
}
/**
* Deletes a Record by its Index
*
* @param $index
*/
public function deleteRecordByIndex($index) {
unset($this->records[$index]);
}
/**
* Get Max Rank for a certain Player
*
* @param mixed $login
* @return int
*/
public function getPlayerMaxRank($login) {
$login = Player::parseLogin($login);
$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
*
* @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-09 17:31:29 +02:00
* @param string $login
*/
public function removePlayer($login) {
unset($this->players[$login]);
}
2014-10-24 19:59:20 +02:00
}