summaryrefslogtreecommitdiff
path: root/client-web/source/room.ts
blob: 53c020fc25b5f99ccec25fdbf8fdd9bc1f7487fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/// <reference lib="dom" />

import { log } from "./logger.ts";
import { RemoteUser } from "./user/remote.ts";
import { User } from "./user/mod.ts";
import { LocalUser } from "./user/local.ts";
import { ClientboundPacket, RelayMessage } from "../../common/packets.d.ts";
import { SignalingConnection } from "./protocol/mod.ts";
import { Chat } from "./chat.ts";

export class Room {
    public users: Map<number, User> = new Map()
    public remote_users: Map<number, RemoteUser> = new Map()
    public local_user!: LocalUser
    public my_id!: number
    public chat: Chat = new Chat(this)

    constructor(public signaling: SignalingConnection) {
        this.signaling.control_handler = (a) => this.control_handler(a)
        this.signaling.relay_handler = (a, b) => this.relay_handler(a, b)
    }

    control_handler(packet: ClientboundPacket) {
        if (packet.message) return // let the relay handler do that
        log("ws", `<- [control packet]: `, packet);
        if (packet.init) {
            this.my_id = packet.init.your_id
            // no need to check compat for now because this is hosted in the same place
            log("*", `server: ${packet.init.version}`)
        } else if (packet.client_join) {
            const p = packet.client_join
            log("*", `${p.id} joined`);
            if (p.id == this.my_id) {
                this.local_user = new LocalUser(this, p.id);
            } else {
                const ru = new RemoteUser(this, p.id)
                this.local_user.add_initial_to_remote(ru)
                this.local_user.identify(ru.id)
                ru.offer()
            }
        } else if (packet.client_leave) {
            const p = packet.client_leave;
            log("*", `${p.id} left`);
            this.users.get(p.id)!.leave()
        }
    }
    relay_handler(sender_id: number, message: RelayMessage) {
        const sender = this.users.get(sender_id)
        if (sender instanceof RemoteUser) {
            sender.on_relay(message)
        } else {
            console.warn("we received a message for ourselves, the server might be broken");
        }
    }
}