Skip to content

Commit

Permalink
refactor: Misc fixes for API model updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Panzerhandschuh committed Apr 6, 2024
1 parent 408fe1c commit 13c9cda
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 79 deletions.
1 change: 1 addition & 0 deletions layout/hud/leaderboards.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<scripts>
<include src="file://{scripts}/common/gamemodes.js" />
<include src="file://{scripts}/common/run.js" />
<include src="file://{scripts}/common/map-cache.js" />
<include src="file://{scripts}/hud/leaderboards.js" />
</scripts>

Expand Down
1 change: 1 addition & 0 deletions layout/hud/map-info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
</styles>
<scripts>
<include src="file://{scripts}/hud/map-info.js" />
<include src="file://{scripts}/common/map-cache.js" />
</scripts>

<MomHudMapInfo class="hudmapinfo">
Expand Down
1 change: 1 addition & 0 deletions layout/pages/loading-screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<scripts>
<include src="file://{scripts}/pages/loading-screen.js" />
<include src="file://{scripts}/common/map-cache.js" />
</scripts>

<Panel class="loadingscreen">
Expand Down
1 change: 1 addition & 0 deletions layout/pages/map-selector/map-selector.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
</styles>
<scripts>
<include src="file://{scripts}/common/gamemodes.js" />
<include src="file://{scripts}/common/map-cache.js" />
<include src="file://{scripts}/pages/map-selector/map-selector.js" />
</scripts>

Expand Down
32 changes: 32 additions & 0 deletions scripts/common/map-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Enum for track types
* @enum {number}
*/
const TrackType = {
MAIN: 0,
STAGE: 1,
BONUS: 2
};

/**
* Enum for map credits
* @enum {number}
*/
const MapCreditType = {
UNKNOWN: -1,
AUTHOR: 0,
CONTRIBUTOR: 1,
TESTER: 2,
SPECIAL_THANKS: 3
};

function GetMainTrack(mapData, gamemode) {
return mapData.leaderboards.find(
(leaderboard) =>
leaderboard.gamemode === gamemode && leaderboard.trackType === TrackType.MAIN && leaderboard.style === 0
);
}

function GetNumZones(mapData) {
return mapData.leaderboards.filter((leaderboard) => leaderboard.trackType === TrackType.STAGE).length;
}
18 changes: 9 additions & 9 deletions scripts/hud/leaderboards.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class HudLeaderboards {
static setMapData(isOfficial) {
$.GetContextPanel().SetHasClass('hud-leaderboards--unofficial', !isOfficial);

const img = GameModeInfoWithNull[GameModeAPI.GetCurrentGameMode()].shortName.toLowerCase();
const img = GameModeInfoWithNull[GameModeAPI.GetCurrentGameMode()].idName.toLowerCase();

this.panels.gamemodeImage.SetImage(`file://{images}/gamemodes/${img}.svg`);

Expand All @@ -52,7 +52,7 @@ class HudLeaderboards {
// Delete existing name labels
for (const label of this.panels.credits.Children().slice(1) || []) label.DeleteAsync(0);

const authorCredits = credits.filter((x) => x.type === 'author');
const authorCredits = credits.filter((x) => x.type === MapCreditType.AUTHOR);

for (const credit of authorCredits) {
const namePanel = $.CreatePanel('Label', this.panels.credits, '', {
Expand Down Expand Up @@ -88,13 +88,13 @@ class HudLeaderboards {
static setMapStats(data) {
const cp = $.GetContextPanel();

cp.SetDialogVariableInt('tier', data.mainTrack?.difficulty);
cp.SetDialogVariable(
'type',
$.Localize(data.mainTrack?.isLinear ? '#MapInfo_Type_Linear' : '#MapInfo_Type_Staged')
);
cp.SetDialogVariableInt('numzones', data.mainTrack?.numZones);
cp.SetDialogVariableInt('runs', data.stats?.completes);
const mainTrack = GetMainTrack(data, GameModeAPI.GetCurrentGameMode());
const numZones = GetNumZones(data);

cp.SetDialogVariableInt('tier', mainTrack?.tier ?? 0);
cp.SetDialogVariable('type', $.Localize(mainTrack?.isLinear ? '#MapInfo_Type_Linear' : '#MapInfo_Type_Staged'));
cp.SetDialogVariableInt('numzones', numZones);
cp.SetDialogVariableInt('runs', data.stats?.completions);
}

static close() {
Expand Down
12 changes: 7 additions & 5 deletions scripts/hud/map-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ class HudMapInfo {
this.cachedInfoContainer.visible = true;

let authorString = '';
for (const [i, item] of mapData['credits'].filter((x) => x.type === 'author').entries())
for (const [i, item] of mapData['credits'].filter((x) => x.type === MapCreditType.AUTHOR).entries())
authorString += (i > 0 ? ', ' : '') + item.user.alias;
const cp = $.GetContextPanel();
cp.SetDialogVariable('author', authorString);

const mainTrack = mapData['mainTrack'];
cp.SetDialogVariableInt('tier', mainTrack['difficulty']);
const mainTrack = GetMainTrack(mapData, GameModeAPI.GetCurrentGameMode());
const numZones = GetNumZones(mapData);

cp.SetDialogVariableInt('tier', mainTrack?.tier ?? 0);
cp.SetDialogVariable(
'zonetype',
$.Localize(mainTrack['isLinear'] ? '#MapInfo_Type_Linear' : '#MapInfo_Type_Staged')
$.Localize(mainTrack?.isLinear ? '#MapInfo_Type_Linear' : '#MapInfo_Type_Staged')
);
cp.SetDialogVariableInt('numzones', mainTrack['numZones']);
cp.SetDialogVariableInt('numzones', numZones);
} else {
this.cachedInfoContainer.visible = false;
}
Expand Down
13 changes: 8 additions & 5 deletions scripts/pages/loading-screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ class LoadingScreen {
return;
}

const mainTrack = GetMainTrack(mapData, GameModeAPI.GetCurrentGameMode());
const numZones = GetNumZones(mapData);

this.panels.cp.SetDialogVariable('mapname', mapData.name);
this.panels.cp.SetDialogVariableInt('tier', mapData.mainTrack.difficulty);
this.panels.cp.SetDialogVariableInt('numzones', mapData.mainTrack.numZones);
this.panels.cp.SetDialogVariable('tracktype', mapData.mainTrack.isLinear ? 'Linear' : 'Staged');
this.panels.cp.SetDialogVariableInt('tier', mainTrack?.tier ?? 0);
this.panels.cp.SetDialogVariableInt('numzones', numZones);
this.panels.cp.SetDialogVariable('tracktype', mainTrack?.isLinear ? 'Linear' : 'Staged');

let authorString = '';
for (const [i, item] of mapData.credits.filter((x) => x.type === 'author').entries())
for (const [i, item] of mapData.credits.filter((x) => x.type === MapCreditType.AUTHOR).entries())
authorString += (i > 0 ? ', ' : '') + item.user.alias;
this.panels.cp.SetDialogVariable('author', authorString);

Expand All @@ -80,6 +83,6 @@ class LoadingScreen {
this.panels.tierAndType.visible = true;
this.panels.numZones.visible = true;

this.panels.backgroundImage.SetImage(mapData.thumbnail.urlLarge);
this.panels.backgroundImage.SetImage(mapData.thumbnail.large);
}
}
107 changes: 48 additions & 59 deletions scripts/pages/map-selector/map-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,84 +28,73 @@ class MapEntry {
const mapData = $.GetContextPanel().mapData;
if (!mapData) return;

const userMapData = $.GetContextPanel().userMapData;
if (!userMapData) return;

const items = [];
const isDownloading = $.GetContextPanel().isDownloading;
const mapID = mapData.id;

if (mapData.inLibrary) {
if (mapData.mapFileNeedsUpdate) {
if (isDownloading) {
items.push({
label: $.Localize('#Action_CancelDownload'),
icon: 'file://{images}/cancel.svg',
style: 'icon-color-red',

jsCallback: () => $.DispatchEvent('MapSelector_ShowConfirmCancelDownload', mapID)
});
} else if (MapCacheAPI.MapQueuedForDownload(mapID)) {
items.push({
label: $.Localize('#Action_RemoveFromQueue'),
icon: 'file://{images}/playlist-remove.svg',
style: 'icon-color-red',
jsCallback: () => $.DispatchEvent('MapSelector_RemoveMapFromDownloadQueue', mapID)
});
} else {
items.push({
label: $.Localize('#Action_DownloadMap'),
icon: 'file://{images}/play.svg',
style: 'icon-color-mid-blue',
jsCallback: () => $.DispatchEvent('MapSelector_TryPlayMap', mapID)
});
if (userMapData.mapFileExists) {
items.push(
{
label: $.Localize('#Action_StartMap'),
icon: 'file://{images}/play.svg',
style: 'icon-color-green',
jsCallback: () => $.DispatchEvent('MapSelector_TryPlayMap', mapID)
},
// Gamemode override submenu
{
label: $.Localize('#Action_StartMapOverride'),
icon: 'file://{images}/alternative-mode.svg',
style: 'icon-color-green',
jsCallback: () => this.showGameModeOverrideMenu()
},
{
label: $.Localize('#Action_DeleteMap'),
icon: 'file://{images}/delete.svg',
style: 'icon-color-red',
jsCallback: () => $.DispatchEvent('MapSelector_DeleteMap', mapID)
}
);
} else {
if (isDownloading) {
items.push({
label: $.Localize('#Action_CancelDownload'),
icon: 'file://{images}/cancel.svg',
style: 'icon-color-red',
jsCallback: () => $.DispatchEvent('MapSelector_ShowConfirmCancelDownload', mapID)
});
} else if (MapCacheAPI.MapQueuedForDownload(mapID)) {
items.push({
label: $.Localize('#Action_RemoveFromQueue'),
icon: 'file://{images}/playlist-remove.svg',
style: 'icon-color-red',
jsCallback: () => $.DispatchEvent('MapSelector_RemoveMapFromDownloadQueue', mapID)
});
} else {
items.push(
{
label: $.Localize('#Action_StartMap'),
icon: 'file://{images}/play.svg',
style: 'icon-color-green',

jsCallback: () => $.DispatchEvent('MapSelector_TryPlayMap', mapID)
},

// Gamemode override submenu
{
label: $.Localize('#Action_StartMapOverride'),
icon: 'file://{images}/alternative-mode.svg',
style: 'icon-color-green',
jsCallback: () => this.showGameModeOverrideMenu()
}
);
items.push({
label: $.Localize('#Action_DownloadMap'),
icon: 'file://{images}/play.svg',
style: 'icon-color-mid-blue',
jsCallback: () => $.DispatchEvent('MapSelector_TryPlayMap', mapID)
});
}

items.push({
label: $.Localize('#Action_DeleteMap'),
icon: 'file://{images}/delete.svg',
style: 'icon-color-red',
jsCallback: () => $.DispatchEvent('MapSelector_ToggleMapStatus', mapID, true, false)
});
} else {
items.push({
label: $.Localize('#Action_DownloadMap'),
icon: 'file://{images}/download.svg',
style: 'icon-color-mid-blue',
jsCallback: () => $.DispatchEvent('MapSelector_TryPlayMap', mapID)
});
}

if (mapData.isFavorited) {
if (userMapData.isFavorited) {
items.push({
label: $.Localize('#Action_RemoveFromFavorites'),
icon: 'file://{images}/favorite-remove.svg',
style: 'icon-color-yellow',

jsCallback: () => $.DispatchEvent('MapSelector_ToggleMapStatus', mapID, false, false)
jsCallback: () => $.DispatchEvent('MapSelector_ToggleMapStatus', mapID, false)
});
} else {
items.push({
label: $.Localize('#Action_AddToFavorites'),
icon: 'file://{images}/star.svg',
style: 'icon-color-yellow',
jsCallback: () => $.DispatchEvent('MapSelector_ToggleMapStatus', mapID, false, true)
jsCallback: () => $.DispatchEvent('MapSelector_ToggleMapStatus', mapID, true)
});
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/pages/map-selector/map-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ class MapSelection {
this.panels.credits.RemoveAndDeleteChildren();

// Find all authors
const authorCredits = mapData.credits.filter((x) => x.type === 'author');
const authorCredits = mapData.credits.filter((x) => x.type === MapCreditType.AUTHOR);

const hasCredits = authorCredits.length > 0;

Expand Down

0 comments on commit 13c9cda

Please sign in to comment.