Commit 367ed2cb authored by timel's avatar timel

refactor: Move canAdd methods inside editDeckStore

parent 9b247064
......@@ -3,12 +3,18 @@ import { proxy } from "valtio";
import { type CardMeta } from "@/api";
import { compareCards, type EditingDeck, type Type } from "./utils";
import { isExtraDeckCard, isToken } from "@/common";
export const editDeckStore = proxy({
deckName: "",
main: [] as CardMeta[],
extra: [] as CardMeta[],
side: [] as CardMeta[],
// 标脏
edited: false,
// 方法
add(type: Type, card: CardMeta) {
editDeckStore[type].push(card);
editDeckStore[type].sort(compareCards);
......@@ -34,5 +40,37 @@ export const editDeckStore = proxy({
editDeckStore.side = [];
editDeckStore.edited = true;
},
edited: false,
/** 一张卡能不能放入某个区 */
canAdd(card: CardMeta, type: Type): { result: boolean; reason: string } {
let result = true,
reason = "";
const initialCards = editDeckStore[type];
// 如果是衍生物,则不能添加
if (isToken(card.data.type ?? 0)) {
result = false;
reason = "不能添加衍生物";
}
// 超出数量,则不能添加
const countLimit = type === "main" ? 60 : 15;
if (initialCards.length >= countLimit) {
result = false;
reason = `超过 ${countLimit} 张的上限`;
}
// 接着需要检查卡的种类
if (
(type === "extra" && !isExtraDeckCard(card.data.type ?? 0)) ||
(type === "main" && isExtraDeckCard(card.data.type ?? 0))
) {
result = false;
reason = "卡片种类不符合";
}
// 同名卡不超过三张
const maxSameCard = 3; // TODO: 禁卡表
const sameCardCount = initialCards.filter((c) => c.id === card.id).length;
if (sameCardCount >= maxSameCard) {
result = false;
reason = `超过同名卡 ${maxSameCard} 张的上限`;
}
return { result, reason };
},
}) satisfies EditingDeck;
......@@ -44,12 +44,7 @@ import { DeckSelect } from "./DeckSelect";
import { editDeckStore } from "./editDeckStore";
import { Filter } from "./Filter";
import styles from "./index.module.scss";
import {
canAdd,
editingDeckToIDeck,
iDeckToEditingDeck,
type Type,
} from "./utils";
import { editingDeckToIDeck, iDeckToEditingDeck, type Type } from "./utils";
const theme: ThemeConfig = {
components: {
......@@ -368,7 +363,7 @@ const DeckZone: React.FC<{
// 当拖拽物在这个拖放区域放下时触发,这个item就是拖拽物的item(拖拽物携带的数据)
drop: ({ value, source }: { value: CardMeta; source: Type | "search" }) => {
if (type === source) return;
const { result, reason } = canAdd(value, type, editDeckStore);
const { result, reason } = editDeckStore.canAdd(value, type);
if (result) {
editDeckStore.add(type, value);
if (source !== "search") {
......@@ -380,7 +375,7 @@ const DeckZone: React.FC<{
},
hover: ({ value, source }) => {
setAllowToDrop(
type !== source ? canAdd(value, type, editDeckStore).result : true
type !== source ? editDeckStore.canAdd(value, type).result : true
);
},
collect: (monitor) => ({
......@@ -416,7 +411,7 @@ const SearchResults: React.FC<{
}> = memo(({ results }) => {
const handleClick = (card: CardMeta) => {
const type = isExtraDeckCard(card.data.type ?? 0) ? "extra" : "main";
canAdd(card, type, editDeckStore).result && editDeckStore.add(type, card);
editDeckStore.canAdd(card, type).result && editDeckStore.add(type, card);
};
return (
<div className={styles["search-cards"]}>
......
......@@ -28,44 +28,6 @@ export const editingDeckToIDeck = (deck: EditingDeck): IDeck => ({
side: deck.side.map((card) => card.id),
});
/** 能不能添加到正在编辑的卡组的区域 */
export const canAdd = (
card: CardMeta,
type: Type,
editDeckStore: EditingDeck
): { result: boolean; reason?: string } => {
let result = true,
reason;
const initialCards = editDeckStore[type];
// 如果是衍生物,则不能添加
if (isToken(card.data.type ?? 0)) {
result = false;
reason = "不能添加衍生物";
}
// 超出数量,则不能添加
const countLimit = type === "main" ? 60 : 15;
if (initialCards.length >= countLimit) {
result = false;
reason = `超过 ${countLimit} 张的上限`;
}
// 接着需要检查卡的种类
if (
(type === "extra" && !isExtraDeckCard(card.data.type ?? 0)) ||
(type === "main" && isExtraDeckCard(card.data.type ?? 0))
) {
result = false;
reason = "卡片种类不符合";
}
// 同名卡不超过三张
const maxSameCard = 3; // TODO: 禁卡表
const sameCardCount = initialCards.filter((c) => c.id === card.id).length;
if (sameCardCount >= maxSameCard) {
result = false;
reason = `超过同名卡 ${maxSameCard} 张的上限`;
}
return { result, reason };
};
/** 卡组内部排序,给array.sort用 */
export const compareCards = (a: CardMeta, b: CardMeta): number => {
const aType = tellCardBasicType(a.data.type ?? 0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment