summaryrefslogtreecommitdiff
path: root/web/web_prompt.c
diff options
context:
space:
mode:
Diffstat (limited to 'web/web_prompt.c')
-rw-r--r--web/web_prompt.c88
1 files changed, 75 insertions, 13 deletions
diff --git a/web/web_prompt.c b/web/web_prompt.c
index 69f4705..201bd7b 100644
--- a/web/web_prompt.c
+++ b/web/web_prompt.c
@@ -8,8 +8,68 @@
#include <emscripten/emscripten.h>
+struct prompt_state {
+ char *msgl, *input, *msgr;
+};
+
+struct prompt_state prompt_buffered, prompt_flushed;
+
enum prompt_mode pm;
+static inline void buffer_str(char *str, char **buffer, char *flushed)
+{
+ if (str == *buffer || (str && *buffer && !strcmp(str, *buffer)))
+ return;
+
+ if (*buffer && *buffer != flushed)
+ free(*buffer);
+
+ *buffer = str ? strdup(str) : NULL;
+}
+
+void prompt_set_msgl(char *str)
+{
+ buffer_str(str, &prompt_buffered.msgl, prompt_flushed.msgl);
+}
+
+void prompt_set_msgr(char *str)
+{
+ buffer_str(str, &prompt_buffered.msgr, prompt_flushed.msgr);
+}
+
+void prompt_set_input(char *str)
+{
+ buffer_str(str, &prompt_buffered.input, prompt_flushed.input);
+}
+
+static inline bool buffered_str_changed(char *buffered, char *flushed)
+{
+ return (!buffered != !flushed) ||
+ (buffered && flushed && strcmp(buffered, flushed));
+}
+
+static inline void free_flushed_str(char *flushed, char *buffered)
+{
+ if (flushed && flushed != buffered)
+ free(flushed);
+}
+
+void prompt_flush()
+{
+ if (buffered_str_changed(prompt_buffered.msgl, prompt_flushed.msgl))
+ EM_ASM({setPromptMsgL($0)}, prompt_buffered.msgl);
+ if (buffered_str_changed(prompt_buffered.msgr, prompt_flushed.msgr))
+ EM_ASM({setPromptMsgR($0)}, prompt_buffered.msgr);
+ if (buffered_str_changed(prompt_buffered.input, prompt_flushed.input))
+ EM_ASM({setPromptInput($0)}, prompt_buffered.input);
+
+ free_flushed_str(prompt_flushed.msgl, prompt_buffered.msgl);
+ free_flushed_str(prompt_flushed.msgr, prompt_buffered.msgr);
+ free_flushed_str(prompt_flushed.input, prompt_buffered.input);
+
+ prompt_flushed = prompt_buffered;
+}
+
void set_prompt_mode(enum prompt_mode mode)
{
pm = mode;
@@ -26,8 +86,8 @@ void set_prompt_mode(enum prompt_mode mode)
void prompt_visit()
{
set_prompt_mode(PM_VISIT);
- EM_ASM({promptMsgL($0)}, "Enter points:");
- EM_ASM({promptMsgR($0)}, "");
+ prompt_set_msgl("Enter points:");
+ prompt_set_msgr(NULL);
EM_ASM({setKeyLabel($0, $1)}, "submit", "OK");
EM_ASM({setKeyLabel($0, $1)}, "rem", "REMAINING");
}
@@ -35,15 +95,15 @@ void prompt_visit()
void prompt_bot_visit()
{
set_prompt_mode(PM_DARTBOARD);
- EM_ASM({promptMsgL($0)}, "Bot is throwing…");
- EM_ASM({promptMsgR($0)}, "");
+ prompt_set_msgl("Bot is throwing…");
+ prompt_set_msgr(NULL);
}
void prompt_num_darts()
{
set_prompt_mode(PM_NUM_DARTS);
- EM_ASM({promptMsgL($0)}, "Darts needed?");
- EM_ASM({promptMsgR($0)}, "");
+ prompt_set_msgl("Darts needed?");
+ prompt_set_msgr(NULL);
EM_ASM({setKeyLabel($0, $1)}, "submit", "OK");
EM_ASM({setKeyLabel($0, $1)}, "rem", "REMAINING");
}
@@ -52,10 +112,10 @@ void prompt_end_match()
{
EM_ASM(setPlayerActive()); // sets all inactive
set_prompt_mode(PM_END_MATCH);
- EM_ASM({promptMsgL($0)},
+ prompt_set_msgl(
state->mode == M_PVC && state->legs[1]->rem <= 0 ? "Bot wins. :(" :
"You win! :)");
- EM_ASM({promptMsgR($0)}, "");
+ prompt_set_msgr(NULL);
EM_ASM({setKeyLabel($0, $1)}, "submit", "END MATCH");
EM_ASM({setKeyLabel($0, $1)}, "rem", "REMATCH");
}
@@ -69,8 +129,8 @@ void prompt_select_mode()
EM_ASM(clearVisits());
set_prompt_mode(PM_SELECT_MODE);
- EM_ASM({promptMsgL($0)}, "Select match mode:");
- EM_ASM({promptMsgR($0)}, "");
+ prompt_set_msgl("Select match mode:");
+ prompt_set_msgr(NULL);
}
bool is_key_active(char *k)
@@ -113,6 +173,8 @@ void prompt_handle_pre(char *command)
void prompt_handle_on_change()
{
+ prompt_flush();
+
if (pm != PM_VISIT)
return;
@@ -132,7 +194,7 @@ void prompt_handle_append(char *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_set_input(str);
prompt_handle_on_change();
}
free(str);
@@ -147,7 +209,7 @@ void prompt_handle_backspace()
size_t len_str = strlen(str);
if (len_str > 0) {
str[len_str-1] = 0;
- EM_ASM({setPromptInput($0)}, str);
+ prompt_set_input(str);
prompt_handle_on_change();
}
free(str);
@@ -158,7 +220,7 @@ void prompt_handle_clear()
if (pm == PM_DARTBOARD)
return;
- EM_ASM({setPromptInput($0)}, "");
+ prompt_set_input(NULL);
prompt_handle_on_change();
}