1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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);
}
|