feat(call-to-play): retain terminal outcomes
Keep complete running and cancelled histories visible for fifteen minutes so peers retain the roster, chat, and outcome long enough to understand what happened. Compact them to terminal tombstones afterward without charging settled calls against the active-history limit. Model running and cancelled as durable read-only frontend states, exclude them from active badges, prune retired raw events, and document the lifecycle. Add peer scenario S49 to prove a late joiner reconstructs a terminal call with its roster and chat intact. Test Plan: - just fmt - just clippy - just test - just frontend-test - just build - just peer-cli-tests S48 S49 - python3 -m py_compile crates/lanspread-peer-cli/scripts/run_extended_scenarios.py - git diff --cached --check
This commit is contained in:
@@ -6,14 +6,20 @@ import {
|
||||
} from './types';
|
||||
|
||||
export const CHECKIN_LEAD_MS = 15 * 60_000;
|
||||
export const STARTED_RETENTION_MS = 3_000;
|
||||
export const EXPIRED_RETENTION_MS = 5 * 60_000;
|
||||
export const TERMINAL_RETENTION_MS = 15 * 60_000;
|
||||
|
||||
export type CallToPlayPhase = 'now' | 'scheduled' | 'checkin';
|
||||
export type CallToPlayStatus = 'started' | 'expired' | 'ready' | 'soon' | 'scheduled' | 'call';
|
||||
export type CallToPlayStatus =
|
||||
| 'running'
|
||||
| 'cancelled'
|
||||
| 'expired'
|
||||
| 'ready'
|
||||
| 'soon'
|
||||
| 'scheduled'
|
||||
| 'call';
|
||||
|
||||
interface MutableNomination extends Nomination {
|
||||
cancelled: boolean;
|
||||
messageIds: Set<string>;
|
||||
}
|
||||
|
||||
@@ -57,8 +63,30 @@ export const inCountOf = (nomination: Nomination, now: number): number =>
|
||||
!isReady(participant, now) && participant.status === 'in'
|
||||
).length;
|
||||
|
||||
export const isTerminal = (nomination: Nomination): boolean =>
|
||||
nomination.state === 'running' || nomination.state === 'cancelled';
|
||||
|
||||
export const activeCallCount = (nominations: ReadonlyArray<Nomination>): number =>
|
||||
nominations.filter(nomination => !isTerminal(nomination)).length;
|
||||
|
||||
export const sortNominations = (
|
||||
nominations: ReadonlyArray<Nomination>,
|
||||
): Nomination[] => [...nominations].sort((left, right) => {
|
||||
const terminalRank = Number(isTerminal(left)) - Number(isTerminal(right));
|
||||
if (terminalRank !== 0) return terminalRank;
|
||||
if (isTerminal(left) && isTerminal(right)) {
|
||||
return (right.terminalAt ?? 0) - (left.terminalAt ?? 0)
|
||||
|| left.id.localeCompare(right.id);
|
||||
}
|
||||
return left.deadline - right.deadline
|
||||
|| right.createdAt - left.createdAt
|
||||
|| left.id.localeCompare(right.id);
|
||||
});
|
||||
|
||||
export const statusOf = (nomination: Nomination, now: number): CallToPlayStatus => {
|
||||
if (nomination.state === 'started') return 'started';
|
||||
if (nomination.state === 'running' || nomination.state === 'cancelled') {
|
||||
return nomination.state;
|
||||
}
|
||||
if (now >= nomination.deadline) return 'expired';
|
||||
if (nomination.state === 'done' || readyCountOf(nomination, now) >= nomination.maxPlayers) {
|
||||
return 'ready';
|
||||
@@ -71,6 +99,39 @@ export const reduceCallToPlayEvents = (
|
||||
input: ReadonlyArray<CallToPlayEvent>,
|
||||
now: number,
|
||||
): Nomination[] => {
|
||||
const nominations = [...groupEvents(input).values()]
|
||||
.map(events => deriveNomination(events, now))
|
||||
.filter((nomination): nomination is Nomination => nomination !== null);
|
||||
return sortNominations(nominations);
|
||||
};
|
||||
|
||||
export const pruneCallToPlayEvents = (
|
||||
previous: ReadonlyMap<string, CallToPlayEvent>,
|
||||
now: number,
|
||||
): ReadonlyMap<string, CallToPlayEvent> => {
|
||||
const retiredEventIds = new Set<string>();
|
||||
for (const events of groupEvents([...previous.values()]).values()) {
|
||||
if (deriveNomination(events, now) !== null) continue;
|
||||
|
||||
const hasCreate = events.some(event => createPayload(event.action) !== null);
|
||||
const expiredTombstone = events.some(event =>
|
||||
(event.action === 'Start' || event.action === 'Cancel')
|
||||
&& now - event.at > TERMINAL_RETENTION_MS
|
||||
);
|
||||
if (hasCreate || expiredTombstone) {
|
||||
for (const event of events) retiredEventIds.add(event.id);
|
||||
}
|
||||
}
|
||||
if (retiredEventIds.size === 0) return previous;
|
||||
|
||||
const next = new Map(previous);
|
||||
for (const eventId of retiredEventIds) next.delete(eventId);
|
||||
return next;
|
||||
};
|
||||
|
||||
const groupEvents = (
|
||||
input: ReadonlyArray<CallToPlayEvent>,
|
||||
): Map<string, CallToPlayEvent[]> => {
|
||||
const unique = new Map(input.map(event => [event.id, event]));
|
||||
const byCall = new Map<string, CallToPlayEvent[]>();
|
||||
for (const event of unique.values()) {
|
||||
@@ -78,67 +139,63 @@ export const reduceCallToPlayEvents = (
|
||||
events.push(event);
|
||||
byCall.set(event.call_id, events);
|
||||
}
|
||||
return byCall;
|
||||
};
|
||||
|
||||
const nominations: Nomination[] = [];
|
||||
for (const events of byCall.values()) {
|
||||
events.sort(compareEvents);
|
||||
const create = events.find(event => createPayload(event.action) !== null);
|
||||
if (!create) continue;
|
||||
const payload = createPayload(create.action);
|
||||
if (!payload) continue;
|
||||
const deriveNomination = (
|
||||
events: CallToPlayEvent[],
|
||||
now: number,
|
||||
): Nomination | null => {
|
||||
events.sort(compareEvents);
|
||||
const create = events.find(event => createPayload(event.action) !== null);
|
||||
if (!create) return null;
|
||||
const payload = createPayload(create.action);
|
||||
if (!payload) return null;
|
||||
|
||||
const nomination: MutableNomination = {
|
||||
id: create.call_id,
|
||||
gameId: payload.game_id,
|
||||
creatorId: create.actor_id,
|
||||
creator: create.actor_name,
|
||||
maxPlayers: payload.max_players,
|
||||
createdAt: create.at,
|
||||
scheduledFor: payload.scheduled_for,
|
||||
deadline: payload.deadline,
|
||||
participants: {
|
||||
[create.actor_id]: {
|
||||
name: create.actor_name,
|
||||
status: payload.scheduled_for === null ? 'ready' : 'in',
|
||||
joinedAt: create.at,
|
||||
},
|
||||
const nomination: MutableNomination = {
|
||||
id: create.call_id,
|
||||
gameId: payload.game_id,
|
||||
creatorId: create.actor_id,
|
||||
creator: create.actor_name,
|
||||
maxPlayers: payload.max_players,
|
||||
createdAt: create.at,
|
||||
scheduledFor: payload.scheduled_for,
|
||||
deadline: payload.deadline,
|
||||
participants: {
|
||||
[create.actor_id]: {
|
||||
name: create.actor_name,
|
||||
status: payload.scheduled_for === null ? 'ready' : 'in',
|
||||
joinedAt: create.at,
|
||||
},
|
||||
messages: [],
|
||||
state: 'open',
|
||||
cancelled: false,
|
||||
messageIds: new Set(),
|
||||
};
|
||||
},
|
||||
messages: [],
|
||||
state: 'open',
|
||||
terminalAt: null,
|
||||
messageIds: new Set(),
|
||||
};
|
||||
|
||||
for (const event of events) {
|
||||
if (compareEvents(event, create) <= 0 || nomination.cancelled) continue;
|
||||
applyEvent(nomination, event);
|
||||
}
|
||||
|
||||
if (nomination.cancelled) continue;
|
||||
if (nomination.state === 'open'
|
||||
&& (readyCountOf(nomination, now) >= nomination.maxPlayers || now >= nomination.deadline)
|
||||
) {
|
||||
nomination.state = 'done';
|
||||
}
|
||||
if (nomination.state === 'started'
|
||||
&& now - (nomination.startedAt ?? now) > STARTED_RETENTION_MS
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
if (nomination.state !== 'started'
|
||||
&& now - nomination.deadline > EXPIRED_RETENTION_MS
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const { cancelled: _, messageIds: __, ...result } = nomination;
|
||||
nominations.push(result);
|
||||
for (const event of events) {
|
||||
if (compareEvents(event, create) > 0) applyEvent(nomination, event);
|
||||
}
|
||||
|
||||
return nominations.sort((a, b) => b.createdAt - a.createdAt || a.id.localeCompare(b.id));
|
||||
if (nomination.state === 'open'
|
||||
&& (readyCountOf(nomination, now) >= nomination.maxPlayers || now >= nomination.deadline)
|
||||
) {
|
||||
nomination.state = 'done';
|
||||
}
|
||||
if (isTerminal(nomination)) {
|
||||
if (now - (nomination.terminalAt ?? now) > TERMINAL_RETENTION_MS) return null;
|
||||
} else if (now - nomination.deadline > EXPIRED_RETENTION_MS) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { messageIds: _, ...result } = nomination;
|
||||
return result;
|
||||
};
|
||||
|
||||
const applyEvent = (nomination: MutableNomination, event: CallToPlayEvent): void => {
|
||||
if (isTerminal(nomination)) return;
|
||||
|
||||
const action = event.action;
|
||||
if (typeof action === 'string') {
|
||||
applyUnitAction(nomination, event, action);
|
||||
@@ -147,7 +204,6 @@ const applyEvent = (nomination: MutableNomination, event: CallToPlayEvent): void
|
||||
|
||||
const response = respondPayload(action);
|
||||
if (response) {
|
||||
if (nomination.state === 'started') return;
|
||||
const existing = nomination.participants[event.actor_id];
|
||||
nomination.participants[event.actor_id] = {
|
||||
name: event.actor_name,
|
||||
@@ -159,7 +215,7 @@ const applyEvent = (nomination: MutableNomination, event: CallToPlayEvent): void
|
||||
}
|
||||
|
||||
const message = messagePayload(action);
|
||||
if (message && nomination.state !== 'started' && !nomination.messageIds.has(message.message_id)) {
|
||||
if (message && !nomination.messageIds.has(message.message_id)) {
|
||||
nomination.messageIds.add(message.message_id);
|
||||
nomination.messages.push({
|
||||
id: message.message_id,
|
||||
@@ -175,7 +231,6 @@ const applyEvent = (nomination: MutableNomination, event: CallToPlayEvent): void
|
||||
const extension = addTimePayload(action);
|
||||
if (extension
|
||||
&& event.actor_id === nomination.creatorId
|
||||
&& nomination.state !== 'started'
|
||||
) {
|
||||
nomination.deadline = extension.deadline;
|
||||
nomination.state = 'open';
|
||||
@@ -189,7 +244,6 @@ const applyUnitAction = (
|
||||
): void => {
|
||||
switch (action) {
|
||||
case 'Rsvp': {
|
||||
if (nomination.state === 'started') return;
|
||||
const existing = nomination.participants[event.actor_id];
|
||||
nomination.participants[event.actor_id] = {
|
||||
name: event.actor_name,
|
||||
@@ -199,19 +253,20 @@ const applyUnitAction = (
|
||||
break;
|
||||
}
|
||||
case 'Leave':
|
||||
if (event.actor_id !== nomination.creatorId && nomination.state !== 'started') {
|
||||
if (event.actor_id !== nomination.creatorId) {
|
||||
delete nomination.participants[event.actor_id];
|
||||
}
|
||||
break;
|
||||
case 'Cancel':
|
||||
if (event.actor_id === nomination.creatorId && nomination.state !== 'started') {
|
||||
nomination.cancelled = true;
|
||||
if (event.actor_id === nomination.creatorId) {
|
||||
nomination.state = 'cancelled';
|
||||
nomination.terminalAt = event.at;
|
||||
}
|
||||
break;
|
||||
case 'Start':
|
||||
if (event.actor_id === nomination.creatorId && nomination.state !== 'started') {
|
||||
nomination.state = 'started';
|
||||
nomination.startedAt = event.at;
|
||||
if (event.actor_id === nomination.creatorId) {
|
||||
nomination.state = 'running';
|
||||
nomination.terminalAt = event.at;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -113,8 +113,8 @@ export interface Nomination {
|
||||
deadline: number;
|
||||
participants: Record<string, CallToPlayParticipant>;
|
||||
messages: CallToPlayMessage[];
|
||||
state: 'open' | 'done' | 'started';
|
||||
startedAt?: number;
|
||||
state: 'open' | 'done' | 'running' | 'cancelled';
|
||||
terminalAt: number | null;
|
||||
}
|
||||
|
||||
export type CallToPlayAction =
|
||||
|
||||
Reference in New Issue
Block a user