Support Sapho ordering for Great Maker votes
Verify and publish checkpoint container / container (push) Successful in 11m14s
Verify and publish checkpoint container / container (push) Successful in 11m14s
This commit is contained in:
1 parent
e81725e4b2
commit
77d02b3231
18 files changed
+798
-21
No files matched your search
@@ -0,0 +1,128 @@
|
||||
import { reorderOrderedOpportunity } from './ordered-opportunity';
|
||||
|
||||
/** Public ordering history for the original Great Maker voting encounter. */
|
||||
export type SaphoGreatMakerUse = {
|
||||
event: string;
|
||||
player: string;
|
||||
mode: 'first' | 'last';
|
||||
cursor: number;
|
||||
};
|
||||
export type SaphoGreatMakerOrder = {
|
||||
version: 1;
|
||||
original: string[];
|
||||
uses: SaphoGreatMakerUse[];
|
||||
};
|
||||
export type SaphoGreatMakerContext = {
|
||||
encounter: string;
|
||||
turn: number;
|
||||
stormOrder: readonly string[];
|
||||
queue: readonly string[];
|
||||
cursor: number;
|
||||
order?: SaphoGreatMakerOrder;
|
||||
};
|
||||
export type SaphoGreatMakerRequest = {
|
||||
event: string;
|
||||
player: string;
|
||||
mode: 'first' | 'last';
|
||||
};
|
||||
|
||||
export class SaphoGreatMakerError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'SaphoGreatMakerError';
|
||||
}
|
||||
}
|
||||
function requireMaker(value: unknown, message: string): asserts value {
|
||||
if (!value) throw new SaphoGreatMakerError(message);
|
||||
}
|
||||
const identifier = (value: unknown): value is string =>
|
||||
typeof value === 'string' && value.trim().length > 0;
|
||||
const count = (value: unknown): value is number =>
|
||||
typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
|
||||
const ids = (value: unknown): value is readonly string[] =>
|
||||
Array.isArray(value) && value.every(identifier) && new Set(value).size === value.length;
|
||||
const exact = (value: unknown, keys: string): value is Record<string, unknown> =>
|
||||
!!value && typeof value === 'object' && !Array.isArray(value) &&
|
||||
[Object.prototype, null].includes(Object.getPrototypeOf(value)) &&
|
||||
Object.keys(value).sort().join(',') === keys;
|
||||
const same = (left: unknown, right: unknown): boolean => JSON.stringify(left) === JSON.stringify(right);
|
||||
const eventFor = (encounter: string, turn: number, cursor: number, revision: number): string =>
|
||||
`saphoGreatMaker:${JSON.stringify([encounter, turn, cursor, revision])}`;
|
||||
|
||||
/** Construct only when a new recorded ordering is needed; unmodified older
|
||||
* encounters need no added field or changed Great Maker signature. */
|
||||
export function initialSaphoGreatMakerOrder(stormOrder: readonly string[]): SaphoGreatMakerOrder {
|
||||
requireMaker(ids(stormOrder) && stormOrder.length >= 2 && stormOrder.length <= 6,
|
||||
'Great Maker needs its original unique two-to-six-player storm order.');
|
||||
return {version: 1, original: [...stormOrder], uses: []};
|
||||
}
|
||||
|
||||
function applyUse(original: readonly string[], queue: readonly string[], use: SaphoGreatMakerUse): string[] {
|
||||
const remaining = queue.slice(use.cursor);
|
||||
requireMaker(use.mode === 'first' || use.mode === 'last', 'Choose first or last.');
|
||||
requireMaker(identifier(use.player) && remaining.includes(use.player),
|
||||
'Only a player who has not yet voted may change position.');
|
||||
requireMaker(use.mode === 'first' ? remaining[0] !== use.player : remaining.at(-1) !== use.player,
|
||||
'This voter already holds that position.');
|
||||
try {
|
||||
const next = reorderOrderedOpportunity({
|
||||
event: use.event, eligible: original, completed: queue.slice(0, use.cursor),
|
||||
remaining, current: remaining[0] ?? null, currentStarted: false,
|
||||
}, {event: use.event, player: use.player, position: use.mode});
|
||||
return [...next.completed, ...next.remaining];
|
||||
} catch (error) {
|
||||
throw new SaphoGreatMakerError(error instanceof Error ? error.message : 'Invalid Great Maker voter order.');
|
||||
}
|
||||
}
|
||||
|
||||
function validate(context: SaphoGreatMakerContext): string[] {
|
||||
requireMaker(context && typeof context === 'object' && identifier(context.encounter) &&
|
||||
count(context.turn) && context.turn > 0 && ids(context.stormOrder) &&
|
||||
context.stormOrder.length >= 2 && context.stormOrder.length <= 6,
|
||||
'Great Maker needs its original encounter, turn and unique storm order.');
|
||||
const original = [...context.stormOrder];
|
||||
requireMaker(ids(context.queue) && context.queue.length === original.length &&
|
||||
context.queue.every(player => original.includes(player)) &&
|
||||
count(context.cursor) && context.cursor <= context.queue.length,
|
||||
'Great Maker needs its complete voter queue and current vote cursor.');
|
||||
if (context.order === undefined) {
|
||||
requireMaker(same(context.queue, original),
|
||||
'A Great Maker queue without a Sapho record must retain its original storm order.');
|
||||
return original;
|
||||
}
|
||||
const order = context.order;
|
||||
requireMaker(exact(order, 'original,uses,version') && order.version === 1 &&
|
||||
ids(order.original) && same(order.original, original) && Array.isArray(order.uses),
|
||||
'The Sapho vote record must retain its exact original voter order.');
|
||||
let queue = [...original], previousCursor = 0;
|
||||
for (const [revision, use] of order.uses.entries()) {
|
||||
requireMaker(exact(use, 'cursor,event,mode,player') && count(use.cursor) &&
|
||||
use.cursor >= previousCursor && use.cursor <= context.cursor &&
|
||||
use.event === eventFor(context.encounter, context.turn, use.cursor, revision),
|
||||
'Sapho vote history has a stale event, revision or completed voter cursor.');
|
||||
queue = applyUse(original, queue, use);
|
||||
previousCursor = use.cursor;
|
||||
}
|
||||
requireMaker(same(context.queue, queue),
|
||||
'The Great Maker voter queue does not match its original Sapho history.');
|
||||
return original;
|
||||
}
|
||||
|
||||
export function validateSaphoGreatMaker(context: SaphoGreatMakerContext): void {
|
||||
validate(context);
|
||||
}
|
||||
export function saphoGreatMakerEvent(context: SaphoGreatMakerContext): string {
|
||||
validate(context);
|
||||
return eventFor(context.encounter, context.turn, context.cursor, context.order?.uses.length ?? 0);
|
||||
}
|
||||
export function reorderSaphoGreatMaker(
|
||||
context: SaphoGreatMakerContext,
|
||||
request: SaphoGreatMakerRequest,
|
||||
): {queue: string[]; order: SaphoGreatMakerOrder} {
|
||||
const original = validate(context), event = saphoGreatMakerEvent(context);
|
||||
requireMaker(request && typeof request === 'object' && request.event === event,
|
||||
'This Sapho Great Maker opportunity has expired.');
|
||||
const use: SaphoGreatMakerUse = {event, player: request.player, mode: request.mode, cursor: context.cursor};
|
||||
return {queue: applyUse(original, context.queue, use),
|
||||
order: {version: 1, original, uses: [...(context.order?.uses ?? []).map(old => ({...old})), use]}};
|
||||
}
|
||||
Reference in new issue
Block a user