| 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * Client-side mirror of the Allianties rule helpers. |
| 5 | * Used for legal-move previews, the cease-fire clock, countdowns, and tests. |
| 6 | * The server (Engine.php) is authoritative for every action. |
| 7 | */ |
| 8 | var Rules = { |
| 9 | TZ: 'Europe/Amsterdam', |
| 10 | STARTING_LIVES: 3, |
| 11 | STARTING_RANGE: 2, |
| 12 | STARTING_AP: 1, |
| 13 | MIN_PLAYERS: 2, |
| 14 | MAX_PLAYERS: 16, |
| 15 | MIN_GRID: 6, |
| 16 | MAX_GRID: 24, |
| 17 | MIN_ARENA: 3, |
| 18 | DEFAULT_SHRINK_DAYS: 7, |
| 19 | COLORS: [ |
| 20 | '#e74c3c', '#3498db', '#2ecc71', '#f1c40f', |
| 21 | '#9b59b6', '#e67e22', '#1abc9c', '#e91e63', |
| 22 | '#00bcd4', '#8bc34a', '#ff5722', '#3f51b5', |
| 23 | '#795548', '#607d8b', '#cddc39', '#ff9800' |
| 24 | ] |
| 25 | }; |
| 26 | |
| 27 | Rules.defaultRules = function () |
| 28 | { |
| 29 | return { |
| 30 | jury: true, |
| 31 | shrinking_arena: false, |
| 32 | shrink_interval_days: this.DEFAULT_SHRINK_DAYS, |
| 33 | ceasefire: false, |
| 34 | ceasefire_ranges: [], |
| 35 | ap_loot: false, |
| 36 | ap_time: '00:00', |
| 37 | tz: this.TZ, |
| 38 | vis_shots: 'public', |
| 39 | vis_gifts: 'public', |
| 40 | vis_ap: 'public', |
| 41 | vis_range: 'public', |
| 42 | vis_lives: 'public', |
| 43 | grid: this.suggestedGrid(1), |
| 44 | grid_auto: true |
| 45 | }; |
| 46 | }; |
| 47 | |
| 48 | /** Suggested square side: max(6 + ceil(n/2), n + 1), capped at 24. */ |
| 49 | Rules.suggestedGrid = function (n) |
| 50 | { |
| 51 | if (n < 1) { n = 1; } |
| 52 | var side = Math.max(6 + Math.ceil(n / 2), n + 1); |
| 53 | return Math.min(this.MAX_GRID, side); |
| 54 | }; |
| 55 | |
| 56 | Rules.gridSize = function (n) |
| 57 | { |
| 58 | return this.suggestedGrid(n); |
| 59 | }; |
| 60 | |
| 61 | Rules.clampGrid = function (size) |
| 62 | { |
| 63 | size = parseInt(size, 10); |
| 64 | if (!size || size < this.MIN_GRID) { return this.MIN_GRID; } |
| 65 | if (size > this.MAX_GRID) { return this.MAX_GRID; } |
| 66 | return size; |
| 67 | }; |
| 68 | |
| 69 | Rules.maxInset = function (size) |
| 70 | { |
| 71 | return Math.max(0, Math.floor((size - this.MIN_ARENA) / 2)); |
| 72 | }; |
| 73 | |
| 74 | Rules.chebyshev = function (x1, y1, x2, y2) |
| 75 | { |
| 76 | return Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2)); |
| 77 | }; |
| 78 | |
| 79 | Rules.inBounds = function (size, x, y) |
| 80 | { |
| 81 | return x >= 0 && y >= 0 && x < size && y < size; |
| 82 | }; |
| 83 | |
| 84 | Rules.isPassable = function (size, inset, x, y) |
| 85 | { |
| 86 | return this.inBounds(size, x, y) |
| 87 | && x >= inset && y >= inset && x < size - inset && y < size - inset; |
| 88 | }; |
| 89 | |
| 90 | /** |
| 91 | * Clock angle (atan2 radians: 0 at 3 o'clock) → minutes after midnight on a |
| 92 | * 24-hour dial with 0 at the top, clockwise, snapped to `step` minutes. |
| 93 | */ |
| 94 | Rules.angleToMinutes = function (ang, step) |
| 95 | { |
| 96 | step = step || 15; |
| 97 | var a = ang + Math.PI / 2; |
| 98 | while (a < 0) { a += 2 * Math.PI; } |
| 99 | while (a >= 2 * Math.PI) { a -= 2 * Math.PI; } |
| 100 | var mins = Math.round((a / (2 * Math.PI)) * 1440 / step) * step; |
| 101 | return mins >= 1440 ? 0 : mins; |
| 102 | }; |
| 103 | |
| 104 | Rules.parseHm = function (v) |
| 105 | { |
| 106 | if (typeof v !== 'string') { return null; } |
| 107 | var m = /^(\d{1,2}):(\d{2})$/.exec(v.trim()); |
| 108 | if (!m) { return null; } |
| 109 | var h = parseInt(m[1], 10), min = parseInt(m[2], 10); |
| 110 | if (h > 23 || min > 59) { return null; } |
| 111 | return (h < 10 ? '0' : '') + h + ':' + (min < 10 ? '0' : '') + min; |
| 112 | }; |
| 113 | |
| 114 | Rules.hmToMinutes = function (hm) |
| 115 | { |
| 116 | var p = hm.split(':'); |
| 117 | return parseInt(p[0], 10) * 60 + parseInt(p[1], 10); |
| 118 | }; |
| 119 | |
| 120 | Rules.minutesToHm = function (mins) |
| 121 | { |
| 122 | mins = ((mins % 1440) + 1440) % 1440; |
| 123 | var h = Math.floor(mins / 60), m = mins % 60; |
| 124 | return (h < 10 ? '0' : '') + h + ':' + (m < 10 ? '0' : '') + m; |
| 125 | }; |
| 126 | |
| 127 | Rules.normalizeRanges = function (raw) |
| 128 | { |
| 129 | if (!Array.isArray(raw)) { return []; } |
| 130 | var out = [], seen = {}; |
| 131 | for (var i = 0; i < raw.length; i++) |
| 132 | { |
| 133 | var r = raw[i]; |
| 134 | if (!r) { continue; } |
| 135 | var start = this.parseHm(r.start || r[0]); |
| 136 | var end = this.parseHm(r.end || r[1]); |
| 137 | if (!start || !end || start === end) { continue; } |
| 138 | var key = start + '-' + end; |
| 139 | if (seen[key]) { continue; } |
| 140 | seen[key] = true; |
| 141 | out.push({ start: start, end: end }); |
| 142 | } |
| 143 | out.sort(function (a, b) { return a.start < b.start ? -1 : a.start > b.start ? 1 : 0; }); |
| 144 | return out.slice(0, 12); |
| 145 | }; |
| 146 | |
| 147 | /** Unix timestamp of ymd 00:00 in `tz` (default Europe/Amsterdam). */ |
| 148 | Rules.amsterdamMidnight = function (ymd, tz) |
| 149 | { |
| 150 | var parts = ymd.split('-').map(Number); |
| 151 | var Y = parts[0], M = parts[1], D = parts[2]; |
| 152 | // First guess: UTC midnight minus one hour (CET). |
| 153 | var utc = Date.UTC(Y, M - 1, D, 0, 0, 0) - 3600 * 1000; |
| 154 | for (var i = 0; i < 5; i++) |
| 155 | { |
| 156 | var p = this._amsterdamParts(utc, tz); |
| 157 | var got = Date.UTC(+p.year, +p.month - 1, +p.day, +p.hour, +p.minute, +(p.second || 0)); |
| 158 | var want = Date.UTC(Y, M - 1, D, 0, 0, 0); |
| 159 | utc += (want - got); |
| 160 | } |
| 161 | return Math.floor(utc / 1000); |
| 162 | }; |
| 163 | |
| 164 | Rules._fmtCache = {}; |
| 165 | Rules._amsterdamParts = function (unixMs, tz) |
| 166 | { |
| 167 | tz = this.validTz(tz); |
| 168 | var fmt = this._fmtCache[tz]; |
| 169 | if (!fmt) |
| 170 | { |
| 171 | fmt = this._fmtCache[tz] = new Intl.DateTimeFormat('en-US', { |
| 172 | timeZone: tz, |
| 173 | year: 'numeric', month: '2-digit', day: '2-digit', |
| 174 | hour: '2-digit', minute: '2-digit', second: '2-digit', |
| 175 | hourCycle: 'h23' |
| 176 | }); |
| 177 | } |
| 178 | var p = {}; |
| 179 | fmt.formatToParts(new Date(unixMs)).forEach(function (x) |
| 180 | { |
| 181 | if (x.type !== 'literal') { p[x.type] = x.value; } |
| 182 | }); |
| 183 | return p; |
| 184 | }; |
| 185 | |
| 186 | Rules.amsterdamYmd = function (unix, tz) |
| 187 | { |
| 188 | var p = this._amsterdamParts(unix * 1000, tz); |
| 189 | return p.year + '-' + p.month + '-' + p.day; |
| 190 | }; |
| 191 | |
| 192 | /** |
| 193 | * Wall-clock HH:MM on a civil date in Europe/Amsterdam, as unix seconds. |
| 194 | * Matches PHP DateTime::modify("+N minutes") from midnight: civil minutes |
| 195 | * walk through DST (spring-forward shortens, fall-back lengthens). |
| 196 | * Non-existent times (e.g. 02:30 on a spring-forward day) land after the gap. |
| 197 | */ |
| 198 | Rules.amsterdamWallclock = function (ymd, hm, tz) |
| 199 | { |
| 200 | var t = this.amsterdamMidnight(ymd, tz); |
| 201 | var target = this.hmToMinutes(hm); |
| 202 | if (target === 0) |
| 203 | { return t; } |
| 204 | for (var i = 0; i < 8; i++) |
| 205 | { |
| 206 | var p = this._amsterdamParts(t * 1000, tz); |
| 207 | var day = p.year + '-' + p.month + '-' + p.day; |
| 208 | var got = (+p.hour) * 60 + (+p.minute); |
| 209 | if (day !== ymd) |
| 210 | { |
| 211 | // Overflowed into another civil day (should be rare for HH:MM). |
| 212 | var sign = day > ymd ? 1 : -1; |
| 213 | got += sign * 1440; |
| 214 | } |
| 215 | var remaining = target - got; |
| 216 | if (remaining === 0) |
| 217 | { return t; } |
| 218 | // If we oscillate around a spring-forward gap, take the later side. |
| 219 | t += remaining * 60; |
| 220 | } |
| 221 | return t; |
| 222 | }; |
| 223 | |
| 224 | Rules.addDaysYmd = function (ymd, days) |
| 225 | { |
| 226 | // Add calendar days via Amsterdam midnight + 26h, then read the ymd and |
| 227 | // correct — safer: midnight + days*24h may skip/double; use Date in UTC |
| 228 | // of the civil date then shift. |
| 229 | var p = ymd.split('-').map(Number); |
| 230 | var d = new Date(Date.UTC(p[0], p[1] - 1, p[2] + days)); |
| 231 | var mm = d.getUTCMonth() + 1, dd = d.getUTCDate(); |
| 232 | return d.getUTCFullYear() + '-' + (mm < 10 ? '0' : '') + mm + '-' + (dd < 10 ? '0' : '') + dd; |
| 233 | }; |
| 234 | |
| 235 | Rules.ceasefireIntervals = function (ranges, now, tz) |
| 236 | { |
| 237 | ranges = this.normalizeRanges(ranges); |
| 238 | if (!ranges.length) { return []; } |
| 239 | var anchor = this.amsterdamYmd(now, tz); |
| 240 | var intervals = []; |
| 241 | for (var d = -1; d <= 3; d++) |
| 242 | { |
| 243 | var ymd = this.addDaysYmd(anchor, d); |
| 244 | var next = this.addDaysYmd(anchor, d + 1); |
| 245 | for (var i = 0; i < ranges.length; i++) |
| 246 | { |
| 247 | var r = ranges[i]; |
| 248 | var startM = this.hmToMinutes(r.start); |
| 249 | var endM = this.hmToMinutes(r.end); |
| 250 | var startTs = this.amsterdamWallclock(ymd, r.start, tz); |
| 251 | var endTs = endM > startM |
| 252 | ? this.amsterdamWallclock(ymd, r.end, tz) |
| 253 | : this.amsterdamWallclock(next, r.end, tz); |
| 254 | if (endTs > startTs) { intervals.push([startTs, endTs]); } |
| 255 | } |
| 256 | } |
| 257 | intervals.sort(function (a, b) { return a[0] - b[0] || a[1] - b[1]; }); |
| 258 | var merged = []; |
| 259 | for (var j = 0; j < intervals.length; j++) |
| 260 | { |
| 261 | var iv = intervals[j]; |
| 262 | if (!merged.length || iv[0] > merged[merged.length - 1][1]) |
| 263 | { merged.push(iv.slice()); } |
| 264 | else |
| 265 | { merged[merged.length - 1][1] = Math.max(merged[merged.length - 1][1], iv[1]); } |
| 266 | } |
| 267 | return merged; |
| 268 | }; |
| 269 | |
| 270 | Rules.ceasefireInfo = function (rules, now) |
| 271 | { |
| 272 | if (!rules || !rules.ceasefire) { return null; } |
| 273 | var ranges = this.normalizeRanges(rules.ceasefire_ranges); |
| 274 | if (!ranges.length) { return null; } |
| 275 | var intervals = this.ceasefireIntervals(ranges, now, rules.tz); |
| 276 | var active = false, until = null; |
| 277 | for (var i = 0; i < intervals.length; i++) |
| 278 | { |
| 279 | var iv = intervals[i]; |
| 280 | if (now >= iv[0] && now < iv[1]) |
| 281 | { |
| 282 | active = true; |
| 283 | until = iv[1]; |
| 284 | break; |
| 285 | } |
| 286 | if (iv[0] > now && (until === null || iv[0] < until)) |
| 287 | { until = iv[0]; } |
| 288 | } |
| 289 | return { |
| 290 | enabled: true, |
| 291 | active: active, |
| 292 | until: until, |
| 293 | seconds: until !== null ? Math.max(0, until - now) : 0, |
| 294 | ranges: ranges |
| 295 | }; |
| 296 | }; |
| 297 | |
| 298 | Rules.formatHms = function (seconds) |
| 299 | { |
| 300 | seconds = Math.max(0, Math.floor(seconds)); |
| 301 | var h = Math.floor(seconds / 3600); |
| 302 | var m = Math.floor((seconds % 3600) / 60); |
| 303 | var s = seconds % 60; |
| 304 | function pad (n) { return n < 10 ? '0' + n : String(n); } |
| 305 | return pad(h) + ':' + pad(m) + ':' + pad(s); |
| 306 | }; |
| 307 | |
| 308 | |
| 309 | /* ---------- time zones: host zone ↔ viewer's local zone ---------- */ |
| 310 | |
| 311 | Rules._tzOk = {}; |
| 312 | /** A usable IANA zone name, or Europe/Amsterdam. */ |
| 313 | Rules.validTz = function (tz) |
| 314 | { |
| 315 | if (!tz || typeof tz !== 'string') { return this.TZ; } |
| 316 | if (this._tzOk[tz] === undefined) |
| 317 | { |
| 318 | try { new Intl.DateTimeFormat('en-US', { timeZone: tz }); this._tzOk[tz] = true; } |
| 319 | catch (e) { this._tzOk[tz] = false; } |
| 320 | } |
| 321 | return this._tzOk[tz] ? tz : this.TZ; |
| 322 | }; |
| 323 | |
| 324 | /** The browser's IANA zone (the player's local time). */ |
| 325 | Rules.localTz = function () |
| 326 | { |
| 327 | try |
| 328 | { |
| 329 | var z = Intl.DateTimeFormat().resolvedOptions().timeZone; |
| 330 | return this.validTz(z || 'UTC'); |
| 331 | } |
| 332 | catch (e) { return 'UTC'; } |
| 333 | }; |
| 334 | |
| 335 | /** HH:MM of a unix instant in `tz`. */ |
| 336 | Rules.hmAt = function (unix, tz) |
| 337 | { |
| 338 | var p = this._amsterdamParts(unix * 1000, tz); |
| 339 | return p.hour + ':' + p.minute; |
| 340 | }; |
| 341 | |
| 342 | /** |
| 343 | * Convert a wall-clock HH:MM in `fromTz` to `toTz`, on the civil date of |
| 344 | * `refUnix` in `fromTz` (DST-aware: the offset of that day is used). |
| 345 | */ |
| 346 | Rules.convertHm = function (hm, fromTz, toTz, refUnix) |
| 347 | { |
| 348 | fromTz = this.validTz(fromTz); |
| 349 | toTz = this.validTz(toTz); |
| 350 | var v = this.parseHm(hm); |
| 351 | if (!v) { return hm; } |
| 352 | if (fromTz === toTz) { return v; } |
| 353 | var ymd = this.amsterdamYmd(refUnix, fromTz); |
| 354 | return this.hmAt(this.amsterdamWallclock(ymd, v, fromTz), toTz); |
| 355 | }; |
| 356 | |
| 357 | /** |
| 358 | * Copy of `rules` with ap_time and cease-fire ranges expressed in `toTz` |
| 359 | * (tz set to `toTz`). Ranges may wrap midnight after conversion. |
| 360 | */ |
| 361 | Rules.rulesInTz = function (rules, toTz, refUnix) |
| 362 | { |
| 363 | var r = Object.assign({}, rules || {}); |
| 364 | var from = this.validTz(r.tz); |
| 365 | toTz = this.validTz(toTz); |
| 366 | var self = this; |
| 367 | r.ap_time = this.convertHm(r.ap_time || '00:00', from, toTz, refUnix); |
| 368 | r.ceasefire_ranges = (r.ceasefire_ranges || []).map(function (x) |
| 369 | { |
| 370 | return { start: self.convertHm(x.start, from, toTz, refUnix), end: self.convertHm(x.end, from, toTz, refUnix) }; |
| 371 | }).filter(function (x) { return x.start !== x.end; }); |
| 372 | r.tz = toTz; |
| 373 | return r; |
| 374 | }; |
| 375 | |
| 376 | if (typeof module !== 'undefined' && module.exports) |
| 377 | { module.exports = Rules; } |
| 378 | else if (typeof globalThis !== 'undefined') |
| 379 | { globalThis.Rules = Rules; } |
| 380 | |
| 381 | /** |
| 382 | * Map a 0–1 position along the timeline track to a spend index 0..n |
| 383 | * (0 = before the first spend, n = after the last / live). Nearest event. |
| 384 | */ |
| 385 | Rules.timelineIndex = function (t, n) |
| 386 | { |
| 387 | n = n | 0; |
| 388 | if (n <= 0) { return 0; } |
| 389 | t = Math.max(0, Math.min(1, Number(t) || 0)); |
| 390 | return Math.round(t * n); |
| 391 | }; |
projects-timfalken-com allianties