Skip to content

Commit 892439c

Browse files
committed
db: Refactor bulkAddPoints to use bulkWrite
1 parent cdc6a57 commit 892439c

3 files changed

Lines changed: 26 additions & 21 deletions

File tree

src/database/points.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const schema = new mongoose.Schema<MapModel>({
3232
},
3333
});
3434

35-
const model = mongoose.model<MapModel>('point', schema, 'points', {
35+
export const model = mongoose.model<MapModel>('point', schema, 'points', {
3636
overwriteModels: true,
3737
});
3838

@@ -53,26 +53,31 @@ export async function addPoints(user: string, points: Record<string, number>, ro
5353
return document.toJSON();
5454
}
5555

56-
export async function bulkAddPoints(bulkData: BulkPointsDataInput, roomId: string): Promise<Model[] | undefined> {
56+
export async function bulkAddPoints(bulkData: BulkPointsDataInput, roomId: string): Promise<boolean | undefined> {
5757
if (!IS_ENABLED.DB) return;
58-
const lookupIds = Object.keys(bulkData).map(userId => `${roomId}-${userId}`);
59-
const userPoints = await model.find({ id: { $in: lookupIds } });
6058
const roomConfig = PSRoomConfigs[roomId].points!;
6159

62-
const documentsToSave = await Promise.all(
63-
Object.values(bulkData).map(async ({ name, id, points }) => {
64-
const existing = userPoints.find(document => document.userId === id);
65-
const document =
66-
existing ?? (await model.create({ id: `${roomId}-${id}`, userId: id, name: name ?? id, roomId, points: new Map() }));
67-
Object.keys(roomConfig.types).forEach(type => document.points.set(type, document.points.get(type) ?? 0));
68-
document.name = name ?? id;
69-
Object.entries(points).forEach(([type, count]) => document.points.set(type, (document.points.get(type) ?? 0) + count));
70-
return document;
71-
})
72-
);
73-
74-
await model.bulkSave(documentsToSave);
75-
return documentsToSave.map(document => document.toJSON());
60+
const bulkQueries = Object.values(bulkData).map(({ id, name, points }) => ({
61+
updateOne: {
62+
filter: { id: `${roomId}-${id}` },
63+
update: {
64+
$setOnInsert: {
65+
id: `${roomId}-${id}`,
66+
roomId,
67+
userId: id,
68+
...Object.fromEntries(
69+
roomConfig.priority.filter(type => typeof points[type] !== 'number').map(type => [`points.${type}`, 0])
70+
),
71+
},
72+
$inc: Object.fromEntries(Object.entries(points).map(([type, amount]) => [`points.${type}`, amount])),
73+
$set: { name: name ?? id },
74+
},
75+
upsert: true,
76+
},
77+
}));
78+
79+
const res = await model.bulkWrite(bulkQueries);
80+
return res.isOk();
7681
}
7782

7883
export async function getPoints(user: string, roomId: string): Promise<Model | null | undefined> {

src/ps/commands/kunc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const command: PSCommand = {
2020
syntax: 'CMD [time?]',
2121
perms: (message, checkPermission) => (message.type === 'chat' ? checkPermission('driver') : true),
2222
categories: ['game'],
23+
// TODO: Add end
2324
async run({ message, arg, $T }): Promise<User[] | null> {
2425
const id = message.type === 'chat' ? message.target.id : `pm-${message.author.id}`;
2526
if (PSKuncInProgress[id]) {

src/ps/commands/points.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,12 @@ export const command: PSCommand[] = [
126126
);
127127
const res = await bulkAddPoints(pointsData, message.target.id);
128128
if (!res) throw new ChatError('Something went wrong...' as ToTranslate);
129+
129130
const pluralData = {
130131
singular: pointsTypes.map(pointsType => pointsType.singular).join('/'),
131132
plural: pointsTypes.map(pointsType => pointsType.plural).join('/'),
132133
};
133-
broadcast(
134-
`Added ${pluralize<TranslatedText>(pointsAmount, pluralData)} to ${res.map(entry => entry.name ?? entry.id).list($T)}.` as ToTranslate
135-
);
134+
broadcast(`Added ${pluralize<TranslatedText>(pointsAmount, pluralData)} to ${users.list($T)}.` as ToTranslate);
136135
},
137136
},
138137
{

0 commit comments

Comments
 (0)