소스 검색

finetune move deck and banlist check

Mistivia 1 개월 전
부모
커밋
b4a3aa4f23
1개의 변경된 파일78개의 추가작업 그리고 7개의 파일을 삭제
  1. 78 7
      src/deck.js

+ 78 - 7
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);