summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-24 19:58:04 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-24 19:58:43 -0700
commit9a02c0367351c311734f42ab2afe2b377ca12908 (patch)
tree845b652771bb82a7ffaef39919dc56f08e2f207b
parent14dd3ef4196af7c2269d80931dfcdcd1c3068857 (diff)
downloaddartboat-9a02c0367351c311734f42ab2afe2b377ca12908.tar.gz
dartboat-9a02c0367351c311734f42ab2afe2b377ca12908.tar.xz
display 2-dart checkout if different than 3-dart checkout
-rw-r--r--checkouts.c38
-rw-r--r--checkouts.h3
-rw-r--r--web/static/sw.js2
-rw-r--r--web/web_scoreboard.c6
4 files changed, 35 insertions, 14 deletions
diff --git a/checkouts.c b/checkouts.c
index a0bfaf2..0099aa5 100644
--- a/checkouts.c
+++ b/checkouts.c
@@ -4,25 +4,45 @@
#include <stdlib.h>
#include <string.h>
-char *checkouts_suggested(int rem)
+int checkouts_suggested(char *buf, int rem, int darts_in_hand)
{
+ buf[0] = 0;
if (rem < 2 || rem > 170)
- return NULL;
+ return 0;
- char *str = NULL, *p = NULL;
+ char *p = buf;
char *target;
- int i = 3, trem = rem;
+ int i = darts_in_hand, trem = rem;
while (i && trem && (target = CHECKOUTS[--i][trem - 1])) {
- if (i == 2)
- p = str = malloc(3 * SEGMENT_MAX_LEN);
- else
+ if (i < darts_in_hand - 1)
*p++ = '-';
p = stpcpy(p, target);
-
trem -= segment_points(segment_from_name(target));
}
- return str;
+ return p - buf;
+}
+
+#define CHECKOUT_2_PRE " ("
+#define CHECKOUT_2_POST ")"
+// buf must be at least 32 bytes
+int checkouts_suggested_3_2(char *buf, int rem)
+{
+ int len = checkouts_suggested(buf, rem, 3);
+ if (!len)
+ return 0;
+
+ char *buf2 = buf + len + sizeof(CHECKOUT_2_PRE) - 1;
+ int len2 = checkouts_suggested(buf2, rem, 2);
+ if (!len2 ||
+ (len == len2 && !strncmp(buf, buf2, len > len2 ? len : len2)))
+ return len;
+
+ memcpy(buf + len, CHECKOUT_2_PRE, sizeof(CHECKOUT_2_PRE) - 1);
+ memcpy(buf2 + len2, CHECKOUT_2_POST, sizeof(CHECKOUT_2_POST));
+ len += sizeof(CHECKOUT_2_PRE) + len2 + sizeof(CHECKOUT_2_POST) - 2;
+
+ return len;
}
char *CHECKOUTS[][170] = {
diff --git a/checkouts.h b/checkouts.h
index 5d0e657..c4ce8db 100644
--- a/checkouts.h
+++ b/checkouts.h
@@ -4,6 +4,7 @@
extern char *CHECKOUTS[3][170];
extern char *SETUP_SHOTS[191];
-char *checkouts_suggested(int rem);
+int checkouts_suggested(char *buf, int rem, int darts_in_hand);
+int checkouts_suggested_3_2(char *buf, int rem);
#endif
diff --git a/web/static/sw.js b/web/static/sw.js
index f09f5fc..33f3c0a 100644
--- a/web/static/sw.js
+++ b/web/static/sw.js
@@ -1,5 +1,5 @@
const CACHE_PREFIX = 'dartboat-'
-const CACHE_VERSION = '24';
+const CACHE_VERSION = '25';
const CACHE_NAME = `${CACHE_PREFIX}${CACHE_VERSION}`;
const CACHE_FILES = [
diff --git a/web/web_scoreboard.c b/web/web_scoreboard.c
index 35af7f4..2e998cc 100644
--- a/web/web_scoreboard.c
+++ b/web/web_scoreboard.c
@@ -176,9 +176,9 @@ void update_player_name(int pn, char *str)
void update_player_sugg(int pn, int rem)
{
- char *sugg = checkouts_suggested(rem);
- scoreboard_set_player_sugg(pn, sugg ? sugg : "");
- free(sugg);
+ char buf[32];
+ checkouts_suggested_3_2(buf, rem);
+ scoreboard_set_player_sugg(pn, buf);
}
static double calc_avg(struct leg *l, int n_darts) {