60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import { ClientMessage, EntityInfo, MenuSlotInfo, 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;
|
|
// the slots mods added to menu 0
|
|
player_menu: MenuSlotInfo[];
|
|
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.player_menu = join.player_menu;
|
|
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") ?? "";
|
|
}
|