2014-02-06 21:18:25 +01:00
|
|
|
<?php
|
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
namespace ManiaControl\Server;
|
2014-02-06 21:18:25 +01:00
|
|
|
|
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
|
|
|
use ManiaControl\ManiaControl;
|
2014-02-19 17:16:24 +01:00
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
|
2014-04-20 15:29:27 +02:00
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\NotInScriptModeException;
|
2014-02-06 21:18:25 +01:00
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/**
|
|
|
|
* Class managing Rankings
|
|
|
|
*
|
|
|
|
* @author kremsy
|
|
|
|
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
|
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
|
|
|
*/
|
2014-02-06 21:18:25 +01:00
|
|
|
class RankingManager implements CallbackListener {
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-02-06 21:18:25 +01:00
|
|
|
* Private Properties
|
|
|
|
*/
|
|
|
|
private $rankings = array();
|
|
|
|
|
|
|
|
/**
|
2014-04-12 12:14:37 +02:00
|
|
|
* Get Rankings
|
|
|
|
*
|
2014-02-06 21:18:25 +01:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getRankings() {
|
|
|
|
return $this->rankings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct player manager
|
|
|
|
*
|
|
|
|
* @param \ManiaControl\ManiaControl $maniaControl
|
|
|
|
*/
|
2014-03-01 11:11:50 +01:00
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
2014-02-06 21:18:25 +01:00
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
|
|
|
|
//Register Callbacks
|
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_MODESCRIPTCALLBACK, $this, 'handleCallbacks');
|
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_MODESCRIPTCALLBACKARRAY, $this, 'handleCallbacks');
|
2014-02-19 12:53:06 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_ONINIT, $this, 'onInit');
|
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
|
|
|
|
*/
|
|
|
|
public function onInit() {
|
2014-02-13 18:18:14 +01:00
|
|
|
try {
|
|
|
|
$this->maniaControl->client->triggerModeScriptEvent('LibXmlRpc_GetRankings', '');
|
2014-04-20 15:29:27 +02:00
|
|
|
} catch(NotInScriptModeException $e) {
|
2014-02-13 18:18:14 +01:00
|
|
|
}
|
2014-02-06 21:18:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle stats on callbacks
|
|
|
|
*
|
|
|
|
* @param array $callback
|
|
|
|
*/
|
|
|
|
public function handleCallbacks(array $callback) {
|
|
|
|
$callbackName = $callback[1][0];
|
|
|
|
|
|
|
|
//TODO not tested in TrackMania
|
|
|
|
switch($callbackName) {
|
|
|
|
case 'LibXmlRpc_Rankings':
|
|
|
|
case 'updateRankings':
|
|
|
|
$this->updateRankings($callback[1][1][0]);
|
|
|
|
break;
|
|
|
|
case 'endRound':
|
|
|
|
case 'beginRound':
|
|
|
|
case 'endMap':
|
|
|
|
case 'endMap1':
|
|
|
|
$this->updateRankings($callback[1]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update Game Rankings
|
|
|
|
*
|
|
|
|
* @param $data
|
|
|
|
*/
|
|
|
|
private function updateRankings($data) {
|
2014-02-23 23:27:30 +01:00
|
|
|
if (!is_string($data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-06 21:18:25 +01:00
|
|
|
$scores = explode(';', $data);
|
|
|
|
foreach($scores as $player) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Current Leading Players (as Login Array)
|
|
|
|
*
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
public function getLeaders() {
|
|
|
|
$leaders = array();
|
|
|
|
$prev = -1;
|
2014-02-13 00:46:41 +01:00
|
|
|
foreach($this->rankings as $score) {
|
2014-02-06 21:18:25 +01:00
|
|
|
if ($prev != -1 && $prev < $score) {
|
|
|
|
return $leaders;
|
|
|
|
}
|
|
|
|
array_push($leaders, $leader);
|
|
|
|
$prev = $score;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|