TrackManiaControl/application/core/Server/RankingManager.php

139 lines
3.5 KiB
PHP
Raw Normal View History

2014-02-06 21:18:25 +01:00
<?php
namespace ManiaControl\Server;
2014-02-06 21:18:25 +01:00
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
2014-04-24 23:11:09 +02:00
use ManiaControl\Callbacks\Callbacks;
2014-02-06 21:18:25 +01:00
use ManiaControl\ManiaControl;
2014-09-09 11:33:55 +02:00
use ManiaControl\Maps\Map;
use Maniaplanet\DedicatedServer\Xmlrpc\GameModeException;
2014-02-06 21:18:25 +01:00
/**
* Class managing Rankings
*
2014-05-02 17:50:30 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
2014-04-24 23:11:09 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
2014-02-06 21:18:25 +01:00
class RankingManager implements CallbackListener {
/*
* Private properties
2014-02-06 21:18:25 +01:00
*/
private $rankings = array();
/**
* Construct a new ranking manager instance
2014-02-06 21:18:25 +01:00
*
* @param ManiaControl $maniaControl
2014-02-06 21:18:25 +01:00
*/
2014-03-01 11:11:50 +01:00
public function __construct(ManiaControl $maniaControl) {
2014-02-06 21:18:25 +01:00
$this->maniaControl = $maniaControl;
// Callbacks
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_MODESCRIPTCALLBACK, $this, 'handleCallbacks');
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_MODESCRIPTCALLBACKARRAY, $this, 'handleCallbacks');
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONINIT, $this, 'onInit');
2014-09-09 11:33:55 +02:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::BEGINMAP, $this, 'handleBeginMap');
2014-03-01 11:11:50 +01:00
//TODO won message at end of the map (disable as setting) (and public announce only all %50 (setting) players)
2014-02-06 21:18:25 +01:00
}
/**
* Initialize the Rankings (never call this Method)
2014-02-06 21:18:25 +01:00
*/
public function onInit() {
2014-02-13 18:18:14 +01:00
try {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getClient()->triggerModeScriptEvent('LibXmlRpc_GetRankings', '');
} catch (GameModeException $e) {
2014-02-13 18:18:14 +01:00
}
2014-02-06 21:18:25 +01:00
}
/**
2014-04-24 23:11:09 +02:00
* Handle stats on callbacks (never call this Method)
2014-02-06 21:18:25 +01:00
*
* @param array $callback
*/
public function handleCallbacks(array $callback) {
$callbackName = $callback[1][0];
//TODO not tested in TrackMania
2014-05-02 17:50:30 +02:00
switch ($callbackName) {
2014-02-06 21:18:25 +01:00
case 'updateRankings':
$this->updateRankings($callback[1][1][0]);
break;
case 'endRound':
case 'beginRound':
case 'endMap':
case 'endMap1':
$this->updateRankings($callback[1]);
break;
}
}
2014-09-09 11:33:55 +02:00
/**
* Clear the rankings on the Begin of a Map
*
* @param Map $map
*/
public function handleBeginMap(Map $map) {
$this->rankings = array();
}
2014-02-06 21:18:25 +01:00
/**
2014-04-24 23:11:09 +02:00
* Update Game Rankings (never call this Method)
2014-02-06 21:18:25 +01:00
*
* @param string $data
2014-02-06 21:18:25 +01:00
*/
2014-04-24 23:11:09 +02:00
public function updateRankings($data) {
2014-02-23 23:27:30 +01:00
if (!is_string($data)) {
return;
}
2014-05-11 21:29:44 +02:00
//TODO in legacy mode, no data is in parameter -> fetch via method getCurrentRanking
2014-02-06 21:18:25 +01:00
$scores = explode(';', $data);
2014-05-02 17:50:30 +02:00
foreach ($scores as $player) {
2014-02-06 21:18:25 +01:00
if (strpos($player, ':') !== false) {
$tmp = explode(':', $player);
$this->rankings[$tmp[0]] = $tmp[1];
}
}
array_multisort($this->rankings, SORT_DESC, SORT_NUMERIC);
//TODO if Local Records activated-> sort asc
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::RANKINGSUPDATED, $this->getRankings());
2014-02-06 21:18:25 +01:00
}
2014-05-02 17:50:30 +02:00
/**
* Get Rankings
*
* @return array
*/
public function getRankings() {
return $this->rankings;
}
2014-02-06 21:18:25 +01:00
/**
* Get the Current Leading Players (as Login Array)
*
* @return array|null
*/
public function getLeaders() {
$leaders = array();
$prev = -1;
2014-05-02 17:50:30 +02:00
foreach ($this->rankings as $score) {
if ($prev !== -1 && $prev < $score) {
2014-02-06 21:18:25 +01:00
return $leaders;
}
2014-07-17 20:01:48 +02:00
// FIXME: $leader doesn't exist
2014-02-06 21:18:25 +01:00
array_push($leaders, $leader);
$prev = $score;
}
return null;
}
2014-04-24 23:11:09 +02:00
public function getPlayerRanking() {
//TODO complete this
}
}