From 5c42c64e0b43ad4d687c58a6b7e9b9ac5d212e17 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Wed, 28 Nov 2018 17:28:13 +0100
Subject: [PATCH] Fixed Crazyhouse; still have to keep track of promoted pawns

---
 TODO                                      |  1 +
 public/javascripts/components/game.js     | 73 +++++++++++++++++------
 public/javascripts/variants/Crazyhouse.js | 33 ++++++++--
 views/rules/Crazyhouse.pug                |  2 +
 4 files changed, 85 insertions(+), 24 deletions(-)

diff --git a/TODO b/TODO
index fe7d8473..58906a83 100644
--- a/TODO
+++ b/TODO
@@ -4,3 +4,4 @@ setInterval "CRON" task in sockets.js to check connected clients
 (every 1hour maybe, or more)
 Systematically show init+dest squares in PGN, maybe after short notation
 (2 moves list, second for de-ambiguification)
+Crazyhouse: keep track of promoted pawns.
diff --git a/public/javascripts/components/game.js b/public/javascripts/components/game.js
index 2897b1cd..f5bef8ef 100644
--- a/public/javascripts/components/game.js
+++ b/public/javascripts/components/game.js
@@ -240,38 +240,75 @@ Vue.component('my-game', {
 				);
 			}
 			elementArray.push(gameDiv);
-			if (!!this.vr.reserve) //TODO: table, show counts for reserve pieces
-				//<tr style="padding:0">
-				//    <td style="padding:0;font-size:10px">3</td>
+			if (!!this.vr.reserve)
 			{
-				let reservePiecesArray = [];
+				let myReservePiecesArray = [];
 				for (let i=0; i<VariantRules.RESERVE_PIECES.length; i++)
 				{
-					reservePiecesArray.push(h('img',
+					myReservePiecesArray.push(h('div',
+					{
+						'class': {'board':true, ['board'+sizeY]:true},
+						attrs: { id: this.getSquareId({x:sizeX,y:i}) }
+					},
+					[
+						h('img',
 						{
 							'class': {"piece":true},
 							attrs: {
 								"src": "/images/pieces/" +
 									this.vr.getReservePpath(this.mycolor,i) + ".svg",
-								id: this.getSquareId({x:sizeX,y:i}),
 							}
-						})
-					);
+						}),
+						h('sup',
+							{style: { "padding-left":"40%"} },
+							[ this.vr.reserve[this.mycolor][VariantRules.RESERVE_PIECES[i]] ]
+						)
+					]));
 				}
-				let reserve = h('div',
-					{'class':{'game':true}}, [
+				let oppReservePiecesArray = [];
+				const oppCol = this.vr.getOppCol(this.mycolor);
+				for (let i=0; i<VariantRules.RESERVE_PIECES.length; i++)
+				{
+					oppReservePiecesArray.push(h('div',
+					{
+						'class': {'board':true, ['board'+sizeY]:true},
+						attrs: { id: this.getSquareId({x:sizeX,y:i}) }
+					},
+					[
+						h('img',
+						{
+							'class': {"piece":true},
+							attrs: {
+								"src": "/images/pieces/" +
+									this.vr.getReservePpath(oppCol,i) + ".svg",
+							}
+						}),
+						h('sup',
+							{style: { "padding-left":"40%"} },
+							[ this.vr.reserve[oppCol][VariantRules.RESERVE_PIECES[i]] ]
+						)
+					]));
+				}
+				let reserves = h('div',
+					{
+						'class':{'game':true},
+						style: {"margin-bottom": "20px"},
+					},
+					[
+						h('div',
+							{
+								'class': { 'row': true },
+								style: {"margin-bottom": "15px"},
+							},
+							myReservePiecesArray
+						),
 						h('div',
 							{ 'class': { 'row': true }},
-							[
-								h('div',
-									{'class':{'board':true, ['board'+sizeY]:true}},
-									reservePiecesArray
-								)
-							]
+							oppReservePiecesArray
 						)
-					],
+					]
 				);
-				elementArray.push(reserve);
+				elementArray.push(reserves);
 			}
 			const eogMessage = this.getEndgameMessage(this.score);
 			const modalEog = [
diff --git a/public/javascripts/variants/Crazyhouse.js b/public/javascripts/variants/Crazyhouse.js
index 9cb07685..9558fe4b 100644
--- a/public/javascripts/variants/Crazyhouse.js
+++ b/public/javascripts/variants/Crazyhouse.js
@@ -31,6 +31,22 @@ class CrazyhouseRules extends ChessRules
 			else if (m.vanish.length == 0)
 				this.reserve[m.appear[0].c][m.appear[0].p]--;
 		});
+		// TODO: keep track of promoted pawns ==> give a pawn if captured.
+	}
+
+	getColor(i,j)
+	{
+		const sizeX = VariantRules.size[0];
+		if (i >= sizeX)
+			return (i==sizeX ? "w" : "b");
+		return this.board[i][j].charAt(0);
+	}
+	getPiece(i,j)
+	{
+		const sizeX = VariantRules.size[0];
+		if (i >= sizeX)
+			return VariantRules.RESERVE_PIECES[j];
+		return this.board[i][j].charAt(1);
 	}
 
 	// Used by the interface:
@@ -52,11 +68,13 @@ class CrazyhouseRules extends ChessRules
 		if (this.reserve[color][p] == 0)
 			return [];
 		let moves = [];
-		for (let i=0; i<sizeX; i++)
+		const [sizeX,sizeY] = VariantRules.size;
+		const pawnShift = (p==VariantRules.PAWN ? 1 : 0);
+		for (let i=pawnShift; i<sizeX-pawnShift; i++)
 		{
 			for (let j=0; j<sizeY; j++)
 			{
-				if (this.board[i][j] != VariantRules.EMPTY)
+				if (this.board[i][j] == VariantRules.EMPTY)
 				{
 					let mv = new Move({
 						appear: [
@@ -66,7 +84,10 @@ class CrazyhouseRules extends ChessRules
 								c: color,
 								p: p
 							})
-						]
+						],
+						vanish: [],
+						start: {x:sizeX, y:y}, //a bit artificial...
+						end: {x:i, y:j}
 					});
 					moves.push(mv);
 				}
@@ -116,7 +137,7 @@ class CrazyhouseRules extends ChessRules
 		super.updateVariables(move);
 		const color = this.turn;
 		if (move.vanish.length==2)
-			this.reserve[color][move.appear[0].p]++;
+			this.reserve[color][move.vanish[1].p]++;
 		if (move.vanish.length==0)
 			this.reserve[color][move.appear[0].p]--;
 	}
@@ -126,7 +147,7 @@ class CrazyhouseRules extends ChessRules
 		super.unupdateVariables(move);
 		const color = this.turn;
 		if (move.vanish.length==2)
-			this.reserve[color][move.appear[0].p]--;
+			this.reserve[color][move.vanish[1].p]--;
 		if (move.vanish.length==0)
 			this.reserve[color][move.appear[0].p]++;
 	}
@@ -139,7 +160,7 @@ class CrazyhouseRules extends ChessRules
 			return super.getNotation(move);
 		// Rebirth:
 		const piece =
-			(move.appear[0].p != VariantRules.PAWN ? move.appear.p.toUpperCase() : "");
+			(move.appear[0].p != VariantRules.PAWN ? move.appear[0].p.toUpperCase() : "");
 		const finalSquare =
 			String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x);
 		return piece + "@" + finalSquare;
diff --git a/views/rules/Crazyhouse.pug b/views/rules/Crazyhouse.pug
index 3928c309..5566bee8 100644
--- a/views/rules/Crazyhouse.pug
+++ b/views/rules/Crazyhouse.pug
@@ -25,6 +25,8 @@ p.
 	not the promoted piece. This is to allow to gain material on last rank without
 	fear of giving a queen to the opponent.
 
+p.warn This detail is not implemented yet (will be very soon).
+
 h3 Credits
 
 p
-- 
2.44.0