Skip to content

Commit

Permalink
Merge pull request #46 from versx/distance-rework
Browse files Browse the repository at this point in the history
Multiple Locations Support + Individual Location Subscriptions
  • Loading branch information
versx authored May 28, 2021
2 parents 663c55f + fd0cd24 commit 8ebc4e7
Show file tree
Hide file tree
Showing 68 changed files with 1,685 additions and 331 deletions.
17 changes: 17 additions & 0 deletions src/data/map.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const Localizer = require('../services/locale.js');
const Location = require('../models/location.js');
const MapPokestop = require('../models/map/pokestop.js');
const config = require('../config.json');

Expand Down Expand Up @@ -33,6 +34,21 @@ const buildCityList = (guilds) => {
return cities;
};

const buildLocationsList = async (guilds, userId) => {
const locs = [];
const locations = await Location.getAllByUserId(userId);
for (let i = 0; i < locations.length; i++) {
const location = locations[i];
if (guilds.includes(location.guildId)) {
locs.push({
'name': location.name,
'guild': location.guildId,
});
}
}
return locs;
};

const getQuestRewards = async () => {
const quests = await MapPokestop.getAll();
const rewards = [];
Expand Down Expand Up @@ -71,6 +87,7 @@ const getLureTypes = () => {
module.exports = {
getPokemonNameIdsList,
buildCityList,
buildLocationsList,
getQuestRewards,
getInvasionTypes,
getLureTypes,
Expand Down
50 changes: 23 additions & 27 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ const uiRoutes = require('./routes/ui.js');
const { sessionStore, } = require('./services/session-store.js');
const utils = require('./services/utils.js');

const Localizer = require('./services/locale.js');
const forms = Localizer.getFormNames();
console.log('forms:', forms);

// TODO: Convert to typescript
// TODO: Import/export options
// TODO: Copy subscriptions to other discord server options
Expand Down Expand Up @@ -109,7 +105,7 @@ console.log('forms:', forms);
await Session.clearOthers(req.session.user_id, req.sessionID);
}
*/
if (config.discord.enabled && !req.session.valid) {
if (!req.session.valid && req.path !== '/login') {
console.error('Invalid user authenticated', req.session.user_id);
req.session.current_path = req.path;
res.redirect('/login');
Expand All @@ -121,32 +117,32 @@ console.log('forms:', forms);
return next();
}

if (req.session.logged_in) {
defaultData.logged_in = req.session.logged_in;
defaultData.username = req.session.username || 'root';
defaultData.user_id = req.session.user_id;
let valid = false;
const guilds = req.session.guilds;
const roles = req.session.roles;
defaultData.servers.forEach(server => {
if (roles[server.id]) {
const userRoles = roles[server.id];
const requiredRoles = config.discord.guilds.filter(x => x.id === server.id);
if (requiredRoles.length > 0) {
if (guilds.includes(server.id) && utils.hasRole(userRoles, requiredRoles[0].roles)) {
valid = true;
}
if (!req.session.logged_in) {
res.redirect('/login');
}
defaultData.logged_in = req.session.logged_in;
defaultData.username = req.session.username || 'root';
defaultData.user_id = req.session.user_id;
let valid = false;
const guilds = req.session.guilds;
const roles = req.session.roles;
defaultData.servers.forEach(server => {
if (roles[server.id]) {
const userRoles = roles[server.id];
const requiredRoles = config.discord.guilds.filter(x => x.id === server.id);
if (requiredRoles.length > 0) {
if (guilds.includes(server.id) && utils.hasRole(userRoles, requiredRoles[0].roles)) {
valid = true;
}
}
});
if (!req.session.valid || !valid) {
console.error('Invalid user authentication, no valid roles for user', req.session.user_id);
res.redirect('/login');
return;
}
return next();
});
if (!req.session.valid || !valid) {
console.error('Invalid user authentication, no valid roles for user', req.session.user_id);
res.redirect('/login');
return;
}
res.redirect('/login');
return next();
});

// API routes
Expand Down
6 changes: 5 additions & 1 deletion src/models/gym.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ Gym.init({
? data
: JSON.parse(data || '[]');
},
}
},
location: {
type: DataTypes.STRING(32),
defaultValue: null,
},
}, {
sequelize,
timestamps: false,
Expand Down
4 changes: 4 additions & 0 deletions src/models/invasion.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ Invasion.init({
: JSON.parse(data || '[]');
},
},
location: {
type: DataTypes.STRING(32),
defaultValue: null,
},
}, {
sequelize,
timestamps: false,
Expand Down
126 changes: 126 additions & 0 deletions src/models/location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
'use strict';

const { DataTypes, Model, } = require('sequelize');
const sequelize = require('../services/sequelize.js')(true);

class Location extends Model {

static getCount(guildId, userId) {
return Location.count({
where: {
guildId: guildId,
userId: userId,
}
});
}

static getAllByUserId(userId) {
return Location.findAll({
where: {
userId: userId,
}
});
}

static getAll(guildId, userId) {
return Location.findAll({
where: {
guildId: guildId,
userId: userId,
}
});
}

static getByName(guildId, userId, name) {
return Location.findOne({
where: {
guildId: guildId,
userId: userId,
name: name,
}
});
}

static getById(id) {
return Location.findByPk(id);
}

static delete(guildId, userId, name) {
return Location.destroy({
where: {
guildId: guildId,
userId: userId,
name: name,
}
});
}

static deleteById(id) {
return Location.destroy({
where: {
id: id,
}
});
}

static deleteAll(guildId, userId) {
return Location.destroy({
where: {
guildId: guildId,
userId: userId,
}
});
}
}

Location.init({
id: {
type: DataTypes.INTEGER(11).UNSIGNED,
primaryKey: true,
autoIncrement: true,
defaultValue: 0,
},
subscriptionId: {
type: DataTypes.INTEGER(11).UNSIGNED,
allowNull: false,
defaultValue: 0,
},
guildId: {
type: DataTypes.BIGINT(20).UNSIGNED,
allowNull: false,
},
userId: {
type: DataTypes.BIGINT(20).UNSIGNED,
allowNull: false,
},
name: {
type: DataTypes.STRING(128),
allowNull: false,
unique: true,
},
distance: {
type: DataTypes.INTEGER(11),
defaultValue: 0,
},
latitude: {
type: DataTypes.DOUBLE(18, 14),
defaultValue: 0,
},
longitude: {
type: DataTypes.DOUBLE(18, 14),
defaultValue: 0,
},
}, {
sequelize,
timestamps: false,
underscored: true,
indexes: [
{
name: 'FK_location_subscriptions_subscription_id',
fields: ['subscription_id'],
},
],
tableName: 'locations',
});

module.exports = Location;
4 changes: 4 additions & 0 deletions src/models/lure.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ Lure.init({
: JSON.parse(data || '[]');
},
},
location: {
type: DataTypes.STRING(32),
defaultValue: null,
},
}, {
sequelize,
timestamps: false,
Expand Down
4 changes: 4 additions & 0 deletions src/models/pokemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ Pokemon.init({
}
*/
},
location: {
type: DataTypes.STRING(32),
defaultValue: null,
},
}, {
sequelize,
timestamps: false,
Expand Down
4 changes: 4 additions & 0 deletions src/models/pvp.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ PVP.init({
}
*/
},
location: {
type: DataTypes.STRING(32),
defaultValue: null,
},
}, {
sequelize,
timestamps: false,
Expand Down
4 changes: 4 additions & 0 deletions src/models/quest.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ Quest.init({
: JSON.parse(data || '[]');
}
},
location: {
type: DataTypes.STRING(32),
defaultValue: null,
},
}, {
sequelize,
timestamps: false,
Expand Down
4 changes: 4 additions & 0 deletions src/models/raid.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ Raid.init({
: JSON.parse(data || '[]');
},
},
location: {
type: DataTypes.STRING(32),
defaultValue: null,
},
}, {
sequelize,
timestamps: false,
Expand Down
Loading

0 comments on commit 8ebc4e7

Please sign in to comment.