#include "web_dom.h" #include "web_opts.h" #include "comp.h" #include #include #include #include #include #include #define PREFIX "dartboat_" int delay_ms = 1000; static int difficulty = 32; static void set_delay(char *val) { delay_ms = atoi(val); } static void set_difficulty(char *val) { int diff = atoi(val); difficulty = diff < 0 ? 0 : diff > 99 ? 99 : diff; comp_set_difficulty(difficulty); } static char *prefix_opt(char *opt) { char len = strlen(opt); char *s = malloc(len + sizeof(PREFIX)); memcpy(s, PREFIX, sizeof(PREFIX) - 1); memcpy(s + sizeof(PREFIX) - 1, opt, len + 1); return s; } static char *read_opt(char *opt) { char *s = prefix_opt(opt); char *val = (char *)EM_ASM_INT({return readOpt($0)}, s); free(s); return val; } static bool read_delay() { char *val = read_opt("delay"); if (!val) return false; set_delay(val); free(val); return true; } static bool read_difficulty() { char *val = read_opt("difficulty"); if (!val) return false; set_difficulty(val); free(val); return true; } void opts_init() { read_delay(); if (!read_difficulty()) comp_set_difficulty(difficulty); char buf[64]; sprintf(buf, "%d", delay_ms); dom_set_value("#delay", buf); sprintf(buf, "%d", difficulty); dom_set_value("#difficulty", buf); } static void store_opt(char *opt, char *val) { char *s = prefix_opt(opt); EM_ASM({storeOpt($0, $1)}, s, val); free(s); } EMSCRIPTEN_KEEPALIVE void set_opt(char *opt, char *val) { if (!strcmp(opt, "delay")) set_delay(val); else if (!strcmp(opt, "difficulty")) set_difficulty(val); else return; store_opt(opt, val); }