Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Misc fixes for API model updates #125

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/hud/tab-menu.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/tab-menu.js" />
</scripts>

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;
}
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
18 changes: 9 additions & 9 deletions scripts/hud/tab-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class HudTabMenu {
static setMapData(isOfficial) {
$.GetContextPanel().SetHasClass('hud-tab-menu--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 @@ -51,7 +51,7 @@ class HudTabMenu {
// 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 @@ -87,13 +87,13 @@ class HudTabMenu {
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
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);
}
}
111 changes: 49 additions & 62 deletions scripts/pages/map-selector/map-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,87 +25,74 @@ class MapEntry {
}

static showContextMenu() {
const mapData = $.GetContextPanel().mapData;
if (!mapData) return;
const { mapData, userMapData, isDownloading } = $.GetContextPanel();
if (!mapData || !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
Loading