11import mongoose from 'mongoose' ;
22
3+ import { PSRoomConfigs } from '@/cache' ;
34import { IS_ENABLED } from '@/enabled' ;
45import { toId } from '@/tools' ;
56
@@ -41,8 +42,10 @@ export async function addPoints(user: string, points: Record<string, number>, ro
4142 if ( ! IS_ENABLED . DB ) return ;
4243 const userId = toId ( user ) ;
4344 const id = `${ roomId } -${ userId } ` ;
45+ const roomConfig = PSRoomConfigs [ roomId ] . points ! ;
4446 const document = ( await model . findOne ( { id } ) ) ?? ( await model . create ( { id, userId, roomId, name : user , points : new Map ( ) } ) ) ;
4547 document . name = user ;
48+ Object . keys ( roomConfig . types ) . forEach ( type => document . points . set ( type , document . points . get ( type ) ?? 0 ) ) ;
4649 Object . entries ( points ) . forEach ( ( [ type , count ] ) => document . points . set ( type , ( document . points . get ( type ) ?? 0 ) + count ) ) ;
4750 await document . save ( ) ;
4851 return document . toJSON ( ) ;
@@ -55,20 +58,22 @@ export async function bulkAddPoints(
5558 if ( ! IS_ENABLED . DB ) return ;
5659 const lookupIds = Object . keys ( bulkData ) . map ( userId => `${ roomId } -${ userId } ` ) ;
5760 const userPoints = await model . find ( { id : { $in : lookupIds } } ) ;
61+ const roomConfig = PSRoomConfigs [ roomId ] . points ! ;
5862
5963 const documentsToSave = await Promise . all (
6064 Object . values ( bulkData ) . map ( async ( { name, id, points } ) => {
6165 const existing = userPoints . find ( document => document . userId === id ) ;
6266 const document =
6367 existing ?? ( await model . create ( { id : `${ roomId } -${ id } ` , userId : id , name : name ?? id , roomId, points : new Map ( ) } ) ) ;
68+ Object . keys ( roomConfig . types ) . forEach ( type => document . points . set ( type , document . points . get ( type ) ?? 0 ) ) ;
6469 document . name = name ?? id ;
6570 Object . entries ( points ) . forEach ( ( [ type , count ] ) => document . points . set ( type , ( document . points . get ( type ) ?? 0 ) + count ) ) ;
6671 return document ;
6772 } )
6873 ) ;
6974
7075 await model . bulkSave ( documentsToSave ) ;
71- return userPoints . map ( document => document . toJSON ( ) ) ;
76+ return documentsToSave . map ( document => document . toJSON ( ) ) ;
7277}
7378
7479export async function getPoints ( user : string , roomId : string ) : Promise < Model | null | undefined > {
0 commit comments