Commit 5d36736c authored by Chunchi Che's avatar Chunchi Che

add replayStore.ts

parent f6f681cc
......@@ -5,6 +5,7 @@ export * from "./matStore";
export * from "./moraStore";
export * from "./placeStore";
export * from "./playerStore";
export * from "./replayStore";
import { devtools } from "valtio/utils";
......@@ -15,6 +16,7 @@ import { matStore } from "./matStore";
import { moraStore } from "./moraStore";
import { placeStore } from "./placeStore";
import { playerStore } from "./playerStore";
import { replayStore } from "./replayStore";
devtools(playerStore, { name: "player", enabled: true });
devtools(chatStore, { name: "chat", enabled: true });
......@@ -23,3 +25,4 @@ devtools(moraStore, { name: "mora", enabled: true });
devtools(matStore, { name: "mat", enabled: true });
devtools(cardStore, { name: "card", enabled: true });
devtools(placeStore, { name: "place", enabled: true });
devtools(replayStore, { name: "replay", enabled: true });
import { proxy } from "valtio";
import { YgoProPacket } from "@/api/ocgcore/ocgAdapter/packet";
// 对局中每一次状态改变的记录
interface ReplaySpot {
msg: string; // 人类能理解的语音,后续考虑用来在对局中展示
packet: ReplayPacket; // 将会保存在回放文件中的数据
}
// 保存回放信息的数据包
interface ReplayPacket {
func: number; // 对应的`GAME_MSG`编号
extraData: ArrayBuffer;
}
class ReplayStore {
inner: ReplaySpot[] = [];
record(ygoPacket: YgoProPacket, msg?: string) {
this.inner.push({
msg: msg ?? "",
packet: ygoPacket2replayPacket(ygoPacket),
});
}
encode(): ArrayBuffer[] {
return this.inner.map((spot) => spot.packet).map(replayPacket2arrayBuffer);
}
clear() {
this.inner = [];
}
}
const ygoPacket2replayPacket = (ygoPacket: YgoProPacket) => ({
func: ygoPacket.exData[0],
extraData: ygoPacket.exData.slice(1),
});
const replayPacket2arrayBuffer = (replayPacket: ReplayPacket) => {
const { func, extraData } = replayPacket;
const packetLen = 1 + 4 + extraData.byteLength;
const array = new Uint8Array(packetLen);
const dataview = new DataView(array.buffer);
dataview.setUint8(0, func);
dataview.setUint32(1, extraData.byteLength, true);
array.set(new Uint8Array(extraData), 5);
return array.buffer;
};
export const replayStore = proxy(new ReplayStore());
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