Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(util): split up state and script utils
- Loading branch information
Showing
7 changed files
with
178 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { doesExist } from '@apextoaster/js-utils'; | ||
|
||
import { WorldEntity } from '../../model/entity'; | ||
import { Actor, isActor } from '../../model/entity/Actor'; | ||
import { isItem, Item } from '../../model/entity/Item'; | ||
import { isRoom, Room } from '../../model/entity/Room'; | ||
import { SIGNAL_PREFIX, VERB_PREFIX } from '../constants'; | ||
import { ScriptMap } from '../types'; | ||
|
||
interface VerbTarget { | ||
actor?: Actor; | ||
item?: Item; | ||
room?: Room; | ||
} | ||
|
||
export function getSignalScripts(target: WorldEntity): ScriptMap { | ||
const scripts: ScriptMap = new Map(); | ||
|
||
for (const [name, script] of target.scripts) { | ||
if (name.startsWith(SIGNAL_PREFIX)) { | ||
scripts.set(name, script); | ||
} | ||
} | ||
|
||
return scripts; | ||
} | ||
|
||
/** | ||
* @todo optimize, currently on a hot path | ||
*/ | ||
export function getVerbScripts(target: VerbTarget): ScriptMap { | ||
const scripts: ScriptMap = new Map(); | ||
|
||
if (isActor(target.actor)) { | ||
mergeVerbScripts(scripts, target.actor.scripts); | ||
|
||
for (const item of target.actor.items) { | ||
mergeVerbScripts(scripts, item.scripts); | ||
} | ||
} | ||
|
||
if (isItem(target.item)) { | ||
mergeVerbScripts(scripts, target.item.scripts); | ||
} | ||
|
||
if (isRoom(target.room)) { | ||
mergeVerbScripts(scripts, target.room.scripts); | ||
|
||
// TODO: add room items? | ||
} | ||
|
||
const scriptNames = Array.from(scripts.keys()); // needs to be pulled AOT since the Map will be mutated | ||
for (const name of scriptNames) { | ||
const script = scripts.get(name); | ||
if (doesExist(script) && script.name.length === 0) { | ||
scripts.delete(name); | ||
} | ||
} | ||
|
||
return scripts; | ||
} | ||
|
||
export function mergeVerbScripts(target: ScriptMap, source: ScriptMap): void { | ||
for (const [name, script] of source) { | ||
if (name.startsWith(VERB_PREFIX)) { | ||
target.set(name, script); | ||
} | ||
} | ||
} |
Oops, something went wrong.