The type system starts to infect the computation engine

This commit is contained in:
Thaum Rystra
2025-01-15 18:36:26 +02:00
parent 12789f1c5e
commit 15ecc05e21
10 changed files with 106 additions and 148 deletions

View File

@@ -8,8 +8,7 @@ import { storedIconsSchema } from '/imports/api/icons/Icons';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS';
import { ConvertToUnion, InferType, TypedSimpleSchema } from '/imports/api/utility/TypedSimpleSchema';
import { Simplify } from 'type-fest';
type PropertyType = Exclude<keyof typeof propertySchemasIndex, 'any'>;
import type { PropertyType } from '/imports/api/properties/PropertyType.type';
const PreComputeCreaturePropertySchema = TypedSimpleSchema.from({
_id: {

View File

@@ -1,59 +1,11 @@
import SimpleSchema from 'simpl-schema';
import ColorSchema, { Colored } from '/imports/api/properties/subSchemas/ColorSchema';
import SharingSchema, { Shared } from '/imports/api/sharing/SharingSchema';
import ColorSchema from '/imports/api/properties/subSchemas/ColorSchema';
import SharingSchema from '/imports/api/sharing/SharingSchema';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS';
import { InferType, TypedSimpleSchema } from '/imports/api/utility/TypedSimpleSchema';
import type { Simplify } from 'type-fest';
export type Creature = Colored & Shared & {
// Strings
_id: string,
name?: string,
alignment?: string,
gender?: string,
picture?: string,
avatarPicture?: string,
// Libraries
allowedLibraries?: string[],
allowedLibraryCollections?: string[],
// Stats that are computed and denormalized outside of recomputation
denormalizedStats?: {
xp: number,
milestoneLevels: number,
},
propCount?: number,
// Does the character need a recompute?
dirty?: boolean,
// Version of computation engine that was last used to compute this creature
computeVersion?: string,
type: 'pc' | 'npc' | 'monster',
computeErrors?: {
type: string,
details?: any,
}[],
// Tabletop
tabletopId?: string,
initiativeRoll?: number,
settings: {
useVariantEncumbrance?: true,
hideSpellcasting?: true,
hideRestButtons?: true,
swapStatAndModifier?: true,
hideUnusedStats?: true,
showTreeTab?: true,
hideSpellsTab?: true,
hideCalculationErrors?: true,
hitDiceResetMultiplier?: number,
discordWebhook?: string,
},
};
//set up the collection for creatures
const Creatures = new Mongo.Collection<Creature>('creatures');
const CreatureSettingsSchema = new SimpleSchema({
const CreatureSettingsSchema = TypedSimpleSchema.from({
//slowed down by carrying too much?
useVariantEncumbrance: {
type: Boolean,
@@ -108,24 +60,7 @@ const CreatureSettingsSchema = new SimpleSchema({
},
});
const IconGroupSchema = new SimpleSchema({
name: {
type: String,
max: STORAGE_LIMITS.name,
optional: true,
},
iconIds: {
type: Array,
max: 4,
defaultValue: [],
},
'iconIds.$': {
type: String,
max: STORAGE_LIMITS.variableName,
},
});
const CreatureSchema = new SimpleSchema({
const CreatureSchema = TypedSimpleSchema.from({
// Strings
name: {
type: String,
@@ -246,10 +181,13 @@ const CreatureSchema = new SimpleSchema({
CreatureSchema.extend(ColorSchema);
CreatureSchema.extend(SharingSchema);
export type Creature = Simplify<{ _id: string } & InferType<typeof CreatureSchema>>;
//set up the collection for creatures
const Creatures = new Mongo.Collection<Creature>('creatures');
//@ts-expect-error attachSchema not defined
Creatures.attachSchema(CreatureSchema);
export default Creatures;
export { CreatureSchema };