Bug fixes, added api

- Fixed bug, where player inventory didnt got sent, if the players party
is empty
- createUser db call resolve was missing
- Default player level is 1
- Added basic api
- Added cfg option to enable or disable api
- Added cfg option to greet or not
This commit is contained in:
Felix 2016-08-25 10:57:06 +02:00
parent 18c4e5937c
commit 2379c940ca
9 changed files with 110 additions and 29 deletions

View File

@ -4,6 +4,11 @@ export default {
VERSION: JSON.parse(fs.readFileSync("./package.json")).version,
// show greeting
GREET: true,
// emit api calls
ENABLE_API: false,
// Server settings
MAX_CONNECTIONS: 64,
PORT: 3000,
@ -12,11 +17,20 @@ export default {
LOCAL_IP: "",
GAME_MODE: 0,
SAVE_INTERVAL: 6e4,
// Better dont touch these
TICK_INTERVAL: 1,
// Timeouts
BOOT_TIMEOUT: 1e4,
PLAYER_CONNECTION_TIMEOUT: 1e3 * 60 * 30,
MINIMUM_CLIENT_VERSION: "0.35.0",
DEFAULT_CONSOLE_COLOR: 32,
TRANSFER_ACCOUNTS: false,
// Server debug options
DEBUG_DUMP_PATH: "logs/",
DEBUG_DUMP_TRAFFIC: true,
DEBUG_LOG_REQUESTS: true,
// Choose a database type
DATABASE_TYPE: "MYSQL",
@ -37,14 +51,6 @@ export default {
// Google maps api key
GMAPS_KEY: "AIzaSyDF9rkP8lhcddBtvH9gVFzjnNo13WtmJIM",
// Server debug options
DEBUG_DUMP_PATH: "logs/",
DEBUG_DUMP_TRAFFIC: true,
DEBUG_LOG_REQUESTS: true,
// Better dont touch these
TICK_INTERVAL: 1,
BOOT_TIMEOUT: 1e4,
// Currently supported pokemon
MAX_POKEMON_NATIONAL_ID: 151,
DUMP_ASSET_PATH: "data/"

44
src/api.js Normal file
View File

@ -0,0 +1,44 @@
import CFG from "../cfg";
export function ready(e) {
if (e) console.log(e);
else console.log("Ready!");
}
/**
* @param {String} msg
* @param {Number} color
* @param {Boolean} nl
*/
export function print(msg, color, newline) {
color = Number.isInteger(color) ? color : CFG.DEFAULT_CONSOLE_COLOR;
process.stdout.write(`[Console] \x1b[${color};1m${msg}\x1b[0m${newline === void 0 ? "\n" : ""}`);
}
/**
* @param {Player} player
*/
export function registerPlayer(player) {
console.log(player.username + " registered!");
}
/**
* @param {Player} player
*/
export function loginPlayer(player) {
console.log(player.username + " logged in!");
}
/**
* @param {Player} player
*/
export function killPlayer(player) {
console.log(player.username + " killed!");
}
/**
* @param {Player} player
*/
export function updatePlayerAvatar(player) {
console.log("Updated avatar of " + player.username + "!");
}

View File

@ -36,7 +36,7 @@ export function deleteQueryByColumnFromTable(column, value, table) {
export function getPkmnByColumn(column, value) {
return new Promise((resolve) => {
this.getQueryByColumnFromTable(column, value, CFG.MYSQL_OWNED_PKMN_TABLE).then((query) => {
resolve(query);
resolve(query || []);
});
});
}

View File

@ -54,7 +54,10 @@ export function createUser(obj) {
let data = this.getUserQueryData(obj);
return new Promise((resolve) => {
this.db.instance.query(query, data, resolve);
this.db.instance.query(query, data, (e) => {
if (e) this.print(e, 31);
else resolve();
});
});
}
@ -127,6 +130,8 @@ export function updateUserParty(player) {
let ii = 0;
let index = 0;
let length = player.party.length;
// dont go on if party empty
if (!length) return resolve();
for (; ii < length; ++ii) {
pkmn = player.party[ii];
data = this.getOwnedPkmnQueryData(pkmn);

View File

@ -3,6 +3,7 @@ import os from "os";
import fse from "fs-extra";
import http from "http";
import proto from "./proto";
import EventEmitter from "events";
import {
inherit,
@ -11,8 +12,7 @@ import {
import CFG from "../cfg";
import pogodown from "pogo-asset-downloader";
import * as _api from "./api";
import * as _setup from "./setup";
import * as _cycle from "./cycle";
import * as _player from "./player";
@ -29,11 +29,13 @@ const greetMessage = fs.readFileSync(".greet", "utf8");
/**
* @class GameServer
*/
class GameServer {
export default class GameServer extends EventEmitter {
/** @constructor */
constructor() {
super(null);
this.STATES = {
PAUSE: false,
DEBUG: false,
@ -61,7 +63,9 @@ class GameServer {
this.clients = [];
this.wild_pokemons = [];
this.greet();
this.initAPI();
if (CFG.GREET) this.greet();
this.print(`Booting Server v${require("../package.json").version}...`, 33);
@ -69,6 +73,18 @@ class GameServer {
}
initAPI() {
if (CFG.ENABLE_API) {
for (let key in _api) {
this.on(key, _api[key]);
};
}
// make sure we still have our print fn
else {
this.on("print", _api["print"]);
}
}
clientAlreadyConnected(client) {
let remoteAddress = client.headers.host;
@ -153,8 +169,7 @@ class GameServer {
* @param {Boolean} nl
*/
print(msg, color, nl) {
color = Number.isInteger(color) ? color : CFG.DEFAULT_CONSOLE_COLOR;
process.stdout.write(`[Console] \x1b[${color};1m${msg}\x1b[0m${nl === void 0 ? "\n" : ""}`);
this.emit("print", msg, color, nl);
}
/**
@ -284,12 +299,17 @@ inherit(GameServer, _mysql_get);
inherit(GameServer, _mysql_query);
inherit(GameServer, _mysql_create);
let server = new GameServer();
process.openStdin().addListener("data", function(data) {
server.stdinInput(data);
});
((Server) => {
process.on("uncaughtException", function(data) {
server.uncaughtException(data);
});
const server = new Server();
process.openStdin().addListener("data", (data) => {
server.stdinInput(data);
});
process.on("uncaughtException", (data) => {
server.uncaughtException(data);
});
})(GameServer);

View File

@ -38,8 +38,8 @@ export default function GetMapObjects(player, wild_pkmns, request) {
{
"id": "108dc9c703a94b619a53a3c29b5c676f.11",
"last_modified_timestamp_ms": "1471621873766",
"latitude": 39.188577,
"longitude": -96.583527,
"latitude": latitude + 0.0002,
"longitude": longitude - 0.0001,
"enabled": true,
"type": "CHECKPOINT"
},

View File

@ -28,7 +28,7 @@ class Player {
this.owner_id = -1;
this._email = null;
this.username = null;
this.username = "unknown";
this.latitude = 0;
this.longitude = 0;
@ -38,7 +38,7 @@ class Player {
this.send_push_notifications = false;
this.exp = 0;
this.level = 0;
this.level = 1;
this.stardust = 0;
this.pokecoins = 0;
@ -338,6 +338,7 @@ export function removePlayer(player) {
if (index >= 0) {
this.clients.splice(index, 1);
this.print(`${player.remoteAddress} disconnected!`, 36);
this.emit("killPlayer", player);
}
else {
this.print("Failed at removing player", 33);
@ -444,11 +445,13 @@ export function forwardPlayer(player) {
}
if (doc) {
this.loginPlayer(player).then((res) => {
this.emit("loginPlayer", player);
resolve(res);
});
}
else {
this.registerPlayer(player).then((res) => {
this.emit("registerPlayer", player);
resolve(res);
});
}
@ -467,7 +470,7 @@ export function loginPlayer(player) {
user = user[0];
this.getPkmnByColumn("owner_id", user.id).then((party) => {
player.updateByObject(user);
player.party = party || [];
player.party = party;
let buffer = GetPlayer(player);
resolve(buffer);
});
@ -484,8 +487,8 @@ export function registerPlayer(player) {
return new Promise((resolve) => {
this.createUser(player).then(() => {
this.print(`${player.email} registered!`, 36);
player.tutorial_state = [];
this.loginPlayer(player).then((res) => {
this.print("Registered and logged in!", 31);
resolve(res);
});
});

View File

@ -114,6 +114,7 @@ export function processResponse(player, req) {
break;
case "SET_AVATAR":
player.updateAvatar(msg);
this.emit("updatePlayerAvatar", player);
buffer = SetAvatar(player);
this.savePlayer(player).then(() => {
resolve(buffer);

View File

@ -41,11 +41,13 @@ export function setup() {
setTimeout(this::this.cycle, 1);
let localIPv4 = this.getLocalIPv4();
this.print(`Server running at ${localIPv4}:${CFG.PORT}`);
this.emit("ready", void 0);
});
}).catch((e) => {
//fse.removeSync(CFG.DUMP_ASSET_PATH);
this.print("Error: " + e + " was not found!", 31);
this.emit("ready", e);
});
}