From 811571e23b68f4318794de3d34aa15cb91ec3d2d Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Tue, 19 Oct 2021 00:18:37 -0400 Subject: [Refactor] Lots of little patches + Make chatbox code a little cleaner + Add a bit of documentation + Begin prep for passwords + Function to easily grab decks from cards ~ I am going to have to completely re-do cards and decks probably. --- scripts/cards/card.js | 10 +++++----- scripts/cards/deck.js | 6 ++---- scripts/gui/chat.js | 34 ++++++++++++++------------------ scripts/gui/lobby.js | 16 ++++++++++++++- scripts/gui/table.js | 54 ++++++++++++++++++++++++++++++++++++++++----------- 5 files changed, 80 insertions(+), 40 deletions(-) (limited to 'scripts') diff --git a/scripts/cards/card.js b/scripts/cards/card.js index 43dc965..47c7f17 100644 --- a/scripts/cards/card.js +++ b/scripts/cards/card.js @@ -7,16 +7,12 @@ const CardPos = ["top", "topl", "topr", "mid", "midt", "midb", "bot", "botl", "b // Every card should have a deck. // Use deck.appendCard or deck.prependCard to make a card visible class Card { - constructor (id, data) + constructor (data) { this.e = document.createElement("card"); this.generateElements(data); this.e.style.left = "0px"; this.e.style.top = "0px"; - - this.getID = function() { - return id; - } } // Generate a card with basic text only @@ -117,4 +113,8 @@ class Card { this.e.style.left = "0px"; this.e.style.top = "0px"; } + + getDeck() { + return this.e.parentElement.deck; + } } diff --git a/scripts/cards/deck.js b/scripts/cards/deck.js index caa62bc..27020f0 100644 --- a/scripts/cards/deck.js +++ b/scripts/cards/deck.js @@ -13,7 +13,7 @@ class Deck { y = 0; e = null; - constructor(id, options = {mode: "stack", smode: "one", sct: 0, pos: [0, 0]}) + constructor(options = {mode: "stack", smode: "one", sct: 0, pos: [0, 0]}) { // View mode // infdraw - infinite draw. always appears as if there are multiple cards @@ -53,9 +53,7 @@ class Deck { this.e.setAttribute("mode", options.mode); - this.getID = function() { - return id; - } + this.e.deck = this; } updatePos() diff --git a/scripts/gui/chat.js b/scripts/gui/chat.js index 4bd6933..428403a 100644 --- a/scripts/gui/chat.js +++ b/scripts/gui/chat.js @@ -4,6 +4,7 @@ class Chat { constructor(e, soc) { this.chats = []; + this.active = null; this.root = e; this.socket = soc; e.getElementsByClassName("toggle-chat")[0].onclick = this.toggle.bind(this); @@ -29,15 +30,8 @@ class Chat { isActive (name) { - for(let i in this.chats) - { - if (this.chats[i].name == name) - { - if(this.chats[i].btn.getAttribute("active") == "true") - return true; - return false; - } - } + if(this.active !== null) + return this.active.name === name; return false; } @@ -68,20 +62,13 @@ class Chat { getActiveChannel () { - for(let i in this.chats) - { - if (this.chats[i].btn.getAttribute("active") == "true") - { - return this.chats[i]; - } - } - - return null; + return this.active; } switchChannel (name) { let c = this.getChannel(name); + this.active = c; if(c == null) return; @@ -111,7 +98,7 @@ class Chat { if(str == "") return; this.chatInput.value = ""; - this.socket.send("chat", str); + this.socket.send("chat", {str}); } recieveMessage (channel, msg) @@ -163,6 +150,15 @@ class Chat { if(c == null) return; + let id = this.chats.indexOf(c); + if(this.isActive(name)) { + if(id == 0 && this.chats.length > 1) + this.switchChannel(this.chats[1].name); + else + this.switchChannel(this.chats[id - 1].name); + } + + while(c.e.firstElementChild != null) c.e.firstElementChild.remove(); diff --git a/scripts/gui/lobby.js b/scripts/gui/lobby.js index 8242843..4907f41 100644 --- a/scripts/gui/lobby.js +++ b/scripts/gui/lobby.js @@ -51,7 +51,7 @@ class TopBar{ // Game represents a single game in the lobby view. It has methods for setting up the elements and such. class Game { - constructor(options = {id: 0, name: ""}, el) + constructor(options = {id: 0, name: "", password: false}, el) { this.getID = function () { return options.id; @@ -74,6 +74,20 @@ class Game { join.addEventListener("click", game.joinGame.bind(game, options.id)); e.appendChild(join); + this.hasPassword = function () { + return options.password; + } + + if(usePass) { + let pass = MakeInput.textInput("", "Game password"); + pass.classList.add("pass"); + e.appendChild(pass); + + this.getUserPass = function () { + return pass.value; + } + } + el.appendChild(e); } } diff --git a/scripts/gui/table.js b/scripts/gui/table.js index e242ac6..ab7fd3b 100644 --- a/scripts/gui/table.js +++ b/scripts/gui/table.js @@ -10,7 +10,8 @@ class Table{ this.socket = socket; - this.decks = []; + this.decks = {}; + this.cards = {}; } openTable () @@ -41,27 +42,53 @@ class Table{ while(this.root.firstElementChild != null) this.root.firstElementChild.remove(); - this.decks = []; + this.decks = {}; this.closeTable(); this.drag.stopDraggingAll(); } /* Deck and card functions */ - newDeck(id, options) + + // {data object} contains deck id and options + // {data.id any} identifier for deck. Probably int or string. + // {data.options object} Options as found in Deck constructor + newDeck(data) { - var d = new Deck(id, options); - this.decks.push(d); + var d = new Deck(data.id, data.options); + this.decks[data.id] = d; this.root.appendChild(d.e); } - newCard(id, data, deck = 0) + // {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) { - var c = new Card(id, data); - this.decks[deck].appendCard(c); + var c = new Card(data.id, data.data); + this.cards[data.id] = c; + this.decks[data.deck].appendCard(c); this.drag.addTarget(c.e); } + // {data object} data from the server + // {data.id any} card id to delete + deleteCard(data) + { + //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) @@ -83,6 +110,7 @@ class Table{ return null; } + // {data object} data from the server moveCard(card, newDeck, index = -1) { for(let d of this.decks) @@ -98,11 +126,17 @@ class Table{ newDeck.addCardAt(card, index); } - moveByID(cardID, deckID, index = -1) + // {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; for(let d of this.decks) { + if (d.removeCardByID(cardID) !== null) + break; let c = d.hasCard(cardID) if(c !== null) card = c; @@ -110,8 +144,6 @@ class Table{ if(d.getID() == deckID) deck = d; } - - this.moveCard(card, deck, index); } checkMove(cardID, deckID, index = -1) -- cgit v1.2.3