Skip to content

Commit

Permalink
feat(settings): add battle notifier settings
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloCorso committed Jun 29, 2023
1 parent a0a7dd4 commit 8741085
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"printWidth": 80,
"singleQuote": true,
"trailingComma": "all",
"arrowParens": "avoid"
}
116 changes: 115 additions & 1 deletion src/api/player.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from 'express';
import { Op } from 'sequelize';
import { like, searchLimit, searchOffset } from '#utils/database';
import { authContext } from '#utils/auth';
import { authContext, authDiscord } from '#utils/auth';
import { pick, omit } from 'lodash-es';
import {
Team,
Expand All @@ -11,6 +11,7 @@ import {
LevelStats,
Level,
Setting,
BnKuskiRule,
} from '../data/models';

const router = express.Router();
Expand Down Expand Up @@ -88,6 +89,91 @@ const ChangeSettings = async data => {
return 1;
};

const BnKuskiRuleAttributes = [
'BattleTypes',
'Designers',
'LevelPatterns',
'BattleAttributes',
'MinDuration',
'MaxDuration',
'IgnoreList',
];

const BnSettings = async DiscordId => {
const get = await Setting.findOne({
where: { DiscordId },
attributes: ['KuskiIndex', 'DiscordId', 'BnEnabled'],
include: [
{
model: BnKuskiRule,
as: 'BnKuskiRules',
attributes: BnKuskiRuleAttributes,
},
],
});
return get;
};

const AllActiveBnSettings = async () => {
const get = await Setting.findAll({
attributes: ['KuskiIndex', 'DiscordId', 'BnEnabled'],
include: [
{
model: BnKuskiRule,
as: 'BnKuskiRules',
attributes: BnKuskiRuleAttributes,
required: true,
},
],
});
return get;
};

const ChangeBnEnabledSetting = async data => {
const setting = await Setting.findOne({
where: { DiscordId: data.DiscordId },
attributes: ['KuskiIndex'],
});
if (setting?.KuskiIndex) {
await Setting.update(
{ BnEnabled: data.BnEnabled },
{ where: { KuskiIndex: setting.KuskiIndex } },
);
}
return 1;
};

const ChangeBnSettings = async data => {
const setting = await Setting.findOne({
where: { DiscordId: data.DiscordId },
attributes: ['KuskiIndex'],
});
if (setting?.KuskiIndex) {
// get BnKuskiRules from user, if there are none and it is setting new rules, set BnEnabled to true
const rules = await BnKuskiRule.findAll({
where: { KuskiIndex: setting.KuskiIndex },
});
const isFirstTimeSetup = !rules.length && data.BnKuskiRules.length > 0;
await Setting.update(
{ BnEnabled: isFirstTimeSetup ? 1 : undefined },
{ where: { KuskiIndex: setting.KuskiIndex } },
);

// delete all BnKuskiRules from this user
await BnKuskiRule.destroy({ where: { KuskiIndex: setting.KuskiIndex } });
// for each rule, create a new BnKuskiRule
await Promise.all(
data.BnKuskiRules.map(async rule => {
await BnKuskiRule.create({
KuskiIndex: setting.KuskiIndex,
...rule,
});
}),
);
}
return 1;
};

const Player = async (IdentifierType, KuskiIdentifier, currentUser) => {
const query = {
where: {},
Expand Down Expand Up @@ -310,6 +396,34 @@ router
res.sendStatus(401);
}
})
.get('/bn/:DiscordId/linked', authDiscord, async (req, res) => {
const data = await Setting.findOne({
where: { DiscordId: req.params.DiscordId },
});
res.json(Boolean(data));
})
.get('/bn/:DiscordId', authDiscord, async (req, res) => {
const data = await BnSettings(req.params.DiscordId);
res.json(data);
})
.get('/bn', authDiscord, async (req, res) => {
const data = await AllActiveBnSettings();
res.json(data);
})
.post('/bn/:DiscordId/toggle/:BnEnabled', authDiscord, async (req, res) => {
const data = await ChangeBnEnabledSetting({
DiscordId: req.params.DiscordId,
BnEnabled: Number(req.params.BnEnabled),
});
res.json(data);
})
.post('/bn/:DiscordId', authDiscord, async (req, res) => {
const data = await ChangeBnSettings({
...req.body,
DiscordId: req.params.DiscordId,
});
res.json(data);
})
.post('/ignore/:Kuski', async (req, res) => {
const auth = authContext(req);
if (auth.auth) {
Expand Down
6 changes: 4 additions & 2 deletions src/config.defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export default {

// auth
// eslint-disable-next-line prettier/prettier
jwtSecret: 'eAwI4zcTDd4Pvc8QtN9z57Fqsr4ENNcTpK1x4A1dCLj0Y44OravXZDzNbA-4VEwAIh1Hw3vn1nhB9ygWLqAGE4GiX6hjjLsJi8IJ',
jwtSecret:
'eAwI4zcTDd4Pvc8QtN9z57Fqsr4ENNcTpK1x4A1dCLj0Y44OravXZDzNbA-4VEwAIh1Hw3vn1nhB9ygWLqAGE4GiX6hjjLsJi8IJ',
jwtAlgo: 'HS256',
recaptcha: {
client: '6Le-n9QUAAAAAG-3bYyysXddxwD6I6iJeDBTHf2r',
Expand All @@ -76,6 +77,7 @@ export default {
admin: 'admin',
},
apiAuth: '', // Authorization header sent by game events
url: 'https://test.elma.online/', // url used in discord messages
url: 'https://test.elma.online/', // url used in discord messages,
bnAuth: 'e5f13420-cf17-4fd5-8cc9-c96959d1048f', // Authorization header sent from BN
},
};
53 changes: 53 additions & 0 deletions src/data/models/BnKuskiRule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import DataType from 'sequelize';
import Model from '../sequelize';

const Setting = Model.define('bn_kuski_rule', {
BnKuskiRuleIndex: {
type: DataType.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
KuskiIndex: {
type: DataType.INTEGER,
allowNull: false,
defaultValue: 0,
},
BattleTypes: {
type: DataType.STRING(255),
allowNull: true,
defaultValue: null,
},
Designers: {
type: DataType.STRING(255),
allowNull: true,
defaultValue: null,
},
LevelPatterns: {
type: DataType.STRING(255),
allowNull: true,
defaultValue: null,
},
BattleAttributes: {
type: DataType.STRING(255),
allowNull: true,
defaultValue: null,
},
MinDuration: {
type: DataType.INTEGER,
allowNull: true,
defaultValue: 0,
},
MaxDuration: {
type: DataType.INTEGER,
allowNull: true,
defaultValue: 0,
},
IgnoreList: {
type: DataType.INTEGER,
allowNull: false,
defaultValue: 0,
},
});

export default Setting;
5 changes: 5 additions & 0 deletions src/data/models/Setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ const Setting = Model.define(
allowNull: false,
defaultValue: 1,
},
BnEnabled: {
type: DataType.INTEGER,
allowNull: false,
defaultValue: 0,
},
},
{
indexes: [
Expand Down
15 changes: 15 additions & 0 deletions src/data/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import MultiTimeFile from './MultiTimeFile';
import Crippled from './Crippled';
import Recap from './Recap';
import ReplayLog from './ReplayLog';
import BnKuskiRule from './BnKuskiRule';

Replay.belongsTo(Kuski, {
foreignKey: 'DrivenBy',
Expand Down Expand Up @@ -511,6 +512,18 @@ Recap.belongsTo(Replay, {
as: 'ReplayData',
});

Setting.hasMany(BnKuskiRule, {
foreignKey: 'KuskiIndex',
sourceKey: 'KuskiIndex',
as: 'BnKuskiRules',
});

BnKuskiRule.belongsTo(Setting, {
foreignKey: 'KuskiIndex',
targetKey: 'KuskiIndex',
as: 'SettingData',
});

function sync(...args) {
return sequelize.sync(...args);
}
Expand Down Expand Up @@ -576,4 +589,6 @@ export {
Crippled,
Recap,
ReplayLog,
ReplayTags,
BnKuskiRule,
}; // add the data model here as well so it exports
12 changes: 8 additions & 4 deletions src/utils/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ export const auth = async body => {
};
}
if (kuskiData.dataValues.Password) {
const md5 = crypto
.createHash('md5')
.update(password)
.digest('hex');
const md5 = crypto.createHash('md5').update(password).digest('hex');
if (md5 === kuskiData.dataValues.Password) {
if (kuskiData.dataValues.Salt) {
await addSha3(
Expand Down Expand Up @@ -124,3 +121,10 @@ export function authContext(req) {
}
return { auth: false, userid: 0 };
}

export function authDiscord(req, res, next) {
if (req.header('Authorization') === config.discord.bnAuth) {
return next();
}
return res.sendStatus(401);
}

0 comments on commit 8741085

Please sign in to comment.