Fix error, make removeUser async, re-enable world saving

This commit is contained in:
yourfriend 2022-07-10 18:19:17 +03:00
parent 41a19c26a6
commit 5e3306276a
No known key found for this signature in database
GPG key ID: C28FFD8607DAC4DE
3 changed files with 19 additions and 10 deletions

View file

@ -37,9 +37,9 @@ export class Player {
}
async writeToSocket(ar: Uint8Array) {
await this.socket.write(ar).catch((e) => {
await this.socket.write(ar).catch(async (e) => {
log.critical(e);
this.server.removeUser(this.socket);
await this.server.removeUser(this.socket);
});
}
@ -75,7 +75,5 @@ export class Player {
);
this.message("You have been moved.");
//await world.save(); TODO: this causes way too many issues
}
}

View file

@ -136,7 +136,7 @@ export class Server {
}
}
}
removeUser(conn: Deno.Conn) {
async removeUser(conn: Deno.Conn) {
const player = this.players.find((e) => e.socket == conn);
if (!player) return;
@ -151,6 +151,8 @@ export class Server {
this.broadcast(`${player.username} has &cleft`);
await this.worlds.find(e => e.name == player.world)!.save();
this.broadcastPacket(
(e) => PacketDefinitions.despawn(player.id, e),
player,
@ -314,7 +316,7 @@ export class Server {
try {
packetIDReadAttempt = await connection.read(packetID);
} catch {
this.removeUser(connection); // TODO: add a reason to this
await this.removeUser(connection); // TODO: add a reason to this
break;
}
@ -323,7 +325,7 @@ export class Server {
if (!packetLength) {
log.critical("Unknown Packet: " + packetID[0]);
this.removeUser(connection); // TODO: add a reason to this
await this.removeUser(connection); // TODO: add a reason to this
break;
}
@ -333,21 +335,27 @@ export class Server {
try {
packetReadAttempt = await connection.read(rawPacket);
} catch {
this.removeUser(connection); // TODO: add a reason to this
await this.removeUser(connection); // TODO: add a reason to this
break;
}
let fullRead = packetReadAttempt!;
while (fullRead < packetLength) {
const halfPacket = new Uint8Array(packetLength - fullRead);
rawPacket = new Uint8Array([...rawPacket, ...halfPacket]);
fullRead += (await connection.read(halfPacket))!;
try {
fullRead += (await connection.read(halfPacket))!;
} catch {
await this.removeUser(connection); // TODO: add a reason to this
break;
}
}
this.handlePacket(rawPacket, packetID[0], connection);
} else {
this.removeUser(connection);
await this.removeUser(connection);
break;
}
}

View file

@ -26,9 +26,12 @@ export default class CommandPlugin extends Plugin {
});
this.on("command", async (command, player, args) => {
if (command == "g") {
await server.worlds.find(e => e.name == player.world)!.save();
const requestedWorld = server.worlds.find((e) =>
e.name.toLowerCase() == args.join(" ").toLowerCase()
);
if (requestedWorld) {
player.toWorld(requestedWorld);
} else {