Skip to content

Commit 8741085

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

File tree

7 files changed

+206
-7
lines changed

7 files changed

+206
-7
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

+115-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import express from 'express';
22
import { Op } from 'sequelize';
33
import { like, searchLimit, searchOffset } from '#utils/database';
4-
import { authContext } from '#utils/auth';
4+
import { authContext, authDiscord } from '#utils/auth';
55
import { pick, omit } from 'lodash-es';
66
import {
77
Team,
@@ -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,91 @@ 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 ChangeBnEnabledSetting = 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+
await Setting.update(
158+
{ BnEnabled: isFirstTimeSetup ? 1 : undefined },
159+
{ where: { KuskiIndex: setting.KuskiIndex } },
160+
);
161+
162+
// delete all BnKuskiRules from this user
163+
await BnKuskiRule.destroy({ where: { KuskiIndex: setting.KuskiIndex } });
164+
// for each rule, create a new BnKuskiRule
165+
await Promise.all(
166+
data.BnKuskiRules.map(async rule => {
167+
await BnKuskiRule.create({
168+
KuskiIndex: setting.KuskiIndex,
169+
...rule,
170+
});
171+
}),
172+
);
173+
}
174+
return 1;
175+
};
176+
91177
const Player = async (IdentifierType, KuskiIdentifier, currentUser) => {
92178
const query = {
93179
where: {},
@@ -310,6 +396,34 @@ router
310396
res.sendStatus(401);
311397
}
312398
})
399+
.get('/bn/:DiscordId/linked', authDiscord, async (req, res) => {
400+
const data = await Setting.findOne({
401+
where: { DiscordId: req.params.DiscordId },
402+
});
403+
res.json(Boolean(data));
404+
})
405+
.get('/bn/:DiscordId', authDiscord, async (req, res) => {
406+
const data = await BnSettings(req.params.DiscordId);
407+
res.json(data);
408+
})
409+
.get('/bn', authDiscord, async (req, res) => {
410+
const data = await AllActiveBnSettings();
411+
res.json(data);
412+
})
413+
.post('/bn/:DiscordId/toggle/:BnEnabled', authDiscord, async (req, res) => {
414+
const data = await ChangeBnEnabledSetting({
415+
DiscordId: req.params.DiscordId,
416+
BnEnabled: Number(req.params.BnEnabled),
417+
});
418+
res.json(data);
419+
})
420+
.post('/bn/:DiscordId', authDiscord, async (req, res) => {
421+
const data = await ChangeBnSettings({
422+
...req.body,
423+
DiscordId: req.params.DiscordId,
424+
});
425+
res.json(data);
426+
})
313427
.post('/ignore/:Kuski', async (req, res) => {
314428
const auth = authContext(req);
315429
if (auth.auth) {

src/config.defaults.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ export default {
4949

5050
// auth
5151
// eslint-disable-next-line prettier/prettier
52-
jwtSecret: 'eAwI4zcTDd4Pvc8QtN9z57Fqsr4ENNcTpK1x4A1dCLj0Y44OravXZDzNbA-4VEwAIh1Hw3vn1nhB9ygWLqAGE4GiX6hjjLsJi8IJ',
52+
jwtSecret:
53+
'eAwI4zcTDd4Pvc8QtN9z57Fqsr4ENNcTpK1x4A1dCLj0Y44OravXZDzNbA-4VEwAIh1Hw3vn1nhB9ygWLqAGE4GiX6hjjLsJi8IJ',
5354
jwtAlgo: 'HS256',
5455
recaptcha: {
5556
client: '6Le-n9QUAAAAAG-3bYyysXddxwD6I6iJeDBTHf2r',
@@ -76,6 +77,7 @@ export default {
7677
admin: 'admin',
7778
},
7879
apiAuth: '', // Authorization header sent by game events
79-
url: 'https://test.elma.online/', // url used in discord messages
80+
url: 'https://test.elma.online/', // url used in discord messages,
81+
bnAuth: 'e5f13420-cf17-4fd5-8cc9-c96959d1048f', // Authorization header sent from BN
8082
},
8183
};

src/data/models/BnKuskiRule.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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: 0,
40+
},
41+
MaxDuration: {
42+
type: DataType.INTEGER,
43+
allowNull: true,
44+
defaultValue: 0,
45+
},
46+
IgnoreList: {
47+
type: DataType.INTEGER,
48+
allowNull: false,
49+
defaultValue: 0,
50+
},
51+
});
52+
53+
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

src/utils/auth.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ export const auth = async body => {
8080
};
8181
}
8282
if (kuskiData.dataValues.Password) {
83-
const md5 = crypto
84-
.createHash('md5')
85-
.update(password)
86-
.digest('hex');
83+
const md5 = crypto.createHash('md5').update(password).digest('hex');
8784
if (md5 === kuskiData.dataValues.Password) {
8885
if (kuskiData.dataValues.Salt) {
8986
await addSha3(
@@ -124,3 +121,10 @@ export function authContext(req) {
124121
}
125122
return { auth: false, userid: 0 };
126123
}
124+
125+
export function authDiscord(req, res, next) {
126+
if (req.header('Authorization') === config.discord.bnAuth) {
127+
return next();
128+
}
129+
return res.sendStatus(401);
130+
}

0 commit comments

Comments
 (0)