|
| 1 | +import type { ExperimentalMillisecondTimestamp } from "cubing/twisty"; |
| 2 | +import { ScrambleTable } from "../../lib"; |
| 3 | +import type { AttemptScrambleInfo } from "../../lib/AttemptScrambleInfo"; |
| 4 | +import type { |
| 5 | + MatchupCallbackIdentifyingInfo, |
| 6 | + MatchupID, |
| 7 | + ResultForTimedAttempt, |
| 8 | +} from "../../lib/elements/SharedState"; |
| 9 | +import { |
| 10 | + type ResultForTimedAttemptWithPenalty, |
| 11 | + formatResultForTimedAttempt, |
| 12 | +} from "../../lib/vendor/timer.cubing.net/stats"; |
| 13 | + |
| 14 | +declare global { |
| 15 | + interface globalThis { |
| 16 | + app: ScrambleTable; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +async function simulateLatency() { |
| 21 | + await new Promise((resolve) => setTimeout(resolve, Math.random() * 250)); |
| 22 | +} |
| 23 | + |
| 24 | +async function refreshCurrentMatchupsCallback() { |
| 25 | + await simulateLatency(); |
| 26 | + return { |
| 27 | + "matchup-23": "Minh Thai vs. Abraham Lincoln", |
| 28 | + "matchup-24": "Gilgamesh vs. Enkidu", |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +function matchupSelectedCallback(matchupID: MatchupID) { |
| 33 | + switch (matchupID) { |
| 34 | + case "matchup-23": { |
| 35 | + app.displays[0].setScramble({ |
| 36 | + competitorName: "Minh Thai", |
| 37 | + eventID: "333", |
| 38 | + matchupID: "matchup-23", |
| 39 | + competitorMatchupID: "minh-thai", |
| 40 | + attemptID: "1", |
| 41 | + scrambleString: |
| 42 | + "B2 D F2 D2 B2 L2 B2 U B2 U L2 F2 L' F D' B2 D' L D R' U", |
| 43 | + }); |
| 44 | + app.displays[0].setScramble({ |
| 45 | + competitorName: "Abraham Lincoln", |
| 46 | + eventID: "333", |
| 47 | + matchupID: "matchup-23", |
| 48 | + competitorMatchupID: "abe", |
| 49 | + attemptID: "1", |
| 50 | + scrambleString: |
| 51 | + "B2 D F2 D2 B2 L2 B2 U B2 U L2 F2 L' F D' B2 D' L D R' U", |
| 52 | + }); |
| 53 | + return; |
| 54 | + } |
| 55 | + case "matchup-24": { |
| 56 | + app.displays[0].setScramble({ |
| 57 | + competitorName: "Gilgamesh", |
| 58 | + eventID: "333", |
| 59 | + matchupID: "matchup-24", |
| 60 | + competitorMatchupID: "gilgamesh", |
| 61 | + attemptID: "1", |
| 62 | + scrambleString: |
| 63 | + "D2 F U R' U' D F L U' R' L2 B D2 R2 L2 F D2 F' L2 F R2", |
| 64 | + }); |
| 65 | + app.displays[0].setScramble({ |
| 66 | + competitorName: "Enkidu", |
| 67 | + eventID: "333", |
| 68 | + matchupID: "matchup-24", |
| 69 | + competitorMatchupID: "enkidu", |
| 70 | + attemptID: "1", |
| 71 | + scrambleString: |
| 72 | + "D2 F U R' U' D F L U' R' L2 B D2 R2 L2 F D2 F' L2 F R2", |
| 73 | + }); |
| 74 | + return; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +const localResultDB: Record< |
| 80 | + number /* display number */, |
| 81 | + ResultForTimedAttemptWithPenalty |
| 82 | +> = {}; |
| 83 | + |
| 84 | +function randomNewResult(): ResultForTimedAttemptWithPenalty { |
| 85 | + if (Math.random() < 0.05) { |
| 86 | + return { |
| 87 | + resultForTimedAttempt: "DNF", |
| 88 | + penaltySeconds: 0, |
| 89 | + }; |
| 90 | + } |
| 91 | + const resultForTimedAttempt = |
| 92 | + Math.floor(Math.random() * 5000) + Math.floor(Math.random() * 5000) + 5000; |
| 93 | + return { |
| 94 | + resultForTimedAttempt, |
| 95 | + penaltySeconds: 0, |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +function accessResult( |
| 100 | + identifyingInfo: MatchupCallbackIdentifyingInfo, |
| 101 | +): ResultForTimedAttemptWithPenalty { |
| 102 | + const uniqueAttemptIdentifier = `${identifyingInfo.matchupID}###${identifyingInfo.competitorMatchupID}###${identifyingInfo.attemptID}`; |
| 103 | + // biome-ignore lint/suspicious/noAssignInExpressions: DRY pattern |
| 104 | + return (localResultDB[uniqueAttemptIdentifier] ??= randomNewResult()); |
| 105 | +} |
| 106 | + |
| 107 | +async function matchupGetResultCallback( |
| 108 | + identifyingInfo: MatchupCallbackIdentifyingInfo, |
| 109 | +): Promise<ResultForTimedAttemptWithPenalty> { |
| 110 | + await simulateLatency(); |
| 111 | + return structuredClone(accessResult(identifyingInfo)); |
| 112 | +} |
| 113 | + |
| 114 | +async function matchupAdjustPenaltyCallback( |
| 115 | + identifyingInfo: MatchupCallbackIdentifyingInfo, |
| 116 | + deltaSeconds: number, |
| 117 | +): Promise<ResultForTimedAttemptWithPenalty> { |
| 118 | + await simulateLatency(); |
| 119 | + const result = accessResult(identifyingInfo); |
| 120 | + |
| 121 | + result.penaltySeconds += deltaSeconds; |
| 122 | + if (result.penaltySeconds < 0) { |
| 123 | + result.penaltySeconds = 0; |
| 124 | + } |
| 125 | + |
| 126 | + return structuredClone(result); |
| 127 | +} |
| 128 | + |
| 129 | +async function matchupFinishAttemptCallback( |
| 130 | + identifyingInfo: MatchupCallbackIdentifyingInfo, |
| 131 | +): Promise<void> { |
| 132 | + const scrambleInfo = await (async (): Promise<AttemptScrambleInfo> => { |
| 133 | + throw new Error("TODO: implement using a proper backend"); |
| 134 | + })(); |
| 135 | + app.displays[identifyingInfo.displayNumber].setScramble(scrambleInfo); |
| 136 | +} |
| 137 | + |
| 138 | +const app = document.body.appendChild( |
| 139 | + new ScrambleTable({ |
| 140 | + competitorScrambleDisplayOptions: { |
| 141 | + resultInput: "show", |
| 142 | + }, |
| 143 | + callbacks: { |
| 144 | + matchupGetResultCallback, |
| 145 | + matchupAdjustPenaltyCallback, |
| 146 | + matchupFinishAttemptCallback, |
| 147 | + refreshCurrentMatchupsCallback, |
| 148 | + resetMatchupCallback: async (matchupID) => |
| 149 | + console.log("Reset matchup:", { matchupID }), |
| 150 | + matchupSelectedCallback, |
| 151 | + }, |
| 152 | + showMatchupsSelection: "show", |
| 153 | + }), |
| 154 | +); |
| 155 | +globalThis.app = app; |
| 156 | + |
| 157 | +app.addEventListener( |
| 158 | + "scramble-cleared", |
| 159 | + (e: CustomEvent<{ displayIndex: number }>) => { |
| 160 | + console.log(`Scramble cleared for display index: ${e.detail.displayIndex}`); |
| 161 | + }, |
| 162 | +); |
| 163 | + |
| 164 | +matchupSelectedCallback("matchup-23"); |
0 commit comments