|
|
@ -1,6 +1,7 @@
|
|
|
|
import {enableMapSet, createDraft, finishDraft, Draft} from 'immer'
|
|
|
|
import {enableMapSet, createDraft, finishDraft, Draft} from 'immer'
|
|
|
|
import GameState, { GameObject, Room, Door, Item, ObjectType } from './types/GameState'
|
|
|
|
import GameState, { GameObject, Room, Door, Item, ObjectType } from './types/GameState'
|
|
|
|
import ParsedCommand, { TokenType, ParsedTokenExpression, ValidCommandDetails, InvalidCommandDetails } from './types/ParsedCommand'
|
|
|
|
import ParsedCommand, { ValidCommandDetails, InvalidCommandDetails } from './types/ParsedCommand'
|
|
|
|
|
|
|
|
import { GameEventMessage, GameEventCommand } from './types/GameEvent'
|
|
|
|
|
|
|
|
|
|
|
|
enableMapSet()
|
|
|
|
enableMapSet()
|
|
|
|
|
|
|
|
|
|
|
@ -13,7 +14,15 @@ export type CommandValidateResult = {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default class Game {
|
|
|
|
export default class Game {
|
|
|
|
private gameState : GameState = {directions: new Map(), rooms: new Map(), doors: new Map(), items: new Map(), player: {location: ''}}
|
|
|
|
private gameState : GameState = {
|
|
|
|
|
|
|
|
directions: new Map(),
|
|
|
|
|
|
|
|
rooms: new Map(),
|
|
|
|
|
|
|
|
doors: new Map(),
|
|
|
|
|
|
|
|
items: new Map(),
|
|
|
|
|
|
|
|
player: {location: ''},
|
|
|
|
|
|
|
|
messages: []
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private draft : Draft<GameState> | null = null
|
|
|
|
private draft : Draft<GameState> | null = null
|
|
|
|
private onChangeListeners : ChangeListener [] = []
|
|
|
|
private onChangeListeners : ChangeListener [] = []
|
|
|
|
|
|
|
|
|
|
|
@ -29,6 +38,16 @@ export default class Game {
|
|
|
|
this.saveDraft()
|
|
|
|
this.saveDraft()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
outputCommand(commandString: string) {
|
|
|
|
|
|
|
|
const state = this.getState()
|
|
|
|
|
|
|
|
state.messages.push(new GameEventCommand(commandString))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
say(message: string) {
|
|
|
|
|
|
|
|
const state = this.getState()
|
|
|
|
|
|
|
|
state.messages.push(new GameEventMessage(message))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
filterValidCommands(commands: ParsedCommand[]) : CommandValidateResult {
|
|
|
|
filterValidCommands(commands: ParsedCommand[]) : CommandValidateResult {
|
|
|
|
let validCommands : ValidCommandDetails[] = []
|
|
|
|
let validCommands : ValidCommandDetails[] = []
|
|
|
|
let invalidCommands : InvalidCommandDetails[] = []
|
|
|
|
let invalidCommands : InvalidCommandDetails[] = []
|
|
|
@ -43,7 +62,12 @@ export default class Game {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
invalidCommands.sort((a,b) => a.severity - b.severity)
|
|
|
|
invalidCommands.sort((a,b) => {
|
|
|
|
|
|
|
|
if(a.severity !== b.severity)
|
|
|
|
|
|
|
|
return a.severity - b.severity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return b.command.getNumTokens() - a.command.getNumTokens()
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
return {validCommands, invalidCommands}
|
|
|
|
return {validCommands, invalidCommands}
|
|
|
|
}
|
|
|
|
}
|
|
|
|