Changeset View
Standalone View
binaries/data/mods/public/gui/savedgames/load.js
var g_SavedGamesMetadata = []; | var g_SavedGamesMetadata = []; | ||||
/** | /** | ||||
* Needed for formatPlayerInfo to show the player civs in the details. | * Needed for formatPlayerInfo to show the player civs in the details. | ||||
*/ | */ | ||||
const g_CivData = loadCivData(); | const g_CivData = loadCivData(); | ||||
function init() | function init() | ||||
{ | { | ||||
let savedGames = Engine.GetSavedGames(); | |||||
// Get current game version and loaded mods | |||||
let engineInfo = Engine.GetEngineInfo(); | |||||
if (Engine.GetGUIObjectByName("compatibilityFilter").checked) | |||||
elexis: comment unneeded | |||||
savedGames = savedGames.filter(game => isCompatibleSavegame(game.metadata, engineInfo)); | |||||
let gameSelection = Engine.GetGUIObjectByName("gameSelection"); | let gameSelection = Engine.GetGUIObjectByName("gameSelection"); | ||||
let savedGames = Engine.GetSavedGames().sort(sortDecreasingDate); | |||||
gameSelection.enabled = !!savedGames.length; | gameSelection.enabled = !!savedGames.length; | ||||
if (!savedGames.length) | Engine.GetGUIObjectByName("gameSelectionFeedback").hidden = !!savedGames.length; | ||||
let selectedGameId = gameSelection.list_data[gameSelection.selected]; | |||||
// Save metadata for the detailed view | |||||
g_SavedGamesMetadata = savedGames.map(game => | |||||
{ | { | ||||
gameSelection.list = [translate("No saved games found")]; | game.metadata.id = game.id; | ||||
gameSelection.selected = -1; | return game.metadata; | ||||
Not Done Inline Actions(1) .id (2) Those three translate calls could become a single translate call here: (3) depending on how many attributes exist, might be shorter to write foo => ([ "prop1": ..., "prop2": ...}), didn't test whether it makes sense here elexis: (1) .id
(2) Those three translate calls could become a single translate call here:
cmpA =… | |||||
Not Done Inline Actions(1) Done. vladislavbelov: (1) Done.
(2) If we want to cache translate, then metadata should contain duplicates… | |||||
return; | }); | ||||
} | |||||
// Get current game version and loaded mods | const sortKey = gameSelection.selected_column; | ||||
let engineInfo = Engine.GetEngineInfo(); | const sortOrder = gameSelection.selected_column_order; | ||||
Not Done Inline Actionswe very rarely use const, maybe let is more in line with the rest of gui/, dont care really elexis: we very rarely use `const`, maybe `let` is more in line with the rest of gui/, dont care really | |||||
Not Done Inline ActionsWe don't change this value, so it has the reason to be const. vladislavbelov: We don't change this value, so it has the reason to be const. | |||||
g_SavedGamesMetadata = g_SavedGamesMetadata.sort((a, b) => | |||||
{ | |||||
let cmpA, cmpB; | |||||
switch (sortKey) | |||||
{ | |||||
case 'date': | |||||
cmpA = +a.time; | |||||
cmpB = +b.time; | |||||
break; | |||||
case 'mapName': | |||||
cmpA = translate(a.initAttributes.settings.Name); | |||||
cmpB = translate(b.initAttributes.settings.Name); | |||||
break; | |||||
case 'mapType': | |||||
cmpA = translateMapType(a.initAttributes.mapType); | |||||
cmpB = translateMapType(b.initAttributes.mapType); | |||||
break; | |||||
case 'description': | |||||
cmpA = a.description; | |||||
cmpB = b.description; | |||||
break; | |||||
} | |||||
Done Inline Actionstrailing whitespace elexis: trailing whitespace | |||||
if (cmpA < cmpB) | |||||
return -sortOrder; | |||||
else if (cmpA > cmpB) | |||||
return +sortOrder; | |||||
return 0; | |||||
}); | |||||
Done Inline Actionstrailing whitespace elexis: trailing whitespace | |||||
let list = g_SavedGamesMetadata.map(metadata => { | |||||
let isCompatible = isCompatibleSavegame(metadata, engineInfo); | |||||
return { | |||||
"date": generateSavegameDateString(metadata, engineInfo), | |||||
"mapName": compatibilityColor(translate(metadata.initAttributes.settings.Name), isCompatible), | |||||
"mapType": compatibilityColor(translateMapType(metadata.initAttributes.mapType), isCompatible), | |||||
"description": compatibilityColor(metadata.description, isCompatible) | |||||
}; | |||||
}); | |||||
g_SavedGamesMetadata = savedGames.map(game => game.metadata); | if (list.length) | ||||
list = prepareForDropdown(list); | |||||
gameSelection.list = savedGames.map(game => generateLabel(game.metadata, engineInfo)); | gameSelection.list_date = list.date || []; | ||||
gameSelection.list_data = savedGames.map(game => game.id); | gameSelection.list_mapName = list.mapName || []; | ||||
Not Done Inline Actionscan it happen that the properties are undefined? can we remove the || [] checks? elexis: can it happen that the properties are undefined? can we remove the `|| []` checks? | |||||
Not Done Inline ActionsLook at the replay code, it uses the same logic. I think there could be this case. vladislavbelov: Look at the replay code, it uses the same logic. I think there could be this case. | |||||
gameSelection.list_mapType = list.mapType || []; | |||||
gameSelection.list_description = list.description || []; | |||||
// Change these last, otherwise crash | |||||
gameSelection.list = g_SavedGamesMetadata.map(metadata => generateSavegameLabel(metadata, engineInfo)); | |||||
Not Done Inline Actionsthose 2 properties don't seem to be used at all for COList, they are inherited from CList, perhaps we can address this sometime. wraitii also was asking about this in D11 elexis: those 2 properties don't seem to be used at all for COList, they are inherited from CList… | |||||
Not Done Inline ActionsWe use it with all that we used with CList. vladislavbelov: We use it with all that we used with CList. | |||||
elexisUnsubmitted Not Done Inline ActionsAdding // list strings used in the delete dialog for clarification. These two inherited list attributes should be removed from OList sometime. elexis: Adding `// list strings used in the delete dialog` for clarification. These two inherited list… | |||||
gameSelection.list_data = g_SavedGamesMetadata.map(metadata => metadata.id); | |||||
if (gameSelection.selected == -1) | if (gameSelection.selected == -1 && savedGames.length) | ||||
gameSelection.selected = 0; | gameSelection.selected = 0; | ||||
Done Inline Actionslength always exists, right? so !! wouldn't be needed? elexis: length always exists, right? so `!!` wouldn't be needed? | |||||
else if (gameSelection.selected >= savedGames.length) // happens when deleting the last saved game | else if (gameSelection.selected >= g_SavedGamesMetadata.length) // happens when deleting the last saved game | ||||
gameSelection.selected = savedGames.length - 1; | gameSelection.selected = g_SavedGamesMetadata.length - 1; | ||||
else | else | ||||
selectionChanged(); | gameSelection.selected = g_SavedGamesMetadata.findIndex(metadata => metadata.id == selectedGameId); | ||||
Engine.GetGUIObjectByName("deleteGameButton").tooltip = deleteTooltip(); | Engine.GetGUIObjectByName("deleteGameButton").tooltip = deleteTooltip(); | ||||
} | } | ||||
function selectionChanged() | function selectionChanged() | ||||
{ | { | ||||
let metadata = g_SavedGamesMetadata[Engine.GetGUIObjectByName("gameSelection").selected]; | let metadata = g_SavedGamesMetadata[Engine.GetGUIObjectByName("gameSelection").selected]; | ||||
Engine.GetGUIObjectByName("invalidGame").hidden = !!metadata; | Engine.GetGUIObjectByName("invalidGame").hidden = !!metadata; | ||||
▲ Show 20 Lines • Show All 117 Lines • Show Last 20 Lines |
comment unneeded