class Bot {
constructor(sessionName = null, name = " ", sid = "", psk = "") {
if (!sid || !serverMap.get(sid)) return;
this.sessionName = sessionName;
this.name = name;
this.serverId = sid;
this.host = serverMap.get(sid).host;
this.hostname = serverMap.get(sid).hostname;
this.psk = psk;
this.ws = new WebSocket(wss://${this.host}:443/, {
headers: {
"Origin": "",
"User-Agent": ""
}
});
this.ws.binaryType = "arraybuffer";
this.codec = new BinCodec();
this.ws.onmessage = this.onMessage.bind(this);
this.ws.onclose = () => {
this.userId && sessions[this.userId] && (delete sessions[this.userId], delete sessions_1[this.userId], delete sessionsNames[this.userId], sendSessions());
}
this.ws.onerror = () => {};
this.entities = new Map();
this.buildings = {};
this.inventory = {};
this.partyInfo = [];
this.partyShareKey = psk;
this.dayCycle = {
cycleStartTick: 100,
nightEndTick: 0,
dayEndTick: 1300,
isDay: 1
};
this.leaderboard = [];
this.messages = [];
this.parties = {};
this.castSpellResponse = {};
this.buildingUids_1 = {};
this.uid = 0;
this.tick = 100;
this.type = "session";
this.hasVerified = false;
this.scripts = new Scripts();
this.petActivated = false;
this.gs = null;
this.rebuilder = new Map();
this.inactiveRebuilder = new Map();
this.reupgrader = new Map();
this.inactiveReupgrader = new Map();
this.nearestPlayer = null;
this.nearestPlayerDistance = Infinity;
this.playerTrickPsk = null;
}
encode(e) {
return new Uint8Array(codec.encode(9, {
name: "message",
msg: e
}));
}
decode(e) {
return codec.decode(new Uint8Array(e)).response.msg;
}
sendMessage(m) {
this.wss && this.wss.send(this.encode(m));
}
sendPacket(event, data) {
this.ws && this.ws.readyState == 1 && this.ws.send(new Uint8Array(this.codec.encode(event, data)));
}
sendData(data) {
sessions[this.userId] && Object.values(sessions[this.userId]).forEach(e => {
let ws = connections.get(e);
ws && ws.isVerified && ws.readyState == 1 && ws.send(encode(data, ;${JSON.stringify(data)}));
});
}
onMessage(msg) {
const opcode = new Uint8Array(msg.data)[0];
if (opcode == 5) {
try {
let e = Module.decodeOpcode5(new Uint8Array(msg.data), this.hostname);
if (new Uint8Array(e[5].extra)[0] == 0 && new Uint8Array(e[5].extra)[1] == 0 && new Uint8Array(e[5].extra)[2] == 0) {
new Bot(this.sessionName, this.name, this.serverId, this.psk);
} else {
this.sendPacket(4, {
displayName: this.name,
extra: e[5].extra
});
this.enterworld2 = e[6];
}
} catch (e) {
console.log(e);
}
return;
}
const data = this.codec.decode(msg.data);
switch (opcode) {
case 0:
this.onEntitiesUpdateHandler(data);
this.sendData({
tick: data.tick,
entities: Array.from(data.entities),
byteSize: data.byteSize,
opcode: 0
});
!this.hasVerified && (this.userId = counts++, sessions[this.userId] = {}, sessionsNames[this.userId] = {
sessionName: this.sessionName || "Session"
}, sessions_1[this.userId] = this, sendSessions(), this.hasVerified = true);
break;
case 4:
this.onEnterWorldHandler(data);
break;
case 9:
this.onRpcUpdateHandler(data);
this.sendData(data);
break;
}
}
onEntitiesUpdateHandler(data) {
this.tick = data.tick;
data.entities.forEach((entity, uid) => {
const entity_1 = this.entities.get(uid);
!entity_1 ? this.entities.set(uid, {
uid: uid,
targetTick: entity,
model: entity.model
}) : Object.keys(entity).forEach(e => entity_1.targetTick[e] = entity[e]);
})
this.nearestPlayer = null;
this.nearestPlayerDistance = Infinity;
this.entities.forEach((entity, uid) => {
if (!data.entities.has(uid)) {
this.entities.delete(uid);
return;
}
if (this.scripts.autoaim) {
if (entity.targetTick.model == "GamePlayer" && entity.targetTick.uid !== this.myPlayer.uid && entity.targetTick.partyId !== this.myPlayer.partyId && !entity.targetTick.dead) {
const distance = Math.hypot(entity.targetTick.position.x - this.myPlayer.position.x, entity.targetTick.position.y - this.myPlayer.position.y);
if (this.nearestPlayerDistance > distance) {
this.nearestPlayerDistance = distance;
this.nearestPlayer = entity.targetTick;
}
}
}
});
this.myPlayer = this.entities.get(this.uid) && this.entities.get(this.uid).targetTick;
this.myPet = this.myPlayer && this.entities.get(this.myPlayer.petUid) && this.entities.get(this.myPlayer.petUid).targetTick;
const userCount = !!Object.keys(sessions[this.userId] || {}).length;
if (!userCount && this.myPlayer) {
this.scripts.autorespawn && this.myPlayer.dead && this.sendPacket(3, {
respawn: 1
});
this.scripts.autoheal && (this.myPlayer.health / 5) <= this.scripts.healset && this.myPlayer.health > 0 && this.heal();
}
if ((!userCount || this.scripts.autopetheal) && this.myPet) {
this.scripts.autoheal && (this.myPet.health / this.myPet.maxHealth) * 100 <= this.scripts.pethealset && this.myPet.health > 0 && (this.buy("PetHealthPotion", 1), this.equip("PetHealthPotion", 1));
}
this.myPet && !this.petActivated && (this.petActivated = true);
!userCount && !this.inventory.HealthPotion && this.buy("HealthPotion", 1);
this.gs && this.scripts.autobuild && this.inactiveRebuilder.forEach(e => {
const x = e[0] * 24 + this.gs.x;
const y = e[1] * 24 + this.gs.y;
if (Math.abs(this.myPlayer.position.x - x) < 576 && Math.abs(this.myPlayer.position.y - y) < 576) {
this.sendPacket(9, {
name: "MakeBuilding",
x: x,
y: y,
type: e[2],
yaw: 0
});
}
})
this.gs && this.scripts.autoupgrade && this.inactiveReupgrader.forEach(e => {
const x = e[0] * 24 + this.gs.x;
const y = e[1] * 24 + this.gs.y;
if (Math.hypot((this.myPlayer.position.x - x), (this.myPlayer.position.y - y)) <= 768) {
if (e[5] - this.tick <= 0) {
e[5] = this.tick + 7;
this.sendPacket(9, {
name: "UpgradeBuilding",
uid: e[4]
});
}
}
})
this.scripts.autoaim && this.nearestPlayer && this.sendPacket(3, {
mouseMoved: ((Math.atan2(this.nearestPlayer.position.y - this.myPlayer.position.y, this.nearestPlayer.position.x - this.myPlayer.position.x) * 180 / Math.PI + 450) % 360) | 0
});
this.scripts.autobow && (this.sendPacket(3, {
space: 0
}), this.sendPacket(3, {
space: 1
}));
this.scripts.autopetrevive && this.petActivated && (this.sendPacket(9, {
name: "BuyItem",
itemName: "PetRevive",
tier: 1
}), this.sendPacket(9, {
name: "EquipItem",
itemName: "PetRevive",
tier: 1
}));
if (this.scripts.playertrick) {
const x = (this.tick * 50 / 1000 + 60) % 120 | 0;
if (x > 20 && x < 60) {
if (this.partyInfo.length != 1) {
this.sendPacket(9, {
name: "LeaveParty"
});
}
}
if (x > 117) {
if (this.partyInfo.length == 1) {
this.playerTrickPsk && this.sendPacket(9, {
name: "JoinPartyByShareKey",
partyShareKey: this.playerTrickPsk
});
}
}
}
}
onEnterWorldHandler(data) {
if (!data.allowed) return;
this.uid = data.uid;
this.enterworld2 && this.ws.send(this.enterworld2);
this.join(this.psk);
this.buy("HatHorns", 1);
this.buy("PetCARL", 1);
this.buy("PetMiner", 1);
this.equip("PetCARL", 1);
this.equip("PetMiner", 1);
for (let i = 0; i < 26; i++) this.ws.send(new Uint8Array([3, 17, 123, 34, 117, 112, 34, 58, 49, 44, 34, 100, 111, 119, 110, 34, 58, 48, 125]));
this.ws.send(new Uint8Array([7, 0]));
this.ws.send(new Uint8Array([9, 6, 0, 0, 0, 126, 8, 0, 0, 108, 27, 0, 0, 146, 23, 0, 0, 82, 23, 0, 0, 8, 91, 11, 0, 8, 91, 11, 0, 0, 0, 0, 0, 32, 78, 0, 0, 76, 79, 0, 0, 172, 38, 0, 0, 120, 155, 0, 0, 166, 39, 0, 0, 140, 35, 0, 0, 36, 44, 0, 0, 213, 37, 0, 0, 100, 0, 0, 0, 120, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 134, 6, 0, 0]));
console.log("bot in game");
}
onRpcUpdateHandler(data) {
switch (data.name) {
case "LocalBuilding":
data.response.forEach(e => {
if (this.buildingUids_1[e.uid]) return;
if (e.dead && !this.buildingUids_1[e.uid]) {
this.buildingUids_1[e.uid] = true;
setTimeout(() => {
delete this.buildingUids_1[e.uid];
}, 500);
}
if (e.type == "GoldStash") {
this.gs = e;
}
if (e.type == "GoldStash" && e.dead) {
if (this.scripts.autobuild) {
this.rebuilder.forEach(e => {
if (e[2] == "GoldStash") return;
this.inactiveRebuilder.set(e[0] + e[1] * 1000, e);
})
}
this.gs = null;
}
this.buildings[e.uid] = e;
e.dead && (delete this.buildings[e.uid]);
if (this.scripts.autobuild && this.gs && this.rebuilder.get((e.x - this.gs.x) / 24 + (e.y - this.gs.y) / 24 * 1000)) {
const index = (e.x - this.gs.x) / 24 + (e.y - this.gs.y) / 24 * 1000;
const _rebuilder = this.rebuilder.get(index);
e.dead ? this.inactiveRebuilder.set(index, _rebuilder) : this.inactiveRebuilder.delete(index);
}
if (this.scripts.autoupgrade && this.gs && this.reupgrader.get((e.x - this.gs.x) / 24 + (e.y - this.gs.y) / 24 * 1000)) {
const index = (e.x - this.gs.x) / 24 + (e.y - this.gs.y) / 24 * 1000;
const _reupgrader = this.reupgrader.get(index);
if (e.dead) {
this.inactiveReupgrader.delete(index);
} else {
if (e.tier < _reupgrader[2]) {
!this.inactiveReupgrader.get(index) && this.inactiveReupgrader.set(index, [_reupgrader[0], _reupgrader[1], _reupgrader[2], e.tier, e.uid, this.tick]);
} else {
this.inactiveReupgrader.delete(index);
}
}
}
})
break;
case "PartyShareKey":
this.partyShareKey = data.response.partyShareKey;
this.psk = data.response.partyShareKey;
break;
case "Dead":
this.buy("HatHorns", 1);
break;
case "SetItem":
this.inventory[data.response.itemName] = data.response;
if (this.inventory.HatHorns && !this.inventory.HatHorns.stacks) this.buy("HatHorns", 1);
if (!this.inventory[data.response.itemName].stacks) delete this.inventory[data.response.itemName];
if (data.response.itemName == "ZombieShield" && data.response.stacks) this.equip("ZombieShield", data.response.tier);
break;
case "PartyInfo":
this.partyInfo = data.response;
break;
case "SetPartyList":
this.parties = {};
data.response.forEach(e => {
this.parties[e.partyId] = e;
});
break;
case "DayCycle":
this.dayCycle = data.response;
break;
case "Leaderboard":
this.leaderboard = data.response;
break;
case "ReceiveChatMessage":
this.messages.push(data.response);
let _messages = [];
if (this.messages.length > 50) {
for (let i = this.messages.length - 50; i < this.messages.length; i++) {
_messages.push(this.messages[i]);
}
this.messages = _messages;
}
break;
case "CastSpellResponse":
this.castSpellResponse = data.response;
break;
}
}
getSyncNeeds() {
const syncNeeds = [];
syncNeeds.push({
allowed: 1,
uid: this.uid,
startingTick: this.tick,
tickRate: 20,
effectiveTickRate: 20,
players: 1,
maxPlayers: 32,
chatChannel: 0,
effectiveDisplayName: this.entities.get(this.uid) ? this.entities.get(this.uid).targetTick.name : this.name,
x1: 0,
y1: 0,
x2: 24000,
y2: 24000,
opcode: 4
});
syncNeeds.push({
name: 'PartyInfo',
response: this.partyInfo,
opcode: 9
});
syncNeeds.push({
name: 'PartyShareKey',
response: {
partyShareKey: this.partyShareKey
},
opcode: 9
});
syncNeeds.push({
name: 'DayCycle',
response: this.dayCycle,
opcode: 9
});
syncNeeds.push({
name: 'Leaderboard',
response: this.leaderboard,
opcode: 9
});
syncNeeds.push({
name: 'SetPartyList',
response: Object.values(this.parties),
opcode: 9
});
const localBuildings = Object.values(this.buildings);
const entities = [];
this.entities.forEach(e => {
entities.push([e.uid, e.targetTick]);
});
return {
tick: this.tick,
entities: entities,
byteSize: 654,
opcode: 0,
syncNeeds: syncNeeds,
localBuildings: localBuildings,
inventory: this.inventory,
messages: this.messages,
serverId: this.serverId,
useRequiredEquipment: true,
petActivated: this.petActivated,
castSpellResponse: this.castSpellResponse,
isPaused: this.myPlayer ? this.myPlayer.isPaused : 0
};
}
disconnect() {
this.ws.send([]);
}
heal() {
if (this.healTimeout_1) return;
this.equip("HealthPotion", 1);
this.buy("HealthPotion", 1);
this.healTimeout_1 = true;
setTimeout(() => {
this.healTimeout_1 = null
}, 500);
}
buy(item, tier) {
this.sendPacket(9, {
name: "BuyItem",
itemName: item,
tier: tier
});
}
equip(item, tier) {
this.sendPacket(9, {
name: "EquipItem",
itemName: item,
tier: tier
});
}
join(psk = "") {
this.sendPacket(9, {
name: "JoinPartyByShareKey",
partyShareKey: psk
});
}
videoalert() {}
}