'use strict'; /** * Client-side mirror of the Allianties rule helpers. * Used for legal-move previews, the cease-fire clock, countdowns, and tests. * The server (Engine.php) is authoritative for every action. */ var Rules = { TZ: 'Europe/Amsterdam', STARTING_LIVES: 3, STARTING_RANGE: 2, STARTING_AP: 1, MIN_PLAYERS: 2, MAX_PLAYERS: 16, MIN_GRID: 6, MAX_GRID: 24, MIN_ARENA: 3, DEFAULT_SHRINK_DAYS: 7, COLORS: [ '#e74c3c', '#3498db', '#2ecc71', '#f1c40f', '#9b59b6', '#e67e22', '#1abc9c', '#e91e63', '#00bcd4', '#8bc34a', '#ff5722', '#3f51b5', '#795548', '#607d8b', '#cddc39', '#ff9800' ] }; Rules.defaultRules = function () { return { jury: true, shrinking_arena: false, shrink_interval_days: this.DEFAULT_SHRINK_DAYS, ceasefire: false, ceasefire_ranges: [], ap_loot: false, ap_time: '00:00', tz: this.TZ, vis_shots: 'public', vis_gifts: 'public', vis_ap: 'public', vis_range: 'public', vis_lives: 'public', grid: this.suggestedGrid(1), grid_auto: true }; }; /** Suggested square side: max(6 + ceil(n/2), n + 1), capped at 24. */ Rules.suggestedGrid = function (n) { if (n < 1) { n = 1; } var side = Math.max(6 + Math.ceil(n / 2), n + 1); return Math.min(this.MAX_GRID, side); }; Rules.gridSize = function (n) { return this.suggestedGrid(n); }; Rules.clampGrid = function (size) { size = parseInt(size, 10); if (!size || size < this.MIN_GRID) { return this.MIN_GRID; } if (size > this.MAX_GRID) { return this.MAX_GRID; } return size; }; Rules.maxInset = function (size) { return Math.max(0, Math.floor((size - this.MIN_ARENA) / 2)); }; Rules.chebyshev = function (x1, y1, x2, y2) { return Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2)); }; Rules.inBounds = function (size, x, y) { return x >= 0 && y >= 0 && x < size && y < size; }; Rules.isPassable = function (size, inset, x, y) { return this.inBounds(size, x, y) && x >= inset && y >= inset && x < size - inset && y < size - inset; }; /** * Clock angle (atan2 radians: 0 at 3 o'clock) → minutes after midnight on a * 24-hour dial with 0 at the top, clockwise, snapped to `step` minutes. */ Rules.angleToMinutes = function (ang, step) { step = step || 15; var a = ang + Math.PI / 2; while (a < 0) { a += 2 * Math.PI; } while (a >= 2 * Math.PI) { a -= 2 * Math.PI; } var mins = Math.round((a / (2 * Math.PI)) * 1440 / step) * step; return mins >= 1440 ? 0 : mins; }; Rules.parseHm = function (v) { if (typeof v !== 'string') { return null; } var m = /^(\d{1,2}):(\d{2})$/.exec(v.trim()); if (!m) { return null; } var h = parseInt(m[1], 10), min = parseInt(m[2], 10); if (h > 23 || min > 59) { return null; } return (h < 10 ? '0' : '') + h + ':' + (min < 10 ? '0' : '') + min; }; Rules.hmToMinutes = function (hm) { var p = hm.split(':'); return parseInt(p[0], 10) * 60 + parseInt(p[1], 10); }; Rules.minutesToHm = function (mins) { mins = ((mins % 1440) + 1440) % 1440; var h = Math.floor(mins / 60), m = mins % 60; return (h < 10 ? '0' : '') + h + ':' + (m < 10 ? '0' : '') + m; }; Rules.normalizeRanges = function (raw) { if (!Array.isArray(raw)) { return []; } var out = [], seen = {}; for (var i = 0; i < raw.length; i++) { var r = raw[i]; if (!r) { continue; } var start = this.parseHm(r.start || r[0]); var end = this.parseHm(r.end || r[1]); if (!start || !end || start === end) { continue; } var key = start + '-' + end; if (seen[key]) { continue; } seen[key] = true; out.push({ start: start, end: end }); } out.sort(function (a, b) { return a.start < b.start ? -1 : a.start > b.start ? 1 : 0; }); return out.slice(0, 12); }; /** Unix timestamp of ymd 00:00 in `tz` (default Europe/Amsterdam). */ Rules.amsterdamMidnight = function (ymd, tz) { var parts = ymd.split('-').map(Number); var Y = parts[0], M = parts[1], D = parts[2]; // First guess: UTC midnight minus one hour (CET). var utc = Date.UTC(Y, M - 1, D, 0, 0, 0) - 3600 * 1000; for (var i = 0; i < 5; i++) { var p = this._amsterdamParts(utc, tz); var got = Date.UTC(+p.year, +p.month - 1, +p.day, +p.hour, +p.minute, +(p.second || 0)); var want = Date.UTC(Y, M - 1, D, 0, 0, 0); utc += (want - got); } return Math.floor(utc / 1000); }; Rules._fmtCache = {}; Rules._amsterdamParts = function (unixMs, tz) { tz = this.validTz(tz); var fmt = this._fmtCache[tz]; if (!fmt) { fmt = this._fmtCache[tz] = new Intl.DateTimeFormat('en-US', { timeZone: tz, year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hourCycle: 'h23' }); } var p = {}; fmt.formatToParts(new Date(unixMs)).forEach(function (x) { if (x.type !== 'literal') { p[x.type] = x.value; } }); return p; }; Rules.amsterdamYmd = function (unix, tz) { var p = this._amsterdamParts(unix * 1000, tz); return p.year + '-' + p.month + '-' + p.day; }; /** * Wall-clock HH:MM on a civil date in Europe/Amsterdam, as unix seconds. * Matches PHP DateTime::modify("+N minutes") from midnight: civil minutes * walk through DST (spring-forward shortens, fall-back lengthens). * Non-existent times (e.g. 02:30 on a spring-forward day) land after the gap. */ Rules.amsterdamWallclock = function (ymd, hm, tz) { var t = this.amsterdamMidnight(ymd, tz); var target = this.hmToMinutes(hm); if (target === 0) { return t; } for (var i = 0; i < 8; i++) { var p = this._amsterdamParts(t * 1000, tz); var day = p.year + '-' + p.month + '-' + p.day; var got = (+p.hour) * 60 + (+p.minute); if (day !== ymd) { // Overflowed into another civil day (should be rare for HH:MM). var sign = day > ymd ? 1 : -1; got += sign * 1440; } var remaining = target - got; if (remaining === 0) { return t; } // If we oscillate around a spring-forward gap, take the later side. t += remaining * 60; } return t; }; Rules.addDaysYmd = function (ymd, days) { // Add calendar days via Amsterdam midnight + 26h, then read the ymd and // correct — safer: midnight + days*24h may skip/double; use Date in UTC // of the civil date then shift. var p = ymd.split('-').map(Number); var d = new Date(Date.UTC(p[0], p[1] - 1, p[2] + days)); var mm = d.getUTCMonth() + 1, dd = d.getUTCDate(); return d.getUTCFullYear() + '-' + (mm < 10 ? '0' : '') + mm + '-' + (dd < 10 ? '0' : '') + dd; }; Rules.ceasefireIntervals = function (ranges, now, tz) { ranges = this.normalizeRanges(ranges); if (!ranges.length) { return []; } var anchor = this.amsterdamYmd(now, tz); var intervals = []; for (var d = -1; d <= 3; d++) { var ymd = this.addDaysYmd(anchor, d); var next = this.addDaysYmd(anchor, d + 1); for (var i = 0; i < ranges.length; i++) { var r = ranges[i]; var startM = this.hmToMinutes(r.start); var endM = this.hmToMinutes(r.end); var startTs = this.amsterdamWallclock(ymd, r.start, tz); var endTs = endM > startM ? this.amsterdamWallclock(ymd, r.end, tz) : this.amsterdamWallclock(next, r.end, tz); if (endTs > startTs) { intervals.push([startTs, endTs]); } } } intervals.sort(function (a, b) { return a[0] - b[0] || a[1] - b[1]; }); var merged = []; for (var j = 0; j < intervals.length; j++) { var iv = intervals[j]; if (!merged.length || iv[0] > merged[merged.length - 1][1]) { merged.push(iv.slice()); } else { merged[merged.length - 1][1] = Math.max(merged[merged.length - 1][1], iv[1]); } } return merged; }; Rules.ceasefireInfo = function (rules, now) { if (!rules || !rules.ceasefire) { return null; } var ranges = this.normalizeRanges(rules.ceasefire_ranges); if (!ranges.length) { return null; } var intervals = this.ceasefireIntervals(ranges, now, rules.tz); var active = false, until = null; for (var i = 0; i < intervals.length; i++) { var iv = intervals[i]; if (now >= iv[0] && now < iv[1]) { active = true; until = iv[1]; break; } if (iv[0] > now && (until === null || iv[0] < until)) { until = iv[0]; } } return { enabled: true, active: active, until: until, seconds: until !== null ? Math.max(0, until - now) : 0, ranges: ranges }; }; Rules.formatHms = function (seconds) { seconds = Math.max(0, Math.floor(seconds)); var h = Math.floor(seconds / 3600); var m = Math.floor((seconds % 3600) / 60); var s = seconds % 60; function pad (n) { return n < 10 ? '0' + n : String(n); } return pad(h) + ':' + pad(m) + ':' + pad(s); }; /* ---------- time zones: host zone ↔ viewer's local zone ---------- */ Rules._tzOk = {}; /** A usable IANA zone name, or Europe/Amsterdam. */ Rules.validTz = function (tz) { if (!tz || typeof tz !== 'string') { return this.TZ; } if (this._tzOk[tz] === undefined) { try { new Intl.DateTimeFormat('en-US', { timeZone: tz }); this._tzOk[tz] = true; } catch (e) { this._tzOk[tz] = false; } } return this._tzOk[tz] ? tz : this.TZ; }; /** The browser's IANA zone (the player's local time). */ Rules.localTz = function () { try { var z = Intl.DateTimeFormat().resolvedOptions().timeZone; return this.validTz(z || 'UTC'); } catch (e) { return 'UTC'; } }; /** HH:MM of a unix instant in `tz`. */ Rules.hmAt = function (unix, tz) { var p = this._amsterdamParts(unix * 1000, tz); return p.hour + ':' + p.minute; }; /** * Convert a wall-clock HH:MM in `fromTz` to `toTz`, on the civil date of * `refUnix` in `fromTz` (DST-aware: the offset of that day is used). */ Rules.convertHm = function (hm, fromTz, toTz, refUnix) { fromTz = this.validTz(fromTz); toTz = this.validTz(toTz); var v = this.parseHm(hm); if (!v) { return hm; } if (fromTz === toTz) { return v; } var ymd = this.amsterdamYmd(refUnix, fromTz); return this.hmAt(this.amsterdamWallclock(ymd, v, fromTz), toTz); }; /** * Copy of `rules` with ap_time and cease-fire ranges expressed in `toTz` * (tz set to `toTz`). Ranges may wrap midnight after conversion. */ Rules.rulesInTz = function (rules, toTz, refUnix) { var r = Object.assign({}, rules || {}); var from = this.validTz(r.tz); toTz = this.validTz(toTz); var self = this; r.ap_time = this.convertHm(r.ap_time || '00:00', from, toTz, refUnix); r.ceasefire_ranges = (r.ceasefire_ranges || []).map(function (x) { return { start: self.convertHm(x.start, from, toTz, refUnix), end: self.convertHm(x.end, from, toTz, refUnix) }; }).filter(function (x) { return x.start !== x.end; }); r.tz = toTz; return r; }; if (typeof module !== 'undefined' && module.exports) { module.exports = Rules; } else if (typeof globalThis !== 'undefined') { globalThis.Rules = Rules; } /** * Map a 0–1 position along the timeline track to a spend index 0..n * (0 = before the first spend, n = after the last / live). Nearest event. */ Rules.timelineIndex = function (t, n) { n = n | 0; if (n <= 0) { return 0; } t = Math.max(0, Math.min(1, Number(t) || 0)); return Math.round(t * n); };