Skip to content

Commit

Permalink
Merge pull request #9 from versx/pokemon-cities
Browse files Browse the repository at this point in the history
Allow Pokemon/PvP specific city subscriptions
  • Loading branch information
versx authored Nov 21, 2020
2 parents 3d4bbd9 + 4a7d544 commit e396032
Show file tree
Hide file tree
Showing 31 changed files with 575 additions and 490 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ config.json
logs

# No geofences
#geofences/*
geofences/*

# No icons
static/img/pokemon/
Expand Down
4 changes: 2 additions & 2 deletions src/config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
],
"urls": {
"images": {
"pokemon": "https://cdn.example.com/images/original/monsters/%s_%s.png",
"eggs": "../img/original/eggs/%s.png"
"pokemon": "https://mygod.github.io/pokicons/v2",
"eggs": "/img/eggs/%s.png"
},
"map": "",
"paypal": ""
Expand Down
24 changes: 13 additions & 11 deletions src/data/map.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
'use strict';

const locale = require('../services/locale.js');
const utils = require('../services/utils.js');
const Localizer = require('../services/locale.js');
const GeofenceService = require('../services/geofence.js');

const svc = new GeofenceService.GeofenceService();
const config = require('../config.json');
const grunttypes = require('../../static/data/grunttype.json');

const getPokemonNameIdsList = () => {
const getPokemonNameIdsList = async () => {
let pokemon = [];
for (let i = 1; i < config.maxPokemonId; i++) {
const pkmnIcon = await Localizer.getPokemonIcon(i);
pokemon.push({
'id': i,
'id_3': (i + '').padStart(3, '0'),
'name': locale.getPokemonName(i),
'image_url': utils.getPokemonIcon(i, 0)
'name': Localizer.getPokemonName(i),
'image_url': pkmnIcon,
});
}
return pokemon;
};

const getGruntRewardIdsList = () => {
const getGruntRewardIdsList = async () => {
const grunts = grunttypes;
const rewards = [];
const keys = Object.keys(grunts);
Expand All @@ -36,11 +36,12 @@ const getGruntRewardIdsList = () => {
if (exists.length > 0) {
continue;
}
const pkmnIcon = await Localizer.getPokemonIcon(pokemonId);
rewards.push({
'pokemon_id': pokemonId,
'pokemon_id_3': (pokemonId + '').padStart(3, '0'),
'name': locale.getPokemonName(pokemonId),
'image_url': utils.getPokemonIcon(pokemonId, 0)
'name': Localizer.getPokemonName(pokemonId),
'image_url': pkmnIcon,
});
}
if (grunt.second_reward) {
Expand All @@ -51,11 +52,12 @@ const getGruntRewardIdsList = () => {
if (exists.length > 0) {
continue;
}
const pkmnIcon = await Localizer.getPokemonIcon(pokemonId);
rewards.push({
'pokemon_id': pokemonId,
'pokemon_id_3': (pokemonId + '').padStart(3, '0'),
'name': locale.getPokemonName(pokemonId),
'image_url': utils.getPokemonIcon(pokemonId, 0)
'name': Localizer.getPokemonName(pokemonId),
'image_url': pkmnIcon,
});
}
}
Expand All @@ -75,7 +77,7 @@ const buildCityList = (guilds) => {
if (guilds.includes(configGuild.id) && configGuild.geofences.includes(geofence.name)) {
cities.push({
'name': geofence.name,
'guild': configGuild.id
'guild': configGuild.id,
});
}
}
Expand Down
21 changes: 14 additions & 7 deletions src/data/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const config = require('../config.json');
const MySQLConnector = require('../services/mysql.js');
const db = new MySQLConnector(config.db.brock);
const locale = require('../services/locale.js');
const Localizer = require('../services/locale.js');

// TODO: Move to model classes

Expand Down Expand Up @@ -96,12 +96,12 @@ const getPokemonSubscriptions = async (guildId, userId) => {
const results = await db.query(sql, args);
if (results) {
results.forEach(result => {
result.name = locale.getPokemonName(result.pokemon_id);
result.name = Localizer.getPokemonName(result.pokemon_id);
result.cp = `${result.min_cp}-4096`;
result.iv = result.min_iv;
result.iv_list = JSON.parse(result.iv_list || '[]');
result.lvl = `${result.min_lvl}-${result.max_lvl}`;
//result.city = result.city;
result.city = JSON.parse(result.city || '[]');
});
}
return results;
Expand All @@ -117,9 +117,9 @@ const getPvpSubscriptions = async (guildId, userId) => {
const results = await db.query(sql, args);
if (results) {
results.forEach(result => {
result.name = locale.getPokemonName(result.pokemon_id);
result.name = Localizer.getPokemonName(result.pokemon_id);
//result.min_rank = result.min_rank;
//result.city = result.city;
result.city = JSON.parse(result.city || '[]');
});
}
return results;
Expand All @@ -135,7 +135,8 @@ const getRaidSubscriptions = async (guildId, userId) => {
const results = await db.query(sql, args);
if (results) {
results.forEach(result => {
result.name = locale.getPokemonName(result.pokemon_id);
result.name = Localizer.getPokemonName(result.pokemon_id);
result.city = JSON.parse(result.city || '[]');
});
}
return results;
Expand All @@ -160,6 +161,11 @@ const getQuestSubscriptions = async (guildId, userId) => {
`;
const args = [guildId, userId];
const results = await db.query(sql, args);
if (results) {
results.forEach(result => {
result.city = JSON.parse(result.city || '[]');
});
}
return results;
};

Expand All @@ -173,7 +179,8 @@ const getInvasionSubscriptions = async (guildId, userId) => {
const results = await db.query(sql, args);
if (results) {
results.forEach(result => {
result.reward = locale.getPokemonName(result.reward_pokemon_id);
result.reward = Localizer.getPokemonName(result.reward_pokemon_id);
result.city = JSON.parse(result.city || '[]');
});
}
return results;
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ const discordRoutes = require('./routes/discord.js');
const uiRoutes = require('./routes/ui.js');
const utils = require('./services/utils.js');

// TODO: Group quests and invasion cities when listing
// TODO: Convert to typescript
// TODO: Import/export options
// TODO: Copy subscriptions to other discord server options
// TODO: Update insert if already exists update
// TODO: Cookie sessions
// TODO: Disable server selector when in edit pages
// TODO: Add proper error messages
// TODO: Add checkbox to only show available invasion rewards
// TODO: Lookup form name to id and show proper icon

(async () => {
// Basic security protections
Expand Down
10 changes: 5 additions & 5 deletions src/models/gym.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,17 @@ class Gym {
return result.affectedRows > 0;
}

static async save(id, guildId, userId, name) {
async save() {
const sql = `
UPDATE gyms
SET name = ?
WHERE guild_id = ? AND user_id = ? AND id = ?
`;
const args = [
name,
guildId,
userId,
id
this.name,
this.guildId,
this.userId,
this.id
];
const result = await db.query(sql, args);
return result.affectedRows === 1;
Expand Down
48 changes: 27 additions & 21 deletions src/models/invasion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const MySQLConnector = require('../services/mysql.js');
const db = new MySQLConnector(config.db.brock);

class Invasion {
constructor(subscriptionId, guildId, userId, rewardPokemonId, city) {
constructor(id, subscriptionId, guildId, userId, rewardPokemonId, city) {
this.id = id;
this.subscriptionId = subscriptionId;
this.guildId = guildId;
this.userId = userId;
Expand All @@ -20,16 +21,18 @@ class Invasion {
`;
const args = [
this.subscriptionId,
this.guildId, this.userId,
this.rewardPokemonId, this.city
this.guildId,
this.userId,
this.rewardPokemonId,
JSON.stringify(this.city || []),
];
const result = await db.query(sql, args);
return result.affectedRows === 1;
}

static async getAll(guildId, userId) {
const sql = `
SELECT subscription_id, guild_id, user_id, reward_pokemon_id, city
SELECT id, subscription_id, guild_id, user_id, reward_pokemon_id, city
FROM invasions
WHERE guild_id = ? AND user_id = ?
`;
Expand All @@ -39,11 +42,12 @@ class Invasion {
const list = [];
results.forEach(result => {
list.push(new Invasion(
result.id,
result.subscription_id,
result.guild_id,
result.user_id,
result.reward_pokemon_id,
result.city
JSON.parse(result.city || '[]'),
));
});
return list;
Expand All @@ -53,7 +57,7 @@ class Invasion {

static async getById(id) {
const sql = `
SELECT subscription_id, guild_id, user_id, reward_pokemon_id, city
SELECT id, subscription_id, guild_id, user_id, reward_pokemon_id, city
FROM invasions
WHERE id = ?
`;
Expand All @@ -62,44 +66,46 @@ class Invasion {
if (results && results.length > 0) {
const result = results[0];
return new Invasion(
result.id,
result.subscription_id,
result.guild_id,
result.user_id,
result.reward_pokemon_id,
result.city
JSON.parse(result.city || '[]'),
);
}
return null;
}

static async getByReward(guildId, userId, reward, city) {
static async getByReward(guildId, userId, reward) {
const sql = `
SELECT subscription_id, guild_id, user_id, reward_pokemon_id, city
SELECT id, subscription_id, guild_id, user_id, reward_pokemon_id, city
FROM invasions
WHERE guild_id = ? AND user_id = ? AND reward_pokemon_id = ? AND city = ?
WHERE guild_id = ? AND user_id = ? AND reward_pokemon_id = ?
LIMIT 1
`;
const args = [guildId, userId, reward, city];
const args = [guildId, userId, reward];
const results = await db.query(sql, args);
if (results && results.length > 0) {
const result = results[0];
return new Invasion(
result.id,
result.subscription_id,
result.guild_id,
result.user_id,
result.reward_pokemon_id,
result.city
JSON.parse(result.city || '[]'),
);
}
return null;
}

static async delete(guildId, userId, rewardPokemonId, city) {
static async delete(guildId, userId, rewardPokemonId) {
const sql = `
DELETE FROM invasions
WHERE guild_id = ? AND user_id = ? AND reward_pokemon_id = ? AND city = ?
WHERE guild_id = ? AND user_id = ? AND reward_pokemon_id = ?
`;
const args = [guildId, userId, rewardPokemonId, city];
const args = [guildId, userId, rewardPokemonId];
const result = await db.query(sql, args);
return result.affectedRows === 1;
}
Expand All @@ -124,18 +130,18 @@ class Invasion {
return result.affectedRows > 0;
}

static async save(id, guildId, userId, reward, city) {
async save() {
const sql = `
UPDATE invasions
SET reward_pokemon_id = ?, city = ?
WHERE guild_id = ? AND user_id = ? AND id = ?
`;
const args = [
reward,
city,
guildId,
userId,
id
this.rewardPokemonId,
JSON.stringify(this.city),
this.guildId,
this.userId,
this.id
];
const result = await db.query(sql, args);
return result.affectedRows === 1;
Expand Down
Loading

0 comments on commit e396032

Please sign in to comment.