Changeset View
Standalone View
binaries/data/mods/public/gui/campaign/load.js
- This file was added.
var g_Campaigns = []; | |||||
var g_CampaignTemplate = null; | |||||
elexis: Perhaps we want JSdoc for these two, I didn't investigate yet what they contain. | |||||
function init() | |||||
{ | |||||
let gameSelection = Engine.GetGUIObjectByName("gameSelection"); | |||||
let campaigns = Engine.BuildDirEntList("campaignsaves/", "*.0adcampaign", false); | |||||
if (!campaigns.length) | |||||
{ | |||||
gameSelection.list = [translate("No ongoing campaigns found")]; | |||||
gameSelection.selected = -1; | |||||
selectionChanged(); | |||||
Engine.GetGUIObjectByName("loadGameButton").enabled = false; | |||||
Engine.GetGUIObjectByName("deleteGameButton").enabled = false; | |||||
return; | |||||
} | |||||
gameSelection.list = campaigns.map(path => generateLabel(pathToGame(path))); | |||||
gameSelection.list_data = campaigns.map(path => pathToGame(path)); | |||||
if (gameSelection.selected == -1) | |||||
Not Done Inline ActionsLooks like it could become one line using Math.min, Math.max (here and in the place it was copied from) elexis: Looks like it could become one line using `Math.min, Math.max` (here and in the place it was… | |||||
gameSelection.selected = 0; | |||||
else if (gameSelection.selected >= campaigns.length) // happens when deleting the last saved game | |||||
gameSelection.selected = campaigns.length - 1; | |||||
else | |||||
selectionChanged(); | |||||
} | |||||
function pathToGame(path) | |||||
Not Done Inline ActionsPerhaps it's easier to just use the actual filenames as keys (if list and list_data are not easily avoidable by JS) elexis: Perhaps it's easier to just use the actual filenames as keys (if `list` and `list_data` are not… | |||||
{ | |||||
return path.replace("campaignsaves/","").replace(".0adcampaign",""); | |||||
} | |||||
function generateLabel(game) | |||||
{ | |||||
Not Done Inline Actionscrime against transifex elexis: crime against transifex | |||||
let campaignData = loadCampaignSave(game); | |||||
if (!campaignData) | |||||
return "Incompatible - " + game; | |||||
if (!loadCampaignTemplate(campaignData.campaign)) | |||||
return "Incompatible - " + game; | |||||
Not Done Inline ActionsIt should be like the replay menu - incompatible items should be gray while keeping the name elexis: It should be like the replay menu - incompatible items should be gray while keeping the name | |||||
Not Done Inline ActionsNot done yet. wraitii: Not done yet. | |||||
return campaignData.userDescription + " - " + g_CampaignTemplate.Name; | |||||
} | |||||
Done Inline Actionssprintf+translate elexis: sprintf+translate | |||||
function selectionChanged() | |||||
{ | |||||
let gameSelection = Engine.GetGUIObjectByName("gameSelection"); | |||||
let selectionEmpty = gameSelection.selected == -1; | |||||
Engine.GetGUIObjectByName("invalidGame").hidden = !selectionEmpty; | |||||
Engine.GetGUIObjectByName("validGame").hidden = selectionEmpty; | |||||
if (selectionEmpty) | |||||
Not Done Inline ActionsWhen you’re riding a dead horse, the best strategy is to get off elexis: When you’re riding a dead horse, the best strategy is to get off | |||||
Not Done Inline ActionsI think this is supposed to update the GUI elements but I believe I've not implemented that yet. wraitii: I think this is supposed to update the GUI elements but I believe I've not implemented that yet. | |||||
return; | |||||
/* | |||||
Engine.GetGUIObjectByName("savedMapName").caption = translate(metadata.initAttributes.settings.Name); | |||||
let mapData = getMapDescriptionAndPreview(metadata.initAttributes.mapType, metadata.initAttributes.map); | |||||
setMapPreviewImage("savedInfoPreview", mapData.preview); | |||||
Engine.GetGUIObjectByName("savedPlayers").caption = metadata.initAttributes.settings.PlayerData.length - 1; | |||||
Engine.GetGUIObjectByName("savedPlayedTime").caption = timeToString(metadata.gui.timeElapsed ? metadata.gui.timeElapsed : 0); | |||||
Engine.GetGUIObjectByName("savedMapType").caption = translateMapType(metadata.initAttributes.mapType); | |||||
Engine.GetGUIObjectByName("savedMapSize").caption = translateMapSize(metadata.initAttributes.settings.Size); | |||||
Engine.GetGUIObjectByName("savedVictory").caption = translateVictoryCondition(metadata.initAttributes.settings.GameType); | |||||
let caption = sprintf(translate("Mods: %(mods)s"), { "mods": metadata.mods.join(translate(", ")) }); | |||||
if (!hasSameMods(metadata, Engine.GetEngineInfo())) | |||||
caption = "[color=\"orange\"]" + caption + "[/color]"; | |||||
Engine.GetGUIObjectByName("savedMods").caption = caption; | |||||
Engine.GetGUIObjectByName("savedPlayersNames").caption = formatPlayerInfo( | |||||
metadata.initAttributes.settings.PlayerData, | |||||
metadata.gui.states | |||||
); | |||||
*/ | |||||
} | |||||
function loadCampaign() | |||||
{ | |||||
let gameSelection = Engine.GetGUIObjectByName("gameSelection"); | |||||
let campaign = gameSelection.list_data[gameSelection.selected]; | |||||
reallyLoadCampaign(campaign); | |||||
Done Inline Actionsperhaps filename so its more clear to translators? elexis: perhaps `filename` so its more clear to translators? | |||||
// TODO: compatibility checks cf saved games | |||||
/* | |||||
// Check compatibility before really loading it | |||||
let sameMods = hasSameMods(metadata, engineInfo); | |||||
if (sameEngineVersion && sameSavegameVersion && sameMods) | |||||
{ | |||||
reallyLoadGame(gameId); | |||||
return; | |||||
} | |||||
Done Inline ActionsRedundant error? elexis: Redundant error? | |||||
if (!sameMods) | |||||
{ | |||||
if (!metadata.mods) | |||||
metadata.mods = []; | |||||
message += translate("The savegame needs a different set of mods:") + "\n" + | |||||
sprintf(translate("Required: %(mods)s"), { | |||||
"mods": metadata.mods.join(translate(", ")) | |||||
Done Inline ActionsThe condition of the if is a tautology isnt it? elexis: The condition of the if is a tautology isnt it? | |||||
Done Inline ActionsPretty sure it bugged out at some point and thus I added this, but it can probably be removed, yes. wraitii: Pretty sure it bugged out at some point and thus I added this, but it can probably be removed… | |||||
}) + "\n" + | |||||
sprintf(translate("Active: %(mods)s"), { | |||||
"mods": engineInfo.mods.join(translate(", ")) | |||||
Done Inline Actionsshould be self-explanatory elexis: should be self-explanatory | |||||
}); | |||||
} | |||||
message += "\n" + translate("Do you still want to proceed?"); | |||||
messageBox( | |||||
500, 250, | |||||
message, | |||||
translate("Warning"), | |||||
[translate("No"), translate("Yes")], | |||||
[init, function(){ reallyLoadGame(gameId); }] | |||||
);*/ | |||||
} | |||||
function reallyLoadCampaign(name) | |||||
{ | |||||
Engine.ConfigDB_CreateValue("user", "currentcampaign", name); | |||||
Engine.ConfigDB_WriteValueToFile("user", "currentcampaign", name, "config/user.cfg"); | |||||
loadCurrentCampaignSave(); | |||||
} | |||||
function deleteCampaign() | |||||
{ | |||||
let gameSelection = Engine.GetGUIObjectByName("gameSelection"); | |||||
let campaign = gameSelection.list_data[gameSelection.selected]; | |||||
if (!campaign) | |||||
return; | |||||
messageBox( | |||||
500, 200, | |||||
sprintf(translate("\"%(label)s\""), { | |||||
"label": gameSelection.list[gameSelection.selected] | |||||
}) + "\n" + translate("Campaign will be permanently deleted, are you sure?"), | |||||
translate("DELETE"), | |||||
[translate("No"), translate("Yes")], | |||||
[null, function(){ reallyDeleteCampaign(campaign); }] | |||||
); | |||||
} | |||||
function reallyDeleteCampaign(name) | |||||
{ | |||||
if (!Engine.DeleteCampaignGame(name)) | |||||
error("Could not delete campaign game " + name); | |||||
if (Engine.ConfigDB_GetValue("user", "currentcampaign") === name) | |||||
{ | |||||
Engine.ConfigDB_RemoveValue("user", "currentcampaign"); | |||||
Engine.ConfigDB_WriteFile("user", "config/user.cfg"); | |||||
// TODO: this doesn't seem to work. | |||||
if (Engine.GetGUIObjectByName("subMenuContinueCampaignButton")) | |||||
Engine.GetGUIObjectByName("subMenuContinueCampaignButton").enabled = false; | |||||
} | |||||
// re-run init to refresh. | |||||
init(); | |||||
} |
Perhaps we want JSdoc for these two, I didn't investigate yet what they contain.