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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
'use strict';
const TABLE_RPC = ["newDeck", "newCard", "deleteDeck", "deleteCard", "moveCard", "swapCard"]
// 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.drag.transform = this.getTransform.bind(this);
drag.addEventListener("dragstart", (e) => {console.log(e)});
drag.addEventListener("dragstop", this.dragMsg.bind(this));
this.socket = socket;
this.decks = {};
this.cards = {};
}
// GUI related functions
// Make the table element visible
openTable ()
{
let state = this.root.getAttribute("state")
if(state == "close" || state == "closed") {
this.root.setAttribute("state", "closed");
setTimeout(this.root.setAttribute.bind(this.root), 50, "state", "open");
}
}
// Make the table element not visible
closeTable ()
{
let state = this.root.getAttribute("state")
if((state != "close" && state != "closed") && state != "") {
this.root.setAttribute("state", "");
setTimeout(this.root.setAttribute.bind(this.root), 50, "state", "close");
}
}
toggleTable ()
{
let state = this.root.getAttribute("state")
if(state == "close" || state == "closed")
this.openTable();
else if (state == "open")
this.closeTable()
}
// Handle a game closing (expectedly or unexpectedly)
handleClose ()
{
this.reset();
}
// Should reset all internal objects and delete all dangling decks and cards.
reset ()
{
for (let e of this.root.children) {
if(e.tagName !== "DRAG") {
e.remove();
}
}
this.decks = {};
this.cards = {};
this.closeTable();
this.drag.stopDraggingAll();
}
/* Main API for server RPC */
// Create a new deck
// {data object} data from the server
// {data.id any} identifier for deck. Probably int or string.
// {data.options object} options as found in Deck constructor
newDeck(data)
{
let d = new Deck(data.id, data.options);
this.decks[data.id] = d;
this.root.appendChild(d.e);
}
// Create a new card
// {data object} contains data from server
// {data.id} card id
// {data.data} card data for visualization on the table
// {data.deck} the id of the deck to add the card to
newCard(data)
{
let c = new Card(data.id, data.deck, data.data);
this.cards[data.id] = c;
this.decks[data.deck].appendCard(c);
this.drag.addTarget(c.e);
}
// Delete a deck
// {id any} id of deck to delete
deleteDeck(id)
{
this.decks[id].e.remove();
for(let i in this.decks[id].cards)
{
delete this.cards[this.decks[id].cards[i].id];
this.decks[id].removeCard(i);
}
delete this.deck[id];
}
// Delete a card
// {id any} id of card to delete
deleteCard(id)
{
let c = this.cards[id];
this.decks[c.deck].removeCardByID(id)
c.e.remove();
delete this.cards[id];
}
// Move a card from one deck to another
// {data object} data from the server
// {data.card any} ID of the card to move
// {data.deck any} ID of the deck to move the card to
// {data.index number} card index in the new deck
moveCard(data)
{
let c = this.cards[data.card];
this.decks[c.deck].removeCardByID(data.card);
this.decks[data.deck].addCardAt(c, data.index);
c.resetPos();
c.e.className = "";
}
// Swap card data with new data
// {data object} data from the server
// {data.card any} ID of the card to swap
// {data.id any} New ID for the card
// {data.data object} visualization data
swapCard(data)
{
// Can't swap a card into a an id of a pre-existing card.
if (this.cards[data.id] != null) {
return false;
}
this.cards[data.id] = this.cards[data.card];
delete this.cards[data.card];
this.cards[data.id].generateElements(data.data);
}
/* Internal functions */
// Check if a position is within a deck
checkDeck(x, y)
{
for(let d of Object.keys(this.decks))
{
if(this.decks[d].isInside(x, y))
return d;
}
return null;
}
// Function to query the server about a player's move
checkMove(cardID, deckID, index = -1)
{
this.socket.send("move", {card: cardID, deck: deckID, index: index});
}
getTransform(x, y)
{
let rot = 0, scale = 0, dtot = 0;
for(let d in this.decks ) {
let r = parseFloat(this.decks[d].e.style.getPropertyValue("--rot"));
let s = parseFloat(this.decks[d].e.style.getPropertyValue("--scale"));
//
//TODO: Some code to properly weight the importance of scale and rotation
// Hopefully this can be done in one pass
let f = 1/this.decks[d].dist(x, y);
let tot = dtot + f;
rot = rot*(dtot/tot) + r*(f/tot);
scale = scale*(dtot/tot) + s*(f/tot);
dtot += tot;
}
return [rot, scale];
}
// DRAG DEBUGGING
// Generic check function for drag debugging
dragCheck(cap)
{
console.log(cap);
}
// More debugging for the drag class
dragMsg (event)
{
if(event.drag.length < 1)
return;
let c = event.drag[0];
let d = this.checkDeck(event.x, event.y);
if(c.e.card !== null)
{
let id = c.e.card.id;
if(d !== null && !this.decks[d].hasCard(id))
this.checkMove(id, d);
else {
c.ep.appendChild(c.e)
c.e.card.resetPos();
}
}
}
}
|