/plugins/LVS.php

https://github.com/pascalmd/PRISM · PHP · 168 lines · 137 code · 31 blank · 0 comment · 10 complexity · 2fce88e6fdfd0282f747d30660463e8e MD5 · raw file

  1. <?php
  2. class LVS extends Plugins
  3. {
  4. const URL = 'http://lfsforum.net/forumdisplay.php?f=312';
  5. const NAME = 'LVS';
  6. const AUTHOR = 'PRISM Dev Team';
  7. const VERSION = PHPInSimMod::VERSION;
  8. const DESCRIPTION = 'Lap Verification System.';
  9. const PATH = '/data/pth/';
  10. private $pth = null;
  11. private $lapValidation = array();
  12. private $onLap = array();
  13. private $onRoad = array();
  14. private $Track = NULL;
  15. public function __construct()
  16. {
  17. $this->registerPacket('onTrackInfo', ISP_STA);
  18. $this->registerPacket('onLoadLayout', ISP_AXI);
  19. $this->registerPacket('onNewPlayer', ISP_NPL);
  20. $this->registerPacket('onPlayerLeave', ISP_PLL);
  21. $this->registerPacket('onNewLap', ISP_LAP);
  22. $this->registerPacket('onVerification', ISP_HLV);
  23. $this->registerPacket('onCarInfo', ISP_MCI);
  24. $this->registerSayCommand('check', 'cmdValid', '<PLID> (LAP) - Check\'s to see if a lap is valid.');
  25. $this->registerSayCommand('plids', 'cmdListPLIDs', 'Gets a list of PLID\'s and the UName and PName connected with that ID.');
  26. }
  27. public function onTrackInfo(IS_STA $STA)
  28. {
  29. if ($this->Track == $STA->Track)
  30. return;
  31. $this->Track = $STA->Track; # Update Track Short Code
  32. $path = ROOTPATH . $this::PATH . $this->Track . '.pth';
  33. if (!file_exists($path))
  34. return $this->pth = NULL; # We don't have a PTH file for this track.
  35. $this->pth = new pth($path);
  36. console("Loaded {$this->Track}.pth");
  37. return PLUGIN_CONTINUE;
  38. }
  39. public function onLoadLayout(IS_AXI $AXI)
  40. {
  41. $trackType = substr($this->Track, -1);
  42. if ($trackType == 'X' OR $trackType == 'Y')
  43. return; # Not a open layout where we need to check for custom pth files.
  44. $path = ROOTPATH . $this::PATH . $AXI->LName . '.pth';
  45. if (!file_exists($path))
  46. return $this->pth = NULL; # We don't have a PTH file for this track.
  47. $this->pth = new pth($path);
  48. console("Loaded {$this->Track}.pth");
  49. return PLUGIN_CONTINUE;
  50. }
  51. public function onNewPlayer(IS_NPL $NPL)
  52. {
  53. $this->onLap[$NPL->PLID] = 1;
  54. $this->lapValidation[$NPL->PLID] = array(1 => TRUE);
  55. }
  56. public function onPlayerLeave(IS_PLL $PLL)
  57. {
  58. unset($this->onLap[$PLL->PLID]);
  59. unset($this->lapValidation[$PLL->PLID]);
  60. }
  61. public function onNewLap(IS_LAP $LAP)
  62. {
  63. $this->onLap[$LAP->PLID] = $LAP->LapsDone;
  64. $this->lapValidation[$LAP->PLID][$this->onLap[$LAP->PLID]] = TRUE;
  65. }
  66. public function onVerification(IS_HLV $HLV)
  67. {
  68. if (!isset($this->lapValidation[$HLV->PLID]))
  69. return PLUGIN_CONTINUE; # In the case where the player that caused the HLV has already also left.
  70. if ($this->lapValidation[$HLV->PLID][$this->onLap[$HLV->PLID]] === FALSE)
  71. return PLUGIN_CONTINUE; # It's already an invalid lap, we don't report it twice.
  72. $cl = $this->getClientByPLID($HLV->PLID);
  73. Msg2Lfs()->Msg("{$cl->PName}'s Lap is ^1invalid^9!")->Send();
  74. $this->lapValidation[$HLV->PLID][$this->onLap[$HLV->PLID]] = FALSE;
  75. }
  76. public function isValid($PLID, $LAP = NULL)
  77. {
  78. if ($LAP === NULL)
  79. $LAP = $this->onLap[$PLID];
  80. return $this->lapValidation[$PLID][$LAP];
  81. }
  82. public function onCarInfo(IS_MCI $MCI)
  83. {
  84. if (!$this->pth) { return PLUGIN_CONTINUE; }
  85. foreach ($MCI->Info as $CompCar)
  86. {
  87. if (!isset($this->lapValidation[$CompCar->PLID]))
  88. continue; # In the case where the player has already left.
  89. $isRoad = $this->pth->isOnRoad($CompCar->X, $CompCar->Y, $CompCar->Node);
  90. if (!isset($this->onRoad[$CompCar->PLID]))
  91. $this->onRoad[$CompCar->PLID] = NULL;
  92. if ($this->onRoad[$CompCar->PLID] == $isRoad)
  93. continue; # They already know.
  94. if ($isRoad === FALSE)
  95. Msg2Lfs()->PLID($CompCar->PLID)->Text('You are ^1off^9 the track!')->Send();
  96. else
  97. Msg2Lfs()->PLID($CompCar->PLID)->Text('You are ^2on^9 the track!')->Send();
  98. $this->onRoad[$CompCar->PLID] = $isRoad;
  99. if ($isRoad === FALSE OR $this->lapValidation[$CompCar->PLID][$this->onLap[$CompCar->PLID]] === FALSE)
  100. continue; # It's already an invalid lap, we don't report it twice.
  101. Msg2Lfs()->Msg("{$this->getClientByPLID($CompCar->PLID)->PName}'s Lap is ^1invalid^9!")->Send();
  102. $this->lapValidation[$CompCar->PLID][$this->onLap[$CompCar->PLID]] = FALSE;
  103. }
  104. }
  105. public function cmdValid($cmd, $ucid)
  106. {
  107. $argc = count($argv = str_getcsv($cmd, ' '));
  108. $plid = (isset($argv[1])) ? $argv[1] : $this->getClientByUCID($ucid)->PLID;
  109. if ($this->isValid($plid, $argv[2]))
  110. Msg2Lfs()->UCID($ucid)->Text('Lap is valid.')->Send();
  111. else
  112. Msg2Lfs()->UCID($ucid)->Text('Lap is not valid.')->Send();
  113. return PLUGIN_HANDLED;
  114. }
  115. public function cmdListPLIDs($cmd, $ucid)
  116. {
  117. ksort($this->onLap);
  118. forEach ($this->onLap as $PLID => $LAP)
  119. {
  120. $cl = $this->getClientByPLID($PLID);
  121. Msg2Lfs()->UCID($ucid)->Text("{$PLID} : {$cl->UCID} - {$cl->UName} - {$cl->PName}")->Send();
  122. }
  123. }
  124. }
  125. ?>