summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-16 14:02:07 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-16 14:04:26 -0700
commit9c8d587a4b9eb3c908027c21aeab0b01b25ae02b (patch)
treefaaf1ac7d7c291c02dcd1e305ee720ac89ee25b7
parent03bbcf1bf6be002dde3463c2370c11b2c7e38732 (diff)
downloaddartboat-9c8d587a4b9eb3c908027c21aeab0b01b25ae02b.tar.gz
dartboat-9c8d587a4b9eb3c908027c21aeab0b01b25ae02b.tar.xz
web: fix ending match mid-comp-throw and starting another quickly
-rw-r--r--web/static/sw.js2
-rw-r--r--web/web_control.c37
-rw-r--r--web/web_control.h1
-rw-r--r--web/web_prompt.c10
4 files changed, 19 insertions, 31 deletions
diff --git a/web/static/sw.js b/web/static/sw.js
index 253d350..ba14e30 100644
--- a/web/static/sw.js
+++ b/web/static/sw.js
@@ -1,5 +1,5 @@
const CACHE_PREFIX = 'dartboat-'
-const CACHE_VERSION = '9';
+const CACHE_VERSION = '10';
const CACHE_NAME = `${CACHE_PREFIX}${CACHE_VERSION}`;
const CACHE_FILES = [
diff --git a/web/web_control.c b/web/web_control.c
index cb83b99..b0dfd3b 100644
--- a/web/web_control.c
+++ b/web/web_control.c
@@ -15,11 +15,7 @@
#include <emscripten/emscripten.h>
-static int num_scheduled;
-void clear_scheduled()
-{
- num_scheduled = 0;
-}
+static int curr_match_id = 0;
void set_active_player(int pn)
{
@@ -45,13 +41,10 @@ void update_user_rem_from_pts(int pts)
}
EMSCRIPTEN_KEEPALIVE
-void end_boat_visit(int rem, double avg, bool scheduled)
+void end_boat_visit(int rem, double avg, int match_id)
{
- if (scheduled) {
- if (!num_scheduled)
- return;
- --num_scheduled;
- }
+ if (!state || match_id != curr_match_id)
+ return;
svg_clear_points();
update_player_rem(2, rem);
@@ -64,11 +57,10 @@ void end_boat_visit(int rem, double avg, bool scheduled)
}
EMSCRIPTEN_KEEPALIVE
-void draw_boat_throwing(int pts, char *str, double x, double y)
+void draw_boat_throwing(int pts, char *str, double x, double y, int match_id)
{
- if (!num_scheduled)
+ if (!state || match_id != curr_match_id)
return;
- --num_scheduled;
char pts_str[10];
sprintf(pts_str, "%d", pts);
@@ -103,7 +95,7 @@ void boat_visit()
(double)l->start / (((l->n_visits - 1) * 3) + v->n_darts) * 3;
if (!delay_ms) {
- end_boat_visit(l->rem, avg, false);
+ end_boat_visit(l->rem, avg, curr_match_id);
return;
}
@@ -121,15 +113,13 @@ void boat_visit()
char *tmp = malloc(len_str + 1); // free in draw_boat_throwing
memcpy(tmp, str, len_str + 1);
- EM_ASM({scheduleCCall($0, $1, $2, $3, $4, $5)},
+ EM_ASM({scheduleCCall($0, $1, $2, $3, $4, $5, $6)},
"draw_boat_throwing", delay_ms * (i+1),
- pts, tmp, c.x, c.y);
- ++num_scheduled;
+ pts, tmp, c.x, c.y, curr_match_id);
}
EM_ASM({scheduleCCall($0, $1, $2, $3, $4)}, "end_boat_visit",
- delay_ms * (v->n_darts + 1), l->rem, avg, true);
- ++num_scheduled;
+ delay_ms * (v->n_darts + 1), l->rem, avg, curr_match_id);
}
void handle_next()
@@ -288,6 +278,8 @@ void match_mode_selected(int mode)
void start_match()
{
if (state) free_state(); // rematch gets us here
+ ++curr_match_id;
+
state = calloc(1, sizeof(*state));
state->mode = match_opts->mode;
state->legs[0] = leg_init(match_opts->start_pts, match_opts->p1_name);
@@ -314,6 +306,11 @@ void start_match()
void end_match()
{
+ // clean up in case match was ended early
+ svg_clear_points();
+ prompt_set_input(NULL);
+ scoreboard_set_player_active(-1);
+
dom_remove_class("#key-exit", "visible");
dom_enable_exit_dialogue(false);
if (state) free_state();
diff --git a/web/web_control.h b/web/web_control.h
index 738f3eb..19c8719 100644
--- a/web/web_control.h
+++ b/web/web_control.h
@@ -1,7 +1,6 @@
#ifndef WEB_CONTROL_H
#define WEB_CONTROL_H
-void clear_scheduled();
void start_match();
void match_mode_selected(int mode);
void end_match();
diff --git a/web/web_prompt.c b/web/web_prompt.c
index 5bb7758..f3b523e 100644
--- a/web/web_prompt.c
+++ b/web/web_prompt.c
@@ -4,7 +4,6 @@
#include "web_match.h"
#include "web_misc.h"
#include "web_scoreboard.h"
-#include "web_svg.h"
#include <stdio.h>
#include <stdlib.h>
@@ -479,15 +478,8 @@ void prompt_handle_undo()
void prompt_handle_exit()
{
- if (key_is_active("exit")) {
- clear_scheduled();
- // FIXME: we need to clean up since end_boat_visit is never
- // called, but it's currently a bit messy
- svg_clear_points();
- prompt_set_input(NULL);
- scoreboard_set_player_active(-1);
+ if (key_is_active("exit"))
end_match();
- }
toggle_key("exit");
}