Index: binaries/data/mods/public/gamesettings/GameSettings.js =================================================================== --- /dev/null +++ binaries/data/mods/public/gamesettings/GameSettings.js @@ -0,0 +1,13 @@ +/** + * Data store for game settings. + * Serializes to/from JSON. + */ +class GameSettings +{ + constructor() + { + this.mapExploration = new MapExploration(); + } +} + +var g_NewGameSettings = new GameSettings(); Index: binaries/data/mods/public/gamesettings/Observable.js =================================================================== --- /dev/null +++ binaries/data/mods/public/gamesettings/Observable.js @@ -0,0 +1,33 @@ +class Observable +{ + constructor() + { + Object.defineProperty(this, "observed", { + "value": {}, + "enumerable": false, + }); + return new Proxy(this, { + "set": (target, key, value) => { + let old; + if (Reflect.has(this, key)) + old = Reflect.get(this, key); + Reflect.set(this, key, value); + if (old === undefined || old !== value) + if (this.observed[key]) + for (let watcher of this.observed[key]) + watcher(key); + return true; + } + }); + } + + watch(watcher, props) + { + for (let prop of props) + { + if (!this.observed[prop]) + this.observed[prop] = []; + this.observed[prop].push(watcher); + } + } +} Index: binaries/data/mods/public/gamesettings/attributes/Cheats.js =================================================================== --- /dev/null +++ binaries/data/mods/public/gamesettings/attributes/Cheats.js @@ -0,0 +1,52 @@ +class Cheats +{ + constructor() + { + this.enabled = g_IsNetworked; + } + + Serialize() + { + this.handler = undefined; + this.gameSettingsControl = undefined; + return this; + } + + Deserialize(data) + { + this.enabled = data.enabled; + } + + guiHandler(handler, gameSettingsControl) + { + this.handler = handler; + + // As a temporary crutch, this is a reference to the old settings system. + // It's set by the GUI. + this.gameSettingsControl = gameSettingsControl; + this.gameSettingsControl.registerGameAttributesBatchChangeHandler(() => this.update()); + // Force a refresh on the next call. + this.enabled = undefined; + } + + update() + { + this.set(this.enabled); + } + + set(enabled, sendMessage = false) + { + let old = this.enabled; + this.enabled = !g_IsNetworked || + enabled && !g_GameAttributes.settings.RatingEnabled; + if (old == this.enabled) + return; + + this.handler.render(); + + g_GameAttributes.settings.CheatsEnabled = this.enabled; + this.gameSettingsControl.updateGameAttributes(); + if (sendMessage) + this.gameSettingsControl.setNetworkGameAttributes(); + } +} Index: binaries/data/mods/public/gamesettings/attributes/MapExploration.js =================================================================== --- /dev/null +++ binaries/data/mods/public/gamesettings/attributes/MapExploration.js @@ -0,0 +1,60 @@ +class MapExploration extends Observable +{ + constructor() + { + super(); + this.explored = false; + this.revealed = false; + + this.watch(() => this.updateLegacy(), ["explored", "revealed"]); + } + + Serialize() + { + return { "explored": this.explored, "revealed": this.revealed }; + } + + Deserialize(data) + { + // TODO + } + + setControl(gameSettingsControl) + { + // As a temporary crutch, this is a reference to the old settings system. + // It's set by the GUI. + this.gameSettingsControl = gameSettingsControl; + this.gameSettingsControl.registerMapChangeHandler((data) => this.onMapChange(data)); + } + + onMapChange(mapData) + { + if (!mapData || !mapData.settings) + return; + if (mapData.settings.RevealMap !== undefined) + this.setExplored(mapData.settings.RevealMap); + else if (mapData.settings.ExploreMap !== undefined) + this.setExplored(mapData.settings.ExploreMap); + } + + setExplored(enabled) + { + this.explored = enabled; + this.revealed = this.revealed && this.explored; + this.gameSettingsControl.setNetworkGameAttributes(); + } + + setRevealed(enabled) + { + this.explored = this.explored || enabled; + this.revealed = enabled; + this.gameSettingsControl.setNetworkGameAttributes(); + } + + updateLegacy() + { + g_GameAttributes.settings.RevealMap = this.revealed; + g_GameAttributes.settings.ExploreMap = this.explored; + this.gameSettingsControl.updateGameAttributes(); + } +} Index: binaries/data/mods/public/gamesettings/attributes/Rating.js =================================================================== --- /dev/null +++ binaries/data/mods/public/gamesettings/attributes/Rating.js @@ -0,0 +1,55 @@ +class Rating extends Observable +{ + constructor() + { + super(); + this.hasXmppClient = Engine.HasXmppClient(); + this.available = false; + this.enabled = false; + + this.watch(() => this.updateLegacy(), ["enabled"]); + } + + Serialize() + { + this.gameSettingsControl = undefined; + return { "enabled": this.enabled }; + } + + Deserialize(data) + { + this.enabled = data.enabled; + } + + setControl(gameSettingsControl) + { + // As a temporary crutch, this is a reference to the old settings system. + // It's set by the GUI. + this.gameSettingsControl = gameSettingsControl; + this.gameSettingsControl.registerGameAttributesBatchChangeHandler(() => this.update()); + } + + onFinalize() + { + if (this.hasXmppClient) + Engine.SetRankedGame(this.enabled); + } + + update() + { + this.available = this.hasXmppClient && g_GameAttributes.settings.PlayerData.length == 2; + this.enabled = this.available && this.enabled; + } + + updateLegacy() + { + g_GameAttributes.settings.RatingEnabled = this.enabled; + this.gameSettingsControl.updateGameAttributes(); + } + + set(enabled) + { + this.enabled = enabled; + this.gameSettingsControl.setNetworkGameAttributes(); + } +} Index: binaries/data/mods/public/gui/gamesetup/Controls/StartGameControl.js =================================================================== --- binaries/data/mods/public/gui/gamesetup/Controls/StartGameControl.js +++ binaries/data/mods/public/gui/gamesetup/Controls/StartGameControl.js @@ -38,6 +38,9 @@ Engine.StartNetworkGame(); else { + // TODO: use serialization and clean all this up. + for (let prop in g_NewGameSettings) + g_GameAttributes.settings[prop] = g_NewGameSettings[prop].Serialize(); Engine.StartGame(g_GameAttributes, g_PlayerAssignments.local.player); this.switchToLoadingPage(); } Index: binaries/data/mods/public/gui/gamesetup/Pages/GameSetupPage/GameSettings/Single/Checkboxes/ExploredMap.js =================================================================== --- binaries/data/mods/public/gui/gamesetup/Pages/GameSetupPage/GameSettings/Single/Checkboxes/ExploredMap.js +++ binaries/data/mods/public/gui/gamesetup/Pages/GameSetupPage/GameSettings/Single/Checkboxes/ExploredMap.js @@ -1,52 +1,21 @@ GameSettingControls.ExploredMap = class extends GameSettingControlCheckbox { - onMapChange(mapData) + constructor(...args) { - let mapValue; - if (mapData && - mapData.settings && - mapData.settings.ExploreMap !== undefined) - mapValue = mapData.settings.ExploreMap; - - if (mapValue !== undefined && mapValue != g_GameAttributes.settings.ExploreMap) - { - g_GameAttributes.settings.ExploreMap = mapValue; - this.gameSettingsControl.updateGameAttributes(); - } - } - - onGameAttributesChange() - { - if (!g_GameAttributes.mapType) - return; - - if (g_GameAttributes.settings.ExploreMap === undefined) - { - g_GameAttributes.settings.ExploreMap = !!g_GameAttributes.settings.RevealMap; - this.gameSettingsControl.updateGameAttributes(); - } - else if (g_GameAttributes.settings.RevealMap && - !g_GameAttributes.settings.ExploreMap) - { - g_GameAttributes.settings.ExploreMap = true; - this.gameSettingsControl.updateGameAttributes(); - } + super(...args); + g_NewGameSettings.mapExploration.setControl(this.gameSettingsControl); + g_NewGameSettings.mapExploration.watch(() => this.render(), ["explored"]); + this.render(); } - onGameAttributesBatchChange() + render() { - if (!g_GameAttributes.mapType) - return; - - this.setChecked(g_GameAttributes.settings.ExploreMap); - this.setEnabled(g_GameAttributes.mapType != "scenario" && !g_GameAttributes.settings.RevealMap); + this.setChecked(g_NewGameSettings.mapExploration.explored); } onPress(checked) { - g_GameAttributes.settings.ExploreMap = checked; - this.gameSettingsControl.updateGameAttributes(); - this.gameSettingsControl.setNetworkGameAttributes(); + g_NewGameSettings.mapExploration.setExplored(checked); } }; Index: binaries/data/mods/public/gui/gamesetup/Pages/GameSetupPage/GameSettings/Single/Checkboxes/RevealedMap.js =================================================================== --- binaries/data/mods/public/gui/gamesetup/Pages/GameSetupPage/GameSettings/Single/Checkboxes/RevealedMap.js +++ binaries/data/mods/public/gui/gamesetup/Pages/GameSetupPage/GameSettings/Single/Checkboxes/RevealedMap.js @@ -1,47 +1,20 @@ GameSettingControls.RevealedMap = class extends GameSettingControlCheckbox { - onMapChange(mapData) + constructor(...args) { - let mapValue; - if (mapData && - mapData.settings && - mapData.settings.RevealMap !== undefined) - mapValue = mapData.settings.RevealMap; - - if (mapValue !== undefined && mapValue != g_GameAttributes.settings.RevealMap) - { - g_GameAttributes.settings.RevealMap = mapValue; - this.gameSettingsControl.updateGameAttributes(); - } - - this.setEnabled(g_GameAttributes.mapType != "scenario"); + super(...args); + g_NewGameSettings.mapExploration.watch(() => this.render(), ["revealed"]); + this.render(); } - onGameAttributesChange() + render() { - if (!g_GameAttributes.mapType) - return; - - if (g_GameAttributes.settings.RevealMap === undefined) - { - g_GameAttributes.settings.RevealMap = false; - this.gameSettingsControl.updateGameAttributes(); - } - } - - onGameAttributesBatchChange() - { - if (!g_GameAttributes.mapType) - return; - - this.setChecked(g_GameAttributes.settings.RevealMap); + this.setChecked(g_NewGameSettings.mapExploration.revealed); } onPress(checked) { - g_GameAttributes.settings.RevealMap = checked; - this.gameSettingsControl.updateGameAttributes(); - this.gameSettingsControl.setNetworkGameAttributes(); + g_NewGameSettings.mapExploration.setRevealed(checked); } }; Index: binaries/data/mods/public/gui/gamesetup/gamesetup.xml =================================================================== --- binaries/data/mods/public/gui/gamesetup/gamesetup.xml +++ binaries/data/mods/public/gui/gamesetup/gamesetup.xml @@ -6,6 +6,10 @@