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

update stores

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