POGOserver/src/cycle.js
Felix 31a230caf7 Update
- Refactored config
- Fixed mongodb bug, where player avatar didnt got saved into db
- Deleted requests.js, instead use protobuf
- Player parallelism should work now, dont share instanced player object
anymore *facepalm*
- Updated version
2016-08-19 12:19:48 +02:00

74 lines
1.4 KiB
JavaScript

import CFG from "../cfg";
export function startCycle() {
this.cycleInstance = setTimeout(() => this.cycle(), CFG.TICK_INTERVAL);
}
export function stopCycle() {
clearTimeout(this.cycleInstance);
}
export function cycle() {
this.stopCycle();
this.startCycle();
if (this.STATES.PAUSE === true) return void 0;
if (this.STATES.CRASH === true) return void 0;
this.updateTimers();
if (this.passedTicks <= 0) return void 0;
this.updatePlayers();
this.resetTimers();
return void 0;
}
export function updateTimers() {
let local = Date.now();
this.passedTicks = local - this.time;
this.tick += this.passedTicks;
this.time = local;
return void 0;
}
export function resetTimers() {
if (this.tick >= 25) {
this.fullTick++;
if (this.fullTick >= 2) {
this.fullTick = 0;
}
this.tick = 0;
// Player timeout tick, not precise
this.playerTimeoutTick();
}
this.saveTick++;
// Save interval
if (this.saveTick >= CFG.SAVE_INTERVAL) {
this.saveAllPlayers();
this.saveTick = 0;
}
return void 0;
}
export function playerTimeoutTick() {
let client = null;
let maxTimeout = CFG.PLAYER_CONNECTION_TIMEOUT;
let ii = 0, length = this.clients.length;
for (; ii < length; ++ii) {
client = this.clients[ii];
if (this.time - client.timeout >= maxTimeout) {
this.print(`${client.remoteAddress} timed out`, 34);
this.savePlayer(client);
this.removePlayer(client);
}
};
}