summaryrefslogtreecommitdiff
path: root/scripts/gui/table.js
blob: c4878a085a7b4b972d6ad0cb5eb37bbee0a358b9 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Table represents and manages the actual game.  It accepts inputs from the server and tries to query the server when the player makes a move.
class Table{
    constructor(e, drag, socket) {
        this.root = e;
        this.drag = drag;

        this.root.addEventListener("mouseup", drag.stopDraggingAll.bind(drag));

        //drag.addEventListener("dragstop", );

        this.socket = socket;

        this.decks = [];
    }

    openTable ()
    {
        let state = this.root.getAttribute("state")
        if((state == "close" || state == "closed") && state != "") {
            this.root.setAttribute("state", "closed");
            setTimeout(this.root.setAttribute.bind(this.root), 50, "state", "open");
        }
    }

    closeTable ()
    {
        let state = this.root.getAttribute("state")
        if(state != "close" && state != "closed") {
            this.root.setAttribute("state", "");
            setTimeout(this.root.setAttribute.bind(this.root), 50, "state", "close");
        }
    }

    handleClose ()
    {
        this.reset();
    }

    reset ()
    {
        while(this.root.firstElementChild != null)
            this.root.firstElementChild.remove();
        
        this.decks = [];

        this.closeTable();
        this.drag.stopDraggingAll();
    }

    /*   Deck and card functions   */
    newDeck(options)
    {
        var d = new Deck(options);
        this.decks.push(d);
        this.root.appendChild(d.e);
    }

    newCard(data, deck = 0)
    {
        var c = new Card(data);
        this.decks[deck].appendCard(c);
        this.drag.addTarget(c.e);
    }

    checkDeck(x, y)
    {
        for(let d of this.decks)
        {
            if(d.isInside(x, y))
                return true;
        }
        return false;
    }

    dragCheck(cap)
    {
        console.log(cap);
    }
}