Skip to content

Commit

Permalink
fix(state): add error messages for pre-state verbs (fixes #99)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jun 4, 2021
1 parent 04a60e3 commit 0f703c6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
12 changes: 10 additions & 2 deletions data/config.yml
Expand Up @@ -14,12 +14,20 @@ locale:
summary: 'wrote {{size}} node graph to {{-path}}'
meta:
create: 'created new world {{name}} ({{id}}) from {{world}} with seed of {{seed}} and room depth of {{depth}}'
debug:
none: 'no world state to debug'
graph:
none: 'no world state to graph'
help: 'available verbs: {{verbs}}'
load:
none: 'no world states loaded from {{-path}}'
state: 'loaded world state {{meta.id}} from {{-path}}'
save: 'saved world state {{meta.id}} from {{-path}}'
quit: 'game over.'
quit: 'quitting'
save:
none: 'no world state to save'
state: 'saved world state {{meta.id}} from {{-path}}'
step:
none: 'please create a world before using any verbs'
world: '{{name}} ({{id}})'
verbs:
common:
Expand Down
59 changes: 50 additions & 9 deletions src/service/state/TurnState.ts
Expand Up @@ -285,16 +285,21 @@ export class LocalStateService implements StateService {
}

public async doStep(actor: Optional<Actor>): Promise<void> {
// TODO: proper wait, don't assume player goes last
if (isNil(actor)) {
return;
}

if (actor.actorType !== ActorType.PLAYER) {
// if there is no world state, there won't be an actor, but this error is more informative
if (isNil(this.state)) {
this.event.emit(EVENT_STATE_OUTPUT, {
line: 'meta.step.none',
step: {
time: 0,
turn: 0,
},
volume: ShowVolume.WORLD,
});
return;
}

if (isNil(this.state)) {
// TODO: proper wait, don't assume player goes last
if (isNil(actor) || actor.actorType !== ActorType.PLAYER) {
return;
}

Expand Down Expand Up @@ -333,6 +338,18 @@ export class LocalStateService implements StateService {
}

public async doDebug(): Promise<void> {
if (isNil(this.state)) {
this.event.emit(EVENT_STATE_OUTPUT, {
line: 'meta.debug.none',
step: {
time: 0,
turn: 0,
},
volume: ShowVolume.WORLD,
});
return;
}

const state = await this.save();
const lines = debugState(state);

Expand All @@ -346,6 +363,18 @@ export class LocalStateService implements StateService {
}

public async doGraph(path: string): Promise<void> {
if (isNil(this.state)) {
this.event.emit(EVENT_STATE_OUTPUT, {
line: 'meta.graph.none',
step: {
time: 0,
turn: 0,
},
volume: ShowVolume.WORLD,
});
return;
}

const state = await this.save();
const lines = graphState(state);
const data = lines.join('\n');
Expand Down Expand Up @@ -442,7 +471,19 @@ export class LocalStateService implements StateService {
}

public async doSave(path: string): Promise<void> {
const state = mustExist(this.state);
if (isNil(this.state)) {
this.event.emit(EVENT_STATE_OUTPUT, {
line: 'meta.save.none',
step: {
time: 0,
turn: 0,
},
volume: ShowVolume.WORLD,
});
return;
}

const state = this.state;
const world = mustFind(this.worlds, (it) => it.meta.id === state.meta.template);

const data: DataFile = {
Expand All @@ -460,7 +501,7 @@ export class LocalStateService implements StateService {
meta: state.meta,
path,
},
line: 'meta.save',
line: 'meta.save.state',
step: state.step,
volume: ShowVolume.WORLD,
});
Expand Down

0 comments on commit 0f703c6

Please sign in to comment.