summaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
Diffstat (limited to 'web')
-rw-r--r--web/dartboat_wasm.c34
-rw-r--r--web/static/dartboat.js48
2 files changed, 28 insertions, 54 deletions
diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c
index 19d37db..5a043d1 100644
--- a/web/dartboat_wasm.c
+++ b/web/dartboat_wasm.c
@@ -8,6 +8,9 @@
#include <emscripten/emscripten.h>
+#define P_USER 1
+#define P_BOAT 2
+
int delay_ms = 1000;
// TODO refactor *everything*
@@ -20,12 +23,12 @@ struct match_state {
struct match_state *state;
void set_user_active() {
- EM_ASM(setUserActive());
+ EM_ASM({setPlayerActive($0)}, P_USER);
EM_ASM({setPromptHandler($0)}, "visit");
}
void set_boat_active() {
- EM_ASM(setBoatActive());
+ EM_ASM({setPlayerActive($0)}, P_BOAT);
EM_ASM({setPromptHandler()}, "");
}
@@ -54,11 +57,7 @@ void suggested(int rem, char *buf) {
EMSCRIPTEN_KEEPALIVE void get_suggested(int rem, int player) {
static char buf[20];
suggested(rem, buf);
-
- if (player == 1)
- EM_ASM({userSugg($0)}, buf);
- else
- EM_ASM({boatSugg($0)}, buf);
+ EM_ASM({updatePlayerSugg($0, $1)}, player, buf);
}
EMSCRIPTEN_KEEPALIVE int is_match_over() {
@@ -70,7 +69,7 @@ EMSCRIPTEN_KEEPALIVE int is_match_over() {
void match_over() {
EM_ASM({setPromptHandler($0)}, "init");
EM_ASM({promptSugg($0)}, "Press OK to play again.");
- EM_ASM(setNoneActive());
+ EM_ASM(setPlayerActive());
}
EMSCRIPTEN_KEEPALIVE void draw_match() {
@@ -138,10 +137,10 @@ char *str_for_rem_state(int rem) {
EMSCRIPTEN_KEEPALIVE void update_user_rem(int rem) {
char *str = str_for_rem_state(rem);
- EM_ASM({updateUserStateRem($0)}, str);
+ EM_ASM({updatePlayerRem($0, $1)}, P_USER, str);
free(str);
- get_suggested(rem, 1);
+ get_suggested(rem, P_USER);
}
EMSCRIPTEN_KEEPALIVE void update_user_rem_from_pts(int pts) {
@@ -150,10 +149,10 @@ EMSCRIPTEN_KEEPALIVE void update_user_rem_from_pts(int pts) {
EMSCRIPTEN_KEEPALIVE void update_boat_rem(int rem) {
char *str = str_for_rem_state(rem);
- EM_ASM({updateBoatStateRem($0)}, str);
+ EM_ASM({updatePlayerRem($0, $1)}, P_BOAT, str);
free(str);
- get_suggested(rem, 2);
+ get_suggested(rem, P_BOAT);
}
void end_boat_throwing(int, int); // FIXME
@@ -218,7 +217,8 @@ EMSCRIPTEN_KEEPALIVE bool user_visit(int points) {
update_user_rem(l->rem);
if (v->rem > 0)
- EM_ASM({updateUserAvg($0)}, (double)(l->start-l->rem)/l->n_visits);
+ EM_ASM({updatePlayerAvg($0, $1)}, P_USER,
+ (double)(l->start-l->rem)/l->n_visits);
EM_ASM({clearMatchLog()});
draw_match();
@@ -249,7 +249,7 @@ EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(int pts, char *str) {
EMSCRIPTEN_KEEPALIVE void end_boat_throwing(int rem, int avg) {
update_boat_rem(rem);
- EM_ASM({updateBoatAvg($0)}, avg);
+ EM_ASM({updatePlayerAvg($0, $1)}, P_BOAT, avg);
EM_ASM({setPromptText($0)}, "");
EM_ASM(clearMatchLog());
EM_ASM({promptSugg($0)}, "");
@@ -267,7 +267,7 @@ EMSCRIPTEN_KEEPALIVE void resp_numdarts(int n_darts) {
struct leg *l = state->l1;
- EM_ASM({updateUserAvg($0)},
+ EM_ASM({updatePlayerAvg($0, $1)}, P_USER,
((double)l->start / (((l->n_visits - 1) * 3) + n_darts)) * 3);
EM_ASM({promptMsg($0)}, "You win! :)");
match_over();
@@ -302,8 +302,8 @@ EMSCRIPTEN_KEEPALIVE void start_match() {
state->l1 = leg_init(501, "User");
state->l2 = leg_init(501, "Bot");
- EM_ASM({updateUserName($0)}, state->l1->name);
- EM_ASM({updateBoatName($0)}, state->l2->name);
+ EM_ASM({updatePlayerName($0, $1)}, P_USER, state->l1->name);
+ EM_ASM({updatePlayerName($0, $1)}, P_BOAT, state->l2->name);
update_user_rem(state->l1->rem);
update_boat_rem(state->l2->rem);
diff --git a/web/static/dartboat.js b/web/static/dartboat.js
index 0014d3a..a47b845 100644
--- a/web/static/dartboat.js
+++ b/web/static/dartboat.js
@@ -127,36 +127,25 @@ function clearOi() {
}
}
-function updateUserStateRem(ptr) {
- document.getElementById('p1-rem').textContent = UTF8ToString(ptr);
+function setPlayerActive(n) {
+ document.querySelectorAll('[id$=-state]').forEach(e =>
+ e.classList[n && e.id == `p${n}-state` ? 'add' : 'remove']('active'));
}
-function updateBoatStateRem(ptr) {
- document.getElementById('p2-rem').textContent = UTF8ToString(ptr);
+function updatePlayerRem(n, ptr) {
+ document.getElementById(`p${n}-rem`).textContent = UTF8ToString(ptr);
}
-function updateUserName(p) {
- document.getElementById('p1-name').textContent = UTF8ToString(p);
+function updatePlayerName(n, ptr) {
+ document.getElementById(`p${n}-name`).textContent = UTF8ToString(ptr);
}
-function updateBoatName(p) {
- document.getElementById('p2-name').textContent = UTF8ToString(p);
+function updatePlayerSugg(n, ptr) {
+ document.getElementById(`p${n}-sugg`).textContent = UTF8ToString(ptr);
}
-function userSugg(p) {
- document.getElementById('p1-sugg').textContent = UTF8ToString(p);
-}
-
-function boatSugg(p) {
- document.getElementById('p2-sugg').textContent = UTF8ToString(p);
-}
-
-function updateUserAvg(avg) {
- document.getElementById('p1-avg').textContent = avg.toFixed(2);
-}
-
-function updateBoatAvg(avg) {
- document.getElementById('p2-avg').textContent = avg.toFixed(2);
+function updatePlayerAvg(n, avg) {
+ document.getElementById(`p${n}-avg`).textContent = avg.toFixed(2);
}
function setPromptText(ptr) {
@@ -175,21 +164,6 @@ function promptUpdateRem() {
Module.ccall('update_user_rem_from_pts', null, ['number'], [_promptGet()]);
}
-function setUserActive() {
- document.getElementById('p2-state').classList.remove('active');
- document.getElementById('p1-state').classList.add('active');
-}
-
-function setBoatActive() {
- document.getElementById('p1-state').classList.remove('active');
- document.getElementById('p2-state').classList.add('active');
-}
-
-function setNoneActive() {
- document.getElementById('p1-state').classList.remove('active');
- document.getElementById('p2-state').classList.remove('active');
-}
-
function scheduleCCall(f, ms, ...args) {
let types = [];
let vals = [];