- Added basic gym map support
- Splitted pokestops and gyms into seperate classes
- Added pokestop and gym tables
This commit is contained in:
Felix 2016-08-31 20:34:05 +02:00
parent 8f0c21c34a
commit 4d7b6bf56c
10 changed files with 220 additions and 108 deletions

View File

@ -41,7 +41,8 @@ export default {
MYSQL_USERNAME: "root",
MYSQL_PASSWORD: "",
MYSQL_USERS_TABLE: "users",
MYSQL_FORT_TABLE: "forts",
MYSQL_GYM_TABLE: "gym",
MYSQL_POKESTOP_TABLE: "pokestop",
MYSQL_OWNED_PKMN_TABLE: "owned_pkmn",
// Used for asset download session

View File

@ -24,8 +24,10 @@ export function createTables() {
return new Promise((resolve) => {
this.createTable(CFG.MYSQL_USERS_TABLE).then(() => {
this.createTable(CFG.MYSQL_OWNED_PKMN_TABLE).then(() => {
this.createTable(CFG.MYSQL_FORT_TABLE).then(() => {
resolve();
this.createTable(CFG.MYSQL_POKESTOP_TABLE).then(() => {
this.createTable(CFG.MYSQL_GYM_TABLE).then(() => {
resolve();
});
});
});
});

8
src/db/tables/gym.table Normal file
View File

@ -0,0 +1,8 @@
id int(11) NOT NULL AUTO_INCREMENT,
cell_id varchar(64) NOT NULL,
latitude double NOT NULL,
longitude double NOT NULL,
team int(1) NOT NULL,
in_battle tinyint(1) NOT NULL DEFAULT '0',
points int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (id)

View File

@ -1,6 +1,8 @@
import s2 from "s2-geometry";
import Fort from "../Fort";
import Gym from "../Fort/Gym";
import Pokestop from "../Fort/Pokestop";
import MapObject from "../MapObject";
import Settings from "../../../modes";
import CFG from "../../../../cfg";
@ -27,6 +29,8 @@ export default class Cell extends MapObject {
this.forts = [];
this.type = obj.type;
}
/**
@ -62,11 +66,38 @@ export default class Cell extends MapObject {
});
}
/**
* @param {String} type
* @return {String}
*/
static getTable(type) {
return (
type === "CHECKPOINT" ?
CFG.MYSQL_POKESTOP_TABLE :
CFG.MYSQL_GYM_TABLE
);
}
getFortsFromDatabase() {
return new Promise((resolve) => {
this.world.instance.getQueryByColumnFromTable("cell_id", this.cellId, CFG.MYSQL_FORT_TABLE).then((forts) => {
resolve(forts || []);
});
try {
let out = [];
this.world.instance.getQueryByColumnFromTable("cell_id", this.cellId, CFG.MYSQL_POKESTOP_TABLE).then((forts) => {
forts = forts || [];
forts.map((fort) => {
fort.type = "CHECKPOINT";
out.push(fort);
});
this.world.instance.getQueryByColumnFromTable("cell_id", this.cellId, CFG.MYSQL_GYM_TABLE).then((forts) => {
forts = forts || [];
forts.map((fort) => {
fort.type = "GYM";
out.push(fort);
});
resolve(out);
});
});
} catch (e) { console.log(e); }
});
}
@ -84,7 +115,7 @@ export default class Cell extends MapObject {
setTimeout(() => {
this.deleteFortById(fort.uid);
this.deleteFortFromDatabase(fort).then(() => {
// do sth after
});
}, (MAP_REFRESH_RATE * 1e3) * 3);
}
@ -95,7 +126,8 @@ export default class Cell extends MapObject {
*/
deleteFortFromDatabase(fort) {
return new Promise((resolve) => {
this.world.instance.db.query(`DELETE FROM ${CFG.MYSQL_FORT_TABLE} WHERE cell_id=? AND id=? LIMIT 1`, [fort.cellId, fort.uid], (e, res) => {
let table = Cell.getTable(fort.type);
this.world.instance.db.query(`DELETE FROM ${table} WHERE cell_id=? AND id=? LIMIT 1`, [fort.cellId, fort.uid], (e, res) => {
resolve();
});
});
@ -140,7 +172,8 @@ export default class Cell extends MapObject {
*/
addFort(obj) {
obj.world = this.world;
let fort = new Fort(obj);
let fort = null;
fort = obj.type === "CHECKPOINT" ? new Pokestop(obj) : new Gym(obj);
this.forts.push(fort);
return (fort);
}

View File

@ -0,0 +1,53 @@
import Fort from "../index";
import print from "../../../../print";
import CFG from "../../../../../cfg";
import {
validName
} from "../../../../utils";
/**
* @class Gym
*/
export default class Gym extends Fort {
/**
* @param {Object} obj
* @constructor
*/
constructor(obj) {
super(obj);
this.team = "RED";
this.guardPkmn = 0;
this.guardPkmnCp = 0;
this.gymPoints = 0;
this.isInBattle = 0;
this.init(obj);
}
/**
* @return {Object}
*/
serialize() {
return ({
id: this.getId(),
last_modified_timestamp_ms: +new Date(),
latitude: this.latitude,
longitude: this.longitude,
enabled: this.enabled,
owned_by_team: this.team,
guard_pokemon_id: this.guardPkmn,
gym_points: this.gymPoints,
active_fort_modifier: []
});
}
}

View File

@ -0,0 +1,71 @@
import Fort from "../index";
import print from "../../../../print";
import CFG from "../../../../../cfg";
import {
validName
} from "../../../../utils";
import ENUM from "../../../../enum";
/**
* @class Gym
*/
export default class Pokestop extends Fort {
/**
* @param {Object} obj
* @constructor
*/
constructor(obj) {
super(obj);
this.name = null;
this.description = null;
this.image_url = null;
this.cooldown = 5e3;
this.experience = 0;
this.rewards = this.parseRewards(obj.rewards);
this.init(obj);
}
/**
* @return {Object}
*/
parseRewards(rewards) {
rewards = rewards || "{}";
let json = null;
try {
json = JSON.parse(rewards);
} catch (e) {
json = {};
print(e, 31);
}
return (json);
}
/**
* @return {Array}
*/
serializeRewards() {
let out = [];
for (let key in this.rewards) {
let amount = this.rewards[key] << 0;
for (let ii = 0; ii < amount; ++ii) {
out.push({
item_id: ENUM.getItemNameById(ENUM.ITEMS, key << 0)
});
};
};
return (out);
}
}

View File

@ -7,8 +7,6 @@ import {
validName
} from "../../../utils";
import enum from "../../../enum";
/**
* @class Fort
*/
@ -25,16 +23,7 @@ export default class Fort extends MapObject {
this.enabled = true;
this.deleted = false;
this.name = null;
this.description = null;
this.image_url = null;
this.cooldown = 5e3;
this.experience = 0;
this.rewards = this.parseRewards(obj.rewards);
this.type = null;
this.init(obj);
@ -52,37 +41,6 @@ export default class Fort extends MapObject {
};
}
/**
* @return {Object}
*/
parseRewards(rewards) {
rewards = rewards || "{}";
let json = null;
try {
json = JSON.parse(rewards);
} catch (e) {
json = {};
print(e, 31);
}
return (json);
}
/**
* @return {Array}
*/
serializeRewards() {
let out = [];
for (let key in this.rewards) {
let amount = this.rewards[key] << 0;
for (let ii = 0; ii < amount; ++ii) {
out.push({
item_id: enum.getItemNameById(enum.ITEMS, key << 0)
});
};
};
return (out);
}
/**
* @return {String}
*/
@ -106,7 +64,7 @@ export default class Fort extends MapObject {
latitude: this.latitude,
longitude: this.longitude,
enabled: this.enabled,
type: "CHECKPOINT"
type: this.type
});
}

View File

@ -1,46 +0,0 @@
import MapObject from "../MapObject";
import print from "../../../print";
import CFG from "../../../../cfg";
import {
validName
} from "../../../utils";
/**
* @class Gym
*/
export default class Gym extends MapObject {
/**
* @param {Object} obj
* @constructor
*/
constructor(obj) {
super(obj);
this.init(obj);
}
/**
* @param {Object} obj
*/
init(obj) {
obj = obj || {};
for (let key in obj) {
if (this.hasOwnProperty(key)) {
this[key] = obj[key];
}
};
}
/**
* @return {Object}
*/
serialize() {
return ({});
}
}

View File

@ -103,14 +103,29 @@ export function deleteFort(cellId, uid) {
*/
export function insertFortIntoDatabase(obj) {
return new Promise((resolve) => {
let cellId = Cell.getIdByPosition(obj.latitude, obj.longitude, obj.zoom);
let query = `INSERT INTO ${CFG.MYSQL_FORT_TABLE} SET cell_id=?, latitude=?, longitude=?, name=?, description=?, image_url=?, experience=?, rewards=?`;
let lat = obj.latitude;
let lng = obj.longitude;
let name = obj.name;
let desc = obj.description;
let img = obj.image || "";
let exp = obj.experience || 500;
if (obj.type === "CHECKPOINT") {
this.insertPokestopIntoDatabase(obj).then((gym) => {
resolve(gym);
});
}
else if (obj.type === "GYM") {
this.insertGymIntoDatabase(obj).then((pokestop) => {
resolve(pokestop);
});
}
});
}
export function insertPokestopIntoDatabase(obj) {
let cellId = Cell.getIdByPosition(obj.latitude, obj.longitude, obj.zoom);
let lat = obj.latitude;
let lng = obj.longitude;
let name = obj.name;
let desc = obj.description;
let img = obj.image || "";
let exp = obj.experience || 500;
let query = `INSERT INTO ${Cell.getTable(obj.type)} SET cell_id=?, latitude=?, longitude=?, name=?, description=?, image_url=?, experience=?, rewards=?`;
return new Promise((resolve) => {
this.instance.db.query(query, [cellId, lat, lng, name, desc, img, exp, ""], (e, res) => {
let insertId = res.insertId;
obj.uid = insertId;
@ -120,4 +135,21 @@ export function insertFortIntoDatabase(obj) {
});
});
});
}
export function insertGymIntoDatabase(obj) {
let cellId = Cell.getIdByPosition(obj.latitude, obj.longitude, obj.zoom);
let lat = obj.latitude;
let lng = obj.longitude;
let query = `INSERT INTO ${Cell.getTable(obj.type)} SET cell_id=?, latitude=?, longitude=?, team=?, in_battle=?, points=?`;
return new Promise((resolve) => {
this.instance.db.query(query, [cellId, lat, lng, 0, 0, 0], (e, res) => {
let insertId = res.insertId;
obj.uid = insertId;
obj.cell_id = cellId;
this.addFort(obj).then((fort) => {
resolve(fort);
});
});
});
}