summaryrefslogtreecommitdiff
path: root/web/dartboat_wasm.c
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-04-21 19:54:05 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-04-21 19:55:54 -0700
commit24b71a541f73bffc4ba65ad479b441df2913ef0a (patch)
tree79e2a33410e810a8dbcf186dd5f7a99366a1e8c0 /web/dartboat_wasm.c
parent77cea41e4bf9cc067150aaf5eed45f1613098d96 (diff)
downloaddartboat-24b71a541f73bffc4ba65ad479b441df2913ef0a.tar.gz
dartboat-24b71a541f73bffc4ba65ad479b441df2913ef0a.tar.xz
rewrite some JS web logic in C
Diffstat (limited to 'web/dartboat_wasm.c')
-rw-r--r--web/dartboat_wasm.c88
1 files changed, 56 insertions, 32 deletions
diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c
index b2c306c..65888b2 100644
--- a/web/dartboat_wasm.c
+++ b/web/dartboat_wasm.c
@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <stdio.h>
+#include <string.h>
#include <time.h>
#include <emscripten/emscripten.h>
@@ -75,36 +76,6 @@ EMSCRIPTEN_KEEPALIVE int is_match_over(struct match_state *state) {
return 0;
}
-EMSCRIPTEN_KEEPALIVE void boat_visit(struct match_state *state) {
- if (state->l1->rem <= 0 || state->l2->rem <= 0)
- return;
-
- struct leg *l = state->l2;
- comp_visit(l);
-
- struct visit *v = l->visits + l->n_visits - 1;
-
- char *s1 = segment_name(v->darts[0]),
- *s2 = v->n_darts > 1 ? segment_name(v->darts[1]) : NULL,
- *s3 = v->n_darts > 2 ? segment_name(v->darts[2]) : NULL;
-
- double avg;
- if (v->rem > 0)
- avg = (double)(l->start - l->rem) / l->n_visits;
- else
- avg = ((double)l->start / (((l->n_visits - 1) * 3) + v->n_darts)) * 3;
-
- EM_ASM({boatVisitRes($0, $1, $2, $3, $4, $5, $6, $7, $8)},
- l->rem, v->n_darts, segment_points(v->darts[0]),
- v->n_darts > 1 ? segment_points(v->darts[1]) : 0,
- v->n_darts > 2 ? segment_points(v->darts[2]) : 0,
- s1, s2, s3, avg);
-
- free(s1);
- free(s2);
- free(s3);
-}
-
EMSCRIPTEN_KEEPALIVE void draw_match(struct match_state *state) {
struct leg *l1 = state->l1;
struct leg *l2 = state->l2;
@@ -148,8 +119,6 @@ EMSCRIPTEN_KEEPALIVE void draw_match(struct match_state *state) {
if (l1->rem <= 0) {
EM_ASM(promptNumDarts());
- //EM_ASM({promptMsg($0)}, "You win! :)");
- //EM_ASM(matchOver());
} else if (l2->rem <= 0) {
EM_ASM({promptMsg($0)}, "Bot wins. :(");
EM_ASM(matchOver());
@@ -158,6 +127,61 @@ EMSCRIPTEN_KEEPALIVE void draw_match(struct match_state *state) {
}
}
+EMSCRIPTEN_KEEPALIVE void boat_visit(struct match_state *state, int delay_ms) {
+ if (state->l1->rem <= 0 || state->l2->rem <= 0)
+ return;
+
+ EM_ASM({promptMsg($0)}, "Bot is throwing…");
+
+ struct leg *l = state->l2;
+ comp_visit(l);
+
+ struct visit *v = l->visits + l->n_visits - 1;
+
+ double avg;
+ if (v->rem > 0)
+ avg = (double)(l->start - l->rem) / l->n_visits;
+ else
+ avg = ((double)l->start / (((l->n_visits - 1) * 3) + v->n_darts)) * 3;
+
+ if (!delay_ms) {
+ EM_ASM({updateBoatRem($0)}, l->rem);
+ EM_ASM({updateBoatAvg($0)}, avg);
+ EM_ASM({clearMatchLog()});
+ draw_match(state);
+ if (!is_match_over(state))
+ EM_ASM(setUserActive());
+ return;
+ }
+
+ int pts = segment_points(v->darts[0]);
+ char str[20];
+ char *seg_name = segment_name(v->darts[0]);
+ strcpy(str, seg_name);
+ free(seg_name);
+
+ EM_ASM({scheduleBoatThrowing($0, $1, $2)}, pts, str, delay_ms);
+
+ if (v->n_darts > 1) {
+ pts += segment_points(v->darts[1]);
+ seg_name = segment_name(v->darts[1]);
+ strcat(strcat(str, "-"), seg_name);
+ free(seg_name);
+ EM_ASM({scheduleBoatThrowing($0, $1, $2)}, pts, str, delay_ms * 2);
+ }
+
+ if (v->n_darts > 2) {
+ pts += segment_points(v->darts[2]);
+ seg_name = segment_name(v->darts[2]);
+ strcat(strcat(str, "-"), seg_name);
+ free(seg_name);
+ EM_ASM({scheduleBoatThrowing($0, $1, $2)}, pts, str, delay_ms * 3);
+ }
+
+ EM_ASM({scheduleEndBoatThrowing($0, $1, $2)},
+ 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;