2014-05-04 00:36:20 +02:00
< ? php
namespace MCTeam\Dedimania ;
use FML\Controls\Frame ;
use FML\Controls\Label ;
use FML\Controls\Quad ;
use FML\Controls\Quads\Quad_BgsPlayerCard ;
use FML\ManiaLink ;
use FML\Script\Features\Paging ;
use ManiaControl\Callbacks\CallbackListener ;
use ManiaControl\Callbacks\CallbackManager ;
use ManiaControl\Callbacks\Callbacks ;
2017-05-08 19:55:07 +02:00
use ManiaControl\Callbacks\Structures\TrackMania\OnWayPointEventStructure ;
2014-05-04 00:36:20 +02:00
use ManiaControl\Callbacks\TimerListener ;
use ManiaControl\Commands\CommandListener ;
2017-05-08 17:45:16 +02:00
use ManiaControl\Files\AsyncHttpRequest ;
2014-08-03 13:29:54 +02:00
use ManiaControl\Logger ;
2014-05-04 00:36:20 +02:00
use ManiaControl\ManiaControl ;
use ManiaControl\Manialinks\ManialinkManager ;
use ManiaControl\Players\Player ;
use ManiaControl\Players\PlayerManager ;
use ManiaControl\Plugins\Plugin ;
2014-05-13 16:40:05 +02:00
use ManiaControl\Utils\Formatter ;
2017-05-12 21:28:26 +02:00
use Maniaplanet\DedicatedServer\Xmlrpc\UnavailableFeatureException ;
2014-05-04 00:36:20 +02:00
/**
* ManiaControl Dedimania Plugin
*
* @ author ManiaControl Team < mail @ maniacontrol . com >
2017-02-04 11:49:23 +01:00
* @ copyright 2014 - 2017 ManiaControl Team
2014-05-04 00:36:20 +02:00
* @ license http :// www . gnu . org / licenses / GNU General Public License , Version 3
*/
class DedimaniaPlugin implements CallbackListener , CommandListener , TimerListener , Plugin {
/*
* Constants
*/
2014-06-17 23:35:56 +02:00
const ID = 8 ;
2017-05-08 17:45:16 +02:00
const VERSION = 0.2 ;
2014-06-17 23:35:56 +02:00
const AUTHOR = 'MCTeam' ;
const NAME = 'Dedimania Plugin' ;
const MLID_DEDIMANIA = 'Dedimania.ManialinkId' ;
const XMLRPC_MULTICALL = 'system.multicall' ;
2017-05-08 18:53:56 +02:00
const DEDIMANIA_URL = 'http://dedimania.net:8082/Dedimania' ;
2014-06-17 23:35:56 +02:00
const DEDIMANIA_OPEN_SESSION = 'dedimania.OpenSession' ;
const DEDIMANIA_CHECK_SESSION = 'dedimania.CheckSession' ;
const DEDIMANIA_GET_RECORDS = 'dedimania.GetChallengeRecords' ;
const DEDIMANIA_PLAYERCONNECT = 'dedimania.PlayerConnect' ;
const DEDIMANIA_PLAYERDISCONNECT = 'dedimania.PlayerDisconnect' ;
const DEDIMANIA_UPDATE_SERVER_PLAYERS = 'dedimania.UpdateServerPlayers' ;
const DEDIMANIA_SET_CHALLENGE_TIMES = 'dedimania.SetChallengeTimes' ;
const DEDIMANIA_WARNINGSANDTTR2 = 'dedimania.WarningsAndTTR2' ;
const SETTING_WIDGET_ENABLE = 'Enable Dedimania Widget' ;
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_LINE_COUNT = 'Widget Displayed Lines Count' ;
const SETTING_WIDGET_LINE_HEIGHT = 'Widget Line Height' ;
const SETTING_DEDIMANIA_CODE = '$l[http://dedimania.net/tm2stats/?do=register]Dedimania Code for ' ;
const CB_DEDIMANIA_CHANGED = 'Dedimania.Changed' ;
const CB_DEDIMANIA_UPDATED = 'Dedimania.Updated' ;
const ACTION_SHOW_DEDIRECORDSLIST = 'Dedimania.ShowDediRecordsList' ;
2014-07-05 18:02:27 +02:00
const DEDIMANIA_DEBUG = false ;
2014-05-04 00:36:20 +02:00
/*
2014-08-02 22:31:46 +02:00
* Private properties
2014-05-04 00:36:20 +02:00
*/
/** @var ManiaControl $maniaControl */
private $maniaControl = null ;
2015-01-12 13:36:30 +01:00
// TODO: there are several spots where $dedimaniaData is null - fix those (look for !$this->dedimaniaData) -> Tried to fix (Remove this TODO if it works in next release)
2014-05-04 00:36:20 +02:00
/** @var DedimaniaData $dedimaniaData */
2015-01-12 13:33:40 +01:00
private $dedimaniaData = null ;
2014-05-04 00:36:20 +02:00
private $updateManialink = false ;
2015-01-12 13:33:40 +01:00
private $checkpoints = array ();
private $init = false ;
2014-05-04 00:36:20 +02:00
2014-07-05 20:24:54 +02:00
private $request = null ;
2014-05-04 00:36:20 +02:00
/**
* @ see \ManiaControl\Plugins\Plugin :: prepare ()
*/
public static function prepare ( ManiaControl $maniaControl ) {
2014-08-13 11:14:29 +02:00
$servers = $maniaControl -> getServer () -> getAllServers ();
2014-05-04 00:36:20 +02:00
foreach ( $servers as $server ) {
2014-08-13 11:14:29 +02:00
$maniaControl -> getSettingManager () -> initSetting ( get_class (), self :: SETTING_DEDIMANIA_CODE . $server -> login . '$l' , '' );
2014-05-04 00:36:20 +02:00
}
}
/**
* @ see \ManiaControl\Plugins\Plugin :: getId ()
*/
public static function getId () {
return self :: ID ;
}
/**
* @ see \ManiaControl\Plugins\Plugin :: getName ()
*/
public static function getName () {
return self :: NAME ;
}
/**
* @ see \ManiaControl\Plugins\Plugin :: getVersion ()
*/
public static function getVersion () {
return self :: VERSION ;
}
/**
* @ see \ManiaControl\Plugins\Plugin :: getAuthor ()
*/
public static function getAuthor () {
return self :: AUTHOR ;
}
/**
* @ see \ManiaControl\Plugins\Plugin :: getDescription ()
*/
public static function getDescription () {
2014-06-14 15:48:27 +02:00
return 'Dedimania Plugin for TrackMania' ;
2014-05-04 00:36:20 +02:00
}
/**
* @ see \ManiaControl\Plugins\Plugin :: load ()
*/
public function load ( ManiaControl $maniaControl ) {
$this -> maniaControl = $maniaControl ;
if ( ! extension_loaded ( 'xmlrpc' )) {
throw new \Exception ( " You need to activate the PHP extension xmlrpc to run this Plugin! " );
}
2014-08-03 01:34:18 +02:00
// Settings
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_ENABLE , true );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_TITLE , 'Dedimania' );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_POSX , - 139 );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_POSY , 7 );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_WIDTH , 40 );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_LINE_HEIGHT , 4 );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_LINE_COUNT , 12 );
2014-08-03 01:34:18 +02:00
// Callbacks
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( Callbacks :: BEGINMAP , $this , 'handleBeginMap' );
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( Callbacks :: ENDMAP , $this , 'handleMapEnd' );
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( CallbackManager :: CB_MP_PLAYERMANIALINKPAGEANSWER , $this , 'handleManialinkPageAnswer' );
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( PlayerManager :: CB_PLAYERCONNECT , $this , 'handlePlayerConnect' );
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( PlayerManager :: CB_PLAYERDISCONNECT , $this , 'handlePlayerDisconnect' );
2017-05-08 19:55:07 +02:00
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( Callbacks :: TM_ONWAYPOINT , $this , 'handleCheckpointCallback' );
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( Callbacks :: TM_ONFINISHLINE , $this , 'handleFinishCallback' );
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( Callbacks :: TM_ONLAPFINISH , $this , 'handleFinishCallback' );
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getTimerManager () -> registerTimerListening ( $this , 'updateEverySecond' , 1000 );
2017-05-08 18:53:56 +02:00
$this -> maniaControl -> getTimerManager () -> registerTimerListening ( $this , 'handleEveryHalfMinute' , 1000 * 30 );
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getTimerManager () -> registerTimerListening ( $this , 'updatePlayerList' , 1000 * 60 * 3 );
2017-05-08 19:55:07 +02:00
$this -> maniaControl -> getCommandManager () -> registerCommandListener ( array ( 'dedirecs' ,
'dedirecords' ), $this , 'showDediRecordsList' , false , 'Shows a list of Dedimania records of the current map.' );
2014-05-04 00:36:20 +02:00
// Open session
2014-08-13 11:14:29 +02:00
$serverInfo = $this -> maniaControl -> getServer () -> getInfo ();
$serverVersion = $this -> maniaControl -> getClient () -> getVersion ();
2017-05-08 17:45:16 +02:00
$packMask = $this -> maniaControl -> getMapManager () -> getCurrentMap () -> environment ;
2014-05-04 00:36:20 +02:00
2014-08-13 11:14:29 +02:00
$dedimaniaCode = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_DEDIMANIA_CODE . $serverInfo -> login . '$l' );
2014-05-24 20:19:41 +02:00
if ( ! $dedimaniaCode ) {
2014-05-04 00:36:20 +02:00
throw new \Exception ( " No Dedimania Code Specified, check the settings! " );
}
2017-04-16 15:30:43 +02:00
//$this->request = AsynchronousFileReader::newRequestTest(self::DEDIMANIA_URL);
2014-07-05 20:24:54 +02:00
2014-05-04 00:36:20 +02:00
$this -> dedimaniaData = new DedimaniaData ( $serverInfo -> login , $dedimaniaCode , $serverInfo -> path , $packMask , $serverVersion );
$this -> openDedimaniaSession ();
}
/**
* Opens the Dedimania Session
*/
private function openDedimaniaSession () {
2014-07-05 18:02:27 +02:00
if ( self :: DEDIMANIA_DEBUG ) {
var_dump ( " Dedi Debug: DedimaniaData before Connecting " );
var_dump ( $this -> dedimaniaData );
}
2014-06-17 23:35:56 +02:00
$content = $this -> encode_request ( self :: DEDIMANIA_OPEN_SESSION , array ( $this -> dedimaniaData -> toArray ()));
2014-05-04 00:36:20 +02:00
2014-07-05 18:02:27 +02:00
if ( self :: DEDIMANIA_DEBUG ) {
var_dump ( " Dedi Debug: XML-RPC Content on Connecting " );
var_dump ( $content );
}
2014-07-05 20:24:54 +02:00
//$this->maniaControl->fileReader->postDataTest($this->request, self::DEDIMANIA_URL, function ($data, $error) {
2017-05-08 17:45:16 +02:00
$asyncHttpRequest = new AsyncHttpRequest ( $this -> maniaControl , self :: DEDIMANIA_URL );
$asyncHttpRequest -> setCallable ( function ( $data , $error ) {
2015-01-12 13:33:40 +01:00
Logger :: log ( " Try to connect on Dedimania " );
if ( ! $data || $error ) {
2017-05-08 18:53:56 +02:00
Logger :: logError ( " Dedimania Error while opening session: ' { $error } ' " );
2015-01-12 13:33:40 +01:00
}
$data = $this -> decode ( $data );
if ( ! is_array ( $data ) || empty ( $data )) {
return ;
}
$methodResponse = $data [ 0 ];
if ( xmlrpc_is_fault ( $methodResponse )) {
$this -> handleXmlRpcFault ( $methodResponse , self :: DEDIMANIA_OPEN_SESSION );
return ;
}
$responseData = $methodResponse [ 0 ];
$this -> dedimaniaData -> sessionId = $responseData [ 'SessionId' ];
if ( $this -> dedimaniaData -> sessionId ) {
Logger :: log ( " Dedimania connection successfully established. " );
$this -> fetchDedimaniaRecords ();
$this -> init = true ;
} else {
Logger :: logError ( " Error while opening Dedimania Connection " );
}
if ( self :: DEDIMANIA_DEBUG ) {
var_dump ( " Dedi Debug: Connect Method Response " );
var_dump ( $methodResponse );
var_dump ( " Dedi Debug: DedimaniaData after Startup " );
var_dump ( $this -> dedimaniaData );
}
2017-05-08 17:45:16 +02:00
});
$asyncHttpRequest -> setContent ( $content );
$asyncHttpRequest -> setCompression ( true );
2017-05-08 18:53:56 +02:00
$asyncHttpRequest -> setTimeout ( 500 );
2017-05-08 17:45:16 +02:00
$asyncHttpRequest -> postData ();
2014-05-04 00:36:20 +02:00
}
/**
* Encode 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' ));
}
/**
* Decodes xml rpc response
*
* @ param string $response
* @ return mixed
*/
private function decode ( $response ) {
return xmlrpc_decode ( $response , 'utf-8' );
}
/**
* Handle xml rpc fault
*
2014-05-13 16:40:05 +02:00
* @ param array $fault
* @ param string $method
2014-05-04 00:36:20 +02:00
*/
2014-05-13 16:40:05 +02:00
private function handleXmlRpcFault ( array $fault , $method ) {
trigger_error ( " XmlRpc Fault on ' { $method } ': ' { $fault [ 'faultString' ] } ( { $fault [ 'faultCode' ] } )! " );
2014-05-04 00:36:20 +02:00
}
/**
* Fetch Dedimania Records
*
* @ param bool $reset
2014-05-09 17:31:29 +02:00
* @ return bool
2014-05-04 00:36:20 +02:00
*/
private function fetchDedimaniaRecords ( $reset = true ) {
2015-01-13 18:06:32 +01:00
if ( ! isset ( $this -> dedimaniaData ) || ! $this -> dedimaniaData -> sessionId ) {
2014-05-04 00:36:20 +02:00
return false ;
}
// Reset records
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 );
2014-06-17 23:35:56 +02:00
$content = $this -> encode_request ( self :: DEDIMANIA_GET_RECORDS , $data );
2014-07-05 20:24:54 +02:00
//var_dump("get recs");
2017-05-08 17:45:16 +02:00
$asyncHttpRequest = new AsyncHttpRequest ( $this -> maniaControl , self :: DEDIMANIA_URL );
$asyncHttpRequest -> setCallable ( function ( $data , $error ) {
2015-01-12 13:33:40 +01:00
if ( $error ) {
2017-05-08 18:53:56 +02:00
Logger :: logError ( 'Dedimania Error while fetching records: ' . $error );
2015-01-12 13:33:40 +01:00
}
$data = $this -> decode ( $data );
2017-05-08 17:45:16 +02:00
//Data[0][0] can be false in error case like map has no checkpoints
2017-05-08 23:28:37 +02:00
if ( ! is_array ( $data ) || empty ( $data ) || ! isset ( $data [ 0 ]) || ! isset ( $data [ 0 ][ 0 ]) || $data [ 0 ][ 0 ] == false ) {
2015-01-12 13:33:40 +01:00
return ;
}
2017-05-08 17:45:16 +02:00
//var_dump($data);
2015-01-12 13:33:40 +01:00
$methodResponse = $data [ 0 ];
if ( xmlrpc_is_fault ( $methodResponse )) {
$this -> handleXmlRpcFault ( $methodResponse , self :: DEDIMANIA_GET_RECORDS );
return ;
}
$responseData = $methodResponse [ 0 ];
2017-05-08 17:45:16 +02:00
2015-01-12 13:33:40 +01:00
if ( ! isset ( $responseData [ 'Players' ]) || ! isset ( $responseData [ 'Records' ])) {
2017-05-08 17:45:16 +02:00
$this -> maniaControl -> getErrorHandler () -> triggerDebugNotice ( 'Invalid Dedimania response! ' . json_encode ( $responseData ));
2015-01-12 13:33:40 +01:00
return ;
}
$this -> dedimaniaData -> serverMaxRank = $responseData [ 'ServerMaxRank' ];
foreach ( $responseData [ 'Players' ] as $player ) {
$dediPlayer = new DedimaniaPlayer ( $player );
$this -> dedimaniaData -> addPlayer ( $dediPlayer );
}
foreach ( $responseData [ 'Records' ] as $key => $record ) {
$this -> dedimaniaData -> records [ $key ] = new RecordData ( $record );
}
if ( self :: DEDIMANIA_DEBUG ) {
var_dump ( " Dedimania Records Fetched " );
}
$this -> updateManialink = true ;
$this -> maniaControl -> getCallbackManager () -> triggerCallback ( self :: CB_DEDIMANIA_UPDATED , $this -> dedimaniaData -> records );
2017-05-08 17:45:16 +02:00
});
$asyncHttpRequest -> setContent ( $content );
$asyncHttpRequest -> setCompression ( true );
2017-05-08 18:53:56 +02:00
$asyncHttpRequest -> setTimeout ( 500 );
2017-05-08 17:45:16 +02:00
$asyncHttpRequest -> postData ();
2014-05-04 00:36:20 +02:00
return true ;
}
/**
* Build server info Structure for callbacks
*/
private function getServerInfo () {
2014-08-13 11:14:29 +02:00
$server = $this -> maniaControl -> getClient () -> getServerOptions ();
2014-05-04 00:36:20 +02:00
if ( ! $server ) {
return null ;
}
2017-05-08 17:45:16 +02:00
if ( $this -> maniaControl -> getPlayerManager () -> getPlayerCount ( false ) <= 0 ) {
2014-05-04 00:36:20 +02:00
return null ;
}
2014-08-13 11:14:29 +02:00
$playerCount = $this -> maniaControl -> getPlayerManager () -> getPlayerCount ();
$spectatorCount = $this -> maniaControl -> getPlayerManager () -> getSpectatorCount ();
2014-05-04 00:36:20 +02:00
2017-05-08 17:45:16 +02:00
return array ( 'SrvName' => $server -> name , 'Comment' => $server -> comment , 'Private' => ( strlen ( $server -> password ) > 0 ), 'NumPlayers' => $playerCount , 'MaxPlayers' => $server -> currentMaxPlayers ,
'NumSpecs' => $spectatorCount , 'MaxSpecs' => $server -> currentMaxSpectators );
2014-05-04 00:36:20 +02:00
}
/**
* Build simple player list for callbacks
*/
private function getPlayerList () {
2014-08-13 11:14:29 +02:00
$players = $this -> maniaControl -> getPlayerManager () -> getPlayers ();
2014-05-04 00:36:20 +02:00
2014-06-14 14:32:29 +02:00
if ( empty ( $players )) {
2014-05-04 00:36:20 +02:00
return null ;
}
$playerInfo = array ();
foreach ( $players as $player ) {
array_push ( $playerInfo , array ( 'Login' => $player -> login , 'IsSpec' => $player -> isSpectator ));
}
return $playerInfo ;
}
/**
2014-05-13 18:26:38 +02:00
* Build Map Info Array for Dedimania Requests
*
* @ return array
2014-05-04 00:36:20 +02:00
*/
private function getMapInfo () {
2014-08-13 11:14:29 +02:00
$map = $this -> maniaControl -> getMapManager () -> getCurrentMap ();
2014-05-04 00:36:20 +02:00
if ( ! $map ) {
return null ;
}
$mapInfo = array ();
$mapInfo [ 'UId' ] = $map -> uid ;
$mapInfo [ 'Name' ] = $map -> rawName ;
$mapInfo [ 'Author' ] = $map -> authorLogin ;
$mapInfo [ 'Environment' ] = $map -> environment ;
$mapInfo [ 'NbCheckpoints' ] = $map -> nbCheckpoints ;
$mapInfo [ 'NbLaps' ] = $map -> nbLaps ;
return $mapInfo ;
}
/**
2014-06-14 14:32:29 +02:00
* Get Dedimania String Representation of the current Game Mode
2014-05-04 00:36:20 +02:00
*
* @ return String
*/
private function getGameModeString () {
2014-08-13 11:14:29 +02:00
$gameMode = $this -> maniaControl -> getServer () -> getGameMode ();
2014-05-04 00:36:20 +02:00
if ( $gameMode === null ) {
2014-08-03 13:29:54 +02:00
Logger :: logError ( " Couldn't retrieve game mode. " );
2014-05-04 00:36:20 +02:00
return null ;
}
switch ( $gameMode ) {
2015-01-12 13:33:40 +01:00
case 0 : {
2014-08-13 11:14:29 +02:00
$scriptNameResponse = $this -> maniaControl -> getClient () -> getScriptName ();
2014-06-14 14:32:29 +02:00
$scriptName = str_replace ( '.Script.txt' , '' , $scriptNameResponse [ 'CurrentValue' ]);
switch ( $scriptName ) {
case 'Rounds' :
case 'Cup' :
case 'Team' :
return 'Rounds' ;
case 'TimeAttack' :
case 'Laps' :
case 'TeamAttack' :
case 'TimeAttackPlus' :
return 'TA' ;
2014-05-04 00:36:20 +02:00
}
break ;
}
case 1 :
case 3 :
2015-01-12 13:33:40 +01:00
case 5 : {
2014-05-04 00:36:20 +02:00
return 'Rounds' ;
}
case 2 :
2015-01-12 13:33:40 +01:00
case 4 : {
2014-05-04 00:36:20 +02:00
return 'TA' ;
}
}
return null ;
}
/**
2014-06-14 14:32:29 +02:00
* Handle 1 Second Callback
2014-05-04 00:36:20 +02:00
*/
2014-06-14 14:32:29 +02:00
public function updateEverySecond () {
2014-07-05 18:02:27 +02:00
if ( ! $this -> updateManialink ) {
2014-05-04 00:36:20 +02:00
return ;
}
$this -> updateManialink = false ;
2014-07-05 18:02:27 +02:00
if ( self :: DEDIMANIA_DEBUG ) {
var_dump ( $this -> dedimaniaData );
var_dump ( " Dedimania Debug: Update Manialink " );
}
2017-05-08 17:45:16 +02:00
if ( $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_ENABLE )) {
2017-05-08 19:55:07 +02:00
$this -> sendManialink ();
2014-05-04 00:36:20 +02:00
}
}
/**
2017-05-08 19:55:07 +02:00
* Builds and Sends the Manialink
2014-05-04 00:36:20 +02:00
*/
2017-05-08 19:55:07 +02:00
private function sendManialink () {
if ( ! isset ( $this -> dedimaniaData ) || ! isset ( $this -> dedimaniaData -> records )) {
return null ;
}
2014-05-04 00:36:20 +02:00
$records = $this -> dedimaniaData -> records ;
2014-08-13 11:14:29 +02:00
$title = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_TITLE );
$posX = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_POSX );
$posY = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_POSY );
$width = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_WIDTH );
$lines = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_LINE_COUNT );
$lineHeight = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_LINE_HEIGHT );
$labelStyle = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getDefaultLabelStyle ();
$quadStyle = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getDefaultQuadStyle ();
$quadSubstyle = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getDefaultQuadSubstyle ();
2014-05-04 00:36:20 +02:00
$manialink = new ManiaLink ( self :: MLID_DEDIMANIA );
$frame = new Frame ();
2017-03-25 19:15:50 +01:00
$manialink -> addChild ( $frame );
2014-06-14 15:48:27 +02:00
$frame -> setPosition ( $posX , $posY );
2014-05-04 00:36:20 +02:00
$backgroundQuad = new Quad ();
2017-03-25 19:15:50 +01:00
$frame -> addChild ( $backgroundQuad );
$backgroundQuad -> setVerticalAlign ( $backgroundQuad :: TOP );
2014-05-04 00:36:20 +02:00
$height = 7. + $lines * $lineHeight ;
$backgroundQuad -> setSize ( $width * 1.05 , $height );
$backgroundQuad -> setStyles ( $quadStyle , $quadSubstyle );
$titleLabel = new Label ();
2017-03-25 19:15:50 +01:00
$frame -> addChild ( $titleLabel );
2014-05-04 00:36:20 +02:00
$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 ) {
/** @var RecordData $record */
if ( $index >= $lines ) {
break ;
}
$y = - 8. - $index * $lineHeight ;
$recordFrame = new Frame ();
2017-03-25 19:15:50 +01:00
$frame -> addChild ( $recordFrame );
2014-05-04 00:36:20 +02:00
$recordFrame -> setPosition ( 0 , $y );
/* $backgroundQuad = new Quad ();
2017-03-25 19:15:50 +01:00
$recordFrame -> addChild ( $backgroundQuad );
2014-05-04 00:36:20 +02:00
$backgroundQuad -> setSize ( $width * 1.04 , $lineHeight * 1.4 );
$backgroundQuad -> setStyles ( $quadStyle , $quadSubstyle ); */
//Rank
$rankLabel = new Label ();
2017-03-25 19:15:50 +01:00
$recordFrame -> addChild ( $rankLabel );
$rankLabel -> setHorizontalAlign ( $rankLabel :: LEFT );
2014-05-04 00:36:20 +02:00
$rankLabel -> setX ( $width * - 0.47 );
$rankLabel -> setSize ( $width * 0.06 , $lineHeight );
$rankLabel -> setTextSize ( 1 );
$rankLabel -> setTextPrefix ( '$o' );
$rankLabel -> setText ( $record -> rank );
$rankLabel -> setTextEmboss ( true );
//Name
$nameLabel = new Label ();
2017-03-25 19:15:50 +01:00
$recordFrame -> addChild ( $nameLabel );
$nameLabel -> setHorizontalAlign ( $nameLabel :: LEFT );
2014-05-04 00:36:20 +02:00
$nameLabel -> setX ( $width * - 0.4 );
$nameLabel -> setSize ( $width * 0.6 , $lineHeight );
$nameLabel -> setTextSize ( 1 );
$nameLabel -> setText ( $record -> nickName );
$nameLabel -> setTextEmboss ( true );
//Time
$timeLabel = new Label ();
2017-03-25 19:15:50 +01:00
$recordFrame -> addChild ( $timeLabel );
$timeLabel -> setHorizontalAlign ( $timeLabel :: RIGHT );
2014-05-04 00:36:20 +02:00
$timeLabel -> setX ( $width * 0.47 );
$timeLabel -> setSize ( $width * 0.25 , $lineHeight );
$timeLabel -> setTextSize ( 1 );
$timeLabel -> setText ( Formatter :: formatTime ( $record -> best ));
$timeLabel -> setTextEmboss ( true );
}
2017-05-08 19:55:07 +02:00
$this -> maniaControl -> getManialinkManager () -> sendManialink ( $manialink );
2014-05-04 00:36:20 +02:00
}
/**
2014-05-13 16:40:05 +02:00
* Handle 1 Minute Callback
2014-05-04 00:36:20 +02:00
*/
2017-05-08 18:53:56 +02:00
public function handleEveryHalfMinute () {
2014-05-04 00:36:20 +02:00
if ( ! $this -> init ) {
return ;
}
$this -> checkDedimaniaSession ();
}
/**
* Checks If a Dedimania Session exists , if not create a new oen
*/
private function checkDedimaniaSession () {
2014-05-24 20:19:41 +02:00
if ( ! $this -> dedimaniaData -> sessionId ) {
2014-05-04 00:36:20 +02:00
$this -> openDedimaniaSession ();
return ;
}
2014-06-17 23:35:56 +02:00
$content = $this -> encode_request ( self :: DEDIMANIA_CHECK_SESSION , array ( $this -> dedimaniaData -> sessionId ));
2017-05-08 17:45:16 +02:00
$asyncHttpRequest = new AsyncHttpRequest ( $this -> maniaControl , self :: DEDIMANIA_URL );
$asyncHttpRequest -> setCallable ( function ( $data , $error ) {
2015-01-12 13:33:40 +01:00
if ( $error ) {
2017-05-08 18:53:56 +02:00
//Reopen session in Timeout case
$this -> openDedimaniaSession ();
//Logger::logError("Dedimania Error while checking session: " . $error);
2015-01-12 13:33:40 +01:00
}
$data = $this -> decode ( $data );
if ( ! is_array ( $data ) || empty ( $data )) {
return ;
}
2017-05-08 18:53:56 +02:00
//var_dump("SESSION CHECK");
//var_dump($data);
2015-01-12 13:33:40 +01:00
$methodResponse = $data [ 0 ];
if ( xmlrpc_is_fault ( $methodResponse )) {
$this -> handleXmlRpcFault ( $methodResponse , self :: DEDIMANIA_CHECK_SESSION );
return ;
}
$responseData = $methodResponse [ 0 ];
if ( is_bool ( $responseData )) {
if ( ! $responseData ) {
$this -> openDedimaniaSession ();
}
}
if ( self :: DEDIMANIA_DEBUG ) {
var_dump ( " Dedi Debug: Session Check ResponseData " );
var_dump ( $responseData );
}
2017-05-08 17:45:16 +02:00
});
$asyncHttpRequest -> setContent ( $content );
$asyncHttpRequest -> setCompression ( true );
2017-05-08 18:53:56 +02:00
$asyncHttpRequest -> setTimeout ( 500 );
2017-05-08 17:45:16 +02:00
$asyncHttpRequest -> postData ();
2014-05-04 00:36:20 +02:00
}
/**
* Handle PlayerConnect callback
*
2014-05-24 20:19:41 +02:00
* @ param Player $player
2014-05-04 00:36:20 +02:00
*/
public function handlePlayerConnect ( Player $player ) {
2015-01-13 18:06:32 +01:00
if ( ! isset ( $this -> dedimaniaData )) {
2014-06-26 19:45:54 +02:00
return ;
}
2014-05-04 00:36:20 +02:00
// Send Dedimania request
$data = array ( $this -> dedimaniaData -> sessionId , $player -> login , $player -> rawNickname , $player -> path , $player -> isSpectator );
$content = $this -> encode_request ( self :: DEDIMANIA_PLAYERCONNECT , $data );
2017-05-08 17:45:16 +02:00
$asyncHttpRequest = new AsyncHttpRequest ( $this -> maniaControl , self :: DEDIMANIA_URL );
$asyncHttpRequest -> setCallable ( function ( $data , $error ) use ( & $player ) {
2015-01-12 13:33:40 +01:00
if ( $error ) {
2017-05-09 14:02:00 +02:00
$this -> openDedimaniaSession ();
2015-01-12 13:33:40 +01:00
}
$data = $this -> decode ( $data );
if ( ! is_array ( $data ) || empty ( $data )) {
return ;
}
$methodResponse = $data [ 0 ];
if ( xmlrpc_is_fault ( $methodResponse )) {
$this -> handleXmlRpcFault ( $methodResponse , self :: DEDIMANIA_PLAYERCONNECT );
return ;
}
$responseData = $methodResponse [ 0 ];
$dediPlayer = new DedimaniaPlayer ( $responseData );
$this -> dedimaniaData -> addPlayer ( $dediPlayer );
// Fetch records if he is the first who joined the server
2017-05-08 17:45:16 +02:00
if ( $this -> maniaControl -> getPlayerManager () -> getPlayerCount ( false ) === 1 ) {
2015-01-12 13:33:40 +01:00
$this -> fetchDedimaniaRecords ( true );
}
2017-05-08 17:45:16 +02:00
if ( $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_ENABLE )) {
2017-05-08 19:55:07 +02:00
$this -> sendManialink ();
2015-01-12 13:33:40 +01:00
}
2017-05-08 18:53:56 +02:00
});
2017-05-08 17:45:16 +02:00
$asyncHttpRequest -> setContent ( $content );
$asyncHttpRequest -> setCompression ( true );
2017-05-08 18:53:56 +02:00
$asyncHttpRequest -> setTimeout ( 500 );
2017-05-08 17:45:16 +02:00
$asyncHttpRequest -> postData ();
2014-05-04 00:36:20 +02:00
}
/**
2014-06-14 22:30:33 +02:00
* Handle Player Disconnect Callback
2014-05-04 00:36:20 +02:00
*
2014-06-14 22:30:33 +02:00
* @ param Player $player
2014-05-04 00:36:20 +02:00
*/
public function handlePlayerDisconnect ( Player $player ) {
2015-01-13 18:06:32 +01:00
if ( ! isset ( $this -> dedimaniaData )) {
2014-06-14 22:30:33 +02:00
return ;
}
2014-05-04 00:36:20 +02:00
$this -> dedimaniaData -> removePlayer ( $player -> login );
// Send Dedimania request
$data = array ( $this -> dedimaniaData -> sessionId , $player -> login , '' );
$content = $this -> encode_request ( self :: DEDIMANIA_PLAYERDISCONNECT , $data );
2017-05-08 17:45:16 +02:00
$asyncHttpRequest = new AsyncHttpRequest ( $this -> maniaControl , self :: DEDIMANIA_URL );
$asyncHttpRequest -> setCallable ( function ( $data , $error ) {
2015-01-12 13:33:40 +01:00
if ( $error ) {
2017-05-09 14:02:00 +02:00
$this -> openDedimaniaSession ();
2015-01-12 13:33:40 +01:00
}
$data = $this -> decode ( $data );
if ( ! is_array ( $data ) || empty ( $data )) {
return ;
}
$methodResponse = $data [ 0 ];
if ( xmlrpc_is_fault ( $methodResponse )) {
$this -> handleXmlRpcFault ( $methodResponse , self :: DEDIMANIA_PLAYERDISCONNECT );
}
2017-05-08 17:45:16 +02:00
});
$asyncHttpRequest -> setContent ( $content );
$asyncHttpRequest -> setCompression ( true );
2017-05-08 18:53:56 +02:00
$asyncHttpRequest -> setTimeout ( 500 );
2017-05-08 17:45:16 +02:00
$asyncHttpRequest -> postData ();
2014-05-04 00:36:20 +02:00
}
/**
2014-05-13 16:40:05 +02:00
* Handle Begin Map Callback
2014-05-04 00:36:20 +02:00
*/
2014-05-13 16:40:05 +02:00
public function handleBeginMap () {
2014-05-04 00:36:20 +02:00
unset ( $this -> dedimaniaData -> records );
2017-05-08 17:45:16 +02:00
$this -> updateManialink = true ;
2014-05-04 00:36:20 +02:00
$this -> fetchDedimaniaRecords ( true );
}
/**
2014-05-13 16:40:05 +02:00
* Handle EndMap Callback
2014-05-04 00:36:20 +02:00
*/
2014-05-13 16:40:05 +02:00
public function handleMapEnd () {
2017-05-08 21:31:35 +02:00
if ( ! isset ( $this -> dedimaniaData ) || ! isset ( $this -> dedimaniaData -> records )) {
2014-05-04 00:36:20 +02:00
return ;
}
2017-05-08 17:45:16 +02:00
//Finish Counts as CP somehow
if ( $this -> maniaControl -> getMapManager () -> getCurrentMap () -> nbCheckpoints < 2 ) {
return ;
}
2017-05-09 14:02:00 +02:00
//Make Sure Dedimania Session is okay
$this -> checkDedimaniaSession ();
2014-05-04 00:36:20 +02:00
// Send dedimania records
$gameMode = $this -> getGameModeString ();
$times = array ();
$replays = array ();
foreach ( $this -> dedimaniaData -> records as $record ) {
if ( $record -> rank > $this -> dedimaniaData -> serverMaxRank ) {
break ;
}
2014-06-14 14:32:29 +02:00
if ( ! $record -> newRecord ) {
2014-05-04 00:36:20 +02:00
continue ;
}
array_push ( $times , array ( 'Login' => $record -> login , 'Best' => $record -> best , 'Checks' => $record -> checkpoints ));
if ( ! isset ( $replays [ 'VReplay' ])) {
$replays [ 'VReplay' ] = $record -> vReplay ;
}
if ( ! isset ( $replays [ 'Top1GReplay' ])) {
$replays [ 'Top1GReplay' ] = $record -> top1GReplay ;
}
if ( ! isset ( $replays [ 'VReplayChecks' ])) {
$replays [ 'VReplayChecks' ] = '' ;
// TODO: VReplayChecks
}
}
xmlrpc_set_type ( $replays [ 'VReplay' ], 'base64' );
xmlrpc_set_type ( $replays [ 'Top1GReplay' ], 'base64' );
2017-05-08 17:45:16 +02:00
$data = array ( $this -> dedimaniaData -> sessionId , $this -> getMapInfo (), $gameMode , $times , $replays );
2014-06-17 23:35:56 +02:00
$content = $this -> encode_request ( self :: DEDIMANIA_SET_CHALLENGE_TIMES , $data );
2014-05-04 00:36:20 +02:00
2014-07-05 18:02:27 +02:00
if ( self :: DEDIMANIA_DEBUG ) {
var_dump ( " Dedimania Debug: Submitting Times at End-Map " , $content );
}
2017-05-08 17:45:16 +02:00
$asyncHttpRequest = new AsyncHttpRequest ( $this -> maniaControl , self :: DEDIMANIA_URL );
$asyncHttpRequest -> setCallable ( function ( $data , $error ) {
2015-01-12 13:33:40 +01:00
if ( $error ) {
2017-05-08 18:53:56 +02:00
Logger :: logError ( " Dedimania Error while submitting times: " . $error );
2015-01-12 13:33:40 +01:00
}
if ( self :: DEDIMANIA_DEBUG ) {
var_dump ( " Dedimania Debug: Submit Data Response " );
var_dump ( $data );
}
$data = $this -> decode ( $data );
if ( ! is_array ( $data ) || empty ( $data )) {
return ;
}
$methodResponse = $data [ 0 ];
if ( xmlrpc_is_fault ( $methodResponse )) {
$this -> handleXmlRpcFault ( $methodResponse , self :: DEDIMANIA_SET_CHALLENGE_TIMES );
return ;
}
// Called method response
if ( ! $methodResponse [ 0 ]) {
Logger :: logError ( " Records Plugin: Submitting dedimania records failed. " );
}
if ( self :: DEDIMANIA_DEBUG ) {
var_dump ( " Dedimania Debug: endMap response " );
var_dump ( $methodResponse );
var_dump ( " Dedimania Data " );
var_dump ( $this -> dedimaniaData );
}
2017-05-08 17:45:16 +02:00
});
$asyncHttpRequest -> setContent ( $content );
$asyncHttpRequest -> setCompression ( false );
2017-05-08 18:53:56 +02:00
$asyncHttpRequest -> setTimeout ( 500 );
2017-05-08 17:45:16 +02:00
$asyncHttpRequest -> postData ();
2014-05-04 00:36:20 +02:00
}
/**
2014-05-13 16:40:05 +02:00
* Update the PlayerList every 3 Minutes
2014-05-04 00:36:20 +02:00
*/
2014-05-13 16:40:05 +02:00
public function updatePlayerList () {
2014-05-04 00:36:20 +02:00
$serverInfo = $this -> getServerInfo ();
$playerList = $this -> getPlayerList ();
$votesInfo = $this -> getVotesInfo ();
2014-06-14 14:32:29 +02:00
if ( ! $serverInfo || ! $votesInfo || ! $playerList || ! isset ( $this -> dedimaniaData ) || ! $this -> dedimaniaData -> sessionId ) {
2014-05-04 00:36:20 +02:00
return ;
}
// Send Dedimania request
$data = array ( $this -> dedimaniaData -> sessionId , $serverInfo , $votesInfo , $playerList );
2014-06-17 23:35:56 +02:00
$content = $this -> encode_request ( self :: DEDIMANIA_UPDATE_SERVER_PLAYERS , $data );
2014-05-04 00:36:20 +02:00
2017-05-08 17:45:16 +02:00
$asyncHttpRequest = new AsyncHttpRequest ( $this -> maniaControl , self :: DEDIMANIA_URL );
$asyncHttpRequest -> setCallable ( function ( $data , $error ) {
2015-01-12 13:33:40 +01:00
if ( $error ) {
2017-05-08 18:53:56 +02:00
Logger :: logError ( " Dedimania Error while update playerlist: " . $error );
2015-01-12 13:33:40 +01:00
}
$data = $this -> decode ( $data );
if ( ! is_array ( $data ) || empty ( $data )) {
return ;
}
$methodResponse = $data [ 0 ];
if ( xmlrpc_is_fault ( $methodResponse )) {
$this -> handleXmlRpcFault ( $methodResponse , self :: DEDIMANIA_UPDATE_SERVER_PLAYERS );
}
2017-05-08 17:45:16 +02:00
});
$asyncHttpRequest -> setContent ( $content );
$asyncHttpRequest -> setCompression ( true );
2017-05-08 18:53:56 +02:00
$asyncHttpRequest -> setTimeout ( 500 );
2017-05-08 17:45:16 +02:00
$asyncHttpRequest -> postData ();
2014-05-04 00:36:20 +02:00
}
/**
2014-05-13 16:03:26 +02:00
* Build Votes Info Array for Callbacks
2014-05-04 00:36:20 +02:00
*/
private function getVotesInfo () {
2014-08-13 11:14:29 +02:00
$map = $this -> maniaControl -> getMapManager () -> getCurrentMap ();
2014-05-04 00:36:20 +02:00
if ( ! $map ) {
return null ;
}
$gameMode = $this -> getGameModeString ();
if ( ! $gameMode ) {
return null ;
}
return array ( 'UId' => $map -> uid , 'GameMode' => $gameMode );
}
/**
2014-05-24 20:29:32 +02:00
* Handle Checkpoint Callback
2014-05-04 00:36:20 +02:00
*
2017-05-08 19:55:07 +02:00
* @ param OnWayPointEventStructure $callback
2014-05-24 20:29:32 +02:00
*/
2017-05-08 19:55:07 +02:00
public function handleCheckpointCallback ( OnWayPointEventStructure $structure ) {
2017-05-08 17:45:16 +02:00
if ( ! $structure -> getLapTime ()) {
return ;
}
2017-05-11 23:35:59 +02:00
$login = $structure -> getLogin ();
2017-05-08 17:45:16 +02:00
if ( ! isset ( $this -> checkpoints [ $login ])) {
$this -> checkpoints [ $login ] = array ();
}
$this -> checkpoints [ $login ][ $structure -> getCheckPointInLap ()] = $structure -> getLapTime ();
2014-05-24 20:29:32 +02:00
}
/**
* Handle Finish Callback
*
2017-05-08 19:55:07 +02:00
* @ param OnWayPointEventStructure $callback
2014-05-24 20:29:32 +02:00
*/
2017-05-08 19:55:07 +02:00
public function handleFinishCallback ( OnWayPointEventStructure $structure ) {
2017-05-08 17:45:16 +02:00
if ( ! isset ( $this -> dedimaniaData )) {
2014-07-20 00:24:25 +02:00
return ;
}
2017-05-08 19:55:07 +02:00
if ( $structure -> getRaceTime () <= 0 ) {
2014-05-24 20:29:32 +02:00
// Invalid time
2014-05-04 00:36:20 +02:00
return ;
}
2014-08-13 11:14:29 +02:00
$map = $this -> maniaControl -> getMapManager () -> getCurrentMap ();
2014-05-04 00:36:20 +02:00
if ( ! $map ) {
return ;
}
2017-05-08 17:45:16 +02:00
if ( $map -> nbCheckpoints < 2 ) {
return ;
}
2017-05-08 19:55:07 +02:00
$player = $structure -> getPlayer ();
$oldRecord = $this -> getDedimaniaRecord ( $player -> login );
if ( $oldRecord -> nullRecord || $oldRecord && $oldRecord -> best > $structure -> getLapTime ()) {
2014-05-04 00:36:20 +02:00
// Save time
$newRecord = new RecordData ( null );
2017-05-08 17:45:16 +02:00
2017-05-08 19:55:07 +02:00
$checkPoints = $this -> getCheckpoints ( $player -> login );
$checkPoints = $checkPoints . " , " . $structure -> getLapTime ();
2017-05-08 17:45:16 +02:00
2017-05-08 19:55:07 +02:00
$newRecord -> constructNewRecord ( $player -> login , $player -> nickname , $structure -> getLapTime (), $checkPoints , true );
2017-05-08 17:45:16 +02:00
2014-05-04 00:36:20 +02:00
if ( $this -> insertDedimaniaRecord ( $newRecord , $oldRecord )) {
// Get newly saved record
foreach ( $this -> dedimaniaData -> records as & $record ) {
if ( $record -> login !== $newRecord -> login ) {
continue ;
}
$newRecord = $record ;
break ;
}
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getCallbackManager () -> triggerCallback ( self :: CB_DEDIMANIA_CHANGED , $newRecord );
2014-05-04 00:36:20 +02:00
// Announce record
if ( $oldRecord -> nullRecord || $newRecord -> rank < $oldRecord -> rank ) {
// Gained rank
$improvement = 'gained the' ;
} else {
// Only improved time
2017-05-08 20:36:29 +02:00
$improvement = 'improved the' ;
2014-05-04 00:36:20 +02:00
}
2017-05-08 19:55:07 +02:00
$message = '$390$<$fff' . $player -> nickname . '$> ' . $improvement . ' $<$ff0' . $newRecord -> rank . '.$> Dedimania Record: $<$fff' . Formatter :: formatTime ( $newRecord -> best ) . '$>' ;
2014-05-04 00:36:20 +02:00
if ( ! $oldRecord -> nullRecord ) {
2017-05-08 19:55:07 +02:00
$message .= ' ($<$ff0' . $oldRecord -> rank . '.$> $<$fff-' . Formatter :: formatTime (( $oldRecord -> best - $structure -> getLapTime ())) . '$>)' ;
2014-05-04 00:36:20 +02:00
}
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getChat () -> sendInformation ( $message . '!' );
2014-05-04 00:36:20 +02:00
$this -> updateManialink = true ;
}
}
}
/**
* Get the dedimania record of the given login
*
* @ param string $login
* @ return RecordData $record
*/
private function getDedimaniaRecord ( $login ) {
2017-05-08 19:55:07 +02:00
if ( ! isset ( $this -> dedimaniaData ) || ! isset ( $this -> dedimaniaData -> records )) {
2014-05-04 00:36:20 +02:00
return new RecordData ( null );
}
$records = $this -> dedimaniaData -> records ;
foreach ( $records as & $record ) {
if ( $record -> login === $login ) {
return $record ;
}
}
return new RecordData ( null );
}
/**
* Get current checkpoint string for dedimania record
*
* @ param string $login
* @ return string
*/
private function getCheckpoints ( $login ) {
if ( ! $login || ! isset ( $this -> checkpoints [ $login ])) {
return null ;
}
$string = '' ;
$count = count ( $this -> checkpoints [ $login ]);
foreach ( $this -> checkpoints [ $login ] as $index => $check ) {
$string .= $check ;
if ( $index < $count - 1 ) {
$string .= ',' ;
}
}
return $string ;
}
/**
* Inserts the given new Dedimania record at the proper position
*
2014-05-09 17:31:29 +02:00
* @ param RecordData $newRecord
* @ param RecordData $oldRecord
2014-05-04 00:36:20 +02:00
* @ return bool
*/
private function insertDedimaniaRecord ( RecordData & $newRecord , RecordData $oldRecord ) {
2017-05-08 17:45:16 +02:00
if ( ! isset ( $this -> dedimaniaData ) || ! isset ( $this -> dedimaniaData -> records ) || $newRecord -> nullRecord ) {
2014-05-04 00:36:20 +02:00
return false ;
}
$insert = false ;
// Get max possible rank
$maxRank = $this -> dedimaniaData -> getPlayerMaxRank ( $newRecord -> login );
// Loop through existing records
foreach ( $this -> dedimaniaData -> records as $key => & $record ) {
if ( $record -> rank > $maxRank ) {
// Max rank reached
return false ;
}
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 [ $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
foreach ( $this -> dedimaniaData -> records as $key2 => $record2 ) {
if ( $record2 -> login !== $oldRecord -> login ) {
continue ;
}
unset ( $this -> dedimaniaData -> records [ $key2 ]);
break ;
}
}
break ;
}
if ( ! $insert && count ( $this -> dedimaniaData -> records ) < $maxRank ) {
// Records list not full - Append new record
$insert = true ;
}
if ( $insert ) {
// Insert new record
array_push ( $this -> dedimaniaData -> records , $newRecord );
// Update ranks
$this -> updateDedimaniaRecordRanks ();
// Save replays
foreach ( $this -> dedimaniaData -> records as & $record ) {
if ( $record -> login !== $newRecord -> login ) {
continue ;
}
$this -> setRecordReplays ( $record );
break ;
}
// Record inserted
return true ;
}
// No new record
return false ;
}
/**
* Update the sorting and the ranks of all dedimania records
*/
private function updateDedimaniaRecordRanks () {
2014-06-14 14:32:29 +02:00
if ( $this -> dedimaniaData -> getRecordCount () === 0 ) {
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getCallbackManager () -> triggerCallback ( self :: CB_DEDIMANIA_UPDATED , $this -> dedimaniaData -> records );
2014-05-04 00:36:20 +02:00
return ;
}
2014-06-14 15:48:27 +02:00
$this -> dedimaniaData -> sortRecords ();
2014-05-04 00:36:20 +02:00
// Update ranks
$rank = 1 ;
foreach ( $this -> dedimaniaData -> records as & $record ) {
$record -> rank = $rank ;
$rank ++ ;
}
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getCallbackManager () -> triggerCallback ( self :: CB_DEDIMANIA_UPDATED , $this -> dedimaniaData -> records );
2014-05-04 00:36:20 +02:00
}
/**
2014-05-09 17:31:29 +02:00
* Update the replay values for the given record
2014-05-04 00:36:20 +02:00
*
2014-05-09 17:31:29 +02:00
* @ param RecordData $record
2014-05-04 00:36:20 +02:00
*/
private function setRecordReplays ( RecordData & $record ) {
// Set validation replay
2017-05-12 21:28:26 +02:00
try {
2017-05-13 15:15:37 +02:00
//TODO verify why it can be that login is not set
if ( ! $record -> login ){
return ;
}
2017-05-12 21:28:26 +02:00
$validationReplay = $this -> maniaControl -> getServer () -> getValidationReplay ( $record -> login );
if ( $validationReplay ) {
$record -> vReplay = $validationReplay ;
}
} catch ( UnavailableFeatureException $e ){
Logger :: logError ( " Unable to get Validation Replay from the Server " );
2014-05-04 00:36:20 +02:00
}
2017-05-12 21:28:26 +02:00
2014-05-04 00:36:20 +02:00
// Set ghost replay
if ( $record -> rank <= 1 ) {
2014-08-13 11:14:29 +02:00
$dataDirectory = $this -> maniaControl -> getServer () -> getDirectory () -> getGameDataFolder ();
2014-05-04 00:36:20 +02:00
if ( ! isset ( $this -> dedimaniaData -> directoryAccessChecked )) {
2014-08-13 11:14:29 +02:00
$access = $this -> maniaControl -> getServer () -> checkAccess ( $dataDirectory );
2014-05-04 00:36:20 +02:00
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-08-13 11:14:29 +02:00
$ghostReplay = $this -> maniaControl -> getServer () -> getGhostReplay ( $record -> login );
2014-05-04 00:36:20 +02:00
if ( $ghostReplay ) {
$record -> top1GReplay = $ghostReplay ;
}
}
}
}
/**
* Handle PlayerManialinkPageAnswer callback
*
* @ param array $callback
*/
public function handleManialinkPageAnswer ( array $callback ) {
$actionId = $callback [ 1 ][ 2 ];
2017-05-12 21:06:43 +02:00
//TODO use manialinkpageanswerlistener
2014-05-04 00:36:20 +02:00
$login = $callback [ 1 ][ 1 ];
2014-08-13 11:14:29 +02:00
$player = $this -> maniaControl -> getPlayerManager () -> getPlayer ( $login );
2014-05-04 00:36:20 +02:00
2014-06-14 14:32:29 +02:00
if ( $actionId === self :: ACTION_SHOW_DEDIRECORDSLIST ) {
2014-05-04 00:36:20 +02:00
$this -> showDediRecordsList ( array (), $player );
}
}
/**
* Shows a ManiaLink list with the local records .
*
* @ param array $chat
* @ param Player $player
*/
public function showDediRecordsList ( array $chat , Player $player ) {
2014-08-13 11:14:29 +02:00
$width = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getListWidgetsWidth ();
$height = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getListWidgetsHeight ();
2014-05-04 00:36:20 +02:00
// get PlayerList
$records = $this -> dedimaniaData -> records ;
if ( ! $records ) {
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getChat () -> sendInformation ( 'There are no Dedimania records on this map!' );
2014-05-04 00:36:20 +02:00
return ;
}
//create manialink
$maniaLink = new ManiaLink ( ManialinkManager :: MAIN_MLID );
$script = $maniaLink -> getScript ();
$paging = new Paging ();
$script -> addFeature ( $paging );
// Main frame
2014-08-13 11:14:29 +02:00
$frame = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getDefaultListFrame ( $script , $paging );
2017-03-25 19:15:50 +01:00
$maniaLink -> addChild ( $frame );
2014-05-04 00:36:20 +02:00
// Start offsets
2014-06-14 15:48:27 +02:00
$posX = - $width / 2 ;
$posY = $height / 2 ;
2014-05-04 00:36:20 +02:00
// Predefine Description Label
2014-08-13 11:14:29 +02:00
$descriptionLabel = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getDefaultDescriptionLabel ();
2017-03-25 19:15:50 +01:00
$frame -> addChild ( $descriptionLabel );
2014-05-04 00:36:20 +02:00
// Headline
$headFrame = new Frame ();
2017-03-25 19:15:50 +01:00
$frame -> addChild ( $headFrame );
2014-06-14 15:48:27 +02:00
$headFrame -> setY ( $posY - 5 );
2014-08-03 01:34:18 +02:00
$array = array ( 'Rank' => $posX + 5 , 'Nickname' => $posX + 18 , 'Login' => $posX + 70 , 'Time' => $posX + 101 );
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getManialinkManager () -> labelLine ( $headFrame , $array );
2014-05-04 00:36:20 +02:00
2014-06-14 15:48:27 +02:00
$index = 0 ;
$posY = $height / 2 - 10 ;
2014-05-15 17:45:08 +02:00
$pageFrame = null ;
2014-05-04 00:36:20 +02:00
foreach ( $records as $listRecord ) {
2014-06-14 15:48:27 +02:00
if ( $index % 15 === 0 ) {
2014-05-04 00:36:20 +02:00
$pageFrame = new Frame ();
2017-03-25 19:15:50 +01:00
$frame -> addChild ( $pageFrame );
2014-06-14 15:48:27 +02:00
$posY = $height / 2 - 10 ;
2017-05-08 17:45:16 +02:00
$paging -> addPageControl ( $pageFrame );
2014-05-04 00:36:20 +02:00
}
$recordFrame = new Frame ();
2017-03-25 19:15:50 +01:00
$pageFrame -> addChild ( $recordFrame );
2014-05-04 00:36:20 +02:00
2014-06-14 15:48:27 +02:00
if ( $index % 2 !== 0 ) {
2014-05-04 00:36:20 +02:00
$lineQuad = new Quad_BgsPlayerCard ();
2017-03-25 19:15:50 +01:00
$recordFrame -> addChild ( $lineQuad );
2014-05-04 00:36:20 +02:00
$lineQuad -> setSize ( $width , 4 );
$lineQuad -> setSubStyle ( $lineQuad :: SUBSTYLE_BgPlayerCardBig );
$lineQuad -> setZ ( 0.001 );
}
if ( strlen ( $listRecord -> nickName ) < 2 ) {
$listRecord -> nickName = $listRecord -> login ;
}
2014-06-14 15:48:27 +02:00
$array = array ( $listRecord -> rank => $posX + 5 , '$fff' . $listRecord -> nickName => $posX + 18 , $listRecord -> login => $posX + 70 , Formatter :: formatTime ( $listRecord -> best ) => $posX + 101 );
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getManialinkManager () -> labelLine ( $recordFrame , $array );
2014-05-04 00:36:20 +02:00
2014-06-14 15:48:27 +02:00
$recordFrame -> setY ( $posY );
2014-05-04 00:36:20 +02:00
2014-06-14 15:48:27 +02:00
$posY -= 4 ;
$index ++ ;
2014-05-04 00:36:20 +02:00
}
// Render and display xml
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getManialinkManager () -> displayWidget ( $maniaLink , $player , 'DediRecordsList' );
2014-05-04 00:36:20 +02:00
}
/**
* Function to retrieve the dedimania records on the current map
*
2014-07-20 00:24:25 +02:00
* @ return RecordData []
2014-05-04 00:36:20 +02:00
*/
public function getDedimaniaRecords () {
2014-07-20 00:24:25 +02:00
if ( $this -> dedimaniaData && $this -> dedimaniaData -> records ) {
2014-05-27 23:00:39 +02:00
return $this -> dedimaniaData -> records ;
2014-05-04 00:36:20 +02:00
}
2014-05-27 23:00:39 +02:00
return null ;
2014-05-04 00:36:20 +02:00
}
/**
* @ see \ManiaControl\Plugins\Plugin :: unload ()
*/
public function unload () {
}
2014-07-20 00:19:54 +02:00
}