I've started playing with PRISM and I admit this is amazing.
I've made simple plugin which displays best laps of each class in the race. I will add some features like results after race. I haven't tested it with more than one car, but it seems to work.
I've made simple plugin which displays best laps of each class in the race. I will add some features like results after race. I haven't tested it with more than one car, but it seems to work.
<?php
php
/**
* PRSIM Best lap plugin
* This plugin allows you to display best lap of each class in the multi-class race.
*
* @author Makao
* @version 0.1.2
*
*/
class BestLap extends Plugins {
const URL = 'http://www.lfsforum.net/showthread.php?t=76342';
const NAME = 'Best Lap';
const AUTHOR = 'Makao';
const VERSION = '0.1.2';
const DESCRIPTION = 'Display best lap times for each class in multi-class races.';
/**
* Best laps by class array
* @access private
* @var array
*/
private $Laps = array(
# Default best lap time (1 hour = 360000 ms), PLID: 0
# class name => time_plid
'GTR' => '360000_0',
'GT2' => '360000_0',
'GT3' => '360000_0',
'GT9' => '360000_0',
'GTP' => '360000_0',
'NGT' => '360000_0',
'BGT' => '360000_0',
'LRF' => '360000_0',
'LX4' => '360000_0',
'TBO' => '360000_0',
'STD' => '360000_0'
);
/**
* All (top) laps array
* @access private
* @var array
*/
private $TopLaps = array();
/**
* Racing classes array
* @access private
* @var array
*/
private $Classes = array(
# class name => car name, intake restriction, additional mass
'GTR' => 'FZR,0,0;FXR,0,0;XRR,0,0',
'GT2' => 'FZR,20,0;FXR,23,0;XRR,24,0',
'GT3' => 'FZR,28,0;FXR,32,0;XRR,34,0',
'GT9' => 'FZR,21,0;FXR,24,0;XRR,25,0',
'GTP' => 'FZR,10,0;FXR,10,10;XRR,10,20',
'NGT' => 'UFR,0,0;XFR,0,0',
'BGT' => 'UFR,45,0;XFR,43,0',
'LRF' => 'LX6,0,0;RAC,0,0;FZ5,0,0',
'LX4' => 'LX4,0,0',
'TBO' => 'XRT,0,0;FXO,0,0;RB4,0,0',
'STD' => 'XFG,0,0;XRG,0,0'
);
/**
* Drivers array
* @access private
* @var array
*/
private $Drivers = array();
/**
* Show Results flag
* @access private
* @var boolean
*/
private $ShowResults = FALSE;
/**
* Constructor
* @access public
* @return void
*/
public function __construct() {
# Commands
$this->registerLocalCommand('reinit', 'reinitCmd', 'Display app main button.');
# Packets
$this->registerPacket('onPrismConnect', ISP_VER);
$this->registerPacket('compareLaps', ISP_LAP);
$this->registerPacket('onRestart', ISP_RST);
$this->registerPacket('newDriver', ISP_NPL);
}
/**
* Reinit Command
* @access public
* @param string $cmd
* @param int $ucid
* @return void
*/
public function reinitCmd($cmd, $ucid)
{
$bestLapsBTN = new Button(Button::$TO_LOCAL, 'bestLapsShow', 'mainButtons');
$bestLapsBTN->L(85)->T(0)->W(15)->H(6);
$bestLapsBTN->BStyle = ISB_DARK | ISB_LEFT | 1;
$bestLapsBTN->registerOnClick($this, 'DisplayResults');
$bestLapsBTN->Text('BEST LAPS')->send();
}
/**
* Display welcome message
* @access public
* @param IS_VER $VER
* @return void
*/
public function onPrismConnect(IS_VER $VER) {
# Hello World!
IS_MSL()->Msg('^3PRISM: Best lap plugin (^1' . $this::VERSION . '^3) connected.')->Send();
# Show 'Best Laps' button
$this->reinitCmd('reinit', 0);
}
/**
* Compare current lap with best one
* @access public
* @param IS_LAP $LAP
* @return void
*/
public function compareLaps(IS_LAP $LAP) {
if ($LAP->LapsDone != 1) { # Do not compare laps after 1st lap
# Compare top laps
if (isset($this->TopLaps[$LAP->PLID])) {
if ($LAP->LTime < $this->TopLaps[$LAP->PLID]) {
$this->TopLaps[$LAP->PLID] = $LAP->LTime;
}
} else {
$this->TopLaps[$LAP->PLID] = $LAP->LTime;
}
# Compare class laps
$class = $this->getClass($LAP->PLID);
if ($class != '') {
list($best, $PLID) = explode('_', $this->Laps[$class]);
if ($LAP->LTime < $best) {
$this->Laps[$class] = $LAP->LTime . '_' . $LAP->PLID;
$lapTime = timeToString($LAP->LTime);
$client = $this->getClientByPLID($LAP->PLID);
# Local message displaying fastest lap of the class
IS_MSL()->Msg('^7' . $class . ': ^8Fastest lap: ' . $lapTime . ' by ' . $client->PName)->Send();
}
}
# Update results if opened
if ($this->ShowResults == TRUE) {
$this->displayResults();
}
}
}
/**
* On restart delete all stored laps and hide results
* @access public
* @param IS_RST $RST
* @return void
*/
public function onRestart(IS_RST $RST) {
$this->hideResults(); # Hide results - no data to display
foreach ($this->Laps as $class => $time) {
$this->Laps[$class] = '360000_0';
}
$this->TopLaps = array();
$this->Drivers = array();
# I am not sure about it, my imagination and it works :)
$this->sendPacket(IS_TINY()->ReqI(255)->SubT(TINY_NPL));
}
/**
* Add drivers to the array
* @access public
* @param IS_NPL $NPL
* @return void
*/
public function newDriver(IS_NPL $NPL) {
$this->Drivers[$NPL->PLID] = array(
'Name' => $NPL->PName,
'Car' => $NPL->CName,
'Class' => $this->assignClass($NPL->CName, $NPL->H_TRes, $NPL->H_Mass)
);
}
/**
* Display final results
* @access public
* @return void
*/
public function displayResults() {
$this->hideResults(); # Hide results to avoid buttons overlaying
$this->ShowResults = TRUE;
$display = array(
'left' => 5,
'top' => 39,
'width' => 60,
'height' => 8
);
# Title button
$titleBTN = new Button(Button::$TO_LOCAL, 'title', 'bestLapData');
$titleBTN->L($display['left'])->T(27)->W($display['width'])->H($display['height'] + 4);
$titleBTN->BStyle = ISB_DARK | ISB_LEFT | 1;
$titleBTN->Text('Top laps')->send();
# Close button
$closeBTN = new Button(Button::$TO_LOCAL, 'close', 'bestLapData');
$closeBTN->L(61)->T(27)->W(4)->H(8);
$closeBTN->BStyle = ISB_LEFT | 5;
$closeBTN->registerOnClick($this, 'HideResults');
$closeBTN->Text('X')->send();
# Top 3 Laps
asort($this->TopLaps);
for ($i = 1; $i <= 3; $i++) {
$time = ($i == 1) ? current($this->TopLaps) : next($this->TopLaps);
if ($time !== FALSE) {
$plid = key($this->TopLaps);
$client = $this->getClientbyPLID($plid);
$topBTN = new Button(Button::$TO_LOCAL, 'top', 'bestLapData');
$topBTN->L($display['left'])->T($display['top'])->W($display['width'])->H($display['height']);
$topBTN->BStyle = ISB_DARK | ISB_LEFT;
$topBTN->Text('^7'.$i.' ' . $client->PName . ' ^7' . timeToString($time))->send();
$display['top'] += 8;
}
}
# Best Laps (according to classes)
foreach ($this->Laps as $class => $data) {
list($time, $plid) = explode('_', $data);
if ($time != 360000) {
$lapTime = timeToString($time);
$client = $this->getClientByPLID($plid);
# Single class result button
$topClassBTN = new Button(Button::$TO_LOCAL, $class, 'bestLapData');
$topClassBTN->L($display['left'])->T($display['top'])->W($display['width'])->H($display['height']);
$topClassBTN->BStyle = ISB_DARK | ISB_LEFT;
$topClassBTN->Text('^7' . $class . ': ^8' . $lapTime . ' by ' . $client->PName)->send();
$display['top'] += 8;
}
}
}
/**
* Hide final results
* @access public
* @return void
*/
public function hideResults() {
ButtonManager::removeButtonsByGroup(Button::$TO_LOCAL, 'bestLapData');
$this->ShowResults = FALSE;
}
/**
* Assign a racing class for driver according to the selected car
* @access private
* @param string $cname
* @param int $intake
* @param int $mass
* @return string
*/
private function assignClass($cname, $intake, $mass) {
foreach ($this->Classes as $class => $data) {
$row = explode(';', $data);
foreach ($row as $handicaps) {
list($_car, $_intake, $_mass) = explode(',', $handicaps);
if ($_car == $cname && $_intake == $intake && $_mass == $mass) {
return $class;
}
}
}
}
/**
* Return vechicle class based on PLID
* @access private
* @param int $PLID
* @return string
*/
private function getClass($PLID) {
foreach ($this->Drivers as $id => $data) {
if ($id == $PLID) {
return $data['Class'];
}
}
}
}
?>