BlueGit

projects-timfalken-com allianties

Files

allianties / src / rules.js

rules.js

rules.js 8.0 KiB Download Raw
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 */
8var 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 SUGGEST_MIN_GRID: 10,
17 MAX_GRID: 24,
18 MIN_ARENA: 3,
19 DEFAULT_SHRINK_DAYS: 7,
20 COLORS: [
21 '#e74c3c', '#3498db', '#2ecc71', '#f1c40f',
22 '#9b59b6', '#e67e22', '#1abc9c', '#e91e63',
23 '#00bcd4', '#8bc34a', '#ff5722', '#3f51b5',
24 '#795548', '#607d8b', '#cddc39', '#ff9800'
25 ]
26};
27
28Rules.defaultRules = function ()
29{
30 return {
31 jury: true,
32 shrinking_arena: false,
33 shrink_interval_days: this.DEFAULT_SHRINK_DAYS,
34 ceasefire: false,
35 ceasefire_ranges: [],
36 ap_loot: false,
37 ap_time: '00:00',
38 vis_shots: 'public',
39 vis_gifts: 'public',
40 vis_ap: 'public',
41 vis_range: 'public',
42 vis_lives: 'public',
43 grid: this.SUGGEST_MIN_GRID,
44 grid_auto: true
45 };
46};
47
48/** Suggested square side: max(10, n + 1), capped at 24. */
49Rules.suggestedGrid = function (n)
50{
51 if (n < 1) { n = 1; }
52 return Math.max(this.SUGGEST_MIN_GRID, Math.min(this.MAX_GRID, n + 1));
53};
54
55Rules.gridSize = function (n)
56{
57 return this.suggestedGrid(n);
58};
59
60Rules.clampGrid = function (size)
61{
62 size = parseInt(size, 10);
63 if (!size || size < this.MIN_GRID) { return this.MIN_GRID; }
64 if (size > this.MAX_GRID) { return this.MAX_GRID; }
65 return size;
66};
67
68Rules.maxInset = function (size)
69{
70 return Math.max(0, Math.floor((size - this.MIN_ARENA) / 2));
71};
72
73Rules.chebyshev = function (x1, y1, x2, y2)
74{
75 return Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2));
76};
77
78Rules.inBounds = function (size, x, y)
79{
80 return x >= 0 && y >= 0 && x < size && y < size;
81};
82
83Rules.isPassable = function (size, inset, x, y)
84{
85 return this.inBounds(size, x, y)
86 && x >= inset && y >= inset && x < size - inset && y < size - inset;
87};
88
89Rules.parseHm = function (v)
90{
91 if (typeof v !== 'string') { return null; }
92 var m = /^(\d{1,2}):(\d{2})$/.exec(v.trim());
93 if (!m) { return null; }
94 var h = parseInt(m[1], 10), min = parseInt(m[2], 10);
95 if (h > 23 || min > 59) { return null; }
96 return (h < 10 ? '0' : '') + h + ':' + (min < 10 ? '0' : '') + min;
97};
98
99Rules.hmToMinutes = function (hm)
100{
101 var p = hm.split(':');
102 return parseInt(p[0], 10) * 60 + parseInt(p[1], 10);
103};
104
105Rules.minutesToHm = function (mins)
106{
107 mins = ((mins % 1440) + 1440) % 1440;
108 var h = Math.floor(mins / 60), m = mins % 60;
109 return (h < 10 ? '0' : '') + h + ':' + (m < 10 ? '0' : '') + m;
110};
111
112Rules.normalizeRanges = function (raw)
113{
114 if (!Array.isArray(raw)) { return []; }
115 var out = [], seen = {};
116 for (var i = 0; i < raw.length; i++)
117 {
118 var r = raw[i];
119 if (!r) { continue; }
120 var start = this.parseHm(r.start || r[0]);
121 var end = this.parseHm(r.end || r[1]);
122 if (!start || !end || start === end) { continue; }
123 var key = start + '-' + end;
124 if (seen[key]) { continue; }
125 seen[key] = true;
126 out.push({ start: start, end: end });
127 }
128 out.sort(function (a, b) { return a.start < b.start ? -1 : a.start > b.start ? 1 : 0; });
129 return out.slice(0, 12);
130};
131
132/** Unix timestamp of ymd 00:00 in Europe/Amsterdam. */
133Rules.amsterdamMidnight = function (ymd)
134{
135 var parts = ymd.split('-').map(Number);
136 var Y = parts[0], M = parts[1], D = parts[2];
137 // First guess: UTC midnight minus one hour (CET).
138 var utc = Date.UTC(Y, M - 1, D, 0, 0, 0) - 3600 * 1000;
139 for (var i = 0; i < 5; i++)
140 {
141 var p = this._amsterdamParts(utc);
142 var got = Date.UTC(+p.year, +p.month - 1, +p.day, +p.hour, +p.minute, +(p.second || 0));
143 var want = Date.UTC(Y, M - 1, D, 0, 0, 0);
144 utc += (want - got);
145 }
146 return Math.floor(utc / 1000);
147};
148
149Rules._amsterdamParts = function (unixMs)
150{
151 var fmt = new Intl.DateTimeFormat('en-US', {
152 timeZone: 'Europe/Amsterdam',
153 year: 'numeric', month: '2-digit', day: '2-digit',
154 hour: '2-digit', minute: '2-digit', second: '2-digit',
155 hourCycle: 'h23'
156 });
157 var p = {};
158 fmt.formatToParts(new Date(unixMs)).forEach(function (x)
159 {
160 if (x.type !== 'literal') { p[x.type] = x.value; }
161 });
162 return p;
163};
164
165Rules.amsterdamYmd = function (unix)
166{
167 var p = this._amsterdamParts(unix * 1000);
168 return p.year + '-' + p.month + '-' + p.day;
169};
170
171/**
172 * Wall-clock HH:MM on a civil date in Europe/Amsterdam, as unix seconds.
173 * Matches PHP DateTime::modify("+N minutes") from midnight: civil minutes
174 * walk through DST (spring-forward shortens, fall-back lengthens).
175 * Non-existent times (e.g. 02:30 on a spring-forward day) land after the gap.
176 */
177Rules.amsterdamWallclock = function (ymd, hm)
178{
179 var t = this.amsterdamMidnight(ymd);
180 var target = this.hmToMinutes(hm);
181 if (target === 0)
182 { return t; }
183 for (var i = 0; i < 8; i++)
184 {
185 var p = this._amsterdamParts(t * 1000);
186 var day = p.year + '-' + p.month + '-' + p.day;
187 var got = (+p.hour) * 60 + (+p.minute);
188 if (day !== ymd)
189 {
190 // Overflowed into another civil day (should be rare for HH:MM).
191 var sign = day > ymd ? 1 : -1;
192 got += sign * 1440;
193 }
194 var remaining = target - got;
195 if (remaining === 0)
196 { return t; }
197 // If we oscillate around a spring-forward gap, take the later side.
198 t += remaining * 60;
199 }
200 return t;
201};
202
203Rules.addDaysYmd = function (ymd, days)
204{
205 // Add calendar days via Amsterdam midnight + 26h, then read the ymd and
206 // correct — safer: midnight + days*24h may skip/double; use Date in UTC
207 // of the civil date then shift.
208 var p = ymd.split('-').map(Number);
209 var d = new Date(Date.UTC(p[0], p[1] - 1, p[2] + days));
210 var mm = d.getUTCMonth() + 1, dd = d.getUTCDate();
211 return d.getUTCFullYear() + '-' + (mm < 10 ? '0' : '') + mm + '-' + (dd < 10 ? '0' : '') + dd;
212};
213
214Rules.ceasefireIntervals = function (ranges, now)
215{
216 ranges = this.normalizeRanges(ranges);
217 if (!ranges.length) { return []; }
218 var anchor = this.amsterdamYmd(now);
219 var intervals = [];
220 for (var d = -1; d <= 3; d++)
221 {
222 var ymd = this.addDaysYmd(anchor, d);
223 var next = this.addDaysYmd(anchor, d + 1);
224 for (var i = 0; i < ranges.length; i++)
225 {
226 var r = ranges[i];
227 var startM = this.hmToMinutes(r.start);
228 var endM = this.hmToMinutes(r.end);
229 var startTs = this.amsterdamWallclock(ymd, r.start);
230 var endTs = endM > startM
231 ? this.amsterdamWallclock(ymd, r.end)
232 : this.amsterdamWallclock(next, r.end);
233 if (endTs > startTs) { intervals.push([startTs, endTs]); }
234 }
235 }
236 intervals.sort(function (a, b) { return a[0] - b[0] || a[1] - b[1]; });
237 var merged = [];
238 for (var j = 0; j < intervals.length; j++)
239 {
240 var iv = intervals[j];
241 if (!merged.length || iv[0] > merged[merged.length - 1][1])
242 { merged.push(iv.slice()); }
243 else
244 { merged[merged.length - 1][1] = Math.max(merged[merged.length - 1][1], iv[1]); }
245 }
246 return merged;
247};
248
249Rules.ceasefireInfo = function (rules, now)
250{
251 if (!rules || !rules.ceasefire) { return null; }
252 var ranges = this.normalizeRanges(rules.ceasefire_ranges);
253 if (!ranges.length) { return null; }
254 var intervals = this.ceasefireIntervals(ranges, now);
255 var active = false, until = null;
256 for (var i = 0; i < intervals.length; i++)
257 {
258 var iv = intervals[i];
259 if (now >= iv[0] && now < iv[1])
260 {
261 active = true;
262 until = iv[1];
263 break;
264 }
265 if (iv[0] > now && (until === null || iv[0] < until))
266 { until = iv[0]; }
267 }
268 return {
269 enabled: true,
270 active: active,
271 until: until,
272 seconds: until !== null ? Math.max(0, until - now) : 0,
273 ranges: ranges
274 };
275};
276
277Rules.formatHms = function (seconds)
278{
279 seconds = Math.max(0, Math.floor(seconds));
280 var h = Math.floor(seconds / 3600);
281 var m = Math.floor((seconds % 3600) / 60);
282 var s = seconds % 60;
283 function pad (n) { return n < 10 ? '0' + n : String(n); }
284 return pad(h) + ':' + pad(m) + ':' + pad(s);
285};
286
287if (typeof module !== 'undefined' && module.exports)
288{ module.exports = Rules; }
289else if (typeof globalThis !== 'undefined')
290{ globalThis.Rules = Rules; }
291
292/**
293 * Map a 0–1 position along the timeline track to a spend index 0..n
294 * (0 = before the first spend, n = after the last / live). Nearest event.
295 */
296Rules.timelineIndex = function (t, n)
297{
298 n = n | 0;
299 if (n <= 0) { return 0; }
300 t = Math.max(0, Math.min(1, Number(t) || 0));
301 return Math.round(t * n);
302};