Skip to content

Commit 731dafd

Browse files
committed
feat(settings): add battle notifier settings
1 parent a0a7dd4 commit 731dafd

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed

.prettierrc.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"printWidth": 80,
3+
"singleQuote": true,
4+
"trailingComma": "all",
5+
"arrowParens": "avoid"
6+
}

src/api/player.js

+109
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
LevelStats,
1212
Level,
1313
Setting,
14+
BnKuskiRule,
1415
} from '../data/models';
1516

1617
const router = express.Router();
@@ -88,6 +89,86 @@ const ChangeSettings = async data => {
8889
return 1;
8990
};
9091

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+
91172
const Player = async (IdentifierType, KuskiIdentifier, currentUser) => {
92173
const query = {
93174
where: {},
@@ -310,6 +391,34 @@ router
310391
res.sendStatus(401);
311392
}
312393
})
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+
})
313422
.post('/ignore/:Kuski', async (req, res) => {
314423
const auth = authContext(req);
315424
if (auth.auth) {

src/data/models/BnKuskiRule.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import DataType from 'sequelize';
2+
import Model from '../sequelize';
3+
4+
const Setting = Model.define('bn_kuski_rule', {
5+
BnKuskiRuleIndex: {
6+
type: DataType.INTEGER,
7+
autoIncrement: true,
8+
allowNull: false,
9+
primaryKey: true,
10+
},
11+
KuskiIndex: {
12+
type: DataType.INTEGER,
13+
allowNull: false,
14+
defaultValue: 0,
15+
},
16+
BattleTypes: {
17+
type: DataType.STRING(255),
18+
allowNull: true,
19+
defaultValue: null,
20+
},
21+
Designers: {
22+
type: DataType.STRING(255),
23+
allowNull: true,
24+
defaultValue: null,
25+
},
26+
LevelPatterns: {
27+
type: DataType.STRING(255),
28+
allowNull: true,
29+
defaultValue: null,
30+
},
31+
BattleAttributes: {
32+
type: DataType.STRING(255),
33+
allowNull: true,
34+
defaultValue: null,
35+
},
36+
MinDuration: {
37+
type: DataType.INTEGER,
38+
allowNull: true,
39+
defaultValue: null,
40+
},
41+
MaxDuration: {
42+
type: DataType.INTEGER,
43+
allowNull: true,
44+
defaultValue: null,
45+
},
46+
IgnoreList: {
47+
type: DataType.INTEGER,
48+
allowNull: false,
49+
defaultValue: 0,
50+
},
51+
CreatedAt: {
52+
type: DataType.INTEGER,
53+
allowNull: true,
54+
defaultValue: null,
55+
},
56+
UpdatedAt: {
57+
type: DataType.INTEGER,
58+
allowNull: true,
59+
defaultValue: null,
60+
},
61+
});
62+
63+
export default Setting;

src/data/models/Setting.js

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ const Setting = Model.define(
6060
allowNull: false,
6161
defaultValue: 1,
6262
},
63+
BnEnabled: {
64+
type: DataType.INTEGER,
65+
allowNull: false,
66+
defaultValue: 0,
67+
},
6368
},
6469
{
6570
indexes: [

src/data/models/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import MultiTimeFile from './MultiTimeFile';
5959
import Crippled from './Crippled';
6060
import Recap from './Recap';
6161
import ReplayLog from './ReplayLog';
62+
import BnKuskiRule from './BnKuskiRule';
6263

6364
Replay.belongsTo(Kuski, {
6465
foreignKey: 'DrivenBy',
@@ -511,6 +512,18 @@ Recap.belongsTo(Replay, {
511512
as: 'ReplayData',
512513
});
513514

515+
Setting.hasMany(BnKuskiRule, {
516+
foreignKey: 'KuskiIndex',
517+
sourceKey: 'KuskiIndex',
518+
as: 'BnKuskiRules',
519+
});
520+
521+
BnKuskiRule.belongsTo(Setting, {
522+
foreignKey: 'KuskiIndex',
523+
targetKey: 'KuskiIndex',
524+
as: 'SettingData',
525+
});
526+
514527
function sync(...args) {
515528
return sequelize.sync(...args);
516529
}
@@ -576,4 +589,6 @@ export {
576589
Crippled,
577590
Recap,
578591
ReplayLog,
592+
ReplayTags,
593+
BnKuskiRule,
579594
}; // add the data model here as well so it exports

0 commit comments

Comments
 (0)