TrackManiaControl/application/plugins/Dedimania/Dedimania.php

1026 lines
29 KiB
PHP
Raw Normal View History

2014-02-14 19:22:11 +01:00
<?php
2014-02-15 01:03:25 +01:00
namespace Dedimania;
2014-02-14 19:22:11 +01:00
2014-02-20 12:20:35 +01:00
use FML\Controls\Control;
use FML\Controls\Frame;
use FML\Controls\Label;
use FML\Controls\Quad;
use FML\ManiaLink;
2014-02-14 19:22:11 +01:00
use ManiaControl\Callbacks\CallbackListener;
2014-02-20 11:04:52 +01:00
use ManiaControl\Callbacks\CallbackManager;
2014-02-14 19:22:11 +01:00
use ManiaControl\Callbacks\TimerListener;
2014-02-19 15:16:45 +01:00
use ManiaControl\Formatter;
2014-02-14 19:22:11 +01:00
use ManiaControl\ManiaControl;
2014-02-19 15:16:45 +01:00
use ManiaControl\Maps\MapManager;
2014-02-16 22:38:14 +01:00
use ManiaControl\Players\Player;
2014-02-19 10:43:37 +01:00
use ManiaControl\Players\PlayerManager;
2014-02-14 19:22:11 +01:00
use ManiaControl\Plugins\Plugin;
class Dedimania implements CallbackListener, TimerListener, Plugin {
2014-02-15 01:03:25 +01:00
/**
* Constants
*/
2014-02-25 18:59:00 +01:00
const ID = 8;
2014-02-16 22:38:14 +01:00
const VERSION = 0.1;
const MLID_DEDIMANIA = 'Dedimania.ManialinkId';
2014-02-15 01:03:25 +01:00
const XMLRPC_MULTICALL = 'system.multicall';
const DEDIMANIA_URL = 'http://dedimania.net:8081/Dedimania';
const DEDIMANIA_OPENSESSION = 'dedimania.OpenSession';
const DEDIMANIA_CHECKSESSION = 'dedimania.CheckSession';
const DEDIMANIA_GETRECORDS = 'dedimania.GetChallengeRecords';
const DEDIMANIA_PLAYERCONNECT = 'dedimania.PlayerConnect';
const DEDIMANIA_PLAYERDISCONNECT = 'dedimania.PlayerDisconnect';
const DEDIMANIA_UPDATESERVERPLAYERS = 'dedimania.UpdateServerPlayers';
const DEDIMANIA_SETCHALLENGETIMES = 'dedimania.SetChallengeTimes';
const DEDIMANIA_WARNINGSANDTTR2 = 'dedimania.WarningsAndTTR2';
2014-02-20 12:20:35 +01:00
const SETTING_WIDGET_TITLE = 'Widget Title';
const SETTING_WIDGET_POSX = 'Widget Position: X';
const SETTING_WIDGET_POSY = 'Widget Position: Y';
const SETTING_WIDGET_WIDTH = 'Widget Width';
const SETTING_WIDGET_LINESCOUNT = 'Widget Displayed Lines Count';
const SETTING_WIDGET_LINEHEIGHT = 'Widget Line Height';
2014-02-24 11:08:15 +01:00
const SETTING_DEDIMANIA_CODE = '$l[http://dedimania.net/tm2stats/?do=register]Dedimania Code for ';
2014-02-15 01:03:25 +01:00
2014-02-14 19:22:11 +01:00
/**
* Private Properties
*/
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
2014-02-15 01:03:25 +01:00
/** @var DedimaniaData $dedimaniaData */
private $dedimaniaData = null;
2014-02-16 22:38:14 +01:00
private $updateManialink = false;
2014-02-20 11:04:52 +01:00
private $checkpoints = array();
2014-02-20 12:20:35 +01:00
private $init = false;
2014-02-14 19:22:11 +01:00
/**
* Prepares the Plugin
*
* @param ManiaControl $maniaControl
* @return mixed
*/
public static function prepare(ManiaControl $maniaControl) {
2014-02-23 15:28:53 +01:00
$servers = $maniaControl->server->getAllServers();
foreach($servers as $server) {
2014-02-24 11:08:15 +01:00
$maniaControl->settingManager->initSetting(get_class(), self::SETTING_DEDIMANIA_CODE . $server->login . '$l', '');
2014-02-23 15:28:53 +01:00
}
2014-02-14 19:22:11 +01:00
}
/**
* Load the plugin
*
* @param \ManiaControl\ManiaControl $maniaControl
* @return bool
*/
public function load(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-02-15 01:03:25 +01:00
2014-02-23 13:15:28 +01:00
if (!extension_loaded('xmlrpc')) {
throw new \Exception("You need to activate the PHP extension xmlrpc to run this Plugin!");
}
2014-02-20 12:20:35 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_TITLE, 'Dedimania');
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_POSX, -139);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_POSY, 7);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_WIDTH, 40);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_LINEHEIGHT, 4);
2014-02-20 13:06:11 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_LINESCOUNT, 12);
2014-02-15 01:03:25 +01:00
2014-02-19 15:16:45 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_BEGINMAP, $this, 'handleBeginMap');
2014-02-20 12:20:35 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_ENDMAP, $this, 'handleMapEnd');
2014-02-19 15:16:45 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect');
2014-02-23 13:15:28 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECT, $this, 'handlePlayerDisconnect');
2014-02-20 11:04:52 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_TM_PLAYERCHECKPOINT, $this, 'handlePlayerCheckpoint');
2014-02-20 12:20:35 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_TM_PLAYERFINISH, $this, 'handlePlayerFinished');
2014-02-16 22:38:14 +01:00
$this->maniaControl->timerManager->registerTimerListening($this, 'updateEverySecond', 1000);
$this->maniaControl->timerManager->registerTimerListening($this, 'handleEveryMinute', 1000 * 60);
2014-02-20 11:04:52 +01:00
$this->maniaControl->timerManager->registerTimerListening($this, 'updatePlayerList', 1000 * 60 * 3);
2014-02-15 01:03:25 +01:00
// Open session
2014-02-20 12:20:35 +01:00
$serverInfo = $this->maniaControl->server->getInfo();
$serverVersion = $this->maniaControl->client->getVersion();
2014-03-14 17:57:50 +01:00
$packMask = substr($this->maniaControl->server->titleId, 2);
2014-02-20 12:20:35 +01:00
2014-02-24 11:08:15 +01:00
$dedimaniaCode = $this->maniaControl->settingManager->getSetting($this, self::SETTING_DEDIMANIA_CODE . $serverInfo->login . '$l');
2014-02-20 12:20:35 +01:00
if ($dedimaniaCode == '') {
2014-02-23 15:28:53 +01:00
throw new \Exception("No Dedimania Code Specified, check the settings!");
2014-02-20 12:20:35 +01:00
}
$this->dedimaniaData = new DedimaniaData($serverInfo->login, $dedimaniaCode, $serverInfo->path, $packMask, $serverVersion);
2014-02-15 01:03:25 +01:00
2014-02-16 22:38:14 +01:00
$this->openDedimaniaSession();
}
2014-02-15 01:03:25 +01:00
2014-02-16 22:38:14 +01:00
/**
* Opens the Dedimania Session
*/
private function openDedimaniaSession() {
$content = $this->encode_request(self::DEDIMANIA_OPENSESSION, array($this->dedimaniaData->toArray()));
2014-02-15 01:03:25 +01:00
2014-02-16 22:38:14 +01:00
$this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) {
$this->maniaControl->log("Try to connect on Dedimania");
2014-02-15 01:03:25 +01:00
2014-02-18 23:29:13 +01:00
if ($error != '') {
$this->maniaControl->log("Dedimania Error: " . $error);
}
$data = $this->decode($data);
2014-02-16 22:38:14 +01:00
if (is_array($data)) {
foreach($data as $index => $methodResponse) {
if (xmlrpc_is_fault($methodResponse)) {
2014-02-20 12:20:35 +01:00
$this->handleXmlRpcFault($methodResponse, self::DEDIMANIA_OPENSESSION);
2014-02-16 22:38:14 +01:00
} else if ($index <= 0) {
2014-02-19 15:16:45 +01:00
$responseData = $methodResponse[0];
2014-02-16 22:38:14 +01:00
$this->dedimaniaData->sessionId = $responseData['SessionId'];
if ($this->dedimaniaData->sessionId != '') {
$this->maniaControl->log("Dedimania connection successfully established.");
$this->fetchDedimaniaRecords();
2014-02-20 12:20:35 +01:00
$this->init = true;
2014-02-16 22:38:14 +01:00
} else {
$this->maniaControl->log("Error while opening Dedimania Connection");
}
}
}
}
2014-02-18 23:29:13 +01:00
}, $content, true);
2014-02-16 22:38:14 +01:00
}
2014-02-15 01:03:25 +01:00
2014-02-16 22:38:14 +01:00
/**
* Handle 1Second callback
*/
public function updateEverySecond($time) {
2014-02-20 12:20:35 +01:00
if (!$this->updateManialink) {
return;
}
if (!$this->dedimaniaData->records) {
return;
}
2014-02-20 13:06:11 +01:00
$this->updateManialink = false;
2014-02-20 12:20:35 +01:00
$manialink = $this->buildManialink();
$this->maniaControl->manialinkManager->sendManialink($manialink);
2014-02-16 22:38:14 +01:00
}
2014-02-15 01:03:25 +01:00
2014-02-16 22:38:14 +01:00
/**
* Check if the session is alive every minute
*
* @param null $callback
*/
public function handleEveryMinute($callback = null) {
2014-02-20 13:06:11 +01:00
if (!$this->init) {
2014-02-20 12:20:35 +01:00
return;
2014-02-20 13:06:11 +01:00
}
2014-02-19 15:16:45 +01:00
$this->checkDedimaniaSession();
}
/**
* Handle PlayerConnect callback
*
2014-02-20 11:04:52 +01:00
* @param \ManiaControl\Players\Player $player
2014-02-19 15:16:45 +01:00
*/
2014-02-20 11:04:52 +01:00
public function handlePlayerConnect(Player $player) {
2014-02-19 15:16:45 +01:00
// Send Dedimania request
2014-02-24 20:20:17 +01:00
$data = array($this->dedimaniaData->sessionId, $player->login, $player->rawNickname, $player->path, $player->isSpectator);
2014-02-19 15:16:45 +01:00
$content = $this->encode_request(self::DEDIMANIA_PLAYERCONNECT, $data);
2014-02-20 12:20:35 +01:00
$this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) use (&$player) {
2014-02-19 15:16:45 +01:00
if ($error != '') {
$this->maniaControl->log("Dedimania Error: " . $error);
}
$data = $this->decode($data);
if (is_array($data)) {
2014-02-23 13:15:28 +01:00
foreach($data as $index => $methodResponse) {
2014-02-19 15:16:45 +01:00
if (xmlrpc_is_fault($methodResponse)) {
2014-02-20 12:20:35 +01:00
$this->handleXmlRpcFault($methodResponse, self::DEDIMANIA_PLAYERCONNECT);
2014-02-23 13:15:28 +01:00
} else if ($index <= 0) {
$responseData = $methodResponse[0];
$this->dedimaniaData->addPlayer(new DedimaniaPlayer($responseData));
2014-03-01 13:13:17 +01:00
//Fetch records if he is the first who joined the server
if (count($this->maniaControl->playerManager->getPlayers()) == 1) {
$this->fetchDedimaniaRecords(true);
}
2014-02-19 15:16:45 +01:00
}
2014-03-02 13:55:58 +01:00
$manialink = $this->buildManialink();
$this->maniaControl->manialinkManager->sendManialink($manialink, $player->login);
2014-02-19 15:16:45 +01:00
}
} else {
if (!$data) {
trigger_error('XmlRpc Error.');
var_dump($data);
}
}
return true;
}, $content, true);
}
/**
* Handle PlayerDisconnect callback
*
2014-02-20 11:04:52 +01:00
* @param \ManiaControl\Players\Player $player
2014-02-19 15:16:45 +01:00
*/
2014-02-20 11:04:52 +01:00
public function handlePlayerDisconnect(Player $player) {
2014-02-23 13:15:28 +01:00
$this->dedimaniaData->removePlayer($player->login);
2014-02-19 15:16:45 +01:00
// Send Dedimania request
$data = array($this->dedimaniaData->sessionId, $player->login, '');
$content = $this->encode_request(self::DEDIMANIA_PLAYERDISCONNECT, $data);
$this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) {
if ($error != '') {
$this->maniaControl->log("Dedimania Error: " . $error);
}
$data = $this->decode($data);
if (is_array($data)) {
foreach($data as $methodResponse) {
if (xmlrpc_is_fault($methodResponse)) {
2014-02-20 12:20:35 +01:00
$this->handleXmlRpcFault($methodResponse, self::DEDIMANIA_PLAYERDISCONNECT);
2014-02-19 15:16:45 +01:00
}
}
} else {
if (!$data) {
trigger_error('XmlRpc Error.');
var_dump($data);
}
}
return true;
}, $content, true);
}
/**
* Handle Begin Map
*
* @param $callback
*/
public function handleBeginMap($callback) {
2014-02-27 13:16:34 +01:00
unset($this->dedimaniaData->records);
2014-02-19 15:16:45 +01:00
$this->fetchDedimaniaRecords(true);
2014-02-20 11:04:52 +01:00
}
2014-02-20 12:20:35 +01:00
2014-02-20 11:04:52 +01:00
/**
* Handle EndMap callback
*
* @param $callback
*/
public function handleMapEnd($callback) {
2014-02-24 22:43:01 +01:00
if (!$this->dedimaniaData || !$this->dedimaniaData->records) {
2014-02-20 12:20:35 +01:00
return;
2014-02-20 13:06:11 +01:00
}
2014-02-20 12:20:35 +01:00
2014-02-20 11:04:52 +01:00
// Send dedimania records
$gameMode = $this->getGameModeString();
$times = array();
$replays = array();
2014-02-23 13:15:28 +01:00
foreach($this->dedimaniaData->records as $record) {
/** @var RecordData $record */
2014-03-01 13:13:17 +01:00
if ($record->rank > $this->dedimaniaData->serverMaxRank) {
2014-02-27 13:16:34 +01:00
break;
}
2014-02-23 13:15:28 +01:00
if ($record->newRecord == false) {
2014-02-20 11:04:52 +01:00
continue;
}
2014-02-23 13:15:28 +01:00
array_push($times, array('Login' => $record->login, 'Best' => $record->best, 'Checks' => $record->checkpoints));
2014-02-25 12:08:18 +01:00
if (!isset($replays['VReplay'])) {
$replays['VReplay'] = $record->vReplay;
2014-02-23 15:37:04 +01:00
}
if (!isset($replays['Top1GReplay'])) {
2014-02-25 12:08:18 +01:00
$replays['Top1GReplay'] = $record->top1GReplay;
2014-02-23 15:37:04 +01:00
}
if (!isset($replays['VReplayChecks'])) {
$replays['VReplayChecks'] = '';
// TODO: VReplayChecks
}
2014-02-20 11:04:52 +01:00
}
2014-02-20 17:59:09 +01:00
2014-02-21 16:07:36 +01:00
xmlrpc_set_type($replays['VReplay'], 'base64');
xmlrpc_set_type($replays['Top1GReplay'], 'base64');
2014-02-20 17:59:09 +01:00
//var_dump($replays);
$data = array($this->dedimaniaData->sessionId, $this->getMapInfo(), $gameMode, $times, $replays);
2014-02-20 13:49:33 +01:00
//var_dump($data);
2014-02-20 11:04:52 +01:00
$content = $this->encode_request(self::DEDIMANIA_SETCHALLENGETIMES, $data);
$this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) {
if ($error != '') {
$this->maniaControl->log("Dedimania Error: " . $error);
}
2014-02-25 18:34:35 +01:00
//var_dump($data);
2014-02-20 11:04:52 +01:00
$data = $this->decode($data);
2014-02-20 17:59:09 +01:00
//var_dump($data);
2014-02-20 11:04:52 +01:00
if (is_array($data)) {
foreach($data as $index => $methodResponse) {
if (xmlrpc_is_fault($methodResponse)) {
2014-02-20 12:20:35 +01:00
$this->handleXmlRpcFault($methodResponse, self::DEDIMANIA_SETCHALLENGETIMES);
2014-02-20 11:04:52 +01:00
} else {
if ($index <= 0) {
// Called method response
$responseData = $methodResponse[0];
if (!$responseData) {
trigger_error("Records Plugin: Submitting dedimania records failed.");
}
continue;
}
// Warnings and TTR
$errors = $methodResponse[0]['methods'][0]['errors'];
if ($errors) {
2014-02-27 13:16:34 +01:00
//TODO just temporary
$this->maniaControl->errorHandler->triggerDebugNotice("Dedimania Notice on SetChallengeTimes: " . $errors);
//trigger_error($errors);
2014-02-20 11:04:52 +01:00
}
}
}
}
2014-02-24 09:49:29 +01:00
}, $content, false);
2014-02-19 15:16:45 +01:00
}
2014-02-20 11:04:52 +01:00
/**
* Update the Playerlist every 3 Minutes
*
* @param $callback
*/
2014-02-19 15:16:45 +01:00
public function updatePlayerList($callback) {
$serverInfo = $this->getServerInfo();
$playerList = $this->getPlayerList();
$votesInfo = $this->getVotesInfo();
2014-02-20 12:20:35 +01:00
if (!$serverInfo || !$votesInfo || !$playerList || !isset($this->dedimaniaData) || $this->dedimaniaData->sessionId == '') {
2014-02-19 15:16:45 +01:00
return;
}
// Send Dedimania request
$data = array($this->dedimaniaData->sessionId, $serverInfo, $votesInfo, $playerList);
$content = $this->encode_request(self::DEDIMANIA_UPDATESERVERPLAYERS, $data);
$this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) {
if ($error != '') {
$this->maniaControl->log("Dedimania Error: " . $error);
}
$data = $this->decode($data);
if (is_array($data)) {
foreach($data as $methodResponse) {
if (xmlrpc_is_fault($methodResponse)) {
2014-02-20 12:20:35 +01:00
$this->handleXmlRpcFault($methodResponse, self::DEDIMANIA_UPDATESERVERPLAYERS);
2014-02-19 15:16:45 +01:00
}
}
} else {
if (!$data) {
trigger_error('XmlRpc Error.');
var_dump($data);
}
}
return true;
}, $content, true);
}
/**
* Handle PlayerCheckpoint callback
2014-02-20 12:20:35 +01:00
*
* @param $callback
2014-02-19 15:16:45 +01:00
*/
public function handlePlayerCheckpoint($callback) {
2014-02-20 12:20:35 +01:00
$data = $callback[1];
$login = $data[1];
$time = $data[2];
//$lap = $data[3];
2014-02-20 11:04:52 +01:00
$cpIndex = $data[4];
if (!isset($this->checkpoints[$login]) || $cpIndex <= 0) {
$this->checkpoints[$login] = array();
}
$this->checkpoints[$login][$cpIndex] = $time;
2014-02-19 15:16:45 +01:00
}
2014-02-20 12:20:35 +01:00
/**
* Plyer finished callback
*
* @param $callback
*/
2014-02-19 15:16:45 +01:00
public function handlePlayerFinished($callback) {
2014-02-20 13:06:11 +01:00
//var_dump($callback);
2014-02-19 15:16:45 +01:00
$data = $callback[1];
if ($data[0] <= 0 || $data[2] <= 0) {
return;
}
$login = $data[1];
$time = $data[2];
$map = $this->maniaControl->mapManager->getCurrentMap();
if (!$map) {
return;
}
$oldRecord = $this->getDedimaniaRecord($login);
2014-02-23 13:15:28 +01:00
if ($oldRecord->nullRecord || $oldRecord && $oldRecord->best > $time) {
2014-02-20 13:49:33 +01:00
$player = $this->maniaControl->playerManager->getPlayer($login);
2014-02-23 13:15:28 +01:00
2014-02-20 13:49:33 +01:00
// Save time
2014-02-23 13:15:28 +01:00
$newRecord = new RecordData(null);
$newRecord->constructNewRecord($login, $player->nickname, $data[2], $this->getCheckpoints($login), true);
2014-02-20 13:49:33 +01:00
if ($this->insertDedimaniaRecord($newRecord, $oldRecord)) {
2014-02-27 23:09:19 +01:00
2014-02-20 13:49:33 +01:00
// Get newly saved record
2014-02-23 13:15:28 +01:00
foreach($this->dedimaniaData->records as &$record) {
/** @var RecordData $record */
if ($record->login !== $newRecord->login) {
2014-02-20 13:49:33 +01:00
continue;
}
$newRecord = $record;
break;
}
2014-02-20 13:06:11 +01:00
2014-02-20 13:49:33 +01:00
// Announce record
2014-02-23 13:15:28 +01:00
if ($oldRecord->nullRecord || $newRecord->rank < $oldRecord->rank) {
2014-02-20 13:49:33 +01:00
// Gained rank
$improvement = 'gained the';
} else {
// Only improved time
$improvement = 'improved her/his';
}
2014-02-23 13:15:28 +01:00
$message = '$<' . $player->nickname . '$> ' . $improvement . ' $<$o' . $newRecord->rank . '.$> Dedimania Record: ' . Formatter::formatTime($newRecord->best);
2014-02-20 13:49:33 +01:00
$this->maniaControl->chat->sendInformation($message);
2014-02-20 13:06:11 +01:00
2014-02-20 13:49:33 +01:00
$this->updateManialink = true;
2014-02-20 13:06:11 +01:00
}
}
2014-02-16 22:38:14 +01:00
}
2014-02-15 01:03:25 +01:00
2014-02-16 22:38:14 +01:00
/**
* Fetch Dedimania Records
*
* @param bool $reset
*/
private function fetchDedimaniaRecords($reset = true) {
2014-02-24 00:05:05 +01:00
if (!$this->dedimaniaData || $this->dedimaniaData->sessionId == '') {
2014-02-16 22:38:14 +01:00
return false;
}
2014-02-19 15:16:45 +01:00
// Reset records
2014-02-16 22:38:14 +01:00
if ($reset) {
$this->dedimaniaData->records = array();
}
2014-02-27 13:16:34 +01:00
2014-02-16 22:38:14 +01:00
$serverInfo = $this->getServerInfo();
$playerInfo = $this->getPlayerList();
$mapInfo = $this->getMapInfo();
$gameMode = $this->getGameModeString();
if (!$serverInfo || !$playerInfo || !$mapInfo || !$gameMode) {
return false;
}
2014-02-27 13:16:34 +01:00
2014-02-16 22:38:14 +01:00
$data = array($this->dedimaniaData->sessionId, $mapInfo, $gameMode, $serverInfo, $playerInfo);
$content = $this->encode_request(self::DEDIMANIA_GETRECORDS, $data);
$this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) {
2014-02-18 23:29:13 +01:00
if ($error != '') {
$this->maniaControl->log("Dedimania Error: " . $error);
}
2014-02-15 01:03:25 +01:00
2014-02-18 23:29:13 +01:00
$data = $this->decode($data);
2014-02-20 13:06:11 +01:00
2014-02-16 22:38:14 +01:00
if (is_array($data)) {
foreach($data as $index => $methodResponse) {
if (xmlrpc_is_fault($methodResponse)) {
2014-02-20 12:20:35 +01:00
$this->handleXmlRpcFault($methodResponse, self::DEDIMANIA_GETRECORDS);
2014-02-16 22:38:14 +01:00
return false;
} else if ($index <= 0) {
2014-02-23 13:15:28 +01:00
$responseData = $methodResponse[0];
$this->dedimaniaData->serverMaxRank = $responseData['ServerMaxRank'];
foreach($responseData['Players'] as $player) {
$dediPlayer = new DedimaniaPlayer(null);
$dediPlayer->constructNewPlayer($player['Login'], $player['MaxRank']);
$this->dedimaniaData->addPlayer($dediPlayer);
}
foreach($responseData['Records'] as $key => $record) {
$this->dedimaniaData->records[$key] = new RecordData($record);
}
2014-02-16 22:38:14 +01:00
}
}
}
$this->updateManialink = true;
return true;
2014-02-18 23:29:13 +01:00
}, $content, true);
2014-02-15 01:03:25 +01:00
2014-02-16 22:38:14 +01:00
return true;
}
/**
* Checks If a Dedimania Session exists, if not create a new oen
*/
private function checkDedimaniaSession() {
if ($this->dedimaniaData->sessionId == '') {
$this->openDedimaniaSession();
return;
}
$content = $this->encode_request(self::DEDIMANIA_CHECKSESSION, array($this->dedimaniaData->sessionId));
$this->maniaControl->fileReader->postData(self::DEDIMANIA_URL, function ($data, $error) {
2014-02-18 23:29:13 +01:00
if ($error != '') {
$this->maniaControl->log("Dedimania Error: " . $error);
}
2014-02-16 22:38:14 +01:00
2014-02-18 23:29:13 +01:00
$data = $this->decode($data);
2014-02-16 22:38:14 +01:00
if (is_array($data)) {
foreach($data as $methodResponse) {
if (xmlrpc_is_fault($methodResponse)) {
2014-02-20 12:20:35 +01:00
$this->handleXmlRpcFault($methodResponse, self::DEDIMANIA_CHECKSESSION);
2014-02-16 22:38:14 +01:00
} else {
$responseData = $methodResponse[0];
if (is_bool($responseData)) {
if (!$responseData) {
$this->openDedimaniaSession();
}
}
}
}
}
2014-02-18 23:29:13 +01:00
}, $content, true);
2014-02-16 22:38:14 +01:00
return;
}
2014-02-19 15:16:45 +01:00
/**
* Inserts the given new Dedimania record at the proper position
*
* @param array $newRecord
* @return bool
*/
2014-02-23 13:15:28 +01:00
private function insertDedimaniaRecord(RecordData &$newRecord, RecordData $oldRecord) {
2014-02-27 13:16:34 +01:00
if ($newRecord->nullRecord) {
2014-02-20 11:04:52 +01:00
return false;
}
2014-02-19 15:16:45 +01:00
2014-02-20 12:20:35 +01:00
$insert = false;
2014-02-19 15:16:45 +01:00
2014-02-20 11:04:52 +01:00
// Get max possible rank
2014-02-23 13:15:28 +01:00
$maxRank = $this->dedimaniaData->getPlayerMaxRank($newRecord->login);
2014-02-20 11:04:52 +01:00
2014-02-19 15:16:45 +01:00
// Loop through existing records
2014-02-23 13:15:28 +01:00
foreach($this->dedimaniaData->records as $key => &$record) {
/** @var RecordData $record */
2014-02-25 12:08:18 +01:00
if ($record->rank > $maxRank) {
// Max rank reached
return false;
}
2014-02-23 13:15:28 +01:00
if ($record->login === $newRecord->login) {
2014-02-19 15:16:45 +01:00
// Old record of the same player
2014-02-23 13:15:28 +01:00
if ($record->best <= $newRecord->best) {
2014-02-19 15:16:45 +01:00
// It's better - Do nothing
return false;
}
// Replace old record
2014-02-23 13:15:28 +01:00
unset($this->dedimaniaData->records[$key]);
2014-02-19 15:16:45 +01:00
$insert = true;
break;
}
// Other player's record
2014-02-23 13:15:28 +01:00
if ($record->best <= $newRecord->best) {
2014-02-19 15:16:45 +01:00
// It's better - Skip
continue;
}
// New record is better - Insert it
$insert = true;
if ($oldRecord) {
// Remove old record
2014-02-23 13:15:28 +01:00
foreach($this->dedimaniaData->records as $key2 => $record2) {
/** @var RecordData $record2 */
if ($record2->login !== $oldRecord->login) {
2014-02-20 11:04:52 +01:00
continue;
2014-02-20 13:06:11 +01:00
}
2014-02-23 13:15:28 +01:00
unset($this->dedimaniaData->records[$key2]);
2014-02-19 15:16:45 +01:00
break;
}
}
break;
}
2014-02-23 13:15:28 +01:00
if (!$insert && count($this->dedimaniaData->records) < $maxRank) {
2014-02-19 15:16:45 +01:00
// Records list not full - Append new record
$insert = true;
}
if ($insert) {
// Insert new record
2014-02-23 13:15:28 +01:00
array_push($this->dedimaniaData->records, $newRecord);
2014-02-19 15:16:45 +01:00
// Update ranks
2014-02-20 11:04:52 +01:00
$this->updateDedimaniaRecordRanks();
2014-02-19 15:16:45 +01:00
// Save replays
2014-02-23 13:15:28 +01:00
foreach($this->dedimaniaData->records as &$record) {
if ($record->login !== $newRecord->login) {
2014-02-20 11:04:52 +01:00
continue;
2014-02-20 13:06:11 +01:00
}
2014-02-20 11:04:52 +01:00
$this->setRecordReplays($record);
2014-02-19 15:16:45 +01:00
break;
}
// Record inserted
return true;
}
// No new record
return false;
}
2014-02-20 11:04:52 +01:00
/**
* Update the sorting and the ranks of all dedimania records
*/
private function updateDedimaniaRecordRanks() {
2014-02-23 13:15:28 +01:00
if ($this->dedimaniaData->getRecordCount() == 0) {
2014-02-20 11:04:52 +01:00
return;
2014-02-20 13:06:11 +01:00
}
2014-02-23 13:15:28 +01:00
//TODO move into class dedimania data
2014-02-20 11:04:52 +01:00
// Sort records
2014-02-23 13:15:28 +01:00
usort($this->dedimaniaData->records, array($this, 'compareRecords'));
2014-02-20 11:04:52 +01:00
// Update ranks
$rank = 1;
2014-02-23 13:15:28 +01:00
foreach($this->dedimaniaData->records as &$record) {
/** @var RecordData $record */
$record->rank = $rank;
2014-02-20 11:04:52 +01:00
$rank++;
}
}
/**
* Compare function for sorting dedimania records
*
2014-02-23 13:15:28 +01:00
* @param \Dedimania\RecordData $first
* @param \Dedimania\RecordData $second
2014-02-20 11:04:52 +01:00
* @return int
*/
2014-02-23 13:15:28 +01:00
private function compareRecords(RecordData $first, RecordData $second) {
//TODO move into class dedimania data
if ($first->best < $second->best) {
2014-02-20 11:04:52 +01:00
return -1;
2014-02-23 13:15:28 +01:00
} else if ($first->best > $second->best) {
2014-02-20 11:04:52 +01:00
return 1;
} else {
2014-02-23 13:15:28 +01:00
if ($first->rank < $second->rank) {
2014-02-20 11:04:52 +01:00
return -1;
} else {
return 1;
}
}
}
/**
* Updates the replay values for the given record
*
* @param array $record
*/
2014-02-23 13:15:28 +01:00
private function setRecordReplays(RecordData &$record) {
2014-02-20 11:04:52 +01:00
// Set validation replay
2014-02-24 09:49:29 +01:00
$validationReplay = $this->maniaControl->server->getValidationReplay($record->login);
2014-02-20 13:06:11 +01:00
if ($validationReplay) {
2014-02-23 13:15:28 +01:00
$record->vReplay = $validationReplay;
2014-02-20 13:06:11 +01:00
}
2014-02-20 11:04:52 +01:00
// Set ghost replay
2014-02-23 13:15:28 +01:00
if ($record->rank <= 1) {
2014-02-20 11:04:52 +01:00
$dataDirectory = $this->maniaControl->server->getDataDirectory();
if (!isset($this->dedimaniaData->directoryAccessChecked)) {
$access = $this->maniaControl->server->checkAccess($dataDirectory);
if (!$access) {
trigger_error("No access to the servers data directory. Can't retrieve ghost replays.");
}
$this->dedimaniaData->directoryAccessChecked = $access;
}
if ($this->dedimaniaData->directoryAccessChecked) {
2014-02-23 13:15:28 +01:00
$ghostReplay = $this->maniaControl->server->getGhostReplay($record->login);
2014-02-20 13:06:11 +01:00
if ($ghostReplay) {
2014-02-23 13:15:28 +01:00
$record->top1GReplay = $ghostReplay;
2014-02-20 13:06:11 +01:00
}
2014-02-20 11:04:52 +01:00
}
}
}
2014-02-16 22:38:14 +01:00
/**
* Build server info Structure for callbacks
*/
private function getServerInfo() {
$server = $this->maniaControl->client->getServerOptions();
if (!$server) {
return null;
}
if (count($this->maniaControl->playerManager->getPlayers()) == 0) {
return null;
}
$playerCount = $this->maniaControl->playerManager->getPlayerCount();
$spectatorCount = $this->maniaControl->playerManager->getSpectatorCount();
return array('SrvName' => $server->name, 'Comment' => $server->comment, 'Private' => (strlen($server->password) > 0), 'NumPlayers' => $playerCount, 'MaxPlayers' => $server->currentMaxPlayers, 'NumSpecs' => $spectatorCount, 'MaxSpecs' => $server->currentMaxSpectators);
}
/**
* Build simple player list for callbacks
*/
2014-02-19 15:16:45 +01:00
private function getPlayerList() {
2014-02-16 22:38:14 +01:00
$players = $this->maniaControl->playerManager->getPlayers();
if (count($players) == 0) {
return null;
}
$playerInfo = array();
foreach($players as $player) {
/** @var Player $player */
array_push($playerInfo, array('Login' => $player->login, 'IsSpec' => $player->isSpectator));
}
return $playerInfo;
}
/**
* Build map info struct for dedimania requests
*/
private function getMapInfo() {
$map = $this->maniaControl->mapManager->getCurrentMap();
if (!$map) {
return null;
}
$mapInfo = array();
$mapInfo['UId'] = $map->uid;
2014-02-24 20:20:17 +01:00
$mapInfo['Name'] = $map->rawName;
2014-02-16 22:38:14 +01:00
$mapInfo['Author'] = $map->authorLogin;
$mapInfo['Environment'] = $map->environment;
$mapInfo['NbCheckpoints'] = $map->nbCheckpoints;
$mapInfo['NbLaps'] = $map->nbLaps;
return $mapInfo;
}
2014-02-19 15:16:45 +01:00
/**
* Build votes info struct for callbacks
*/
private function getVotesInfo() {
$map = $this->maniaControl->mapManager->getCurrentMap();
if (!$map) {
return null;
}
$gameMode = $this->getGameModeString();
if (!$gameMode) {
return null;
}
return array('UId' => $map->uid, 'GameMode' => $gameMode);
}
/**
* Get the dedimania record of the given login
*
* @param string $login
2014-02-23 13:15:28 +01:00
* @return RecordData $record
2014-02-19 15:16:45 +01:00
*/
private function getDedimaniaRecord($login) {
2014-02-20 13:06:11 +01:00
if (!$this->dedimaniaData->records) {
2014-02-23 13:15:28 +01:00
return new RecordData(null);
2014-02-20 13:06:11 +01:00
}
2014-02-23 13:15:28 +01:00
$records = $this->dedimaniaData->records;
foreach($records as &$record) {
/** @var RecordData $record */
if ($record->login === $login) {
2014-02-19 15:16:45 +01:00
return $record;
2014-02-20 13:06:11 +01:00
}
2014-02-19 15:16:45 +01:00
}
2014-02-23 13:15:28 +01:00
return new RecordData(null);
2014-02-19 15:16:45 +01:00
}
2014-02-16 22:38:14 +01:00
/**
* Get Dedimania string representation of the current game mode
*
* @return String
*/
private function getGameModeString() {
$gameMode = $this->maniaControl->server->getGameMode();
if ($gameMode === null) {
trigger_error("Couldn't retrieve game mode. ");
return null;
}
switch($gameMode) {
case 1:
case 3:
case 5:
{
return 'Rounds';
}
case 2:
case 4:
{
return 'TA';
}
}
return null;
2014-02-15 01:03:25 +01:00
}
2014-02-20 11:04:52 +01:00
/**
* Get current checkpoint string for dedimania record
*
* @param string $login
* @return string
*/
private function getCheckpoints($login) {
2014-02-20 13:06:11 +01:00
if (!$login || !isset($this->checkpoints[$login])) {
2014-02-20 11:04:52 +01:00
return null;
2014-02-20 13:06:11 +01:00
}
2014-02-20 11:04:52 +01:00
$string = '';
$count = count($this->checkpoints[$login]);
foreach($this->checkpoints[$login] as $index => $check) {
$string .= $check;
2014-02-20 13:06:11 +01:00
if ($index < $count - 1) {
2014-02-20 11:04:52 +01:00
$string .= ',';
2014-02-20 13:06:11 +01:00
}
2014-02-20 11:04:52 +01:00
}
return $string;
}
2014-02-15 01:03:25 +01:00
/**
* Encode the given xml rpc method and params
2014-02-15 01:03:25 +01:00
*
* @param string $method
* @param array $params
* @return string
*/
private function encode_request($method, $params) {
$paramArray = array(array('methodName' => $method, 'params' => $params), array('methodName' => self::DEDIMANIA_WARNINGSANDTTR2, 'params' => array()));
return xmlrpc_encode_request(self::XMLRPC_MULTICALL, array($paramArray), array('encoding' => 'UTF-8', 'escaping' => 'markup'));
}
2014-02-16 22:38:14 +01:00
/**
* Handle xml rpc fault
2014-02-16 22:38:14 +01:00
*
* @param $fault
2014-02-20 12:20:35 +01:00
* @param $method
2014-02-16 22:38:14 +01:00
*/
2014-02-20 12:20:35 +01:00
private function handleXmlRpcFault($fault, $method) {
trigger_error('XmlRpc Fault on ' . $method . ': ' . $fault['faultString'] . ' (' . $fault['faultCode'] . ')');
}
/**
* Build Manialink
*
* @return \FML\ManiaLink
*/
2014-02-20 12:20:35 +01:00
private function buildManialink() {
if (!$this->dedimaniaData->records) {
return null;
2014-02-20 12:20:35 +01:00
}
2014-02-23 13:15:28 +01:00
$records = $this->dedimaniaData->records;
2014-02-20 12:20:35 +01:00
$title = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_TITLE);
$pos_x = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_POSX);
$pos_y = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_POSY);
$width = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_WIDTH);
$lines = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_LINESCOUNT);
$lineHeight = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_LINEHEIGHT);
$labelStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultLabelStyle();
$quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadStyle();
$quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadSubstyle();
$manialink = new ManiaLink(self::MLID_DEDIMANIA);
$frame = new Frame();
$manialink->add($frame);
$frame->setPosition($pos_x, $pos_y);
$backgroundQuad = new Quad();
$frame->add($backgroundQuad);
$backgroundQuad->setVAlign(Control::TOP);
$height = 7. + $lines * $lineHeight;
$backgroundQuad->setSize($width * 1.05, $height);
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
$titleLabel = new Label();
$frame->add($titleLabel);
$titleLabel->setPosition(0, $lineHeight * -0.9);
$titleLabel->setWidth($width);
$titleLabel->setStyle($labelStyle);
$titleLabel->setTextSize(2);
$titleLabel->setText($title);
$titleLabel->setTranslate(true);
foreach($records as $index => $record) {
2014-02-23 13:15:28 +01:00
/** @var RecordData $record */
2014-02-20 13:06:11 +01:00
if ($index >= $lines) {
2014-02-20 12:20:35 +01:00
break;
2014-02-20 13:06:11 +01:00
}
2014-02-20 12:20:35 +01:00
$y = -8. - $index * $lineHeight;
$recordFrame = new Frame();
$frame->add($recordFrame);
$recordFrame->setPosition(0, $y);
2014-03-01 19:23:21 +01:00
/*$backgroundQuad = new Quad();
2014-02-20 12:20:35 +01:00
$recordFrame->add($backgroundQuad);
$backgroundQuad->setSize($width * 1.04, $lineHeight * 1.4);
2014-03-01 19:23:21 +01:00
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);*/
2014-02-20 12:20:35 +01:00
//Rank
$rankLabel = new Label();
$recordFrame->add($rankLabel);
$rankLabel->setHAlign(Control::LEFT);
$rankLabel->setX($width * -0.47);
$rankLabel->setSize($width * 0.06, $lineHeight);
$rankLabel->setTextSize(1);
$rankLabel->setTextPrefix('$o');
2014-02-23 13:15:28 +01:00
$rankLabel->setText($record->rank);
2014-03-01 19:23:21 +01:00
$rankLabel->setTextEmboss(true);
2014-02-20 12:20:35 +01:00
//Name
$nameLabel = new Label();
$recordFrame->add($nameLabel);
$nameLabel->setHAlign(Control::LEFT);
$nameLabel->setX($width * -0.4);
$nameLabel->setSize($width * 0.6, $lineHeight);
$nameLabel->setTextSize(1);
2014-02-23 13:15:28 +01:00
$nameLabel->setText($record->nickName);
2014-03-01 19:23:21 +01:00
$nameLabel->setTextEmboss(true);
2014-02-20 12:20:35 +01:00
//Time
$timeLabel = new Label();
$recordFrame->add($timeLabel);
$timeLabel->setHAlign(Control::RIGHT);
$timeLabel->setX($width * 0.47);
$timeLabel->setSize($width * 0.25, $lineHeight);
$timeLabel->setTextSize(1);
2014-02-23 13:15:28 +01:00
$timeLabel->setText(Formatter::formatTime($record->best));
2014-03-01 19:23:21 +01:00
$timeLabel->setTextEmboss(true);
2014-02-20 12:20:35 +01:00
}
2014-03-02 13:00:23 +01:00
return $manialink;
2014-02-16 22:38:14 +01:00
}
2014-03-12 13:07:02 +01:00
2014-02-15 01:03:25 +01:00
/**
* Decodes xml rpc response
*
* @param string $response
* @return mixed
*/
private function decode($response) {
return xmlrpc_decode($response, 'utf-8');
2014-02-14 19:22:11 +01:00
}
/**
* Unload the plugin and its resources
*/
public function unload() {
2014-02-16 22:38:14 +01:00
$this->maniaControl->timerManager->unregisterTimerListenings($this);
$this->maniaControl->callbackManager->unregisterCallbackListener($this);
2014-02-15 01:03:25 +01:00
unset($this->maniaControl);
2014-02-14 19:22:11 +01:00
}
/**
* Get plugin id
*
* @return int
*/
public static function getId() {
2014-02-16 22:38:14 +01:00
return self::ID;
2014-02-14 19:22:11 +01:00
}
/**
* Get Plugin Name
*
* @return string
*/
public static function getName() {
2014-02-16 22:38:14 +01:00
return "Dedimania Plugin";
2014-02-14 19:22:11 +01:00
}
/**
* Get Plugin Version
*
* @return float
*/
public static function getVersion() {
2014-02-16 22:38:14 +01:00
return self::VERSION;
2014-02-14 19:22:11 +01:00
}
/**
* Get Plugin Author
*
* @return string
*/
public static function getAuthor() {
2014-02-16 22:38:14 +01:00
return "kremsy and steeffeen";
2014-02-14 19:22:11 +01:00
}
/**
* Get Plugin Description
*
* @return string
*/
public static function getDescription() {
2014-02-16 22:38:14 +01:00
return "Dedimania Plugin for Trackmania";
2014-02-15 01:03:25 +01:00
}
}