From f9832072ce18f8d3fc3d11bc1a660675717c8d66 Mon Sep 17 00:00:00 2001 From: Felix Date: Sat, 10 Sep 2016 14:50:39 +0200 Subject: [PATCH] Update - Added pokemon family enums - Stable candy bag - Pokemon candy support - Release pokemons, even from database and award candies --- src/enum.js | 1 + src/models/Player/CandyBag/index.js | 73 ++++++++++++++++++--- src/models/Player/index.js | 31 ++++++--- src/models/Player/packets/GetInventory.js | 6 ++ src/models/Player/packets/ReleasePokemon.js | 33 +++++----- src/models/Pokemon/index.js | 30 +++++++++ src/models/World/packets/CatchPokemon.js | 1 - 7 files changed, 142 insertions(+), 33 deletions(-) diff --git a/src/enum.js b/src/enum.js index 9ff3288..be8327a 100644 --- a/src/enum.js +++ b/src/enum.js @@ -6,6 +6,7 @@ export default { GENDER: proto.Enums.Gender, TUTORIAL: proto.Enums.TutorialState, POKEMON_IDS: proto.Enums.PokemonId, + POKEMON_FAMILY: proto.Enums.PokemonFamilyId, getNameById: (emu, id) => { id <<= 0; for (let key in emu) { diff --git a/src/models/Player/CandyBag/index.js b/src/models/Player/CandyBag/index.js index 6a734ed..a277348 100644 --- a/src/models/Player/CandyBag/index.js +++ b/src/models/Player/CandyBag/index.js @@ -1,3 +1,7 @@ +import { GAME_MASTER } from "../../../shared"; + +import ENUM from "../../../enum"; + /** * @class CandyBag */ @@ -19,12 +23,48 @@ export default class CandyBag { * @param {Number} dex * @return {Object} */ - getCandyByDexNumber(dex) { + getPkmnTemplate(dex) { + let tmpl = GAME_MASTER.getPokemonTmplByDex(dex); + return (tmpl); + } + + /** + * @param {Number} dex + * @return {String} + */ + getPkmnFamily(dex) { return ( - this.candies[dex << 0] || null + this.getPkmnTemplate(dex).family_id ); } + /** + * @param {Number} dex + * @return {Object} + */ + createCandy(dex) { + let id = ENUM.getIdByName(ENUM.POKEMON_FAMILY, this.getPkmnFamily(dex << 0)); + let candy = { + amount: 0 + }; + this.candies[id] = candy; + return (candy); + } + + /** + * @param {Number} dex + * @return {Object} + */ + getCandyByDexNumber(dex) { + let id = ENUM.getIdByName(ENUM.POKEMON_FAMILY, this.getPkmnFamily(dex << 0)); + if (this.candies[id] !== void 0) { + return (this.candies[id]); + } + else { + return (this.createCandy(id) || null); + } + } + /** * @param {Number} dex * @return {Number} @@ -41,7 +81,7 @@ export default class CandyBag { */ addCandy(dex, amount) { let candy = this.getCandyByDexNumber(dex); - candy.amount += amount << 0; + candy.amount += parseInt(amount); } /** @@ -50,24 +90,39 @@ export default class CandyBag { */ removeCandy(dex, amount) { let candy = this.getCandyByDexNumber(dex); - candy.amount -= amount << 0; + candy.amount -= parseInt(amount); if (candy.amount < 0) candy.amount = 0; } /** - * @return {String} + * @return {Array} */ serialize() { - return ( - JSON.stringify(this.candies) - ); + let out = []; + for (let key in this.candies) { + let candy = this.candies[key]; + if (!(candy.amount > 0)) continue; + out.push({ + modified_timestamp_ms: +new Date() - 1e3, + inventory_item_data: { + candy: { + family_id: this.getPkmnFamily(key << 0), + candy: candy.amount + } + } + }); + }; + return (out); } /** * @param {String} str */ parseJSON(str) { - this.candies = JSON.parse(str); + let candies = JSON.parse(str); + for (let candy in candies) { + this.createCandy(candy).amount = parseInt(candies[candy]); + }; } } \ No newline at end of file diff --git a/src/models/Player/index.js b/src/models/Player/index.js index df72a95..1d3f312 100644 --- a/src/models/Player/index.js +++ b/src/models/Player/index.js @@ -133,7 +133,9 @@ export default class Player extends MapObject { resolve(this.LevelUpRewards(msg)); break; case "RELEASE_POKEMON": - resolve(this.ReleasePokemon(msg)); + this.ReleasePokemon(msg).then((result) => { + resolve(result); + }); break; case "GET_PLAYER_PROFILE": resolve(this.GetPlayerProfile(msg)); @@ -264,26 +266,26 @@ export default class Player extends MapObject { /** * @param {WildPokemon} pkmn * @param {String} ball - * @return {Object} */ catchPkmn(pkmn, ball) { this.info.exp += 100; this.info.stardust += 100; this.info.pkmnCaptured += 1; this.currentEncounter = null; - pkmn.owner = this; - pkmn.calcStats(); pkmn.catchedBy(this); pkmn.pokeball = ball; return new Promise((resolve) => { + pkmn.owner = this; + pkmn.calcStats(); pkmn.insertIntoDatabase().then((insertId) => { - print(insertId, 36); - pkmn.uid = pkmn.insertId; - let partyPkmn = this.party.addPkmn(pkmn); + pkmn = this.party.addPkmn(pkmn); + pkmn.owner = this; + pkmn.uid = insertId; + pkmn.addCandies(3); print(`${this.username} catched a wild ${pkmn.getPkmnName()}!`); resolve({ status: "CATCH_SUCCESS", - captured_pokemon_id: partyPkmn.uid, + captured_pokemon_id: pkmn.uid, capture_award: { activity_type: ["ACTIVITY_CATCH_POKEMON"], xp: [100], @@ -295,6 +297,19 @@ export default class Player extends MapObject { }); } + /** + * @param {WildPokemon} pkmn + */ + releasePkmn(pkmn) { + pkmn.addCandies(3); + this.party.deletePkmn(pkmn.uid); + return new Promise((resolve) => { + pkmn.deleteFromDatabase().then(() => { + resolve(); + }); + }); + } + } inherit(Player, _packets); \ No newline at end of file diff --git a/src/models/Player/packets/GetInventory.js b/src/models/Player/packets/GetInventory.js index 6545a79..9128255 100644 --- a/src/models/Player/packets/GetInventory.js +++ b/src/models/Player/packets/GetInventory.js @@ -9,12 +9,18 @@ export default function GetInventory(msg) { let items = this.bag.serialize(); let stats = this.info.serialize(); let party = this.party.serialize(); + let candies = this.candyBag.serialize(); let currencies = this.currency.serialize(); //let pokedex = this.pokedex.serialize(); items.push(stats); + items.push(candies); items.push(currencies); + candies.map((candy) => { + items.push(candy); + }); + party.map((pkmn) => { items.push(pkmn); }); diff --git a/src/models/Player/packets/ReleasePokemon.js b/src/models/Player/packets/ReleasePokemon.js index 556d346..c024e70 100644 --- a/src/models/Player/packets/ReleasePokemon.js +++ b/src/models/Player/packets/ReleasePokemon.js @@ -7,22 +7,25 @@ import POGOProtos from "pokemongo-protobuf"; export default function ReleasePokemon(msg) { let buffer = null; + let schema = "POGOProtos.Networking.Responses.ReleasePokemonResponse"; + let pkmn = this.party.getPkmnById(msg.pokemon_id); - if (pkmn) { - this.party.deletePkmn(pkmn.uid); - buffer = { - result: "SUCCESS", - candy_awarded: 0 - }; - } else { - buffer = { - result: "FAILED" - }; - } - - return ( - POGOProtos.serialize(buffer, "POGOProtos.Networking.Responses.ReleasePokemonResponse") - ); + return new Promise((resolve) => { + if (pkmn) { + this.releasePkmn(pkmn).then((result) => { + buffer = { + result: "SUCCESS", + candy_awarded: 3 + }; + resolve(POGOProtos.serialize(buffer, schema)); + }); + } else { + buffer = { + result: "FAILED" + }; + resolve(POGOProtos.serialize(buffer, schema)); + } + }); } \ No newline at end of file diff --git a/src/models/Pokemon/index.js b/src/models/Pokemon/index.js index d8f41e9..0e31efc 100644 --- a/src/models/Pokemon/index.js +++ b/src/models/Pokemon/index.js @@ -14,6 +14,7 @@ import { import print from "../../print"; import CFG from "../../../cfg"; +import ENUM from "../../enum"; import * as _calc from "./calc"; import * as _actions from "./action"; @@ -150,6 +151,25 @@ export default class Pokemon extends MapObject { ); } + /** + * @param {Number} dex + * @return {String} + */ + getPkmnFamily(dex) { + return ( + this.getPkmnTemplate(dex).family_id + ); + } + + /** + * @param {Number} amount + */ + addCandies(amount) { + let family = this.getPkmnFamily(this.dexNumber); + let id = ENUM.getIdByName(ENUM.POKEMON_FAMILY, family) << 0; + if (this.owner) this.owner.candyBag.addCandy(id, parseInt(amount)); + } + /** * @return {Boolean} */ @@ -220,6 +240,16 @@ export default class Pokemon extends MapObject { }); } + deleteFromDatabase() { + let query = `DELETE FROM ${CFG.MYSQL_OWNED_PKMN_TABLE} WHERE id=? AND owner_id=? LIMIT 1`; + return new Promise((resolve) => { + this.owner.world.db.query(query, [this.uid, this.owner.uid], (e, res) => { + if (e) return print(e, 31); + resolve(res); + }); + }); + } + /** * @return {Object} */ diff --git a/src/models/World/packets/CatchPokemon.js b/src/models/World/packets/CatchPokemon.js index ed8d2b4..e7dffa5 100644 --- a/src/models/World/packets/CatchPokemon.js +++ b/src/models/World/packets/CatchPokemon.js @@ -17,7 +17,6 @@ export default function CatchPokemon(msg) { player.bag[ball] -= 1; return new Promise((resolve) => { - // Invalid pkmn if (!pkmn) { player.currentEncounter = null;