summaryrefslogtreecommitdiff
path: root/web/dartboat_wasm.c
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-04-22 00:48:52 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-04-22 00:48:52 -0700
commit95a3eded5c9d3863363e2af9d1966e1892ead129 (patch)
treeb38caba5ea3b4f6809707d88a80f659bccfc979c /web/dartboat_wasm.c
parent4d94b22fa44367e682159ad922c53c3bb7f60930 (diff)
downloaddartboat-95a3eded5c9d3863363e2af9d1966e1892ead129.tar.gz
dartboat-95a3eded5c9d3863363e2af9d1966e1892ead129.tar.xz
transfer more web ui logic to c code
Diffstat (limited to 'web/dartboat_wasm.c')
-rw-r--r--web/dartboat_wasm.c86
1 files changed, 67 insertions, 19 deletions
diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c
index 65888b2..c648a29 100644
--- a/web/dartboat_wasm.c
+++ b/web/dartboat_wasm.c
@@ -15,6 +15,16 @@ struct match_state {
int complete;
};
+void set_user_active() {
+ EM_ASM(setUserActive());
+ EM_ASM(setPromptHandlerVisit());
+}
+
+void set_boat_active() {
+ EM_ASM(setBoatActive());
+ EM_ASM(setPromptHandlerNull());
+}
+
void suggested(int rem, char *buf) {
buf[0] = 0;
@@ -47,15 +57,22 @@ EMSCRIPTEN_KEEPALIVE void get_suggested(int rem, int player) {
EM_ASM({boatSugg($0)}, buf);
}
-EMSCRIPTEN_KEEPALIVE bool user_visit(struct match_state *state, int points) {
+EMSCRIPTEN_KEEPALIVE int is_match_over(struct match_state *state) {
if (state->l1->rem <= 0 || state->l2->rem <= 0)
- return false;
+ return 1;
+ return 0;
+}
- if (points > state->l1->rem || state->l1->rem - points == 1 ||
+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)
+ points == 166 || points == 163) {
+
+ EM_ASM(oi());
return false;
+ }
struct leg *l = state->l1;
struct visit *v = l->visits + l->n_visits++;
@@ -67,13 +84,16 @@ EMSCRIPTEN_KEEPALIVE bool user_visit(struct match_state *state, int points) {
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;
}
-EMSCRIPTEN_KEEPALIVE int is_match_over(struct match_state *state) {
- if (state->l1->rem <= 0 || state->l2->rem <= 0)
- return 1;
- return 0;
+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) {
@@ -118,10 +138,11 @@ EMSCRIPTEN_KEEPALIVE void draw_match(struct match_state *state) {
}
if (l1->rem <= 0) {
- EM_ASM(promptNumDarts());
+ EM_ASM({promptMsg($0)}, "Darts needed?");
+ EM_ASM(setPromptHandlerNumDarts());
} else if (l2->rem <= 0) {
EM_ASM({promptMsg($0)}, "Bot wins. :(");
- EM_ASM(matchOver());
+ match_over();
} else {
EM_ASM({promptMsg($0)}, "Enter points:");
}
@@ -145,12 +166,12 @@ EMSCRIPTEN_KEEPALIVE void boat_visit(struct match_state *state, int delay_ms) {
avg = ((double)l->start / (((l->n_visits - 1) * 3) + v->n_darts)) * 3;
if (!delay_ms) {
- EM_ASM({updateBoatRem($0)}, l->rem);
+ EM_ASM({updateBoatStateRem($0)}, l->rem);
EM_ASM({updateBoatAvg($0)}, avg);
EM_ASM({clearMatchLog()});
draw_match(state);
if (!is_match_over(state))
- EM_ASM(setUserActive());
+ set_user_active();
return;
}
@@ -182,18 +203,40 @@ EMSCRIPTEN_KEEPALIVE void boat_visit(struct match_state *state, int delay_ms) {
l->rem, avg, delay_ms * (v->n_darts + 1));
}
-EMSCRIPTEN_KEEPALIVE int resp_numdarts(struct match_state *state, int n_darts) {
- if (n_darts < 1 || n_darts > 4)
- return 0;
+EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(struct match_state *state, int pts, char *str) {
+ char buf[10];
+ sprintf(buf, "%d", pts);
+
+ EM_ASM({updateBoatStateRem($0)}, state->l2->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) {
+ EM_ASM({updateBoatStateRem($0)}, rem);
+ EM_ASM({updateBoatAvg($0)}, avg);
+ EM_ASM({setPromptText($0)}, "");
+ EM_ASM(clearMatchLog());
+ EM_ASM({promptSugg($0)}, "");
+ draw_match(state);
+
+ if (!is_match_over(state))
+ set_user_active();
+}
+
+EMSCRIPTEN_KEEPALIVE void resp_numdarts(struct match_state *state, int n_darts) {
+ if (n_darts < 1 || n_darts > 3) {
+ EM_ASM(oi());
+ return;
+ }
struct leg *l = state->l1;
EM_ASM({updateUserAvg($0)},
((double)l->start / (((l->n_visits - 1) * 3) + n_darts)) * 3);
EM_ASM({promptMsg($0)}, "You win! :)");
- EM_ASM(matchOver());
-
- return 1;
+ match_over();
+ EM_ASM(setPromptHandlerInit());
}
void init_boat() {
@@ -215,7 +258,12 @@ EMSCRIPTEN_KEEPALIVE struct match_state *start_match() {
state->l2 = leg_init(501, "Bot");
EM_ASM({updateUserRem($0)}, state->l1->rem);
- EM_ASM({updateBoatRem($0)}, state->l2->rem);
+ EM_ASM({updateBoatStateRem($0)}, state->l2->rem);
+
+ EM_ASM(clearMatchLog());
+ set_user_active();
+ EM_ASM({promptSugg($0)}, "");
+ draw_match(state);
return state;
}