add thumbnail support using tmio

This commit is contained in:
beu committed 2026-12-01 16:41:38 +01:00
1 parent d21a36d5aa
commit 3333db36db
1 file changed
+60 -3
+60 -3
View File
@@ -33,7 +33,7 @@ class MatchManagerPickAndBan implements ManialinkPageAnswerListener, CallbackLis
* Constants
*/
const PLUGIN_ID = 223;
const PLUGIN_VERSION = 0.4;
const PLUGIN_VERSION = 0.5;
const PLUGIN_NAME = 'MatchManager Pick & Ban';
const PLUGIN_AUTHOR = 'Beu';
@@ -420,6 +420,7 @@ class MatchManagerPickAndBan implements ManialinkPageAnswerListener, CallbackLis
// build map config
$maps = [];
$mapUids = [];
foreach (explode(',', $mapFiles) as $mapPath) {
try {
$mapInfo = $this->maniaControl->getClient()->getMapInfo($mapPath);
@@ -429,6 +430,9 @@ class MatchManagerPickAndBan implements ManialinkPageAnswerListener, CallbackLis
$mapObject->filePath = $mapPath;
$mapObject->name = $mapInfo->name;
$maps[$mapInfo->uId] = $mapObject;
if (!in_array($mapInfo->uId, $mapUids, true)) {
$mapUids[] = $mapInfo->uId;
}
} catch (Exception $e) {
$this->maniaControl->getChat()->sendErrorToAdmins($this->MatchManagerCore->getChatPrefix() . 'Error with the Map to play "' . $mapPath . '": ' . $e->getMessage());
@@ -474,12 +478,61 @@ class MatchManagerPickAndBan implements ManialinkPageAnswerListener, CallbackLis
$this->log('Starting Pick & Ban phase');
$this->sendManialink();
$this->manageStep();
$this->fetchMapThumbnails($mapUids);
$this->maniaControl->getTimerManager()->registerTimerListening($this, 'handleTimer', 1000);
return false;
}
/**
* Fetch thumbnail URLs sequentially and asynchronously from Trackmania.io for each map UID
*
* @param string[] $mapUids
* @param MatchManagerPickAndBan_State|null $state
*/
private function fetchMapThumbnails(array $mapUids, ?MatchManagerPickAndBan_State $state = null): void {
if ($state === null) {
if (empty($mapUids)) return;
$state = $this->state;
}
if ($this->state !== $state) return;
if (empty($mapUids)) {
$this->log('All map thumbnails fetched, sending updated state');
$this->sendStateToProduction();
$this->sendStateToPlayers();
return;
}
$mapUid = array_shift($mapUids);
$url = 'https://trackmania.io/api/map/' . urlencode($mapUid);
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, $url);
$asyncHttpRequest->setContentType(AsyncHttpRequest::CONTENT_TYPE_JSON);
$asyncHttpRequest->setHeaders(['User-Agent: ManiaControl v' . ManiaControl::VERSION . ' - ' . self::PLUGIN_NAME . ' v' . self::PLUGIN_VERSION . 'by beu - Server Login: ' . $this->maniaControl->getServer()->login]);
$asyncHttpRequest->setCallable(function ($json, $error) use ($mapUid, $mapUids, $state) {
if ($this->state !== $state) return;
if ($error || !$json) {
$this->logError('Error when fetching thumbnail for map ' . $mapUid . ': ' . ($error ?: 'Empty response'));
} else {
$data = json_decode($json, true);
if (is_array($data) && isset($data['thumbnailUrl']) && is_string($data['thumbnailUrl'])) {
if (array_key_exists($mapUid, $state->maps)) {
$state->maps[$mapUid]->thumbnailUrl = $data['thumbnailUrl'];
}
} else {
$this->logError('No thumbnailUrl found for map ' . $mapUid);
}
}
$this->fetchMapThumbnails($mapUids, $state);
});
$asyncHttpRequest->getData();
}
public function handleEndMatch() {
if ($this->state->status === MatchManagerPickAndBan_State::STATUS_COMPLETED) {
$setting = $this->maniaControl->getSettingManager()->getSettingObject($this->MatchManagerCore, $this->MatchManagerCore::SETTING_MODE_MAPS);
@@ -661,7 +714,8 @@ main() {
Tools::FitLabelValue(Label_MapName, 1.8, 0.5, 0.1);
declare CMlQuad Quad_MapThumbnail <=> (Frame_Map.GetFirstChild("quad-thumbnail") as CMlQuad);
Quad_MapThumbnail.ImageUrl = "file://Thumbnails/MapUid/" ^ MapInfo.mapUid;
if (MapInfo.thumbnailUrl == "") Quad_MapThumbnail.ImageUrl = "file://Thumbnails/MapUid/" ^ MapInfo.mapUid;
else Quad_MapThumbnail.ImageUrl = MapInfo.thumbnailUrl;
if (MapInfo.picked || MapInfo.banned) {
Quad_MapThumbnail.Colorize = C_Color_White;
@@ -766,7 +820,8 @@ main() {
Label_MapName.Value = MapInfo.name;
Tools::FitLabelValue(Label_MapName, 1.8, 0.5, 0.1);
Quad_MapThumbnail.ImageUrl = "file://Thumbnails/MapUid/" ^ MapInfo.mapUid;
if (MapInfo.thumbnailUrl == "") Quad_MapThumbnail.ImageUrl = "file://Thumbnails/MapUid/" ^ MapInfo.mapUid;
else Quad_MapThumbnail.ImageUrl = MapInfo.thumbnailUrl;
if (Step.login == "") {
if (Step.type == C_StepType_Ban) {
@@ -968,6 +1023,7 @@ main() {
Text name;
Boolean picked;
Boolean banned;
Text thumbnailUrl;
}
#Struct K_MatchManagerPickAndBan_State {
@@ -1028,4 +1084,5 @@ class MatchManagerPickAndBan_Map {
public string $name;
public bool $picked = false;
public bool $banned = false;
public string $thumbnailUrl = '';
}