diff options
-rw-r--r-- | comp.c | 3 | ||||
-rw-r--r-- | curses.c | 6 | ||||
-rw-r--r-- | dartboat.c | 22 | ||||
-rw-r--r-- | web/static/dartboat.js | 21 | ||||
-rw-r--r-- | web/web_control.c | 16 | ||||
-rw-r--r-- | web/web_dom.c | 9 | ||||
-rw-r--r-- | web/web_prompt.c | 15 | ||||
-rw-r--r-- | web/web_scoreboard.c | 31 | ||||
-rw-r--r-- | web/web_svg.c | 42 |
9 files changed, 102 insertions, 63 deletions
@@ -94,7 +94,8 @@ void comp_visit(struct leg *l) struct pcoords dc = throw_dart(tc); struct segment ds = get_segment(dc); - v->ccoords[v->n_darts] = pol_to_cart(dc); // FIXME double conversion + // FIXME double conversion + v->ccoords[v->n_darts] = pol_to_cart(dc); v->darts[v->n_darts++] = ds; v->points += segment_points(ds); @@ -94,7 +94,8 @@ void curses_draw(struct leg *l1, struct leg *l2) int buflen = 0; int wlines = LINES - 3; - int n_visits = l1->n_visits > l2->n_visits ? l1->n_visits : l2->n_visits; + int n_visits = l1->n_visits > l2->n_visits ? l1->n_visits : + l2->n_visits; int start_visit = (wlines > n_visits) ? 0 : (n_visits - wlines); int offset = 0; @@ -132,7 +133,8 @@ void curses_draw(struct leg *l1, struct leg *l2) for (int j = 0; j < v->n_darts; ++j) { char *n = segment_name(v->darts[j]); - buflen += sprintf(buf + buflen, j == 0 ? " %4s" :" %4s", n); + buflen += sprintf(buf + buflen, + j == 0 ? " %4s" :" %4s", n); free(n); } flushbuf(buf, &buflen, COLOR_PAIR(C_DARTS)); @@ -43,16 +43,19 @@ void user_visit(struct leg *l) if (l->rem <= 170) { char *target = CHECKOUTS[2][l->rem-1]; if (target) { - int trem = l->rem - segment_points(segment_from_name(target)); + int trem = l->rem - segment_points( + segment_from_name(target)); len += sprintf(status + len, " (%s", target); if (trem) { target = CHECKOUTS[1][trem-1]; len += sprintf(status + len, "-%s", target); - trem = trem - segment_points(segment_from_name(target)); + trem = trem - segment_points( + segment_from_name(target)); if (trem) { target = CHECKOUTS[0][trem-1]; - len += sprintf(status + len, "-%s", target); + len += sprintf(status + len, + "-%s", target); } } len += sprintf(status + len, ")"); @@ -118,18 +121,20 @@ void curses_match(int start_points, char *n1, void (*f1)(struct leg *), void cvc_curses_match(int start_points) { - curses_match(start_points, "Dartboat 1", comp_visit, "Dartboat 2", - comp_visit); + curses_match(start_points, "Dartboat 1", comp_visit, + "Dartboat 2", comp_visit); } void pvc_curses_match(int start_points) { - curses_match(start_points, "David", user_visit, "Dartboat", comp_visit); + curses_match(start_points, "David", user_visit, + "Dartboat", comp_visit); } void pvp_curses_match(int start_points) { - curses_match(start_points, "David", user_visit, "Davidn't", user_visit); + curses_match(start_points, "David", user_visit, + "Davidn't", user_visit); } void test_averages() @@ -146,7 +151,8 @@ void test_averages() comp_visit(l); leg_free(l); - darts += (l->n_visits-1)*3 + l->visits[l->n_visits-1].n_darts; + darts += (l->n_visits - 1) * 3 + + l->visits[l->n_visits-1].n_darts; } printf("%d %f\n", stdev, (double)(501*rounds)/darts*3); diff --git a/web/static/dartboat.js b/web/static/dartboat.js index 70e4677..e09e8f6 100644 --- a/web/static/dartboat.js +++ b/web/static/dartboat.js @@ -46,8 +46,8 @@ function elemToggleClass(sel, c) { function elemSetUniqClass(sel, c, sel_set) { selstr = sel && UTF8ToString(sel); cstr = UTF8ToString(c); - $$(UTF8ToString(sel_set)).forEach(e => - e.classList[sel && e.matches(selstr) ? 'add' : 'remove'](cstr)); + $$(UTF8ToString(sel_set)).forEach(e => e.classList[ + sel && e.matches(selstr) ? 'add' : 'remove'](cstr)); } function elemScrollToBottom(sel) { @@ -64,16 +64,21 @@ function elemAppendElemv(sel, elemc, elemv, off_ns, off_name, off_content, const nsptr = HEAP32[(struct + off_ns)>>2]; const name = UTF8ToString(HEAP32[(struct + off_name)>>2]); - const e = nsptr ? document.createElementNS(UTF8ToString(nsptr), name) : + const e = nsptr ? + document.createElementNS(UTF8ToString(nsptr), name) : document.createElement(name); const n_attrs = HEAP32[(struct + off_n_attrs)>>2]; if (n_attrs) { - const attr_names = HEAP32[(struct + off_attr_names)>>2]; + const attr_names = + HEAP32[(struct + off_attr_names)>>2]; const attr_vals = HEAP32[(struct + off_attr_vals)>>2]; for (let i = 0; i < n_attrs; ++i) - e.setAttribute(UTF8ToString(HEAP32[(attr_names + i*4)>>2]), - UTF8ToString(HEAP32[(attr_vals + i*4)>>2])); + e.setAttribute( + UTF8ToString( + HEAP32[(attr_names + i*4)>>2]), + UTF8ToString( + HEAP32[(attr_vals + i*4)>>2])); } const content = HEAP32[(struct + off_content)>>2]; @@ -121,7 +126,9 @@ document.addEventListener('DOMContentLoaded', () => { f = e => $(`#${e.target.dataset.modal}`).style.display = 'block'; $$('[data-modal]').forEach(x => x.addEventListener('click', f)); f = e => { - if (e.target === e.currentTarget) e.target.style.display = 'none'; }; + if (e.target === e.currentTarget) + e.target.style.display = 'none'; + }; $$('.modal').forEach(x => x.addEventListener('click', f)); }); diff --git a/web/web_control.c b/web/web_control.c index e1e322d..5bf3c7d 100644 --- a/web/web_control.c +++ b/web/web_control.c @@ -105,8 +105,9 @@ 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)}, "draw_boat_throwing", - delay_ms * (i+1), pts, tmp, c.x, c.y); + EM_ASM({scheduleCCall($0, $1, $2, $3, $4, $5)}, + "draw_boat_throwing", delay_ms * (i+1), + pts, tmp, c.x, c.y); } EM_ASM({scheduleCCall($0, $1, $2, $3)}, "end_boat_visit", @@ -181,11 +182,10 @@ void user_undo() if (state->num_darts) { state->num_darts = 0; + struct leg *l = state->active_leg; scoreboard_set_player_avg(state->active_player, - ((double)(state->active_leg->start - - state->active_leg->visits[ - state->active_leg->n_visits-2].rem) / - (state->active_leg->n_visits-1))); + ((double)(l->start - l->visits[l->n_visits-2].rem) / + (l->n_visits - 1))); scoreboard_set_player_active(state->active_player); handle_next(); return; @@ -206,8 +206,8 @@ void user_undo() memcpy(v, 0, sizeof(*v)); if (state->mode == M_PVC && state->legs[1]->n_visits > l->n_visits) { - state->legs[1]->rem += - state->legs[1]->visits[--state->legs[1]->n_visits].points; + struct leg *bl = state->legs[1]; + bl->rem += bl->visits[--bl->n_visits].points; ++state->boat_undone; } diff --git a/web/web_dom.c b/web/web_dom.c index ddb59ad..3a862f7 100644 --- a/web/web_dom.c +++ b/web/web_dom.c @@ -61,7 +61,10 @@ void append_elemv(char *sel, int elemc, struct elem **elemv) { EM_ASM({elemAppendElemv($0, $1, $2, $3, $4, $5, $6, $7, $8)}, sel, elemc, elemv, - offsetof(struct elem, ns), offsetof(struct elem, name), - offsetof(struct elem, content), offsetof(struct elem, n_attrs), - offsetof(struct elem, attr_names), offsetof(struct elem, attr_vals)); + offsetof(struct elem, ns), + offsetof(struct elem, name), + offsetof(struct elem, content), + offsetof(struct elem, n_attrs), + offsetof(struct elem, attr_names), + offsetof(struct elem, attr_vals)); } diff --git a/web/web_prompt.c b/web/web_prompt.c index 814b08e..22ec4c1 100644 --- a/web/web_prompt.c +++ b/web/web_prompt.c @@ -22,7 +22,8 @@ enum prompt_mode pm; void oi() { EM_ASM({elemAddClass($0, $1)}, "#oi", "visible"); - oi_timeout = EM_ASM_INT({return scheduleCCall($0, $1)}, "clear_oi", 3000); + oi_timeout = EM_ASM_INT({return scheduleCCall($0, $1)}, + "clear_oi", 3000); } EMSCRIPTEN_KEEPALIVE @@ -102,8 +103,9 @@ void set_prompt_mode(enum prompt_mode mode) EM_ASM({elemRemoveClass($0, $1)}, "#prompt", "active"); EM_ASM({elemSetUniqClass($0, $1, $2)}, - (pm == PM_DARTBOARD ? "#keypad-dartboard" : - pm == PM_SELECT_MODE ? "#keypad-select_mode" : "#keypad-default"), + pm == PM_DARTBOARD ? "#keypad-dartboard" : + pm == PM_SELECT_MODE ? "#keypad-select_mode" : + "#keypad-default", "visible", ".keypad"); } @@ -137,8 +139,8 @@ void prompt_end_match() scoreboard_set_player_active(-1); // sets all inactive set_prompt_mode(PM_END_MATCH); prompt_set_msgl( - state->mode == M_PVC && state->legs[1]->rem <= 0 ? "Bot wins. :(" : - "You win! :)"); + state->mode == M_PVC && state->legs[1]->rem <= 0 ? + "Bot wins. :(" : "You win! :)"); prompt_set_msgr(NULL); EM_ASM({elemSetContent($0, $1)}, "#key-submit", "END MATCH"); EM_ASM({elemSetContent($0, $1)}, "#key-rem", "REMATCH"); @@ -180,7 +182,8 @@ void deactivate_key(char *k) char *prompt_get() { - return (char *)EM_ASM_INT({return elemGetContent($0)}, "#prompt-input"); + return (char *)EM_ASM_INT({return elemGetContent($0)}, + "#prompt-input"); } void prompt_handle_pre(char *command) diff --git a/web/web_scoreboard.c b/web/web_scoreboard.c index bc1a9d0..1b3660d 100644 --- a/web/web_scoreboard.c +++ b/web/web_scoreboard.c @@ -83,25 +83,29 @@ void scoreboard_flush_player_info(int pn) if (buffered_str_changed(buffered_info[pn-1].name, flushed_info[pn-1].name)) { strcpy(sel + len, "name"); - EM_ASM({elemSetContent($0, $1)}, sel, buffered_info[pn-1].name); + EM_ASM({elemSetContent($0, $1)}, + sel, buffered_info[pn-1].name); } if (buffered_str_changed(buffered_info[pn-1].rem, flushed_info[pn-1].rem)) { strcpy(sel + len, "rem"); - EM_ASM({elemSetContent($0, $1)}, sel, buffered_info[pn-1].rem); + EM_ASM({elemSetContent($0, $1)}, + sel, buffered_info[pn-1].rem); } if (buffered_str_changed(buffered_info[pn-1].sugg, flushed_info[pn-1].sugg)) { strcpy(sel + len, "sugg"); - EM_ASM({elemSetContent($0, $1)}, sel, buffered_info[pn-1].sugg); + EM_ASM({elemSetContent($0, $1)}, + sel, buffered_info[pn-1].sugg); } if (buffered_str_changed(buffered_info[pn-1].avg, flushed_info[pn-1].avg)) { strcpy(sel + len, "avg"); - EM_ASM({elemSetContent($0, $1)}, sel, buffered_info[pn-1].avg); + EM_ASM({elemSetContent($0, $1)}, + sel, buffered_info[pn-1].avg); } free_flushed_str(flushed_info[pn-1].name, buffered_info[pn-1].name); @@ -139,14 +143,20 @@ void update_player_sugg(int pn, int rem) scoreboard_set_player_sugg(pn, str); } +static double calc_avg(struct leg *l, int n_darts) { + if (!l->n_visits) + return 0; + + if (l->rem > 0) + return ((double)(l->start - l->rem) / l->n_visits); + + return ((double)l->start / (((l->n_visits - 1) * 3) + n_darts) * 3); +} + void update_player_avg(int pn, int n_darts) { struct leg *l = state->legs[pn-1]; - scoreboard_set_player_avg(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); + scoreboard_set_player_avg(pn, calc_avg(l, n_darts)); } void update_player_rem(int pn, int rem) @@ -238,7 +248,8 @@ void draw_visits() strcpy(buf, "… "); for (int j = 0; j < v->n_darts; ++j) { char *n = segment_name(v->darts[j]); - sprintf(buf + strlen(buf), j == 0 ? "%4s" : " %4s", n); // FIXME + sprintf(buf + strlen(buf), + j == 0 ? "%4s" : " %4s", n); // FIXME free(n); } elemv[elemc++] = gen_div(buf, "visit-col6"); diff --git a/web/web_svg.c b/web/web_svg.c index 3d3288c..ffdd104 100644 --- a/web/web_svg.c +++ b/web/web_svg.c @@ -28,14 +28,14 @@ static inline struct elem *svg_elem_init(char *name, int size_attrs) // draws a ring if angles are equal struct elem *gen_arc(double a1, double a2, double r1, double r2, char *col) { - struct ccoords cc1 = pol_to_cart((struct pcoords){ - .a = (a1 == a2 ? 1 : a2), .r = r1 }); - struct ccoords cc2 = pol_to_cart((struct pcoords){ - .a = (a1 == a2 ? 0 : a1), .r = r1 }); - struct ccoords cc3 = pol_to_cart((struct pcoords){ - .a = (a1 == a2 ? 0 : a1), .r = r2 }); - struct ccoords cc4 = pol_to_cart((struct pcoords){ - .a = (a1 == a2 ? 1 : a2), .r = r2 }); + struct ccoords cc1 = pol_to_cart( + (struct pcoords){ .a = (a1 == a2 ? 1 : a2), .r = r1 }); + struct ccoords cc2 = pol_to_cart( + (struct pcoords){ .a = (a1 == a2 ? 0 : a1), .r = r1 }); + struct ccoords cc3 = pol_to_cart( + (struct pcoords){ .a = (a1 == a2 ? 0 : a1), .r = r2 }); + struct ccoords cc4 = pol_to_cart( + (struct pcoords){ .a = (a1 == a2 ? 1 : a2), .r = r2 }); struct elem *e = svg_elem_init("path", 2); elem_add_attr(e, "fill", col); @@ -100,22 +100,24 @@ int draw_spider(int elemc, struct elem **elemv) { for (int i = 5; i > 1; --i) { elemv[elemc++] = gen_ring(OUTER_DISTS[i], WIRE_WIDTH, C_WIRE); - elemv[elemc++] = gen_ring(OUTER_DISTS[i], WIRE_WIDTH/2, C_WIRE_INNER); + elemv[elemc++] = gen_ring(OUTER_DISTS[i], WIRE_WIDTH/2, + C_WIRE_INNER); } for (int i = 0; i < NUM_SECTORS; ++i) { double a = 90 - i*SECTOR_WIDTH - SECTOR_WIDTH/2; if (a < 0) a += 360; - elemv[elemc++] = gen_line(a, OUTER_DISTS[1], OUTER_DISTS[5] + 10, - WIRE_WIDTH, C_WIRE); - elemv[elemc++] = gen_line(a, OUTER_DISTS[1], OUTER_DISTS[5] + 10, - WIRE_WIDTH/2, C_WIRE_INNER); + elemv[elemc++] = gen_line(a, OUTER_DISTS[1], + OUTER_DISTS[5] + 10, WIRE_WIDTH, C_WIRE); + elemv[elemc++] = gen_line(a, OUTER_DISTS[1], + OUTER_DISTS[5] + 10, WIRE_WIDTH/2, C_WIRE_INNER); } for (int i = 1; i >= 0; --i) { elemv[elemc++] = gen_ring(OUTER_DISTS[i], WIRE_WIDTH, C_WIRE); - elemv[elemc++] = gen_ring(OUTER_DISTS[i], WIRE_WIDTH/2, C_WIRE_INNER); + elemv[elemc++] = gen_ring(OUTER_DISTS[i], WIRE_WIDTH/2, + C_WIRE_INNER); } return elemc; @@ -123,7 +125,8 @@ int draw_spider(int elemc, struct elem **elemv) int draw_numbers(int elemc, struct elem **elemv) { - elemv[elemc++] = gen_ring(DIAMETER/2 - WIRE_WIDTH*4, WIRE_WIDTH, "#ddd"); + elemv[elemc++] = gen_ring(DIAMETER/2 - WIRE_WIDTH*4, WIRE_WIDTH, + "#ddd"); int r = DIAMETER/2 - 33/2; for (int i = 0; i < 20; ++i) { @@ -136,7 +139,8 @@ int draw_numbers(int elemc, struct elem **elemv) char buf[512]; double a = 90 - i*SECTOR_WIDTH; if (a < 0) a += 360; - struct ccoords cc = pol_to_cart((struct pcoords){ .a = a, .r = r }); + struct ccoords cc = pol_to_cart( + (struct pcoords){ .a = a, .r = r }); sprintf(buf, "scale(1 -1) translate(%f %f) rotate(%f)", cc.x, -cc.y, a <= 180 ? 90 -a : 270 -a); elem_add_attr(e, "transform", buf); @@ -159,8 +163,10 @@ void svg_draw_board() for (int j = 0; j < NUM_SECTORS; ++j) { double a = 90 - j*SECTOR_WIDTH; if (a < 0) a += 360; - elemv[elemc++] = gen_segment(a, OUTER_DISTS[i-1], OUTER_DISTS[i], - i%2 ? (j%2 ? C_GREEN : C_RED) : (j%2 ? C_WHITE : C_BLACK)); + elemv[elemc++] = gen_segment(a, OUTER_DISTS[i-1], + OUTER_DISTS[i], + i % 2 ? (j % 2 ? C_GREEN : C_RED) : + (j % 2 ? C_WHITE : C_BLACK)); } } |