From b4a3aa4f23ecaa6f0f11fa0c572c17a28f9569ac Mon Sep 17 00:00:00 2001
From: Mistivia <i@mistivia.com>
Date: Sat, 22 Feb 2025 02:11:35 +0800
Subject: finetune move deck and banlist check

---
 src/deck.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 78 insertions(+), 7 deletions(-)

diff --git a/src/deck.js b/src/deck.js
index 593e0ef..6147c77 100644
--- a/src/deck.js
+++ b/src/deck.js
@@ -32,11 +32,41 @@ function sanitizeDeck(cardCnt, deck) {
     return ret;
 }
 
+function groupAndSort(arr) {
+    const seen = {};
+    const count = {};
+    const uniqueList = [];
+    
+    for (const num of arr) {
+        if (!(num in seen)) {
+            seen[num] = true;
+            uniqueList.push(num);
+        }
+        count[num] = (count[num] || 0) + 1;
+    }
+    
+    const result = [];
+    for (const num of uniqueList) {
+        const times = count[num];
+        for (let i = 0; i < times; i++) {
+            result.push(num);
+        }
+    }
+    return result;
+}
+
 function setDeck(d) {
     let cardCnt = new Map();
+
     d.main = sanitizeDeck(cardCnt, d.main);
+    d.main = groupAndSort(d.main);
+
     d.side = sanitizeDeck(cardCnt, d.side);
+    d.side= groupAndSort(d.side);
+
     d.extra = sanitizeDeck(cardCnt, d.extra);
+    d.extra= groupAndSort(d.extra);
+
     deckState = d;
     deck.set(d);
     localStorage.setItem('cachedDeck', JSON.stringify(d));
@@ -53,7 +83,7 @@ function canAdd(d, id) {
     for (let c of d.extra) {
         if (c === id) count += 1;
     }
-    if (count + 1 > 3) return false;
+    if (count + 1 > cardLimit(id, formatState)) return false;
     return true;
 }
 
@@ -75,9 +105,41 @@ let deckOps = {
         let id = deckState[from][fromIdx];
         if (cardDb[id].isExtra && to === 'main') return;
         if (!cardDb[id].isExtra && to === 'extra') return;
-        deckState[from].splice(fromIdx, 1);
-        if (toIdx === -1) deckState[to].push(id);
-        else deckState[to].splice(toIdx, 0, id);
+        if (from === to) {
+            let count = 0;
+            for (let c of deckState[from]) {
+                if (c === id) count += 1;
+            }
+            deckState[from] = deckState[from].map((x) => x === id ? null : x);
+            if (to === 'main') {
+                for (let i = 0; i < count; i++) {
+                    deckOps.add2main(id, toIdx);
+                }
+            } else if (to === 'extra') {
+                for (let i = 0; i < count; i++) {
+                    deckOps.add2extra(id, toIdx);
+                }
+            } else if (to === 'side') {
+                for (let i = 0; i < count; i++) {
+                    deckOps.add2side(id, toIdx);
+                }
+            }
+            deckState[from] = deckState[from].filter((x) => x !== null);
+        } else {
+            if (to === 'extra') {
+                deckState[from][fromIdx] = null;
+                deckOps.add2extra(id, toIdx);
+                deckState[from] = deckState[from].filter((x) => x !== null);
+            } else if (to === 'main') {
+                deckState[from][fromIdx] = null;
+                deckOps.add2main(id, toIdx);
+                deckState[from] = deckState[from].filter((x) => x !== null);
+            } else if (to === 'side') {
+                deckState[from][fromIdx] = null;
+                deckOps.add2side(id, toIdx);
+                deckState[from] = deckState[from].filter((x) => x !== null);
+            }
+        }
         setDeck(deckState);
     },
     "add2extra": (id, targetIdx) => {
@@ -87,7 +149,10 @@ let deckOps = {
         if (canAdd(d, id)) {
             if (targetIdx === -1) 
                 d.extra.push(id);
-            else {
+            else if (d.extra.includes(id)) {
+                d.extra.push(id);
+                d.extra = groupAndSort(d.extra);
+            } else {
                 d.extra.splice(targetIdx, 0, id);
             }
             setDeck(d);
@@ -100,7 +165,10 @@ let deckOps = {
         if (canAdd(d, id)) {
             if (targetIdx === -1) 
                 d.main.push(id);
-            else {
+            else if (d.main.includes(id)) {
+                d.main.push(id);
+                d.main= groupAndSort(d.main);
+            } else {
                 d.main.splice(targetIdx, 0, id);
             }
             setDeck(d);
@@ -111,7 +179,10 @@ let deckOps = {
         if (canAdd(d, id)) {
             if (targetIdx === -1) 
                 d.side.push(id);
-            else {
+            else if (d.side.includes(id)) {
+                d.side.push(id);
+                d.side= groupAndSort(d.side);
+            } else {
                 d.side.splice(targetIdx, 0, id);
             }
             setDeck(d);
-- 
cgit v1.0

