const maxRolls = 250
export const roll = mutation({
args: {
type: v.optional(v.string()),
label: v.optional(v.string()),
characterId: v.optional(v.id("characters")),
dice: v.array(v.object({ sides: v.number(), count: v.number() })),
},
handler: async (ctx, { dice, ...data }) => {
const user = await requirePlayerUser(ctx)
// make dice rolls
const rolls = []
for (const dieItem of dice) {
for (let i = 0; i < dieItem.count; i++) {
rolls.push({
sides: dieItem.sides,
result: Math.floor(Math.random() * dieItem.sides) + 1,
})
}
}
// add new dice roll
await ctx.db.insert("diceRolls", {
...data,
discordUserId: user.discordUserId,
dice: rolls,
})
// remove old dice rolls if there are too many
const allRolls = await ctx.db.query("diceRolls").collect()
const excessRolls = allRolls.slice(0, -maxRolls)
await Promise.all(excessRolls.map((r) => ctx.db.delete(r._id)))
},
})
const maxRolls = 250
export const roll = mutation({
args: {
type: v.optional(v.string()),
label: v.optional(v.string()),
characterId: v.optional(v.id("characters")),
dice: v.array(v.object({ sides: v.number(), count: v.number() })),
},
handler: async (ctx, { dice, ...data }) => {
const user = await requirePlayerUser(ctx)
// make dice rolls
const rolls = []
for (const dieItem of dice) {
for (let i = 0; i < dieItem.count; i++) {
rolls.push({
sides: dieItem.sides,
result: Math.floor(Math.random() * dieItem.sides) + 1,
})
}
}
// add new dice roll
await ctx.db.insert("diceRolls", {
...data,
discordUserId: user.discordUserId,
dice: rolls,
})
// remove old dice rolls if there are too many
const allRolls = await ctx.db.query("diceRolls").collect()
const excessRolls = allRolls.slice(0, -maxRolls)
await Promise.all(excessRolls.map((r) => ctx.db.delete(r._id)))
},
})