2014-05-03 21:57:38 +02:00
< ? php
namespace MCTeam ;
use FML\Controls\Control ;
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\Admin\AuthenticationManager ;
use ManiaControl\Callbacks\CallbackListener ;
use ManiaControl\Callbacks\CallbackManager ;
use ManiaControl\Callbacks\Callbacks ;
2014-05-24 19:16:48 +02:00
use ManiaControl\Callbacks\Models\RecordCallback ;
2014-05-03 21:57:38 +02:00
use ManiaControl\Callbacks\TimerListener ;
use ManiaControl\Commands\CommandListener ;
use ManiaControl\ManiaControl ;
use ManiaControl\Manialinks\ManialinkManager ;
2014-05-03 23:49:58 +02:00
use ManiaControl\Maps\Map ;
2014-05-03 21:57:38 +02:00
use ManiaControl\Players\Player ;
use ManiaControl\Players\PlayerManager ;
use ManiaControl\Plugins\Plugin ;
2014-05-13 14:15:00 +02:00
use ManiaControl\Settings\Setting ;
2014-05-03 23:49:58 +02:00
use ManiaControl\Settings\SettingManager ;
2014-05-13 16:40:05 +02:00
use ManiaControl\Utils\Formatter ;
2014-05-03 21:57:38 +02:00
/**
* ManiaControl Local Records Plugin
2014-05-03 23:49:58 +02:00
*
* @ author ManiaControl Team < mail @ maniacontrol . com >
* @ copyright 2014 ManiaControl Team
* @ license http :// www . gnu . org / licenses / GNU General Public License , Version 3
2014-05-03 21:57:38 +02:00
*/
class LocalRecordsPlugin implements CallbackListener , CommandListener , TimerListener , Plugin {
/*
* Constants
*/
2014-05-03 23:49:58 +02:00
const ID = 7 ;
const VERSION = 0.2 ;
2014-05-13 14:15:00 +02:00
const NAME = 'Local Records Plugin' ;
const AUTHOR = 'MCTeam' ;
2014-05-03 23:49:58 +02:00
const MLID_RECORDS = 'ml_local_records' ;
const TABLE_RECORDS = 'mc_localrecords' ;
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_WIDGET_ENABLE = 'Enable Local Records Widget' ;
const SETTING_NOTIFY_ONLY_DRIVER = 'Notify only the Driver on New Records' ;
2014-05-03 21:57:38 +02:00
const SETTING_NOTIFY_BEST_RECORDS = 'Notify Publicly only for the X Best Records' ;
const SETTING_ADJUST_OUTER_BORDER = 'Adjust outer Border to Number of actual Records' ;
2014-05-03 23:49:58 +02:00
const CB_LOCALRECORDS_CHANGED = 'LocalRecords.Changed' ;
const ACTION_SHOW_RECORDSLIST = 'LocalRecords.ShowRecordsList' ;
2014-05-03 21:57:38 +02:00
/*
* Private Properties
*/
2014-05-03 23:49:58 +02:00
/** @var ManiaControl $maniaControl */
2014-05-03 21:57:38 +02:00
private $maniaControl = null ;
private $updateManialink = false ;
private $checkpoints = array ();
/**
2014-05-03 23:49:58 +02:00
* @ see \ManiaControl\Plugins\Plugin :: prepare ()
2014-05-03 21:57:38 +02:00
*/
public static function prepare ( ManiaControl $maniaControl ) {
}
/**
2014-05-03 23:49:58 +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 () {
return 'Plugin offering tracking of local records and manialinks to display them.' ;
}
/**
2014-05-03 21:57:38 +02:00
* @ see \ManiaControl\Plugins\Plugin :: load ()
*/
public function load ( ManiaControl $maniaControl ) {
$this -> maniaControl = $maniaControl ;
$this -> initTables ();
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
// Init settings
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_WIDGET_TITLE , 'Local Records' );
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_WIDGET_POSX , - 139. );
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_WIDGET_POSY , 75 );
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_WIDGET_WIDTH , 40. );
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_WIDGET_LINESCOUNT , 15 );
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_WIDGET_LINEHEIGHT , 4. );
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_WIDGET_ENABLE , true );
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_NOTIFY_ONLY_DRIVER , false );
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_NOTIFY_BEST_RECORDS , - 1 );
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_ADJUST_OUTER_BORDER , false );
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
// Register for callbacks
$this -> maniaControl -> timerManager -> registerTimerListening ( $this , 'handle1Second' , 1000 );
2014-05-24 19:16:48 +02:00
2014-05-24 16:39:12 +02:00
$this -> maniaControl -> callbackManager -> registerCallbackListener ( Callbacks :: AFTERINIT , $this , 'handleAfterInit' );
2014-05-03 21:57:38 +02:00
$this -> maniaControl -> callbackManager -> registerCallbackListener ( Callbacks :: BEGINMAP , $this , 'handleMapBegin' );
2014-05-13 14:15:00 +02:00
$this -> maniaControl -> callbackManager -> registerCallbackListener ( SettingManager :: CB_SETTING_CHANGED , $this , 'handleSettingChanged' );
2014-05-03 21:57:38 +02:00
$this -> maniaControl -> callbackManager -> registerCallbackListener ( CallbackManager :: CB_MP_PLAYERMANIALINKPAGEANSWER , $this , 'handleManialinkPageAnswer' );
2014-05-24 19:16:48 +02:00
$this -> maniaControl -> callbackManager -> registerCallbackListener ( PlayerManager :: CB_PLAYERCONNECT , $this , 'handlePlayerConnect' );
$this -> maniaControl -> callbackManager -> registerCallbackListener ( RecordCallback :: CHECKPOINT , $this , 'handleCheckpointCallback' );
$this -> maniaControl -> callbackManager -> registerCallbackListener ( RecordCallback :: LAPFINISH , $this , 'handleLapFinishCallback' );
$this -> maniaControl -> callbackManager -> registerCallbackListener ( RecordCallback :: FINISH , $this , 'handleFinishCallback' );
2014-05-03 21:57:38 +02:00
$this -> maniaControl -> commandManager -> registerCommandListener ( array ( 'recs' , 'records' ), $this , 'showRecordsList' , false , 'Shows a list of Local Records on the current map.' );
$this -> maniaControl -> commandManager -> registerCommandListener ( 'delrec' , $this , 'deleteRecord' , true , 'Removes a record from the database.' );
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
$this -> updateManialink = true ;
2014-05-03 23:49:58 +02:00
return true ;
2014-05-03 21:57:38 +02:00
}
/**
* Initialize needed database tables
*/
private function initTables () {
$mysqli = $this -> maniaControl -> database -> mysqli ;
2014-05-03 23:49:58 +02:00
$query = " CREATE TABLE IF NOT EXISTS ` " . self :: TABLE_RECORDS . " ` (
2014-05-03 21:57:38 +02:00
`index` int ( 11 ) NOT NULL AUTO_INCREMENT ,
`mapIndex` int ( 11 ) NOT NULL ,
`playerIndex` int ( 11 ) NOT NULL ,
`time` int ( 11 ) NOT NULL ,
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
PRIMARY KEY ( `index` ),
UNIQUE KEY `player_map_record` ( `mapIndex` , `playerIndex` )
) ENGINE = MyISAM DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci AUTO_INCREMENT = 1 ; " ;
$mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error , E_USER_ERROR );
}
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
$mysqli -> query ( " ALTER TABLE ` " . self :: TABLE_RECORDS . " ` ADD `checkpoints` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL " );
if ( $mysqli -> error ) {
if ( ! strstr ( $mysqli -> error , 'Duplicate' )) {
trigger_error ( $mysqli -> error , E_USER_ERROR );
}
}
}
/**
2014-05-03 23:49:58 +02:00
* @ see \ManiaControl\Plugins\Plugin :: unload ()
2014-05-03 21:57:38 +02:00
*/
2014-05-03 23:49:58 +02:00
public function unload () {
2014-05-09 20:12:08 +02:00
$this -> maniaControl -> manialinkManager -> hideManialink ( self :: MLID_RECORDS );
2014-05-03 21:57:38 +02:00
}
/**
* Handle ManiaControl After Init
*/
public function handleAfterInit () {
$this -> updateManialink = true ;
}
/**
2014-05-13 16:40:05 +02:00
* Handle 1 Second Callback
2014-05-03 21:57:38 +02:00
*/
2014-05-13 16:40:05 +02:00
public function handle1Second () {
2014-05-03 21:57:38 +02:00
if ( ! $this -> updateManialink ) {
return ;
}
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
$this -> updateManialink = false ;
2014-05-13 16:03:26 +02:00
if ( $this -> maniaControl -> settingManager -> getSettingValue ( $this , self :: SETTING_WIDGET_ENABLE )) {
2014-05-03 21:57:38 +02:00
$manialink = $this -> buildManialink ();
$this -> maniaControl -> manialinkManager -> sendManialink ( $manialink );
}
}
2014-05-03 23:49:58 +02:00
/**
* Build the local records manialink
*
* @ return string
*/
private function buildManialink () {
$map = $this -> maniaControl -> mapManager -> getCurrentMap ();
if ( ! $map ) {
return null ;
}
2014-05-13 16:03:26 +02:00
$title = $this -> maniaControl -> settingManager -> getSettingValue ( $this , self :: SETTING_WIDGET_TITLE );
$pos_x = $this -> maniaControl -> settingManager -> getSettingValue ( $this , self :: SETTING_WIDGET_POSX );
$pos_y = $this -> maniaControl -> settingManager -> getSettingValue ( $this , self :: SETTING_WIDGET_POSY );
$width = $this -> maniaControl -> settingManager -> getSettingValue ( $this , self :: SETTING_WIDGET_WIDTH );
$lines = $this -> maniaControl -> settingManager -> getSettingValue ( $this , self :: SETTING_WIDGET_LINESCOUNT );
$lineHeight = $this -> maniaControl -> settingManager -> getSettingValue ( $this , self :: SETTING_WIDGET_LINEHEIGHT );
2014-05-03 23:49:58 +02:00
$labelStyle = $this -> maniaControl -> manialinkManager -> styleManager -> getDefaultLabelStyle ();
$quadStyle = $this -> maniaControl -> manialinkManager -> styleManager -> getDefaultQuadStyle ();
$quadSubstyle = $this -> maniaControl -> manialinkManager -> styleManager -> getDefaultQuadSubstyle ();
$records = $this -> getLocalRecords ( $map );
if ( ! is_array ( $records )) {
trigger_error ( " Couldn't fetch player records. " );
return null ;
}
$manialink = new ManiaLink ( self :: MLID_RECORDS );
$frame = new Frame ();
$manialink -> add ( $frame );
$frame -> setPosition ( $pos_x , $pos_y );
$backgroundQuad = new Quad ();
$frame -> add ( $backgroundQuad );
$backgroundQuad -> setVAlign ( Control :: TOP );
2014-05-13 16:03:26 +02:00
$adjustOuterBorder = $this -> maniaControl -> settingManager -> getSettingValue ( $this , self :: SETTING_ADJUST_OUTER_BORDER );
2014-05-03 23:49:58 +02:00
$height = 7. + ( $adjustOuterBorder ? count ( $records ) : $lines ) * $lineHeight ;
$backgroundQuad -> setSize ( $width * 1.05 , $height );
$backgroundQuad -> setStyles ( $quadStyle , $quadSubstyle );
$backgroundQuad -> setAction ( self :: ACTION_SHOW_RECORDSLIST );
$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 );
// Times
foreach ( $records as $index => $record ) {
if ( $index >= $lines ) {
break ;
}
$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 );
*/
$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 );
$rankLabel -> setTextEmboss ( true );
$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 ( $record -> nickname );
$nameLabel -> setTextEmboss ( true );
$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 -> time ));
$timeLabel -> setTextEmboss ( true );
}
return $manialink ;
}
/**
* Fetch local records for the given map
*
* @ param Map $map
* @ param int $limit
* @ return array
*/
public function getLocalRecords ( Map $map , $limit = - 1 ) {
$mysqli = $this -> maniaControl -> database -> mysqli ;
2014-06-14 14:32:29 +02:00
$limit = ( $limit > 0 ? 'LIMIT ' . $limit : '' );
2014-05-03 23:49:58 +02:00
$query = " SELECT * FROM (
SELECT recs .* , @ rank := @ rank + 1 as `rank` FROM `" . self::TABLE_RECORDS . "` recs , ( SELECT @ rank := 0 ) ra
WHERE recs . `mapIndex` = { $map -> index }
ORDER BY recs . `time` ASC
{ $limit }) records
LEFT JOIN `" . PlayerManager::TABLE_PLAYERS . "` players
ON records . `playerIndex` = players . `index` ; " ;
$result = $mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error );
return null ;
}
$records = array ();
while ( $record = $result -> fetch_object ()) {
array_push ( $records , $record );
}
$result -> free ();
return $records ;
}
2014-05-13 14:15:00 +02:00
/**
* Handle Setting Changed Callback
*
* @ param Setting $setting
*/
2014-05-13 16:54:32 +02:00
public function handleSettingChanged ( Setting $setting ) {
2014-05-13 14:15:00 +02:00
if ( ! $setting -> belongsToClass ( $this )) {
2014-05-03 21:57:38 +02:00
return ;
}
2014-05-13 14:15:00 +02:00
switch ( $setting -> setting ) {
case self :: SETTING_WIDGET_ENABLE :
{
if ( $setting -> value ) {
$this -> updateManialink = true ;
} else {
$this -> maniaControl -> manialinkManager -> hideManialink ( self :: MLID_RECORDS );
}
break ;
}
2014-05-03 21:57:38 +02:00
}
}
/**
2014-05-24 19:16:48 +02:00
* Handle Checkpoint Callback
2014-05-03 23:49:58 +02:00
*
2014-05-24 19:16:48 +02:00
* @ param RecordCallback $callback
2014-05-03 21:57:38 +02:00
*/
2014-05-24 19:16:48 +02:00
public function handleCheckpointCallback ( RecordCallback $callback ) {
if ( $callback -> isLegacyCallback ) {
return ;
2014-05-03 21:57:38 +02:00
}
2014-05-24 19:16:48 +02:00
if ( ! isset ( $this -> checkpoints [ $callback -> login ])) {
$this -> checkpoints [ $callback -> login ] = array ();
}
$this -> checkpoints [ $callback -> login ][ $callback -> lapCheckpoint ] = $callback -> lapTime ;
2014-05-03 21:57:38 +02:00
}
/**
2014-05-24 19:16:48 +02:00
* Handle LapFinish Callback
2014-05-03 23:49:58 +02:00
*
2014-05-24 19:16:48 +02:00
* @ param RecordCallback $callback
2014-05-03 21:57:38 +02:00
*/
2014-05-24 19:16:48 +02:00
public function handleLapFinishCallback ( RecordCallback $callback ) {
$this -> handleFinishCallback ( $callback );
2014-05-03 21:57:38 +02:00
}
/**
2014-05-24 19:16:48 +02:00
* Handle Finish Callback
2014-05-03 23:49:58 +02:00
*
2014-05-24 19:16:48 +02:00
* @ param RecordCallback $callback
2014-05-03 21:57:38 +02:00
*/
2014-05-24 19:16:48 +02:00
public function handleFinishCallback ( RecordCallback $callback ) {
if ( $callback -> isLegacyCallback ) {
2014-05-03 21:57:38 +02:00
return ;
}
2014-05-24 19:16:48 +02:00
if ( $callback -> time <= 0 ) {
// Invalid time
2014-05-03 21:57:38 +02:00
return ;
}
2014-05-03 23:49:58 +02:00
2014-05-24 19:16:48 +02:00
$map = $this -> maniaControl -> mapManager -> getCurrentMap ();
$checkpointsString = $this -> getCheckpoints ( $callback -> player -> login );
$this -> checkpoints [ $callback -> login ] = array ();
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
// Check old record of the player
2014-05-24 19:16:48 +02:00
$oldRecord = $this -> getLocalRecord ( $map , $callback -> player );
2014-05-03 21:57:38 +02:00
if ( $oldRecord ) {
2014-05-24 19:16:48 +02:00
if ( $oldRecord -> time < $callback -> time ) {
2014-05-03 21:57:38 +02:00
// Not improved
return ;
}
2014-05-24 19:16:48 +02:00
if ( $oldRecord -> time == $callback -> time ) {
2014-05-03 21:57:38 +02:00
// Same time
2014-06-14 14:37:09 +02:00
// TODO: respect notify-settings
2014-05-24 19:16:48 +02:00
$message = '$<$fff' . $callback -> player -> nickname . '$> equalized his/her $<$ff0' . $oldRecord -> rank . '.$> Local Record: $<$fff' . Formatter :: formatTime ( $oldRecord -> time ) . '$>!' ;
2014-05-03 21:57:38 +02:00
$this -> maniaControl -> chat -> sendInformation ( '$3c0' . $message );
return ;
}
}
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
// Save time
$mysqli = $this -> maniaControl -> database -> mysqli ;
2014-05-03 23:49:58 +02:00
$query = " INSERT INTO ` " . self :: TABLE_RECORDS . " ` (
2014-05-03 21:57:38 +02:00
`mapIndex` ,
`playerIndex` ,
`time` ,
`checkpoints`
) VALUES (
{ $map -> index },
2014-05-24 19:16:48 +02:00
{ $callback -> player -> index },
{ $callback -> time },
'{$checkpointsString}'
2014-05-03 21:57:38 +02:00
) ON DUPLICATE KEY UPDATE
`time` = VALUES ( `time` ),
`checkpoints` = VALUES ( `checkpoints` ); " ;
$mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error );
return ;
}
$this -> updateManialink = true ;
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
// Announce record
2014-06-14 14:32:29 +02:00
$newRecord = $this -> getLocalRecord ( $map , $callback -> player );
$improvedRank = ( ! $oldRecord || $newRecord -> rank < $oldRecord -> rank );
2014-05-03 23:49:58 +02:00
2014-05-13 16:03:26 +02:00
$notifyOnlyDriver = $this -> maniaControl -> settingManager -> getSettingValue ( $this , self :: SETTING_NOTIFY_ONLY_DRIVER );
$notifyOnlyBestRecords = $this -> maniaControl -> settingManager -> getSettingValue ( $this , self :: SETTING_NOTIFY_BEST_RECORDS );
2014-06-14 14:32:29 +02:00
$message = '$3c0' ;
if ( $notifyOnlyDriver ) {
$message .= 'You' ;
2014-05-03 21:57:38 +02:00
} else {
2014-06-14 14:32:29 +02:00
$message .= '$<$fff' . $callback -> player -> nickname . '$>' ;
}
$message .= ' ' . ( $improvedRank ? 'gained' : 'improved' ) . ' the' ;
$message .= ' $<$ff0' . $newRecord -> rank . '.$> Local Record:' ;
$message .= ' $<$fff' . Formatter :: formatTime ( $newRecord -> time ) . '$>!' ;
if ( $oldRecord ) {
$message .= ' (' ;
if ( $improvedRank ) {
$message .= '$<$ff0' . $oldRecord -> rank . '.$> ' ;
2014-05-03 23:49:58 +02:00
}
2014-06-14 14:32:29 +02:00
$timeDiff = $oldRecord -> time - $newRecord -> time ;
$message .= '$<$fff-' . Formatter :: formatTime ( $timeDiff ) . '$>)' ;
}
if ( $notifyOnlyDriver ) {
$this -> maniaControl -> chat -> sendInformation ( $message , $callback -> player );
} else if ( ! $notifyOnlyBestRecords || $newRecord -> rank <= $notifyOnlyBestRecords ) {
$this -> maniaControl -> chat -> sendInformation ( $message );
2014-05-03 21:57:38 +02:00
}
$this -> maniaControl -> callbackManager -> triggerCallback ( self :: CB_LOCALRECORDS_CHANGED , $newRecord );
}
2014-05-24 19:16:48 +02:00
/**
* 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 ;
}
2014-05-03 21:57:38 +02:00
/**
2014-05-03 23:49:58 +02:00
* Retrieve the local record for the given map and login
*
* @ param Map $map
* @ param Player $player
* @ return mixed
2014-05-03 21:57:38 +02:00
*/
2014-05-03 23:49:58 +02:00
private function getLocalRecord ( Map $map , Player $player ) {
$mysqli = $this -> maniaControl -> database -> mysqli ;
$query = " SELECT records.* FROM (
SELECT recs .* , @ rank := @ rank + 1 as `rank` FROM `" . self::TABLE_RECORDS . "` recs , ( SELECT @ rank := 0 ) ra
WHERE recs . `mapIndex` = { $map -> index }
ORDER BY recs . `time` ASC ) records
WHERE records . `playerIndex` = { $player -> index }; " ;
$result = $mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( " Couldn't retrieve player record for ' { $player -> login } '. " . $mysqli -> error );
return null ;
2014-05-03 21:57:38 +02:00
}
2014-05-03 23:49:58 +02:00
$record = $result -> fetch_object ();
$result -> free ();
return $record ;
2014-05-03 21:57:38 +02:00
}
/**
2014-05-24 19:16:48 +02:00
* Handle PlayerConnect callback
2014-05-03 23:49:58 +02:00
*
2014-05-24 19:16:48 +02:00
* @ param Player $player
2014-05-03 21:57:38 +02:00
*/
2014-05-24 19:16:48 +02:00
public function handlePlayerConnect ( Player $player ) {
$this -> updateManialink = true ;
}
/**
* Handle BeginMap callback
*
* @ param Map $map
*/
public function handleMapBegin ( Map $map ) {
$this -> updateManialink = true ;
2014-05-03 23:49:58 +02:00
}
/**
* Handle PlayerManialinkPageAnswer callback
*
* @ param array $callback
*/
public function handleManialinkPageAnswer ( array $callback ) {
$actionId = $callback [ 1 ][ 2 ];
$login = $callback [ 1 ][ 1 ];
$player = $this -> maniaControl -> playerManager -> getPlayer ( $login );
2014-05-24 19:16:48 +02:00
if ( $actionId === self :: ACTION_SHOW_RECORDSLIST ) {
2014-05-03 23:49:58 +02:00
$this -> showRecordsList ( array (), $player );
2014-05-03 21:57:38 +02:00
}
}
/**
* Shows a ManiaLink list with the local records .
2014-05-03 23:49:58 +02:00
*
* @ param array $chat
2014-05-03 21:57:38 +02:00
* @ param Player $player
*/
public function showRecordsList ( array $chat , Player $player ) {
2014-05-03 23:49:58 +02:00
$width = $this -> maniaControl -> manialinkManager -> styleManager -> getListWidgetsWidth ();
2014-05-03 21:57:38 +02:00
$height = $this -> maniaControl -> manialinkManager -> styleManager -> getListWidgetsHeight ();
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
// get PlayerList
$records = $this -> getLocalRecords ( $this -> maniaControl -> mapManager -> getCurrentMap ());
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
// create manialink
$maniaLink = new ManiaLink ( ManialinkManager :: MAIN_MLID );
2014-05-03 23:49:58 +02:00
$script = $maniaLink -> getScript ();
$paging = new Paging ();
2014-05-03 21:57:38 +02:00
$script -> addFeature ( $paging );
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
// Main frame
$frame = $this -> maniaControl -> manialinkManager -> styleManager -> getDefaultListFrame ( $script , $paging );
$maniaLink -> add ( $frame );
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
// Start offsets
$x = - $width / 2 ;
$y = $height / 2 ;
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
// Predefine Description Label
$descriptionLabel = $this -> maniaControl -> manialinkManager -> styleManager -> getDefaultDescriptionLabel ();
$frame -> add ( $descriptionLabel );
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
// Headline
$headFrame = new Frame ();
$frame -> add ( $headFrame );
$headFrame -> setY ( $y - 5 );
$array = array ( " Rank " => $x + 5 , " Nickname " => $x + 18 , " Login " => $x + 70 , " Time " => $x + 101 );
$this -> maniaControl -> manialinkManager -> labelLine ( $headFrame , $array );
2014-05-03 23:49:58 +02:00
2014-05-15 17:45:08 +02:00
$i = 0 ;
$y = $height / 2 - 10 ;
$pageFrame = null ;
2014-05-03 21:57:38 +02:00
foreach ( $records as $listRecord ) {
2014-05-15 17:45:08 +02:00
if ( $i % 15 === 0 ) {
2014-05-03 21:57:38 +02:00
$pageFrame = new Frame ();
$frame -> add ( $pageFrame );
$y = $height / 2 - 10 ;
$paging -> addPage ( $pageFrame );
}
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
$recordFrame = new Frame ();
$pageFrame -> add ( $recordFrame );
2014-05-03 23:49:58 +02:00
2014-06-14 14:32:29 +02:00
if ( $i % 2 !== 0 ) {
2014-05-03 21:57:38 +02:00
$lineQuad = new Quad_BgsPlayerCard ();
$recordFrame -> add ( $lineQuad );
$lineQuad -> setSize ( $width , 4 );
$lineQuad -> setSubStyle ( $lineQuad :: SUBSTYLE_BgPlayerCardBig );
$lineQuad -> setZ ( 0.001 );
}
2014-05-03 23:49:58 +02:00
if ( strlen ( $listRecord -> nickname ) < 2 ) {
$listRecord -> nickname = $listRecord -> login ;
}
$array = array ( $listRecord -> rank => $x + 5 , '$fff' . $listRecord -> nickname => $x + 18 , $listRecord -> login => $x + 70 , Formatter :: formatTime ( $listRecord -> time ) => $x + 101 );
2014-05-03 21:57:38 +02:00
$this -> maniaControl -> manialinkManager -> labelLine ( $recordFrame , $array );
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
$recordFrame -> setY ( $y );
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
$y -= 4 ;
$i ++ ;
}
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
// Render and display xml
$this -> maniaControl -> manialinkManager -> displayWidget ( $maniaLink , $player , 'PlayerList' );
}
/**
2014-05-03 23:49:58 +02:00
* Delete a Player ' s record
*
* @ param array $chat
* @ param Player $player
2014-05-03 21:57:38 +02:00
*/
2014-05-03 23:49:58 +02:00
public function deleteRecord ( array $chat , Player $player ) {
if ( ! $this -> maniaControl -> authenticationManager -> checkRight ( $player , AuthenticationManager :: AUTH_LEVEL_MASTERADMIN )) {
$this -> maniaControl -> authenticationManager -> sendNotAllowed ( $player );
return ;
2014-05-03 21:57:38 +02:00
}
2014-05-03 23:49:58 +02:00
$chatCommand = explode ( ' ' , $chat [ 1 ][ 2 ]);
$recordId = ( int ) $chatCommand [ 1 ];
2014-05-24 19:16:48 +02:00
if ( $recordId <= 0 ) {
$this -> maniaControl -> chat -> sendError ( 'No Record ID given!' , $player );
return ;
}
2014-05-03 21:57:38 +02:00
2014-05-24 19:16:48 +02:00
$currentMap = $this -> maniaControl -> mapManager -> getCurrentMap ();
$records = $this -> getLocalRecords ( $currentMap );
if ( count ( $records ) < $recordId ) {
$this -> maniaControl -> chat -> sendError ( 'Cannot remove record $<$fff' . $recordId . '$>!' , $player );
return ;
}
2014-05-03 21:57:38 +02:00
2014-05-24 19:16:48 +02:00
$mysqli = $this -> maniaControl -> database -> mysqli ;
2014-06-14 14:32:29 +02:00
$query = " DELETE FROM ` " . self :: TABLE_RECORDS . " ` WHERE `mapIndex` = { $currentMap -> index } AND `playerIndex` = { $player -> index } ; " ;
2014-05-24 19:16:48 +02:00
$mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error );
return ;
2014-05-03 21:57:38 +02:00
}
2014-05-24 19:16:48 +02:00
$this -> maniaControl -> callbackManager -> triggerCallback ( self :: CB_LOCALRECORDS_CHANGED , null );
$this -> maniaControl -> chat -> sendInformation ( 'Record no. $<$fff' . $recordId . '$> has been removed!' );
2014-05-03 21:57:38 +02:00
}
}