Ticket #3355: t3355_move_ceasefire_v2.1.patch

File t3355_move_ceasefire_v2.1.patch, 11.4 KB (added by elexis, 9 years ago)

Alternative version where undefined instead of false is returned in case of error.

  • binaries/data/mods/public/gui/common/settings.js

     
    66
    77/**
    88 * The maximum number of teams allowed.
    99 */
    1010const g_MaxTeams = 4;
    1111
    1212// The following settings will be loaded here:
    1313// AIDifficulties, Ceasefire, GameSpeeds, GameTypes, MapTypes,
    1414// MapSizes, PlayerDefaults, PopulationCapacity, StartingResources
     15
     16const g_SettingsDirectory = "simulation/data/settings/";
     17
     18/**
     19 * An object containing all values given by setting name.
     20 * Used by lobby, gamesetup, session, summary screen and replay menu.
     21 */
     22const g_Settings = loadAvailableSettings();
     23
     24/**
     25 * Loads and translates all values of all settings which
     26 * can be configured by dropdowns in the gamesetup.
     27 *
     28 * @returns {Object|undefined}
     29 */
     30function loadAvailableSettings()
     31{
     32    var settings = {};
     33
     34    settings.Ceasefire = loadCeasefire();
     35    if (!settings.Ceasefire)
     36        return undefined;
     37
     38    return settings;
     39}
     40
     41/**
     42 * Loads available ceasefire settings.
     43 *
     44 * @returns {Array|undefined}
     45 */
     46function loadCeasefire()
     47{
     48    var json = Engine.ReadJSONFile(g_SettingsDirectory + "Ceasefire.json");
     49
     50    if (!json || json.Default === undefined || !json.Times || !Array.isArray(json.Times))
     51    {
     52        error("Could not load Ceasefire.json");
     53        return undefined;
     54    }
     55
     56    return json.Times.map(timeout => { return {
     57        "Duration": timeout,
     58        "Default": timeout == json.Default,
     59        "Title": timeout == 0 ? translateWithContext("ceasefire", "No ceasefire") :
     60            sprintf(translatePluralWithContext("ceasefire", "%(minutes)s minute", "%(minutes)s minutes", timeout), { "minutes": timeout })
     61    }});
     62}
     63
     64/**
     65 * Creates an array with all values of that property of the given setting and
     66 * finds the index of the default value.
     67 *
     68 * This allows easy copying of setting values to dropdown lists.
     69 *
     70 * @param settingValues {Array}
     71 * @returns {Object|undefined}
     72 */
     73function prepareForDropdown(settingValues)
     74{
     75    if (!settingValues)
     76        return undefined;
     77
     78    var settings = { "Default": 0 };
     79    for (let index in settingValues)
     80    {
     81        for (let property in settingValues[index])
     82        {
     83            if (property == "Default")
     84                continue;
     85
     86            if (index == 0)
     87                settings[property] = [];
     88
     89            // Switch property and index
     90            settings[property][index] = settingValues[index][property];
     91        }
     92
     93        // Copy default value
     94        if (settingValues[index].Default)
     95            settings.Default = +index;
     96    }
     97    return settings;
     98}
  • binaries/data/mods/public/gui/gamesetup/gamesetup.js

     
    88// TODO: Move these somewhere like simulation\data\game_types.json, Atlas needs them too
    99// Translation: Type of victory condition.
    1010const POPULATION_CAP = ["50", "100", "150", "200", "250", "300", translate("Unlimited")];
    1111const POPULATION_CAP_DATA = [50, 100, 150, 200, 250, 300, 10000];
    1212const POPULATION_CAP_DEFAULTIDX = 5;
    1313// Translation: Amount of starting resources.
    1414const STARTING_RESOURCES = [translateWithContext("startingResources", "Very Low"), translateWithContext("startingResources", "Low"), translateWithContext("startingResources", "Medium"), translateWithContext("startingResources", "High"), translateWithContext("startingResources", "Very High"), translateWithContext("startingResources", "Deathmatch")];
    1515const STARTING_RESOURCES_DATA = [100, 300, 500, 1000, 3000, 50000];
    1616const STARTING_RESOURCES_DEFAULTIDX = 1;
    17 // Translation: Ceasefire.
    18 const CEASEFIRE = [translateWithContext("ceasefire", "No ceasefire"), translateWithContext("ceasefire", "5 minutes"), translateWithContext("ceasefire", "10 minutes"), translateWithContext("ceasefire", "15 minutes"), translateWithContext("ceasefire", "20 minutes"), translateWithContext("ceasefire", "30 minutes"), translateWithContext("ceasefire", "45 minutes"), translateWithContext("ceasefire", "60 minutes")];
    19 const CEASEFIRE_DATA = [0, 5, 10, 15, 20, 30, 45, 60];
    20 const CEASEFIRE_DEFAULTIDX = 0;
     17
     18const g_Ceasefire = prepareForDropdown(g_Settings ? g_Settings.Ceasefire : undefined);
    2119
    2220////////////////////////////////////////////////////////////////////////////////////////////////
    2321
    2422// Is this is a networked game, or offline
    2523var g_IsNetworked;
    2624
    2725// Is this user in control of game settings (i.e. is a network server, or offline player)
    2826var g_IsController;
    2927
     
    7775var g_LoadingState = 0; // 0 = not started, 1 = loading, 2 = loaded
    7876
    7977// Filled by scripts in victory_conditions/
    8078var g_VictoryConditions = {};
    8179
    8280////////////////////////////////////////////////////////////////////////////////////////////////
    8381
    8482function init(attribs)
    8583{
     84    if (!g_Settings)
     85    {
     86        cancelSetup();
     87        return;
     88    }
     89
    8690    switch (attribs.type)
    8791    {
    8892    case "offline":
    8993        g_IsNetworked = false;
    9094        g_IsController = true;
    9195        break;
    9296    case "server":
    9397        g_IsNetworked = true;
    9498        g_IsController = true;
     
    214218        startingResourcesL.selected = STARTING_RESOURCES_DEFAULTIDX;
    215219        startingResourcesL.onSelectionChange = function() {
    216220            if (this.selected != -1)
    217221                g_GameAttributes.settings.StartingResources = STARTING_RESOURCES_DATA[this.selected];
    218222
    219223            updateGameAttributes();
    220224        }
    221225
    222226        var ceasefireL = Engine.GetGUIObjectByName("ceasefire");
    223         ceasefireL.list = CEASEFIRE;
    224         ceasefireL.list_data = CEASEFIRE_DATA;
    225         ceasefireL.selected = CEASEFIRE_DEFAULTIDX;
     227        ceasefireL.list = g_Ceasefire.Title;
     228        ceasefireL.list_data = g_Ceasefire.Duration;
     229        ceasefireL.selected = g_Ceasefire.Default;
    226230        ceasefireL.onSelectionChange = function() {
    227231            if (this.selected != -1)
    228                 g_GameAttributes.settings.Ceasefire = CEASEFIRE_DATA[this.selected];
     232                g_GameAttributes.settings.Ceasefire = g_Ceasefire.Duration[this.selected];
    229233
    230234            updateGameAttributes();
    231235        }
    232236
    233237        var victoryConditions = Engine.GetGUIObjectByName("victoryCondition");
    234238        var victories = getVictoryConditions();
    235239        victoryConditions.list = victories.text;
    236240        victoryConditions.list_data = victories.data;
    237241        victoryConditions.onSelectionChange = function() {
     
    423427    log("Net message: " + uneval(message));
    424428
    425429    switch (message.type)
    426430    {
    427431    case "netstatus":
    428432        switch (message.status)
    429433        {
    430434        case "disconnected":
    431435            cancelSetup();
    432             if (Engine.HasXmppClient())
    433                 Engine.SwitchGuiPage("page_lobby.xml");
    434             else
    435                 Engine.SwitchGuiPage("page_pregame.xml");
    436436            reportDisconnect(message.reason);
    437437            break;
    438438
    439439        default:
    440440            error("Unrecognised netstatus type " + message.status);
    441441            break;
    442442        }
    443443        break;
    444444
     
    810810function cancelSetup()
    811811{
    812812    if (g_IsController)
    813813        saveGameAttributes();
    814814
    815815    Engine.DisconnectNetworkGame();
    816816
    817817    if (Engine.HasXmppClient())
    818818    {
    819         // Set player presence
    820819        Engine.LobbySetPlayerPresence("available");
    821820
    822         // Unregister the game
    823821        if (g_IsController)
    824822            Engine.SendUnregisterGame();
     823
     824        Engine.SwitchGuiPage("page_lobby.xml");
    825825    }
     826    else
     827        Engine.SwitchGuiPage("page_pregame.xml");
    826828}
    827829
    828830function onTick()
    829831{
     832    if (!g_Settings)
     833        return;
     834
    830835    // First tick happens before first render, so don't load yet
    831836    if (g_LoadingState == 0)
    832837    {
    833838        g_LoadingState++;
    834839    }
    835840    else if (g_LoadingState == 1)
    836841    {
    837842        Engine.GetGUIObjectByName("loadingWindow").hidden = true;
    838843        Engine.GetGUIObjectByName("setupWindow").hidden = false;
     
    12461251    observerLateJoin.checked = g_GameAttributes.settings.ObserverLateJoin;
    12471252    observerLateJoinText.caption = observerLateJoin.checked ? translate("Yes") : translate("No");
    12481253
    12491254    gameSpeedText.caption = g_GameSpeeds.names[speedIdx];
    12501255    gameSpeedBox.selected = speedIdx;
    12511256    populationCap.selected = (mapSettings.PopulationCap !== undefined && POPULATION_CAP_DATA.indexOf(mapSettings.PopulationCap) != -1 ? POPULATION_CAP_DATA.indexOf(mapSettings.PopulationCap) : POPULATION_CAP_DEFAULTIDX);
    12521257    populationCapText.caption = POPULATION_CAP[populationCap.selected];
    12531258    startingResources.selected = (mapSettings.StartingResources !== undefined && STARTING_RESOURCES_DATA.indexOf(mapSettings.StartingResources) != -1 ? STARTING_RESOURCES_DATA.indexOf(mapSettings.StartingResources) : STARTING_RESOURCES_DEFAULTIDX);
    12541259    startingResourcesText.caption = STARTING_RESOURCES[startingResources.selected];
    1255     ceasefire.selected = (mapSettings.Ceasefire !== undefined && CEASEFIRE_DATA.indexOf(mapSettings.Ceasefire) != -1 ? CEASEFIRE_DATA.indexOf(mapSettings.Ceasefire) : CEASEFIRE_DEFAULTIDX);
    1256     ceasefireText.caption = CEASEFIRE[ceasefire.selected];
     1260    ceasefire.selected = mapSettings.Ceasefire !== undefined && g_Ceasefire.Duration.indexOf(mapSettings.Ceasefire) != -1 ? g_Ceasefire.Duration.indexOf(mapSettings.Ceasefire) : g_Ceasefire.Default;
     1261    ceasefireText.caption = g_Ceasefire.Title[ceasefire.selected];
    12571262
    12581263    // Update map preview
    12591264    Engine.GetGUIObjectByName("mapPreview").sprite = "cropped:(0.78125,0.5859375)session/icons/mappreview/" + getMapPreview(mapName);
    12601265
    12611266    // Hide/show settings depending on whether we can change them or not
    12621267    var updateDisplay = function(guiObjChg, guiObjDsp, chg) {
    12631268        guiObjChg.hidden = !chg;
    12641269        guiObjDsp.hidden = chg;
    12651270    };
  • binaries/data/mods/public/gui/gamesetup/gamesetup.xml

     
    11<?xml version="1.0" encoding="utf-8"?>
    22
    33<objects>
    44
    55    <script file="gui/common/functions_civinfo.js"/>
    66    <script file="gui/common/functions_global_object.js"/>
    77    <script file="gui/common/functions_utility.js"/>
    88    <script file="gui/common/network.js"/>
    99    <script file="gui/common/settings.js"/>
     10
     11    <!-- After settings.js, which defines g_Settings -->
    1012    <script file="gui/gamesetup/gamesetup.js"/>
     13
    1114    <!-- After gamesetup.js which defines g_VictoryConditions -->
    1215    <script directory="gui/gamesetup/victory_conditions/"/>
    1316
    1417    <!-- Add a translucent black background to fade out the menu page -->
    1518    <object type="image" style="ModernWindow" size="0 0 100% 100%">
    1619
    1720        <object style="TitleText" type="text" size="50%-128 4 50%+128 36">
    1821            <translatableAttribute id="caption">Match Setup</translatableAttribute>
    1922        </object>
     
    234237            <!-- Cancel Button -->
    235238            <object
    236239                name="cancelGame"
    237240                type="button"
    238241                style="StoneButton"
    239242                size="100%-308 100%-52 100%-168 100%-24"
    240243                tooltip_style="onscreenToolTip"
    241244            >
    242245                <translatableAttribute id="caption">Back</translatableAttribute>
    243                 <action on="Press">
    244                     <![CDATA[
    245                         cancelSetup();
    246                         if(!Engine.HasXmppClient())
    247                             Engine.SwitchGuiPage("page_pregame.xml");
    248                         else
    249                             Engine.SwitchGuiPage("page_lobby.xml");
    250                     ]]>
    251                 </action>
     246                <action on="Press">cancelSetup();</action>
    252247            </object>
    253248
    254249            <!-- Options -->
    255250            <object name="gameOptionsBox" size="100%-425 529 100%-25 525">
    256251                <!-- More Options Button -->
    257252                <object
    258253                    name="showMoreOptions"
    259254                    type="button"
    260255                    style="StoneButton"
  • binaries/data/mods/public/simulation/data/settings/Ceasefire.json

     
     1{
     2    "Default": 0,
     3    "Times": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 45, 60]
     4}