1
1
import express from 'express' ;
2
2
import { Op } from 'sequelize' ;
3
3
import { like , searchLimit , searchOffset } from '#utils/database' ;
4
- import { authContext } from '#utils/auth' ;
4
+ import { authContext , authDiscord } from '#utils/auth' ;
5
5
import { pick , omit } from 'lodash-es' ;
6
6
import {
7
7
Team ,
@@ -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,92 @@ 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
+ const setting = await Setting . findOne ( {
134
+ where : { DiscordId : data . DiscordId } ,
135
+ attributes : [ 'KuskiIndex' ] ,
136
+ } ) ;
137
+ if ( setting ?. KuskiIndex ) {
138
+ await Setting . update (
139
+ { BnEnabled : data . BnEnabled } ,
140
+ { where : { KuskiIndex : setting . KuskiIndex } } ,
141
+ ) ;
142
+ }
143
+ return 1 ;
144
+ } ;
145
+
146
+ const ChangeBnSettings = async data => {
147
+ const setting = await Setting . findOne ( {
148
+ where : { DiscordId : data . DiscordId } ,
149
+ attributes : [ 'KuskiIndex' ] ,
150
+ } ) ;
151
+ if ( setting ?. KuskiIndex ) {
152
+ // get BnKuskiRules from user, if there are none and it is setting new rules, set BnEnabled to true
153
+ const rules = await BnKuskiRule . findAll ( {
154
+ where : { KuskiIndex : setting . KuskiIndex } ,
155
+ } ) ;
156
+ const isFirstTimeSetup = ! rules . length && data . BnKuskiRules . length > 0 ;
157
+ const BnEnabled = isFirstTimeSetup ? 1 : data . BnEnabled ;
158
+ await Setting . update (
159
+ { BnEnabled : BnEnabled ? 1 : 0 } ,
160
+ { where : { KuskiIndex : setting . KuskiIndex } } ,
161
+ ) ;
162
+
163
+ // delete all BnKuskiRules from this user
164
+ await BnKuskiRule . destroy ( { where : { KuskiIndex : setting . KuskiIndex } } ) ;
165
+ // for each rule, create a new BnKuskiRule
166
+ await Promise . all (
167
+ data . BnKuskiRules . map ( async rule => {
168
+ await BnKuskiRule . create ( {
169
+ KuskiIndex : setting . KuskiIndex ,
170
+ ...rule ,
171
+ } ) ;
172
+ } ) ,
173
+ ) ;
174
+ }
175
+ return 1 ;
176
+ } ;
177
+
91
178
const Player = async ( IdentifierType , KuskiIdentifier , currentUser ) => {
92
179
const query = {
93
180
where : { } ,
@@ -310,6 +397,34 @@ router
310
397
res . sendStatus ( 401 ) ;
311
398
}
312
399
} )
400
+ . get ( '/bn/:DiscordId/linked' , authDiscord , async ( req , res ) => {
401
+ const data = await Setting . findOne ( {
402
+ where : { DiscordId : req . params . DiscordId } ,
403
+ } ) ;
404
+ res . json ( Boolean ( data ) ) ;
405
+ } )
406
+ . get ( '/bn/:DiscordId' , authDiscord , async ( req , res ) => {
407
+ const data = await BnSettings ( req . params . DiscordId ) ;
408
+ res . json ( data ) ;
409
+ } )
410
+ . get ( '/bn' , authDiscord , async ( req , res ) => {
411
+ const data = await AllActiveBnSettings ( ) ;
412
+ res . json ( data ) ;
413
+ } )
414
+ . post ( '/bn/:DiscordId/toggle/:BnEnabled' , authDiscord , async ( req , res ) => {
415
+ const data = await ChangeBnEnabledSettings ( {
416
+ DiscordId : req . params . DiscordId ,
417
+ BnEnabled : Number ( req . params . BnEnabled ) ,
418
+ } ) ;
419
+ res . json ( data ) ;
420
+ } )
421
+ . post ( '/bn/:DiscordId' , authDiscord , async ( req , res ) => {
422
+ const data = await ChangeBnSettings ( {
423
+ ...req . body ,
424
+ DiscordId : req . params . DiscordId ,
425
+ } ) ;
426
+ res . json ( data ) ;
427
+ } )
313
428
. post ( '/ignore/:Kuski' , async ( req , res ) => {
314
429
const auth = authContext ( req ) ;
315
430
if ( auth . auth ) {
0 commit comments