summaryrefslogtreecommitdiff
path: root/web/dartboat_wasm.c
diff options
context:
space:
mode:
Diffstat (limited to 'web/dartboat_wasm.c')
-rw-r--r--web/dartboat_wasm.c115
1 files changed, 66 insertions, 49 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());
}