summaryrefslogtreecommitdiff
path: root/client-web/source/user/mod.ts
blob: 59c58b7b2cb8f19a978ce0a38462515b3064156b (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
/// <reference lib="dom" />

import { epre, espan } from "../helper.ts";
import { ROOM_CONTAINER } from "../index.ts";
import { Resource } from "../resource/mod.ts";
import { Room } from "../room.ts"

export abstract class User {
    protected el: HTMLElement
    public local = false
    public resources: Map<string, Resource> = new Map()

    private name_el = espan("")
    protected stats_el = epre("", { class: "stats" })
    private _name?: string
    get name() { return this._name }
    set name(n: string | undefined) { this._name = n; this.name_el.textContent = this.display_name }
    get display_name() { return this.name ?? `unknown (${this.id})` }

    constructor(public room: Room, public id: number) {
        room.users.set(this.id, this)

        this.el = document.createElement("div")
        this.el.classList.add("user")
        ROOM_CONTAINER.append(this.el)
        this.setup_view()
    }
    leave() {
        this.room.users.delete(this.id)
    }

    setup_view() {
        const info_el = document.createElement("div")
        info_el.classList.add("info")
        this.name_el.textContent = this.display_name
        this.name_el.classList.add("name")
        info_el.append(this.name_el, this.stats_el)
        this.el.append(info_el)
    }
}