cla66ic/classes/Player.ts
2024-04-28 18:33:00 +03:00

87 lines
2 KiB
TypeScript

import { Position, Rotation, World } from "./classes.ts";
import { PacketDefinitions, PacketWriter } from "./Packets.ts";
import { config, log } from "../deps.ts";
import { Server } from "./Server.ts";
import {Socket} from 'bun';
export class Player {
socket: Socket<{dataBuffer?: Buffer}>;
private server: Server;
username: string;
ip: string;
id: number;
world = config.main;
position: Position;
rotation: Rotation = { yaw: 0, pitch: 0 };
constructor(
socket: Socket<{dataBuffer?: Buffer}>,
username: string,
position: Position,
server: Server,
) {
this.socket = socket;
this.username = username;
this.position = position;
this.server = server;
this.ip = this.socket.remoteAddress;
let id = Math.floor(Math.random() * server.maxUsers);
// reassigns ID until finds available one
while (server.players.find((e) => e.id == id)) {
id = Math.floor(Math.random() * server.maxUsers);
}
this.id = id;
}
async writeToSocket(ar: Uint8Array) {
try {
this.socket.write(ar)
} catch(e) {
log.critical(e);
await this.server.removeUser(
this.socket,
"Write failed" + e.message.split("\n")[0],
);
}
}
message(text: string, id = 0) {
text.replaceAll("%", "&").match(/.{1,64}/g)?.forEach(async (pic) => {
await this.writeToSocket(
new PacketWriter()
.writeByte(0x0d)
.writeSByte(id)
.writeString(pic)
.toPacket(),
);
});
}
toWorld(world: World) {
this.server.broadcastPacket(
(e) => PacketDefinitions.despawn(this.id, e),
this,
);
this.world = world.name;
PacketDefinitions.sendPackets(this, world);
this.server.broadcastPacket(
(e) => PacketDefinitions.spawn(this, this.id, e),
this,
);
this.server.broadcastPacket(
(e) => PacketDefinitions.spawn(e, e.id, this),
this,
);
this.message("You have been moved.");
}
}