From 3a3f1d129896fad749cf802b41e72d8fc05f0cb8 Mon Sep 17 00:00:00 2001
From: David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
Date: Tue, 26 Apr 2022 17:23:52 -0700
Subject: web: support quick rematch, fix checkout points

---
 web/dartboat_wasm.c    | 60 +++++++++++++++++++++++++++++++++++++++++---------
 web/static/dartboat.js | 49 +++++++++++++++++++++++++----------------
 web/static/index.html  |  6 ++---
 web/static/style.css   |  4 +---
 4 files changed, 83 insertions(+), 36 deletions(-)

diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c
index a083780..f8a076d 100644
--- a/web/dartboat_wasm.c
+++ b/web/dartboat_wasm.c
@@ -59,9 +59,13 @@ void set_active_player(int pn)
 
 	if (state->mode == M_PVC && pn == 2) {
 		EM_ASM({promptMsgL($0)}, "Bot is throwing…");
+		EM_ASM({setKeyLabelSubmit($0)}, "OK");
+		EM_ASM({setKeyLabelRem($0)}, "REMAINING");
 		set_prompt_mode(PM_NONE);
 	} else {
 		EM_ASM({promptMsgL($0)}, "Enter points:");
+		EM_ASM({setKeyLabelSubmit($0)}, "OK");
+		EM_ASM({setKeyLabelRem($0)}, "REMAINING");
 		set_prompt_mode(PM_VISIT);
 	}
 	EM_ASM({promptMsgR($0)}, "");
@@ -231,6 +235,8 @@ void prompt_num_darts()
 	set_prompt_mode(PM_NUM_DARTS);
 	EM_ASM({promptMsgL($0)}, "Darts needed?");
 	EM_ASM({promptMsgR($0)}, "");
+	EM_ASM({setKeyLabelSubmit($0)}, "OK");
+	EM_ASM({setKeyLabelRem($0)}, "REMAINING");
 }
 
 void prompt_end_match()
@@ -240,10 +246,12 @@ void prompt_end_match()
 	EM_ASM({promptMsgL($0)},
 		state->mode == M_PVC && state->legs[1]->rem <= 0 ? "Bot wins. :(" :
 			"You win! :)");
-	EM_ASM({promptMsgR($0)}, "Press OK to end match.");
+	EM_ASM({promptMsgR($0)}, "");
+	EM_ASM({setKeyLabelSubmit($0)}, "END MATCH");
+	EM_ASM({setKeyLabelRem($0)}, "REMATCH");
 }
 
-EMSCRIPTEN_KEEPALIVE void prompt_select_mode()
+void prompt_select_mode()
 {
 	for (int pn = 1; pn < 3; ++pn) {
 		EM_ASM({hidePlayerInfo($0)}, pn);
@@ -299,7 +307,7 @@ EMSCRIPTEN_KEEPALIVE void user_visit(int points)
 		points == 175 || points == 173 || points == 172 || points == 169 ||
 		points == 166 || points == 163 ||
 		(state->active_leg->rem - points == 0 &&
-			(points == 168 || points == 165 || points == 162 ||
+			(points > 170 || points == 168 || points == 165 || points == 162 ||
 				points == 159))) {
 		EM_ASM(oi());
 		return;
@@ -405,7 +413,7 @@ EMSCRIPTEN_KEEPALIVE void start_match(int mode)
 		return;
 	}
 
-	if (state) free_state(); // this should never happen
+	if (state) free_state(); // rematch gets us here
 	state = calloc(1, sizeof(*state));
 	state->mode = mode;
 	state->legs[0] = leg_init(501, mode == M_PVP ? "Player 1" : "User");
@@ -454,6 +462,21 @@ void init_boat()
 	EM_ASM({updateStdev($0)}, horizontal_stdev);
 }
 
+bool is_key_active(char *k)
+{
+	return (EM_ASM_INT({return isKeyActive($0)}, k));
+}
+
+void toggle_key(char *k)
+{
+	EM_ASM({toggleKey($0)}, k);
+}
+
+void deactivate_key(char *k)
+{
+	EM_ASM({deactivateKey($0)}, k);
+}
+
 char *prompt_get()
 {
 	return (char *)EM_ASM_INT({return promptGet()});
@@ -468,7 +491,13 @@ void prompt_handle_pre(char *action)
 
 	if ((pm == PM_VISIT || pm == PM_NUM_DARTS || pm == PM_END_MATCH) &&
 		strcmp(action, "undo"))
-		EM_ASM(deactivateUndo());
+		deactivate_key("undo");
+
+	if (pm == PM_END_MATCH && strcmp(action, "rem"))
+		deactivate_key("rem");
+
+	if (pm == PM_END_MATCH && strcmp(action, "submit"))
+		deactivate_key("submit");
 }
 
 void prompt_handle_on_change()
@@ -525,7 +554,9 @@ void prompt_handle_clear()
 void prompt_handle_submit()
 {
 	if (pm == PM_END_MATCH) {
-		end_match();
+		if (is_key_active("submit"))
+			end_match();
+		toggle_key("submit");
 		return;
 	}
 
@@ -546,8 +577,15 @@ void prompt_handle_submit()
 	free(str);
 }
 
-void prompt_handle_submit_rem()
+void prompt_handle_rem()
 {
+	if (pm == PM_END_MATCH) {
+		if (is_key_active("rem"))
+			start_match(state->mode);
+		toggle_key("rem");
+		return;
+	}
+
 	if (pm != PM_VISIT)
 		return;
 
@@ -564,9 +602,9 @@ void prompt_handle_undo()
 		return;
 
 	prompt_handle_clear();
-	if (EM_ASM_INT({return isUndoActive()}))
+	if (is_key_active("undo"))
 		user_undo();
-	EM_ASM(toggleUndo());
+	toggle_key("undo");
 }
 
 EMSCRIPTEN_KEEPALIVE void prompt_handle(char *action, char *data)
@@ -581,8 +619,8 @@ EMSCRIPTEN_KEEPALIVE void prompt_handle(char *action, char *data)
 		prompt_handle_clear();
 	else if (!strcmp(action, "submit"))
 		prompt_handle_submit();
-	else if (!strcmp(action, "submit_rem"))
-		prompt_handle_submit_rem();
+	else if (!strcmp(action, "rem"))
+		prompt_handle_rem();
 	else if (!strcmp(action, "undo"))
 		prompt_handle_undo();
 }
diff --git a/web/static/dartboat.js b/web/static/dartboat.js
index f0855c2..da59e59 100644
--- a/web/static/dartboat.js
+++ b/web/static/dartboat.js
@@ -38,24 +38,28 @@ function promptClear() {
 	document.getElementById('prompt-input').textContent = '';
 }
 
-function isUndoActive() {
-	return document.getElementById('key_undo').classList.contains('active');
+function setPromptActive() {
+	document.getElementById('prompt').classList.add('active')
 }
 
-function toggleUndo() {
-	document.getElementById('key_undo').classList.toggle('active');
+function setPromptInactive() {
+	document.getElementById('prompt').classList.remove('active')
 }
 
-function deactivateUndo() {
-	document.getElementById('key_undo').classList.remove('active');
+function setPromptInput(ptr) {
+	document.getElementById('prompt-input').textContent = UTF8ToString(ptr);
 }
 
-function setPromptActive() {
-	document.getElementById('prompt').classList.add('active')
+function promptMsgL(p) {
+	document.getElementById('prompt-msg-l').textContent = UTF8ToString(p);
 }
 
-function setPromptInactive() {
-	document.getElementById('prompt').classList.remove('active')
+function promptMsgR(p) {
+	document.getElementById('prompt-msg-r').textContent = UTF8ToString(p);
+}
+
+function promptHandle(action, data) {
+	Module.ccall('prompt_handle', null, ['string', 'string'], [action, data]);
 }
 
 function setKeypad(keypad) {
@@ -65,20 +69,27 @@ function setKeypad(keypad) {
 	});
 }
 
-function promptHandle(action, data) {
-	Module.ccall('prompt_handle', null, ['string', 'string'], [action, data]);
+function isKeyActive(k) {
+	return document.getElementById(`key-${UTF8ToString(k)}`).classList
+		.contains('active');
 }
 
-function setPromptInput(ptr) {
-	document.getElementById('prompt-input').textContent = UTF8ToString(ptr);
+function toggleKey(k) {
+	document.getElementById(`key-${UTF8ToString(k)}`).classList
+		.toggle('active');
 }
 
-function promptMsgL(p) {
-	document.getElementById('prompt-msg-l').textContent = UTF8ToString(p);
+function deactivateKey(k) {
+	document.getElementById(`key-${UTF8ToString(k)}`).classList
+		.remove('active');
 }
 
-function promptMsgR(p) {
-	document.getElementById('prompt-msg-r').textContent = UTF8ToString(p);
+function setKeyLabelSubmit(ptr) {
+	document.getElementById('key-submit').textContent = UTF8ToString(ptr);
+}
+
+function setKeyLabelRem(ptr) {
+	document.getElementById('key-rem').textContent = UTF8ToString(ptr);
 }
 
 function setPlayerActive(n) {
@@ -186,7 +197,7 @@ document.addEventListener('keydown', e => {
 	else if (e.key == 'c')
 		promptHandle('clear');
 	else if (e.key == 'r')
-		promptHandle('submit_rem');
+		promptHandle('rem');
 	else if (e.key == 'u')
 		promptHandle('undo');
 });
diff --git a/web/static/index.html b/web/static/index.html
index f986ee9..7fda38c 100644
--- a/web/static/index.html
+++ b/web/static/index.html
@@ -43,9 +43,9 @@
 					<div onclick="promptHandle('append', '9')" class="key num">9</div>
 					<div onclick="promptHandle('clear')" class="key">CLEAR</div>
 					<div onclick="promptHandle('append', '0')" class="key num">0</div>
-					<div onclick="promptHandle('submit')" class="key ok">OK</div>
-					<div onclick="promptHandle('undo')" class="key" id="key_undo">UNDO</div>
-					<div onclick="promptHandle('submit_rem')" class="key">REMAINING</div>
+					<div onclick="promptHandle('submit')" class="key" id="key-submit">OK</div>
+					<div onclick="promptHandle('undo')" class="key" id="key-undo">UNDO</div>
+					<div onclick="promptHandle('rem')" class="key" id="key-rem">REMAINING</div>
 				</div>
 				<div id="keypad-select_mode" class="keypad">
 					<div onclick="promptHandle('append', '1'); promptHandle('submit')" class="key"><span class="keyboard-val">[1]</span> Play against bot</div>
diff --git a/web/static/style.css b/web/static/style.css
index 475f726..e22c4ce 100644
--- a/web/static/style.css
+++ b/web/static/style.css
@@ -222,10 +222,8 @@ div.key.num {
 	font-weight: 700;
 }
 
-div.key.ok {
+div.key#key-submit {
 	grid-row-end: span 2;
-
-	font-size: 2.5em;
 }
 
 div#settings-bar {
-- 
cgit v1.2.3-70-g09d2