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.
93 lines • 3.12 kB
JavaScript
/**
* In-memory implementation of the TableRepository interface.
* Stores tables in memory, useful for testing and development.
*/
export class InMemoryTableRepository {
constructor() {
this.tables = new Map();
}
/**
* Saves a table to the repository.
* @param table The table to save.
* @returns The ID of the saved table.
*/
async save(table) {
this.tables.set(table.id, table);
return await Promise.resolve(table.id);
}
/**
* Gets a table by its ID.
* @param id The ID of the table to get.
* @returns The table, or null if not found.
*/
async getById(id) {
const table = this.tables.get(id);
return await Promise.resolve(table ?? null);
}
/**
* Updates an existing table.
* @param table The updated table.
* @throws Error if the table does not exist.
*/
async update(table) {
if (!this.tables.has(table.id)) {
throw new Error(`Table with ID ${table.id} does not exist`);
}
this.tables.set(table.id, table);
await Promise.resolve();
}
/**
* Lists tables based on optional filter criteria.
* @param filter Optional filter criteria.
* @returns An array of tables matching the filter.
*/
async list(filter) {
let tables = Array.from(this.tables.values());
await Promise.resolve();
if (filter) {
tables = tables.filter(table => {
// Check if all filter criteria match
return Object.entries(filter).every(([key, value]) => {
// Handle nested properties with dot notation (e.g., "entries.length")
const keys = key.split('.');
let obj = table;
// Navigate to the nested property
let currentObj = obj;
for (let i = 0; i < keys.length - 1; i++) {
currentObj = currentObj[keys[i]];
if (currentObj === undefined)
return false;
}
const prop = keys[keys.length - 1];
// Special case for array length
if (prop === 'length' && Array.isArray(currentObj)) {
return currentObj.length === value;
}
// Regular property comparison
return currentObj[prop] === value;
});
});
}
return tables;
}
/**
* Deletes a table by its ID.
* @param id The ID of the table to delete.
* @throws Error if the table does not exist.
*/
async delete(id) {
if (!this.tables.has(id)) {
throw new Error(`Table with ID ${id} does not exist`);
}
this.tables.delete(id);
await Promise.resolve();
}
/**
* Clears all tables from the repository.
* Useful for testing.
*/
clear() {
this.tables.clear();
}
}
//# sourceMappingURL=in-memory-table-repository.js.map