|
|
@ -1,4 +1,12 @@ |
|
|
|
import { ErrorType, EventResult, IncomingEvent, IncomingEventType, OutgoingEventType } from './gameEvent' |
|
|
|
import { |
|
|
|
|
|
|
|
ErrorType, |
|
|
|
|
|
|
|
EventResult, |
|
|
|
|
|
|
|
EventResultClass, |
|
|
|
|
|
|
|
IncomingEvent, |
|
|
|
|
|
|
|
IncomingEventType, |
|
|
|
|
|
|
|
OutgoingEvent, |
|
|
|
|
|
|
|
OutgoingEventType, |
|
|
|
|
|
|
|
} from './gameEvent' |
|
|
|
import { GamePhase, GameState, oppositePlayer, PlayerSide, PlayerState, RoundStarter } from './gameState' |
|
|
|
import { GamePhase, GameState, oppositePlayer, PlayerSide, PlayerState, RoundStarter } from './gameState' |
|
|
|
import { |
|
|
|
import { |
|
|
|
DieFace, |
|
|
|
DieFace, |
|
|
@ -41,7 +49,7 @@ export function handleEvent(state: GameState, event: IncomingEvent): EventResult |
|
|
|
return handleAbortEvent<GameState>(state, event) |
|
|
|
return handleAbortEvent<GameState>(state, event) |
|
|
|
default: |
|
|
|
default: |
|
|
|
return { |
|
|
|
return { |
|
|
|
newState: state, |
|
|
|
type: EventResultClass.FAILURE, |
|
|
|
error: { |
|
|
|
error: { |
|
|
|
type: ErrorType.UnexpectedError, |
|
|
|
type: ErrorType.UnexpectedError, |
|
|
|
message: 'Event type not yet implemented', |
|
|
|
message: 'Event type not yet implemented', |
|
|
@ -50,7 +58,7 @@ export function handleEvent(state: GameState, event: IncomingEvent): EventResult |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
return { |
|
|
|
return { |
|
|
|
newState: state, |
|
|
|
type: EventResultClass.FAILURE, |
|
|
|
error: { |
|
|
|
error: { |
|
|
|
type: ErrorType.UnexpectedError, |
|
|
|
type: ErrorType.UnexpectedError, |
|
|
|
message: `Failed handling event: ${e}`, |
|
|
|
message: `Failed handling event: ${e}`, |
|
|
@ -73,23 +81,24 @@ export function handleSelectActionEvent<T extends SelectActionState<unknown>>( |
|
|
|
): EventResult<T> { |
|
|
|
): EventResult<T> { |
|
|
|
if (state.gamePhase !== GamePhase.ROUND_START) { |
|
|
|
if (state.gamePhase !== GamePhase.ROUND_START) { |
|
|
|
return { |
|
|
|
return { |
|
|
|
|
|
|
|
type: EventResultClass.FAILURE, |
|
|
|
error: { |
|
|
|
error: { |
|
|
|
type: ErrorType.NotValidRightNow, |
|
|
|
type: ErrorType.NotValidRightNow, |
|
|
|
message: `You can only change actions when the turn has not started yet.`, |
|
|
|
message: `You can only change actions when the turn has not started yet.`, |
|
|
|
}, |
|
|
|
}, |
|
|
|
newState: state, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (index < 0 || index >= state.theme.actions.length || index !== Math.floor(index)) { |
|
|
|
if (index < 0 || index >= state.theme.actions.length || index !== Math.floor(index)) { |
|
|
|
return { |
|
|
|
return { |
|
|
|
|
|
|
|
type: EventResultClass.FAILURE, |
|
|
|
error: { |
|
|
|
error: { |
|
|
|
type: ErrorType.InvalidIndex, |
|
|
|
type: ErrorType.InvalidIndex, |
|
|
|
message: `Select an integer action index between 0 (inclusive) and ${state.theme.actions.length} (exclusive)`, |
|
|
|
message: `Select an integer action index between 0 (inclusive) and ${state.theme.actions.length} (exclusive)`, |
|
|
|
}, |
|
|
|
}, |
|
|
|
newState: state, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return { |
|
|
|
return { |
|
|
|
|
|
|
|
type: EventResultClass.SUCCESS, |
|
|
|
newState: { |
|
|
|
newState: { |
|
|
|
...state, |
|
|
|
...state, |
|
|
|
action: state.theme.actions[index], |
|
|
|
action: state.theme.actions[index], |
|
|
@ -109,16 +118,16 @@ export function handleToggleSelectIndexEvent<D extends { readonly state: DieStat |
|
|
|
): EventResult<T> { |
|
|
|
): EventResult<T> { |
|
|
|
if (state.gamePhase !== GamePhase.TURN_ROLLED) { |
|
|
|
if (state.gamePhase !== GamePhase.TURN_ROLLED) { |
|
|
|
return { |
|
|
|
return { |
|
|
|
|
|
|
|
type: EventResultClass.FAILURE, |
|
|
|
error: { |
|
|
|
error: { |
|
|
|
type: ErrorType.NotValidRightNow, |
|
|
|
type: ErrorType.NotValidRightNow, |
|
|
|
message: `You can only toggle the selected state of a die when dice have been rolled.`, |
|
|
|
message: `You can only toggle the selected state of a die when dice have been rolled.`, |
|
|
|
}, |
|
|
|
}, |
|
|
|
newState: state, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (!isValidIndex(index, state.lastRoll.length)) { |
|
|
|
if (!isValidIndex(index, state.lastRoll.length)) { |
|
|
|
return { |
|
|
|
return { |
|
|
|
newState: state, |
|
|
|
type: EventResultClass.FAILURE, |
|
|
|
error: { |
|
|
|
error: { |
|
|
|
type: ErrorType.InvalidIndex, |
|
|
|
type: ErrorType.InvalidIndex, |
|
|
|
message: `Select an integer action index between 0 (inclusive) and ${state.lastRoll.length} (exclusive)`, |
|
|
|
message: `Select an integer action index between 0 (inclusive) and ${state.lastRoll.length} (exclusive)`, |
|
|
@ -126,6 +135,7 @@ export function handleToggleSelectIndexEvent<D extends { readonly state: DieStat |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return { |
|
|
|
return { |
|
|
|
|
|
|
|
type: EventResultClass.SUCCESS, |
|
|
|
newState: { |
|
|
|
newState: { |
|
|
|
...state, |
|
|
|
...state, |
|
|
|
lastRoll: [ |
|
|
|
lastRoll: [ |
|
|
@ -147,14 +157,15 @@ function handleSelectAllEvent<D extends { readonly state: DieState }, T extends |
|
|
|
): EventResult<T> { |
|
|
|
): EventResult<T> { |
|
|
|
if (state.gamePhase !== GamePhase.TURN_ROLLED) { |
|
|
|
if (state.gamePhase !== GamePhase.TURN_ROLLED) { |
|
|
|
return { |
|
|
|
return { |
|
|
|
|
|
|
|
type: EventResultClass.FAILURE, |
|
|
|
error: { |
|
|
|
error: { |
|
|
|
type: ErrorType.NotValidRightNow, |
|
|
|
type: ErrorType.NotValidRightNow, |
|
|
|
message: `You can only change the selected state of the dice when dice have been rolled.`, |
|
|
|
message: `You can only change the selected state of the dice when dice have been rolled.`, |
|
|
|
}, |
|
|
|
}, |
|
|
|
newState: state, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return { |
|
|
|
return { |
|
|
|
|
|
|
|
type: EventResultClass.SUCCESS, |
|
|
|
newState: { |
|
|
|
newState: { |
|
|
|
...state, |
|
|
|
...state, |
|
|
|
lastRoll: state.lastRoll.map<D>((x) => ({ |
|
|
|
lastRoll: state.lastRoll.map<D>((x) => ({ |
|
|
@ -172,14 +183,15 @@ function handleHoldDiceEvent<D extends { readonly state: DieState }, T extends D |
|
|
|
): EventResult<T> { |
|
|
|
): EventResult<T> { |
|
|
|
if (state.gamePhase !== GamePhase.TURN_ROLLED) { |
|
|
|
if (state.gamePhase !== GamePhase.TURN_ROLLED) { |
|
|
|
return { |
|
|
|
return { |
|
|
|
|
|
|
|
type: EventResultClass.FAILURE, |
|
|
|
error: { |
|
|
|
error: { |
|
|
|
type: ErrorType.NotValidRightNow, |
|
|
|
type: ErrorType.NotValidRightNow, |
|
|
|
message: `You can only toggle the hold state of dice when dice have been rolled.`, |
|
|
|
message: `You can only toggle the hold state of dice when dice have been rolled.`, |
|
|
|
}, |
|
|
|
}, |
|
|
|
newState: state, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return { |
|
|
|
return { |
|
|
|
|
|
|
|
type: EventResultClass.SUCCESS, |
|
|
|
newState: { |
|
|
|
newState: { |
|
|
|
...state, |
|
|
|
...state, |
|
|
|
lastRoll: state.lastRoll.map<D>((v) => { |
|
|
|
lastRoll: state.lastRoll.map<D>((v) => { |
|
|
@ -249,11 +261,11 @@ export interface EndTurnState<P extends EndTurnPlayer> { |
|
|
|
function handleEndTurnEvent<P extends EndTurnPlayer, T extends EndTurnState<P>>(state: T): EventResult<T> { |
|
|
|
function handleEndTurnEvent<P extends EndTurnPlayer, T extends EndTurnState<P>>(state: T): EventResult<T> { |
|
|
|
if (state.gamePhase !== GamePhase.TURN_ROLLED && state.gamePhase !== GamePhase.TURN_FAILED) { |
|
|
|
if (state.gamePhase !== GamePhase.TURN_ROLLED && state.gamePhase !== GamePhase.TURN_FAILED) { |
|
|
|
return { |
|
|
|
return { |
|
|
|
|
|
|
|
type: EventResultClass.FAILURE, |
|
|
|
error: { |
|
|
|
error: { |
|
|
|
type: ErrorType.NotValidRightNow, |
|
|
|
type: ErrorType.NotValidRightNow, |
|
|
|
message: `You can only end your turn when it's in progress.`, |
|
|
|
message: `You can only end your turn when it's in progress.`, |
|
|
|
}, |
|
|
|
}, |
|
|
|
newState: state, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
const failed = state.gamePhase === GamePhase.TURN_FAILED || !state.currentDiceEndTurn |
|
|
|
const failed = state.gamePhase === GamePhase.TURN_FAILED || !state.currentDiceEndTurn |
|
|
@ -262,6 +274,7 @@ function handleEndTurnEvent<P extends EndTurnPlayer, T extends EndTurnState<P>>( |
|
|
|
// The winner of this round has not yet been decided; this is better than the last turn this round.
|
|
|
|
// The winner of this round has not yet been decided; this is better than the last turn this round.
|
|
|
|
// Moving on to the next turn...
|
|
|
|
// Moving on to the next turn...
|
|
|
|
return { |
|
|
|
return { |
|
|
|
|
|
|
|
type: EventResultClass.SUCCESS, |
|
|
|
newState: { |
|
|
|
newState: { |
|
|
|
...state, |
|
|
|
...state, |
|
|
|
lastTurnTotal: state.currentTurnTotal, |
|
|
|
lastTurnTotal: state.currentTurnTotal, |
|
|
@ -324,7 +337,12 @@ function handleEndTurnEvent<P extends EndTurnPlayer, T extends EndTurnState<P>>( |
|
|
|
newPhase === GamePhase.VICTORY |
|
|
|
newPhase === GamePhase.VICTORY |
|
|
|
? oppositePlayer(state.phaseOwner) |
|
|
|
? oppositePlayer(state.phaseOwner) |
|
|
|
: determineNextRoundStarter(state, state.difficulty) |
|
|
|
: determineNextRoundStarter(state, state.difficulty) |
|
|
|
|
|
|
|
const events: OutgoingEvent[] = [] |
|
|
|
|
|
|
|
const endRoundEvent = |
|
|
|
|
|
|
|
state.gamePhase === GamePhase.TURN_FAILED ? OutgoingEventType.FailTurn : OutgoingEventType.AbandonTurn |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
return { |
|
|
|
|
|
|
|
type: EventResultClass.SUCCESS, |
|
|
|
newState: { |
|
|
|
newState: { |
|
|
|
...state, |
|
|
|
...state, |
|
|
|
players, |
|
|
|
players, |
|
|
@ -338,7 +356,24 @@ function handleEndTurnEvent<P extends EndTurnPlayer, T extends EndTurnState<P>>( |
|
|
|
countedFails: 0, |
|
|
|
countedFails: 0, |
|
|
|
selectedDiceValue: 0, |
|
|
|
selectedDiceValue: 0, |
|
|
|
}, |
|
|
|
}, |
|
|
|
events: [], |
|
|
|
events: [ |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
type: OutgoingEventType.PassTurn, |
|
|
|
|
|
|
|
target: state.phaseOwner, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
...(newPhase === GamePhase.VICTORY |
|
|
|
|
|
|
|
? [] |
|
|
|
|
|
|
|
: [ |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
type: endRoundEvent, |
|
|
|
|
|
|
|
target: state.phaseOwner, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
type: OutgoingEventType.StartRound, |
|
|
|
|
|
|
|
target: newRoundOwner, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
]), |
|
|
|
|
|
|
|
], |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|