summaryrefslogtreecommitdiff
path: root/web/web_scoreboard.c
diff options
context:
space:
mode:
Diffstat (limited to 'web/web_scoreboard.c')
-rw-r--r--web/web_scoreboard.c128
1 files changed, 118 insertions, 10 deletions
diff --git a/web/web_scoreboard.c b/web/web_scoreboard.c
index fa1a672..79bddb2 100644
--- a/web/web_scoreboard.c
+++ b/web/web_scoreboard.c
@@ -15,20 +15,106 @@
static struct {
bufstr name, rem, sugg, avg;
-} bufs[2];
+} bufs[4];
-void scoreboard_set_player_active(int pn)
+#define NUM_SLOTS 2
+static int slots[NUM_SLOTS];
+static int active_pn = -1;
+
+static void flush_slot(int slot)
+{
+ int pn = slots[slot - 1];
+ if (pn == -1) return;
+
+ // FIXME: should be combined with flushing player info
+ char sel[32];
+ int len = sprintf(sel, "#p%d-", slot);
+ strcpy(sel + len, "name");
+ dom_set_content(sel, bufstr_flush(&bufs[pn-1].name));
+ strcpy(sel + len, "rem");
+ dom_set_content(sel, bufstr_flush(&bufs[pn-1].rem));
+ strcpy(sel + len, "sugg");
+ dom_set_content(sel, bufstr_flush(&bufs[pn-1].sugg));
+ strcpy(sel + len, "avg");
+ dom_set_content(sel, bufstr_flush(&bufs[pn-1].avg));
+}
+
+static int player_slot(int pn)
+{
+ for (int i = 0; i < NUM_SLOTS; ++i) {
+ if (slots[i] == pn)
+ return i + 1;
+ }
+
+ return -1;
+}
+
+static void set_slot_player(int slot, int pn)
+{
+ if (slots[slot - 1] != pn) {
+ slots[slot - 1] = pn;
+ flush_slot(slot);
+ }
+}
+
+static void set_slot_active(int slot)
{
char buf[64], *sel = buf;
- if (pn == -1)
+ if (slot == -1)
sel = NULL;
else
- sprintf(sel, "#p%d-info", pn);
+ sprintf(sel, "#p%d-info", slot);
dom_set_uniq_class(sel, "active", "[id$=-info]");
}
+EMSCRIPTEN_KEEPALIVE
+void scoreboard_prev_slot()
+{
+ set_slot_player(2, slots[0]);
+ set_slot_player(1, slots[1] == 1 ? match_num_players() : slots[1] - 1);
+ set_slot_active(player_slot(active_pn));
+}
+
+EMSCRIPTEN_KEEPALIVE
+void scoreboard_next_slot()
+{
+ set_slot_player(1, slots[1]);
+ set_slot_player(2, slots[0] == match_num_players() ? 1 : slots[0] + 1);
+ set_slot_active(player_slot(active_pn));
+}
+
+void scoreboard_set_player_active(int pn)
+{
+ active_pn = pn;
+
+ if (pn == -1) {
+ set_slot_active(-1);
+ return;
+ }
+
+ int np = match_num_players();
+ int slot1, slot2, pslot;
+ if (np == 1) {
+ slot1 = 1;
+ slot2 = -1;
+ pslot = 1;
+ } else if (np == 2) {
+ slot1 = 1;
+ slot2 = 2;
+ pslot = pn == 1 ? 1 : 2;
+ } else {
+ slot1 = pn;
+ slot2 = pn == np ? 1 : pn + 1;
+ pslot = 1;
+ }
+
+ set_slot_player(1, slot1);
+ set_slot_player(2, slot2);
+ set_slot_active(pslot);
+}
+
void scoreboard_set_player_name(int pn, char *str)
{
bufstr_buf(&bufs[pn-1].name, str);
@@ -53,8 +139,11 @@ void scoreboard_set_player_avg(int pn, double avg)
void scoreboard_flush_player_info(int pn)
{
+ int slot = player_slot(pn);
+ if (slot == -1) return;
+
char sel[32];
- int len = sprintf(sel, "#p%d-", pn);
+ int len = sprintf(sel, "#p%d-", slot);
if (bufstr_changed(&bufs[pn-1].name)) {
strcpy(sel + len, "name");
@@ -133,14 +222,23 @@ void update_player_rem(int pn, int rem)
update_player_sugg(pn, rem);
}
-void show_player_info(int pn)
+void scoreboard_show_info(int num_players)
{
- char sel[64];
- sprintf(sel, "#p%d-info-inner", pn);
- dom_add_class(sel, "visible");
+ set_slot_player(1, 1);
+ dom_add_class("#p1-info-inner", "visible");
+
+ if (num_players > 1) {
+ set_slot_player(2, 2);
+ dom_add_class("#p2-info-inner", "visible");
+ }
+
+ if (num_players > 2) {
+ dom_add_class("#info-slot-prev", "visible");
+ dom_add_class("#info-slot-next", "visible");
+ }
}
-void clear_player_info(int pn)
+static void clear_player_info(int pn)
{
scoreboard_set_player_name(pn, NULL);
scoreboard_set_player_rem(pn, NULL);
@@ -148,6 +246,16 @@ void clear_player_info(int pn)
scoreboard_set_player_avg(pn, 0);
}
+void scoreboard_hide_info()
+{
+ dom_set_uniq_class(NULL, "visible", "[id$=-info-inner]");
+ for (int pn = 1; pn < 4; ++pn) // FIXME
+ clear_player_info(pn);
+
+ dom_remove_class("#info-slot-prev", "visible");
+ dom_remove_class("#info-slot-next", "visible");
+}
+
static struct dom_elem *create_div(char *str, char *class)
{
struct dom_elem *e = dom_elem_init(NULL, "div", 1);