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-14 19:22:11 +01:00
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\TimerListener;
|
|
|
|
use ManiaControl\ManiaControl;
|
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-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-16 22:38:14 +01:00
|
|
|
const USE_COMPRESSION = false;
|
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 $manialink = null;
|
|
|
|
//private $lastSendManialink = array();
|
|
|
|
private $updateManialink = false;
|
2014-02-14 19:22:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares the Plugin
|
|
|
|
*
|
|
|
|
* @param ManiaControl $maniaControl
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function prepare(ManiaControl $maniaControl) {
|
|
|
|
// TODO: Implement prepare() method.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-15 12:41:29 +01:00
|
|
|
return;
|
2014-02-15 01:03:25 +01:00
|
|
|
|
2014-02-19 10:43:37 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT,$this, 'handlePlayerConnect');
|
2014-02-16 22:38:14 +01:00
|
|
|
$this->maniaControl->timerManager->registerTimerListening($this, 'updateEverySecond', 1000);
|
|
|
|
$this->maniaControl->timerManager->registerTimerListening($this, 'handleEveryMinute', 1000 * 60);
|
|
|
|
//TODO parse settings
|
2014-02-15 01:03:25 +01:00
|
|
|
|
|
|
|
// Open session
|
2014-02-15 12:53:33 +01:00
|
|
|
$serverInfo = $this->maniaControl->server->getInfo();
|
2014-02-15 13:04:31 +01:00
|
|
|
$serverVersion = $this->maniaControl->client->getVersion();
|
2014-02-15 01:03:25 +01:00
|
|
|
$packMask = substr($this->maniaControl->server->titleId, 2);
|
2014-02-16 22:38:14 +01:00
|
|
|
$this->dedimaniaData = new DedimaniaData(".paragoncanyon", "a3ee654ac8", $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)) {
|
|
|
|
$this->handleXmlRpcFault($methodResponse);
|
|
|
|
} else if ($index <= 0) {
|
|
|
|
$responseData = $methodResponse[0];
|
|
|
|
$this->dedimaniaData->sessionId = $responseData['SessionId'];
|
|
|
|
if ($this->dedimaniaData->sessionId != '') {
|
|
|
|
$this->maniaControl->log("Dedimania connection successfully established.");
|
|
|
|
$this->fetchDedimaniaRecords();
|
|
|
|
} 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) {
|
|
|
|
$this->updateManialink = false;
|
|
|
|
//TODO send manialink
|
|
|
|
}
|
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) {
|
|
|
|
//$this->checkDedimaniaSession();
|
2014-02-19 10:43:37 +01:00
|
|
|
//TODO uncomment
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($reset) {
|
|
|
|
// Reset records
|
|
|
|
$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-16 22:38:14 +01:00
|
|
|
if (is_array($data)) {
|
|
|
|
foreach($data as $index => $methodResponse) {
|
|
|
|
if (xmlrpc_is_fault($methodResponse)) {
|
|
|
|
$this->handleXmlRpcFault($methodResponse);
|
|
|
|
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)) {
|
|
|
|
$this->handleXmlRpcFault($methodResponse);
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
private function getPlayerList($votes = false) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
private function handleXmlRpcFault($fault) {
|
|
|
|
trigger_error('XmlRpc Fault: ' . $fault['faultString'] . ' (' . $fault['faultCode'] . ')');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|