From 9aeba28ad5f7417aaae64b3a863a7dde388dffe5 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Mon, 25 Jan 2021 23:49:05 +0100
Subject: [PATCH] Better behavior in Dynamo. Some image attributions in rules

---
 client/src/translations/rules/Bario/en.pug  |  4 +-
 client/src/translations/rules/Bario/es.pug  |  4 +-
 client/src/translations/rules/Bario/fr.pug  |  4 +-
 client/src/translations/rules/Dynamo/en.pug |  4 +-
 client/src/translations/rules/Dynamo/es.pug |  4 +-
 client/src/translations/rules/Dynamo/fr.pug |  6 +-
 client/src/translations/rules/Emergo/en.pug |  1 +
 client/src/translations/rules/Emergo/es.pug |  1 +
 client/src/translations/rules/Emergo/fr.pug |  1 +
 client/src/variants/Dynamo.js               | 70 ++++++++++++++-------
 10 files changed, 68 insertions(+), 31 deletions(-)

diff --git a/client/src/translations/rules/Bario/en.pug b/client/src/translations/rules/Bario/en.pug
index fc93a66a..6c8508c6 100644
--- a/client/src/translations/rules/Bario/en.pug
+++ b/client/src/translations/rules/Bario/en.pug
@@ -13,7 +13,9 @@ p
 
 figure
   img.img-center(src="/variants/Bario/chessboard.png")
-  figcaption.text-center Undefined pieces on first ranks.
+  figcaption.text-center.
+    [From author's website with his permission]
+    Undefined pieces on first ranks.
 
 p.
   Queens, rooks, bishops and knights begin the game in a reserve below
diff --git a/client/src/translations/rules/Bario/es.pug b/client/src/translations/rules/Bario/es.pug
index 65334fcf..710979cb 100644
--- a/client/src/translations/rules/Bario/es.pug
+++ b/client/src/translations/rules/Bario/es.pug
@@ -13,7 +13,9 @@ p
 
 figure
   img.img-center(src="/variants/Bario/chessboard.png")
-  figcaption.text-center Piezas no definidas en las primeras filas.
+  figcaption.text-center.
+    [Del sitio del autor, con su permiso]
+    Piezas no definidas en las primeras filas.
 
 p.
   Damas, torres, alfiles y caballos comienzan el juego en una reserva bajo
diff --git a/client/src/translations/rules/Bario/fr.pug b/client/src/translations/rules/Bario/fr.pug
index bfae7c2b..f2d1896e 100644
--- a/client/src/translations/rules/Bario/fr.pug
+++ b/client/src/translations/rules/Bario/fr.pug
@@ -13,7 +13,9 @@ p
 
 figure
   img.img-center(src="/variants/Bario/chessboard.png")
-  figcaption.text-center Pièces indéfinies sur les premières rangées.
+  figcaption.text-center.
+    [Depuis le site de l'auteur, avec sa permission]
+    Pièces indéfinies sur les premières rangées.
 
 p.
   Dames, tours, fous et cavaliers commencent la partie dans une réserve sous
diff --git a/client/src/translations/rules/Dynamo/en.pug b/client/src/translations/rules/Dynamo/en.pug
index ac4263c8..874ae27e 100644
--- a/client/src/translations/rules/Dynamo/en.pug
+++ b/client/src/translations/rules/Dynamo/en.pug
@@ -38,8 +38,8 @@ p.
   bishop or the queen.
 
 p.
-  Note: if you don't want to play a second part in a move, just click on any
-  empty square: this will replace the second part by an empty move.
+  Note: if an action is possible but you don't want to play a second part in
+  a move, click on any empty square: this will send an empty move.
 
 figure.diagram-container
   .diagram.diag12
diff --git a/client/src/translations/rules/Dynamo/es.pug b/client/src/translations/rules/Dynamo/es.pug
index e7843ceb..b28e32a2 100644
--- a/client/src/translations/rules/Dynamo/es.pug
+++ b/client/src/translations/rules/Dynamo/es.pug
@@ -38,8 +38,8 @@ p.
   empujado afuera por el alfil o la dama.
 
 p.
-  Nota: si no desea ejecutar un segundo movimiento, haga clic simplemente en un
-  cuadrado vacío: esto reemplazará la segunda parte con un movimiento vacío.
+  Nota: si una acción es posible pero no desea ejecutar una segunda parte del
+  movimiento, haga clic en un cuadrado vacío: esto enviará una jugada vacía.
 
 figure.diagram-container
   .diagram.diag12
diff --git a/client/src/translations/rules/Dynamo/fr.pug b/client/src/translations/rules/Dynamo/fr.pug
index a31d514f..ea254bc3 100644
--- a/client/src/translations/rules/Dynamo/fr.pug
+++ b/client/src/translations/rules/Dynamo/fr.pug
@@ -38,9 +38,9 @@ p.
   poussé en dehors par le fou ou la dame.
 
 p.
-  Note : si vous ne voulez pas exécuter une seconde partie de coup, cliquez
-  simplement sur une case vide : cela remplacera la seconde partie par un coup
-  vide.
+  Note : si une action est possible mais vous ne voulez pas exécuter une
+  seconde partie de coup, cliquez sur une case vide :
+  cela enverra un coup vide.
 
 figure.diagram-container
   .diagram.diag12
diff --git a/client/src/translations/rules/Emergo/en.pug b/client/src/translations/rules/Emergo/en.pug
index 492ceef9..25397c0a 100644
--- a/client/src/translations/rules/Emergo/en.pug
+++ b/client/src/translations/rules/Emergo/en.pug
@@ -4,6 +4,7 @@ p.boxed
 figure.diagram-container
   img.img-center(src="/variants/Emergo/match_2004_U-con.jpg")
   figcaption.text-center.
+    [Wikipedia]
     Game played on an orthogonal depiction of a standard 9x9 Emergo board.
 
 p
diff --git a/client/src/translations/rules/Emergo/es.pug b/client/src/translations/rules/Emergo/es.pug
index 7388479b..37191bca 100644
--- a/client/src/translations/rules/Emergo/es.pug
+++ b/client/src/translations/rules/Emergo/es.pug
@@ -4,6 +4,7 @@ p.boxed
 figure.diagram-container
   img.img-center(src="/variants/Emergo/match_2004_U-con.jpg")
   figcaption.text-center.
+    [Wikipedia]
     Juego jugado en una representación ortogonal
     de un tablero de Emergo estándar 9x9.
 
diff --git a/client/src/translations/rules/Emergo/fr.pug b/client/src/translations/rules/Emergo/fr.pug
index 22f30f74..52f3d7fc 100644
--- a/client/src/translations/rules/Emergo/fr.pug
+++ b/client/src/translations/rules/Emergo/fr.pug
@@ -4,6 +4,7 @@ p.boxed
 figure.diagram-container
   img.img-center(src="/variants/Emergo/match_2004_U-con.jpg")
   figcaption.text-center.
+    [Wikipedia]
     Partie jouée sur une représentation orthogonale
     d'un plateau d'Emergo standard 9x9.
 
diff --git a/client/src/variants/Dynamo.js b/client/src/variants/Dynamo.js
index 35c41c58..9b29e3ca 100644
--- a/client/src/variants/Dynamo.js
+++ b/client/src/variants/Dynamo.js
@@ -739,6 +739,15 @@ export class DynamoRules extends ChessRules {
     return potentialMoves;
   }
 
+  getEmptyMove() {
+    return new Move({
+      start: { x: -1, y: -1 },
+      end: { x: -1, y: -1 },
+      appear: [],
+      vanish: []
+    });
+  }
+
   doClick(square) {
     // A click to promote a piece on subTurn 2 would trigger this.
     // For now it would then return [NaN, NaN] because surrounding squares
@@ -754,36 +763,55 @@ export class DynamoRules extends ChessRules {
       !this.underCheck(this.turn) &&
       (La == 0 || !this.oppositeMoves(this.amoves[La-1], this.firstMove[Lf-1]))
     ) {
-      return {
-        start: { x: -1, y: -1 },
-        end: { x: -1, y: -1 },
-        appear: [],
-        vanish: []
-      };
+      return this.getEmptyMove();
     }
     return null;
   }
 
   play(move) {
-    move.flags = JSON.stringify(this.aggregateFlags());
-    V.PlayOnBoard(this.board, move);
-    // NOTE; if subTurn == 1, there may be no available moves at subTurn == 2.
-    // However, it's quite easier to wait for a user click.
-    if (this.subTurn == 2) {
+    const color = this.turn;
+    move.subTurn = this.subTurn; //for undo
+    const gotoNext = (mv) => {
       const L = this.firstMove.length;
-      this.amoves.push(this.getAmove(this.firstMove[L-1], move));
-      this.turn = V.GetOppCol(this.turn);
+      this.amoves.push(this.getAmove(this.firstMove[L-1], mv));
+      this.turn = V.GetOppCol(color);
+      this.subTurn = 1;
       this.movesCount++;
+    };
+    move.flags = JSON.stringify(this.aggregateFlags());
+    V.PlayOnBoard(this.board, move);
+    if (this.subTurn == 2) gotoNext(move);
+    else {
+      this.subTurn = 2;
+      this.firstMove.push(move);
+      this.toNewKingPos(move);
+      if (
+        // Condition is true on empty arrays:
+        this.getAllPotentialMoves().every(m => {
+          V.PlayOnBoard(this.board, m);
+          this.toNewKingPos(m);
+          const res = this.underCheck(color);
+          V.UndoOnBoard(this.board, m);
+          this.toOldKingPos(m);
+          return res;
+        })
+      ) {
+        // No valid move at subTurn 2
+        gotoNext(this.getEmptyMove());
+      }
+      this.toOldKingPos(move);
     }
-    else this.firstMove.push(move);
-    this.subTurn = 3 - this.subTurn;
     this.postPlay(move);
   }
 
-  postPlay(move) {
-    if (move.start.x < 0) return;
+  toNewKingPos(move) {
     for (let a of move.appear)
       if (a.p == V.KING) this.kingPos[a.c] = [a.x, a.y];
+  }
+
+  postPlay(move) {
+    if (move.start.x < 0) return;
+    this.toNewKingPos(move);
     this.updateCastleFlags(move);
   }
 
@@ -806,12 +834,12 @@ export class DynamoRules extends ChessRules {
       this.turn = V.GetOppCol(this.turn);
       this.movesCount--;
     }
-    else this.firstMove.pop();
-    this.subTurn = 3 - this.subTurn;
-    this.postUndo(move);
+    if (move.subTurn == 1) this.firstMove.pop();
+    this.subTurn = move.subTurn;
+    this.toOldKingPos(move);
   }
 
-  postUndo(move) {
+  toOldKingPos(move) {
     // (Potentially) Reset king position
     for (let v of move.vanish)
       if (v.p == V.KING) this.kingPos[v.c] = [v.x, v.y];
-- 
2.44.0