-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheligibility.js
More file actions
354 lines (325 loc) · 10.4 KB
/
Copy patheligibility.js
File metadata and controls
354 lines (325 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Pure, framework-free eligibility engine.
//
// Single source of truth for "can the current user act on this teammate
// right now?" given the project schedule, recurring availability, and a
// few cooldowns. Intentionally NO React imports so it stays trivially
// unit-testable and reusable from server-style logic later.
export const GRACE_FRACTION = 0.2;
export const LATE_FRACTION = 0.7;
export const SPARK_COOLDOWN_MS = 60 * 1000;
export const CHEER_COOLDOWN_MS = 5 * 1000;
const MS_PER_MIN = 60 * 1000;
const MS_PER_DAY = 24 * 60 * MS_PER_MIN;
// ---------- Time helpers ----------
function toMs(input) {
if (input == null) return null;
if (typeof input === "number") return input;
if (input instanceof Date) return input.getTime();
const t = Date.parse(input);
return Number.isNaN(t) ? null : t;
}
function startOfLocalDay(ms) {
const d = new Date(ms);
d.setHours(0, 0, 0, 0);
return d.getTime();
}
function localWeekday(ms) {
// 0 = Sunday .. 6 = Saturday — matches Postgres dow.
return new Date(ms).getDay();
}
function localMinutes(ms) {
const d = new Date(ms);
return d.getHours() * 60 + d.getMinutes();
}
// ---------- Availability ----------
// slots: array of { weekday: 0..6, start_minute: 0..1439, end_minute: 1..1440 }
// "when" is a ms epoch.
//
// We treat slots as recurring weekly intervals and check membership.
// (TZ handling is currently best-effort: we use the browser's local TZ.
// The DB stores tz alongside slots so a future enhancement can adjust.)
export function isInAvailability(slots, when) {
if (!slots || slots.length === 0) {
// No availability declared = always available. This keeps the demo
// usable even before anyone configures their calendar.
return true;
}
const w = localWeekday(when);
const m = localMinutes(when);
return slots.some(
(s) => s.weekday === w && m >= s.start_minute && m < s.end_minute
);
}
// Find the next future availability slot start (ms epoch) given recurring slots.
// Returns null if no slots configured.
export function nextAvailabilityStart(slots, fromMs) {
if (!slots || slots.length === 0) return null;
const baseDay = startOfLocalDay(fromMs);
for (let dayOffset = 0; dayOffset < 8; dayOffset += 1) {
const dayStart = baseDay + dayOffset * MS_PER_DAY;
const wd = localWeekday(dayStart);
const todays = slots
.filter((s) => s.weekday === wd)
.sort((a, b) => a.start_minute - b.start_minute);
for (const slot of todays) {
const candidate = dayStart + slot.start_minute * MS_PER_MIN;
if (candidate > fromMs) return candidate;
}
}
return null;
}
// ---------- Window state ----------
// Returns { kind, progress (0..1 inside the window), msToOpen, msToGraceEnd,
// msToWindowEnd } with kind one of:
// asleep | grace | open | late | working | done | verified | declined | expired
export function windowState(task, when) {
if (!task) return { kind: "asleep", progress: 0 };
// Lifecycle states short-circuit the time math.
if (task.status === "working") return { kind: "working", progress: 1 };
if (task.status === "done") return { kind: "done", progress: 1 };
if (task.status === "verified") return { kind: "verified", progress: 1 };
if (task.status === "declined") return { kind: "declined", progress: 0 };
if (task.status === "expired") return { kind: "expired", progress: 1 };
const start = toMs(task.scheduled_start_at);
const end = toMs(task.scheduled_end_at);
if (start == null || end == null || end <= start) {
return { kind: "asleep", progress: 0 };
}
if (when < start) {
return {
kind: "asleep",
progress: 0,
msToOpen: start - when,
msToGraceEnd: start + (end - start) * GRACE_FRACTION - when,
msToWindowEnd: end - when,
};
}
if (when >= end) {
return { kind: "expired", progress: 1, msToWindowEnd: 0 };
}
const length = end - start;
const progress = (when - start) / length;
const graceEnd = start + length * GRACE_FRACTION;
const lateStart = start + length * LATE_FRACTION;
if (when < graceEnd) {
return {
kind: "grace",
progress,
msToGraceEnd: graceEnd - when,
msToWindowEnd: end - when,
};
}
if (when < lateStart) {
return {
kind: "open",
progress,
msToWindowEnd: end - when,
msToLate: lateStart - when,
};
}
return {
kind: "late",
progress,
msToWindowEnd: end - when,
};
}
// ---------- Action eligibility ----------
//
// Each helper returns { ok, reason, unlocksAt? } so the UI can show a
// useful tooltip / countdown when the action is locked.
export function canStart(self, selfTask, when) {
if (!self || !selfTask) {
return { ok: false, reason: "No task assigned." };
}
if (selfTask.status === "working") {
return { ok: false, reason: "You are already working." };
}
if (!["accepted", "idle"].includes(selfTask.status)) {
return { ok: false, reason: "Task is not in a startable state." };
}
const ws = windowState(selfTask, when);
if (ws.kind === "asleep") {
return {
ok: false,
reason: "Window has not opened yet.",
unlocksAt: when + (ws.msToOpen ?? 0),
};
}
if (ws.kind === "expired") {
return { ok: false, reason: "Window has closed." };
}
return { ok: true };
}
export function canSpark({
actor,
target,
targetTask,
targetAvailability,
lastSparkAt,
when,
}) {
if (!actor || !target) return { ok: false, reason: "No target." };
if (actor.id === target.id) {
return { ok: false, reason: "Cannot spark yourself." };
}
if (target.status !== "idle") {
return { ok: false, reason: `${target.name} is already ${target.status}.` };
}
const sparkLastMs = toMs(lastSparkAt);
if (sparkLastMs != null) {
const sinceSpark = when - sparkLastMs;
if (sinceSpark < SPARK_COOLDOWN_MS) {
return {
ok: false,
reason: "Spark cooldown.",
unlocksAt: sparkLastMs + SPARK_COOLDOWN_MS,
};
}
}
if (!targetTask) {
return {
ok: false,
reason: `${target.name} has no active task — nothing to spark.`,
};
}
if (!isInAvailability(targetAvailability, when)) {
const nextStart = nextAvailabilityStart(targetAvailability, when);
return {
ok: false,
reason: `${target.name} is off-duty.`,
unlocksAt: nextStart,
};
}
const ws = windowState(targetTask, when);
switch (ws.kind) {
case "asleep":
return {
ok: false,
reason: `${target.name}'s window opens later.`,
unlocksAt: when + (ws.msToOpen ?? 0),
};
case "grace":
return {
ok: false,
reason: `Self-start grace — let ${target.name} start on their own.`,
unlocksAt: when + (ws.msToGraceEnd ?? 0),
};
case "open":
case "late":
return { ok: true };
case "expired":
return { ok: false, reason: `${target.name}'s window has closed.` };
case "done":
case "verified":
return { ok: false, reason: `${target.name} already finished.` };
default:
return { ok: false, reason: `${target.name} is busy.` };
}
}
export function canChallenge({
actor,
target,
targetTask,
targetAvailability,
when,
}) {
if (!actor || !target) return { ok: false, reason: "No target." };
if (actor.id === target.id) {
return { ok: false, reason: "Cannot challenge yourself." };
}
if (!targetTask) return { ok: false, reason: "No task to challenge." };
if (!isInAvailability(targetAvailability, when)) {
const nextStart = nextAvailabilityStart(targetAvailability, when);
return {
ok: false,
reason: `${target.name} is off-duty.`,
unlocksAt: nextStart,
};
}
const ws = windowState(targetTask, when);
if (ws.kind === "late" && ["idle", "working"].includes(target.status)) {
return { ok: true };
}
if (ws.kind === "open") {
return {
ok: false,
reason: "Challenge unlocks at 70% of the window.",
unlocksAt:
when + (ws.msToLate ?? Math.max(0, (ws.msToWindowEnd ?? 0) * 0.5)),
};
}
if (ws.kind === "grace") {
return {
ok: false,
reason: "Cannot challenge during the self-start grace.",
unlocksAt: when + (ws.msToGraceEnd ?? 0),
};
}
if (ws.kind === "asleep") {
return {
ok: false,
reason: `${target.name}'s window has not opened.`,
unlocksAt: when + (ws.msToOpen ?? 0),
};
}
return { ok: false, reason: `${target.name} is not challengeable.` };
}
// Can `actor` claim a passed-and-orphaned task right now?
//
// Rules:
// - Task must be unassigned and `proposed`.
// - Actor must NOT be in the task's `passed_by` set (no take-backs).
// - Actor's calendar must say they're available now (or no calendar
// declared, which we treat as always-available for demo simplicity).
// - Window state must NOT be `expired` — once the deadline is gone the
// task is dead, not orphaned.
export function canClaim({ actor, task, actorAvailability, when }) {
if (!actor) return { ok: false, reason: "Sign in first." };
if (!task) return { ok: false, reason: "No task." };
if (task.assignee_id) {
return { ok: false, reason: "Already taken." };
}
if (task.status !== "proposed") {
return { ok: false, reason: "Not claimable right now." };
}
const passedBy = Array.isArray(task.passed_by) ? task.passed_by : [];
if (passedBy.includes(actor.id)) {
return { ok: false, reason: "You already passed this one." };
}
if (!isInAvailability(actorAvailability, when)) {
const nextStart = nextAvailabilityStart(actorAvailability, when);
return {
ok: false,
reason: "You're off-duty.",
unlocksAt: nextStart,
};
}
const ws = windowState(task, when);
if (ws.kind === "expired") {
return { ok: false, reason: "Window has closed." };
}
return { ok: true };
}
export function canCheer({ actor, target, targetAvailability, lastCheerAt, when }) {
if (!actor || !target) return { ok: false, reason: "No target." };
if (actor.id === target.id) {
return { ok: false, reason: "Cannot cheer yourself." };
}
const lastMs = toMs(lastCheerAt);
if (lastMs != null && when - lastMs < CHEER_COOLDOWN_MS) {
return {
ok: false,
reason: "Cheer cooldown.",
unlocksAt: lastMs + CHEER_COOLDOWN_MS,
};
}
if (!isInAvailability(targetAvailability, when)) {
const nextStart = nextAvailabilityStart(targetAvailability, when);
return {
ok: false,
reason: `${target.name} is off-duty.`,
unlocksAt: nextStart,
};
}
return { ok: true };
}