aboutsummaryrefslogtreecommitdiff
path: root/diplomacy/web/src/gui/utils/load_game_from_disk.js
diff options
context:
space:
mode:
authornotoraptor <notoraptor@users.noreply.github.com>2019-07-17 15:16:43 -0400
committerPhilip Paquette <pcpaquette@gmail.com>2019-07-17 15:16:43 -0400
commit2701df1e3b03c7c605ccf212a02987d53fbd0609 (patch)
treed3637573d8585e32914c33cbd03ec0baf9c68ae3 /diplomacy/web/src/gui/utils/load_game_from_disk.js
parente9872eea32d4f66b9c7ca8c14d530c18f6c18506 (diff)
[web] Make button "Delete all" remove only orders from current se… (#49)
- Make button "Delete all" remove only orders from current selected power. - Reorganize code - [web] Remove bugged and useless function gameReloaded() from game page. - This function caused error `engine.getPhaseType is not a function` for games with deadlines. - Move function saveGameToDisk into its own file. - [web] Add documentation to methods involved in orders management to help understand what happens. - Move methods getServerOrders() from game GUI component to game engine object. - Rename method onSetNoOrders to onSetEmptyOrdersSet. - Rename property in PowerActionsForm: onNoOrders => onPass. - [web] Update sending orders to send request clearOrders when local orders list is null. - Renamed local file: - components/power_order => power_orders - forms/power_actions_form => power_order_creation_form - Move power orders buttons bar to a separate file: - components/power_orders_actions_bar - [web] Improve messages about local/server defined orders.
Diffstat (limited to 'diplomacy/web/src/gui/utils/load_game_from_disk.js')
-rw-r--r--diplomacy/web/src/gui/utils/load_game_from_disk.js83
1 files changed, 83 insertions, 0 deletions
diff --git a/diplomacy/web/src/gui/utils/load_game_from_disk.js b/diplomacy/web/src/gui/utils/load_game_from_disk.js
new file mode 100644
index 0000000..ca49aa0
--- /dev/null
+++ b/diplomacy/web/src/gui/utils/load_game_from_disk.js
@@ -0,0 +1,83 @@
+import $ from "jquery";
+import {STRINGS} from "../../diplomacy/utils/strings";
+import {Game} from "../../diplomacy/engine/game";
+
+export function loadGameFromDisk(onLoad, onError) {
+ const input = $(document.createElement('input'));
+ input.attr("type", "file");
+ input.trigger('click');
+ input.change(event => {
+ const file = event.target.files[0];
+ if (!file.name.match(/\.json$/i)) {
+ onError(`Invalid JSON filename ${file.name}`);
+ return;
+ }
+ const reader = new FileReader();
+ reader.onload = () => {
+ const savedData = JSON.parse(reader.result);
+ const gameObject = {};
+ gameObject.game_id = `(local) ${savedData.id}`;
+ gameObject.map_name = savedData.map;
+ gameObject.rules = savedData.rules;
+ gameObject.state_history = {};
+ gameObject.message_history = {};
+ gameObject.order_history = {};
+ gameObject.result_history = {};
+
+ // Load all saved phases (expect the latest one) to history fields.
+ for (let i = 0; i < savedData.phases.length - 1; ++i) {
+ const savedPhase = savedData.phases[i];
+ const gameState = savedPhase.state;
+ const phaseOrders = savedPhase.orders || {};
+ const phaseResults = savedPhase.results || {};
+ const phaseMessages = {};
+ if (savedPhase.messages) {
+ for (let message of savedPhase.messages) {
+ phaseMessages[message.time_sent] = message;
+ }
+ }
+ if (!gameState.name)
+ gameState.name = savedPhase.name;
+ gameObject.state_history[gameState.name] = gameState;
+ gameObject.message_history[gameState.name] = phaseMessages;
+ gameObject.order_history[gameState.name] = phaseOrders;
+ gameObject.result_history[gameState.name] = phaseResults;
+ }
+
+ // Load latest phase separately and use it later to define the current game phase.
+ const latestPhase = savedData.phases[savedData.phases.length - 1];
+ const latestGameState = latestPhase.state;
+ const latestPhaseOrders = latestPhase.orders || {};
+ const latestPhaseResults = latestPhase.results || {};
+ const latestPhaseMessages = {};
+ if (latestPhase.messages) {
+ for (let message of latestPhase.messages) {
+ latestPhaseMessages[message.time_sent] = message;
+ }
+ }
+ if (!latestGameState.name)
+ latestGameState.name = latestPhase.name;
+ // TODO: NB: What is latest phase in loaded JSON contains order results? Not sure if it is well handled.
+ gameObject.result_history[latestGameState.name] = latestPhaseResults;
+
+ gameObject.messages = [];
+ gameObject.role = STRINGS.OBSERVER_TYPE;
+ gameObject.status = STRINGS.COMPLETED;
+ gameObject.timestamp_created = 0;
+ gameObject.deadline = 0;
+ gameObject.n_controls = 0;
+ gameObject.registration_password = '';
+ const game = new Game(gameObject);
+
+ // Set game current phase and state using latest phase found in JSON file.
+ game.setPhaseData({
+ name: latestGameState.name,
+ state: latestGameState,
+ orders: latestPhaseOrders,
+ messages: latestPhaseMessages
+ });
+ onLoad(game);
+ };
+ reader.readAsText(file);
+ });
+}