diff options
-rw-r--r-- | checkouts.c | 30 | ||||
-rw-r--r-- | checkouts.h | 8 | ||||
-rw-r--r-- | web/web_scoreboard.c | 15 |
3 files changed, 32 insertions, 21 deletions
diff --git a/checkouts.c b/checkouts.c index 5062ecf..a0bfaf2 100644 --- a/checkouts.c +++ b/checkouts.c @@ -1,7 +1,29 @@ -#ifndef CHECKOUT_H -#define CHECKOUT_H +#include "board.h" +#include "checkouts.h" -#include <stddef.h> +#include <stdlib.h> +#include <string.h> + +char *checkouts_suggested(int rem) +{ + if (rem < 2 || rem > 170) + return NULL; + + char *str = NULL, *p = NULL; + char *target; + int i = 3, trem = rem; + while (i && trem && (target = CHECKOUTS[--i][trem - 1])) { + if (i == 2) + p = str = malloc(3 * SEGMENT_MAX_LEN); + else + *p++ = '-'; + p = stpcpy(p, target); + + trem -= segment_points(segment_from_name(target)); + } + + return str; +} char *CHECKOUTS[][170] = { { @@ -715,5 +737,3 @@ char *SETUP_SHOTS[191] = { "T20", // 190 "T19", // 191 }; - -#endif diff --git a/checkouts.h b/checkouts.h index 870f24f..5d0e657 100644 --- a/checkouts.h +++ b/checkouts.h @@ -1,9 +1,9 @@ -#ifndef CHECKOUT_H -#define CHECKOUT_H - -#include <stddef.h> +#ifndef CHECKOUTS_H +#define CHECKOUTS_H extern char *CHECKOUTS[3][170]; extern char *SETUP_SHOTS[191]; +char *checkouts_suggested(int rem); + #endif diff --git a/web/web_scoreboard.c b/web/web_scoreboard.c index 657c326..35af7f4 100644 --- a/web/web_scoreboard.c +++ b/web/web_scoreboard.c @@ -176,18 +176,9 @@ void update_player_name(int pn, char *str) void update_player_sugg(int pn, int rem) { - char str[15] = {0}, *p = str; - - if (rem > 1 && rem <= 170) { - char *target; - int i = 3, trem = rem; - while (i && trem && (target = CHECKOUTS[--i][trem-1])) { - p += sprintf(p, i == 2 ? "%s" : "-%s", target); - trem -= segment_points(segment_from_name(target)); - } - } - - scoreboard_set_player_sugg(pn, str); + char *sugg = checkouts_suggested(rem); + scoreboard_set_player_sugg(pn, sugg ? sugg : ""); + free(sugg); } static double calc_avg(struct leg *l, int n_darts) { |