Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
Update server.js
Browse files Browse the repository at this point in the history
  • Loading branch information
CookieCat45 committed Oct 3, 2023
1 parent a0860a9 commit 3460f76
Showing 1 changed file with 56 additions and 23 deletions.
79 changes: 56 additions & 23 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
const WSServer = require("ws").WebSocketServer;
const prompt = require("prompt-sync")();
const { Client, ClientPacket } = require("archipelago.js");
const { Client, ServerPacket } = require("archipelago.js");
const { EventEmitter } = require("ws");


let client= new Client();
let client = new Client();

async function main() {
let initialRoomInfo;
let initialConnected;
let pendingMessages = [];
let inventory;
let connected = false;
client.addListener("RoomInfo", packet => initialRoomInfo = packet);
client.addListener("Connected", packet => initialConnected = packet);
client.addListener("ReceivedItems", packet => {
if (packet.index == 0) {
inventory = packet;
}
else {
inventory.items.push(packet.items);
}
});

while (true) {
try {
Expand All @@ -36,7 +47,8 @@ async function main() {
});

break;
} catch {
}
catch {
console.error("Unable to connect... try again!\n");
}
}
Expand All @@ -45,41 +57,57 @@ async function main() {
const localServerPort = localServer.address().port;
console.log(`Connected to Archipelago server. Connect your client to 'localhost:${localServerPort}'`);

client.addListener("PacketReceived", (packet) => {
if (!connected) {
pendingMessages.push(packet);
}
});

client.addListener("PrintJSON", (packet, message) => {
let string = "";
if (packet.type === "Chat") {
string = `${client.players.alias(packet.slot)}: ${message}`;
}
else if (packet.type === "ServerChat") {
string = `[Server]: ${message}`;
}
else {
string = message;
}

console.log(string);
});

localServer.on("connection", (socket) => {
connected = true;
console.log("Client connected...\n");
socket.send(JSON.stringify([initialRoomInfo]));

client.removeListener("PacketReceieved", onPacket);
client.removeListener("PacketReceived", onPacket);
client.addListener("PacketReceived", onPacket);

function onPacket(packet) {
socket.send(JSON.stringify([packet]));
}

// client.removeListener doesn't work with PrintJSON for some reason, which leads to a memory leak + message spam
/*
function onPrint(packet, message)
{
let string = "";
if (packet.type === "Chat") {
string = `${client.players.alias(packet.slot)}: ${message}`;
} else if (packet.type === "ServerChat") {
string = `[Server]: ${message}`;
} else {
string = message;
}
console.log(string);
}
*/

socket.on("message", (message) => {
let packets = JSON.parse(message);

for (let i = 0; i < packets.length; i++) {
// game is connecting, will happen multiple times as the game does map transitions
if (packets[i].cmd == "Connect") {
socket.send(JSON.stringify([initialConnected]));
} else if (packets[i].cmd === "ConnectUpdate" && !packets.includes("Proxy")) {
if (pendingMessages.length > 0) {
socket.send(JSON.stringify(pendingMessages));
pendingMessages.length = 0;
}

if (inventory != undefined) {
// Items can get eaten if one gets sent right as the game does a map transition, so send entire inventory
socket.send(JSON.stringify([inventory]));
}
}
else if (packets[i].cmd === "ConnectUpdate" && !packets.includes("Proxy")) {
packets[i].tags.push("Proxy");
}
}
Expand All @@ -89,7 +117,12 @@ async function main() {
});

client.send(...packets);
})
});

socket.on("close", () => {
connected = false;
console.log("Client disconnected...\n");
});
});
}

Expand Down

0 comments on commit 3460f76

Please sign in to comment.