Changeset View
Standalone View
binaries/data/mods/public/gui/campaign/load.js
- This file was added.
function init() | |||||
{ | |||||
let gameSelection = Engine.GetGUIObjectByName("gameSelection"); | |||||
elexis: Perhaps we want JSdoc for these two, I didn't investigate yet what they contain. | |||||
let campaigns = Engine.ListDirectoryFiles("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) | |||||
gameSelection.selected = 0; | |||||
else if (gameSelection.selected >= campaigns.length) // happens when deleting the last saved game | |||||
gameSelection.selected = campaigns.length - 1; | |||||
elexisUnsubmitted 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… | |||||
else | |||||
selectionChanged(); | |||||
} | |||||
function pathToGame(path) | |||||
{ | |||||
return path.replace("campaignsaves/", "").replace(".0adcampaign", ""); | |||||
} | |||||
elexisUnsubmitted 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… | |||||
function generateLabel(game) | |||||
{ | |||||
let campaignData = loadCampaignSave(game); | |||||
if (!campaignData) | |||||
return "Incompatible - " + game; | |||||
elexisUnsubmitted Not Done Inline Actionscrime against transifex elexis: crime against transifex | |||||
if (!loadCampaignTemplate(campaignData.campaign)) | |||||
return "Incompatible - " + game; | |||||
return sprintf(translate("%(userDesc)s - %(campaignName)s"), { | |||||
"userDesc": campaignData.userDescription, | |||||
"campaignName": g_CampaignTemplate.Name | |||||
}); | |||||
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. | |||||
} | |||||
Done Inline Actionssprintf+translate elexis: sprintf+translate | |||||
function selectionChanged() | |||||
{ | |||||
let gameSelection = Engine.GetGUIObjectByName("gameSelection"); | |||||
let selectionEmpty = gameSelection.selected == -1; | |||||
if (selectionEmpty) | |||||
return; | |||||
} | |||||
elexisUnsubmitted 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 | |||||
wraitiiAuthorUnsubmitted 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. | |||||
function loadCampaign() | |||||
{ | |||||
let gameSelection = Engine.GetGUIObjectByName("gameSelection"); | |||||
let campaign = gameSelection.list_data[gameSelection.selected]; | |||||
reallyLoadCampaign(campaign); | |||||
// TODO: compatibility checks of saved games | |||||
} | |||||
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("\"%(filename)s\""), { | |||||
"filename": gameSelection.list[gameSelection.selected] | |||||
}) + "\n" + translate("Campaign will be permanently deleted, are you sure?"), | |||||
Done Inline Actionsperhaps filename so its more clear to translators? elexis: perhaps `filename` so its more clear to translators? | |||||
translate("DELETE"), | |||||
[translate("No"), translate("Yes")], | |||||
[null, function(){ reallyDeleteCampaign(campaign); }] | |||||
); | |||||
} | |||||
function reallyDeleteCampaign(name) | |||||
{ | |||||
if (!Engine.DeleteJSONFile("campaignsaves/" + name + ".0adcampaign")) | |||||
return; | |||||
if (Engine.ConfigDB_GetValue("user", "currentcampaign") === name) | |||||
Done Inline ActionsRedundant error? elexis: Redundant error? | |||||
{ | |||||
Engine.ConfigDB_RemoveValue("user", "currentcampaign"); | |||||
Engine.ConfigDB_WriteFile("user", "config/user.cfg"); | |||||
Engine.GetGUIObjectByName("subMenuContinueCampaignButton").enabled = false; | |||||
} | |||||
init(); | |||||
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… | |||||
} | |||||
Done Inline Actionsshould be self-explanatory elexis: should be self-explanatory |
Perhaps we want JSdoc for these two, I didn't investigate yet what they contain.