TrackManiaControl/application/plugins/Dedimania/Dedimania.php

1031 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-15 01:03:25 +01:00
require_once "DedimaniaData.php";
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;
2014-02-21 16:07:36 +01:00
use Maniaplanet\DedicatedServer\Xmlrpc\Base64;
2014-02-14 19:22:11 +01:00
class Dedimania implements CallbackListener, TimerListener, Plugin {
2014-02-15 01:03:25 +01:00
/**
* Constants
*/
2014-02-16 22:38:14 +01:00
const ID = 100;
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';
const SETTING_SERVER_LOGINS = 'Serverlogins, splitted by ;';
const SETTING_DEDIMANIA_CODES = 'Dedimania Codes, splitted by ;';
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-20 12:20:35 +01:00
$maniaControl->settingManager->initSetting(get_class(), self::SETTING_SERVER_LOGINS, '');
$maniaControl->settingManager->initSetting(get_class(), self::SETTING_DEDIMANIA_CODES, '');
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-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-20 12:20:35 +01:00
//TODO what was CB_IC_ClientUpdated?
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');
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $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();
$packMask = substr($this->maniaControl->server->titleId, 2);
$logins = $this->maniaControl->settingManager->getSetting($this, self::SETTING_SERVER_LOGINS);
$codes = $this->maniaControl->settingManager->getSetting($this, self::SETTING_DEDIMANIA_CODES);
if ($logins == '' || $codes == '') {
throw new \Exception("No Dedimania Data Specified, check the settings!");
}
$logins = explode(";", $logins);
$codes = explode(";", $codes);
$dedimaniaCode = "";
foreach($logins as $key => $login) {
if ($login == $serverInfo->login) {
if (!isset($codes[$key])) {
throw new \Exception("No Dedimania Code Specified, check the settings!");
}
$dedimaniaCode = $codes[$key];
}
}
if ($dedimaniaCode == '') {
throw new \Exception("No Valid Serverlogin Specified, check the settings!");
}
$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
$data = array($this->dedimaniaData->sessionId, $player->login, $player->nickname, $player->path, $player->isSpectator);
$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)) {
foreach($data as $methodResponse) {
if (xmlrpc_is_fault($methodResponse)) {
2014-02-20 12:20:35 +01:00
$this->handleXmlRpcFault($methodResponse, self::DEDIMANIA_PLAYERCONNECT);
2014-02-19 15:16:45 +01:00
}
}
} else {
if (!$data) {
trigger_error('XmlRpc Error.');
var_dump($data);
}
}
2014-02-20 12:20:35 +01:00
$manialink = $this->buildManialink();
$this->maniaControl->manialinkManager->sendManialink($manialink, $player->login);
2014-02-19 15:16:45 +01:00
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-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) {
$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-20 13:06:11 +01:00
if (!$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();
foreach($this->dedimaniaData->records['Records'] as $record) {
if (!isset($record['New']) || !$record['New']) {
continue;
}
array_push($times, array('Login' => $record['Login'], 'Best' => $record['Best'], 'Checks' => $record['Checks']));
if (!isset($replays['VReplay'])) {
$replays['VReplay'] = $record['VReplay'];
}
if (!isset($replays['Top1GReplay']) && isset($record['Top1GReplay'])) {
$replays['Top1GReplay'] = $record['Top1GReplay'];
}
// TODO: VReplayChecks
}
if (!isset($replays['VReplay'])) {
$replays['VReplay'] = '';
}
if (!isset($replays['VReplayChecks'])) {
$replays['VReplayChecks'] = '';
}
if (!isset($replays['Top1GReplay'])) {
$replays['Top1GReplay'] = '';
}
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-20 13:49:33 +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) {
trigger_error($errors);
}
}
}
}
2014-02-21 16:07:56 +01:00
}, $content, false); //TODO compression
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-20 13:49:33 +01:00
$save = true;
2014-02-19 15:16:45 +01:00
if ($oldRecord) {
if ($oldRecord['Best'] <= $time) {
$save = false;
}
}
2014-02-20 13:49:33 +01:00
if ($save) {
$player = $this->maniaControl->playerManager->getPlayer($login);
// Save time
$newRecord = array('Login' => $login, 'NickName' => $player->nickname, 'Best' => $data[2], 'Checks' => $this->getCheckpoints($login), 'New' => true);
if ($this->insertDedimaniaRecord($newRecord, $oldRecord)) {
// Get newly saved record
foreach($this->dedimaniaData->records['Records'] as &$record) {
if ($record['Login'] !== $newRecord['Login']) {
continue;
}
$newRecord = $record;
break;
}
2014-02-20 13:06:11 +01:00
2014-02-20 13:49:33 +01:00
// Announce record
if (!$oldRecord || $newRecord['Rank'] < $oldRecord['Rank']) {
// Gained rank
$improvement = 'gained the';
} else {
// Only improved time
$improvement = 'improved her/his';
}
$message = '$<' . $player->nickname . '$> ' . $improvement . ' $<$o' . $newRecord['Rank'] . '.$> Dedimania Record: ' . Formatter::formatTime($newRecord['Best']);
$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) {
if ($this->dedimaniaData->sessionId == '') {
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();
}
$serverInfo = $this->getServerInfo();
$playerInfo = $this->getPlayerList();
$mapInfo = $this->getMapInfo();
$gameMode = $this->getGameModeString();
if (!$serverInfo || !$playerInfo || !$mapInfo || !$gameMode) {
return false;
}
$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) {
$responseData = $methodResponse[0];
$this->dedimaniaData->records = $responseData;
}
}
}
$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
*/
private function insertDedimaniaRecord(&$newRecord, $oldRecord) {
2014-02-20 13:06:11 +01:00
if (!$newRecord || !$this->dedimaniaData->records || !isset($this->dedimaniaData->records['Records'])) {
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
$maxRank = $this->getMaxRank($newRecord['Login']);
2014-02-20 12:20:35 +01:00
if (!$maxRank) {
2014-02-20 11:04:52 +01:00
$maxRank = 30;
2014-02-20 12:20:35 +01:00
}
2014-02-20 11:04:52 +01:00
2014-02-19 15:16:45 +01:00
// Loop through existing records
2014-02-20 11:04:52 +01:00
foreach($this->dedimaniaData->records['Records'] as $key => &$record) {
2014-02-19 15:16:45 +01:00
if ($record['Login'] === $newRecord['Login']) {
// Old record of the same player
if ($record['Best'] <= $newRecord['Best']) {
// It's better - Do nothing
return false;
}
// Replace old record
unset($this->dedimaniaData->records['Records'][$key]);
$insert = true;
break;
}
// Other player's record
if ($record['Best'] <= $newRecord['Best']) {
// It's better - Skip
continue;
}
// New record is better - Insert it
$insert = true;
if ($oldRecord) {
// Remove old record
2014-02-20 11:04:52 +01:00
foreach($this->dedimaniaData->records['Records'] as $key2 => $record2) {
2014-02-20 13:06:11 +01:00
if ($record2['Login'] !== $oldRecord['Login']) {
2014-02-20 11:04:52 +01:00
continue;
2014-02-20 13:06:11 +01:00
}
2014-02-19 15:16:45 +01:00
unset($this->dedimaniaData->records['Records'][$key2]);
break;
}
}
break;
}
2014-02-20 11:04:52 +01:00
if (!$insert && count($this->dedimaniaData->records['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-20 11:04:52 +01:00
array_push($this->dedimaniaData->records['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-20 12:20:35 +01:00
foreach($this->dedimaniaData->records['Records'] as &$record) {
2014-02-20 13:06:11 +01:00
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-20 13:49:33 +01:00
if (!$this->dedimaniaData->records || !isset($this->dedimaniaData->records['Records'])) {
2014-02-20 11:04:52 +01:00
return;
2014-02-20 13:06:11 +01:00
}
2014-02-20 11:04:52 +01:00
// Sort records
usort($this->dedimaniaData->records['Records'], array($this, 'compareRecords'));
// Update ranks
$rank = 1;
foreach($this->dedimaniaData->records['Records'] as &$record) {
$record['Rank'] = $rank;
$rank++;
}
}
/**
* Compare function for sorting dedimania records
*
* @param array $first
* @param array $second
* @return int
*/
private function compareRecords($first, $second) {
if ($first['Best'] < $second['Best']) {
return -1;
} else if ($first['Best'] > $second['Best']) {
return 1;
} else {
if ($first['Rank'] < $second['Rank']) {
return -1;
} else {
return 1;
}
}
}
/**
* Updates the replay values for the given record
*
* @param array $record
*/
private function setRecordReplays(&$record) {
// Set validation replay
$validationReplay = $this->maniaControl->server->getValidationReplay($record['Login']);
2014-02-20 13:06:11 +01:00
if ($validationReplay) {
2014-02-20 11:04:52 +01:00
$record['VReplay'] = $validationReplay;
2014-02-20 13:06:11 +01:00
}
2014-02-20 11:04:52 +01:00
// Set ghost replay
if ($record['Rank'] <= 1) {
$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) {
$ghostReplay = $this->maniaControl->server->getGhostReplay($record['Login']);
2014-02-20 13:06:11 +01:00
if ($ghostReplay) {
2014-02-20 11:04:52 +01:00
$record['Top1GReplay'] = $ghostReplay;
2014-02-20 13:06:11 +01:00
}
2014-02-20 11:04:52 +01:00
}
}
}
/**
* Get max rank for given login
*/
private function getMaxRank($login) {
2014-02-20 13:06:11 +01:00
if (!isset($this->dedimaniaData->records)) {
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
$records = $this->dedimaniaData->records;
$maxRank = $records['ServerMaxRank'];
foreach($records['Players'] as $player) {
if ($player['Login'] === $login) {
2014-02-20 13:06:11 +01:00
if ($player['MaxRank'] > $maxRank) {
2014-02-20 11:04:52 +01:00
$maxRank = $player['MaxRank'];
2014-02-20 13:06:11 +01:00
}
2014-02-20 11:04:52 +01:00
break;
}
}
return $maxRank;
}
2014-02-19 15:16:45 +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
$client = null;
$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;
$mapInfo['Name'] = $map->name;
$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
* @return array $record
*/
private function getDedimaniaRecord($login) {
2014-02-20 13:06:11 +01:00
if (!$this->dedimaniaData->records) {
2014-02-19 15:16:45 +01:00
return null;
2014-02-20 13:06:11 +01:00
}
2014-02-19 15:16:45 +01:00
$records = $this->dedimaniaData->records['Records'];
2014-02-20 12:20:35 +01:00
foreach($records as $record) {
2014-02-20 13:06:11 +01:00
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
}
return null;
}
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
/**
* Encodes the given xml rpc method and params
*
* @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
/**
* Handles xml rpc fault
*
* @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'] . ')');
}
private function buildManialink() {
if (!$this->dedimaniaData->records) {
return '';
}
$records = $this->dedimaniaData->records['Records'];
$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-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);
$backgroundQuad = new Quad();
$recordFrame->add($backgroundQuad);
$backgroundQuad->setSize($width * 1.04, $lineHeight * 1.4);
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
//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');
$rankLabel->setText($record['Rank']);
//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);
$nameLabel->setText(Formatter::stripDirtyCodes($record['NickName']));
//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);
$timeLabel->setText(Formatter::formatTime($record['Best']));
}
return $manialink->render()->saveXML();
2014-02-16 22:38:14 +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
}
}