Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 2x 2x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 2x 2x 2x 2x 2x 2x 2x 1x 2x 1x 2x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x | import type { GameResultsReport } from '@/lib/arcade/game-sdk/types'
import type { GameBreakEndReason, SessionFlowState, SessionPlan } from '@/db/schema/session-plans'
export type SessionFlowEvent =
| { type: 'PART_TRANSITION_STARTED' }
| { type: 'PART_TRANSITION_COMPLETED'; shouldRunBreak: boolean }
| { type: 'BREAK_STARTED'; game?: string | null }
| { type: 'BREAK_FINISHED'; reason: GameBreakEndReason; results?: GameResultsReport | null }
| { type: 'BREAK_RESULTS_ACKED' }
| { type: 'SESSION_COMPLETED' }
| { type: 'SESSION_ABANDONED' }
export class InvalidFlowTransitionError extends Error {
constructor(
message: string,
public readonly state: SessionFlowState,
public readonly eventType: SessionFlowEvent['type']
) {
super(message)
this.name = 'InvalidFlowTransitionError'
}
}
export interface AppliedFlowEvent {
changed: boolean
patch: {
flowState?: SessionFlowState
flowUpdatedAt?: Date
flowVersion?: number
breakStartedAt?: Date | null
breakReason?: GameBreakEndReason | null
breakSelectedGame?: string | null
breakResults?: GameResultsReport | null
}
}
function sameResults(
a: GameResultsReport | null | undefined,
b: GameResultsReport | null | undefined
): boolean {
return JSON.stringify(a ?? null) === JSON.stringify(b ?? null)
}
function noChange(): AppliedFlowEvent {
return { changed: false, patch: {} }
}
export function applyFlowEvent(plan: SessionPlan, event: SessionFlowEvent): AppliedFlowEvent {
const current = plan.flowState ?? 'practicing'
const currentVersion = plan.flowVersion ?? 0
const now = new Date()
const commit = (patch: AppliedFlowEvent['patch']): AppliedFlowEvent => ({
changed: true,
patch: {
...patch,
flowUpdatedAt: now,
flowVersion: currentVersion + 1,
},
})
switch (event.type) {
case 'PART_TRANSITION_STARTED': {
if (current === 'part_transition') return noChange()
if (current !== 'practicing') {
throw new InvalidFlowTransitionError(
`PART_TRANSITION_STARTED not allowed from ${current}`,
current,
event.type
)
}
return commit({
flowState: 'part_transition',
breakStartedAt: null,
breakReason: null,
breakSelectedGame: null,
breakResults: null,
})
}
case 'PART_TRANSITION_COMPLETED': {
if (event.shouldRunBreak) {
if (current === 'break_pending' || current === 'break_active') return noChange()
} else if (current === 'practicing') {
return noChange()
}
if (current !== 'part_transition') {
throw new InvalidFlowTransitionError(
`PART_TRANSITION_COMPLETED not allowed from ${current}`,
current,
event.type
)
}
if (!event.shouldRunBreak) {
return commit({
flowState: 'practicing',
breakStartedAt: null,
breakReason: null,
breakSelectedGame: null,
breakResults: null,
})
}
return commit({
flowState: 'break_pending',
breakStartedAt: plan.breakStartedAt ?? now,
breakReason: null,
breakSelectedGame: null,
breakResults: null,
})
}
case 'BREAK_STARTED': {
const selectedGame = event.game ?? null
if (current === 'break_active') {
if ((plan.breakSelectedGame ?? null) === selectedGame || event.game === undefined) {
return noChange()
}
return commit({
breakSelectedGame: selectedGame,
})
}
if (current !== 'break_pending') {
throw new InvalidFlowTransitionError(
`BREAK_STARTED not allowed from ${current}`,
current,
event.type
)
}
return commit({
flowState: 'break_active',
breakStartedAt: plan.breakStartedAt ?? now,
breakSelectedGame: selectedGame,
})
}
case 'BREAK_FINISHED': {
const targetState: SessionFlowState =
event.reason === 'gameFinished' ? 'break_results' : 'practicing'
const targetResults = event.reason === 'gameFinished' ? (event.results ?? null) : null
const alreadyApplied =
current === targetState &&
(plan.breakReason ?? null) === event.reason &&
sameResults(plan.breakResults, targetResults)
if (alreadyApplied) return noChange()
if (current !== 'break_active' && current !== 'break_pending') {
throw new InvalidFlowTransitionError(
`BREAK_FINISHED not allowed from ${current}`,
current,
event.type
)
}
return commit({
flowState: targetState,
breakReason: event.reason,
breakResults: targetResults,
})
}
case 'BREAK_RESULTS_ACKED': {
const alreadyApplied =
current === 'practicing' &&
plan.breakReason === null &&
(plan.breakSelectedGame ?? null) === null &&
plan.breakResults == null
if (alreadyApplied) return noChange()
if (current !== 'break_results') {
throw new InvalidFlowTransitionError(
`BREAK_RESULTS_ACKED not allowed from ${current}`,
current,
event.type
)
}
return commit({
flowState: 'practicing',
breakReason: null,
breakSelectedGame: null,
breakResults: null,
})
}
case 'SESSION_COMPLETED': {
if (current === 'completed') return noChange()
if (current === 'abandoned') {
throw new InvalidFlowTransitionError(
'SESSION_COMPLETED not allowed from abandoned',
current,
event.type
)
}
return commit({
flowState: 'completed',
})
}
case 'SESSION_ABANDONED': {
if (current === 'abandoned') return noChange()
if (current === 'completed') {
throw new InvalidFlowTransitionError(
'SESSION_ABANDONED not allowed from completed',
current,
event.type
)
}
return commit({
flowState: 'abandoned',
})
}
}
}
|