summaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
Diffstat (limited to 'web')
-rw-r--r--web/web_misc.c29
-rw-r--r--web/web_misc.h14
-rw-r--r--web/web_prompt.c58
-rw-r--r--web/web_scoreboard.c69
4 files changed, 74 insertions, 96 deletions
diff --git a/web/web_misc.c b/web/web_misc.c
new file mode 100644
index 0000000..a754f9e
--- /dev/null
+++ b/web/web_misc.c
@@ -0,0 +1,29 @@
+#include "web_misc.h"
+
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+
+void bufstr_buf(bufstr *b, char *str)
+{
+ if (str == b->buf || (str && b->buf && !strcmp(str, b->buf)))
+ return;
+
+ if (b->buf != b->fl)
+ free(b->buf);
+ b->buf = str ? strdup(str) : NULL;
+}
+
+bool bufstr_changed(bufstr *b)
+{
+ return (!b->buf != !b->fl) ||
+ (b->buf != b->fl && strcmp(b->buf, b->fl));
+}
+
+char *bufstr_flush(bufstr *b)
+{
+ if (b->fl != b->buf)
+ free(b->fl);
+ b->fl = b->buf;
+ return b->fl;
+}
diff --git a/web/web_misc.h b/web/web_misc.h
new file mode 100644
index 0000000..9872181
--- /dev/null
+++ b/web/web_misc.h
@@ -0,0 +1,14 @@
+#ifndef WEB_MISC_H
+#define WEB_MISC_H
+
+#include <stdbool.h>
+
+typedef struct bufstr {
+ char *fl, *buf;
+} bufstr;
+
+void bufstr_buf(bufstr *b, char *str);
+bool bufstr_changed(bufstr *b);
+char *bufstr_flush(bufstr *b);
+
+#endif
diff --git a/web/web_prompt.c b/web/web_prompt.c
index 0278817..2ceb2d4 100644
--- a/web/web_prompt.c
+++ b/web/web_prompt.c
@@ -2,6 +2,7 @@
#include "web_dom.h"
#include "web_prompt.h"
#include "web_match.h"
+#include "web_misc.h"
#include "web_scoreboard.h"
#include <stdio.h>
@@ -12,14 +13,12 @@
int oi_timeout;
-struct prompt_state {
- char *msgl, *input, *msgr;
-};
-
-struct prompt_state prompt_buffered, prompt_flushed;
-
enum prompt_mode pm;
+static struct {
+ bufstr msgl, msgr, input;
+} bufs;
+
void oi()
{
dom_add_class("#oi", "visible");
@@ -37,58 +36,29 @@ void clear_oi()
oi_timeout = 0;
}
-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);
+ bufstr_buf(&bufs.msgl, str);
}
void prompt_set_msgr(char *str)
{
- buffer_str(str, &prompt_buffered.msgr, prompt_flushed.msgr);
+ bufstr_buf(&bufs.msgr, str);
}
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);
+ bufstr_buf(&bufs.input, str);
}
void prompt_flush()
{
- if (buffered_str_changed(prompt_buffered.msgl, prompt_flushed.msgl))
- dom_set_content("#prompt-msg-l", prompt_buffered.msgl);
- if (buffered_str_changed(prompt_buffered.msgr, prompt_flushed.msgr))
- dom_set_content("#prompt-msg-r", prompt_buffered.msgr);
- if (buffered_str_changed(prompt_buffered.input, prompt_flushed.input))
- dom_set_content("#prompt-input", 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;
+ if (bufstr_changed(&bufs.msgl))
+ dom_set_content("#prompt-msg-l", bufstr_flush(&bufs.msgl));
+ if (bufstr_changed(&bufs.msgr))
+ dom_set_content("#prompt-msg-r", bufstr_flush(&bufs.msgr));
+ if (bufstr_changed(&bufs.input))
+ dom_set_content("#prompt-input", bufstr_flush(&bufs.input));
}
void set_prompt_mode(enum prompt_mode mode)
diff --git a/web/web_scoreboard.c b/web/web_scoreboard.c
index 71b239d..d0a5947 100644
--- a/web/web_scoreboard.c
+++ b/web/web_scoreboard.c
@@ -1,5 +1,6 @@
#include "web_dom.h"
#include "web_match.h"
+#include "web_misc.h"
#include "web_scoreboard.h"
#include "checkouts.h"
@@ -12,11 +13,9 @@
#include <emscripten/emscripten.h>
-struct player_info {
- char *name, *rem, *sugg, *avg;
-};
-
-struct player_info buffered_info[2], flushed_info[2];
+static struct {
+ bufstr name, rem, sugg, avg;
+} bufs[2];
void scoreboard_set_player_active(int pn)
{
@@ -30,86 +29,52 @@ void scoreboard_set_player_active(int pn)
dom_set_uniq_class(psel, "active", "[id$=-info]");
}
-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 scoreboard_set_player_name(int pn, char *str)
{
- buffer_str(str, &buffered_info[pn-1].name, flushed_info[pn-1].name);
+ bufstr_buf(&bufs[pn-1].name, str);
}
void scoreboard_set_player_rem(int pn, char *str)
{
- buffer_str(str, &buffered_info[pn-1].rem, flushed_info[pn-1].rem);
+ bufstr_buf(&bufs[pn-1].rem, str);
}
void scoreboard_set_player_sugg(int pn, char *str)
{
- buffer_str(str, &buffered_info[pn-1].sugg, flushed_info[pn-1].sugg);
+ bufstr_buf(&bufs[pn-1].sugg, str);
}
void scoreboard_set_player_avg(int pn, double avg)
{
char *str = malloc(8);
sprintf(str, "%.2f", avg);
- buffer_str(str, &buffered_info[pn-1].avg, flushed_info[pn-1].avg);
-}
-
-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);
+ bufstr_buf(&bufs[pn-1].avg, str);
}
void scoreboard_flush_player_info(int pn)
{
- char sel[64];
+ char sel[32];
int len = sprintf(sel, "#p%d-", pn);
- if (buffered_str_changed(buffered_info[pn-1].name,
- flushed_info[pn-1].name)) {
+ if (bufstr_changed(&bufs[pn-1].name)) {
strcpy(sel + len, "name");
- dom_set_content(sel, buffered_info[pn-1].name);
+ dom_set_content(sel, bufstr_flush(&bufs[pn-1].name));
}
- if (buffered_str_changed(buffered_info[pn-1].rem,
- flushed_info[pn-1].rem)) {
+ if (bufstr_changed(&bufs[pn-1].rem)) {
strcpy(sel + len, "rem");
- dom_set_content(sel, buffered_info[pn-1].rem);
+ dom_set_content(sel, bufstr_flush(&bufs[pn-1].rem));
}
- if (buffered_str_changed(buffered_info[pn-1].sugg,
- flushed_info[pn-1].sugg)) {
+ if (bufstr_changed(&bufs[pn-1].sugg)) {
strcpy(sel + len, "sugg");
- dom_set_content(sel, buffered_info[pn-1].sugg);
+ dom_set_content(sel, bufstr_flush(&bufs[pn-1].sugg));
}
- if (buffered_str_changed(buffered_info[pn-1].avg,
- flushed_info[pn-1].avg)) {
+ if (bufstr_changed(&bufs[pn-1].avg)) {
strcpy(sel + len, "avg");
- dom_set_content(sel, buffered_info[pn-1].avg);
+ dom_set_content(sel, bufstr_flush(&bufs[pn-1].avg));
}
-
- free_flushed_str(flushed_info[pn-1].name, buffered_info[pn-1].name);
- free_flushed_str(flushed_info[pn-1].rem, buffered_info[pn-1].rem);
- free_flushed_str(flushed_info[pn-1].sugg, buffered_info[pn-1].sugg);
- free_flushed_str(flushed_info[pn-1].avg, buffered_info[pn-1].avg);
-
- flushed_info[pn-1] = buffered_info[pn-1];
}
void scoreboard_flush()