summaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-04-22 22:22:57 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-04-22 22:22:57 -0700
commit07f0616f88553e0a4b3a5c23b0024e78d94d9d7e (patch)
tree9883976c9a2f370c53b98a6dcd456b98df05dc1e /web
parente8592a80976919681d92a0d6ff89bae6d5fb992f (diff)
downloaddartboat-07f0616f88553e0a4b3a5c23b0024e78d94d9d7e.tar.gz
dartboat-07f0616f88553e0a4b3a5c23b0024e78d94d9d7e.tar.xz
implement generic ccall scheduler in web js
Diffstat (limited to 'web')
-rw-r--r--web/dartboat_wasm.c45
-rw-r--r--web/static/dartboat.js29
2 files changed, 28 insertions, 46 deletions
diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c
index e8cde21..19d37db 100644
--- a/web/dartboat_wasm.c
+++ b/web/dartboat_wasm.c
@@ -156,6 +156,7 @@ EMSCRIPTEN_KEEPALIVE void update_boat_rem(int rem) {
get_suggested(rem, 2);
}
+void end_boat_throwing(int, int); // FIXME
EMSCRIPTEN_KEEPALIVE void boat_visit() {
if (state->l1->rem <= 0 || state->l2->rem <= 0)
@@ -175,41 +176,27 @@ EMSCRIPTEN_KEEPALIVE void boat_visit() {
avg = ((double)l->start / (((l->n_visits - 1) * 3) + v->n_darts)) * 3;
if (!delay_ms) {
- update_boat_rem(l->rem);
- EM_ASM({updateBoatAvg($0)}, avg);
- EM_ASM({clearMatchLog()});
- draw_match();
- if (!is_match_over())
- set_user_active();
+ end_boat_throwing(l->rem, avg);
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);
+ int pts = 0;
+ char str[20] = {0};
- 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);
+ for (int i = 0; i < v->n_darts; ++i) {
+ pts += segment_points(v->darts[i]);
+ char *seg_name = segment_name(v->darts[i]);
+ if (i) strcat(str, "-");
+ strcat(str, seg_name);
free(seg_name);
- EM_ASM({scheduleBoatThrowing($0, $1, $2)}, pts, str, delay_ms * 3);
+ EM_ASM({scheduleCCall($0, $1, $2, $3, $4, $5)},
+ "draw_boat_throwing", delay_ms * (i+1),
+ "number", pts, "string", str);
}
- EM_ASM({scheduleEndBoatThrowing($0, $1, $2)},
- l->rem, avg, delay_ms * (v->n_darts + 1));
+ EM_ASM({scheduleCCall($0, $1, $2, $3, $4, $5)},
+ "end_boat_throwing", delay_ms * (v->n_darts + 1),
+ "number", l->rem, "number", avg);
}
EMSCRIPTEN_KEEPALIVE bool user_visit(int points) {
@@ -245,7 +232,7 @@ EMSCRIPTEN_KEEPALIVE bool user_visit(int points) {
}
// FIXME neg
-EMSCRIPTEN_KEEPALIVE bool user_visit_to_rem(int rem) {
+EMSCRIPTEN_KEEPALIVE void user_visit_to_rem(int rem) {
user_visit(state->l1->rem - rem);
}
diff --git a/web/static/dartboat.js b/web/static/dartboat.js
index 2a26a0c..0014d3a 100644
--- a/web/static/dartboat.js
+++ b/web/static/dartboat.js
@@ -190,20 +190,16 @@ function setNoneActive() {
document.getElementById('p2-state').classList.remove('active');
}
-function scheduleBoatThrowing(pts, ptr, delay_ms) {
- // ptr to c string must be copied before returning
- let str = UTF8ToString(ptr);
- setTimeout(
- function() { Module.ccall('draw_boat_throwing', null,
- ['number', 'string'], [pts, str]) },
- delay_ms);
-}
+function scheduleCCall(f, ms, ...args) {
+ let types = [];
+ let vals = [];
+
+ for (let i = 0; i < args.length/2; i++)
+ vals[i] = (types[i] = UTF8ToString(args[i*2])) == 'string' ?
+ UTF8ToString(args[i*2+1]) : args[i*2+1];
-function scheduleEndBoatThrowing(rem, avg, delay_ms) {
- setTimeout(
- function() { Module.ccall('end_boat_throwing', null,
- ['number', 'number'], [rem, avg]) },
- delay_ms);
+ let func = UTF8ToString(f);
+ setTimeout(function() { Module.ccall(func, null, types, vals) }, ms);
}
const POINT_CLASSES = [180, 140, 100, 60, 40, 20, 1, 0];
@@ -213,11 +209,10 @@ function drawVisit(visit_no, u_pts, u_rem, b_pts, b_rem, b_darts) {
for (let [i, ptr] of [u_pts, u_rem, visit_no, b_rem, b_pts, b_darts].entries()) {
let div = document.createElement('div');
let v = div.textContent = UTF8ToString(ptr);
- if (i == 0 || i == 4) {
+ if (i == 0 || i == 4)
div.className = `p${POINT_CLASSES.find(x => x <= v)}`;
- } else if (i == 5) {
- if (v) div.textContent = `… ${v}`;
- }
+ else if (i == 5 && v)
+ div.textContent = `… ${v}`;
div.className += ` visit-col${i+1}`;
elem.append(div);
}