From 0e010b53fbaa243760f5aabd8ad5ed85c87243fd Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Tue, 19 Oct 2021 14:22:18 -0400 Subject: [Refactor] Bugfixes, updated table API --- scripts/cards/card.js | 15 ++++---- scripts/gui/table.js | 104 ++++++++++++++++++++++---------------------------- 2 files changed, 54 insertions(+), 65 deletions(-) (limited to 'scripts') diff --git a/scripts/cards/card.js b/scripts/cards/card.js index 47c7f17..9223f14 100644 --- a/scripts/cards/card.js +++ b/scripts/cards/card.js @@ -5,7 +5,7 @@ const CardPos = ["top", "topl", "topr", "mid", "midt", "midb", "bot", "botl", "b // Card class represents one card. // Every card should have a deck. -// Use deck.appendCard or deck.prependCard to make a card visible +// Use deck.appendCard, deck.prependCard, or deck.addCardAt to make a card visible class Card { constructor (data) { @@ -13,6 +13,7 @@ class Card { this.generateElements(data); this.e.style.left = "0px"; this.e.style.top = "0px"; + this.e.card = this; } // Generate a card with basic text only @@ -43,7 +44,7 @@ class Card { if (i == "style") { for (j in data.style) - area.style[j] = data.style[j]; + area.style.setProperty(j, data[i].style[j]); } if (data[i].type == "text") @@ -53,7 +54,7 @@ class Card { e.innerText = data[i].text; for (let j in data[i].style) - e.style[j] = data[i].style[j]; + e.style.setProperty(j, data[i].style[j]); area.appendChild(e); @@ -65,7 +66,7 @@ class Card { e.style.backgroundImage = "url(\"" + assetURL + data[i].image + "\")"; for (let j in data[i].style) - e.style[j] = data[i].style[j]; + e.style.setProperty(j, data[i].style[j]); area.appendChild(e); } @@ -78,10 +79,10 @@ class Card { static generateObjectCard (data, el) { // Generate card areas. - for (let i in CardPos) + for (let i of CardPos) { - if (typeof data[CardPos[i]] == "object") - el.appendChild(this.generateCArea(data[CardPos[i]], CardPos[i], data.assetURL)); + if (typeof data[i] == "object") + el.appendChild(this.generateCArea(data[i], i, data.assetURL)); } } diff --git a/scripts/gui/table.js b/scripts/gui/table.js index ab7fd3b..ecdefd7 100644 --- a/scripts/gui/table.js +++ b/scripts/gui/table.js @@ -14,6 +14,9 @@ class Table{ this.cards = {}; } + // GUI related functions + + // Make the table element visible openTable () { let state = this.root.getAttribute("state") @@ -23,6 +26,7 @@ class Table{ } } + // Make the table element not visible closeTable () { let state = this.root.getAttribute("state") @@ -32,27 +36,31 @@ class Table{ } } + // Handle a game closing (expectedly or unexpectedly) handleClose () { this.reset(); } + // Should reset all internal objects and delete all dangling decks and cards. reset () { while(this.root.firstElementChild != null) this.root.firstElementChild.remove(); this.decks = {}; + this.cards = {}; this.closeTable(); this.drag.stopDraggingAll(); } - /* Deck and card functions */ + /* Main API for server RPC */ - // {data object} contains deck id and options + // 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 + // {data.options object} options as found in Deck constructor newDeck(data) { var d = new Deck(data.id, data.options); @@ -60,6 +68,7 @@ class Table{ 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 @@ -72,90 +81,69 @@ class Table{ this.drag.addTarget(c.e); } - // {data object} data from the server - // {data.id any} card id to delete - deleteCard(data) + // Delete a deck + // {id any} id of deck to delete + deleteDeck(id) { - //this.cards[data.id].getDeck().removeCardByID - this.cards[data.id].e.remove(); - delete this.cards[data.id]; - } - - // {data object} data from the server - // {data.id any} deck id to delete - deleteDeck(data) - { - //for(let i of this.deck[data.id].cards) - //this.deck[] - } - - checkDeck(x, y) - { - for(let d of this.decks) + this.decks[id].e.remove(); + for(let i in this.decks[id].cards) { - if(d.isInside(x, y)) - return d; + delete this.cards[this.decks[id].cards[i].getID()]; + this.decks[id].removeCard(i); } - return null; - } - - checkCard (el) - { - for(let d of this.decks) - { - let c = d.checkCard(el); - if(c !== null) - return c; - } - return null; + //this.deck[] } - // {data object} data from the server - moveCard(card, newDeck, index = -1) + // Delete a card + // {id any} id of card to delete + deleteCard(id) { - for(let d of this.decks) - { - if (d.removeCardByID(card.getID()) !== null) - break; - } - card.resetPos(); - - if(index < 0) - newDeck.appendCard(card); - else - newDeck.addCardAt(card, index); + this.cards[id].getDeck().removeCardByID(id) + this.cards[id].e.remove(); + delete this.cards[id]; } + // Move a card from one deck to another // {data object} data from the server // {data.cardID any} ID of the card to move // {data.deckID any} ID of the deck to move the card to // {data.index number} card index in the new deck moveByID(data) { - let card, deck; + this.cards[data.cardID].getDeck().removeCardByID(data.cardID); + this.decks[data.deckID].addCardAt(this.cards[data.cardID], data.index); + } + + + /* Internal functions */ + + // Check if a position is within a deck + checkDeck(x, y) + { for(let d of this.decks) { - if (d.removeCardByID(cardID) !== null) - break; - let c = d.hasCard(cardID) - if(c !== null) - card = c; - - if(d.getID() == deckID) - deck = d; + if(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("game", {type: "move", card: cardID, deck: deckID, pos: index}); } + + // 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) -- cgit v1.2.3