/* eslint-disable no-unused-vars */ import { BucketTypes, validateBucketConfig } from "../buckets.mjs"; import { Logger } from "../Logger.mjs"; import { PrivacyMode } from "../privacy.mjs"; import { tableSchema } from "./model.mjs"; /* NOTE: This database design currently does not support anything like a default subtable or nested tables more than 1-layer deep. These limitations are currently intentional and if the desire for those functionalities is requested, this is some information on how they can be implemented. Tables >= 1 layer deep: Adding a "parent" property to each table that accepts a table's ID, there will need to be a defined table at the specified ID, and each table's ID should no longer contain the parent table(s) within it (e.g. "Dice/d4" -> "d4"). This could also be made in such a way where any buckets/graph settings on the parent are applied to the subtable when it is created. The primary unknown with this idea is what do we do when someone has a table like "Dice/d4" and then attempts to make a subtable "Dice/d4/crits" or something like that. Default Subtables: This would require a partial implementation similar to the Tables >= 1 layer deep, however each table would accept an "options" top level property that accepts a "defaultSubtable" property specifying the ID of the subtable that should be selected by default, this defaultSubtable property would *only* be valid on tables that are parents to other tables. */ const { deleteProperty, diffObject, expandObject, mergeObject } = foundry.utils; /** * The generic Database implementation, any subclasses should implement all of * the required methods, optionally overriding the methods provided by this class, * data validation should be used on any and all of the create* methods to ensure * consistency across databases. */ export class Database { // MARK: Table Ops static async createTable(tableConfig) { if (!game.user.isGM) { ui.notifications.error(`You do not have the required permission to create a new table`); return false; }; const { error, value: corrected } = tableSchema.validate( tableConfig, { abortEarly: false, convert: true, dateFormat: `iso`, render: false }, ); if (error) { ui.notifications.error(`Table being created did not conform to required schema, see console for more information.`, { console: false }); Logger.error(error); return false; }; const name = tableConfig.name; const [ table, subtable ] = name.split(`/`); const tables = game.settings.get(__ID__, `tables`); if (subtable && tables[table]) { ui.notifications.error(`Cannot add subtable for a table that already exists`); return false; }; if (table === `Dice`) { if (!subtable.match(/^d[0-9]+$/)) { ui.notifications.error(`Cannot create a Dice subtable that doesn't use "dX" as it's subtable name.`); return false; }; if (tableConfig.buckets.type === BucketTypes.RANGE) { ui.notifications.error(`Cannot create a Dice subtable with a non-range bucket type`); return false; }; }; if (tables[name]) { ui.notifications.error(`Cannot create table that already exists`); return false; }; tables[name] = corrected; game.settings.set(__ID__, `tables`, tables); this.render({ tags: [`table`] }); return true; }; /** @returns {Array