From ff9039f2457cc9ddd9f2fae1e9ac3367f8376bf6 Mon Sep 17 00:00:00 2001 From: Andrio Celos Date: Wed, 5 Oct 2022 16:00:08 +1100 Subject: [PATCH] Save the last deck, and select the starter deck by default --- TableturfBattleClient/src/Pages/LobbyPage.ts | 28 ++++++++++++++++++++ TableturfBattleClient/src/app.ts | 23 ---------------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/TableturfBattleClient/src/Pages/LobbyPage.ts b/TableturfBattleClient/src/Pages/LobbyPage.ts index cfc87f7..d631046 100644 --- a/TableturfBattleClient/src/Pages/LobbyPage.ts +++ b/TableturfBattleClient/src/Pages/LobbyPage.ts @@ -40,6 +40,16 @@ function updatePlayerListItem(playerIndex: number) { listItem.innerText = "Waiting..."; } +function updateDeckCount() { + var count = 0; + for (var el of cardButtons) { + if (el.inputElement.checked) + count++; + } + document.getElementById('countLabel')!.innerText = count.toString(); + submitDeckButton.disabled = (count != 15); +} + submitDeckButton.addEventListener('click', e => { let req = new XMLHttpRequest(); req.open('POST', `${config.apiBaseUrl}/games/${currentGame!.id}/chooseDeck`); @@ -60,4 +70,22 @@ submitDeckButton.addEventListener('click', e => { data.append('deckName', 'Deck'); data.append('deckCards', cardsString); req.send(data.toString()); + localStorage.setItem('lastDeck', cardsString); }); + +const starterDeck = [ 6, 34, 159, 13, 45, 137, 22, 52, 141, 28, 55, 103, 40, 56, 92 ]; +const lastDeckString = localStorage.getItem('lastDeck'); +const lastDeck = lastDeckString?.split(/\+/)?.map(s => parseInt(s)) || starterDeck; + +cardDatabase.loadAsync().then(cards => { + const cardList = document.getElementById('cardList')!; + for (const card of cards) { + const button = new CardButton('checkbox', card); + cardButtons.push(button); + button.inputElement.checked = lastDeck != null && lastDeck.includes(card.number); + button.inputElement.addEventListener('input', updateDeckCount); + cardList.appendChild(button.element); + } + updateDeckCount(); + document.getElementById('cardListLoadingSection')!.hidden = true; +}).catch(e => document.getElementById('errorModal')!.hidden = false); diff --git a/TableturfBattleClient/src/app.ts b/TableturfBattleClient/src/app.ts index cddd256..7be67c0 100644 --- a/TableturfBattleClient/src/app.ts +++ b/TableturfBattleClient/src/app.ts @@ -36,10 +36,6 @@ function onGameStateChange(game: any, playerData: any) { break; case GameState.Preparing: showSection('deck'); - for (const button of cardButtons.slice(0, 15)) { - button.inputElement.checked = true; - } - submitDeckButton.disabled = false; break; case GameState.Redraw: case GameState.Ongoing: @@ -75,25 +71,6 @@ function onGameStateChange(game: any, playerData: any) { showSection('preGame'); -cardDatabase.loadAsync().then(cards => { - const cardList = document.getElementById('cardList')!; - for (var card of cards) { - const button = new CardButton('checkbox', card); - cardButtons.push(button); - button.inputElement.addEventListener('input', e => { - var count = 0; - for (var el of cardButtons) { - if (el.inputElement.checked) - count++; - } - document.getElementById('countLabel')!.innerText = count.toString(); - submitDeckButton.disabled = (count != 15); - }); - cardList.appendChild(button.element); - } - document.getElementById('cardListLoadingSection')!.hidden = true; -}).catch(e => document.getElementById('errorModal')!.hidden = false); - function isInternetExplorer() { return !!(window.document as any).documentMode; // This is a non-standard property implemented only by Internet Explorer. }