/* * plugin_tetris_3d - SCORPIOX BOT open-source 3D block game (clean room) * * A single dependency-free C99 binary that serves THREE modes: * 1. --meta / SX_PLUGIN_META=1 -> JSON discovery metadata * 2. ?action=scores | ?action=save_score | ?format=json -> JSON node API * 3. GET (default) -> self-contained HTML game UI * * The game itself is a 3D block-stacking puzzle rendered by a small software * 3D projector onto a 2D canvas. There is no WebGL requirement, no CDN, no * third-party JavaScript and no runtime library beyond libc, so the UI works * offline on every node and produces zero network requests. * * High scores are stored locally in a small JSON file inside the node plugin * directory: * POSIX ~/.scorpiox/plugins/.tetris_scores.json * ($BOT_PLUGIN_DIR when set, /workspace/.scorpiox/plugins on BotCloud) * Windows %LOCALAPPDATA%\scorpiox\plugins\.tetris_scores.json * override SX_TETRIS_SCORES (full path, used by the test suite) * * Nothing else is read or written. There is no parameter anywhere in this * file that can point it at another host. * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #if defined(_WIN32) && !defined(_WIN32_WINNT) # define _WIN32_WINNT 0x0601 #endif #if !defined(_WIN32) # define _POSIX_C_SOURCE 200809L # define _DEFAULT_SOURCE 1 # if defined(__APPLE__) # define _DARWIN_C_SOURCE 1 # endif #endif #include #include #include #include #include #include #if defined(_WIN32) # define WIN32_LEAN_AND_MEAN # include # include # define SX_SEP '\\' #else # include # include # include # define SX_SEP '/' #endif #define PLUGIN_SLUG "tetris-3d" #define PLUGIN_ID "plugin_tetris_3d" #define PLUGIN_NAME "tetris" #define PLUGIN_DESC "3D block game" #define PLUGIN_VER "1.0.1" #define MAX_SCORES 10 #define PATH_SZ 1024 #define BODY_SZ 2048 /* ------------------------------------------------------------------ */ /* Small string helpers */ /* ------------------------------------------------------------------ */ static void str_copy(char *dst, size_t sz, const char *src) { size_t i = 0; if (!dst || sz == 0) return; if (!src) { dst[0] = '\0'; return; } while (i + 1 < sz && src[i]) { dst[i] = src[i]; i++; } dst[i] = '\0'; } static void url_decode(char *s) { char *o = s; if (!s) return; while (*s) { if (*s == '%' && isxdigit((unsigned char)s[1]) && isxdigit((unsigned char)s[2])) { int hi = s[1] <= '9' ? s[1] - '0' : (tolower((unsigned char)s[1]) - 'a' + 10); int lo = s[2] <= '9' ? s[2] - '0' : (tolower((unsigned char)s[2]) - 'a' + 10); *o++ = (char)((hi << 4) | lo); s += 3; } else if (*s == '+') { *o++ = ' '; s++; } else { *o++ = *s++; } } *o = '\0'; } /* Read one query/body parameter, percent-decoded. Returns 1 when present. */ static int get_query_val(const char *qs, const char *key, char *out, size_t outsz) { size_t klen; const char *p; if (!out || outsz == 0) return 0; out[0] = '\0'; if (!qs || !key) return 0; klen = strlen(key); for (p = qs; *p; ) { if (strncmp(p, key, klen) == 0 && p[klen] == '=') { const char *v = p + klen + 1; size_t i = 0; while (*v && *v != '&' && i + 1 < outsz) out[i++] = *v++; out[i] = '\0'; url_decode(out); return 1; } p = strchr(p, '&'); if (!p) break; p++; } return 0; } /* ------------------------------------------------------------------ */ /* Node security */ /* ------------------------------------------------------------------ */ /* Accept X-Bot-Password, Authorization: Bearer or ?pwd=. The bot web * frontend appends ?pwd= for iframes, so all three forms must work. When * BOT_PASSWORD is unset the node is unauthenticated by configuration and the * check is a no-op. --meta stays unauthenticated so discovery works. */ static void require_node_auth(void) { const char *np = getenv("BOT_PASSWORD"); const char *xp; const char *auth; if (!np || !*np) return; xp = getenv("HTTP_X_BOT_PASSWORD"); if (xp && strcmp(xp, np) == 0) return; auth = getenv("HTTP_AUTHORIZATION"); if (auth) { while (*auth == ' ') auth++; if (strncmp(auth, "Bearer ", 7) == 0) { auth += 7; while (*auth == ' ') auth++; if (strcmp(auth, np) == 0) return; } } { char qpwd[256]; const char *qs = getenv("QUERY_STRING"); if (qs && get_query_val(qs, "pwd", qpwd, sizeof qpwd) && strcmp(qpwd, np) == 0) return; } fputs("Status: 401 Unauthorized\r\n" "Content-Type: application/json; charset=utf-8\r\n" "Cache-Control: no-store\r\n\r\n" "{\"ok\":false,\"error\":\"invalid node password\"}\n", stdout); exit(0); } /* ------------------------------------------------------------------ */ /* Local high-score store */ /* ------------------------------------------------------------------ */ typedef struct { long v[MAX_SCORES]; int n; } Scores; static void path_join(char *dst, size_t sz, const char *a, const char *b) { if (!dst || sz == 0) return; dst[0] = '\0'; if (!a || !b) return; if (!*a) { str_copy(dst, sz, b); return; } str_copy(dst, sz, a); { size_t l = strlen(dst); if (l + 1 < sz) { dst[l] = SX_SEP; dst[l + 1] = '\0'; } } { size_t l = strlen(dst); size_t i = 0; while (l + i + 1 < sz && b[i]) { dst[l + i] = b[i]; i++; } dst[l + i] = '\0'; } } static void mkdir_one(const char *p) { #if defined(_WIN32) _mkdir(p); #else mkdir(p, 0700); #endif } /* Resolve /.tetris_scores.json */ static int scores_path(char *out, size_t sz) { const char *env = getenv("SX_TETRIS_SCORES"); char dir[PATH_SZ]; const char *base; if (env && *env) { str_copy(out, sz, env); return 1; } base = getenv("BOT_PLUGIN_DIR"); if (base && *base) { mkdir_one(base); dir[0] = '\0'; str_copy(dir, sizeof dir, base); } else { #if defined(_WIN32) const char *la = getenv("LOCALAPPDATA"); if (la && *la) { path_join(dir, sizeof dir, la, "scorpiox"); } else { const char *up = getenv("USERPROFILE"); if (up && *up) { path_join(dir, sizeof dir, up, ".scorpiox"); } else { str_copy(dir, sizeof dir, "."); } } mkdir_one(dir); { char tmp[PATH_SZ]; path_join(tmp, sizeof tmp, dir, "plugins"); str_copy(dir, sizeof dir, tmp); mkdir_one(dir); } #else const char *home = getenv("HOME"); # if defined(__APPLE__) if (access("/workspace", W_OK) == 0) { path_join(dir, sizeof dir, "/workspace/.scorpiox", "plugins"); } else # endif if (home && *home) { path_join(dir, sizeof dir, home, ".scorpiox"); mkdir_one(dir); { char tmp[PATH_SZ]; path_join(tmp, sizeof tmp, dir, "plugins"); str_copy(dir, sizeof dir, tmp); } mkdir_one(dir); } else { str_copy(dir, sizeof dir, "."); } #endif } path_join(out, sz, dir, ".tetris_scores.json"); return 1; } static void scores_load(Scores *s) { char path[PATH_SZ]; FILE *fp; char buf[BODY_SZ]; size_t got; memset(s, 0, sizeof *s); if (!scores_path(path, sizeof path)) return; fp = fopen(path, "rb"); if (!fp) return; got = fread(buf, 1, sizeof buf - 1, fp); fclose(fp); buf[got] = '\0'; { const char *p = strstr(buf, "\"scores\""); if (!p) p = buf; p = strchr(p, '['); if (!p) return; p++; while (*p && s->n < MAX_SCORES) { while (*p && !isdigit((unsigned char)*p) && *p != ']') p++; if (!*p || *p == ']') break; { long v = strtol(p, NULL, 10); if (v > 0) s->v[s->n++] = v; while (*p && *p != ',' && *p != ']') p++; if (*p == ',') p++; } } } } static int scores_save(const Scores *s) { char path[PATH_SZ]; FILE *fp; int i; if (!scores_path(path, sizeof path)) return 0; fp = fopen(path, "wb"); if (!fp) return 0; fputs("{\"scores\":[", fp); for (i = 0; i < s->n; i++) { if (i) fputc(',', fp); fprintf(fp, "%ld", s->v[i]); } fputs("]}\n", fp); fclose(fp); return 1; } static void scores_add(Scores *s, long val) { int i, pos; if (val <= 0) return; if (s->n < MAX_SCORES) { pos = s->n++; } else { if (val <= s->v[MAX_SCORES - 1]) return; pos = MAX_SCORES - 1; } for (i = pos; i > 0 && s->v[i - 1] < val; i--) s->v[i] = s->v[i - 1]; s->v[i] = val; } static void scores_print_json(const Scores *s) { int i; printf("{\"ok\":true,\"plugin\":\"%s\",\"version\":\"%s\",\"highscore\":%ld,\"scores\":[", PLUGIN_ID, PLUGIN_VER, s->n ? s->v[0] : 0L); for (i = 0; i < s->n; i++) { if (i) fputc(',', stdout); printf("%ld", s->v[i]); } fputs("]}\n", stdout); } /* Read the POST body when the frontend actually sent one. Guarded on * CONTENT_LENGTH so an inherited empty stdin never blocks the process. */ static int read_body(char *out, size_t sz) { const char *cl = getenv("CONTENT_LENGTH"); long len; size_t got; if (!out || sz == 0) return 0; out[0] = '\0'; if (!cl || !*cl) return 0; len = strtol(cl, NULL, 10); if (len <= 0) return 0; if ((size_t)len > sz - 1) len = (long)sz - 1; got = fread(out, 1, (size_t)len, stdin); out[got] = '\0'; return (int)got; } /* ------------------------------------------------------------------ */ /* Modes */ /* ------------------------------------------------------------------ */ #include "ui_html.h" static const char *method(void) { const char *m = getenv("REQUEST_METHOD"); return (m && *m) ? m : "GET"; } int main(int argc, char **argv) { const char *qs; char action[64]; char valstr[64]; char body[BODY_SZ]; Scores scores; /* 1. Discovery metadata (unauthenticated so /plugins can probe it) */ if ((argc > 1 && strcmp(argv[1], "--meta") == 0) || getenv("SX_PLUGIN_META")) { printf("{\"name\":\"%s\",\"desc\":\"%s\"}\n", PLUGIN_NAME, PLUGIN_DESC); return 0; } if (argc > 1 && strcmp(argv[1], "--version") == 0) { printf("%s %s\n", PLUGIN_ID, PLUGIN_VER); return 0; } require_node_auth(); qs = getenv("QUERY_STRING"); if (!qs) qs = ""; action[0] = '\0'; valstr[0] = '\0'; get_query_val(qs, "action", action, sizeof action); /* 2. JSON API: ?action=..., ?format=json, or any POST with a body */ if (action[0] || strstr(qs, "format=json") || strncmp(method(), "POST", 4) == 0) { char format[32]; format[0] = '\0'; get_query_val(qs, "format", format, sizeof format); if (!action[0] && strncmp(method(), "POST", 4) == 0) str_copy(action, sizeof action, "save_score"); if (!action[0]) str_copy(action, sizeof action, "scores"); read_body(body, sizeof body); if (!get_query_val(qs, "val", valstr, sizeof valstr) && body[0]) { if (!get_query_val(body, "val", valstr, sizeof valstr)) (void)get_query_val(body, "score", valstr, sizeof valstr); } if (!valstr[0]) get_query_val(qs, "score", valstr, sizeof valstr); if (!valstr[0] && body[0]) { /* a bare numeric body is accepted too */ const char *p = body; while (*p && !isdigit((unsigned char)*p) && *p != '-') p++; if (*p) str_copy(valstr, sizeof valstr, p); } scores_load(&scores); fputs("Content-Type: application/json; charset=utf-8\r\n" "Cache-Control: no-store\r\n\r\n", stdout); if (strcmp(action, "save_score") == 0) { long v = strtol(valstr, NULL, 10); if (v > 0) { scores_add(&scores, v); if (!scores_save(&scores)) printf("{\"ok\":false,\"error\":\"score_store_unwritable\",\"highscore\":%ld}\n", scores.n ? scores.v[0] : 0L); else scores_print_json(&scores); } else { printf("{\"ok\":false,\"error\":\"invalid_score\"}\n"); } return 0; } if (strcmp(action, "scores") == 0 || strcmp(action, "highscore") == 0 || strcmp(action, "leaderboard") == 0) { scores_print_json(&scores); return 0; } printf("{\"ok\":false,\"error\":\"unknown_action\"}\n"); return 0; } /* 3. HTML UI */ fputs("Content-Type: text/html; charset=utf-8\r\n" "Cache-Control: no-store\r\n\r\n", stdout); fputs(UI_HTML, stdout); return 0; }