[Plugin] ManiaRecords v1.1.2 Beta

Post here every finished plugin

Moderator: NADEO

DerekAU
Posts: 1
Joined: 26 Oct 2011, 01:01

Re: [Plugin] ManiaRecords v1.1.2 Beta

Post by DerekAU »

I'm consistently getting this exception on my server right now (~40 players) ManiaLive SVN r273

Code: Select all

 -> ErrorException with code 0
    Undefined index: timestamp
  - in
      /home/tm2/ManiaLive2/libraries/ManiaLivePlugins/TheM/ManiaRecords/ManiaRe
      cords.php on line 628
  - Stack: #0
      /home/tm2/ManiaLive2/libraries/ManiaLivePlugins/TheM/ManiaRecords/ManiaRe
      cords.php(628):
      ManiaLive\Application\ErrorHandling::createExceptionFromError(8,
      'Undefined index...', '/home/tm2/Mania...', 628, Array)
           #1 [internal function]:
      ManiaLivePlugins\TheM\ManiaRecords\ManiaRecords->cmpint(Array, Array)
           #2
      /home/tm2/ManiaLive2/libraries/ManiaLivePlugins/TheM/ManiaRecords/ManiaRe
      cords.php(621): usort(Array, Array)
           #3
      /home/tm2/ManiaLive2/libraries/ManiaLivePlugins/TheM/ManiaRecords/ManiaRe
      cords.php(392):
      ManiaLivePlugins\TheM\ManiaRecords\ManiaRecords->array_sort_int(Array,
      'record', 'timestamp')
           #4
      /home/tm2/ManiaLive2/libraries/ManiaLivePlugins/TheM/ManiaRecords/ManiaRe
      cords.php(301):
      ManiaLivePlugins\TheM\ManiaRecords\ManiaRecords->wrPanel(Array)
           #5 [internal function]:
      ManiaLivePlugins\TheM\ManiaRecords\ManiaRecords->onPlayerFinish(61,
      'jondam', 35871)
           #6
      /home/tm2/ManiaLive2/libraries/ManiaLive/DedicatedApi/Callback/Event.php(
      28): call_user_func_array(Array, Array)
           #7
      /home/tm2/ManiaLive2/libraries/ManiaLive/Event/Dispatcher.php(50):
      ManiaLive\DedicatedApi\Callback\Event->fireDo(Object(ManiaLivePlugins\The
      M\ManiaRecords\ManiaRecords))
           #8
      /home/tm2/ManiaLive2/libraries/ManiaLive/DedicatedApi/Connection.php(108)
      :
      ManiaLive\Event\Dispatcher::dispatch(Object(ManiaLive\DedicatedApi\Callba
      ck\Event))
           #9
      /home/tm2/ManiaLive2/libraries/ManiaLive/Application/AbstractApplication.
      php(150): ManiaLive\DedicatedApi\Connection->executeCallbacks()
           #10 /home/tm2/ManiaLive2/bootstrapper.php(80):
      ManiaLive\Application\AbstractApplication->run()
           #11 {main}
Also, i'm getting a delay of up to 40 seconds where ManiaLive freezes at the end of a round. I have tried to narrow down the problem plugin as much as possible and I think it is happening in the ManiaRecords widget based on lines like this in my runtime log.

Code: Select all

[21:30:08] [MLEPP] [CHAT] [ ßàm!Stuman] (stuman): gg
*Round Ends Here*
[WR] First WR: 0:48.084 - Last WR: 0:48.271 Difference: 0:00.187 on $i$999Road$i$ff0Runner
[21:30:55] [MLEPP] [Music] Enabling song: 03 Disconnected -
[21:30:56] [MLEPP] [LocalRecords] Current record by pudenry with a time of 0:48.106.
Is it possible that the ManiaRecords plugin is causing this with some internet lookup? I'm assuming it doesn't utilise threading to pass off the task so therefore is hanging the main mlepp process.

Also it would be nice if the widget for ManiaRecords disappeared at first checkpoint like the widgets in MLEPP do, but this is a very low priority issue.

Thanks for the hard work,
Derek
TheM
Posts: 1379
Joined: 15 Jun 2010, 14:30
Location: Uden, Noord-Brabant, Netherlands
Contact:

Re: [Plugin] ManiaRecords v1.1.2 Beta

Post by TheM »

First: we're busy including ManiaRecords and Dedimania² into the next MLEPP release.
DerekAU: it's correct that the ManiaRecords plugin is causing a delay on the end of the round.
This delay can take up to a minute. I haven't been able to find the solution for this problem, and therefore there is no solution released yet.
I might use Threading in the coming release, but also without that it shouldn't take this long.
I'll have a look on the error when I'm at home, later this evening.
Global moderator | Dutch moderator | Laddermoderator | ManiaWiki moderator
Server Manager/webmaster of Smurfen.net, join us on Canyon, Valley and Royal!
ESL (Game) Staff Head for TrackMania (Stadium, Canyon and Valley).
nocturne
Posts: 208
Joined: 23 Jun 2010, 21:31

Re: [Plugin] ManiaRecords v1.1.2 Beta

Post by nocturne »

Well, seeing as you stated you are running ML r273, that'd lead me to believe you're also using the latest dedicated server version -- in which case it should be stated that this plugin isn't compatible, due to Nadeo's incredibly insane idea to change (almost) every instance of 'Challenge' to 'Map'. Though, I don't know if this is causing your exact problem..

A week or two back I made a hacked-up fix, though I've neglected testing it so far..

/libraries/ManiaLivePlugins/TheM/ManiaRecords/ManiaRecords.php

Code: Select all

<?php

/**
 * -- ManiaLive plugin --
 * @name ManiaRecords for ManiaLive
 * @date 05-10-2011
 * @version v1.1.2
 * @website http://tm.maniarecords.org
 * 
 * Thanks to Xymph & Co for the quick Xaseco2
 * Thanks to McRobert for styling the plugin
 * Thanks to uBm for the port to FoxControl
 * Thanks to undef.de for his Records-Eyepiece Implementation
 * Thanks to TheM for the port to ManiaLive
 * Thanks to forza for the TMX-Implementation
 * 
 * @author Bullracing!vni
 * @ported Max "TheM" Klaversma <maxklaversma@gmail.com>
 * @copyright 2011
 * 
 * DO NOT EDIT THIS FILE ! 
 * IF YOU MAKE CHANGES ON THE CODE, YOUR SERVER MAY
 * BE BANNED PERMANENTLY FROM THE MANIARECORDS DATABASE.
 * -----------------------------------------------------------
 * Changes v1.1.2
 * Added /maniarecords command (does the same as /worldrecords)
 * Namechange (WorldRecords --> ManiaRecords)
 * -----------------------------------------------------------
 * Changes v1.1.1
 * Added /worldrecords command (gives recordwindow)
 * Code cleanup
 * -----------------------------------------------------------
 * Changes v1.1.0
 * Fixed bug which prevented the plugin to submit records.
 * Changed to API v4.0.0
 * -----------------------------------------------------------
 * Changes v1.0.9
 * Fixed bug which prevented the WorldRecords to make any connection with the World Database.
 * Cleanup
 * -----------------------------------------------------------
 * Changes v1.0.8
 * Initial ManiaLive port
 * -----------------------------------------------------------
 */

namespace ManiaLivePlugins\TheM\ManiaRecords;

use ManiaLive\Utilities\Console;
use ManiaLib\Utils\TMStrings as String;
use ManiaLive\DedicatedApi\Connection;
use ManiaLive\Data\Storage;
use ManiaLive\Utilities\Time;
use ManiaLivePlugins\TheM\ManiaRecords\Config;

use ManiaLivePlugins\TheM\ManiaRecords\Gui\Windows\listWindow;
use ManiaLivePlugins\TheM\ManiaRecords\Gui\Controls\Header;
use ManiaLivePlugins\TheM\ManiaRecords\Gui\Controls\Normal;

class ManiaRecords extends \ManiaLive\PluginHandler\Plugin {
    public $config;
    
    public $wr;
    public $wrcache;
    public $datacache;
    public $wrlastrec;
    public $wrdbcount;
    
    public $itemholderint;
    public $itemholderint2;
    
    public $serverLogin;
    public $serverName;
    public $gameVersion;
    public $serverHash;
    
    public $version = 1;
    public $version_string = '1.1.2';
    
    public $api_path = '/api_v_4_0_0.php';
    public $api_timeout = 10;
    public $api_version = 1;
    public $api_host = 'api.trackmaniaserver.org';
    
    public $controller = '';
    public $lastaction = '';
    public $detailsopen = true;
    
    function onLoad() {
        $this->wr['controller'] = $this->controller = 'ManiaLive r'.\ManiaLiveApplication\Version;
        $this->config = Config::getInstance();
        
        $this->enableDedicatedEvents();
        $this->enableStorageEvents();
        
        $this->setVersion($this->api_version);
        $this->setPublicMethod('getVersion');
        
        $this->api_host = $this->get_api_host();
        
        $cmd = $this->registerChatCommand('maniarecords', 'maniarecordsChat', 0, true);
        $cmd->help = 'Shows the World Records for this track.';
        
        $cmd = $this->registerChatCommand('worldrecords', 'maniarecordsChat', 0, true);
        $cmd->help = 'Shows the World Records for this track.';
        
        $this->gameVersion = $this->connection->getVersion();
        $this->wr['serverlogin'] = $this->serverLogin = $this->storage->serverLogin;
        $this->wr['servername'] = $this->serverName = String::stripAllTmStyle($this->removespecchars($this->storage->server->name));
        $this->wr['serverhash'] = $this->serverHash = $this->contribute();
        $this->wr['version_string'] = $this->version_string;
        
        $this->wr['playermax'] = $this->connection->getMaxPlayers();
        $this->wr['playermax'] = $this->wr['playermax']['CurrentValue'];
        $this->wr['playercount'] = count($this->storage->players);
        
        if($this->gameVersion->name != 'ManiaPlanet') {
            die('############## ManiaRecords-Plugin is for TM2 only! ###################');
        }
        
        Console::println('#######################################'); 
        Console::println('# ManiaRecords v'.$this->version_string); 
        Console::println('# by Bullracing!vni');
        Console::println('# ManiaLive plugin by TheM (MaxSmurf)'); 
        Console::println('# STR: '.$this->serverHash); 
        Console::println('# API: '.$this->api_host); 
        Console::println('#######################################');
        
        $this->initiateConnection();
    }
    
    function initiateConnection() {
        $this->wr['data'] = array();
        $this->wr['map']['uid'] = $this->removespecchars($this->storage->currentMap->uId);
        $this->wr['map']['name'] = $this->removespecchars($this->storage->currentMap->name);
        $this->wr['map']['aut'] = $this->storage->currentMap->author;
        $this->wr['map']['gamemode'] = $this->connection->getCurrentGameInfo()->gameMode;
        $this->wr['method'] = 'request';
        
        $response = $this->mm_request($this->wr);
        $this->wrPanel($response['wrs']); 
    }
    
    function maniarecordsChat($login) {
        $answer = array();
        $answer[0] = '';
        $answer[1] = $login;
        $answer[2] = '76426932';
        $this->showDetailedWRs($answer);
    }
    
    function onPlayerConnect($login, $isSpec) {
        $playerObj = $this->storage->getPlayerObject($login);
        if($this->wrcache != '') {
            $this->connection->sendDisplayManialinkPage($playerObj, $this->wrcache, 0, false);
        }
        
        if($this->config->show_welcome === true) {
            $loginObj = $this->storage->getPlayerObject($login);
            $this->connection->chatSendServerMessage('$fff»» $093This server uses the $l[http://maniarecords.org]$08FManiaRecords$FFF.org$l$093 - Worldrecord Database!', $playerObj);
        }
    }

    function onBeginMap($map, $warmUp, $matchContinuation) {
        if(!isset($map) || is_null($map)) {
            return;
        }
        
        if($map['UId'] != '' && $map['Name'] != '') {
            $this->wrPanel_hide();
            if($this->lastaction == 'STARTRACE') {
                return;
            }
            
            $this->lastaction = 'STARTRACE';
            
            $this->wr['data'] = array();
            $this->wr['map']['uid'] = $this->removespecchars($this->storage->currentMap->uId); 
            $this->wr['map']['name'] = $this->removespecchars($this->storage->currentMap->name);
            $this->wr['map']['aut'] = $this->storage->currentMap->author; 
            $this->wr['map']['gamemode'] = $this->connection->getCurrentGameInfo()->gameMode;
            $this->wr['method'] = 'request';
            
            $this->wr['playermax'] = $this->connection->getMaxPlayers();
            $this->wr['playermax'] = $this->wr['playermax']['CurrentValue'];
            $this->wr['playercount'] = count($this->storage->players);
            
            $response = $this->mm_request($this->wr);
            if(!isset($response['wrs'])) {
                $response['wrs'] = array();
                $wrlastrec = 0; 
                $wrdbcount = 0; 
                if($this->config->show_wr_callout === true) {
                    $this->connection->chatSendServerMessage('$fff»» $093This track does not have any World Records (yet)!');
                    Console::println('[WR] No WRs on this track!');
                } else {
                    Console::println('[WR] [NoDisplay] No WRs on this track!');
                }
            } else { 
                $wrlastrec = $response['wrs'][count($response['wrs'])-1]['record']; 
                $wrdbcount = count($response['wrs']);
                $span = Time::fromTM($wrlastrec-$response['wrs'][0]['record']); 
                if($this->config->show_wr_callout === true) {
                    $this->connection->chatSendServerMessage('$fff»» $093The World Record on $fff$l[tm.maniarecords.org/?a=trackinfo&uid='.$map['UId'].']'.$map['Name'].'$l$z$s$093 is driven by $fff'.$response['wrs'][0]['nickname'].'$z$s$093 with a time of $fff'.Time::fromTM($response['wrs'][0]['record']).'$093!');
                    Console::println('[WR] First WR: '.Time::fromTM($response['wrs'][0]['record']).' - Last WR: '.Time::fromTM($wrlastrec).' Difference: '.$span.' on '.$map['Name']);
                } else {
                    Console::println('[WR] [NoDisplay] First WR: '.Time::fromTM($response['wrs'][0]['record']).' - Last WR: '.Time::fromTM($wrlastrec).' Difference: '.$span.' on '.$map['Name']);
                }
            }
            $this->wrPanel($response['wrs']); 
        }
    }
    
    function onEndRace($rankings, $map) {
        $this->wrPanel_hide(); 
        
        if($this->lastaction == 'ENDRACE') {
            return;
        }
        
        $this->lastaction = 'ENDRACE';
        $this->wr['method'] = 'submit';
        $this->wr['map']['uid'] = $this->removespecchars($this->storage->currentMap->uId); 
        $this->wr['map']['name'] = $this->removespecchars($this->storage->currentMap->name); 
        $this->wr['map']['aut'] = $this->storage->currentMap->author;
        $this->wr['map']['gamemode'] = $this->connection->getCurrentGameInfo()->gameMode;
        $response = $this->mm_request($this->wr);
         
        if(!isset($response['wrs'])) {
            $response['wrs'] = array();
            $wrlastrec = 0; 
            $wrdbcount = 0; 
            if($this->config->show_wr_callout === true) {
                $this->connection->chatSendServerMessage('$fff»» $093This track does not have any World Records (yet)!');
                Console::println('[WR] No WRs on this track!');
            } else {
                Console::println('[WR] [NoDisplay] No WRs on this track!');
            }
        } else { 
            $wrlastrec = $response['wrs'][count($response['wrs'])-1]['record']; 
            $wrdbcount = count($response['wrs']);
            $span = Time::fromTM($wrlastrec-$response['wrs'][0]['record']); 
            if($this->config->show_wr_callout === true) {
                $this->connection->chatSendServerMessage('$fff»» $093The World Record on $fff$l[tm.maniarecords.org/?a=trackinfo&uid='.$this->storage->currentMap->uId.']'.$this->storage->currentMap->name.'$l$z$s$093 is driven by $fff'.$response['wrs'][0]['nickname'].'$z$s$093 with a time of $fff'.Time::fromTM($response['wrs'][0]['record']).'$093!');
                Console::println('[WR] First WR: '.Time::fromTM($response['wrs'][0]['record']).' - Last WR: '.Time::fromTM($wrlastrec).' Difference: '.$span.' on '.$map['Name']);
            } else {
                Console::println('[WR] [NoDisplay] First WR: '.Time::fromTM($response['wrs'][0]['record']).' - Last WR: '.Time::fromTM($wrlastrec).' Difference: '.$span.' on '.$map['Name']);
            }
        }
        $this->wrPanel($response['wrs']);
    }
    
    function onPlayerFinish($playerUid, $login, $timeOrScore) {
        $login = $login; 
        $score = $timeOrScore;
        $loginObj = $this->storage->getPlayerObject($login);
        $nickname = $loginObj->nickName;
        
        if(isset($this->wr['cplog'][$login])) {
            $cps = $this->wr['cplog'][$login]; 
        } else {
            $cps = array();
        }
         
        $this->wr['cplog'][$login] = array();
        
        if($score) {
            if(!isset($this->wr['data'][$login]) || (!isset($this->wr['data'][$login]['best'])) || ($this->wr['data'][$login]['best'] > $score)) { 
                $this->wr['data'][$login]['best'] = $score; 
                $this->wr['data'][$login]['nickname'] = $this->removespecchars($nickname); 
                $this->wr['data'][$login]['nicknameclean'] = String::stripAllTmStyle($this->removespecchars($nickname)); 
                $this->wr['data'][$login]['timestamp'] = time(); 
                $this->wr['data'][$login]['cp'] = $cps; 
                
                $addnew = true; 
                $showchat = false; 
                $inranks = false; 
                
                if($this->datacache['wrs']) {
                    $k = 0;
                    foreach($this->datacache['wrs'] as $record ) { 
                        if(($record['login'] == $login) && ($record['record'] > $score)) {
                            unset($this->datacache['wrs'][$k]);
                            $addnew = true;
                            $showchat = true;
                            $inranks = true;
                        }
                        
                        if(($record['login'] == $login) && ($record['record'] <= $score)) {
                            $addnew = false;
                            $showchat = false;
                            $inranks = true; 
                        }
                        $k++;
                    }
                    
                    $this->datacache['wrs'] = array_values($this->datacache['wrs']);
                }
                
                if($addnew == true) {
                    $this->datacache['wrs'][] = array('login' => $login, 'nickname' => $this->removespecchars($nickname), 'record' => $score);
                    $this->wrPanel($this->datacache['wrs']);
                }
                
                if($inranks && $showchat) {
                    $newrecords = $this->array_sort_int($this->datacache['wrs'], 'record', 'timestamp');
                    for($ranking = 0; $ranking < count($newrecords); $ranking++) { 
                        if($newrecords[$ranking]['login'] == $login) {
                            if($ranking+1 <= 30) {
                                if($this->config->show_rank_notifications === true) {
                                    if($ranking+1 <= $this->config->show_public_notification_rank) { 
                                        $this->connection->chatSendServerMessage('$fff»» $fff'.$nickname.'$z$s $093gained the $fff'.($ranking+1).'.$093 World Record with a time of $FFF'.Time::fromTM($newrecords[$ranking]['record']).'$093!');
                                    } else { 
                                        $this->connection->chatSendServerMessage('$fff»» $093Congratulations! You claimed the $fff'.($ranking+1).'.$093 World Record with a time of $FFF'.Time::fromTM($newrecords[$ranking]['record']).'$093!', $login);
                                    } 
                                }
                            }
                            break; 
                        }
                    }
                } elseif(!$inranks && !$showchat) {
                    $newrecords = $this->array_sort_int($this->datacache['wrs'], 'record', 'timestamp');
                    for($ranking = 0; $ranking < count($newrecords); $ranking++) { 
                        if($ranking < 30) {
                            if($newrecords[$ranking]['login'] == $login) { 
                                if($ranking+1 <= 30) { 
                                    if($this->config->show_rank_notifications === true) { 
                                        if($ranking+1 <= $this->config->show_public_notification_rank) {
                                            $this->connection->chatSendServerMessage('$fff»» $fff'.$nickname.'$z$s $093gained the $fff'.($ranking+1).'.$093 World Record with a time of $FFF'.Time::fromTM($newrecords[$ranking]['record']).'$093!'); 
                                        } else {
                                            $this->connection->chatSendServerMessage('$fff»» $093Congratulations! You claimed the $fff'.($ranking+1).'.$093 World Record with a time of$FFF'.Time::fromTM($newrecords[$ranking]['record']).'$093!', $login); 
                                        }
                                    } 
                                } 
                                break;
                            } 
                        }
                    }
                }
            } 
        }
    }
    
    function onPlayerCheckpoint($playerUid, $playerLogin, $timeScore, $currentLap, $checkpointIndex) {
        $this->wr['cplog'][$playerLogin][] = $timeScore;
    }
    
    function onPlayerManialinkPageAnswer($playerUid, $login, $answer, array $entries) {
        $array = array();
        $array[0] = $playerUid;
        $array[1] = $login;
        $array[2] = $answer;
        $this->showDetailedWRs($array);
    }
    
    function wrPanel_hide() {
        $xml = '<manialink id="90120002" version="0"></manialink>'; 
        $this->connection->sendDisplayManialinkPage(null, $xml, 0, false); 
        $this->detailsopen = false;
    }
    
    function wrPanel($table) { 
        if($this->lastaction == 'STARTRACE') {
            $posx = $this->config->posX_race;
            $posy = $this->config->posY_race;
            $maxwrs = $this->config->maxwrs_race; 
            $stylewr = $this->config->style_Racewr; 
            $substylewr = $this->config->substyle_Racewr; 
            $styleTitlewr = $this->config->style_RaceTitlewr; 
            $substyleTitlewr = $this->config->substyle_RaceTitlewr; 
            $TextColorwr = $this->config->style_Color_TitlewrRace; 
            $styleIconwr = $this->config->style_Icon_Racewr; 
            $substyleIconwr = $this->config->substyle_Icon_Racewr; 
            $TextColorPositionwr = $this->config->style_RacePositionwr; 
            $TextColorTimewr = $this->config->style_RaceTimewr; 
            $backdark = $this->config->style_RaceBackdark; 
        } else {
            $posx = $this->config->posX_scores;
            $posy = $this->config->posY_scores;
            $maxwrs = $this->config->maxwrs_score;
            $stylewr = $this->config->style_Scorewr; 
            $substylewr = $this->config->substyle_Scorewr; 
            $styleTitlewr = $this->config->style_ScoreTitlewr; 
            $substyleTitlewr = $this->config->substyle_ScoreTitlewr; 
            $TextColorwr = $this->config->style_Color_TitlewrScore; 
            $styleIconwr = $this->config->style_Icon_Scorewr; 
            $substyleIconwr = $this->config->substyle_Icon_Scorewr; 
            $TextColorPositionwr = $this->config->style_ScorePositionwr; 
            $TextColorTimewr = $this->config->style_ScoreTimewr;
            $backdark = $this->config->style_ScoreBackdark; 
        } 
        
        $table = $this->array_sort_int($table, 'record', 'timestamp'); 
        $height_head = -3; 
        $height_recs = $maxwrs*($this->config->fontsize+0.5);
        if(count($table) > $maxwrs) {
            $maxwrs -= 1;
        }
         
        if(count($table) > $maxwrs) {
            $reccount = $maxwrs+1.5;
        } else {
            $reccount = count($table)+0.5;
        } 
        
        $xml = '<manialink id="90120001" version="0">
       <frame posn="'.$posx.' '.$posy.' 0">
          <quad posn="0 0 0" sizen="15.5 '.(abs($height_head) + $height_recs + 1).'" style="'.$stylewr.'" substyle="'.$substylewr.'" action="76426932" ></quad>
          <quad posn="0.4 -0.36 3" sizen="14.7 2" style="'.$styleTitlewr.'" substyle="'.$substyleTitlewr.'" ></quad>';
        $xml .= '<quad posn="0.4 -0.36 3.1" sizen="2.6 2.6" style="'.$styleIconwr.'" substyle="'.$substyleIconwr.'"/>';
        $xml .= '<label posn="1 -0.8 3.2" sizen="15.5 '.(abs($height_head)).'" halign="left" valign="top" textsize="1" scale="0.9" textcolor="'.$TextColorwr.'" text="        ManiaRecords v'.$this->version_string.'"></label>';
        
        $y = ($height_head-0.5); 
        
        if($this->config->style_DarkenScores && $backdark && count($table)) {
            $xml .= '<quad posn="0.2 '.$height_head.' -1" sizen="15.1 '.($reccount*($this->config->fontsize+0.5)).'" style="BgRaceScore2" substyle="BgScores" ></quad>';
        }
        
        if(isset($this->wr['map']['uid'])) { 
            if($table) {
                $count = 0;
                foreach($table as $record) { 
                    if($count++ >= $maxwrs) {
                        break;
                    } 
                    
                    $xml .= '<label posn="0.5 '.$y.' 0" sizen="2 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="'.$TextColorPositionwr.'" text="'.$count.'."></label>'; 
                    $xml .= '<label posn="1.9 '.$y.' 0" sizen="5 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="'.$TextColorTimewr.'" text="'.Time::fromTM($record['record']).'"></label>'; 
                    $xml .= '<label posn="6 '.$y.' 0" sizen="10.4 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="FFFF" text="'.$record['nickname'].'"></label>'; 
                    $y -= ($this->config->fontsize+0.5);
                }
                
                if(count($table) > $maxwrs) { 
                    if(count($table)-1 >= 29) {
                        $lastpos = 29;
                    } else {
                        $lastpos = count($table)-1;
                    }
                     
                    $record = $table[$lastpos]; 
                    $xml .= '<label posn="0.5 '.$y.' 0" sizen="2 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="'.$TextColorPositionwr.'" text="'.($lastpos+1).'."></label>'; 
                    $xml .= '<label posn="1.9 '.$y.' 0" sizen="5 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="'.$TextColorTimewr.'" text="'.Time::fromTM($record['record']).'"></label>'; 
                    $xml .= '<label posn="6 '.$y.' 0" sizen="10.4 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="FFFF" text="'.$record['nickname'].'"></label>'; 
                    $y -= ($this->config->fontsize+0.5);
                } 
            } else { 
                $xml .= '<label posn="0.5 '.$y.' 0" sizen="2 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="'.$TextColorPositionwr.'" text="--."></label>'; 
                $xml .= '<label posn="1.9 '.$y.' 0" sizen="5 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="0F0F" text="-:--.---"></label>'; 
                $xml .= '<label posn="6 '.$y.' 0" sizen="10.4 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="FFFF" text="--"></label>'; 
            }
        } else { 
            $xml .= '<label posn="1 '.$y.' 0" sizen="20 '.($this->config->fontsize+0.5).'" halign="left" valign="top" textsize="'.$this->config->fontsize.'" textcolor="FFFF" text="Receiving data"></label>';
        }
        
        $xml .= '
    	</frame>
    	</manialink>'; 
        $this->connection->sendDisplayManialinkPage(null, $xml, 0, false); 
        $this->wrcache = $xml;
    }
    
    function showDetailedWRs($answer) { 
        $table = $this->array_sort_int($this->datacache['wrs'], 'record', 'timestamp');
        $player = $this->storage->getPlayerObject($answer[1]);
        
        if($answer[2] == '76426932') {
            if(!empty($table)) {
                $window = listWindow::Create($answer[1]);
        		$window->setSize(180, 95);
        		$window->clearAll();
        		// prepare cols ...
        		$window->addColumn('Rec', 0.1);
        		$window->addColumn('Time', 0.3);
                $window->addColumn('Nickname', 0.4);
                $window->addColumn('Link', 0.2);
        
        		// refresh records for this window ...
        		$window->clearItems();
        		$id = 1;
        		foreach($table as $record) {
                    if($id < 31) {
            			$entry = array
            			(
            				'Rec' => $id,
            				'Time' => Time::fromTM($record['record']),
                            'Nickname' => $record['nickname'],
                            'Link' => '$l[http://worldranking.trackmaniaserver.org/?a=players&login='.$record['login'].']Click$l'
            			);
            			$id++;
            			$window->addItem($entry);
                    }
        		}
        
        		$window->centerOnScreen();
        		$window->show();
            } else {
                $this->connection->chatSendServerMessage('$fff»» $093This track does not have any World Records yet!', $answer[1]);
            }
            $this->detailsopen = true;
        }
        
        if($answer[2] == '76426933') {
            $xml = '<manialink id="90120002">
    		</manialink>';
            $this->connection->sendDisplayManialinkPage($player, $xml, 0, false); 
        }
    }
    
    function mm_request($data) {
        $data_to_send = 'data='.$this->build_request($data); 
        $fp = fsockopen($this->api_host, 80, $errno, $errstr, $this->api_timeout); 
        fputs($fp, "POST ".$this->api_path." HTTP/1.1\r\n"); 
        fputs($fp, "Host: ".$this->api_host."\r\n"); 
        fputs($fp, "Referer: ".$this->serverLogin."\r\n"); 
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 
        fputs($fp, "Content-length: ". strlen($data_to_send) ."\r\n"); 
        fputs($fp, "Connection: close\r\n\r\n"); 
        fputs($fp, $data_to_send);
        
        $res = ""; 
        while(!feof($fp)) { 
            $res .= fgets($fp, 1024); 
        } 
        
        $result = explode("\r\n\r\n", $res, 2);
        $header = isset($result[0]) ? $result[0] : '';
        $content = isset($result[1]) ? $result[1] : ''; 
        fclose($fp); 
        $ret = unserialize(($content)); 
        $this->datacache = $ret; 
        if(isset($ret['info'])) {
            $this->connection->chatSendServerMessage('$fff»» $093'.$ret['info']);
        } 
        return $ret;
    }
    
    function get_api_host() {
        $data_to_send = 'get=apihost'; 
        $fp = fsockopen('api.trackmaniaserver.org', 80, $errno, $errstr, $this->api_timeout); 
        fputs($fp, "POST /redirector.php HTTP/1.1\r\n"); 
        fputs($fp, "Host: api.trackmaniaserver.org\r\n"); 
        fputs($fp, "Referer: ".$this->serverLogin."\r\n"); 
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 
        fputs($fp, "Content-length: ". strlen($data_to_send) ."\r\n"); 
        fputs($fp, "Connection: close\r\n\r\n"); 
        fputs($fp, $data_to_send); 
        
        $res = ""; 
        while(!feof($fp)) { 
            $res .= fgets($fp, 1024); 
        } 
        fclose($fp);
        $result = explode("\r\n\r\n", $res, 2);
        $header = isset($result[0]) ? $result[0] : '';
        $content = isset($result[1]) ? $result[1] : ''; 
        return $content;
    }
    
    function build_request($data) { 
        $reqdata = array();
        $reqdata['method'] = $data['method'];
        $reqdata['svr']['login']= $data['serverlogin']; 
        $reqdata['svr']['name']= base64_encode($data['servername']); 
        $reqdata['svr']['hash']= $data['serverhash']; 
        $reqdata['svr']['contr'] = $data['controller']; 
        $reqdata['svr']['ver']= $data['version_string'];
        $reqdata['svr']['pmax']= $data['playermax']; 
        $reqdata['svr']['pcur']= $data['playercount'];
        $reqdata['cha']['uid']= $data['map']['uid']; 
        $reqdata['cha']['name']= base64_encode($data['map']['name']); 
        $reqdata['cha']['gamemode'] = $data['map']['gamemode'];
        $reqdata['cha']['aut'] = $data['map']['aut'];
        $reqdata['pla'] = array(); 
        
        foreach ($data['data'] as $login => $values) {
            $item = array();
            $item['login'] = $login;
            $item['nickname'] = base64_encode($values['nickname']);
            $item['score'] = $values['best'];
            
            if($values['cp']) {
                $item['ctrl'] = $values['cp'][count($values['cp'])-1];
                
                $item['allcps'] = '';
                $cpnum = 0;
                foreach($values['cp'] as $cp) {
                    if($cpnum > 0) {
                        $item['allcps'] .= ',';
                    }
                    $item['allcps'] .= $cp;
                    $cpnum++;
                }
            }
            
            if($this->wrdbcount >= 30) { 
                if($values['best'] <= $wrlastrec) {
                    $reqdata['pla'][] = $item; 
                }
            } else { 
                $reqdata['pla'][] = $item;
            } 
        }
        return urlencode(base64_encode(serialize($reqdata)));
    } 
        
    function removespecchars($text) { 
        $rem = array("'", '\\', '"', "\\", '/', ':', '{', '}');
        return str_replace($rem,'',$text);
    }
    
    function contribute() {
        return md5(filesize(__FILE__));
    }
    
    function array_sort_int($array_unsorted, $item, $item2 = false) { 
        if(!is_array($array_unsorted)) {
            return;
        }
          
        $this->itemholderint = $item;
        $this->itemholderint2 = $item2;
        usort($array_unsorted, array($this, "cmpint")); 
        return $array_unsorted;
    }

    function cmpint($a, $b) { 
        if((int)$a[$this->itemholderint] == (int)$b[$this->itemholderint]) {
            if($this->itemholderint2) {
				return ((int)$a[$this->itemholderint2] < (int)$b[$this->itemholderint2]) ? -1 : 1;
            }
            return 0;
        }
        return ((int)$a[$this->itemholderint] < (int)$b[$this->itemholderint]) ? -1 : 1;
    } 
}
?>
User avatar
papic
Posts: 61
Joined: 15 Jun 2010, 12:17
Location: Montrond les Bains

Re: [Plugin] ManiaRecords v1.1.2 Beta

Post by papic »

yop

i tested a nocturne fix
2011.11.01_16:05
Occured on 01.11.2011 at 16:05:08 at process with ID #3588
---------------------------------
-> ErrorException with code 0
Undefined index: timestamp
- in C:\wamp\ManiaLive2_r273\libraries\ManiaLivePlugins\TheM\ManiaRecords\ManiaRecords.php on line 628
- Stack: #0 C:\wamp\ManiaLive2_r273\libraries\ManiaLivePlugins\TheM\ManiaRecords\ManiaRecords.php(628): ManiaLive\Application\ErrorHandling::createExceptionFromError(8, 'Undefined index...', 'C:\wamp\ManiaLi...', 628, Array)
#1 [internal function]: ManiaLivePlugins\TheM\ManiaRecords\ManiaRecords->cmpint(Array, Array)
#2 C:\wamp\ManiaLive2_r273\libraries\ManiaLivePlugins\TheM\ManiaRecords\ManiaRecords.php(621): usort(Array, Array)
#3 C:\wamp\ManiaLive2_r273\libraries\ManiaLivePlugins\TheM\ManiaRecords\ManiaRecords.php(392): ManiaLivePlugins\TheM\ManiaRecords\ManiaRecords->array_sort_int(Array, 'record', 'timestamp')
#4 C:\wamp\ManiaLive2_r273\libraries\ManiaLivePlugins\TheM\ManiaRecords\ManiaRecords.php(301): ManiaLivePlugins\TheM\ManiaRecords\ManiaRecords->wrPanel(Array)
#5 [internal function]: ManiaLivePlugins\TheM\ManiaRecords\ManiaRecords->onPlayerFinish(247, 'papic', 49628)
#6 C:\wamp\ManiaLive2_r273\libraries\ManiaLive\DedicatedApi\Callback\Event.php(28): call_user_func_array(Array, Array)
#7 C:\wamp\ManiaLive2_r273\libraries\ManiaLive\Event\Dispatcher.php(50): ManiaLive\DedicatedApi\Callback\Event->fireDo(Object(ManiaLivePlugins\TheM\ManiaRecords\ManiaRecords))
#8 C:\wamp\ManiaLive2_r273\libraries\ManiaLive\DedicatedApi\Connection.php(109): ManiaLive\Event\Dispatcher::dispatch(Object(ManiaLive\DedicatedApi\Callback\Event))
#9 C:\wamp\ManiaLive2_r273\libraries\ManiaLive\Application\AbstractApplication.php(150): ManiaLive\DedicatedApi\Connection->executeCallbacks()
#10 C:\wamp\ManiaLive2_r273\bootstrapper.php(80): ManiaLive\Application\AbstractApplication->run()
#11 {main}
User avatar
Xymph
Posts: 1399
Joined: 15 Jun 2010, 20:35
Contact:

Re: [Plugin] ManiaRecords v1.1.2 Beta

Post by Xymph »

nocturne wrote:Well, seeing as you stated you are running ML r273, that'd lead me to believe you're also using the latest dedicated server version -- in which case it should be stated that this plugin isn't compatible, due to Nadeo's incredibly insane idea to change (almost) every instance of 'Challenge' to 'Map'.
That's only true if the controller uses SetApiVersion("2011-10-06"). If not, all the *Map* methods are still available with Challenge in their names (albeit deprecated), and callbacks also use Challenge/Race instead of Map/Match in their names.

You may not agree with Nadeo rolling out a new naming scheme that makes more sense going forward (to SM/QM), but it's up to controllers to actively enable it and thus do away with the old names.

It would have been even better though if that scheme had been implemented before the first beta dedicated server came out in July, so that controller/plugin developers could have gotten used to it before the official game release. But hindsight is 20-20. :)
Developer of XASECO for TMF/TMN ESWC & XASECO2 for TM²: see XAseco.org
Find your way around the Mania community from the TMN ESWC hub, TMF hub, TM² hub, and SM hub
nocturne
Posts: 208
Joined: 23 Jun 2010, 21:31

Re: [Plugin] ManiaRecords v1.1.2 Beta

Post by nocturne »

Ah... I was unaware that the old methods/callbacks were indeed still supported.. Without actually looking, I'd imagine that ML r273 does indeed specify the api version explicitly, otherwise I wouldn't have experienced so many bugs upon attempting an install with MLEPP r1133 when I finally made it to tm2.


As far as 'moving forward' goes... Let's just see if Nadeo finishes this environment, let alone another 2-3.. and then another 2 games..?
User avatar
Xymph
Posts: 1399
Joined: 15 Jun 2010, 20:35
Contact:

Re: [Plugin] ManiaRecords v1.1.2 Beta

Post by Xymph »

nocturne wrote:As far as 'moving forward' goes... Let's just see if Nadeo finishes this environment, let alone another 2-3.. and then another 2 games..?
True, it'll take a few years to see whether the planned finish line (3 games with 3 envs each) will be reached. But since the next release will be ShootMania Storm, that's already an argument for replacing 'Challenge' with 'Map' right now, as the latter fits the FPS genre much better than the former.

Actually, if you think about it, controller developers already anticipated this way way back by naming some chat commands /nextmap, /admin restartmap, etc. :)
Developer of XASECO for TMF/TMN ESWC & XASECO2 for TM²: see XAseco.org
Find your way around the Mania community from the TMN ESWC hub, TMF hub, TM² hub, and SM hub
nocturne
Posts: 208
Joined: 23 Jun 2010, 21:31

Re: [Plugin] ManiaRecords v1.1.2 Beta

Post by nocturne »

papic wrote:yop. . .i tested a nocturne fix [insert error message]
Ah, sry... I didn't spot your message at all.. :teub:

I looked again and noticed I didn't edit the names of the storage (or whatever) classes, so I ended up coming up with this:

Code: Select all

<?php

/**
 * -- ManiaLive plugin --
 * @name ManiaRecords for ManiaLive
 * @date 05-10-2011
 * @version v1.1.2
 * @website http://tm.maniarecords.org
 * 
 * Thanks to Xymph & Co for the quick Xaseco2
 * Thanks to McRobert for styling the plugin
 * Thanks to uBm for the port to FoxControl
 * Thanks to undef.de for his Records-Eyepiece Implementation
 * Thanks to TheM for the port to ManiaLive
 * Thanks to forza for the TMX-Implementation
 * 
 * @author Bullracing!vni
 * @ported Max "TheM" Klaversma <maxklaversma@gmail.com>
 * @copyright 2011
 * 
 * DO NOT EDIT THIS FILE ! 
 * IF YOU MAKE CHANGES ON THE CODE, YOUR SERVER MAY
 * BE BANNED PERMANENTLY FROM THE MANIARECORDS DATABASE.
 * -----------------------------------------------------------
 * Changes v1.1.2
 * Added /maniarecords command (does the same as /worldrecords)
 * Namechange (WorldRecords --> ManiaRecords)
 * -----------------------------------------------------------
 * Changes v1.1.1
 * Added /worldrecords command (gives recordwindow)
 * Code cleanup
 * -----------------------------------------------------------
 * Changes v1.1.0
 * Fixed bug which prevented the plugin to submit records.
 * Changed to API v4.0.0
 * -----------------------------------------------------------
 * Changes v1.0.9
 * Fixed bug which prevented the WorldRecords to make any connection with the World Database.
 * Cleanup
 * -----------------------------------------------------------
 * Changes v1.0.8
 * Initial ManiaLive port
 * -----------------------------------------------------------
 */

namespace ManiaLivePlugins\TheM\ManiaRecords;

use ManiaLive\Utilities\Console;
use ManiaLib\Utils\TMStrings as String;
use ManiaLive\DedicatedApi\Connection;
use ManiaLive\Data\Storage;
use ManiaLive\Utilities\Time;
use ManiaLivePlugins\TheM\ManiaRecords\Config;

use ManiaLivePlugins\TheM\ManiaRecords\Gui\Windows\listWindow;
use ManiaLivePlugins\TheM\ManiaRecords\Gui\Controls\Header;
use ManiaLivePlugins\TheM\ManiaRecords\Gui\Controls\Normal;

class ManiaRecords extends \ManiaLive\PluginHandler\Plugin {
    public $config;
    
    public $wr;
    public $wrcache;
    public $datacache;
    public $wrlastrec;
    public $wrdbcount;
    
    public $itemholderint;
    public $itemholderint2;
    
    public $serverLogin;
    public $serverName;
    public $gameVersion;
    public $serverHash;
    
    public $version = 1;
    public $version_string = '1.1.2';
    
    public $api_path = '/api_v_4_0_0.php';
    public $api_timeout = 10;
    public $api_version = 1;
    public $api_host = 'api.trackmaniaserver.org';
    
    public $controller = '';
    public $lastaction = '';
    public $detailsopen = true;
    
    function onLoad() {
        $this->wr['controller'] = $this->controller = 'ManiaLive r'.\ManiaLiveApplication\Version;
        $this->config = Config::getInstance();
        
        $this->enableDedicatedEvents();
        $this->enableStorageEvents();
        
        $this->setVersion($this->api_version);
        $this->setPublicMethod('getVersion');
        
        $this->api_host = $this->get_api_host();
        
        $cmd = $this->registerChatCommand('maniarecords', 'maniarecordsChat', 0, true);
        $cmd->help = 'Shows the World Records for this track.';
        
        $cmd = $this->registerChatCommand('worldrecords', 'maniarecordsChat', 0, true);
        $cmd->help = 'Shows the World Records for this track.';
        
        $this->gameVersion = $this->connection->getVersion();
        $this->wr['serverlogin'] = $this->serverLogin = $this->storage->serverLogin;
        $this->wr['servername'] = $this->serverName = String::stripAllTmStyle($this->removespecchars($this->storage->server->name));
        $this->wr['serverhash'] = $this->serverHash = $this->contribute();
        $this->wr['version_string'] = $this->version_string;
        
        $this->wr['playermax'] = $this->connection->getMaxPlayers();
        $this->wr['playermax'] = $this->wr['playermax']['CurrentValue'];
        $this->wr['playercount'] = count($this->storage->players);
        
        if($this->gameVersion->name != 'ManiaPlanet') {
            die('############## ManiaRecords-Plugin is for TM2 only! ###################');
        }
        
        Console::println('#######################################'); 
        Console::println('# ManiaRecords v'.$this->version_string); 
        Console::println('# by Bullracing!vni');
        Console::println('# ManiaLive plugin by TheM (MaxSmurf)'); 
        Console::println('# STR: '.$this->serverHash); 
        Console::println('# API: '.$this->api_host); 
        Console::println('#######################################');
        
        $this->initiateConnection();
    }
    
    function initiateConnection() {
        $this->wr['data'] = array();
        $this->wr['challenge']['uid'] = $this->removespecchars($this->storage->currentMap->uId);
        $this->wr['challenge']['name'] = $this->removespecchars($this->storage->currentMap->name);
        $this->wr['challenge']['aut'] = $this->storage->currentMap->author;
        $this->wr['challenge']['gamemode'] = $this->connection->getCurrentGameInfo()->gameMode;
        $this->wr['method'] = 'request';
        
        $response = $this->mm_request($this->wr);
        $this->wrPanel($response['wrs']); 
    }
    
    function maniarecordsChat($login) {
        $answer = array();
        $answer[0] = '';
        $answer[1] = $login;
        $answer[2] = '76426932';
        $this->showDetailedWRs($answer);
    }
    
    function onPlayerConnect($login, $isSpec) {
        $playerObj = $this->storage->getPlayerObject($login);
        if($this->wrcache != '') {
            $this->connection->sendDisplayManialinkPage($playerObj, $this->wrcache, 0, false);
        }
        
        if($this->config->show_welcome === true) {
            $loginObj = $this->storage->getPlayerObject($login);
            $this->connection->chatSendServerMessage('$fff»» $093This server uses the $l[http://maniarecords.org]$08FManiaRecords$FFF.org$l$093 - Worldrecord Database!', $playerObj);
        }
    }

    function onBeginMap($challenge, $warmUp, $matchContinuation) {
        if(!isset($challenge) || is_null($challenge)) {
            return;
        }
        
        if($challenge['UId'] != '' && $challenge['Name'] != '') {
            $this->wrPanel_hide();
            if($this->lastaction == 'STARTRACE') {
                return;
            }
            
            $this->lastaction = 'STARTRACE';
            
            $this->wr['data'] = array();
            $this->wr['challenge']['uid'] = $this->removespecchars($this->storage->currentMap->uId); 
            $this->wr['challenge']['name'] = $this->removespecchars($this->storage->currentMap->name);
            $this->wr['challenge']['aut'] = $this->storage->currentMap->author; 
            $this->wr['challenge']['gamemode'] = $this->connection->getCurrentGameInfo()->gameMode;
            $this->wr['method'] = 'request';
            
            $this->wr['playermax'] = $this->connection->getMaxPlayers();
            $this->wr['playermax'] = $this->wr['playermax']['CurrentValue'];
            $this->wr['playercount'] = count($this->storage->players);
            
            $response = $this->mm_request($this->wr);
            if(!isset($response['wrs'])) {
                $response['wrs'] = array();
                $wrlastrec = 0; 
                $wrdbcount = 0; 
                if($this->config->show_wr_callout === true) {
                    $this->connection->chatSendServerMessage('$fff»» $093This track does not have any World Records (yet)!');
                    Console::println('[WR] No WRs on this track!');
                } else {
                    Console::println('[WR] [NoDisplay] No WRs on this track!');
                }
            } else { 
                $wrlastrec = $response['wrs'][count($response['wrs'])-1]['record']; 
                $wrdbcount = count($response['wrs']);
                $span = Time::fromTM($wrlastrec-$response['wrs'][0]['record']); 
                if($this->config->show_wr_callout === true) {
                    $this->connection->chatSendServerMessage('$fff»» $093The World Record on $fff$l[tm.maniarecords.org/?a=trackinfo&uid='.$challenge['UId'].']'.$challenge['Name'].'$l$z$s$093 is driven by $fff'.$response['wrs'][0]['nickname'].'$z$s$093 with a time of $fff'.Time::fromTM($response['wrs'][0]['record']).'$093!');
                    Console::println('[WR] First WR: '.Time::fromTM($response['wrs'][0]['record']).' - Last WR: '.Time::fromTM($wrlastrec).' Difference: '.$span.' on '.$challenge['Name']);
                } else {
                    Console::println('[WR] [NoDisplay] First WR: '.Time::fromTM($response['wrs'][0]['record']).' - Last WR: '.Time::fromTM($wrlastrec).' Difference: '.$span.' on '.$challenge['Name']);
                }
            }
            $this->wrPanel($response['wrs']); 
        }
    }
    
    function onEndRace($rankings, $challenge) {
        $this->wrPanel_hide(); 
        
        if($this->lastaction == 'ENDRACE') {
            return;
        }
        
        $this->lastaction = 'ENDRACE';
        $this->wr['method'] = 'submit';
        $this->wr['challenge']['uid'] = $this->removespecchars($this->storage->currentMap->uId); 
        $this->wr['challenge']['name'] = $this->removespecchars($this->storage->currentMap->name); 
        $this->wr['challenge']['aut'] = $this->storage->currentMap->author;
        $this->wr['challenge']['gamemode'] = $this->connection->getCurrentGameInfo()->gameMode;
        $response = $this->mm_request($this->wr);
         
        if(!isset($response['wrs'])) {
            $response['wrs'] = array();
            $wrlastrec = 0; 
            $wrdbcount = 0; 
            if($this->config->show_wr_callout === true) {
                $this->connection->chatSendServerMessage('$fff»» $093This track does not have any World Records (yet)!');
                Console::println('[WR] No WRs on this track!');
            } else {
                Console::println('[WR] [NoDisplay] No WRs on this track!');
            }
        } else { 
            $wrlastrec = $response['wrs'][count($response['wrs'])-1]['record']; 
            $wrdbcount = count($response['wrs']);
            $span = Time::fromTM($wrlastrec-$response['wrs'][0]['record']); 
            if($this->config->show_wr_callout === true) {
                $this->connection->chatSendServerMessage('$fff»» $093The World Record on $fff$l[tm.maniarecords.org/?a=trackinfo&uid='.$this->storage->currentMap->uId.']'.$this->storage->currentMap->name.'$l$z$s$093 is driven by $fff'.$response['wrs'][0]['nickname'].'$z$s$093 with a time of $fff'.Time::fromTM($response['wrs'][0]['record']).'$093!');
                Console::println('[WR] First WR: '.Time::fromTM($response['wrs'][0]['record']).' - Last WR: '.Time::fromTM($wrlastrec).' Difference: '.$span.' on '.$challenge['Name']);
            } else {
                Console::println('[WR] [NoDisplay] First WR: '.Time::fromTM($response['wrs'][0]['record']).' - Last WR: '.Time::fromTM($wrlastrec).' Difference: '.$span.' on '.$challenge['Name']);
            }
        }
        $this->wrPanel($response['wrs']);
    }
    
    function onPlayerFinish($playerUid, $login, $timeOrScore) {
        $login = $login; 
        $score = $timeOrScore;
        $loginObj = $this->storage->getPlayerObject($login);
        $nickname = $loginObj->nickName;
        
        if(isset($this->wr['cplog'][$login])) {
            $cps = $this->wr['cplog'][$login]; 
        } else {
            $cps = array();
        }
         
        $this->wr['cplog'][$login] = array();
        
        if($score) {
            if(!isset($this->wr['data'][$login]) || (!isset($this->wr['data'][$login]['best'])) || ($this->wr['data'][$login]['best'] > $score)) { 
                $this->wr['data'][$login]['best'] = $score; 
                $this->wr['data'][$login]['nickname'] = $this->removespecchars($nickname); 
                $this->wr['data'][$login]['nicknameclean'] = String::stripAllTmStyle($this->removespecchars($nickname)); 
                $this->wr['data'][$login]['timestamp'] = time(); 
                $this->wr['data'][$login]['cp'] = $cps; 
                
                $addnew = true; 
                $showchat = false; 
                $inranks = false; 
                
                if($this->datacache['wrs']) {
                    $k = 0;
                    foreach($this->datacache['wrs'] as $record ) { 
                        if(($record['login'] == $login) && ($record['record'] > $score)) {
                            unset($this->datacache['wrs'][$k]);
                            $addnew = true;
                            $showchat = true;
                            $inranks = true;
                        }
                        
                        if(($record['login'] == $login) && ($record['record'] <= $score)) {
                            $addnew = false;
                            $showchat = false;
                            $inranks = true; 
                        }
                        $k++;
                    }
                    
                    $this->datacache['wrs'] = array_values($this->datacache['wrs']);
                }
                
                if($addnew == true) {
                    $this->datacache['wrs'][] = array('login' => $login, 'nickname' => $this->removespecchars($nickname), 'record' => $score);
                    $this->wrPanel($this->datacache['wrs']);
                }
                
                if($inranks && $showchat) {
                    $newrecords = $this->array_sort_int($this->datacache['wrs'], 'record', 'timestamp');
                    for($ranking = 0; $ranking < count($newrecords); $ranking++) { 
                        if($newrecords[$ranking]['login'] == $login) {
                            if($ranking+1 <= 30) {
                                if($this->config->show_rank_notifications === true) {
                                    if($ranking+1 <= $this->config->show_public_notification_rank) { 
                                        $this->connection->chatSendServerMessage('$fff»» $fff'.$nickname.'$z$s $093gained the $fff'.($ranking+1).'.$093 World Record with a time of $FFF'.Time::fromTM($newrecords[$ranking]['record']).'$093!');
                                    } else { 
                                        $this->connection->chatSendServerMessage('$fff»» $093Congratulations! You claimed the $fff'.($ranking+1).'.$093 World Record with a time of $FFF'.Time::fromTM($newrecords[$ranking]['record']).'$093!', $login);
                                    } 
                                }
                            }
                            break; 
                        }
                    }
                } elseif(!$inranks && !$showchat) {
                    $newrecords = $this->array_sort_int($this->datacache['wrs'], 'record', 'timestamp');
                    for($ranking = 0; $ranking < count($newrecords); $ranking++) { 
                        if($ranking < 30) {
                            if($newrecords[$ranking]['login'] == $login) { 
                                if($ranking+1 <= 30) { 
                                    if($this->config->show_rank_notifications === true) { 
                                        if($ranking+1 <= $this->config->show_public_notification_rank) {
                                            $this->connection->chatSendServerMessage('$fff»» $fff'.$nickname.'$z$s $093gained the $fff'.($ranking+1).'.$093 World Record with a time of $FFF'.Time::fromTM($newrecords[$ranking]['record']).'$093!'); 
                                        } else {
                                            $this->connection->chatSendServerMessage('$fff»» $093Congratulations! You claimed the $fff'.($ranking+1).'.$093 World Record with a time of$FFF'.Time::fromTM($newrecords[$ranking]['record']).'$093!', $login); 
                                        }
                                    } 
                                } 
                                break;
                            } 
                        }
                    }
                }
            } 
        }
    }
    
    function onPlayerCheckpoint($playerUid, $playerLogin, $timeScore, $currentLap, $checkpointIndex) {
        $this->wr['cplog'][$playerLogin][] = $timeScore;
    }
    
    function onPlayerManialinkPageAnswer($playerUid, $login, $answer, array $entries) {
        $array = array();
        $array[0] = $playerUid;
        $array[1] = $login;
        $array[2] = $answer;
        $this->showDetailedWRs($array);
    }
    
    function wrPanel_hide() {
        $xml = '<manialink id="90120002" version="0"></manialink>'; 
        $this->connection->sendDisplayManialinkPage(null, $xml, 0, false); 
        $this->detailsopen = false;
    }
    
    function wrPanel($table) { 
        if($this->lastaction == 'STARTRACE') {
            $posx = $this->config->posX_race;
            $posy = $this->config->posY_race;
            $maxwrs = $this->config->maxwrs_race; 
            $stylewr = $this->config->style_Racewr; 
            $substylewr = $this->config->substyle_Racewr; 
            $styleTitlewr = $this->config->style_RaceTitlewr; 
            $substyleTitlewr = $this->config->substyle_RaceTitlewr; 
            $TextColorwr = $this->config->style_Color_TitlewrRace; 
            $styleIconwr = $this->config->style_Icon_Racewr; 
            $substyleIconwr = $this->config->substyle_Icon_Racewr; 
            $TextColorPositionwr = $this->config->style_RacePositionwr; 
            $TextColorTimewr = $this->config->style_RaceTimewr; 
            $backdark = $this->config->style_RaceBackdark; 
        } else {
            $posx = $this->config->posX_scores;
            $posy = $this->config->posY_scores;
            $maxwrs = $this->config->maxwrs_score;
            $stylewr = $this->config->style_Scorewr; 
            $substylewr = $this->config->substyle_Scorewr; 
            $styleTitlewr = $this->config->style_ScoreTitlewr; 
            $substyleTitlewr = $this->config->substyle_ScoreTitlewr; 
            $TextColorwr = $this->config->style_Color_TitlewrScore; 
            $styleIconwr = $this->config->style_Icon_Scorewr; 
            $substyleIconwr = $this->config->substyle_Icon_Scorewr; 
            $TextColorPositionwr = $this->config->style_ScorePositionwr; 
            $TextColorTimewr = $this->config->style_ScoreTimewr;
            $backdark = $this->config->style_ScoreBackdark; 
        } 
        
        $table = $this->array_sort_int($table, 'record', 'timestamp'); 
        $height_head = -3; 
        $height_recs = $maxwrs*($this->config->fontsize+0.5);
        if(count($table) > $maxwrs) {
            $maxwrs -= 1;
        }
         
        if(count($table) > $maxwrs) {
            $reccount = $maxwrs+1.5;
        } else {
            $reccount = count($table)+0.5;
        } 
        
        $xml = '<manialink id="90120001" version="0">
       <frame posn="'.$posx.' '.$posy.' 0">
          <quad posn="0 0 0" sizen="15.5 '.(abs($height_head) + $height_recs + 1).'" style="'.$stylewr.'" substyle="'.$substylewr.'" action="76426932" ></quad>
          <quad posn="0.4 -0.36 3" sizen="14.7 2" style="'.$styleTitlewr.'" substyle="'.$substyleTitlewr.'" ></quad>';
        $xml .= '<quad posn="0.4 -0.36 3.1" sizen="2.6 2.6" style="'.$styleIconwr.'" substyle="'.$substyleIconwr.'"/>';
        $xml .= '<label posn="1 -0.8 3.2" sizen="15.5 '.(abs($height_head)).'" halign="left" valign="top" textsize="1" scale="0.9" textcolor="'.$TextColorwr.'" text="        ManiaRecords v'.$this->version_string.'"></label>';
        
        $y = ($height_head-0.5); 
        
        if($this->config->style_DarkenScores && $backdark && count($table)) {
            $xml .= '<quad posn="0.2 '.$height_head.' -1" sizen="15.1 '.($reccount*($this->config->fontsize+0.5)).'" style="BgRaceScore2" substyle="BgScores" ></quad>';
        }
        
        if(isset($this->wr['challenge']['uid'])) { 
            if($table) {
                $count = 0;
                foreach($table as $record) { 
                    if($count++ >= $maxwrs) {
                        break;
                    } 
                    
                    $xml .= '<label posn="0.5 '.$y.' 0" sizen="2 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="'.$TextColorPositionwr.'" text="'.$count.'."></label>'; 
                    $xml .= '<label posn="1.9 '.$y.' 0" sizen="5 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="'.$TextColorTimewr.'" text="'.Time::fromTM($record['record']).'"></label>'; 
                    $xml .= '<label posn="6 '.$y.' 0" sizen="10.4 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="FFFF" text="'.$record['nickname'].'"></label>'; 
                    $y -= ($this->config->fontsize+0.5);
                }
                
                if(count($table) > $maxwrs) { 
                    if(count($table)-1 >= 29) {
                        $lastpos = 29;
                    } else {
                        $lastpos = count($table)-1;
                    }
                     
                    $record = $table[$lastpos]; 
                    $xml .= '<label posn="0.5 '.$y.' 0" sizen="2 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="'.$TextColorPositionwr.'" text="'.($lastpos+1).'."></label>'; 
                    $xml .= '<label posn="1.9 '.$y.' 0" sizen="5 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="'.$TextColorTimewr.'" text="'.Time::fromTM($record['record']).'"></label>'; 
                    $xml .= '<label posn="6 '.$y.' 0" sizen="10.4 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="FFFF" text="'.$record['nickname'].'"></label>'; 
                    $y -= ($this->config->fontsize+0.5);
                } 
            } else { 
                $xml .= '<label posn="0.5 '.$y.' 0" sizen="2 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="'.$TextColorPositionwr.'" text="--."></label>'; 
                $xml .= '<label posn="1.9 '.$y.' 0" sizen="5 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="0F0F" text="-:--.---"></label>'; 
                $xml .= '<label posn="6 '.$y.' 0" sizen="10.4 '.($this->config->fontsize+0.5).'" halign="left" valign="top" scale="0.9" textsize="'.$this->config->fontsize.'" textcolor="FFFF" text="--"></label>'; 
            }
        } else { 
            $xml .= '<label posn="1 '.$y.' 0" sizen="20 '.($this->config->fontsize+0.5).'" halign="left" valign="top" textsize="'.$this->config->fontsize.'" textcolor="FFFF" text="Receiving data"></label>';
        }
        
        $xml .= '
    	</frame>
    	</manialink>'; 
        $this->connection->sendDisplayManialinkPage(null, $xml, 0, false); 
        $this->wrcache = $xml;
    }
    
    function showDetailedWRs($answer) { 
        $table = $this->array_sort_int($this->datacache['wrs'], 'record', 'timestamp');
        $player = $this->storage->getPlayerObject($answer[1]);
        
        if($answer[2] == '76426932') {
            if(!empty($table)) {
                $window = listWindow::Create($answer[1]);
        		$window->setSize(180, 95);
        		$window->clearAll();
        		// prepare cols ...
        		$window->addColumn('Rec', 0.1);
        		$window->addColumn('Time', 0.3);
                $window->addColumn('Nickname', 0.4);
                $window->addColumn('Link', 0.2);
        
        		// refresh records for this window ...
        		$window->clearItems();
        		$id = 1;
        		foreach($table as $record) {
                    if($id < 31) {
            			$entry = array
            			(
            				'Rec' => $id,
            				'Time' => Time::fromTM($record['record']),
                            'Nickname' => $record['nickname'],
                            'Link' => '$l[http://worldranking.trackmaniaserver.org/?a=players&login='.$record['login'].']Click$l'
            			);
            			$id++;
            			$window->addItem($entry);
                    }
        		}
        
        		$window->centerOnScreen();
        		$window->show();
            } else {
                $this->connection->chatSendServerMessage('$fff»» $093This track does not have any World Records yet!', $answer[1]);
            }
            $this->detailsopen = true;
        }
        
        if($answer[2] == '76426933') {
            $xml = '<manialink id="90120002">
    		</manialink>';
            $this->connection->sendDisplayManialinkPage($player, $xml, 0, false); 
        }
    }
    
    function mm_request($data) {
        $data_to_send = 'data='.$this->build_request($data); 
        $fp = fsockopen($this->api_host, 80, $errno, $errstr, $this->api_timeout); 
        fputs($fp, "POST ".$this->api_path." HTTP/1.1\r\n"); 
        fputs($fp, "Host: ".$this->api_host."\r\n"); 
        fputs($fp, "Referer: ".$this->serverLogin."\r\n"); 
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 
        fputs($fp, "Content-length: ". strlen($data_to_send) ."\r\n"); 
        fputs($fp, "Connection: close\r\n\r\n"); 
        fputs($fp, $data_to_send);
        
        $res = ""; 
        while(!feof($fp)) { 
            $res .= fgets($fp, 1024); 
        } 
        
        $result = explode("\r\n\r\n", $res, 2);
        $header = isset($result[0]) ? $result[0] : '';
        $content = isset($result[1]) ? $result[1] : ''; 
        fclose($fp); 
        $ret = unserialize(($content)); 
        $this->datacache = $ret; 
        if(isset($ret['info'])) {
            $this->connection->chatSendServerMessage('$fff»» $093'.$ret['info']);
        } 
        return $ret;
    }
    
    function get_api_host() {
        $data_to_send = 'get=apihost'; 
        $fp = fsockopen('api.trackmaniaserver.org', 80, $errno, $errstr, $this->api_timeout); 
        fputs($fp, "POST /redirector.php HTTP/1.1\r\n"); 
        fputs($fp, "Host: api.trackmaniaserver.org\r\n"); 
        fputs($fp, "Referer: ".$this->serverLogin."\r\n"); 
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 
        fputs($fp, "Content-length: ". strlen($data_to_send) ."\r\n"); 
        fputs($fp, "Connection: close\r\n\r\n"); 
        fputs($fp, $data_to_send); 
        
        $res = ""; 
        while(!feof($fp)) { 
            $res .= fgets($fp, 1024); 
        } 
        fclose($fp);
        $result = explode("\r\n\r\n", $res, 2);
        $header = isset($result[0]) ? $result[0] : '';
        $content = isset($result[1]) ? $result[1] : ''; 
        return $content;
    }
    
    function build_request($data) { 
        $reqdata = array();
        $reqdata['method'] = $data['method'];
        $reqdata['svr']['login']= $data['serverlogin']; 
        $reqdata['svr']['name']= base64_encode($data['servername']); 
        $reqdata['svr']['hash']= $data['serverhash']; 
        $reqdata['svr']['contr'] = $data['controller']; 
        $reqdata['svr']['ver']= $data['version_string'];
        $reqdata['svr']['pmax']= $data['playermax']; 
        $reqdata['svr']['pcur']= $data['playercount'];
        $reqdata['cha']['uid']= $data['challenge']['uid']; 
        $reqdata['cha']['name']= base64_encode($data['challenge']['name']); 
        $reqdata['cha']['gamemode'] = $data['challenge']['gamemode'];
        $reqdata['cha']['aut'] = $data['challenge']['aut'];
        $reqdata['pla'] = array(); 
        
        foreach ($data['data'] as $login => $values) {
            $item = array();
            $item['login'] = $login;
            $item['nickname'] = base64_encode($values['nickname']);
            $item['score'] = $values['best'];
            
            if($values['cp']) {
                $item['ctrl'] = $values['cp'][count($values['cp'])-1];
                
                $item['allcps'] = '';
                $cpnum = 0;
                foreach($values['cp'] as $cp) {
                    if($cpnum > 0) {
                        $item['allcps'] .= ',';
                    }
                    $item['allcps'] .= $cp;
                    $cpnum++;
                }
            }
            
            if($this->wrdbcount >= 30) { 
                if($values['best'] <= $wrlastrec) {
                    $reqdata['pla'][] = $item; 
                }
            } else { 
                $reqdata['pla'][] = $item;
            } 
        }
        return urlencode(base64_encode(serialize($reqdata)));
    } 
        
    function removespecchars($text) { 
        $rem = array("'", '\\', '"', "\\", '/', ':', '{', '}');
        return str_replace($rem,'',$text);
    }
    
    function contribute() {
        return md5(filesize(__FILE__));
    }
    
    function array_sort_int($array_unsorted, $item, $item2 = false) { 
        if(!is_array($array_unsorted)) {
            return;
        }
          
        $this->itemholderint = $item;
        $this->itemholderint2 = $item2;
        usort($array_unsorted, array($this, "cmpint")); 
        return $array_unsorted;
    }

    function cmpint($a, $b) { 
        if((int)$a[$this->itemholderint] == (int)$b[$this->itemholderint]) {
            if($this->itemholderint2) {
				return ((int)$a[$this->itemholderint2] < (int)$b[$this->itemholderint2]) ? -1 : 1;
            }
            return 0;
        }
        return ((int)$a[$this->itemholderint] < (int)$b[$this->itemholderint]) ? -1 : 1;
    } 
}
?>
It doesn't crash during normal operation at least, though I'm in no condition to try to set a world record atm, hehe.
User avatar
papic
Posts: 61
Joined: 15 Jun 2010, 12:17
Location: Montrond les Bains

Re: [Plugin] ManiaRecords v1.1.2 Beta

Post by papic »

It's ok :yes:

thank you very much nocturne :3

edit

finally it does not work, the next track does not take into account the new record.
nocturne
Posts: 208
Joined: 23 Jun 2010, 21:31

Re: [Plugin] ManiaRecords v1.1.2 Beta

Post by nocturne »

I'll have to take another look I guess... :teub:

There is also the MLEPP development version..
http://code.google.com/p/mlepp/source/b ... niaRecords
Post Reply

Return to “ManiaLive Plugins”

Who is online

Users browsing this forum: No registered users and 1 guest