@@ -11,6 +11,7 @@ import {
11
11
LevelStats ,
12
12
Level ,
13
13
Setting ,
14
+ BnKuskiRule ,
14
15
} from '../data/models' ;
15
16
16
17
const router = express . Router ( ) ;
@@ -88,6 +89,86 @@ const ChangeSettings = async data => {
88
89
return 1 ;
89
90
} ;
90
91
92
+ const BnKuskiRuleAttributes = [
93
+ 'BattleTypes' ,
94
+ 'Designers' ,
95
+ 'LevelPatterns' ,
96
+ 'BattleAttributes' ,
97
+ 'MinDuration' ,
98
+ 'MaxDuration' ,
99
+ 'IgnoreList' ,
100
+ ] ;
101
+
102
+ const BnSettings = async DiscordId => {
103
+ const get = await Setting . findOne ( {
104
+ where : { DiscordId } ,
105
+ attributes : [ 'KuskiIndex' , 'DiscordId' , 'BnEnabled' ] ,
106
+ include : [
107
+ {
108
+ model : BnKuskiRule ,
109
+ as : 'BnKuskiRules' ,
110
+ attributes : BnKuskiRuleAttributes ,
111
+ } ,
112
+ ] ,
113
+ } ) ;
114
+ return get ;
115
+ } ;
116
+
117
+ const AllActiveBnSettings = async ( ) => {
118
+ const get = await Setting . findAll ( {
119
+ attributes : [ 'KuskiIndex' , 'DiscordId' , 'BnEnabled' ] ,
120
+ include : [
121
+ {
122
+ model : BnKuskiRule ,
123
+ as : 'BnKuskiRules' ,
124
+ attributes : BnKuskiRuleAttributes ,
125
+ required : true ,
126
+ } ,
127
+ ] ,
128
+ } ) ;
129
+ return get ;
130
+ } ;
131
+
132
+ const ChangeBnEnabledSettings = async data => {
133
+ await Setting . update (
134
+ { BnEnabled : data . BnEnabled ? 1 : 0 } ,
135
+ { where : { DiscordId : data . DiscordId } } ,
136
+ ) ;
137
+ return 1 ;
138
+ } ;
139
+
140
+ const ChangeBnSettings = async data => {
141
+ const setting = await Setting . findOne ( {
142
+ where : { DiscordId : data . DiscordId } ,
143
+ attributes : [ 'KuskiIndex' ] ,
144
+ } ) ;
145
+ if ( setting ?. KuskiIndex ) {
146
+ // get BnKuskiRules from user, if there are none and it is setting new rules, set BnEnabled to true
147
+ const rules = await BnKuskiRule . findAll ( {
148
+ where : { KuskiIndex : setting . KuskiIndex } ,
149
+ } ) ;
150
+ const isFirstTimeSetup = ! rules . length && data . BnKuskiRules . length > 0 ;
151
+ const BnEnabled = isFirstTimeSetup ? 1 : data . BnEnabled ;
152
+ await Setting . update (
153
+ { BnEnabled : BnEnabled ? 1 : 0 } ,
154
+ { where : { KuskiIndex : setting . KuskiIndex } } ,
155
+ ) ;
156
+
157
+ // delete all BnKuskiRules from this user
158
+ await BnKuskiRule . destroy ( { where : { KuskiIndex : setting . KuskiIndex } } ) ;
159
+ // for each rule, create a new BnKuskiRule
160
+ await Promise . all (
161
+ data . BnKuskiRules . map ( async rule => {
162
+ await BnKuskiRule . create ( {
163
+ KuskiIndex : setting . KuskiIndex ,
164
+ ...rule ,
165
+ } ) ;
166
+ } ) ,
167
+ ) ;
168
+ }
169
+ return 1 ;
170
+ } ;
171
+
91
172
const Player = async ( IdentifierType , KuskiIdentifier , currentUser ) => {
92
173
const query = {
93
174
where : { } ,
@@ -310,6 +391,34 @@ router
310
391
res . sendStatus ( 401 ) ;
311
392
}
312
393
} )
394
+ . get ( '/bn/:DiscordId/linked' , async ( req , res ) => {
395
+ const data = await Setting . findOne ( {
396
+ where : { DiscordId : req . params . DiscordId } ,
397
+ } ) ;
398
+ res . json ( Boolean ( data ) ) ;
399
+ } )
400
+ . get ( '/bn/:DiscordId' , async ( req , res ) => {
401
+ const data = await BnSettings ( req . params . DiscordId ) ;
402
+ res . json ( data ) ;
403
+ } )
404
+ . get ( '/bn' , async ( req , res ) => {
405
+ const data = await AllActiveBnSettings ( ) ;
406
+ res . json ( data ) ;
407
+ } )
408
+ . post ( '/bn/:DiscordId/toggle/:BnEnabled' , async ( req , res ) => {
409
+ const data = await ChangeBnEnabledSettings ( {
410
+ DiscordId : req . params . DiscordId ,
411
+ BnEnabled : req . params . BnEnabled ,
412
+ } ) ;
413
+ res . json ( data ) ;
414
+ } )
415
+ . post ( '/bn/:DiscordId' , async ( req , res ) => {
416
+ const data = await ChangeBnSettings ( {
417
+ ...req . body ,
418
+ DiscordId : req . params . DiscordId ,
419
+ } ) ;
420
+ res . json ( data ) ;
421
+ } )
313
422
. post ( '/ignore/:Kuski' , async ( req , res ) => {
314
423
const auth = authContext ( req ) ;
315
424
if ( auth . auth ) {
0 commit comments