Compare commits

...

2 Commits

Author SHA1 Message Date
Beu 5f3495b2a8 manage when a player join the server 2023-09-08 19:58:41 +02:00
Beu 36cf818731 Manage when player finish the map 2023-09-08 19:58:16 +02:00
1 changed files with 76 additions and 30 deletions

View File

@ -10,15 +10,16 @@ use ManiaControl\Plugins\Plugin;
use ManiaControl\Players\PlayerManager; use ManiaControl\Players\PlayerManager;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\Callbacks; use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Callbacks\Structures\TrackMania\OnWayPointEventStructure;
use ManiaControl\Commands\CommandListener; use ManiaControl\Commands\CommandListener;
use ManiaControl\Manialinks\LabelLine; use ManiaControl\Manialinks\LabelLine;
use ManiaControl\Manialinks\ManialinkManager; use ManiaControl\Manialinks\ManialinkManager;
use ManiaControl\Players\Player; use ManiaControl\Players\Player;
use ManiaControl\Callbacks\TimerListener; use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Manialinks\ManialinkPageAnswerListener; use ManiaControl\Manialinks\ManialinkPageAnswerListener;
use ManiaControl\Maps\Map; use ManiaControl\Maps\Map;
use ManiaControl\Utils\Formatter;
/** /**
* ClimbTheMap * ClimbTheMap
@ -41,6 +42,7 @@ class ClimbTheMap implements ManialinkPageAnswerListener, TimerListener, Command
// Callbacks // Callbacks
const CB_UPDATEPBS = 'Trackmania.ClimbTheMap.UpdatePBs'; const CB_UPDATEPBS = 'Trackmania.ClimbTheMap.UpdatePBs';
const CB_REQUESTPB = 'Trackmania.ClimbTheMap.RequestPB';
// Methods // Methods
const M_SETPLAYERSPB = 'Trackmania.ClimbTheMap.SetPlayersPB'; const M_SETPLAYERSPB = 'Trackmania.ClimbTheMap.SetPlayersPB';
@ -57,6 +59,7 @@ class ClimbTheMap implements ManialinkPageAnswerListener, TimerListener, Command
private $maniaControl = null; private $maniaControl = null;
private $manialink = ""; private $manialink = "";
private $wraltitude = 0; private $wraltitude = 0;
private $wrtime = 0;
/** /**
* @see \ManiaControl\Plugins\Plugin::prepare() * @see \ManiaControl\Plugins\Plugin::prepare()
@ -109,9 +112,10 @@ class ClimbTheMap implements ManialinkPageAnswerListener, TimerListener, Command
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'handleAfterInit'); $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'handleAfterInit');
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::MP_STARTROUNDSTART, $this, 'handleStartRound'); $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::MP_STARTROUNDSTART, $this, 'handleStartRound');
$this->maniaControl->getCallbackManager()->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect'); $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONFINISHLINE, $this, 'handleFinishCallback');
$this->maniaControl->getCallbackManager()->registerScriptCallbackListener(self::CB_UPDATEPBS, $this, 'handleUpdatePBs'); $this->maniaControl->getCallbackManager()->registerScriptCallbackListener(self::CB_UPDATEPBS, $this, 'handleUpdatePBs');
$this->maniaControl->getCallbackManager()->registerScriptCallbackListener(self::CB_REQUESTPB, $this, 'handleRequestPB');
$this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::A_SHOW_ALTITUDE_RECORDS, $this, 'handleShowAltitudeRecords'); $this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::A_SHOW_ALTITUDE_RECORDS, $this, 'handleShowAltitudeRecords');
$this->maniaControl->getCommandManager()->registerCommandListener('records', $this, 'handleShowAltitudeRecords', false); $this->maniaControl->getCommandManager()->registerCommandListener('records', $this, 'handleShowAltitudeRecords', false);
@ -127,7 +131,8 @@ class ClimbTheMap implements ManialinkPageAnswerListener, TimerListener, Command
`mapIndex` INT(11) NOT NULL, `mapIndex` INT(11) NOT NULL,
`login` varchar(36) NOT NULL, `login` varchar(36) NOT NULL,
`altitude` INT(11) NOT NULL, `altitude` INT(11) NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `time` int(11) DEFAULT -1,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`index`), PRIMARY KEY (`index`),
UNIQUE KEY `map_player` (`mapIndex`,`login`) UNIQUE KEY `map_player` (`mapIndex`,`login`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;'; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;';
@ -140,16 +145,6 @@ class ClimbTheMap implements ManialinkPageAnswerListener, TimerListener, Command
public function handleAfterInit() { public function handleAfterInit() {
$this->handleStartRound(); $this->handleStartRound();
} }
/**
* Handle when a player connects
*
* @param Player $player
*/
public function handlePlayerConnect(Player $player) {
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
}
public function handleStartRound() { public function handleStartRound() {
$map = $this->maniaControl->getMapManager()->getCurrentMap(); $map = $this->maniaControl->getMapManager()->getCurrentMap();
@ -169,12 +164,33 @@ class ClimbTheMap implements ManialinkPageAnswerListener, TimerListener, Command
$wr = $this->getWR($map->index); $wr = $this->getWR($map->index);
if ($wr !== null) { if ($wr !== null) {
$this->wraltitude = $wr[1]; $this->wraltitude = $wr[1];
$this->maniaControl->getClient()->triggerModeScriptEvent(self::M_SETWR, [$wr[0], strval($wr[1])]); $this->wrtime = $wr[2];
$this->maniaControl->getClient()->triggerModeScriptEvent(self::M_SETWR, [$wr[0], strval($wr[1]), strval($wr[2])]);
} else { } else {
$this->wraltitude = 0; $this->wraltitude = 0;
} }
} }
public function handleFinishCallback(OnWayPointEventStructure $structure) {
$map = $this->maniaControl->getMapManager()->getCurrentMap();
if ($map === null) return;
$mapIndex = $map->index;
$login = $structure->getLogin();
$time = $structure->getRaceTime();
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
$stmt = $mysqli->prepare("INSERT INTO `" . self::DB_CLIMBTHEMAP . "` (`mapIndex`, `login`, `time`, `altitude`)
VALUES (?, ?, ?, -1) ON DUPLICATE KEY UPDATE
`time` = IF(`time` < 0 OR `time` > VALUES(`time`),
VALUES(`time`),
`time`);");
$stmt->bind_param('isi', $mapIndex, $login, $time);
$stmt->execute();
// Reset manialink cache
$this->manialink = "";
}
public function handleUpdatePBs(array $data) { public function handleUpdatePBs(array $data) {
$json = json_decode($data[1][0]); $json = json_decode($data[1][0]);
if ($json !== null) { if ($json !== null) {
@ -191,20 +207,32 @@ class ClimbTheMap implements ManialinkPageAnswerListener, TimerListener, Command
$stmt->bind_param('iss', $mapIndex, $login, $altitude); $stmt->bind_param('iss', $mapIndex, $login, $altitude);
foreach ($json as $login => $altitude) { foreach ($json as $login => $altitude) {
$stmt->execute(); $stmt->execute();
if ($this->wraltitude < $altitude) {
$this->wraltitude = $altitude;
}
} }
$mysqli->commit(); $mysqli->commit();
// Reset manialink cache // Reset manialink cache
$this->manialink = ""; $this->manialink = "";
} }
} }
/**
* Handle when a player connects
* Can't use the C++ Callback because the it's received by Maniacontrol before that the Maniascript initialized the Player
*
* @param Player $player
*/
public function handleRequestPB(array $data) {
$login = $data[1][0];
$map = $this->maniaControl->getMapManager()->getCurrentMap();
if ($map === null) return;
// Send PB
$pbs = $this->getPlayersPB($map->index, [$login]);
if (count($pbs) > 0) {
$this->maniaControl->getClient()->triggerModeScriptEvent(self::M_SETPLAYERSPB, [json_encode($pbs)]);
}
}
public function handle1Minute() { public function handle1Minute() {
$map = $this->maniaControl->getMapManager()->getCurrentMap(); $map = $this->maniaControl->getMapManager()->getCurrentMap();
if ($map === null) return; if ($map === null) return;
@ -212,15 +240,16 @@ class ClimbTheMap implements ManialinkPageAnswerListener, TimerListener, Command
$wr = $this->getWR($map->index); $wr = $this->getWR($map->index);
// Update WR if done on an another server // Update WR if done on an another server
if ($wr !== null && $this->wraltitude < $wr[1]) { if ($wr !== null && ($this->wraltitude !== $wr[1] || $this->wrtime !== $wr[2])) {
$this->wraltitude = $wr[1]; $this->wraltitude = $wr[1];
$this->maniaControl->getClient()->triggerModeScriptEvent(self::M_SETWR, [$wr[0], strval($wr[1])]); $this->wrtime = $wr[2];
$this->maniaControl->getClient()->triggerModeScriptEvent(self::M_SETWR, [$wr[0], strval($wr[1]), strval($wr[2])]);
} }
} }
private function getPlayersPB(int $mapIndex, array $logins) { private function getPlayersPB(int $mapIndex, array $logins) {
if (count($logins) === 0) return; if (count($logins) === 0) return [];
$return = []; $return = [];
$mysqli = $this->maniaControl->getDatabase()->getMysqli(); $mysqli = $this->maniaControl->getDatabase()->getMysqli();
@ -242,7 +271,15 @@ class ClimbTheMap implements ManialinkPageAnswerListener, TimerListener, Command
private function getWR(int $mapIndex) { private function getWR(int $mapIndex) {
$mysqli = $this->maniaControl->getDatabase()->getMysqli(); $mysqli = $this->maniaControl->getDatabase()->getMysqli();
$stmt = $mysqli->prepare('SELECT login,altitude FROM `' . self::DB_CLIMBTHEMAP . '` WHERE `mapIndex` = ? ORDER BY altitude DESC, time ASC LIMIT 1;'); $stmt = $mysqli->prepare('SELECT `login`,`altitude`,`time` FROM `' . self::DB_CLIMBTHEMAP . '`
WHERE `mapIndex` = ?
ORDER BY
CASE
WHEN `time` > 0 THEN `time`
ELSE `altitude`
END DESC,
`date` ASC
LIMIT 1;');
$stmt->bind_param('i', $mapIndex); $stmt->bind_param('i', $mapIndex);
if (!$stmt->execute()) { if (!$stmt->execute()) {
trigger_error('Error executing MySQL query: ' . $stmt->error); trigger_error('Error executing MySQL query: ' . $stmt->error);
@ -254,7 +291,7 @@ class ClimbTheMap implements ManialinkPageAnswerListener, TimerListener, Command
$player = $this->maniaControl->getPlayerManager()->getPlayer($data["login"]); $player = $this->maniaControl->getPlayerManager()->getPlayer($data["login"]);
if ($player !== null) { if ($player !== null) {
return [$player->nickname, $data["altitude"]]; return [$player->nickname, $data["altitude"], $data["time"]];
} }
} }
@ -269,11 +306,16 @@ class ClimbTheMap implements ManialinkPageAnswerListener, TimerListener, Command
$mysqli = $this->maniaControl->getDatabase()->getMysqli(); $mysqli = $this->maniaControl->getDatabase()->getMysqli();
$stmt = $mysqli->prepare('SELECT ctm.index,ctm.login,p.nickname,ctm.altitude,ctm.time FROM `' . self::DB_CLIMBTHEMAP . '` ctm $stmt = $mysqli->prepare('SELECT ctm.index,ctm.login,p.nickname,ctm.altitude,ctm.time,ctm.date FROM `' . self::DB_CLIMBTHEMAP . '` ctm
LEFT JOIN `' . PlayerManager::TABLE_PLAYERS . '` p LEFT JOIN `' . PlayerManager::TABLE_PLAYERS . '` p
ON ctm.login = p.login ON ctm.login = p.login
WHERE `mapIndex` = ? WHERE `mapIndex` = ?
ORDER BY altitude DESC, time ASC;'); ORDER BY
CASE
WHEN `time` > 0 THEN `time`
ELSE `altitude`
END DESC,
`date` ASC');
$stmt->bind_param('i', $mapIndex); $stmt->bind_param('i', $mapIndex);
if (!$stmt->execute()) { if (!$stmt->execute()) {
trigger_error('Error executing MySQL query: ' . $stmt->error); trigger_error('Error executing MySQL query: ' . $stmt->error);
@ -320,7 +362,8 @@ class ClimbTheMap implements ManialinkPageAnswerListener, TimerListener, Command
$labelLine = new LabelLine($headFrame); $labelLine = new LabelLine($headFrame);
$labelLine->addLabelEntryText('Rank', $posX + 5); $labelLine->addLabelEntryText('Rank', $posX + 5);
$labelLine->addLabelEntryText('Nickname', $posX + 18); $labelLine->addLabelEntryText('Nickname', $posX + 18);
$labelLine->addLabelEntryText('Altitude', $posX + $width * 0.6); $labelLine->addLabelEntryText('Altitude', $posX + $width * 0.5);
$labelLine->addLabelEntryText('Time', $posX + $width * 0.6);
$labelLine->addLabelEntryText('Date (UTC)', $posX + $width * 0.75); $labelLine->addLabelEntryText('Date (UTC)', $posX + $width * 0.75);
$labelLine->render(); $labelLine->render();
@ -352,8 +395,11 @@ class ClimbTheMap implements ManialinkPageAnswerListener, TimerListener, Command
$labelLine = new LabelLine($recordFrame); $labelLine = new LabelLine($recordFrame);
$labelLine->addLabelEntryText($index + 1, $posX + 5, 13); $labelLine->addLabelEntryText($index + 1, $posX + 5, 13);
$labelLine->addLabelEntryText($record["nickname"], $posX + 18, 52); $labelLine->addLabelEntryText($record["nickname"], $posX + 18, 52);
$labelLine->addLabelEntryText($record["altitude"], $posX + $width * 0.6, 31); $labelLine->addLabelEntryText($record["altitude"], $posX + $width * 0.5, 31);
$labelLine->addLabelEntryText($record["time"], $posX + $width * 0.75, 30); if ($record["time"] > 0) {
$labelLine->addLabelEntryText(Formatter::formatTime($record["time"]), $posX + $width * 0.6, 30);
}
$labelLine->addLabelEntryText($record["date"], $posX + $width * 0.75, 30);
$labelLine->render(); $labelLine->render();
$recordFrame->setY($posY); $recordFrame->setY($posY);