summaryrefslogtreecommitdiff
path: root/web/web_prompt.c
blob: abc5a05a1c95a871ce133a3b4a32c4d74ed215fe (plain) (blame)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "web_control.h"
#include "web_prompt.h"
#include "web_match.h"
#include "web_ui.h"

#include <stdlib.h>
#include <string.h>

#include <emscripten/emscripten.h>

enum prompt_mode pm;

void set_prompt_mode(enum prompt_mode mode)
{
	pm = mode;

	if (pm != PM_DARTBOARD)
		EM_ASM(setPromptActive());
	else
		EM_ASM(setPromptInactive());

	EM_ASM({setKeypad($0)}, pm == PM_DARTBOARD ? "dartboard" :
		pm == PM_SELECT_MODE ? "select_mode" : "default");
}

void prompt_num_darts()
{
	set_prompt_mode(PM_NUM_DARTS);
	EM_ASM({promptMsgL($0)}, "Darts needed?");
	EM_ASM({promptMsgR($0)}, "");
	EM_ASM({setKeyLabel($0, $1)}, "submit", "OK");
	EM_ASM({setKeyLabel($0, $1)}, "rem", "REMAINING");
}

void prompt_end_match()
{
	EM_ASM(setPlayerActive()); // sets all inactive
	set_prompt_mode(PM_END_MATCH);
	EM_ASM({promptMsgL($0)},
		state->mode == M_PVC && state->legs[1]->rem <= 0 ? "Bot wins. :(" :
			"You win! :)");
	EM_ASM({promptMsgR($0)}, "");
	EM_ASM({setKeyLabel($0, $1)}, "submit", "END MATCH");
	EM_ASM({setKeyLabel($0, $1)}, "rem", "REMATCH");
}

void prompt_select_mode()
{
	for (int pn = 1; pn < 3; ++pn) {
		EM_ASM({hidePlayerInfo($0)}, pn);
		clear_player_info(pn);
	}
	EM_ASM(clearVisits());

	set_prompt_mode(PM_SELECT_MODE);
	EM_ASM({promptMsgL($0)}, "Select match mode:");
	EM_ASM({promptMsgR($0)}, "");
}

char *prompt_get()
{
	return (char *)EM_ASM_INT({return promptGet()});
}

void prompt_handle_pre(char *command)
{
	if (pm == PM_DARTBOARD)
		return;

	EM_ASM(clearOi());

	if ((pm == PM_VISIT || pm == PM_NUM_DARTS || pm == PM_END_MATCH) &&
		strcmp(command, "undo"))
		deactivate_key("undo");

	if (pm == PM_END_MATCH && strcmp(command, "rem"))
		deactivate_key("rem");

	if (pm == PM_END_MATCH && strcmp(command, "submit"))
		deactivate_key("submit");
}

void prompt_handle_on_change()
{
	if (pm != PM_VISIT)
		return;

	char *str = prompt_get();
	update_user_rem_from_pts(atoi(str));
	free(str);
}

void prompt_handle_append(char *data)
{
	if (pm != PM_SELECT_MODE && pm != PM_VISIT && pm != PM_NUM_DARTS)
		return;

	char *str = prompt_get();
	size_t len_str = strlen(str);
	size_t len_data = strlen(data);
	if (len_str < 3) {
		str = realloc(str, len_str + len_data + 1);
		memcpy(str + len_str, data, len_data + 1);
		EM_ASM({setPromptInput($0)}, str);
		prompt_handle_on_change();
	}
	free(str);
}

void prompt_handle_backspace()
{
	if (pm == PM_DARTBOARD)
		return;

	char *str = prompt_get();
	size_t len_str = strlen(str);
	if (len_str > 0) {
		str[len_str-1] = 0;
		EM_ASM({setPromptInput($0)}, str);
		prompt_handle_on_change();
	}
	free(str);
}

void prompt_handle_clear()
{
	if (pm == PM_DARTBOARD)
		return;

	EM_ASM({setPromptInput($0)}, "");
	prompt_handle_on_change();
}

void prompt_handle_submit()
{
	if (pm == PM_END_MATCH) {
		if (is_key_active("submit"))
			end_match();
		toggle_key("submit");
		return;
	}

	if (pm != PM_VISIT && pm != PM_NUM_DARTS && pm != PM_SELECT_MODE)
		return;

	char *str = prompt_get();
	prompt_handle_clear();
	if (*str) {
		if (pm == PM_VISIT)
			user_visit(atoi(str));
		else if (pm == PM_NUM_DARTS)
			user_num_darts(atoi(str));
		else if (pm == PM_SELECT_MODE)
			start_match(atoi(str));
	}

	free(str);
}

void prompt_handle_rem()
{
	if (pm == PM_END_MATCH) {
		if (is_key_active("rem"))
			start_match(state->mode);
		toggle_key("rem");
		return;
	}

	if (pm != PM_VISIT)
		return;

	char *str = prompt_get();
	prompt_handle_clear();
	if (*str)
		user_visit_to_rem(atoi(str));
	free(str);
}

void prompt_handle_undo()
{
	if (pm != PM_VISIT && pm != PM_NUM_DARTS && pm != PM_END_MATCH)
		return;

	prompt_handle_clear();
	if (is_key_active("undo"))
		user_undo();
	toggle_key("undo");
}

EMSCRIPTEN_KEEPALIVE
void prompt_handle(char *command, char *data)
{
	prompt_handle_pre(command);

	if (!strcmp(command, "append"))
		prompt_handle_append(data);
	else if (!strcmp(command, "backspace"))
		prompt_handle_backspace();
	else if (!strcmp(command, "clear"))
		prompt_handle_clear();
	else if (!strcmp(command, "submit"))
		prompt_handle_submit();
	else if (!strcmp(command, "rem"))
		prompt_handle_rem();
	else if (!strcmp(command, "undo"))
		prompt_handle_undo();
}