summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--web/dartboat_wasm.c115
-rw-r--r--web/static/dartboat.js40
2 files changed, 79 insertions, 76 deletions
diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c
index c648a29..7dbfc59 100644
--- a/web/dartboat_wasm.c
+++ b/web/dartboat_wasm.c
@@ -8,6 +8,8 @@
#include <emscripten/emscripten.h>
+int delay_ms = 1000;
+
// TODO refactor *everything*
struct match_state {
@@ -15,6 +17,8 @@ struct match_state {
int complete;
};
+struct match_state *state;
+
void set_user_active() {
EM_ASM(setUserActive());
EM_ASM(setPromptHandlerVisit());
@@ -57,46 +61,19 @@ EMSCRIPTEN_KEEPALIVE void get_suggested(int rem, int player) {
EM_ASM({boatSugg($0)}, buf);
}
-EMSCRIPTEN_KEEPALIVE int is_match_over(struct match_state *state) {
+EMSCRIPTEN_KEEPALIVE int is_match_over() {
if (state->l1->rem <= 0 || state->l2->rem <= 0)
return 1;
return 0;
}
-EMSCRIPTEN_KEEPALIVE bool user_visit(struct match_state *state, int points) {
- if (state->l1->rem <= 0 || state->l2->rem <= 0 ||
- points > state->l1->rem || state->l1->rem - points == 1 ||
- points > 180 || points == 179 || points == 178 || points == 176 ||
- points == 175 || points == 173 || points == 172 || points == 169 ||
- points == 166 || points == 163) {
-
- EM_ASM(oi());
- return false;
- }
-
- struct leg *l = state->l1;
- struct visit *v = l->visits + l->n_visits++;
- v->points = points;
- l->rem -= points;
- v->rem = l->rem;
- EM_ASM({updateUserRem($0)}, l->rem);
-
- if (v->rem > 0)
- EM_ASM({updateUserAvg($0)}, (double)(l->start-l->rem)/l->n_visits);
-
- if (!is_match_over(state))
- set_boat_active();
-
- return true;
-}
-
void match_over() {
EM_ASM(setPromptHandlerInit());
EM_ASM({promptSugg($0)}, "Press OK to play again.");
EM_ASM(setNoOneActive());
}
-EMSCRIPTEN_KEEPALIVE void draw_match(struct match_state *state) {
+EMSCRIPTEN_KEEPALIVE void draw_match() {
struct leg *l1 = state->l1;
struct leg *l2 = state->l2;
@@ -148,7 +125,7 @@ EMSCRIPTEN_KEEPALIVE void draw_match(struct match_state *state) {
}
}
-EMSCRIPTEN_KEEPALIVE void boat_visit(struct match_state *state, int delay_ms) {
+EMSCRIPTEN_KEEPALIVE void boat_visit() {
if (state->l1->rem <= 0 || state->l2->rem <= 0)
return;
@@ -169,8 +146,8 @@ EMSCRIPTEN_KEEPALIVE void boat_visit(struct match_state *state, int delay_ms) {
EM_ASM({updateBoatStateRem($0)}, l->rem);
EM_ASM({updateBoatAvg($0)}, avg);
EM_ASM({clearMatchLog()});
- draw_match(state);
- if (!is_match_over(state))
+ draw_match();
+ if (!is_match_over())
set_user_active();
return;
}
@@ -203,28 +180,62 @@ EMSCRIPTEN_KEEPALIVE void boat_visit(struct match_state *state, int delay_ms) {
l->rem, avg, delay_ms * (v->n_darts + 1));
}
-EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(struct match_state *state, int pts, char *str) {
+EMSCRIPTEN_KEEPALIVE bool user_visit(int points) {
+ if (state->l1->rem <= 0 || state->l2->rem <= 0 ||
+ points > state->l1->rem || state->l1->rem - points == 1 ||
+ points > 180 || points == 179 || points == 178 || points == 176 ||
+ points == 175 || points == 173 || points == 172 || points == 169 ||
+ points == 166 || points == 163) {
+
+ EM_ASM(oi());
+ return false;
+ }
+
+ struct leg *l = state->l1;
+ struct visit *v = l->visits + l->n_visits++;
+ v->points = points;
+ l->rem -= points;
+ v->rem = l->rem;
+ EM_ASM({updateUserRem($0)}, l->rem);
+
+ if (v->rem > 0)
+ EM_ASM({updateUserAvg($0)}, (double)(l->start-l->rem)/l->n_visits);
+
+ EM_ASM({clearMatchLog()});
+ draw_match();
+
+ if (!is_match_over()) {
+ set_boat_active();
+ boat_visit();
+ }
+
+ return true;
+}
+
+EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(int pts, char *str) {
char buf[10];
sprintf(buf, "%d", pts);
- EM_ASM({updateBoatStateRem($0)}, state->l2->rem - pts);
+ int rem = state->l2->n_visits > 1 ? state->l2->visits[state->l2->n_visits-2].rem : state->l2->start;
+
+ EM_ASM({updateBoatStateRem($0)}, rem - pts);
EM_ASM({setPromptText($0)}, buf);
EM_ASM({promptSugg($0)}, str);
}
-EMSCRIPTEN_KEEPALIVE void end_boat_throwing(struct match_state *state, int rem, int avg) {
+EMSCRIPTEN_KEEPALIVE void end_boat_throwing(int rem, int avg) {
EM_ASM({updateBoatStateRem($0)}, rem);
EM_ASM({updateBoatAvg($0)}, avg);
EM_ASM({setPromptText($0)}, "");
EM_ASM(clearMatchLog());
EM_ASM({promptSugg($0)}, "");
- draw_match(state);
+ draw_match();
- if (!is_match_over(state))
+ if (!is_match_over())
set_user_active();
}
-EMSCRIPTEN_KEEPALIVE void resp_numdarts(struct match_state *state, int n_darts) {
+EMSCRIPTEN_KEEPALIVE void resp_numdarts(int n_darts) {
if (n_darts < 1 || n_darts > 3) {
EM_ASM(oi());
return;
@@ -249,11 +260,21 @@ void init_boat() {
init_board();
EM_ASM({updateStdev($0)}, horizontal_stdev);
+ EM_ASM({updateDelay($0)}, delay_ms);
}
-EMSCRIPTEN_KEEPALIVE struct match_state *start_match() {
+EMSCRIPTEN_KEEPALIVE void free_match() {
+ leg_free(state->l1);
+ leg_free(state->l2);
+ free(state);
+ state = NULL;
+}
+
+EMSCRIPTEN_KEEPALIVE void start_match() {
+ if (state) free_match();
+
init_boat();
- struct match_state *state = calloc(1, sizeof(struct match_state));
+ state = calloc(1, sizeof(struct match_state));
state->l1 = leg_init(501, "User");
state->l2 = leg_init(501, "Bot");
@@ -263,15 +284,7 @@ EMSCRIPTEN_KEEPALIVE struct match_state *start_match() {
EM_ASM(clearMatchLog());
set_user_active();
EM_ASM({promptSugg($0)}, "");
- draw_match(state);
-
- return state;
-}
-
-EMSCRIPTEN_KEEPALIVE void free_match(struct match_state *state) {
- leg_free(state->l1);
- leg_free(state->l2);
- free(state);
+ draw_match();
}
EMSCRIPTEN_KEEPALIVE void change_stdev(float hstdev, float vstdev) {
@@ -279,6 +292,10 @@ EMSCRIPTEN_KEEPALIVE void change_stdev(float hstdev, float vstdev) {
vertical_stdev = vstdev;
}
+EMSCRIPTEN_KEEPALIVE void change_delay(int delay) {
+ delay_ms = delay;
+}
+
int main() {
EM_ASM(initMatch());
}
diff --git a/web/static/dartboat.js b/web/static/dartboat.js
index 2e9429c..662fd3f 100644
--- a/web/static/dartboat.js
+++ b/web/static/dartboat.js
@@ -1,16 +1,9 @@
-let match_state, user_rem;
+let user_rem;
let oi_timeout;
let prompt_handler;
-let delay_ms = 1000;
const POINT_CLASSES = [180, 140, 100, 60, 40, 20, 1, 0];
-function stcall(f, ret_type, arg_types, args) {
- return Module.ccall(f, ret_type,
- arg_types ? ['number'].concat(arg_types) : ['number'],
- args ? [match_state].concat(args) : [match_state]);
-}
-
let prompt_handler_visit = {
append(val) {
let elem = document.getElementById('prompt');
@@ -40,14 +33,7 @@ let prompt_handler_visit = {
if (pts_is_rem)
pts = user_rem - pts;
- if (!stcall('user_visit', 'number', ['number'], [pts]))
- return;
-
- clearMatchLog();
- stcall('draw_match');
-
- if (!stcall('is_match_over', 'number'))
- stcall('boat_visit', 'number', ['number'], [delay_ms]);
+ Module.ccall('user_visit', 'number', ['number'], [pts]);
},
submit_rem() {
@@ -77,7 +63,7 @@ let prompt_handler_num_darts = {
if (!val) return;
promptClear();
- stcall('resp_numdarts', null, ['number'], [val]);
+ Module.ccall('resp_numdarts', null, ['number'], [val]);
}
}
@@ -133,10 +119,7 @@ function clearMatchLog() {
}
function initMatch() {
- if (match_state)
- stcall('free_match');
- //updateDelay(delay_ms);
- match_state = stcall('start_match', 'number');
+ Module.ccall('start_match');
}
function oi() {
@@ -224,14 +207,17 @@ function setPromptText(ptr) {
function scheduleBoatThrowing(pts, ptr, delay_ms) {
// ptr to c string must be copied before returning
let str = UTF8ToString(ptr);
- setTimeout(function() {
- stcall('draw_boat_throwing', null, ['number', 'string'], [pts, str]) },
- delay_ms);
+ setTimeout(
+ function() { Module.ccall('draw_boat_throwing', null,
+ ['number', 'string'], [pts, str]) },
+ delay_ms);
}
function scheduleEndBoatThrowing(rem, avg, delay_ms) {
- setTimeout(function() {
- stcall('end_boat_throwing', null, ['number', 'number'], [rem, avg]) }, delay_ms);
+ setTimeout(
+ function() { Module.ccall('end_boat_throwing', null,
+ ['number', 'number'], [rem, avg]) },
+ delay_ms);
}
function drawVisit(visit_no, u_pts, u_rem, b_pts, b_rem, b_darts) {
@@ -261,7 +247,7 @@ function updateStdev(val) {
}
function delayChanged(val) {
- delay_ms = val;
+ Module.ccall('change_delay', null, ['number'], [val]);
}
function updateDelay(val) {