Skip to content

Commit

Permalink
Merge pull request #57 from versx/multi-pkmn-subs
Browse files Browse the repository at this point in the history
Add Support for Multiple Pokemon Subscriptions
  • Loading branch information
versx authored Jun 16, 2021
2 parents f454e12 + c7f72e2 commit 2416898
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 78 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "whmgr-ui",
"version": "1.10.3",
"version": "1.11.0",
"description": "",
"main": "src/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/data/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const config = require('../config.json');

const getPokemonNameIdsList = async () => {
const pokemon = [];
for (let i = 1; i < config.maxPokemonId; i++) {
for (let i = 1; i <= config.maxPokemonId; i++) {
const pkmnIcon = await Localizer.getPokemonIcon(i);
pokemon.push({
'id': i,
Expand Down
39 changes: 9 additions & 30 deletions src/models/pokemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,6 @@ const sequelize = require('../services/sequelize.js')(true);

class Pokemon extends Model {

static fromPokemonFields = [
//'id',
'guildId',
'userId',
'subscriptionId',
'pokemonId',
'form',
'minCp',
'minIv',
'ivList',
'minLvl',
'maxLvl',
'gender',
'size',
'city',
];

static getCount(guildId, userId) {
return Pokemon.count({
where: {
Expand All @@ -31,16 +14,6 @@ class Pokemon extends Model {
});
}

static async create(pokemon) {
if (pokemon.length === 0) {
return;
}
const results = await Pokemon.bulkCreate(pokemon, {
updateOnDuplicate: Pokemon.fromPokemonFields,
});
console.log('[Pokemon] Results:', results);
}

static getAll(guildId, userId) {
return Pokemon.findAll({
where: {
Expand All @@ -50,15 +23,21 @@ class Pokemon extends Model {
});
}

static getByPokemon(guildId, userId, pokemonId, form) {
static getByPokemon(guildId, userId, pokemon, form, iv, ivList, minLevel, maxLevel, gender, size) {
return Pokemon.findOne({
where: {
guildId: guildId,
userId: userId,
pokemonId: pokemonId,
pokemonId: pokemon,
form: {
[Op.or]: [null, form],
},
minIv: iv,
ivList: ivList,
minLvl: minLevel,
maxLvl: maxLevel,
gender: gender,
size: size,
}
});
}
Expand Down Expand Up @@ -105,7 +84,7 @@ Pokemon.init({
allowNull: false,
},
pokemonId: {
type: DataTypes.INTEGER(11).UNSIGNED,
type: DataTypes.TEXT(),
allowNull: false,
},
form: {
Expand Down
105 changes: 63 additions & 42 deletions src/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,34 @@ router.post('/server/:guild_id/user/:user_id', async (req, res) => {
if (pokemon) {
for (let pkmn of pokemon) {
pkmn = pkmn.toJSON();
const pkmnName = Localizer.getPokemonName(pkmn.pokemonId);
const pkmnIcon = await Localizer.getPokemonIcon(pkmn.pokemonId);
pkmn.name = `<img src='${pkmnIcon}' width='auto' height='32'>&nbsp;${pkmnName}`;
//if (pkmn.pokemonId === 'All') {
// pkmn.name = `<img src='/img/pokemon.png' width='auto' height='32'>&nbsp;` + Localizer.getValue('All');
//} else {
const ids = pkmn.pokemonId.split(',').sort((a, b) => a - b);
const icons = [];
const maxIcons = 8;
if (ids.length === 1) {
const id = ids[0];
const name = Localizer.getPokemonName(id);
const icon = await Localizer.getPokemonIcon(id);
const url = `<img src='${icon}' width='auto' height='32'>&nbsp;${name}`;
icons.push(url);
} else {
for (const [index, id] of ids.entries()) {
const icon = await Localizer.getPokemonIcon(id);
const url = `<img src='${icon}' width='auto' height='32'>&nbsp;`;
icons.push(url);
if (index > maxIcons) {
icons.push('<span><small style="color: grey;">& ' + (ids.length - 8) + ' more...</small></span>');
break;
}
}
}
pkmn.name = icons.join(' ');
//}
pkmn.cp = `${pkmn.minCp}-4096`;
pkmn.iv = pkmn.minIv;
pkmn.ivList = pkmn.ivList.length;//(JSON.parse(pkmn.iv_list || '[]') || []).length;
pkmn.ivList = pkmn.ivList.length;
pkmn.lvl = `${pkmn.minLvl}-${pkmn.maxLvl}`;
pkmn.gender = pkmn.gender === '*'
? 'All'
Expand Down Expand Up @@ -362,7 +384,7 @@ router.post('/server/:guild_id/user/:user_id', async (req, res) => {
router.post('/pokemon/new', async (req, res) => {
const {
guild_id,
pokemon,
//pokemon,
form,
iv,
iv_list,
Expand All @@ -373,51 +395,50 @@ router.post('/pokemon/new', async (req, res) => {
city,
location,
} = req.body;
let pokemon = req.body.pokemon;
const user_id = req.session.user_id;
const areas = city ? getAreas(guild_id, city.split(',')) : [];
const subscription = await Subscription.getSubscription(guild_id, user_id);
if (!subscription) {
showError(res, 'pokemon/new', `Failed to get user subscription for GuildId: ${guild_id} and UserId: ${user_id}`);
return;
}
const sql = [];
const split = pokemon.split(',');
const minIv = iv || 0;
const ivList = iv_list ? iv_list.replace(/\r\n/g, ',').replace(/\n/g, ',').split(',') : [];
for (const pokemonId of split) {
let exists = await Pokemon.getByPokemon(guild_id, user_id, pokemonId, form);
if (exists) {
exists.minCp = 0;
exists.minIv = iv || 0;
exists.ivList = ivList;
exists.minLvl = min_lvl || 0;
exists.maxLvl = max_lvl || 35;
exists.gender = gender || '*';
exists.size = size || 0;
exists.city = utils.arrayUnique(exists.city.concat(areas));
exists.location = location || null;
} else {
exists = Pokemon.build({
id: 0,
subscriptionId: subscription.id,
guildId: guild_id,
userId: user_id,
pokemonId: pokemonId,
form: form || null,
minCp: 0,
minIv: isUltraRarePokemon(pokemonId) ? 0 : iv || 0,
ivList: ivList,
minLvl: min_lvl || 0,
maxLvl: max_lvl || 35,
gender: gender || '*',
size: size || 0,
city: areas,
location: location || null,
});
}
sql.push(exists.toJSON());
const minLevel = min_lvl || 0;
const maxLevel = max_lvl || 35;
let exists = await Pokemon.getByPokemon(guild_id, user_id, pokemon, form, minIv, ivList, minLevel, maxLevel, gender, size);
if (exists) {
exists.minCp = 0;
exists.minIv = minIv;
exists.ivList = ivList;
exists.minLvl = minLevel;
exists.maxLvl = maxLevel;
exists.gender = gender;
exists.size = size;
exists.city = utils.arrayUnique(exists.city.concat(areas));
exists.location = location || null;
} else {
exists = Pokemon.build({
id: 0,
subscriptionId: subscription.id,
guildId: guild_id,
userId: user_id,
pokemonId: pokemon,
form: form || null,
minCp: 0,
minIv: /*isUltraRarePokemon(pokemonId) ? 0 :*/ iv || 0,
ivList: ivList,
minLvl: min_lvl || 0,
maxLvl: max_lvl || 35,
gender: gender || '*',
size: size || 0,
city: areas,
location: location || null,
});
}
try {
await Pokemon.create(sql);
await Pokemon.create(exists.toJSON());
} catch (err) {
console.error(err);
showError(res, 'pokemon/new', `Failed to create Pokemon ${pokemon} subscriptions for guild: ${guild_id} user: ${user_id}`);
Expand All @@ -430,7 +451,7 @@ router.post('/pokemon/edit/:id', async (req, res) => {
const id = req.params.id;
const {
guild_id,
//pokemon,
pokemon,
form,
iv,
iv_list,
Expand All @@ -446,7 +467,7 @@ router.post('/pokemon/edit/:id', async (req, res) => {
const areas = city ? getAreas(guild_id, city.split(',')) : [];
if (pkmn) {
const ivList = iv_list ? iv_list.replace('\r', '').split('\n') : [];
//pkmn.pokemonId = pokemon;
pkmn.pokemonId = pokemon;
pkmn.form = form;
pkmn.minCp = 0;
// If pokemon is rare (Unown, Azelf, etc), set IV value to 0
Expand Down
4 changes: 3 additions & 1 deletion src/routes/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ router.get('/pokemon/edit/:id', async (req, res) => {
return;
}
data.pokemon = await map.getPokemonNameIdsList();
const pokemonIds = pokemon.pokemonId.split(',');
data.pokemon.forEach(pkmn => {
pkmn.selected = parseInt(pkmn.id) === pokemon.pokemonId;
pkmn.selected = pokemonIds.includes(pkmn.id.toString());// || pokemon.pokemonId === 'All';
});
data.pokemon_ids = pokemon.pokemonId;
data.forms = Localizer.getFormNames();
data.form = pokemon.form;
data.iv = pokemon.minIv;
Expand Down
4 changes: 4 additions & 0 deletions src/services/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class Localizer {
})();
}

getValue(key) {
return i18n.__(key);
}

getPokemonName(pokemonId) {
const name = i18n.__('poke_' + pokemonId);
return name;
Expand Down
28 changes: 28 additions & 0 deletions src/views/pokemon/edit.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,34 @@
<br>
<div class="w-75" style="float: none; margin: 0 auto;">
<form action="/api/pokemon/edit/{{id}}" method="post">
<div class="form-group">
<div class="form-group">
{{Search}}
<input type="text" class="form-control" id="search" value="" placeholder="i.e Pikachu or 25" onkeyup="onPokemonSearch()">
</div>
{{Pokemon}}
<div id="pokemon-list" name="pokemon-list" class="pokemon-list custom-control">
{{#pokemon}}
<div class="pokemon-icon-sprite item {{#selected}}active{{/selected}}" style="{{#selected}}background-color: dodgerblue;{{/selected}}" id="{{id}}" name="{{name}}" data-value="{{id}}" onclick="onPokemonClicked(this)">
<img src="{{image_url}}" width="48" height="48" />
<span class="caption">#{{id}}</span>
</div>
{{/pokemon}}
</div>
<input type="hidden" id="pokemon" name="pokemon" value="{{pokemon_ids}}" />
<br>
<button id="select_all" type="button" class="btn btn-primary btn-sm">{{Select All}}</button>
<button id="select_none" type="button" class="btn btn-primary btn-sm">{{Select None}}</button>
<button id="select_invert" type="button" class="btn btn-primary btn-sm">{{Invert Selection}}</button>
<button id="select_gen1" type="button" class="btn btn-warning btn-sm">{{Select Gen1}}</button>
<button id="select_gen2" type="button" class="btn btn-warning btn-sm">{{Select Gen2}}</button>
<button id="select_gen3" type="button" class="btn btn-warning btn-sm">{{Select Gen3}}</button>
<button id="select_gen4" type="button" class="btn btn-warning btn-sm">{{Select Gen4}}</button>
<button id="select_gen5" type="button" class="btn btn-warning btn-sm">{{Select Gen5}}</button>
<button id="select_gen6" type="button" class="btn btn-warning btn-sm">{{Select Gen6}}</button>
<button id="select_rare" type="button" class="btn btn-success btn-sm">{{Select Rare}}</button>
<button id="select_ultra" type="button" class="btn btn-danger btn-sm">{{Select Ultra Rare}}</button>
</div>
<div class="form-group">
{{Form}}
<!--
Expand Down
6 changes: 3 additions & 3 deletions src/views/pokemon/pokemon.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<table id="table" class="table {{table_class}} table-sm table-striped table-bordered table-hover dt-responsive nowrap" style="position: center; width:100%">
<thead class="thead-dark">
<tr>
<th class="all">{{ID}}</th>
<!--<th class="all">{{ID}}</th>-->
<th class="all">{{Pokemon}}</th>
<th class="min-desktop">{{Form}}</th>
<th class="min-desktop">{{CP}}</th>
Expand Down Expand Up @@ -119,7 +119,7 @@
"lengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
"pageLength": 100,
"columns": [
{ "data": "pokemonId" },
//{ "data": "pokemonId" },
{ "data": "name" },
{ "data": "form" },
{ "data": "cp" },
Expand All @@ -136,7 +136,7 @@
"order": [[ 0, "asc" ]],
"search.caseInsensitive": true,
"columnDefs": [{
"targets": [10],
"targets": [9],
"orderable": false,
}],
"responsive": true,
Expand Down

0 comments on commit 2416898

Please sign in to comment.