alclient
Version:
A node client for interacting with Adventure Land - The Code MMORPG. This package extends the functionality of 'alclient' by managing a mongo database.
171 lines • 6.78 kB
JavaScript
import { Constants } from "./Constants.js";
import { Character } from "./Character.js";
import { Tools } from "./Tools.js";
export class PingCompensatedCharacter extends Character {
constructor(userID, userAuth, characterID, g, serverData) {
super(userID, userAuth, characterID, g, serverData);
}
async connect() {
try {
await super.connect();
this.pingLoop();
}
catch (e) {
this.disconnect();
throw e;
}
}
setNextSkill(skill, next) {
super.setNextSkill(skill, new Date(next.getTime() - this.ping));
}
parseCharacter(data) {
super.parseCharacter(data);
// Get ping compensation
const pingCompensation = this.ping;
// Compensate movement
if (this.moving) {
const distanceTraveled = (this.speed * pingCompensation) / 1000;
const angle = Math.atan2(this.going_y - this.y, this.going_x - this.x);
const distanceToGoal = Tools.distance({ x: this.x, y: this.y }, { x: this.going_x, y: this.going_y });
if (distanceTraveled > distanceToGoal) {
this.moving = false;
this.x = this.going_x;
this.y = this.going_y;
}
else {
this.x = this.x + Math.cos(angle) * distanceTraveled;
this.y = this.y + Math.sin(angle) * distanceTraveled;
}
}
// Compensate conditions
for (const condition in this.s) {
if (this.s[condition].ms !== undefined) {
this.s[condition].ms -= pingCompensation;
if (this.s[condition].ms < 0) {
delete this.s[condition];
}
}
}
// Compensate processes
for (const process in this.q) {
if (this.q[process].ms !== undefined) {
this.q[process].ms -= pingCompensation;
if (this.q[process].ms < 0) {
delete this.q[process];
}
}
}
// Compensate channels
for (const channel in this.c) {
if (this.c[channel].ms !== undefined) {
this.c[channel].ms -= pingCompensation;
if (this.c[channel].ms < 0) {
delete this.c[channel];
}
}
}
}
parseEntities(data) {
super.parseEntities(data);
// Get ping compensation
const pingCompensation = this.ping;
for (const monster of data.monsters) {
// Compensate position
const entity = this.entities.get(monster.id);
if (!entity || !entity.moving)
continue;
const distanceTraveled = (entity.speed * pingCompensation) / 1000;
const angle = Math.atan2(entity.going_y - entity.y, entity.going_x - entity.x);
const distanceToGoal = Tools.distance({ x: entity.x, y: entity.y }, { x: entity.going_x, y: entity.going_y });
if (distanceTraveled >= distanceToGoal) {
entity.moving = false;
entity.x = entity.going_x;
entity.y = entity.going_y;
}
else {
entity.x = entity.x + Math.cos(angle) * distanceTraveled;
entity.y = entity.y + Math.sin(angle) * distanceTraveled;
}
// Compensate conditions
for (const condition in entity.s) {
if (entity.s[condition].ms === undefined)
continue;
entity.s[condition].ms -= pingCompensation;
if (entity.s[condition].ms < 0) {
delete entity.s[condition];
}
}
}
for (const player of data.players) {
// Compensate position
const entity = this.players.get(player.id);
if (!entity || !entity.moving)
continue;
const distanceTraveled = (entity.speed * pingCompensation) / 1000;
const angle = Math.atan2(entity.going_y - entity.y, entity.going_x - entity.x);
const distanceToGoal = Tools.distance({ x: entity.x, y: entity.y }, { x: entity.going_x, y: entity.going_y });
if (distanceTraveled >= distanceToGoal) {
entity.moving = false;
entity.x = entity.going_x;
entity.y = entity.going_y;
}
else {
entity.x = entity.x + Math.cos(angle) * distanceTraveled;
entity.y = entity.y + Math.sin(angle) * distanceTraveled;
}
// Compensate conditions
for (const condition in entity.s) {
if (entity.s[condition].ms === undefined)
continue;
entity.s[condition].ms -= pingCompensation;
if (entity.s[condition].ms < 0) {
delete entity.s[condition];
}
}
// Compensate processes
for (const process in entity.q) {
if (entity.q[process].ms !== undefined) {
entity.q[process].ms -= pingCompensation;
if (entity.q[process].ms < 0) {
delete entity.q[process];
}
}
}
// Compensate channels
for (const channel in entity.c) {
if (entity.c[channel].ms !== undefined) {
entity.c[channel].ms -= pingCompensation;
if (entity.c[channel].ms < 0) {
delete entity.c[channel];
}
}
}
}
}
parseQData(data) {
// Get ping compensation
const pingCompensation = this.ping;
// Update our `q` to match the new data
for (const process in data.q) {
const newCooldown = data.q[process].ms - pingCompensation;
if (newCooldown <= 0)
delete this.q[process];
else
this.q[process] = data.q[process];
}
}
pingLoop() {
if (!this.socket || this.socket.disconnected) {
this.timeouts.set("pingLoop", setTimeout(() => this.pingLoop(), 1000));
return;
}
this.sendPing(false).catch(console.error);
if (this.pings.length > Math.ceil(Constants.MAX_PINGS / 10)) {
this.timeouts.set("pingLoop", setTimeout(() => this.pingLoop(), Constants.PING_EVERY_MS));
}
else {
this.timeouts.set("pingLoop", setTimeout(() => this.pingLoop(), 1000));
}
}
}
//# sourceMappingURL=PingCompensatedCharacter.js.map