"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PingCompensatedCharacter = void 0;
const Constants_1 = require("./Constants");
const Character_1 = require("./Character");
const Tools_1 = require("./Tools");
class PingCompensatedCharacter extends Character_1.Character {
    async connect() {
        const promise = super.connect();
        return promise.then(async () => { this.pingLoop(); });
    }
    setNextSkill(skill, next) {
        let pingCompensation = 0;
        if (this.pings.length > 0) {
            pingCompensation = Math.min(...this.pings);
        }
        this.nextSkill.set(skill, new Date(next.getTime() - pingCompensation));
    }
    async parseEntities(data) {
        super.parseEntities(data);
        const pingCompensation = Math.min(...this.pings) / 2;
        for (const monster of data.monsters) {
            const entity = this.entities.get(monster.id);
            if (!entity || !entity.moving)
                continue;
            const distanceTravelled = entity.speed * pingCompensation / 1000;
            const angle = Math.atan2(entity.going_y - entity.y, entity.going_x - entity.x);
            const distanceToGoal = Tools_1.Tools.distance({ x: entity.x, y: entity.y }, { x: entity.going_x, y: entity.going_y });
            if (distanceTravelled > distanceToGoal) {
                entity.moving = false;
                entity.x = entity.going_x;
                entity.y = entity.going_y;
            }
            else {
                entity.x = entity.x + Math.cos(angle) * distanceTravelled;
                entity.y = entity.y + Math.sin(angle) * distanceTravelled;
            }
            for (const condition in entity.s) {
                if (entity.s[condition].ms) {
                    entity.s[condition].ms -= pingCompensation;
                }
            }
        }
        for (const player of data.players) {
            const entity = this.players.get(player.id);
            if (!entity || !entity.moving)
                continue;
            const distanceTravelled = entity.speed * pingCompensation / 1000;
            const angle = Math.atan2(entity.going_y - entity.y, entity.going_x - entity.x);
            const distanceToGoal = Tools_1.Tools.distance({ x: entity.x, y: entity.y }, { x: entity.going_x, y: entity.going_y });
            if (distanceTravelled > distanceToGoal) {
                entity.moving = false;
                entity.x = entity.going_x;
                entity.y = entity.going_y;
            }
            else {
                entity.x = entity.x + Math.cos(angle) * distanceTravelled;
                entity.y = entity.y + Math.sin(angle) * distanceTravelled;
            }
            for (const condition in entity.s) {
                if (entity.s[condition].ms) {
                    entity.s[condition].ms -= pingCompensation;
                }
            }
        }
    }
    updateCharacter(data) {
        super.updateCharacter(data);
        const pingCompensation = Math.min(...this.pings) / 2;
        if (this.moving) {
            const distanceTravelled = this.speed * pingCompensation / 1000;
            const angle = Math.atan2(this.going_y - this.y, this.going_x - this.x);
            const distanceToGoal = Tools_1.Tools.distance({ x: this.x, y: this.y }, { x: this.going_x, y: this.going_y });
            if (distanceTravelled > distanceToGoal) {
                this.moving = false;
                this.x = this.going_x;
                this.y = this.going_y;
            }
            else {
                this.x = this.x + Math.cos(angle) * distanceTravelled;
                this.y = this.y + Math.sin(angle) * distanceTravelled;
            }
        }
        for (const condition in this.s) {
            if (this.s[condition].ms) {
                this.s[condition].ms -= pingCompensation;
            }
        }
    }
    pingLoop() {
        if (this.socket.disconnected)
            return;
        this.sendPing(false);
        if (this.pings.length > Constants_1.Constants.MAX_PINGS / 10) {
            this.timeouts.set("pingLoop", setTimeout(async () => { this.pingLoop(); }, Constants_1.Constants.PING_EVERY_MS));
        }
        else {
            this.timeouts.set("pingLoop", setTimeout(async () => { this.pingLoop(); }, 1000));
        }
    }
}
exports.PingCompensatedCharacter = PingCompensatedCharacter;
//# sourceMappingURL=PingCompensatedCharacter.js.map