Started work on tabletop view

This commit is contained in:
Stefan Zermatten
2020-07-17 23:31:12 +02:00
parent 47345b3694
commit 95d8d2cb9a
24 changed files with 749 additions and 94 deletions

View File

@@ -1,11 +0,0 @@
import SimpleSchema from 'simpl-schema';
let Campaigns = new Mongo.Collection('campaigns');
let CampaignSchema = new SimpleSchema({
});
Campaigns.attachSchema(CampaignSchema);
export default Campaigns;

View File

@@ -1,53 +0,0 @@
import SimpleSchema from 'simpl-schema';
let Encounters = new Mongo.Collection('encounters');
const CreatureInitiativeSchema = new SimpleSchema({
name: {
type: String,
optional: true,
},
initiativeRoll: {
type: SimpleSchema.Integer,
},
});
const InitiativeSchema = new SimpleSchema({
// An ordered list of all creatures in the initiative order
creatures: {
type: Array,
defaultValue: [],
},
'creatures.$': {
type: CreatureInitiativeSchema,
},
active: {
type: Boolean,
defaultValue: false,
},
roundNumber: {
type: SimpleSchema.Integer,
defaultValue: 0,
},
initiativeNumber: {
type: SimpleSchema.Integer,
optional: true,
},
});
// A creature can be in one ecounter at a time.
// All creatures in an encounter have a shared time and space.
let EncounterSchema = new SimpleSchema({
name: {
type: String,
optional: true,
},
initiative: {
type: InitiativeSchema,
defaultValue: {},
},
});
Encounters.attachSchema(EncounterSchema);
export default Encounters;

View File

@@ -5,7 +5,7 @@ import deathSaveSchema from '/imports/api/properties/subSchemas/DeathSavesSchema
import ColorSchema from '/imports/api/properties/subSchemas/ColorSchema.js';
import SharingSchema from '/imports/api/sharing/SharingSchema.js';
import {assertEditPermission} from '/imports/api/sharing/sharingPermissions.js';
import { getUserTier } from '/imports/api/users/patreon/tiers.js';
import { assertUserHasPaidBenefits } from '/imports/api/users/patreon/tiers.js';
import '/imports/api/creature/removeCreature.js';
import '/imports/api/creature/restCreature.js';
@@ -107,6 +107,17 @@ let CreatureSchema = new SimpleSchema({
defaultValue: {}
},
// Tabletop
tabletop: {
type: String,
regEx: SimpleSchema.RegEx.id,
optional: true,
},
initiativeRoll: {
type: SimpleSchema.Integer,
optional: true,
},
// Settings
settings: {
type: CreatureSettingsSchema,
@@ -136,11 +147,7 @@ const insertCreature = new ValidatedMethod({
throw new Meteor.Error('Creatures.methods.insert.denied',
'You need to be logged in to insert a creature');
}
let tier = getUserTier(this.userId);
if (!tier.paidBenefits){
throw new Meteor.Error('Creatures.methods.insert.denied',
`The ${tier.name} tier does not allow you to insert a creature`);
}
assertUserHasPaidBenefits(this.userId);
// Create the creature document
let charId = Creatures.insert({

View File

@@ -0,0 +1,189 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import { assertUserHasPaidBenefits } from '/imports/api/users/patreon/tiers.js';
import Creatures from '/imports/api/creature/Creatures.js';
let Tabletops = new Mongo.Collection('tabletops');
const InitiativeSchema = new SimpleSchema({
active: {
type: Boolean,
defaultValue: false,
},
roundNumber: {
type: SimpleSchema.Integer,
defaultValue: 0,
},
initiativeNumber: {
type: SimpleSchema.Integer,
optional: true,
},
activeCreature: {
type: String,
regEx: SimpleSchema.RegEx.id,
optional: true,
},
});
// All creatures in a tabletop have a shared time and space.
let TabletopSchema = new SimpleSchema({
name: {
type: String,
optional: true,
},
initiative: {
type: InitiativeSchema,
defaultValue: {},
},
gameMaster: {
type: String,
regEx: SimpleSchema.RegEx.id,
},
players: {
type: Array,
defaultValue: [],
},
'players.$': {
type: String,
regEx: SimpleSchema.RegEx.id,
},
});
Tabletops.attachSchema(TabletopSchema);
function assertUserIsTabletopOwner(tabletopId, userId){
let tabletop = Tabletops.findOne(tabletopId);
if (!tabletop){
throw new Meteor.Error('Tabletop does not exist',
'No tabletop could be found for the given tabletop id');
}
if (tabletop.gameMaster !== userId){
throw new Meteor.Error('Not the owner',
'The user is not the owner of the given tabletop');
}
}
function assertUserInTabletop(tabletopId, userId){
let tabletop = Tabletops.findOne(tabletopId);
if (!tabletop){
throw new Meteor.Error('Tabletop does not exist',
'No tabletop could be found for the given tabletop id');
}
if (tabletop.gameMaster !== userId && !tabletop.players.includes(userId)){
throw new Meteor.Error('Not in tabletop',
'The user is not the gamemaster or a player in the given tabletop');
}
}
const insertTabletop = new ValidatedMethod({
name: 'tabletops.insert',
validate: null,
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run() {
if (!this.userId) {
throw new Meteor.Error('tabletops.insert.denied',
'You need to be logged in to insert a tabletop');
}
assertUserHasPaidBenefits(this.userId);
return Tabletops.insert({
gameMaster: this.userId,
});
},
});
const removeTabletop = new ValidatedMethod({
name: 'tabletops.remove',
validate: new SimpleSchema({
tabletopId: {
type: String,
regEx: SimpleSchema.RegEx.id,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({tabletopId}) {
if (!this.userId) {
throw new Meteor.Error('tabletops.remove.denied',
'You need to be logged in to remove a tabletop');
}
assertUserHasPaidBenefits(this.userId);
assertUserIsTabletopOwner(tabletopId, this.userId);
let removed = Tabletops.remove({
_id: tabletopId,
});
Creatures.update({
tabletop: tabletopId,
}, {
$unset: {tabletop: 1},
});
return removed;
},
});
const addCreaturesToTabletop = new ValidatedMethod({
name: 'tabletops.addCreatures',
validate: new SimpleSchema({
'creatureIds': {
type: Array,
},
'creatureIds.$': {
type: String,
regEx: SimpleSchema.RegEx.id,
},
tabletopId: {
type: String,
regEx: SimpleSchema.RegEx.id,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 10,
timeInterval: 5000,
},
run({tabletopId, creatureIds}) {
if (!this.userId) {
throw new Meteor.Error('tabletops.addCreatures.denied',
'You need to be logged in to remove a tabletop');
}
assertUserHasPaidBenefits(this.userId);
assertUserInTabletop(tabletopId, this.userId);
Creatures.update({
_id: {$in: creatureIds},
$or: [
{writers: this.userId},
{owner: this.userId},
],
}, {
$set: {tabletop: tabletopId},
}, {
multi: true,
});
},
});
export default Tabletops;
export { insertTabletop, removeTabletop, addCreaturesToTabletop };

View File

@@ -77,5 +77,13 @@ export function getUserTier(user){
}
}
export function assertUserHasPaidBenefits(user){
let tier = getUserTier(user);
if (!tier.paidBenefits){
throw new Meteor.Error('Creatures.methods.insert.denied',
`The ${tier.name} tier does not allow you to insert a creature`);
}
}
export default TIERS;
export { GUEST_TIER };