Files
dragonroll/backend/src/models/Actor.js
Gitea OpenCode Agent 299380d143
All checks were successful
Test / test (push) Successful in 2s
Fix issue #72: Link actor with a discord user
2026-10-15 17:06:25 +00:00

76 lines
3.1 KiB
JavaScript

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ActorSchema = new Schema({
name: { type: String, required: true, trim: true },
type: { type: String, enum: ['character', 'npc', 'creature'], default: 'character' },
species: { type: String, default: '' },
className: { type: String, default: '' },
subclass: { type: String, default: '' },
level: { type: Number, min: 1, max: 20, default: 1 },
background: { type: String, default: '' },
challengeRating: { type: String, default: '' },
description: { type: String, default: '' },
image: { type: String, default: '' },
abilityScores: {
str: { type: Number, min: 1, max: 30, default: 10 },
dex: { type: Number, min: 1, max: 30, default: 10 },
con: { type: Number, min: 1, max: 30, default: 10 },
int: { type: Number, min: 1, max: 30, default: 10 },
wis: { type: Number, min: 1, max: 30, default: 10 },
cha: { type: Number, min: 1, max: 30, default: 10 }
},
armorClass: { type: Number, min: 0, default: 10 },
speed: { type: Number, min: 0, default: 30 },
// The two pools the sheet draws as bars under the portrait (Syndria98/dragonroll#64): the bar
// fills `current` of `max`, so both travel together. `current` is kept below `max` by whoever
// writes them (routes/actor.js), the same way the sheet clamps the bar.
hitPoints: {
current: { type: Number, min: 0, default: 10 },
max: { type: Number, min: 0, default: 10 }
},
hitDice: {
current: { type: Number, min: 0, default: 1 },
max: { type: Number, min: 0, default: 1 }
},
proficiencies: { type: String, default: '' },
inventory: [{
name: { type: String, trim: true, default: '' },
quantity: { type: Number, min: 1, default: 1 },
weight: { type: Number, min: 0, default: 0 }
}],
traits: { type: String, default: '' },
spells: { type: String, default: '' },
campaign: {
type: Schema.Types.ObjectId,
ref: 'Campaign',
required: true
},
// The folder this actor stands in: a Folder of the same campaign whose target is
// 'actor' (null = the root of the actor list).
folder: {
type: Schema.Types.ObjectId,
ref: 'Folder',
default: null
},
foundryActorId: { type: String },
// The Discord player this character belongs to (Syndria98/dragonroll#72): each player
// of the server a campaign is linked to holds one character of it. Only
// routes/discord.js writes it, so the campaign keeps owning it whichever guild claimed it.
discordUserId: { type: String, default: null },
date: { type: Date, default: Date.now }
});
ActorSchema.index(
{ campaign: 1, foundryActorId: 1 },
{ unique: true, partialFilterExpression: { foundryActorId: { $type: 'string' } } }
);
// A player holds at most one character of a campaign: the id is unique within it.
ActorSchema.index(
{ campaign: 1, discordUserId: 1 },
{ unique: true, partialFilterExpression: { discordUserId: { $type: 'string' } } }
);
module.exports = mongoose.model('Actor', ActorSchema);