2014-05-03 21:57:38 +02:00
< ? php
namespace MCTeam ;
use FML\Controls\Frame ;
use FML\Controls\Label ;
use FML\Controls\Quad ;
use FML\Controls\Quads\Quad_BgsPlayerCard ;
2017-05-19 01:16:57 +02:00
use FML\Controls\Quads\Quad_Icons64x64_1 ;
2014-05-03 21:57:38 +02:00
use FML\ManiaLink ;
use FML\Script\Features\Paging ;
use ManiaControl\Admin\AuthenticationManager ;
use ManiaControl\Callbacks\CallbackListener ;
use ManiaControl\Callbacks\Callbacks ;
2017-05-08 19:55:07 +02:00
use ManiaControl\Callbacks\Structures\TrackMania\OnWayPointEventStructure ;
2014-05-03 21:57:38 +02:00
use ManiaControl\Callbacks\TimerListener ;
use ManiaControl\Commands\CommandListener ;
2014-08-03 13:29:54 +02:00
use ManiaControl\Logger ;
2014-05-03 21:57:38 +02:00
use ManiaControl\ManiaControl ;
2017-05-08 20:07:15 +02:00
use ManiaControl\Manialinks\LabelLine ;
2014-05-03 21:57:38 +02:00
use ManiaControl\Manialinks\ManialinkManager ;
2017-05-19 13:33:02 +02:00
use ManiaControl\Manialinks\ManialinkPageAnswerListener ;
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 ;
2017-05-21 23:11:52 +02:00
use MCTeam\Common\RecordWidget ;
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 >
2018-03-27 20:11:40 +02:00
* @ copyright 2014 - 2018 ManiaControl Team
2014-05-03 23:49:58 +02:00
* @ license http :// www . gnu . org / licenses / GNU General Public License , Version 3
2014-05-03 21:57:38 +02:00
*/
2017-05-19 14:44:34 +02:00
class LocalRecordsPlugin implements ManialinkPageAnswerListener , CallbackListener , CommandListener , TimerListener , Plugin {
2014-05-03 21:57:38 +02:00
/*
* Constants
*/
2017-05-19 01:16:57 +02:00
const ID = 7 ;
2017-07-18 21:31:48 +02:00
const VERSION = 0.5 ;
2017-05-19 01:16:57 +02:00
const NAME = 'Local Records Plugin' ;
const AUTHOR = 'MCTeam' ;
const MLID_RECORDS = 'ml_local_records' ;
const TABLE_RECORDS = 'mc_localrecords' ;
2017-07-18 21:31:48 +02:00
const SETTING_MULTILAP_SAVE_SINGLE = 'Save every Lap as Record in Multilap' ;
2017-05-19 01:16:57 +02: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_WIDGET_ENABLE = 'Enable Local Records Widget' ;
const SETTING_NOTIFY_ONLY_DRIVER = 'Notify only the Driver on New Records' ;
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' ;
const SETTING_RECORDS_BEFORE_AFTER = 'Number of Records displayed before and after a player' ;
const CB_LOCALRECORDS_CHANGED = 'LocalRecords.Changed' ;
const ACTION_SHOW_RECORDSLIST = 'LocalRecords.ShowRecordsList' ;
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
/*
2014-08-02 22:31:46 +02:00
* Private properties
2014-05-03 21:57:38 +02:00
*/
2014-05-03 23:49:58 +02:00
/** @var ManiaControl $maniaControl */
2017-05-21 23:11:52 +02:00
private $maniaControl = null ;
/** @var \MCTeam\Common\RecordWidget $recordWidget */
private $recordWidget = null ;
2014-05-03 21:57:38 +02:00
private $updateManialink = false ;
2017-05-08 19:55:07 +02:00
private $checkpoints = array ();
2017-07-18 21:31:48 +02:00
private $scriptName = 'TimeAttack' ;
2014-05-03 21:57:38 +02:00
/**
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 ()
2015-01-19 11:47:06 +01:00
* @ param \ManiaControl\ManiaControl $maniaControl
* @ return bool
2014-05-03 21:57:38 +02:00
*/
public function load ( ManiaControl $maniaControl ) {
$this -> maniaControl = $maniaControl ;
$this -> initTables ();
2014-05-03 23:49:58 +02:00
2017-05-21 23:11:52 +02:00
$this -> recordWidget = new RecordWidget ( $this -> maniaControl );
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_TITLE , 'Local Records' );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_POSX , - 139. );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_POSY , 75 );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_WIDTH , 40. );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_LINESCOUNT , 15 );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_LINEHEIGHT , 4. );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_ENABLE , true );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_NOTIFY_ONLY_DRIVER , false );
2017-05-08 20:36:29 +02:00
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_NOTIFY_BEST_RECORDS , 10 );
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_ADJUST_OUTER_BORDER , false );
2017-05-19 01:16:57 +02:00
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_RECORDS_BEFORE_AFTER , 2 );
2017-07-18 21:31:48 +02:00
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_MULTILAP_SAVE_SINGLE , false );
2014-08-03 01:34:18 +02:00
// Callbacks
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getTimerManager () -> registerTimerListening ( $this , 'handle1Second' , 1000 );
2017-07-18 21:31:48 +02:00
$this -> maniaControl -> getTimerManager () -> registerTimerListening ( $this , 'handle1Minute' , 60000 );
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( Callbacks :: AFTERINIT , $this , 'handleAfterInit' );
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( Callbacks :: BEGINMAP , $this , 'handleMapBegin' );
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( SettingManager :: CB_SETTING_CHANGED , $this , 'handleSettingChanged' );
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( PlayerManager :: CB_PLAYERCONNECT , $this , 'handlePlayerConnect' );
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' );
2017-07-18 21:31:48 +02:00
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( Callbacks :: TM_ONLAPFINISH , $this , 'handleFinishLapCallback' );
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getCommandManager () -> registerCommandListener ( array ( 'recs' , 'records' ), $this , 'showRecordsList' , false , 'Shows a list of Local Records on the current map.' );
$this -> maniaControl -> getCommandManager () -> registerCommandListener ( 'delrec' , $this , 'deleteRecord' , true , 'Removes a record from the database.' );
2014-05-03 23:49:58 +02:00
2017-05-19 14:44:34 +02:00
$this -> maniaControl -> getManialinkManager () -> registerManialinkPageAnswerListener ( self :: ACTION_SHOW_RECORDSLIST , $this , 'handleShowRecordsList' );
2017-05-19 13:33:02 +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 () {
2014-08-13 11:14:29 +02:00
$mysqli = $this -> maniaControl -> getDatabase () -> getMysqli ();
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 );
}
2018-07-03 18:15:37 +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 21:57:38 +02:00
}
/**
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-08-13 11:14:29 +02:00
$this -> maniaControl -> getManialinkManager () -> hideManialink ( self :: MLID_RECORDS );
2014-05-03 21:57:38 +02:00
}
/**
* Handle ManiaControl After Init
2017-07-18 21:31:48 +02:00
*
2017-06-21 18:07:33 +02:00
* @ internal
2014-05-03 21:57:38 +02:00
*/
public function handleAfterInit () {
$this -> updateManialink = true ;
}
/**
2014-05-13 16:40:05 +02:00
* Handle 1 Second Callback
2017-07-18 21:31:48 +02:00
*
2017-06-21 18:07:33 +02:00
* @ internal
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 ;
2017-05-08 19:55:07 +02:00
if ( $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_ENABLE )) {
2017-05-19 01:16:57 +02:00
$this -> sendWidgetManiaLink ();
2014-05-03 21:57:38 +02:00
}
}
2017-07-18 21:31:48 +02:00
/** Fetch the Current Scriptname every Minute
*
* @ internal
*/
public function handle1Minute () {
$scriptNameResponse = $this -> maniaControl -> getClient () -> getScriptName ();
$this -> scriptName = str_replace ( '.Script.txt' , '' , $scriptNameResponse [ 'CurrentValue' ]);
}
2014-05-03 23:49:58 +02:00
/**
2017-05-19 01:16:57 +02:00
* Build the local records widget ManiaLink and send it to the players
2014-05-03 23:49:58 +02:00
*
* @ return string
*/
2017-05-19 01:16:57 +02:00
private function sendWidgetManiaLink () {
2014-08-13 11:14:29 +02:00
$map = $this -> maniaControl -> getMapManager () -> getCurrentMap ();
2014-05-03 23:49:58 +02:00
if ( ! $map ) {
return null ;
}
2017-05-19 01:16:57 +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_LINESCOUNT );
$lineHeight = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_LINEHEIGHT );
$recordsBeforeAfter = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_RECORDS_BEFORE_AFTER );
$labelStyle = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getDefaultLabelStyle ();
$quadStyle = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getDefaultQuadStyle ();
$quadSubstyle = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getDefaultQuadSubstyle ();
2017-05-20 10:43:14 +02:00
$records = $this -> getLocalRecords ( $map , 1000 ); //TODO limit setting
2014-05-03 23:49:58 +02:00
if ( ! is_array ( $records )) {
2014-08-03 13:29:54 +02:00
Logger :: logError ( " Couldn't fetch player records. " );
2014-05-03 23:49:58 +02:00
return null ;
}
2017-05-20 10:43:14 +02:00
//TODO maybe only store if player is connected
2017-05-19 01:16:57 +02:00
$playerRecords = array ();
foreach ( $records as $index => $record ) {
$playerRecords [ $record -> playerIndex ] = $index ;
}
2017-05-19 14:44:34 +02:00
$frame = new Frame ();
2014-06-14 15:48:27 +02:00
$frame -> setPosition ( $posX , $posY );
2014-05-03 23:49:58 +02:00
$backgroundQuad = new Quad ();
2017-03-25 19:15:50 +01:00
$frame -> addChild ( $backgroundQuad );
$backgroundQuad -> setVerticalAlign ( $backgroundQuad :: TOP );
2014-08-13 11:14:29 +02:00
$adjustOuterBorder = $this -> maniaControl -> getSettingManager () -> 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 ();
2017-03-25 19:15:50 +01:00
$frame -> addChild ( $titleLabel );
2014-05-03 23:49:58 +02:00
$titleLabel -> setPosition ( 0 , $lineHeight * - 0.9 );
$titleLabel -> setWidth ( $width );
$titleLabel -> setStyle ( $labelStyle );
$titleLabel -> setTextSize ( 2 );
$titleLabel -> setText ( $title );
$titleLabel -> setTranslate ( true );
2017-05-21 23:11:52 +02:00
$topRecordsCount = $lines - $recordsBeforeAfter * 2 - 1 ;
$preGeneratedTopRecordsFrame = $this -> recordWidget -> generateRecordsFrame ( $records , $topRecordsCount );
$preGeneratedRecordsFrameWithRecord = $this -> recordWidget -> generateRecordsFrame ( $records , $lines - 1 );
2017-05-19 01:16:57 +02:00
2017-05-21 23:11:52 +02:00
$players = $this -> maniaControl -> getPlayerManager () -> getPlayers ();
$playersWithoutRecord = array ();
2017-05-19 01:16:57 +02:00
foreach ( $players as $player ) {
2017-05-21 23:11:52 +02:00
$sendManiaLink = true ;
2017-05-19 14:44:34 +02:00
$maniaLink = new ManiaLink ( self :: MLID_RECORDS );
$maniaLink -> addChild ( $frame );
$listFrame = new Frame ();
$maniaLink -> addChild ( $listFrame );
$listFrame -> setPosition ( $posX , $posY );
2017-05-19 01:16:57 +02:00
if ( isset ( $playerRecords [ $player -> index ]) && $playerRecords [ $player -> index ] >= $topRecordsCount ) {
2017-05-21 23:11:52 +02:00
$listFrame -> addChild ( $preGeneratedTopRecordsFrame );
2017-05-19 01:16:57 +02:00
2017-05-19 13:33:02 +02:00
$y = - 8 - $topRecordsCount * $lineHeight ;
2017-05-19 01:16:57 +02:00
$playerIndex = $playerRecords [ $player -> index ];
//Line separator
2017-05-21 23:11:52 +02:00
$quad = $this -> recordWidget -> getLineSeparatorQuad ( $width );
2017-05-19 14:44:34 +02:00
$listFrame -> addChild ( $quad );
2017-05-19 01:16:57 +02:00
$quad -> setY ( $y + $lineHeight / 2 );
//Generate the Records around a player and display below topRecords
for ( $i = $playerIndex - $recordsBeforeAfter ; $i <= $playerIndex + $recordsBeforeAfter ; $i ++ ) {
2017-05-21 23:11:52 +02:00
if ( array_key_exists ( $i , $records )) { //If there are no records behind you
$recordFrame = $this -> recordWidget -> generateRecordLineFrame ( $records [ $i ], $player );
2017-05-20 10:25:50 +02:00
$recordFrame -> setY ( $y );
$listFrame -> addChild ( $recordFrame );
$y -= $lineHeight ;
}
2017-05-19 01:16:57 +02:00
}
2017-05-21 23:11:52 +02:00
} else if ( isset ( $playerRecords [ $player -> index ]) && $playerRecords [ $player -> index ] < $topRecordsCount ) {
$playersWithoutRecord [] = $player ;
$sendManiaLink = false ;
2017-05-19 01:16:57 +02:00
} else {
2017-05-21 23:11:52 +02:00
if ( $record = $this -> getLocalRecord ( $map , $player )) {
$record -> nickname = $player -> nickname ;
$recordFrame = $preGeneratedRecordsFrameWithRecord ;
$listFrame -> addChild ( $recordFrame );
2017-05-19 01:16:57 +02:00
2017-05-21 23:11:52 +02:00
$y = - 8 - ( $lines - 1 ) * $lineHeight ;
2017-05-19 01:16:57 +02:00
2017-05-21 23:11:52 +02:00
//Line separator
$quad = $this -> recordWidget -> getLineSeparatorQuad ( $width );
$listFrame -> addChild ( $quad );
$quad -> setY ( $y + $lineHeight / 2 );
2017-05-19 01:16:57 +02:00
2017-05-22 17:20:16 +02:00
$recordFrame = $this -> recordWidget -> generateRecordLineFrame ( $record , $player );
$listFrame -> addChild ( $recordFrame );
$recordFrame -> setY ( $y );
2017-05-19 01:16:57 +02:00
2017-05-21 23:11:52 +02:00
} else {
$playersWithoutRecord [] = $player ;
$sendManiaLink = false ;
}
}
2017-05-19 01:16:57 +02:00
2017-05-21 23:11:52 +02:00
if ( $sendManiaLink ) {
$this -> maniaControl -> getManialinkManager () -> sendManialink ( $maniaLink , $player );
2014-05-03 23:49:58 +02:00
}
2017-05-21 23:11:52 +02:00
}
2014-05-03 23:49:58 +02:00
2017-05-21 23:11:52 +02:00
if ( $playersWithoutRecord ) {
$maniaLink = new ManiaLink ( self :: MLID_RECORDS );
$maniaLink -> addChild ( $frame );
2014-05-03 23:49:58 +02:00
2017-05-21 23:11:52 +02:00
$listFrame = $this -> recordWidget -> generateRecordsFrame ( $records , $lines );
$maniaLink -> addChild ( $listFrame );
$listFrame -> setPosition ( $posX , $posY );
2014-05-03 23:49:58 +02:00
2017-05-21 23:11:52 +02:00
$this -> maniaControl -> getManialinkManager () -> sendManialink ( $maniaLink , $playersWithoutRecord );
2014-05-03 23:49:58 +02:00
}
}
/**
* Fetch local records for the given map
*
2017-06-21 18:07:33 +02:00
* @ api
2017-05-19 01:16:57 +02:00
* @ param \ManiaControl\Maps\Map $map
* @ param int $limit
* @ return array | null
2014-05-03 23:49:58 +02:00
*/
public function getLocalRecords ( Map $map , $limit = - 1 ) {
2014-08-13 11:14:29 +02:00
$mysqli = $this -> maniaControl -> getDatabase () -> getMysqli ();
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
*
2017-05-19 13:33:02 +02:00
* @ internal
2014-05-13 14:15:00 +02:00
* @ 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 ) {
2017-05-08 19:55:07 +02:00
case self :: SETTING_WIDGET_ENABLE : {
2014-05-13 14:15:00 +02:00
if ( $setting -> value ) {
$this -> updateManialink = true ;
} else {
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getManialinkManager () -> hideManialink ( self :: MLID_RECORDS );
2014-05-13 14:15:00 +02:00
}
break ;
}
2017-05-07 20:22:59 +02:00
default :
2017-05-21 23:11:52 +02:00
$this -> updateRecordWidget ();
2017-05-07 20:22:59 +02:00
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
*
2017-05-19 13:33:02 +02:00
* @ internal
2017-07-18 21:31:48 +02:00
* @ param \ManiaControl\Callbacks\Structures\TrackMania\OnWayPointEventStructure $structure
2014-05-03 21:57:38 +02:00
*/
2017-05-08 19:55:07 +02:00
public function handleCheckpointCallback ( OnWayPointEventStructure $structure ) {
2017-05-11 23:34:51 +02:00
$playerLogin = $structure -> getLogin ();
2017-05-08 19:55:07 +02:00
if ( ! isset ( $this -> checkpoints [ $playerLogin ])) {
$this -> checkpoints [ $playerLogin ] = array ();
2014-05-24 19:16:48 +02:00
}
2017-05-08 19:55:07 +02:00
$this -> checkpoints [ $playerLogin ][ $structure -> getCheckPointInLap ()] = $structure -> getLapTime ();
2014-05-03 21:57:38 +02:00
}
2017-07-18 21:31:48 +02:00
/**
* Handle End of Lap Callback
*
* @ internal
* @ param \ManiaControl\Callbacks\Structures\TrackMania\OnWayPointEventStructure $structure
*/
public function handleFinishLapCallback ( OnWayPointEventStructure $structure ) {
$multiLapSaveSingle = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_MULTILAP_SAVE_SINGLE );
if ( $this -> scriptName != " TimeAttack " && ! $multiLapSaveSingle ) {
//Do Nothing on Finishing a Single Lap
} else {
//Save on every pass through of a Lap
$this -> saveRecord ( $structure , $structure -> getLapTime ());
}
}
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
*
2017-05-19 13:33:02 +02:00
* @ internal
2017-05-11 23:34:51 +02:00
* @ param \ManiaControl\Callbacks\Structures\TrackMania\OnWayPointEventStructure $structure
2014-05-03 21:57:38 +02:00
*/
2017-05-08 19:55:07 +02:00
public function handleFinishCallback ( OnWayPointEventStructure $structure ) {
2018-07-03 18:13:00 +02:00
$multiLapSaveSingle = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_MULTILAP_SAVE_SINGLE );
if ( $this -> scriptName != " TimeAttack " && $multiLapSaveSingle ) {
//Save last lap time only
$this -> saveRecord ( $structure , $structure -> getLapTime ());
} else {
//Save full race time
$this -> saveRecord ( $structure , $structure -> getRaceTime ());
}
2017-07-18 21:31:48 +02:00
}
private function saveRecord ( OnWayPointEventStructure $structure , $time ) {
if ( $time <= 0 ) {
2014-05-24 19:16:48 +02:00
// Invalid time
2014-05-03 21:57:38 +02:00
return ;
}
2014-05-03 23:49:58 +02:00
2014-08-13 11:14:29 +02:00
$map = $this -> maniaControl -> getMapManager () -> getCurrentMap ();
2014-05-24 19:16:48 +02:00
2017-05-08 19:55:07 +02:00
$player = $structure -> getPlayer ();
2017-05-19 01:16:57 +02:00
if ( ! $player ) { //TODO verify why this can happen
2017-05-13 23:42:20 +02:00
return ;
}
2017-05-19 13:34:22 +02:00
$checkpointsString = $this -> getCheckpoints ( $player -> login );
2017-05-08 19:55:07 +02:00
$this -> checkpoints [ $player -> login ] = array ();
2014-05-03 23:49:58 +02:00
2017-06-03 15:13:36 +02:00
$notifyOnlyDriver = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_NOTIFY_ONLY_DRIVER );
$notifyOnlyBestRecords = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_NOTIFY_BEST_RECORDS );
2014-05-03 21:57:38 +02:00
// Check old record of the player
2017-05-08 19:55:07 +02:00
$oldRecord = $this -> getLocalRecord ( $map , $player );
2014-05-03 21:57:38 +02:00
if ( $oldRecord ) {
2017-07-18 21:31:48 +02:00
if ( $oldRecord -> time < $time ) {
2014-05-03 21:57:38 +02:00
// Not improved
return ;
}
2017-07-18 21:31:48 +02:00
if ( $oldRecord -> time == $time ) {
2014-05-03 21:57:38 +02:00
// Same time
2017-06-03 15:13:36 +02:00
$message = '$3c0' ;
if ( $notifyOnlyDriver ) {
$message .= 'You' ;
} else {
$message .= '$<$fff' . $player -> nickname . '$>' ;
}
$message .= ' equalized the $<$ff0' . $oldRecord -> rank . '.$> Local Record:' ;
$message .= ' $<$fff' . Formatter :: formatTime ( $oldRecord -> time ) . '$>!' ;
if ( $notifyOnlyDriver ) {
$this -> maniaControl -> getChat () -> sendInformation ( $message , $player );
} else if ( ! $notifyOnlyBestRecords || $oldRecord -> rank <= $notifyOnlyBestRecords ) {
$this -> maniaControl -> getChat () -> sendInformation ( $message );
}
2014-05-03 21:57:38 +02:00
return ;
}
}
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
// Save time
2014-08-13 11:14:29 +02:00
$mysqli = $this -> maniaControl -> getDatabase () -> getMysqli ();
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 },
2017-05-08 19:55:07 +02:00
{ $player -> index },
2017-07-18 21:31:48 +02:00
{ $time },
2014-05-24 19:16:48 +02:00
'{$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
2017-05-08 19:55:07 +02:00
$newRecord = $this -> getLocalRecord ( $map , $player );
2014-06-14 14:32:29 +02:00
$improvedRank = ( ! $oldRecord || $newRecord -> rank < $oldRecord -> rank );
2014-05-03 23:49:58 +02:00
2017-06-03 15:13:36 +02:00
2014-06-14 14:32:29 +02:00
$message = '$3c0' ;
if ( $notifyOnlyDriver ) {
$message .= 'You' ;
2014-05-03 21:57:38 +02:00
} else {
2017-05-08 19:55:07 +02:00
$message .= '$<$fff' . $player -> nickname . '$>' ;
2014-06-14 14:32:29 +02:00
}
$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 ;
2017-05-08 19:55:07 +02:00
$message .= '$<$fff-' . Formatter :: formatTime ( $timeDiff ) . '$>)' ;
2014-06-14 14:32:29 +02:00
}
if ( $notifyOnlyDriver ) {
2017-05-08 19:55:07 +02:00
$this -> maniaControl -> getChat () -> sendInformation ( $message , $player );
2014-06-14 14:32:29 +02:00
} else if ( ! $notifyOnlyBestRecords || $newRecord -> rank <= $notifyOnlyBestRecords ) {
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getChat () -> sendInformation ( $message );
2014-05-03 21:57:38 +02:00
}
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getCallbackManager () -> triggerCallback ( self :: CB_LOCALRECORDS_CHANGED , $newRecord );
2014-05-03 21:57:38 +02:00
}
2017-07-18 21:31:48 +02:00
2014-05-24 19:16:48 +02:00
/**
2017-06-21 18:07:33 +02:00
* Get current checkpoint string for local record
2014-05-24 19:16:48 +02:00
*
2017-06-21 18:07:33 +02:00
* @ api
2014-05-24 19:16:48 +02:00
* @ param string $login
* @ return string
*/
2017-06-21 18:07:33 +02:00
public function getCheckpoints ( $login ) {
2014-05-24 19:16:48 +02:00
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
*
2017-06-21 18:07:33 +02:00
* @ api
2014-05-03 23:49:58 +02:00
* @ param Map $map
* @ param Player $player
* @ return mixed
2014-05-03 21:57:38 +02:00
*/
2017-06-21 18:07:33 +02:00
public function getLocalRecord ( Map $map , Player $player ) {
2014-08-13 11:14:29 +02:00
$mysqli = $this -> maniaControl -> getDatabase () -> getMysqli ();
2014-05-03 23:49:58 +02:00
$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-06-14 15:48:27 +02:00
* Handle Player Connect Callback
2017-05-19 14:44:34 +02:00
*
2017-05-19 13:33:02 +02:00
* @ internal
2014-05-03 21:57:38 +02:00
*/
2014-06-14 15:48:27 +02:00
public function handlePlayerConnect () {
2014-05-24 19:16:48 +02:00
$this -> updateManialink = true ;
}
/**
2014-06-14 15:48:27 +02:00
* Handle Begin Map Callback
2017-05-19 14:44:34 +02:00
*
2017-05-19 13:33:02 +02:00
* @ internal
2014-05-24 19:16:48 +02:00
*/
2014-06-14 15:48:27 +02:00
public function handleMapBegin () {
2014-05-24 19:16:48 +02:00
$this -> updateManialink = true ;
2014-05-03 23:49:58 +02:00
}
2017-05-19 13:33:02 +02:00
2014-05-03 23:49:58 +02:00
/**
2017-05-19 13:33:02 +02:00
* Handle the ManiaLink answer of the showRecordsList action
2014-05-03 23:49:58 +02:00
*
2017-05-19 13:33:02 +02:00
* @ internal
* @ param array $callback
* @ param \ManiaControl\Players\Player $player
2014-05-03 23:49:58 +02:00
*/
2017-05-19 14:44:34 +02:00
public function handleShowRecordsList ( array $callback , Player $player ) {
2017-05-19 13:33:02 +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
*
2017-06-21 18:07:33 +02:00
* @ api
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-08-13 11:14:29 +02:00
$width = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getListWidgetsWidth ();
$height = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getListWidgetsHeight ();
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
// get PlayerList
2017-05-19 01:16:57 +02:00
$records = $this -> getLocalRecords ( $this -> maniaControl -> getMapManager () -> getCurrentMap (), 200 );
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
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-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
// Start offsets
2014-06-14 15:48:27 +02:00
$posX = - $width / 2 ;
$posY = $height / 2 ;
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +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-03 23:49:58 +02:00
2014-05-03 21:57:38 +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 );
2017-05-08 20:07:15 +02:00
$labelLine = new LabelLine ( $headFrame );
2017-05-19 01:16:57 +02:00
$labelLine -> addLabelEntryText ( 'Rank' , $posX + 5 );
$labelLine -> addLabelEntryText ( 'Nickname' , $posX + 18 );
$labelLine -> addLabelEntryText ( 'Login' , $posX + 70 );
2017-05-08 20:07:15 +02:00
$labelLine -> addLabelEntryText ( 'Time' , $posX + 101 );
$labelLine -> render ();
2014-05-03 23:49:58 +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-03 21:57:38 +02:00
foreach ( $records as $listRecord ) {
2014-06-14 15:48:27 +02:00
if ( $index % 15 === 0 ) {
2014-05-03 21:57:38 +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-07 20:22:59 +02:00
$paging -> addPageControl ( $pageFrame );
2014-05-03 21:57:38 +02:00
}
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
$recordFrame = new Frame ();
2017-03-25 19:15:50 +01:00
$pageFrame -> addChild ( $recordFrame );
2014-05-03 23:49:58 +02:00
2017-05-08 20:07:15 +02:00
if ( $index % 2 === 0 ) {
2014-05-03 21:57:38 +02:00
$lineQuad = new Quad_BgsPlayerCard ();
2017-03-25 19:15:50 +01:00
$recordFrame -> addChild ( $lineQuad );
2014-05-03 21:57:38 +02:00
$lineQuad -> setSize ( $width , 4 );
$lineQuad -> setSubStyle ( $lineQuad :: SUBSTYLE_BgPlayerCardBig );
2017-05-08 20:07:15 +02:00
$lineQuad -> setZ ( - 0.001 );
2014-05-03 21:57:38 +02:00
}
2014-05-03 23:49:58 +02:00
2017-05-19 01:16:57 +02:00
if ( $listRecord -> login === $player -> login ) {
$currentQuad = new Quad_Icons64x64_1 ();
$recordFrame -> addChild ( $currentQuad );
$currentQuad -> setX ( $posX + 3.5 );
$currentQuad -> setSize ( 4 , 4 );
$currentQuad -> setSubStyle ( $currentQuad :: SUBSTYLE_ArrowBlue );
}
2014-05-03 23:49:58 +02:00
if ( strlen ( $listRecord -> nickname ) < 2 ) {
$listRecord -> nickname = $listRecord -> login ;
}
2017-05-08 20:07:15 +02:00
$labelLine = new LabelLine ( $recordFrame );
2017-05-19 01:16:57 +02:00
$labelLine -> addLabelEntryText ( $listRecord -> rank , $posX + 5 , 13 );
$labelLine -> addLabelEntryText ( '$fff' . $listRecord -> nickname , $posX + 18 , 52 );
$labelLine -> addLabelEntryText ( $listRecord -> login , $posX + 70 , 31 );
$labelLine -> addLabelEntryText ( Formatter :: formatTime ( $listRecord -> time ), $posX + 101 , $width / 2 - ( $posX + 110 ));
2017-05-08 20:07:15 +02:00
$labelLine -> render ();
2014-05-03 23:49:58 +02:00
2014-06-14 15:48:27 +02:00
$recordFrame -> setY ( $posY );
2014-05-03 23:49:58 +02:00
2014-06-14 15:48:27 +02:00
$posY -= 4 ;
$index ++ ;
2014-05-03 21:57:38 +02:00
}
2014-05-03 23:49:58 +02:00
2014-05-03 21:57:38 +02:00
// Render and display xml
2017-05-10 21:21:04 +02:00
$this -> maniaControl -> getManialinkManager () -> displayWidget ( $maniaLink , $player , 'LocalRecords' );
2014-05-03 21:57:38 +02:00
}
/**
2014-05-03 23:49:58 +02:00
* Delete a Player ' s record
*
2017-06-21 18:07:33 +02:00
* @ internal
2014-05-03 23:49:58 +02:00
* @ 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 ) {
2017-05-08 19:55:07 +02:00
if ( ! $this -> maniaControl -> getAuthenticationManager () -> checkRight ( $player , AuthenticationManager :: AUTH_LEVEL_MASTERADMIN )) {
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getAuthenticationManager () -> sendNotAllowed ( $player );
2014-05-03 23:49:58 +02:00
return ;
2014-05-03 21:57:38 +02:00
}
2014-06-15 00:21:30 +02:00
$commandParts = explode ( ' ' , $chat [ 1 ][ 2 ]);
if ( count ( $commandParts ) < 2 ) {
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getChat () -> sendUsageInfo ( 'Missing Record ID! (Example: //delrec 3)' , $player );
2014-05-24 19:16:48 +02:00
return ;
}
2014-05-03 21:57:38 +02:00
2017-05-08 19:55:07 +02:00
$recordId = ( int ) $commandParts [ 1 ];
2014-08-13 11:14:29 +02:00
$currentMap = $this -> maniaControl -> getMapManager () -> getCurrentMap ();
2014-05-24 19:16:48 +02:00
$records = $this -> getLocalRecords ( $currentMap );
if ( count ( $records ) < $recordId ) {
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getChat () -> sendError ( 'Cannot remove record $<$fff' . $recordId . '$>!' , $player );
2014-05-24 19:16:48 +02:00
return ;
}
2014-05-03 21:57:38 +02:00
2014-08-13 11:14:29 +02:00
$mysqli = $this -> maniaControl -> getDatabase () -> getMysqli ();
2014-06-15 00:21:30 +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
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getCallbackManager () -> triggerCallback ( self :: CB_LOCALRECORDS_CHANGED , null );
$this -> maniaControl -> getChat () -> sendInformation ( 'Record no. $<$fff' . $recordId . '$> has been removed!' );
2014-05-03 21:57:38 +02:00
}
2017-05-21 23:11:52 +02:00
/**
* Update the RecordWidget variables
*/
private function updateRecordWidget () {
2017-07-18 21:31:48 +02:00
$width = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_WIDTH );
$lineHeight = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_LINEHEIGHT );
2017-05-21 23:11:52 +02:00
$this -> recordWidget -> setWidth ( $width );
$this -> recordWidget -> setLineHeight ( $lineHeight );
$this -> updateManialink = true ;
}
2018-07-03 18:15:37 +02:00
}