random-tables-mcp
Version:
An MCP (Model Context Protocol) server for managing and rolling on random-table assets used in tabletop RPGs. Create, update, and roll on random tables with support for nested tables, weighted entries, and range-based results.
70 lines • 2.49 kB
JavaScript
import { Range } from '../value-objects/roll-range.js';
import { RollTemplate } from '../value-objects/roll-template.js';
/**
* Represents an entry in a random table.
*/
export class TableEntry {
/**
* Creates a new TableEntry instance.
* @param id Unique identifier for the entry.
* @param content The content of this entry (text, reference to another table, etc.).
* @param weight Probability weight (default: 1).
* @param range Optional range of values this entry corresponds to (e.g., "1-5" on a d20).
*/
constructor(id, content, weight = 1, range) {
this.id = id;
this.content = content;
this.weight = weight;
this.range = range;
if (weight <= 0) {
throw new Error('Weight must be greater than zero');
}
}
/**
* Checks if a value is within this entry's range.
* @param value The value to check.
* @returns True if the entry has no range or if the value is within the range, false otherwise.
*/
isInRange(value) {
if (!this.range) {
return false;
}
return this.range.contains(value);
}
/**
* Checks if this entry's content is a template.
* @returns True if the content contains template references, false otherwise.
*/
isTemplate() {
return RollTemplate.isTemplate(this.content);
}
/**
* Creates a copy of this entry with updated properties.
* @param updates Object containing the properties to update.
* @returns A new TableEntry instance with updated properties.
*/
update(updates) {
return new TableEntry(this.id, updates.content ?? this.content, updates.weight ?? this.weight, updates.range ?? this.range);
}
/**
* Creates a TableEntry from a plain object.
* @param obj The object to create the entry from.
* @returns A new TableEntry instance.
*/
static fromObject(obj) {
return new TableEntry(obj.id, obj.content, obj.weight, obj.range ? new Range(obj.range.min, obj.range.max) : undefined);
}
/**
* Converts this entry to a plain object.
* @returns A plain object representation of this entry.
*/
toObject() {
return {
id: this.id,
content: this.content,
weight: this.weight,
range: this.range ? { min: this.range.min, max: this.range.max } : undefined,
};
}
}
//# sourceMappingURL=table-entry.js.map