From 3f37dd087dcc588279a2eba43fd3e1faf79fac1e Mon Sep 17 00:00:00 2001 From: Ashelyn Dawn Date: Fri, 24 Jul 2020 13:40:56 -0600 Subject: [PATCH] Beginnings of verb hooks and rules engine --- src/engine/Parser.ts | 16 ++-------------- src/engine/RulesEngine.ts | 12 ++++++++++-- src/engine/types/ParsedCommand.ts | 4 ++-- src/engine/types/Verb.ts | 17 +++++++++++++++-- src/engine/types/VerbBuilder.ts | 29 +++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 src/engine/types/VerbBuilder.ts diff --git a/src/engine/Parser.ts b/src/engine/Parser.ts index 57a1369..663c193 100644 --- a/src/engine/Parser.ts +++ b/src/engine/Parser.ts @@ -2,6 +2,7 @@ import Game from "./Game"; import RulesEngine from './RulesEngine' import ParsedCommand, { InvalidCommandDetails } from "./types/ParsedCommand"; import Verb from './types/Verb'; +import VerbBuilder from "./types/VerbBuilder"; export default class Parser { private game : Game @@ -27,7 +28,7 @@ export default class Parser { if(validationResult.validCommands.length < 1) { this.handleError(validationResult.invalidCommands) } else { - // TODO: Do the thing + this.engine.runCommand(validationResult.validCommands[0]) } this.game.saveDraft() @@ -71,16 +72,3 @@ export default class Parser { return new VerbBuilder(verb) } } - -class VerbBuilder { - private verb : Verb - - constructor(verb : Verb) { - this.verb = verb - } - - as(template : string) : VerbBuilder { - this.verb.understand(template) - return this - } -} diff --git a/src/engine/RulesEngine.ts b/src/engine/RulesEngine.ts index 2272c61..28635e3 100644 --- a/src/engine/RulesEngine.ts +++ b/src/engine/RulesEngine.ts @@ -1,4 +1,12 @@ +import { ValidCommandDetails } from "./types/ParsedCommand"; + +// This class allows for hooking up "global events" for things such as checking +// victory conditions, acting when play begins, or other such things. These +// event types are different from player actions, which are all considered +// "command" events export default class RulesEngine { - // constructor() { - // } + + runCommand(action: ValidCommandDetails) { + + } } diff --git a/src/engine/types/ParsedCommand.ts b/src/engine/types/ParsedCommand.ts index b462618..8c27f85 100644 --- a/src/engine/types/ParsedCommand.ts +++ b/src/engine/types/ParsedCommand.ts @@ -43,7 +43,7 @@ export enum ParsingErrorSeverity { NoSuchObject = 1 } -export type ValidCommandDetails = {isValid: true, command: ParsedCommand, subject: GameObject | null, object: GameObject | null} +export type ValidCommandDetails = {isValid: true, verb: Verb, subject: GameObject | null, object: GameObject | null} export type InvalidCommandDetails = {isValid: false, command: ParsedCommand, reason: string, severity: ParsingErrorSeverity} export default class ParsedCommand { @@ -99,7 +99,7 @@ export default class ParsedCommand { return { isValid: true, - command: this, + verb: this.verb, subject, object } diff --git a/src/engine/types/Verb.ts b/src/engine/types/Verb.ts index 096a7eb..5f0761d 100644 --- a/src/engine/types/Verb.ts +++ b/src/engine/types/Verb.ts @@ -1,17 +1,30 @@ -import ParsedCommand, {TokenType, ParsedTokenLiteral, ParsedTokenExpression} from './ParsedCommand' +import ParsedCommand, {TokenType, ParsedTokenLiteral, ParsedTokenExpression, ValidCommandDetails} from './ParsedCommand' import NounPosition from './NounPosition' import { ObjectType } from './GameState' +import Game from "../Game" + +export type VerbAction = (action : ValidCommandDetails, game : Game) => void export default class Verb { static expressionRegex = /^\[([a-z|]+)\]$/ + private templates : Template[] = [] readonly name : string - private templates : Template[] = [] + + private hooks : {before: VerbAction[], after : VerbAction[], carryOut: VerbAction[]} = { + before: [], + after: [], + carryOut: [] + } constructor(name : string) { this.name = name } + on(type : 'before' | 'after' | 'carryOut', action : VerbAction) { + this.hooks[type].push(action) + } + understand(templateString : string) { const words : string[] = templateString.split(' ') const tokens : TemplateToken[] = words.map((word : string) => { diff --git a/src/engine/types/VerbBuilder.ts b/src/engine/types/VerbBuilder.ts new file mode 100644 index 0000000..dc357a4 --- /dev/null +++ b/src/engine/types/VerbBuilder.ts @@ -0,0 +1,29 @@ +import Verb, { VerbAction } from "./Verb" + +export default class VerbBuilder { + private verb : Verb + + constructor(verb : Verb) { + this.verb = verb + } + + as(template : string) : VerbBuilder { + this.verb.understand(template) + return this + } + + before(callback : VerbAction) : VerbBuilder { + this.verb.on('before', callback) + return this + } + + carryOut(callback : VerbAction) : VerbBuilder { + this.verb.on('carryOut', callback) + return this + } + + after(callback : VerbAction) : VerbBuilder { + this.verb.on('after', callback) + return this + } +}