summaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-03 07:54:36 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-03 07:54:36 -0700
commitd9add805c3b556d2fa357713bda577f4dc6f2d49 (patch)
tree2f55e9d07c7e1cdee267a414ccd7c7b302a581ea /web
parent50f5ce01c543ef2e4d780f3aed8a565b142865ea (diff)
downloaddartboat-d9add805c3b556d2fa357713bda577f4dc6f2d49.tar.gz
dartboat-d9add805c3b556d2fa357713bda577f4dc6f2d49.tar.xz
web: buffer and flush prompt
Diffstat (limited to 'web')
-rw-r--r--web/static/dartboat.js18
-rw-r--r--web/web_control.c10
-rw-r--r--web/web_prompt.c88
-rw-r--r--web/web_prompt.h5
4 files changed, 95 insertions, 26 deletions
diff --git a/web/static/dartboat.js b/web/static/dartboat.js
index 719bd8d..359abbd 100644
--- a/web/static/dartboat.js
+++ b/web/static/dartboat.js
@@ -50,16 +50,16 @@ function setPromptInactive() {
$('#prompt').classList.remove('active')
}
-function setPromptInput(ptr) {
- $('#prompt-input').textContent = UTF8ToString(ptr);
+function setPromptMsgL(ptr) {
+ $('#prompt-msg-l').textContent = ptr ? UTF8ToString(ptr) : '';
}
-function promptMsgL(ptr) {
- $('#prompt-msg-l').textContent = UTF8ToString(ptr);
+function setPromptMsgR(ptr) {
+ $('#prompt-msg-r').textContent = ptr ? UTF8ToString(ptr) : '';
}
-function promptMsgR(ptr) {
- $('#prompt-msg-r').textContent = UTF8ToString(ptr);
+function setPromptInput(ptr) {
+ $('#prompt-input').textContent = ptr ? UTF8ToString(ptr) : '';
}
function setKeypad(keypad) {
@@ -98,15 +98,15 @@ function hidePlayerInfo(n) {
}
function updatePlayerName(n, ptr) {
- $(`#p${n}-name`).textContent = ptr && UTF8ToString(ptr);
+ $(`#p${n}-name`).textContent = ptr ? UTF8ToString(ptr) : '';
}
function updatePlayerRem(n, ptr) {
- $(`#p${n}-rem`).textContent = ptr && UTF8ToString(ptr);
+ $(`#p${n}-rem`).textContent = ptr ? UTF8ToString(ptr) : '';
}
function updatePlayerSugg(n, ptr) {
- $(`#p${n}-sugg`).textContent = ptr && UTF8ToString(ptr);
+ $(`#p${n}-sugg`).textContent = ptr ? UTF8ToString(ptr) : '';
}
function updatePlayerAvg(n, avg) {
diff --git a/web/web_control.c b/web/web_control.c
index 3891750..25f2ccb 100644
--- a/web/web_control.c
+++ b/web/web_control.c
@@ -43,8 +43,8 @@ void end_boat_visit(int rem, double avg)
EM_ASM(svgClearPoints());
update_player_rem(2, rem);
scoreboard_set_player_avg(2, avg);
- EM_ASM({setPromptInput($0)}, "");
- EM_ASM({promptMsgR($0)}, "");
+ prompt_set_input(NULL);
+ prompt_set_msgr(NULL);
draw_visits();
handle_next();
@@ -62,10 +62,11 @@ void draw_boat_throwing(int pts, char *str, double x, double y)
svg_draw_point(x, y);
update_player_rem(2, rem - pts);
- EM_ASM({setPromptInput($0)}, pts_str);
- EM_ASM({promptMsgR($0)}, str);
+ prompt_set_input(pts_str);
+ prompt_set_msgr(str);
free(str);
+ prompt_flush();
scoreboard_flush_player_info(2); // FIXME bot hard-coded as player 2
}
@@ -136,6 +137,7 @@ void handle_next()
}
}
+ prompt_flush();
scoreboard_flush();
}
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();
}
diff --git a/web/web_prompt.h b/web/web_prompt.h
index 5afbaef..8758000 100644
--- a/web/web_prompt.h
+++ b/web/web_prompt.h
@@ -11,6 +11,11 @@ enum prompt_mode {
extern enum prompt_mode pm;
+void prompt_set_msgl(char *str);
+void prompt_set_msgr(char *str);
+void prompt_set_input(char *str);
+void prompt_flush();
+
void prompt_visit();
void prompt_bot_visit();
void prompt_num_darts();