summaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-01 15:07:11 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-01 15:07:11 -0700
commit22646d963e2b0c9b5ef629aa7704158026cfa6d1 (patch)
tree173473a78da784cf9a5e96770c5a87df5c575a8e /web
parent9459b7fb563caae520bdf8d11da9b070ebff45e9 (diff)
downloaddartboat-22646d963e2b0c9b5ef629aa7704158026cfa6d1.tar.gz
dartboat-22646d963e2b0c9b5ef629aa7704158026cfa6d1.tar.xz
web: split code into more files
This is currently very messy with all the files interdependent on one another and a lot of the logic still in the main file. It will take some time to refactor and clean up.
Diffstat (limited to 'web')
-rw-r--r--web/dartboat_wasm.c158
-rw-r--r--web/web_match.c20
-rw-r--r--web/web_match.h29
-rw-r--r--web/web_svg.c (renamed from web/svg.c)2
-rw-r--r--web/web_svg.h (renamed from web/svg.h)4
-rw-r--r--web/web_ui.c134
-rw-r--r--web/web_ui.h18
7 files changed, 208 insertions, 157 deletions
diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c
index 658277d..c43e73d 100644
--- a/web/dartboat_wasm.c
+++ b/web/dartboat_wasm.c
@@ -1,4 +1,5 @@
-#include "svg.h"
+#include "web_svg.h"
+#include "web_ui.h"
#include "checkouts.h"
#include "comp.h"
@@ -11,6 +12,8 @@
#include <emscripten/emscripten.h>
+#include "web_match.h"
+
int delay_ms = 1000;
enum prompt_mode {
@@ -36,25 +39,6 @@ void set_prompt_mode(enum prompt_mode mode)
pm == PM_SELECT_MODE ? "select_mode" : "default");
}
-enum match_mode {
- M_FIRST = 1,
- M_PVC = M_FIRST,
- M_P,
- M_PVP,
- M_LAST = M_PVP
-};
-
-struct match_state {
- enum match_mode mode;
- struct leg *legs[2];
- struct leg *active_leg;
- int active_player;
- int boat_undone;
- int num_darts;
-};
-
-struct match_state *state;
-
void set_active_player(int pn)
{
state->active_player = pn;
@@ -78,103 +62,11 @@ void toggle_active_player()
set_active_player(3 - state->active_player);
}
-void update_player_sugg(int pn, int rem)
-{
- char str[15] = {0}, *p = str;
-
- if (rem > 1 && rem <= 170) {
- char *target;
- int i = 3, trem = rem;
- while (i && trem && (target = CHECKOUTS[--i][trem-1])) {
- p += sprintf(p, i == 2 ? "%s" : "-%s", target);
- trem -= segment_points(segment_from_name(target));
- }
- }
-
- EM_ASM({updatePlayerSugg($0, $1)}, pn, str);
-}
-
-bool is_match_over()
-{
- return state->legs[0]->rem <= 0 || state->legs[1]->rem <= 0;
-}
-
-void draw_visits()
-{
- EM_ASM({clearVisits()});
-
- char visit_no[10], p1_pts[10], p1_rem[10],
- p2_pts[10], p2_rem[10], p2_darts[100];
-
- sprintf(p1_rem, "%d", state->legs[0]->start);
- sprintf(p2_rem, "%d", state->legs[1]->start);
- EM_ASM({drawVisit($0, $1, $2, $3, $4, $5)},
- "0", "", p1_rem, "", state->mode == M_P ? "" : p2_rem, "");
-
- int n_visits = state->legs[0]->n_visits > state->legs[1]->n_visits ?
- state->legs[0]->n_visits : state->legs[1]->n_visits;
- for (int i = 0; i < n_visits; ++i) {
- visit_no[0] = p1_pts[0] = p1_rem[0] =
- p2_pts[0] = p2_rem[0] = p2_darts[0] = 0;
-
- sprintf(visit_no, "%d", i + 1);
-
- struct visit *v = state->legs[0]->visits + i;
- sprintf(p1_pts, "%d", v->points);
- sprintf(p1_rem, "%d", v->rem);
-
- if (i < state->legs[1]->n_visits) {
- v = state->legs[1]->visits + i;
- sprintf(p2_pts, "%d", v->points);
- sprintf(p2_rem, "%d", v->rem);
-
- for (int j = 0; j < v->n_darts; ++j) {
- char *n = segment_name(v->darts[j]);
- sprintf(p2_darts + (j ? (j*5 - 1) : 0),
- j == 0 ? "%4s" : " %4s", n);
- free(n);
- }
- }
-
- EM_ASM({drawVisit($0, $1, $2, $3, $4, $5)},
- visit_no, p1_pts, p1_rem, p2_pts, p2_rem, p2_darts);
- }
-}
-
-void update_player_rem(int pn, int rem)
-{
- char str[5];
- if (rem < 0 || rem == 1)
- strcpy(str, "BUST");
- else
- sprintf(str, "%d", rem);
-
- EM_ASM({updatePlayerRem($0, $1)}, pn, str);
- if (pn == 1 || state->mode != M_PVC)
- update_player_sugg(pn, rem);
-}
-
EMSCRIPTEN_KEEPALIVE void update_user_rem_from_pts(int pts)
{
update_player_rem(state->active_player, state->active_leg->rem - pts);
}
-EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(int pts, char *str, double x, double y)
-{
- char pts_str[10];
- sprintf(pts_str, "%d", pts);
-
- int rem = state->legs[1]->n_visits > 1 ?
- state->legs[1]->visits[state->legs[1]->n_visits-2].rem :
- state->legs[1]->start;
-
- svg_draw_point(x, y);
- update_player_rem(2, rem - pts);
- EM_ASM({setPromptInput($0)}, pts_str);
- EM_ASM({promptMsgR($0)}, str);
- free(str);
-}
-
void handle_next();
EMSCRIPTEN_KEEPALIVE void end_boat_visit(int rem, double avg)
{
@@ -230,14 +122,6 @@ EMSCRIPTEN_KEEPALIVE void boat_visit()
delay_ms * (v->n_darts + 1), l->rem, avg);
}
-void clear_player_info(int pn)
-{
- EM_ASM({updatePlayerName($0, $1)}, pn, "");
- EM_ASM({updatePlayerRem($0, $1)}, pn, "");
- EM_ASM({updatePlayerSugg($0, $1)}, pn, "");
- EM_ASM({updatePlayerAvg($0, $1)}, pn, 0);
-}
-
void prompt_num_darts()
{
set_prompt_mode(PM_NUM_DARTS);
@@ -297,16 +181,6 @@ void handle_next()
}
}
-void update_player_avg(int pn, int n_darts)
-{
- struct leg *l = state->legs[pn-1];
- EM_ASM({updatePlayerAvg($0, $1)}, pn,
- l->n_visits ?
- (l->rem > 0 ? ((double)(l->start - l->rem) / l->n_visits) :
- ((double)l->start / (((l->n_visits - 1) * 3) + n_darts) * 3)) :
- 0);
-}
-
EMSCRIPTEN_KEEPALIVE void user_visit(int points)
{
if (!is_points_valid(points, state->active_leg->rem)) {
@@ -400,15 +274,6 @@ EMSCRIPTEN_KEEPALIVE void user_num_darts(int n)
handle_next();
}
-void free_state()
-{
- state->legs[1]->n_visits += state->boat_undone; // avoid memory leak
- leg_free(state->legs[0]);
- leg_free(state->legs[1]);
- free(state);
- state = NULL;
-}
-
EMSCRIPTEN_KEEPALIVE void start_match(int mode)
{
if (mode < M_FIRST || mode > M_LAST) {
@@ -466,21 +331,6 @@ void init_boat()
EM_ASM({updateStdev($0)}, horizontal_stdev);
}
-bool is_key_active(char *k)
-{
- return (EM_ASM_INT({return isKeyActive($0)}, k));
-}
-
-void toggle_key(char *k)
-{
- EM_ASM({toggleKey($0)}, k);
-}
-
-void deactivate_key(char *k)
-{
- EM_ASM({deactivateKey($0)}, k);
-}
-
char *prompt_get()
{
return (char *)EM_ASM_INT({return promptGet()});
diff --git a/web/web_match.c b/web/web_match.c
new file mode 100644
index 0000000..47bc6e6
--- /dev/null
+++ b/web/web_match.c
@@ -0,0 +1,20 @@
+#include "web_match.h"
+
+#include <stddef.h>
+#include <stdlib.h>
+
+struct match_state *state;
+
+void free_state()
+{
+ state->legs[1]->n_visits += state->boat_undone; // avoid memory leak
+ leg_free(state->legs[0]);
+ leg_free(state->legs[1]);
+ free(state);
+ state = NULL;
+}
+
+bool is_match_over()
+{
+ return state->legs[0]->rem <= 0 || state->legs[1]->rem <= 0;
+}
diff --git a/web/web_match.h b/web/web_match.h
new file mode 100644
index 0000000..4b4c7bb
--- /dev/null
+++ b/web/web_match.h
@@ -0,0 +1,29 @@
+#ifndef WEB_MATCH_H
+#define WEB_MATCH_H
+
+#include "match.h"
+
+enum match_mode {
+ M_FIRST = 1,
+ M_PVC = M_FIRST,
+ M_P,
+ M_PVP,
+ M_LAST = M_PVP
+};
+
+struct match_state {
+ enum match_mode mode;
+ struct leg *legs[2];
+ struct leg *active_leg;
+ int active_player;
+ int boat_undone;
+ int num_darts;
+};
+
+extern struct match_state *state;
+
+void free_state();
+
+bool is_match_over();
+
+#endif
diff --git a/web/svg.c b/web/web_svg.c
index 6744ac6..9b8f25d 100644
--- a/web/svg.c
+++ b/web/web_svg.c
@@ -1,4 +1,4 @@
-#include "svg.h"
+#include "web_svg.h"
#include "board.h"
#include "comp.h"
diff --git a/web/svg.h b/web/web_svg.h
index 3321846..aae0b0e 100644
--- a/web/svg.h
+++ b/web/web_svg.h
@@ -1,5 +1,5 @@
-#ifndef SVG_H
-#define SVG_H
+#ifndef WEB_SVG_H
+#define WEB_SVG_H
void svg_draw_board();
void svg_draw_point(double x, double y);
diff --git a/web/web_ui.c b/web/web_ui.c
new file mode 100644
index 0000000..7ac4182
--- /dev/null
+++ b/web/web_ui.c
@@ -0,0 +1,134 @@
+#include "web_match.h"
+#include "web_svg.h"
+#include "web_ui.h"
+
+#include "checkouts.h"
+#include "match.h"
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <emscripten/emscripten.h>
+
+void update_player_sugg(int pn, int rem)
+{
+ char str[15] = {0}, *p = str;
+
+ if (rem > 1 && rem <= 170) {
+ char *target;
+ int i = 3, trem = rem;
+ while (i && trem && (target = CHECKOUTS[--i][trem-1])) {
+ p += sprintf(p, i == 2 ? "%s" : "-%s", target);
+ trem -= segment_points(segment_from_name(target));
+ }
+ }
+
+ EM_ASM({updatePlayerSugg($0, $1)}, pn, str);
+}
+
+void update_player_avg(int pn, int n_darts)
+{
+ struct leg *l = state->legs[pn-1];
+ EM_ASM({updatePlayerAvg($0, $1)}, pn,
+ l->n_visits ?
+ (l->rem > 0 ? ((double)(l->start - l->rem) / l->n_visits) :
+ ((double)l->start / (((l->n_visits - 1) * 3) + n_darts) * 3)) :
+ 0);
+}
+
+void draw_visits()
+{
+ EM_ASM({clearVisits()});
+
+ char visit_no[10], p1_pts[10], p1_rem[10],
+ p2_pts[10], p2_rem[10], p2_darts[100];
+
+ sprintf(p1_rem, "%d", state->legs[0]->start);
+ sprintf(p2_rem, "%d", state->legs[1]->start);
+ EM_ASM({drawVisit($0, $1, $2, $3, $4, $5)},
+ "0", "", p1_rem, "", state->mode == M_P ? "" : p2_rem, "");
+
+ int n_visits = state->legs[0]->n_visits > state->legs[1]->n_visits ?
+ state->legs[0]->n_visits : state->legs[1]->n_visits;
+ for (int i = 0; i < n_visits; ++i) {
+ visit_no[0] = p1_pts[0] = p1_rem[0] =
+ p2_pts[0] = p2_rem[0] = p2_darts[0] = 0;
+
+ sprintf(visit_no, "%d", i + 1);
+
+ struct visit *v = state->legs[0]->visits + i;
+ sprintf(p1_pts, "%d", v->points);
+ sprintf(p1_rem, "%d", v->rem);
+
+ if (i < state->legs[1]->n_visits) {
+ v = state->legs[1]->visits + i;
+ sprintf(p2_pts, "%d", v->points);
+ sprintf(p2_rem, "%d", v->rem);
+
+ for (int j = 0; j < v->n_darts; ++j) {
+ char *n = segment_name(v->darts[j]);
+ sprintf(p2_darts + (j ? (j*5 - 1) : 0),
+ j == 0 ? "%4s" : " %4s", n);
+ free(n);
+ }
+ }
+
+ EM_ASM({drawVisit($0, $1, $2, $3, $4, $5)},
+ visit_no, p1_pts, p1_rem, p2_pts, p2_rem, p2_darts);
+ }
+}
+
+void update_player_rem(int pn, int rem)
+{
+ char str[5];
+ if (rem < 0 || rem == 1)
+ strcpy(str, "BUST");
+ else
+ sprintf(str, "%d", rem);
+
+ EM_ASM({updatePlayerRem($0, $1)}, pn, str);
+ if (pn == 1 || state->mode != M_PVC)
+ update_player_sugg(pn, rem);
+}
+
+void clear_player_info(int pn)
+{
+ EM_ASM({updatePlayerName($0, $1)}, pn, "");
+ EM_ASM({updatePlayerRem($0, $1)}, pn, "");
+ EM_ASM({updatePlayerSugg($0, $1)}, pn, "");
+ EM_ASM({updatePlayerAvg($0, $1)}, pn, 0);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void draw_boat_throwing(int pts, char *str, double x, double y)
+{
+ char pts_str[10];
+ sprintf(pts_str, "%d", pts);
+
+ int rem = state->legs[1]->n_visits > 1 ?
+ state->legs[1]->visits[state->legs[1]->n_visits-2].rem :
+ state->legs[1]->start;
+
+ svg_draw_point(x, y);
+ update_player_rem(2, rem - pts);
+ EM_ASM({setPromptInput($0)}, pts_str);
+ EM_ASM({promptMsgR($0)}, str);
+ free(str);
+}
+
+bool is_key_active(char *k)
+{
+ return (EM_ASM_INT({return isKeyActive($0)}, k));
+}
+
+void toggle_key(char *k)
+{
+ EM_ASM({toggleKey($0)}, k);
+}
+
+void deactivate_key(char *k)
+{
+ EM_ASM({deactivateKey($0)}, k);
+}
diff --git a/web/web_ui.h b/web/web_ui.h
new file mode 100644
index 0000000..fa961fb
--- /dev/null
+++ b/web/web_ui.h
@@ -0,0 +1,18 @@
+#ifndef WEB_UI_H
+#define WEB_UI_H
+
+#include <stdbool.h>
+
+void update_player_sugg(int pn, int rem);
+void update_player_avg(int pn, int n_darts);
+void update_player_rem(int pn, int rem);
+void clear_player_info(int pn);
+
+void draw_visits();
+void draw_boat_throwing(int pts, char *str, double x, double y);
+
+bool is_key_active(char *k);
+void toggle_key(char *k);
+void deactivate_key(char *k);
+
+#endif