// Where characters are, and where they have been. A visit is recorded every // time a character is put somewhere and is never removed by moving on: the // set of visited locations is what a player is allowed to see. Every function // takes a Prisma client or transaction client as `db`. async function recordVisits(db, characterIds, locationId) { if (characterIds.length === 0) return; await db.characterLocationVisit.createMany({ data: characterIds.map((characterId) => ({ characterId, locationId })), skipDuplicates: true, }); } async function moveCharacters(db, characterIds, locationId) { if (characterIds.length === 0) return; await db.character.updateMany({ where: { id: { in: characterIds } }, data: { currentLocationId: locationId } }); await recordVisits(db, characterIds, locationId); } // Ids of the locations any of this user's characters in the campaign has been to. async function visitedLocationIds(db, userId, campaignId) { const visits = await db.characterLocationVisit.findMany({ where: { character: { userId, campaignId } }, select: { locationId: true }, distinct: ['locationId'], }); return new Set(visits.map((v) => v.locationId)); } module.exports = { recordVisits, moveCharacters, visitedLocationIds };