From 33105a895a0b3c2b266c66058f5a8ec930c88504 Mon Sep 17 00:00:00 2001
From: notoraptor <notoraptor@users.noreply.github.com>
Date: Thu, 18 Jul 2019 14:48:29 -0400
Subject: Reset local orders for a power if we receive a power orders update.

- Does not allow to join a power which is eliminated.
- When clicking on "reset", reset local orders only for current selected power.
---
 diplomacy/web/src/gui/pages/content_game.jsx | 39 +++++++++++++++++++++-------
 1 file changed, 30 insertions(+), 9 deletions(-)

(limited to 'diplomacy/web/src/gui/pages/content_game.jsx')

diff --git a/diplomacy/web/src/gui/pages/content_game.jsx b/diplomacy/web/src/gui/pages/content_game.jsx
index 7e7c7e9..2941b6b 100644
--- a/diplomacy/web/src/gui/pages/content_game.jsx
+++ b/diplomacy/web/src/gui/pages/content_game.jsx
@@ -363,7 +363,7 @@ export class ContentGame extends React.Component {
             .catch(error => this.getPage().error('Error when updating possible orders: ' + error.toString()));
     }
 
-    notifiedLocalStateChange(networkGame) {
+    notifiedLocalStateChange(networkGame, notification) {
         networkGame.getAllPossibleOrders()
             .then(allPossibleOrders => {
                 networkGame.local.setPossibleOrders(allPossibleOrders);
@@ -373,6 +373,9 @@ export class ContentGame extends React.Component {
                         <ContentGame data={networkGame.local}/>,
                         {info: `Possible orders re-loaded.`}
                     );
+                    if (notification.power_name) {
+                        this.reloadPowerServerOrders(notification.power_name);
+                    }
                     this.reloadDeadlineTimer(networkGame);
                 }
             })
@@ -403,8 +406,8 @@ export class ContentGame extends React.Component {
             networkGame.addOnGamePhaseUpdate(this.notifiedGamePhaseUpdated);
             networkGame.addOnGameStatusUpdate(this.notifiedNetworkGame);
             networkGame.addOnOmniscientUpdated(this.notifiedNetworkGame);
-            networkGame.addOnPowerOrdersUpdate(this.notifiedNetworkGame);
-            networkGame.addOnPowerOrdersFlag(this.notifiedNetworkGame);
+            networkGame.addOnPowerOrdersUpdate(this.notifiedLocalStateChange);
+            networkGame.addOnPowerOrdersFlag(this.notifiedLocalStateChange);
             networkGame.addOnPowerVoteUpdated(this.notifiedNetworkGame);
             networkGame.addOnPowerWaitFlag(this.notifiedNetworkGame);
             networkGame.addOnVoteCountUpdated(this.notifiedNetworkGame);
@@ -530,13 +533,30 @@ export class ContentGame extends React.Component {
     }
 
     /**
-     * Reset local orders and replace them with current server orders.
+     * Reset local orders and replace them with current server orders for given power.
+     * @param {string} powerName - name of power to update
      */
-    reloadServerOrders() {
-        // TODO: This method should reset orders to server version only for current powers, not for all powers as she currently does.
+    reloadPowerServerOrders(powerName) {
         const serverOrders = this.props.data.getServerOrders();
-        this.__store_orders(serverOrders);
-        this.setState({orders: serverOrders});
+        const engine = this.props.data;
+        const allOrders = this.__get_orders(engine);
+        if (!allOrders.hasOwnProperty(powerName)) {
+            this.getPage().error(`Unknown power ${powerName}.`);
+            return;
+        }
+        allOrders[powerName] = serverOrders[powerName];
+        this.__store_orders(allOrders);
+        this.setState({orders: allOrders});
+    }
+
+    /**
+     * Reset local orders and replace them with current server orders for current selected power.
+     */
+    reloadServerOrders() {
+        const currentPowerName = this.getCurrentPowerName();
+        if (currentPowerName) {
+            this.reloadPowerServerOrders(currentPowerName);
+        }
     }
 
     /**
@@ -558,7 +578,8 @@ export class ContentGame extends React.Component {
     }
 
     /**
-     * Remove all local orders for current selected power
+     * Remove all local orders for current selected power, including empty orders set.
+     * Equivalent request is clearOrders().
      */
     onRemoveAllCurrentPowerOrders() {
         const currentPowerName = this.getCurrentPowerName();
-- 
cgit v1.2.3

