From 4bec4883792f86cd835936113a9d39bebcf0344f Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Sun, 15 May 2022 02:32:08 -0700 Subject: web: implement difficulty abstraction for stdev --- web/static/index.html | 6 ++--- web/static/style.css | 4 +++ web/static/sw.js | 2 +- web/web_opts.c | 71 ++++++++++++++++++++++++++++++++++++--------------- 4 files changed, 59 insertions(+), 24 deletions(-) (limited to 'web') diff --git a/web/static/index.html b/web/static/index.html index 2ed654b..472247d 100644 --- a/web/static/index.html +++ b/web/static/index.html @@ -17,7 +17,7 @@
dartboat™
-
+
@@ -75,8 +75,8 @@

dartboat

dartboat uses an internal representation of a specification dartboard. Darts are thrown following a normal distribution, with the resultant coordinates used to calculate the segments in which they land. The idea is that this provides a more realistic opponent than picking points at random.

Settings

-

(delay) — milliseconds it takes the computer to throw each dart.

-

(stdev) — the standard deviation of the computer's throws in millimetres. A value of 24 translates to a three-dart average of roughly 35. A value of 13 would be a 65 average, and a value of 8 a 95 average.

+

(delay: 0-9999) — milliseconds it takes the computer to throw each dart.

+

(difficulty: 0-99) — precision of the computer's throws. Starting with a three-dart average of 10 points at difficulty 0, every difficulty increase of 8 corresponds to an average points increase of 10, e.g. difficulty 32 would be a 50-point average. (The rate of increase quickly increases upon reaching an average of 120 points.)

Interface

To avoid destructive actions being a misclick away, certain actions require two presses—one to activate the button and another to trigger it.

The controls are designed to be keyboard-friendly. The keys should be fairly intuitive for the most part.

diff --git a/web/static/style.css b/web/static/style.css index 852cffd..f0f4b2f 100644 --- a/web/static/style.css +++ b/web/static/style.css @@ -144,6 +144,10 @@ body { border: 0; } +#titlebar input#difficulty { + width: 2ch; +} + #titlebar div.button { background-color: #3d2466; font-weight: 700; diff --git a/web/static/sw.js b/web/static/sw.js index bf1d818..7576286 100644 --- a/web/static/sw.js +++ b/web/static/sw.js @@ -1,5 +1,5 @@ const CACHE_PREFIX = 'dartboat-' -const CACHE_VERSION = '5'; +const CACHE_VERSION = '6'; const CACHE_NAME = `${CACHE_PREFIX}${CACHE_VERSION}`; const CACHE_FILES = [ diff --git a/web/web_opts.c b/web/web_opts.c index 553e05d..e1c5eb5 100644 --- a/web/web_opts.c +++ b/web/web_opts.c @@ -13,17 +13,53 @@ #define PREFIX "dartboat_" int delay_ms = 1000; +static int difficulty = 32; + +struct { int diff; double stdev; } DIFF_PRESETS[] = { + { 0, 66.80}, // 10 + { 8, 40.24}, // 20 + {16, 28.14}, // 30 + {24, 21.35}, // 40 + {32, 17.20}, // 50 + {40, 14.34}, // 60 + {48, 12.15}, // 70 + {56, 10.36}, // 80 + {64, 8.83}, // 90 + {72, 7.48}, // 100 + {80, 6.32}, // 110 + {88, 5.34}, // 120 + {99, 0.00}, // 167 +}; +#define NUM_DIFF_PRESETS (sizeof(DIFF_PRESETS) / sizeof(*DIFF_PRESETS)) void set_delay(char *val) { delay_ms = atoi(val); } -void set_stdev(char *val) +void set_stdev(int diff) { - double stdev = strtod(val, NULL); - if (isfinite(stdev)) - horizontal_stdev = vertical_stdev = stdev; + if (diff < DIFF_PRESETS[0].diff) + diff = DIFF_PRESETS[0].diff; + else if (diff > DIFF_PRESETS[NUM_DIFF_PRESETS - 1].diff) + diff = DIFF_PRESETS[NUM_DIFF_PRESETS - 1].diff; + + size_t ind = 1; + while (ind < NUM_DIFF_PRESETS - 1 && DIFF_PRESETS[ind].diff <= diff) + ind++; + + double step = (DIFF_PRESETS[ind - 1].stdev - DIFF_PRESETS[ind].stdev) / + (DIFF_PRESETS[ind].diff - DIFF_PRESETS[ind - 1].diff); + horizontal_stdev = vertical_stdev = DIFF_PRESETS[ind - 1].stdev - + ((diff - DIFF_PRESETS[ind - 1].diff) * step); +} + +void set_difficulty(char *val) +{ + int diff = atoi(val); + difficulty = diff < 0 ? 0 : diff > 99 ? 99 : diff; + + set_stdev(difficulty); } char *prefix_opt(char *opt) @@ -53,34 +89,29 @@ void read_delay() free(val); } -void read_stdev() +void read_difficulty() { - char *val = read_opt("stdev"); + char *val = read_opt("difficulty"); if (!val) return; - set_stdev(val); + set_difficulty(val); free(val); } void opts_init() { + // TODO call only when no stored diff setting + set_stdev(difficulty); + read_delay(); - read_stdev(); + read_difficulty(); char buf[64]; sprintf(buf, "%d", delay_ms); dom_set_value("#delay", buf); - int len = sprintf(buf, "%4.2f", horizontal_stdev); - char *dot = strchr(buf, '.'); - if (dot) { - for (char *ptr = buf + len - 1; ptr >= dot; --ptr) { - if (*ptr != '0' && *ptr != '.') - break; - *ptr = 0; - } - } - dom_set_value("#stdev", buf); + sprintf(buf, "%d", difficulty); + dom_set_value("#difficulty", buf); } void store_opt(char *opt, char *val) @@ -95,8 +126,8 @@ void set_opt(char *opt, char *val) { if (!strcmp(opt, "delay")) set_delay(val); - else if (!strcmp(opt, "stdev")) - set_stdev(val); + else if (!strcmp(opt, "difficulty")) + set_difficulty(val); else return; -- cgit v1.2.3-70-g09d2