Skip to content

Commit

Permalink
fix(world): match entities by ID segment when searching state (fixes #57
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ssube committed May 18, 2021
1 parent 531a3eb commit 50708bc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/service/render/InkRender.ts
Expand Up @@ -61,13 +61,16 @@ export class InkRender extends BaseRender implements Render {
// add the turn marker
result.output.unshift(`turn ${result.turn} > ${result.line}`);

this.logger.debug(result, 'showing step result');
await super.showStep(result);

this.logger.debug(result, 'firing step event');
this.emits.emit('step', result);
}

protected onLine(line: string): RemoveResult<StepResult> {
return onceWithRemove(this.emits, 'step', () => {
this.logger.debug({ line }, 'firing line event');
this.emits.emit('line', line);
});
}
Expand Down
18 changes: 10 additions & 8 deletions src/service/render/component/ink/Frame.tsx
Expand Up @@ -24,14 +24,17 @@ const DEFAULT_STATE: StepResult = {

export const Frame = (props: FrameProps) => {
const { exit } = useApp();
const [state, setState] = useState(DEFAULT_STATE);
const [line, setLine] = useState('');
const [state, setState] = useState(DEFAULT_STATE);

const pushError = (err?: Error) => {
if (doesExist(err)) {
setState({
...state,
output: [...state.output, err.message].slice(-HISTORY_SIZE),
output: [
...state.output,
err.message
].slice(-HISTORY_SIZE),
});
}
};
Expand All @@ -53,15 +56,14 @@ export const Frame = (props: FrameProps) => {
const { pending, remove } = props.onLine(line);

pending.then((stepState) => {
const merged = [
...state.output,
...stepState.output
];
setLine('');
setState({
...stepState,
output: merged.slice(-HISTORY_SIZE),
output: [
...state.output,
...stepState.output
].slice(-HISTORY_SIZE),
});
setLine('');
}).catch(pushError);

// TODO: when should onLine remove be called?
Expand Down
2 changes: 1 addition & 1 deletion src/service/script/common/ActorStep.ts
Expand Up @@ -114,7 +114,7 @@ export async function ActorStepLookTarget(this: Actor, scope: ScriptScope): Prom

export async function ActorStepLookRoom(this: Actor, scope: ScriptScope): Promise<void> {
if (doesExist(scope.room)) {
await scope.focus.show(`You are a ${this.meta.name}: ${this.meta.desc}`);
await scope.focus.show(`You are a ${this.meta.name}: ${this.meta.desc} (${this.meta.id})`);
await scope.focus.show(`You are in ${scope.room.meta.name} (${scope.room.meta.id}): ${scope.room.meta.desc}`);

for (const item of this.items) {
Expand Down
12 changes: 11 additions & 1 deletion src/service/state/LocalStateService.ts
Expand Up @@ -192,6 +192,12 @@ export class LocalStateService implements StateService {

const input = await this.getActorInput(player);
const cmd = await input.parse(params.line);
this.logger.debug({
cmd,
focus: state.focus,
params,
player,
}, 'parsed player input');

// handle meta commands
switch (cmd.verb) {
Expand Down Expand Up @@ -347,7 +353,11 @@ export class LocalStateService implements StateService {
const spent = Date.now() - start;
this.state.step.turn += 1;
this.state.step.time += spent;
this.logger.debug({ spent, step: this.state.step }, 'finished world state step');
this.logger.debug({
seen: seen.size,
spent,
step: this.state.step,
}, 'finished world state step');

return mustExist(this.focus).flush();
}
Expand Down
13 changes: 12 additions & 1 deletion src/util/entity.ts
Expand Up @@ -23,7 +23,7 @@ export function matchMetadata(entity: Immutable<Entity>, filter: Partial<Metadat
let matched = true;

if (doesExist(filter.id)) {
matched = matched && entity.meta.id.toLocaleLowerCase().startsWith(filter.id);
matched = matched && matchID(entity.meta.id.toLocaleLowerCase(), filter.id);
}

if (doesExist(filter.name)) {
Expand All @@ -32,3 +32,14 @@ export function matchMetadata(entity: Immutable<Entity>, filter: Partial<Metadat

return matched;
}

export function matchID(value: string, filter: string): boolean {
const valueParts = value.split('-');
const filterParts = filter.split('-');

if (filterParts.length < valueParts.length) {
return false;
}

return valueParts.every((it, idx) => it === filterParts[idx]);
}

0 comments on commit 50708bc

Please sign in to comment.