summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--web/dartboat_wasm.c152
-rw-r--r--web/static/dartboat.js2
2 files changed, 69 insertions, 85 deletions
diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c
index 6bbb97a..989e0c2 100644
--- a/web/dartboat_wasm.c
+++ b/web/dartboat_wasm.c
@@ -8,20 +8,21 @@
#include <emscripten/emscripten.h>
-#define M_PVC 1
-#define M_P 2
-#define M_PVP 3
-
int delay_ms = 500;
+enum match_mode {
+ M_PVC = 1,
+ M_P,
+ M_PVP
+};
+
// TODO refactor *everything*
struct match_state {
- int mode;
+ enum match_mode mode;
struct leg *l1, *l2, *active_l;
int active_p;
int undone_count;
- int complete;
};
struct match_state *state;
@@ -36,99 +37,100 @@ void switch_active_user() {
}
void set_user_active() {
+ state->active_p = 1;
+ state->active_l = state->l1;
+
EM_ASM({setPlayerActive($0)}, 1);
EM_ASM({setPromptHandler($0)}, "visit");
}
void set_boat_active() {
+ state->active_p = 2;
+ state->active_l = state->l2;
+
EM_ASM({setPlayerActive($0)}, 2);
EM_ASM({setPromptHandler()}, "");
}
-void suggested(int rem, char *buf) {
- buf[0] = 0;
+void update_player_sugg(int player, int rem)
+{
+ char str[15] = {0}, *p = str;
if (rem > 1 && rem <= 170) {
- char *target = CHECKOUTS[2][rem-1];
- if (target) {
- int trem = rem - segment_points(segment_from_name(target));
- int len = sprintf(buf, "%s", target);
- if (trem) {
- target = CHECKOUTS[1][trem-1];
- len += sprintf(buf + len, "-%s", target);
- trem = trem - segment_points(segment_from_name(target));
-
- if (trem) {
- target = CHECKOUTS[0][trem-1];
- len += sprintf(buf + len, "-%s", target);
- }
- }
+ 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));
}
}
-}
-EMSCRIPTEN_KEEPALIVE void get_suggested(int rem, int player) {
- static char buf[20];
- suggested(rem, buf);
- EM_ASM({updatePlayerSugg($0, $1)}, player, buf);
+ EM_ASM({updatePlayerSugg($0, $1)}, player, str);
}
-EMSCRIPTEN_KEEPALIVE int is_match_over() {
- if (state->l1->rem <= 0 || state->l2->rem <= 0)
- return 1;
- return 0;
+bool is_match_over() {
+ return state->l1->rem <= 0 || state->l2->rem <= 0;
}
-void match_over() {
+void prompt_match_over() {
EM_ASM({setPromptHandler($0)}, "match_over");
EM_ASM({promptMsgR($0)}, "Press OK to end match.");
EM_ASM(setPlayerActive());
}
-EMSCRIPTEN_KEEPALIVE void draw_match() {
+EMSCRIPTEN_KEEPALIVE void draw_visits() {
+ EM_ASM({clearVisits()});
+
struct leg *l1 = state->l1;
struct leg *l2 = state->l2;
- char visit_no[10], u_pts[10], u_rem[10], b_pts[10], b_rem[10], b_darts[100];
+ char visit_no[10], p1_pts[10], p1_rem[10],
+ p2_pts[10], p2_rem[10], p2_darts[100];
- sprintf(u_rem, "%d", l1->start);
- sprintf(b_rem, "%d", l2->start);
+ sprintf(p1_rem, "%d", l1->start);
+ sprintf(p2_rem, "%d", l2->start);
EM_ASM({drawVisit($0, $1, $2, $3, $4, $5)},
- "0", "", u_rem, "", state->mode == M_P ? "" : b_rem, "");
+ "0", "", p1_rem, "", state->mode == M_P ? "" : p2_rem, "");
int n_visits = l1->n_visits > l2->n_visits ? l1->n_visits : l2->n_visits;
for (int i = 0; i < n_visits; ++i) {
- visit_no[0] = u_pts[0] = u_rem[0] = b_pts[0] = b_rem[0] = b_darts[0] = 0;
+ 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 = l1->visits + i;
- sprintf(u_pts, "%d", v->points);
- sprintf(u_rem, "%d", v->rem);
+ sprintf(p1_pts, "%d", v->points);
+ sprintf(p1_rem, "%d", v->rem);
if (i < l2->n_visits) {
v = l2->visits + i;
- sprintf(b_pts, "%d", v->points);
- sprintf(b_rem, "%d", v->rem);
+ 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(b_darts + (j ? (j * 5 - 1) : 0), j == 0 ? "%4s" : " %4s", n);
+ 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, u_pts, u_rem, b_pts, b_rem, b_darts);
+ visit_no, p1_pts, p1_rem, p2_pts, p2_rem, p2_darts);
}
+}
+
+EMSCRIPTEN_KEEPALIVE void draw_match() {
+ draw_visits();
- if (l1->rem <= 0) {
+ if (state->l1->rem <= 0) {
EM_ASM({promptMsgL($0)}, "Darts needed?");
EM_ASM({setPromptHandler($0)}, "num_darts");
- } else if (l2->rem <= 0) {
+ } else if (state->l2->rem <= 0) {
if (state->mode == M_PVC) {
EM_ASM({promptMsgL($0)}, "Bot wins. :(");
- match_over();
+ prompt_match_over();
} else {
EM_ASM({promptMsgL($0)}, "Darts needed?");
EM_ASM({setPromptHandler($0)}, "num_darts");
@@ -138,35 +140,21 @@ EMSCRIPTEN_KEEPALIVE void draw_match() {
}
}
-char *str_for_rem_state(int rem) {
- char *buf = malloc(10);
-
+EMSCRIPTEN_KEEPALIVE void update_player_rem(int player, int rem) {
+ char str[10];
if (rem < 0 || rem == 1)
- strcpy(buf, "BUST");
+ strcpy(str, "BUST");
else
- sprintf(buf, "%d", rem);
+ sprintf(str, "%d", rem);
- return buf;
-}
-
-EMSCRIPTEN_KEEPALIVE void update_user_rem(int rem) {
- char *str = str_for_rem_state(rem);
- EM_ASM({updatePlayerRem($0, $1)}, state->active_p, str);
- free(str);
+ EM_ASM({updatePlayerRem($0, $1)}, player, str);
- get_suggested(rem, state->active_p);
+ if (player == 1 || state->mode != M_PVC)
+ update_player_sugg(player, rem);
}
EMSCRIPTEN_KEEPALIVE void update_user_rem_from_pts(int pts) {
- update_user_rem(state->active_l->rem - pts);
-}
-
-EMSCRIPTEN_KEEPALIVE void update_boat_rem(int rem) {
- char *str = str_for_rem_state(rem);
- EM_ASM({updatePlayerRem($0, $1)}, 2, str);
- free(str);
-
- //get_suggested(rem, 2);
+ update_player_rem(state->active_p, state->active_l->rem - pts);
}
void end_boat_throwing(int, double); // FIXME
@@ -235,13 +223,12 @@ EMSCRIPTEN_KEEPALIVE bool user_visit(int points) {
v->points = points;
l->rem -= points;
v->rem = l->rem;
- update_user_rem(l->rem);
+ update_player_rem(state->active_p, l->rem);
if (v->rem > 0)
EM_ASM({updatePlayerAvg($0, $1)}, state->active_p,
(double)(l->start-l->rem)/l->n_visits);
- EM_ASM({clearVisits()});
draw_match();
if (!is_match_over()) {
@@ -289,13 +276,12 @@ EMSCRIPTEN_KEEPALIVE bool user_undo() {
EM_ASM({updatePlayerAvg($0, $1)}, 2,
state->l2->n_visits ?
(double)(state->l2->start-state->l2->rem)/state->l2->n_visits : 0);
- update_user_rem(l->rem);
+ update_player_rem(state->active_p, l->rem);
if (state->mode == M_PVC)
- update_boat_rem(state->l2->rem);
+ update_player_rem(2, state->l2->rem);
memcpy(v, 0, sizeof(*v));
- EM_ASM({clearVisits()});
EM_ASM({promptMsgR($0)}, "");
EM_ASM({setPromptHandler($0)}, "visit");
draw_match();
@@ -310,16 +296,15 @@ EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(int pts, char *str) {
int rem = state->l2->n_visits > 1 ?
state->l2->visits[state->l2->n_visits-2].rem : state->l2->start;
- update_boat_rem(rem - pts);
+ update_player_rem(2, rem - pts);
EM_ASM({setPromptInput($0)}, buf);
EM_ASM({promptMsgR($0)}, str);
}
EMSCRIPTEN_KEEPALIVE void end_boat_throwing(int rem, double avg) {
- update_boat_rem(rem);
+ update_player_rem(2, rem);
EM_ASM({updatePlayerAvg($0, $1)}, 2, avg);
EM_ASM({setPromptInput($0)}, "");
- EM_ASM(clearVisits());
EM_ASM({promptMsgR($0)}, "");
draw_match();
@@ -338,7 +323,7 @@ EMSCRIPTEN_KEEPALIVE void resp_numdarts(int n_darts) {
EM_ASM({updatePlayerAvg($0, $1)}, state->active_p,
((double)l->start / (((l->n_visits - 1) * 3) + n_darts)) * 3);
EM_ASM({promptMsgL($0)}, "You win! :)");
- match_over();
+ prompt_match_over();
}
void init_boat() {
@@ -362,7 +347,7 @@ EMSCRIPTEN_KEEPALIVE void free_match() {
state = NULL;
}
-EMSCRIPTEN_KEEPALIVE void match_init() {
+EMSCRIPTEN_KEEPALIVE void prompt_match_mode() {
for (int i = 1; i < 3; ++i) {
EM_ASM({hidePlayerInfo($0)}, i);
EM_ASM({updatePlayerName($0, $1)}, i, "");
@@ -392,7 +377,7 @@ EMSCRIPTEN_KEEPALIVE void start_match(int mode) {
state->l1 = leg_init(501, "User");
state->l2 = leg_init(501, "Bot");
} else {
- state->l1 = leg_init(501, "Player 1");
+ state->l1 = leg_init(501, mode == M_PVP ? "Player 1" : "User");
state->l2 = leg_init(501, "Player 2");
}
EM_ASM({showPlayerInfo($0)}, 1);
@@ -403,12 +388,11 @@ EMSCRIPTEN_KEEPALIVE void start_match(int mode) {
EM_ASM({updatePlayerName($0, $1)}, 1, state->l1->name);
EM_ASM({updatePlayerName($0, $1)}, 2, state->l2->name);
- update_user_rem(state->l1->rem);
- update_boat_rem(state->l2->rem);
+ update_player_rem(1, state->l1->rem);
+ update_player_rem(2, state->l2->rem);
EM_ASM({updatePlayerAvg($0, $1)}, 1, 0);
EM_ASM({updatePlayerAvg($0, $1)}, 2, 0);
- EM_ASM(clearVisits());
set_user_active();
EM_ASM({promptMsgR($0)}, "");
draw_match();
@@ -426,5 +410,5 @@ EMSCRIPTEN_KEEPALIVE void set_delay(int delay) {
int main() {
EM_ASM(read_opts());
init_boat();
- match_init();
+ prompt_match_mode();
}
diff --git a/web/static/dartboat.js b/web/static/dartboat.js
index f3c5849..4299dd3 100644
--- a/web/static/dartboat.js
+++ b/web/static/dartboat.js
@@ -119,7 +119,7 @@ prompt_handlers.match_over = {
append() {},
submit() {
- Module.ccall('match_init');
+ Module.ccall('prompt_match_mode');
}
};