Files
bworld/client/network.ts
T

54 lines
1.5 KiB
TypeScript

import { ClientMessage, EntityInfo, PlayerInfo, ServerMessage } from "$/common/protocol.ts";
import type { ModListing } from "$/common/mod_loader.ts";
import type { Join, ServerSocket, Welcome } from "./handshake.ts";
export class Connection {
#server: ServerSocket;
id: string;
name: string;
mods: ModListing[];
// the server's block id for each of its numeric ids, for reading chunks
server_blocks: string[];
spawn: { x: number; y: number; z: number; yaw: number; pitch: number };
selected_slot: number;
time: number;
// who was already there when we joined, they become entities in the level
initial_players: PlayerInfo[];
initial_entities: EntityInfo[];
constructor(server: ServerSocket, welcome: Welcome, join: Join) {
this.#server = server;
this.id = join.id;
this.name = join.name;
this.mods = welcome.mods;
this.server_blocks = join.blocks;
this.spawn = join.spawn;
this.selected_slot = join.selected_slot;
this.time = join.time;
this.initial_players = join.players;
this.initial_entities = join.entities;
}
// handled by the packet listener inside the game loop, not whenever the socket feels like it
get incoming(): ServerMessage[] {
return this.#server.messages;
}
get closed() {
return this.#server.closed;
}
send(message: ClientMessage) {
this.#server.send(message);
}
close() {
this.#server.close();
}
}
// what the name field starts as, from ?name=
export function default_player_name(): string {
return new URLSearchParams(location.search).get("name") ?? "";
}