@@ -2,8 +2,7 @@ import { readdir, readFile, writeFile as _writeFile } from "fs/promises";
22
33import { client } from "./client.js" ;
44import { defaultSettings } from "./constants.js" ;
5- import { GuildStorage } from "./types" ;
6- import { PlayerStatsDatabaseInfo , PlayerStorage } from "./types.js" ;
5+ import { GuildStorage , PlayerStatsDatabaseInfo , PlayerStorage } from "./types.js" ;
76
87const writeFile = new Proxy ( _writeFile , {
98 apply ( target , thisArg , argArray ) {
@@ -18,11 +17,13 @@ class PlayerStatsDatabase {
1817 private waitingForInit = false ;
1918 private writeOps : { [ guildID : string ] : Promise < void > } = { } ;
2019
21- defaultValue : PlayerStorage = {
22- wins : 0 ,
23- losses : 0 ,
24- preferredSettings : defaultSettings
25- } as const ;
20+ get defaultValue ( ) {
21+ return {
22+ wins : 0 ,
23+ losses : 0 ,
24+ preferredSettings : defaultSettings
25+ } as Readonly < PlayerStorage > ;
26+ }
2627
2728 private init = async ( ) => {
2829 if ( this . hasInitalized ) return ;
@@ -84,20 +85,20 @@ class PlayerStatsDatabase {
8485 if ( ! this . hasInitalized ) return ;
8586
8687 const guild = this . cache [ guildId ] ;
88+ if ( ! guild ) return ;
8789 if ( guild . settingsVersion !== this . latestSettingsVersion ) {
8890 this . cache [ guildId ] = this . migrateSettings ( guild ) ;
8991 this . write ( guildId ) ;
9092 }
91- return guild [ playerId ] ;
93+ if ( guild [ playerId ] ) return JSON . parse ( JSON . stringify ( guild [ playerId ] ) ) ;
9294 }
9395
9496 getOrCreate ( guildId : string , playerId : string ) : PlayerStorage {
9597 const value = this . get ( guildId , playerId ) ;
9698 if ( value ) return value ;
9799
98- const defaultValue = JSON . parse ( JSON . stringify ( this . defaultValue ) ) ;
99- this . set ( guildId , playerId , defaultValue ) ;
100- return defaultValue ;
100+ this . set ( guildId , playerId , this . defaultValue ) ;
101+ return this . defaultValue ;
101102 }
102103
103104 getAllForGuild ( guildId : string ) : GuildStorage | undefined {
@@ -108,35 +109,45 @@ class PlayerStatsDatabase {
108109 this . cache [ guildId ] = this . migrateSettings ( guild ) ;
109110 this . write ( guildId ) ;
110111 }
111- return guild ;
112+ return JSON . parse ( JSON . stringify ( guild ) ) ;
112113 }
113114
114- set ( guildId : string , playerId : string , newValue : Partial < PlayerStorage > ) : void {
115- if ( ! this . hasInitalized ) return ;
115+ set ( guildId : string , playerId : string , newValue : Partial < PlayerStorage > ) : boolean {
116+ if ( ! this . hasInitalized ) return false ;
117+ if ( ! this . cache [ guildId ] ) return false ;
116118
117119 let current = this . cache [ guildId ] [ playerId ] ;
118- if ( ! current ) current = JSON . parse ( JSON . stringify ( this . defaultValue ) ) ;
119- this . cache [ guildId ] [ playerId ] = { ...newValue , ...current } ;
120+ if ( ! current ) current = this . defaultValue ;
121+ for ( const [ k , v ] of Object . entries ( newValue ) ) {
122+ current [ k ] = v ;
123+ }
124+ this . cache [ guildId ] [ playerId ] = current ;
120125
121126 if ( this . cache [ guildId ] . settingsVersion !== this . latestSettingsVersion )
122127 this . cache [ guildId ] = this . migrateSettings ( this . cache [ guildId ] ) ;
123128
124129 this . write ( guildId ) ;
130+ return true ;
125131 }
126132
127- setBulk ( guildId : string , values : { [ id : string ] : Partial < PlayerStorage > } ) : void {
128- if ( ! this . hasInitalized ) return ;
133+ setBulk ( guildId : string , values : { [ id : string ] : Partial < PlayerStorage > } ) : boolean {
134+ if ( ! this . hasInitalized ) return false ;
135+ if ( ! this . cache [ guildId ] ) return false ;
129136
130- Object . entries ( values ) . forEach ( ( [ id , value ] ) => {
131- let current = this . cache [ guildId ] [ id ] ;
132- if ( ! current ) current = JSON . parse ( JSON . stringify ( this . defaultValue ) ) ;
133- this . cache [ guildId ] [ id ] = { ...value , ...current } ;
137+ Object . entries ( values ) . forEach ( ( [ playerId , newValue ] ) => {
138+ let current = this . cache [ guildId ] [ playerId ] ;
139+ if ( ! current ) current = this . defaultValue ;
140+ for ( const [ k , v ] of Object . entries ( newValue ) ) {
141+ current [ k ] = v ;
142+ }
143+ this . cache [ guildId ] [ playerId ] = current ;
134144 } ) ;
135145
136146 if ( this . cache [ guildId ] . settingsVersion !== this . latestSettingsVersion )
137147 this . cache [ guildId ] = this . migrateSettings ( this . cache [ guildId ] ) ;
138148
139149 this . write ( guildId ) ;
150+ return true ;
140151 }
141152
142153 constructor ( ) {
0 commit comments