Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/gui/loadgame/SavegameList.js
/** | /** | ||||
* This class obtains the list of savegames from the engine, | * This class obtains the list of savegames from the engine, | ||||
* builds the list dependent on selected filters and sorting order. | * builds the list dependent on selected filters and sorting order. | ||||
* | * | ||||
* If the selected savegame changes, class instances that subscribed via | * If the selected savegame changes, class instances that subscribed via | ||||
* registerSelectionChangeHandler will have their onSelectionChange function | * registerSelectionChangeHandler will have their onSelectionChange function | ||||
* called with the relevant savegame data. | * called with the relevant savegame data. | ||||
*/ | */ | ||||
class SavegameList | class SavegameList | ||||
{ | { | ||||
constructor() | constructor(campaignRun) | ||||
{ | { | ||||
this.savedGamesMetadata = []; | this.savedGamesMetadata = []; | ||||
this.selectionChangeHandlers = []; | this.selectionChangeHandlers = []; | ||||
// If not null, only show games for the following campaign run | |||||
// (campaign save-games are not shown by default). | |||||
this.campaignRun = campaignRun; | |||||
this.gameSelection = Engine.GetGUIObjectByName("gameSelection"); | this.gameSelection = Engine.GetGUIObjectByName("gameSelection"); | ||||
this.gameSelectionFeedback = Engine.GetGUIObjectByName("gameSelectionFeedback"); | this.gameSelectionFeedback = Engine.GetGUIObjectByName("gameSelectionFeedback"); | ||||
this.confirmButton = Engine.GetGUIObjectByName("confirmButton"); | this.confirmButton = Engine.GetGUIObjectByName("confirmButton"); | ||||
this.compatibilityFilter = Engine.GetGUIObjectByName("compatibilityFilter"); | this.compatibilityFilter = Engine.GetGUIObjectByName("compatibilityFilter"); | ||||
this.compatibilityFilter.onPress = () => { this.updateSavegameList(); }; | this.compatibilityFilter.onPress = () => { this.updateSavegameList(); }; | ||||
this.initSavegameList(); | this.initSavegameList(); | ||||
} | } | ||||
Show All 38 Lines | class SavegameList | ||||
updateSavegameList() | updateSavegameList() | ||||
{ | { | ||||
let savedGames = Engine.GetSavedGames(); | let savedGames = Engine.GetSavedGames(); | ||||
// Get current game version and loaded mods | // Get current game version and loaded mods | ||||
let engineInfo = Engine.GetEngineInfo(); | let engineInfo = Engine.GetEngineInfo(); | ||||
if (this.compatibilityFilter.checked) | if (this.compatibilityFilter.checked) | ||||
savedGames = savedGames.filter(game => this.isCompatibleSavegame(game.metadata, engineInfo)); | savedGames = savedGames.filter(game => { | ||||
return this.isCompatibleSavegame(game.metadata, engineInfo) && | |||||
this.campaignFilter(game.metadata, this.campaignRun); | |||||
}); | |||||
else if (this.campaignRun) | |||||
savedGames = savedGames.filter(game => this.campaignFilter(game.metadata, this.campaignRun)); | |||||
this.gameSelection.enabled = !!savedGames.length; | this.gameSelection.enabled = !!savedGames.length; | ||||
this.gameSelectionFeedback.hidden = !!savedGames.length; | this.gameSelectionFeedback.hidden = !!savedGames.length; | ||||
let selectedGameId = this.gameSelection.list_data[this.gameSelection.selected]; | let selectedGameId = this.gameSelection.list_data[this.gameSelection.selected]; | ||||
// Save metadata for the detailed view | // Save metadata for the detailed view | ||||
this.savedGamesMetadata = savedGames.map(game => { | this.savedGamesMetadata = savedGames.map(game => { | ||||
game.metadata.id = game.id; | game.metadata.id = game.id; | ||||
return game.metadata; | return game.metadata; | ||||
}); | }); | ||||
let sortKey = this.gameSelection.selected_column; | let sortKey = this.gameSelection.selected_column; | ||||
let sortOrder = this.gameSelection.selected_column_order; | let sortOrder = this.gameSelection.selected_column_order; | ||||
this.savedGamesMetadata = this.savedGamesMetadata.sort((a, b) => { | this.savedGamesMetadata = this.savedGamesMetadata.sort((a, b) => { | ||||
let cmpA, cmpB; | let cmpA, cmpB; | ||||
switch (sortKey) | switch (sortKey) | ||||
Lint: ESLintBear (default-case): `Expected a default case.` | |||||
{ | { | ||||
case 'date': | case 'date': | ||||
cmpA = +a.time; | cmpA = +a.time; | ||||
cmpB = +b.time; | cmpB = +b.time; | ||||
break; | break; | ||||
case 'mapName': | case 'mapName': | ||||
cmpA = translate(a.initAttributes.settings.Name); | cmpA = translate(a.initAttributes.settings.Name); | ||||
cmpB = translate(b.initAttributes.settings.Name); | cmpB = translate(b.initAttributes.settings.Name); | ||||
Show All 12 Lines | this.savedGamesMetadata = this.savedGamesMetadata.sort((a, b) => { | ||||
return -sortOrder; | return -sortOrder; | ||||
else if (cmpA > cmpB) | else if (cmpA > cmpB) | ||||
return +sortOrder; | return +sortOrder; | ||||
return 0; | return 0; | ||||
}); | }); | ||||
let list = this.savedGamesMetadata.map(metadata => { | let list = this.savedGamesMetadata.map(metadata => { | ||||
let isCompatible = this.isCompatibleSavegame(metadata, engineInfo); | let isCompatible = this.isCompatibleSavegame(metadata, engineInfo) && | ||||
this.campaignFilter(metadata, this.campaignRun); | |||||
return { | return { | ||||
"date": this.generateSavegameDateString(metadata, engineInfo), | "date": this.generateSavegameDateString(metadata, engineInfo), | ||||
"mapName": compatibilityColor(translate(metadata.initAttributes.settings.Name), isCompatible), | "mapName": compatibilityColor(translate(metadata.initAttributes.settings.Name), isCompatible), | ||||
"mapType": compatibilityColor(translateMapType(metadata.initAttributes.mapType), isCompatible), | "mapType": compatibilityColor(translateMapType(metadata.initAttributes.mapType), isCompatible), | ||||
"description": compatibilityColor(metadata.description, isCompatible) | "description": compatibilityColor(metadata.description, isCompatible) | ||||
}; | }; | ||||
}); | }); | ||||
Show All 13 Lines | updateSavegameList() | ||||
// If the last savegame was deleted, or if it was hidden by the compatibility filter, select the new last item. | // If the last savegame was deleted, or if it was hidden by the compatibility filter, select the new last item. | ||||
let selectedGameIndex = this.savedGamesMetadata.findIndex(metadata => metadata.id == selectedGameId); | let selectedGameIndex = this.savedGamesMetadata.findIndex(metadata => metadata.id == selectedGameId); | ||||
if (selectedGameIndex != -1) | if (selectedGameIndex != -1) | ||||
this.gameSelection.selected = selectedGameIndex; | this.gameSelection.selected = selectedGameIndex; | ||||
else if (this.gameSelection.selected >= this.savedGamesMetadata.length) | else if (this.gameSelection.selected >= this.savedGamesMetadata.length) | ||||
this.gameSelection.selected = this.savedGamesMetadata.length - 1; | this.gameSelection.selected = this.savedGamesMetadata.length - 1; | ||||
} | } | ||||
campaignFilter(metadata, campaignRun) | |||||
{ | |||||
if (!campaignRun) | |||||
return !metadata.initAttributes.campaignData; | |||||
if (metadata.initAttributes.campaignData) | |||||
Done Inline Actionsinline ? Stan: inline ? | |||||
return metadata.initAttributes.campaignData.run == campaignRun; | |||||
return false; | |||||
} | |||||
isCompatibleSavegame(metadata, engineInfo) | isCompatibleSavegame(metadata, engineInfo) | ||||
{ | { | ||||
return engineInfo && | return engineInfo && | ||||
metadata.engine_version && | metadata.engine_version && | ||||
metadata.engine_version == engineInfo.engine_version && | metadata.engine_version == engineInfo.engine_version && | ||||
hasSameMods(metadata.mods, engineInfo.mods); | hasSameMods(metadata.mods, engineInfo.mods); | ||||
} | } | ||||
Show All 23 Lines |
Wildfire Games · Phabricator
Expected a default case.