@casual-simulation/aux-common
Version:
Common library for AUX projects
32 lines • 1.25 kB
JavaScript
import { valuesToKey, buildLookupTable } from './BotLookupTable';
import { zip, sortBy } from 'es-toolkit/compat';
/**
* Defines a helper class for BotLookupTable which is able to dynamically create and reuse
* lookup tables for a calculation context.
*/
export class BotLookupTableHelper {
constructor() {
this._tables = new Map();
}
/**
* Queries the given bot calculation context for bots that match a set of tags with the given values.
* @param calc The bot calculation context.
* @param tags The tags to query against.
* @param values The values to query for.
* @param defaulst The default values to use for missing tags.
*/
query(calc, tags, values, defaults) {
const zipped = zip(tags, values);
const sorted = sortBy(zipped, (z) => z[0]);
const sortedTags = sorted.map((s) => s[0]);
const sortedValues = sorted.map((s) => s[1]);
let key = valuesToKey(sortedTags);
let table = this._tables.get(key);
if (!table) {
table = buildLookupTable(calc, sortedTags, defaults);
this._tables.set(key, table);
}
return table.query(sortedValues);
}
}
//# sourceMappingURL=BotLookupTableHelper.js.map