Commit 96426b81 authored by Chunchi Che's avatar Chunchi Che

update stores

parent 479b7694
...@@ -3,6 +3,7 @@ import { proxy } from "valtio"; ...@@ -3,6 +3,7 @@ import { proxy } from "valtio";
import { CardMeta, ygopro } from "@/api"; import { CardMeta, ygopro } from "@/api";
import type { Interactivity } from "./matStore/types"; import type { Interactivity } from "./matStore/types";
import { NeosStore } from "./shared";
/** /**
* 场上某位置的状态 * 场上某位置的状态
...@@ -26,7 +27,7 @@ export interface CardType { ...@@ -26,7 +27,7 @@ export interface CardType {
selected: boolean; // 当前卡是否被选择成为效果的对象 selected: boolean; // 当前卡是否被选择成为效果的对象
} }
class CardStore { class CardStore implements NeosStore {
inner: CardType[] = []; inner: CardType[] = [];
at(zone: ygopro.CardZone, controller: number): CardType[]; at(zone: ygopro.CardZone, controller: number): CardType[];
at( at(
...@@ -90,6 +91,9 @@ class CardStore { ...@@ -90,6 +91,9 @@ class CardStore {
card.location.is_overlay card.location.is_overlay
); );
} }
reset(): void {
this.inner = [];
}
} }
export const cardStore = proxy(new CardStore()); export const cardStore = proxy(new CardStore());
......
import { proxy } from "valtio"; import { proxy } from "valtio";
export interface ChatState { import { NeosStore } from "./shared";
export interface ChatState extends NeosStore {
message: string; message: string;
} }
export const chatStore = proxy<ChatState>({ export const chatStore = proxy<ChatState>({
message: "", message: "",
reset() {
chatStore.message = "";
},
}); });
import { proxy } from "valtio"; import { proxy } from "valtio";
export interface JoinState { import { NeosStore } from "./shared";
export interface JoinState extends NeosStore {
value: boolean; value: boolean;
} }
export const joinStore = proxy<JoinState>({ export const joinStore = proxy<JoinState>({
value: false, value: false,
reset() {
joinStore.value = false;
},
}); });
/* eslint valtio/avoid-this-in-proxy: 0 */ /* eslint valtio/avoid-this-in-proxy: 0 */
import { Omit } from "@react-spring/web";
import { proxy } from "valtio"; import { proxy } from "valtio";
import { ygopro } from "@/api"; import { ygopro } from "@/api";
...@@ -52,11 +53,7 @@ const initInfo: MatState["initInfo"] = (() => { ...@@ -52,11 +53,7 @@ const initInfo: MatState["initInfo"] = (() => {
}); });
})(); })();
/** const initialState: Omit<MatState, "reset"> = {
* 💡 决斗盘状态仓库,本文件核心,
* 具体介绍可以点进`MatState`去看
*/
export const matStore: MatState = proxy<MatState>({
chains: [], chains: [],
timeLimits: { timeLimits: {
...@@ -92,6 +89,20 @@ export const matStore: MatState = proxy<MatState>({ ...@@ -92,6 +89,20 @@ export const matStore: MatState = proxy<MatState>({
}, },
// methods // methods
isMe, isMe,
};
/**
* 💡 决斗盘状态仓库,本文件核心,
* 具体介绍可以点进`MatState`去看
*/
export const matStore: MatState = proxy<MatState>({
...initialState,
reset() {
Object.entries(initialState).forEach((key) => {
// @ts-ignore
matStore[key] = initialState[key];
});
},
}); });
// @ts-ignore 挂到全局,便于调试 // @ts-ignore 挂到全局,便于调试
......
import type { ygopro } from "@/api"; import type { ygopro } from "@/api";
import { NeosStore } from "../shared";
// >>> play mat state >>> // >>> play mat state >>>
export interface BothSide<T> { export interface BothSide<T> {
...@@ -9,7 +11,7 @@ export interface BothSide<T> { ...@@ -9,7 +11,7 @@ export interface BothSide<T> {
of: (controller: number) => T; of: (controller: number) => T;
} }
export interface MatState { export interface MatState extends NeosStore {
selfType: number; selfType: number;
initInfo: BothSide<InitInfo> & { initInfo: BothSide<InitInfo> & {
......
...@@ -4,6 +4,7 @@ import { ygopro } from "@/api"; ...@@ -4,6 +4,7 @@ import { ygopro } from "@/api";
import { matStore } from "@/stores"; import { matStore } from "@/stores";
import type { Interactivity } from "./matStore/types"; import type { Interactivity } from "./matStore/types";
import { NeosStore } from "./shared";
export type PlaceInteractivity = export type PlaceInteractivity =
| Interactivity<{ | Interactivity<{
...@@ -26,8 +27,7 @@ const genPLaces = (n: number): BlockState[] => ...@@ -26,8 +27,7 @@ const genPLaces = (n: number): BlockState[] =>
disabled: false, disabled: false,
})); }));
export const placeStore = proxy({ const initialState = {
inner: {
[MZONE]: { [MZONE]: {
me: genPLaces(7), me: genPLaces(7),
op: genPLaces(7), op: genPLaces(7),
...@@ -36,7 +36,19 @@ export const placeStore = proxy({ ...@@ -36,7 +36,19 @@ export const placeStore = proxy({
me: genPLaces(6), me: genPLaces(6),
op: genPLaces(6), op: genPLaces(6),
}, },
}, };
class PlaceStore implements NeosStore {
inner: {
[MZONE]: {
me: BlockState[];
op: BlockState[];
};
[SZONE]: {
me: BlockState[];
op: BlockState[];
};
} = initialState;
set( set(
zone: ygopro.CardZone.MZONE | ygopro.CardZone.SZONE, zone: ygopro.CardZone.MZONE | ygopro.CardZone.SZONE,
controller: number, controller: number,
...@@ -45,7 +57,7 @@ export const placeStore = proxy({ ...@@ -45,7 +57,7 @@ export const placeStore = proxy({
) { ) {
placeStore.inner[zone][matStore.isMe(controller) ? "me" : "op"][sequence] = placeStore.inner[zone][matStore.isMe(controller) ? "me" : "op"][sequence] =
state; state;
}, }
clearAllInteractivity() { clearAllInteractivity() {
(["me", "op"] as const).forEach((who) => { (["me", "op"] as const).forEach((who) => {
([MZONE, SZONE] as const).forEach((where) => { ([MZONE, SZONE] as const).forEach((where) => {
...@@ -54,5 +66,10 @@ export const placeStore = proxy({ ...@@ -54,5 +66,10 @@ export const placeStore = proxy({
); );
}); });
}); });
}, }
}); reset(): void {
placeStore.inner = initialState;
}
}
export const placeStore = proxy(new PlaceStore());
...@@ -3,6 +3,7 @@ import { proxy } from "valtio"; ...@@ -3,6 +3,7 @@ import { proxy } from "valtio";
import { ygopro } from "@/api"; import { ygopro } from "@/api";
import SelfType = ygopro.StocTypeChange.SelfType; import SelfType = ygopro.StocTypeChange.SelfType;
import { NeosStore } from "./shared";
export interface Player { export interface Player {
name?: string; name?: string;
...@@ -17,7 +18,7 @@ export interface deckInfo { ...@@ -17,7 +18,7 @@ export interface deckInfo {
sideCnt: number; sideCnt: number;
} }
export interface PlayerState { export interface PlayerState extends NeosStore {
player0: Player; player0: Player;
player1: Player; player1: Player;
observerCount: number; observerCount: number;
...@@ -27,12 +28,16 @@ export interface PlayerState { ...@@ -27,12 +28,16 @@ export interface PlayerState {
getOpPlayer: () => Player; getOpPlayer: () => Player;
} }
export const playerStore = proxy<PlayerState>({ const initialState = {
player0: {}, player0: {},
player1: {}, player1: {},
observerCount: 0, observerCount: 0,
isHost: false, isHost: false,
selfType: SelfType.UNKNOWN, selfType: SelfType.UNKNOWN,
};
export const playerStore = proxy<PlayerState>({
...initialState,
getMePlayer() { getMePlayer() {
if (this.selfType === SelfType.PLAYER1) return this.player0; if (this.selfType === SelfType.PLAYER1) return this.player0;
return this.player1; return this.player1;
...@@ -41,4 +46,10 @@ export const playerStore = proxy<PlayerState>({ ...@@ -41,4 +46,10 @@ export const playerStore = proxy<PlayerState>({
if (this.selfType === SelfType.PLAYER1) return this.player1; if (this.selfType === SelfType.PLAYER1) return this.player1;
return this.player0; return this.player0;
}, },
reset() {
Object.entries(initialState).forEach((key) => {
// @ts-ignore
playerStore[key] = initialState[key];
});
},
}); });
...@@ -2,6 +2,8 @@ import { proxy } from "valtio"; ...@@ -2,6 +2,8 @@ import { proxy } from "valtio";
import { YgoProPacket } from "@/api/ocgcore/ocgAdapter/packet"; import { YgoProPacket } from "@/api/ocgcore/ocgAdapter/packet";
import { NeosStore } from "./shared";
// 对局中每一次状态改变的记录 // 对局中每一次状态改变的记录
interface ReplaySpot { interface ReplaySpot {
packet: ReplayPacket; // 将会保存在回放文件中的数据 packet: ReplayPacket; // 将会保存在回放文件中的数据
...@@ -14,7 +16,7 @@ interface ReplayPacket { ...@@ -14,7 +16,7 @@ interface ReplayPacket {
} }
// 保存对局回放数据的`Store` // 保存对局回放数据的`Store`
class ReplayStore { class ReplayStore implements NeosStore {
inner: ReplaySpot[] = []; inner: ReplaySpot[] = [];
record(ygoPacket: YgoProPacket) { record(ygoPacket: YgoProPacket) {
this.inner.push({ this.inner.push({
...@@ -24,7 +26,7 @@ class ReplayStore { ...@@ -24,7 +26,7 @@ class ReplayStore {
encode(): ArrayBuffer[] { encode(): ArrayBuffer[] {
return this.inner.map((spot) => spot.packet).map(replayPacket2arrayBuffer); return this.inner.map((spot) => spot.packet).map(replayPacket2arrayBuffer);
} }
clear() { reset() {
this.inner = []; this.inner = [];
} }
} }
......
// Neos项目中所有Store需要实现的接口
// 用于统一管理状态的初始化和重置
export interface NeosStore {
reset(): void;
}
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