From 27a6d311f49e4c1ae9415f4dc4b79dfcb690c80d Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Mon, 25 Jul 2022 11:08:50 +0200
Subject: [PATCH] Progress on Go game

---
 base_rules.js            |  3 +--
 variants/Weiqi/class.js  | 52 +++++++++++++++++++++++++++++++++-------
 variants/Weiqi/style.css | 27 ++++++++++++++++++++-
 3 files changed, 71 insertions(+), 11 deletions(-)

diff --git a/base_rules.js b/base_rules.js
index 4627eb7..4ee389f 100644
--- a/base_rules.js
+++ b/base_rules.js
@@ -1013,11 +1013,10 @@ export default class ChessRules {
     // TODO: onpointerdown/move/up ? See reveal.js /controllers/touch.js
   }
 
+  // NOTE: not called if isDiagram, or genFenOnly
   removeListeners() {
     let container = document.getElementById(this.containerId);
     this.windowResizeObs.unobserve(container);
-    if (this.isDiagram)
-      return; //no listeners in this case
     if ('onmousedown' in window) {
       this.mouseListeners.forEach(ml => {
         document.removeEventListener(ml.type, ml.listener);
diff --git a/variants/Weiqi/class.js b/variants/Weiqi/class.js
index e528719..ce8531f 100644
--- a/variants/Weiqi/class.js
+++ b/variants/Weiqi/class.js
@@ -1,8 +1,3 @@
-//TODO:
-// - pass btn on top + message if opponent just passed
-// - do not count points: rely on players' ability to do that
-// - implement Ko rule (need something in fen: lastMove)
-
 import ChessRules from "/base_rules.js";
 import Move from "/utils/Move.js";
 import PiPo from "/utils/PiPo.js";
@@ -10,7 +5,6 @@ import {ArrayFun} from "/utils/array.js";
 
 export default class WeiqiRules extends ChessRules {
 
-  // TODO: option oneColor (just alter pieces class of white stones)
   static get Options() {
     return {
       input: [
@@ -19,6 +13,12 @@ export default class WeiqiRules extends ChessRules {
           variable: "bsize",
           type: "number",
           defaut: 9
+        },
+        {
+          label: "One color",
+          variable: "onecolor",
+          type: "checkbox",
+          defaut: false
         }
       ]
     };
@@ -82,15 +82,40 @@ export default class WeiqiRules extends ChessRules {
   genRandInitBaseFen() {
     const fenLine = C.FenEmptySquares(this.size.y);
     return {
-      fen: (fenLine + '/').repeat(this.size.x - 1) + fenLine + " w 0",
+      fen: (fenLine + '/').repeat(this.size.x - 1) + fenLine,
       o: {}
     };
   }
 
+  constructor(o) {
+    super(o);
+    if (!o.genFenOnly && !o.diagram) {
+      
+      this.passListener = () => this.play({pass: true}); //TODO: wrong, need to use buildMoveStack (warning empty move...)
+
+      // Show pass btn
+      let passBtn = document.createElement("button");
+      C.AddClass_es(passBtn, "pass-btn");
+      passBtn.innerHTML = "pass";
+      passBtn.addEventListener("click", this.passListener);
+      let container = document.getElementById(this.containerId);
+      container.appendChild(passBtn);
+    }
+  }
+
+  removeListeners() {
+    super.removeListeners();
+    let passBtn = document.getElementsByClassName("pass-btn")[0];
+    passBtn.removeEventListener("click", this.passListener);
+  }
+
   pieces(color, x, y) {
+    let classe_s = ["stone"];
+    if (this.options["onecolor"] && color == 'w')
+      classe_s.push("one-color");
     return {
       's': {
-        "class": "stone",
+        "class": classe_s,
         moves: []
       }
     };
@@ -162,6 +187,17 @@ export default class WeiqiRules extends ChessRules {
     return res;
   }
 
+  play(move) {
+    if (move.pass) {
+      if (this.turn != this.playerColor)
+        super.displayMessage(null, "pass", "pass-text", 2000);
+      else
+        this.turn = C.GetOppCol(this.turn);
+    }
+    else
+      super.play(move);
+  }
+
   filterValid(moves) {
     // Suicide check not here, because side-computation of captures
     return moves;
diff --git a/variants/Weiqi/style.css b/variants/Weiqi/style.css
index daf96e4..54b9a40 100644
--- a/variants/Weiqi/style.css
+++ b/variants/Weiqi/style.css
@@ -5,6 +5,31 @@
 piece.white.stone {
   background-image: url('/variants/Weiqi/pieces/black_stone.svg');
 }
-piece.black.stone {
+piece.black.stone, piece.white.stone.one-color {
   background-image: url('/variants/Weiqi/pieces/white_stone.svg');
 }
+
+button.pass-btn {
+  display: block;
+  position: relative;
+  margin: 0 auto;
+}
+
+/* TODO: copy-paste from Chakart */
+div.pass-text {
+  position: relative;
+  margin-top: 15px;
+  width: 100%;
+  text-align: center;
+  background-color: transparent;
+  color: darkred;
+  font-weight: bold;
+  font-size: 2em;
+  animation: blinker 0.5s linear infinite;
+}
+
+@keyframes blinker {
+  50% {
+    opacity: 0;
+  }
+}
-- 
2.44.0