Commit 2c415f42 authored by timel's avatar timel

feat: valtio store logic 40%

parent ef0484d8
...@@ -6,6 +6,10 @@ import { ...@@ -6,6 +6,10 @@ import {
addMonsterPlaceInteractivities, addMonsterPlaceInteractivities,
} from "@/reducers/duel/mod"; } from "@/reducers/duel/mod";
import { InteractType, valtioStore } from "@/valtioStores";
const { matStore } = valtioStore;
export default (selectPlace: MsgSelectPlace, dispatch: AppDispatch) => { export default (selectPlace: MsgSelectPlace, dispatch: AppDispatch) => {
if (selectPlace.count != 1) { if (selectPlace.count != 1) {
console.warn(`Unhandled case: ${selectPlace}`); console.warn(`Unhandled case: ${selectPlace}`);
...@@ -19,14 +23,22 @@ export default (selectPlace: MsgSelectPlace, dispatch: AppDispatch) => { ...@@ -19,14 +23,22 @@ export default (selectPlace: MsgSelectPlace, dispatch: AppDispatch) => {
dispatch( dispatch(
addMonsterPlaceInteractivities([place.controler, place.sequence]) addMonsterPlaceInteractivities([place.controler, place.sequence])
); );
matStore.monsters.setPlaceInteractivityType(
place.controler,
place.sequence,
InteractType.PLACE_SELECTABLE
);
break; break;
} }
case ygopro.CardZone.SZONE: { case ygopro.CardZone.SZONE: {
dispatch( dispatch(
addMagicPlaceInteractivities([place.controler, place.sequence]) addMagicPlaceInteractivities([place.controler, place.sequence])
); );
matStore.magics.setPlaceInteractivityType(
place.controler,
place.sequence,
InteractType.PLACE_SELECTABLE
);
break; break;
} }
default: { default: {
......
...@@ -4,7 +4,10 @@ import { ...@@ -4,7 +4,10 @@ import {
setPositionModalPositions, setPositionModalPositions,
} from "@/reducers/duel/mod"; } from "@/reducers/duel/mod";
import { AppDispatch } from "@/store"; import { AppDispatch } from "@/store";
import MsgSelectPosition = ygopro.StocGameMessage.MsgSelectPosition;
import { matStore } from "@/valtioStores";
type MsgSelectPosition = ygopro.StocGameMessage.MsgSelectPosition;
export default (selectPosition: MsgSelectPosition, dispatch: AppDispatch) => { export default (selectPosition: MsgSelectPosition, dispatch: AppDispatch) => {
const player = selectPosition.player; const player = selectPosition.player;
...@@ -13,5 +16,6 @@ export default (selectPosition: MsgSelectPosition, dispatch: AppDispatch) => { ...@@ -13,5 +16,6 @@ export default (selectPosition: MsgSelectPosition, dispatch: AppDispatch) => {
dispatch( dispatch(
setPositionModalPositions(positions.map((position) => position.position)) setPositionModalPositions(positions.map((position) => position.position))
); );
dispatch(setPositionModalIsOpen(true)); dispatch(setPositionModalIsOpen(true));
}; };
...@@ -53,6 +53,8 @@ export default ( ...@@ -53,6 +53,8 @@ export default (
}, },
]) ])
); );
// >>> 删除 >>>
dispatch(initMonsters(0)); dispatch(initMonsters(0));
dispatch(initMonsters(1)); dispatch(initMonsters(1));
dispatch(initMagics(0)); dispatch(initMagics(0));
...@@ -63,10 +65,18 @@ export default ( ...@@ -63,10 +65,18 @@ export default (
dispatch(initDeck({ player: 0, deskSize: start.deckSize1 })); dispatch(initDeck({ player: 0, deskSize: start.deckSize1 }));
dispatch(initDeck({ player: 1, deskSize: start.deckSize2 })); dispatch(initDeck({ player: 1, deskSize: start.deckSize2 }));
matStore.decks.add(0, Array(start.deckSize1).fill(0));
matStore.decks.add(1, Array(start.deckSize2).fill(0));
dispatch(initBanishedZone(0)); dispatch(initBanishedZone(0));
dispatch(initBanishedZone(1)); dispatch(initBanishedZone(1));
dispatch(initHint());
// <<< 删除 <<<
// 上面的删除就可以了
matStore.monsters.at(0).forEach((x) => (x.location.controler = 0));
matStore.monsters.at(1).forEach((x) => (x.location.controler = 1));
matStore.magics.at(0).forEach((x) => (x.location.controler = 0));
matStore.magics.at(1).forEach((x) => (x.location.controler = 1));
matStore.decks.add(0, Array(start.deckSize1).fill(0));
matStore.decks.add(1, Array(start.deckSize2).fill(0));
dispatch(initHint()); // 直接删除
}; };
export * from "./types";
import { proxy } from "valtio"; import { proxy } from "valtio";
import { fetchCard } from "@/api/cards"; import { fetchCard } from "@/api/cards";
...@@ -11,6 +13,7 @@ import type { ...@@ -11,6 +13,7 @@ import type {
InitInfo, InitInfo,
PlayMatState, PlayMatState,
} from "./types"; } from "./types";
import { InteractType } from "./types";
import { DESCRIPTION_LIMIT, fetchStrings, getStrings } from "@/api/strings"; import { DESCRIPTION_LIMIT, fetchStrings, getStrings } from "@/api/strings";
/** /**
...@@ -129,6 +132,9 @@ const wrap = <T extends DuelFieldState>( ...@@ -129,6 +132,9 @@ const wrap = <T extends DuelFieldState>(
const res: CardsBothSide<T> = proxy({ const res: CardsBothSide<T> = proxy({
...entity, ...entity,
at: (controller: number) => {
return res[getWhom(controller)];
},
remove: (controller: number, sequence: number) => { remove: (controller: number, sequence: number) => {
res[getWhom(controller)].splice(sequence, 1); res[getWhom(controller)].splice(sequence, 1);
}, },
...@@ -174,12 +180,19 @@ const wrap = <T extends DuelFieldState>( ...@@ -174,12 +180,19 @@ const wrap = <T extends DuelFieldState>(
clearIdleInteractivities: (controller: number, sequence: number) => { clearIdleInteractivities: (controller: number, sequence: number) => {
res[getWhom(controller)][sequence].idleInteractivities = []; res[getWhom(controller)][sequence].idleInteractivities = [];
}, },
setPlaceInteractivity: ( setPlaceInteractivityType: (
controller: number, controller: number,
sequence: number, sequence: number,
interactivity: CardState["placeInteractivity"] interactType: InteractType
) => { ) => {
res[getWhom(controller)][sequence].placeInteractivity = interactivity; res[getWhom(controller)][sequence].placeInteractivity = {
interactType: interactType,
response: {
controler: controller,
zone,
sequence,
},
};
}, },
clearPlaceInteractivity: (controller: number, sequence: number) => { clearPlaceInteractivity: (controller: number, sequence: number) => {
res[getWhom(controller)][sequence].placeInteractivity = undefined; res[getWhom(controller)][sequence].placeInteractivity = undefined;
...@@ -227,10 +240,10 @@ export const matStore = proxy<PlayMatState>({ ...@@ -227,10 +240,10 @@ export const matStore = proxy<PlayMatState>({
* 根据controller判断是自己还是对方 * 根据controller判断是自己还是对方
* 不要往外export,尽量逻辑收拢在store内部 * 不要往外export,尽量逻辑收拢在store内部
*/ */
const getWhom = (controller: number) => const getWhom = (controller: number): "me" | "op" =>
judgeSelf(controller, matStore.selfType) ? "me" : "op"; judgeSelf(controller, matStore.selfType) ? "me" : "op";
function judgeSelf(player: number, selfType: number): boolean { const judgeSelf = (player: number, selfType: number): boolean => {
switch (selfType) { switch (selfType) {
case 1: case 1:
// 自己是先攻 // 自己是先攻
...@@ -243,4 +256,4 @@ function judgeSelf(player: number, selfType: number): boolean { ...@@ -243,4 +256,4 @@ function judgeSelf(player: number, selfType: number): boolean {
console.error("judgeSelf error", player, selfType); console.error("judgeSelf error", player, selfType);
return false; return false;
} }
} };
...@@ -9,16 +9,39 @@ export interface BothSide<T> { ...@@ -9,16 +9,39 @@ export interface BothSide<T> {
} }
export interface CardsBothSide<T extends DuelFieldState> extends BothSide<T> { export interface CardsBothSide<T extends DuelFieldState> extends BothSide<T> {
remove: (player: number, sequence: number) => void; // 移除特定位置的卡片 /** 根据controller返回对应的数组 */
add: (controller: number, ids: number[]) => void; // 在末尾添加卡片 at: (controller: number) => T;
insert: (controller: number, sequence: number, id: number) => void; // 在指定位置插入卡片 /** 移除特定位置的卡片 */
remove: (player: number, sequence: number) => void;
/** 在末尾添加卡片 */
add: (controller: number, ids: number[]) => void;
/** 在指定位置插入卡片 */
insert: (controller: number, sequence: number, id: number) => void;
/** 设置占据这个位置的卡片信息 */
setOccupant: ( setOccupant: (
controller: number, controller: number,
sequence: number, sequence: number,
id: number, id: number,
position?: ygopro.CardPosition position?: ygopro.CardPosition
) => void; // 设置卡片的卡片信息 ) => void;
removeOccupant: (controller: number, sequence: number) => void; // 移除卡片的卡片信息 /** 移除卡片的卡片信息 */
removeOccupant: (controller: number, sequence: number) => void;
/** 添加 idle 的交互性 */
addIdleInteractivity: (
controller: number,
sequence: number,
interactivity: CardState["idleInteractivities"][number]
) => void;
/** 移除 idle 的交互性 */
clearIdleInteractivities: (controller: number, sequence: number) => void;
/** 设置 place 的交互种类 */
setPlaceInteractivityType: (
controller: number,
sequence: number,
interactType: InteractType
) => void;
/** 移除 place 的交互性 */
clearPlaceInteractivity: (controller: number, sequence: number) => void;
} }
export interface PlayMatState { export interface PlayMatState {
......
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