Commit 75fdc88a authored by timel's avatar timel

fix: types

parent fcc09e9f
...@@ -36,29 +36,31 @@ export default (move: MsgMove, dispatch: AppDispatch) => { ...@@ -36,29 +36,31 @@ export default (move: MsgMove, dispatch: AppDispatch) => {
const to = move.to; const to = move.to;
const reason = move.reason; const reason = move.reason;
// TODO: 如果后面做动画的话,要考虑DECK的情况。
// 现在不会对DECK做判断
switch (from.location) { switch (from.location) {
case ygopro.CardZone.MZONE: case ygopro.CardZone.MZONE:
case ygopro.CardZone.SZONE: { case ygopro.CardZone.SZONE: {
// 魔陷和怪兽需要清掉占用、清掉超量素材 // 魔陷和怪兽需要清掉占用、清掉超量素材
const target = matStore.getZone(from.location).at(from.controler)[ const target = matStore.in(from.location).of(from.controler)[
from.sequence from.sequence
]; ];
target.occupant = undefined; target.occupant = undefined;
target.overlay_materials = []; target.overlay_materials = [];
break; break;
} }
case ygopro.CardZone.HAND:
case ygopro.CardZone.REMOVED: case ygopro.CardZone.REMOVED:
case ygopro.CardZone.GRAVE: case ygopro.CardZone.GRAVE:
case ygopro.CardZone.HAND: case ygopro.CardZone.HAND:
case ygopro.CardZone.EXTRA: { case ygopro.CardZone.EXTRA: {
// 其余区域就是在list删掉这张卡 // 其余区域就是在list删掉这张卡
matStore.getZone(from.location).remove(from.controler, from.sequence); matStore.in(from.location).remove(from.controler, from.sequence);
break; break;
} }
// 仅仅去除超量素材 // 仅仅去除超量素材
case ygopro.CardZone.OVERLAY: { case ygopro.CardZone.OVERLAY: {
const target = matStore.monsters.at(from.controler)[from.sequence]; const target = matStore.monsters.of(from.controler)[from.sequence];
if (target && target.overlay_materials) { if (target && target.overlay_materials) {
target.overlay_materials.splice(from.overlay_sequence, 1); target.overlay_materials.splice(from.overlay_sequence, 1);
} }
...@@ -81,23 +83,28 @@ export default (move: MsgMove, dispatch: AppDispatch) => { ...@@ -81,23 +83,28 @@ export default (move: MsgMove, dispatch: AppDispatch) => {
FIXME_fetchOverlayMeta(to.controler, to.sequence, sorted); FIXME_fetchOverlayMeta(to.controler, to.sequence, sorted);
// 设置Occupant,和魔陷区/其他区共用一个逻辑,特地不写break // 设置Occupant,和魔陷区/其他区共用一个逻辑,特地不写break
} }
case ygopro.CardZone.SZONE: case ygopro.CardZone.SZONE: {
case ygopro.CardZone.DECK:
case ygopro.CardZone.REMOVED:
case ygopro.CardZone.GRAVE:
case ygopro.CardZone.HAND:
case ygopro.CardZone.EXTRA: {
matStore matStore
.getZone(to.location) .in(to.location)
.setOccupant(to.controler, to.sequence, code, to.position); .setOccupant(to.controler, to.sequence, code, to.position);
break; break;
// FIXME 这里逻辑不对...
} }
case ygopro.CardZone.REMOVED:
case ygopro.CardZone.GRAVE:
case ygopro.CardZone.EXTRA:
case ygopro.CardZone.HAND: { case ygopro.CardZone.HAND: {
matStore.hands.insert(to.controler, to.sequence, code); matStore.hands.insert(to.controler, to.sequence, code);
break; break;
} }
case ygopro.CardZone.OVERLAY: { case ygopro.CardZone.OVERLAY: {
if (reason == REASON_MATERIAL) {
// 超量素材在进行超量召唤时,若玩家未选择超量怪兽的位置,会“沉到决斗盘下面”,`reason`字段值是`REASON_MATERIAL`
// 这时候将它们放到一个栈中,待超量怪兽的Move消息到来时从栈中获取超量素材补充到状态中
OVERLAY_STACK.push({ code, sequence: to.overlay_sequence });
} else {
// 其他情况下,比如“宵星的机神 丁吉尔苏”的“补充超量素材”效果,直接更新状态中
FIXME_fetchOverlayMeta(to.controler, to.sequence, [code], true);
}
break; break;
} }
default: { default: {
......
...@@ -3,43 +3,47 @@ import { fetchEsHintMeta } from "@/reducers/duel/hintSlice"; ...@@ -3,43 +3,47 @@ import { fetchEsHintMeta } from "@/reducers/duel/hintSlice";
import { setMagicPosition, setMonsterPosition } from "@/reducers/duel/mod"; import { setMagicPosition, setMonsterPosition } from "@/reducers/duel/mod";
import { AppDispatch } from "@/store"; import { AppDispatch } from "@/store";
import MsgPosChange = ygopro.StocGameMessage.MsgPosChange; import MsgPosChange = ygopro.StocGameMessage.MsgPosChange;
import { matStore } from "@/valtioStores"; import {
matStore,
fetchEsHintMeta as FIXME_fetchEsHintMeta,
} from "@/valtioStores";
export default (posChange: MsgPosChange, dispatch: AppDispatch) => { export default (posChange: MsgPosChange, dispatch: AppDispatch) => {
const cardInfo = posChange.card_info; const { location, controler, sequence } = posChange.card_info;
switch (cardInfo.location) { switch (location) {
case ygopro.CardZone.MZONE: { case ygopro.CardZone.MZONE: {
dispatch( dispatch(
setMonsterPosition({ setMonsterPosition({
controler: cardInfo.controler, controler: controler,
sequence: cardInfo.sequence, sequence,
position: posChange.cur_position, position: posChange.cur_position,
}) })
); );
matStore.monsters.at(cardInfo.controler)[ matStore.monsters.of(controler)[sequence].location.position =
cardInfo.sequence posChange.cur_position;
].location.position = posChange.cur_position;
break; break;
} }
case ygopro.CardZone.SZONE: { case ygopro.CardZone.SZONE: {
dispatch( dispatch(
setMagicPosition({ setMagicPosition({
controler: cardInfo.controler, controler: controler,
sequence: cardInfo.sequence, sequence,
position: posChange.cur_position, position: posChange.cur_position,
}) })
); );
matStore.magics.at(cardInfo.controler)[ matStore.magics.of(controler)[sequence].location.position =
cardInfo.sequence posChange.cur_position;
].location.position = posChange.cur_position;
break; break;
} }
default: { default: {
console.log(`Unhandled zone ${cardInfo.location}`); console.log(`Unhandled zone ${location}`);
} }
} }
dispatch(fetchEsHintMeta({ originMsg: 1600 })); dispatch(fetchEsHintMeta({ originMsg: 1600 }));
FIXME_fetchEsHintMeta({
originMsg: 1600,
});
}; };
...@@ -55,9 +55,9 @@ function reloadDuelField( ...@@ -55,9 +55,9 @@ function reloadDuelField(
reload: true, reload: true,
}; };
}); });
matStore.getZone(cardZone).at(controller).length = 0; matStore.in(cardZone).of(controller).length = 0;
matStore matStore
.getZone(cardZone) .in(cardZone)
.at(controller) .of(controller)
.push(...cards); .push(...cards);
} }
...@@ -87,7 +87,7 @@ export default (selectBattleCmd: MsgSelectBattleCmd, dispatch: AppDispatch) => { ...@@ -87,7 +87,7 @@ export default (selectBattleCmd: MsgSelectBattleCmd, dispatch: AppDispatch) => {
const tmp = map[interactType]; const tmp = map[interactType];
if (tmp) { if (tmp) {
matStore matStore
.getZone(cardInfo.location) .in(cardInfo.location)
.addIdleInteractivity(player, cardInfo.sequence, { .addIdleInteractivity(player, cardInfo.sequence, {
...tmp, ...tmp,
interactType, interactType,
......
...@@ -84,7 +84,7 @@ export default (selectIdleCmd: MsgSelectIdleCmd, dispatch: AppDispatch) => { ...@@ -84,7 +84,7 @@ export default (selectIdleCmd: MsgSelectIdleCmd, dispatch: AppDispatch) => {
const tmp = map[interactType]; const tmp = map[interactType];
if (tmp) { if (tmp) {
matStore matStore
.getZone(cardInfo.location) .in(cardInfo.location)
.addIdleInteractivity(player, cardInfo.sequence, { .addIdleInteractivity(player, cardInfo.sequence, {
...tmp, ...tmp,
interactType, interactType,
......
...@@ -24,6 +24,6 @@ export default (shuffleHand: MsgShuffleHand, dispatch: AppDispatch) => { ...@@ -24,6 +24,6 @@ export default (shuffleHand: MsgShuffleHand, dispatch: AppDispatch) => {
}; };
}); });
matStore.hands.at(controller).length = 0; matStore.hands.of(controller).length = 0;
matStore.hands.at(controller).push(...metas); matStore.hands.of(controller).push(...metas);
}; };
...@@ -71,10 +71,10 @@ export default ( ...@@ -71,10 +71,10 @@ export default (
// <<< 删除 <<< // <<< 删除 <<<
// 上面的删除就可以了 // 上面的删除就可以了
matStore.monsters.at(0).forEach((x) => (x.location.controler = 0)); matStore.monsters.of(0).forEach((x) => (x.location.controler = 0));
matStore.monsters.at(1).forEach((x) => (x.location.controler = 1)); matStore.monsters.of(1).forEach((x) => (x.location.controler = 1));
matStore.magics.at(0).forEach((x) => (x.location.controler = 0)); matStore.magics.of(0).forEach((x) => (x.location.controler = 0));
matStore.magics.at(1).forEach((x) => (x.location.controler = 1)); matStore.magics.of(1).forEach((x) => (x.location.controler = 1));
matStore.decks.add(0, Array(start.deckSize1).fill(0)); matStore.decks.add(0, Array(start.deckSize1).fill(0));
matStore.decks.add(1, Array(start.deckSize2).fill(0)); matStore.decks.add(1, Array(start.deckSize2).fill(0));
......
...@@ -10,7 +10,7 @@ export default (updateData: MsgUpdateData, dispatch: AppDispatch) => { ...@@ -10,7 +10,7 @@ export default (updateData: MsgUpdateData, dispatch: AppDispatch) => {
const { player: controller, zone, actions } = updateData; const { player: controller, zone, actions } = updateData;
if (controller !== undefined && zone !== undefined && actions !== undefined) { if (controller !== undefined && zone !== undefined && actions !== undefined) {
const field = matStore.getZone(zone).at(controller); const field = matStore.in(zone).of(controller);
actions.forEach((action) => { actions.forEach((action) => {
const sequence = action.location?.sequence; const sequence = action.location?.sequence;
if (typeof sequence !== "undefined") { if (typeof sequence !== "undefined") {
......
...@@ -12,11 +12,11 @@ export default (msgUpdateHp: MsgUpdateHp, dispatch: AppDispatch) => { ...@@ -12,11 +12,11 @@ export default (msgUpdateHp: MsgUpdateHp, dispatch: AppDispatch) => {
if (msgUpdateHp.type_ == MsgUpdateHp.ActionType.DAMAGE) { if (msgUpdateHp.type_ == MsgUpdateHp.ActionType.DAMAGE) {
dispatch(fetchEsHintMeta({ originMsg: "玩家收到伤害时" })); // TODO: i18n dispatch(fetchEsHintMeta({ originMsg: "玩家收到伤害时" })); // TODO: i18n
FIXME_fetchEsHintMeta({ originMsg: "玩家收到伤害时" }); FIXME_fetchEsHintMeta({ originMsg: "玩家收到伤害时" });
matStore.initInfo.at(msgUpdateHp.player).life -= msgUpdateHp.value; matStore.initInfo.of(msgUpdateHp.player).life -= msgUpdateHp.value;
} else if (msgUpdateHp.type_ == MsgUpdateHp.ActionType.RECOVER) { } else if (msgUpdateHp.type_ == MsgUpdateHp.ActionType.RECOVER) {
dispatch(fetchEsHintMeta({ originMsg: "玩家生命值回复时" })); // TODO: i18n dispatch(fetchEsHintMeta({ originMsg: "玩家生命值回复时" })); // TODO: i18n
FIXME_fetchEsHintMeta({ originMsg: "玩家生命值回复时" }); FIXME_fetchEsHintMeta({ originMsg: "玩家生命值回复时" });
matStore.initInfo.at(msgUpdateHp.player).life += msgUpdateHp.value; matStore.initInfo.of(msgUpdateHp.player).life += msgUpdateHp.value;
} }
dispatch(updateHp(msgUpdateHp)); // 可以删除了 dispatch(updateHp(msgUpdateHp)); // 可以删除了
......
import { ygopro, fetchCard } from "@/api";
import { matStore, getCardByLocation } from "@/valtioStores";
export const fetchCardMeta = async (
zone: ygopro.CardZone,
controler: number,
sequence: number,
code: number,
position?: ygopro.CardPosition
) => {
await matStore.getZone(zone).setOccupant(controler, sequence, code, position);
};
...@@ -102,7 +102,7 @@ export const fetchCheckCardMeta = async ( ...@@ -102,7 +102,7 @@ export const fetchCheckCardMeta = async (
const newID = const newID =
code != 0 code != 0
? code ? code
: matStore.getZone(location.location).at(controller)[location.sequence] : matStore.in(location.location).of(controller)[location.sequence]
?.occupant?.id || 0; ?.occupant?.id || 0;
const newOption = { const newOption = {
meta: { id: newID, data: {}, text: {} }, meta: { id: newID, data: {}, text: {} },
......
...@@ -63,8 +63,8 @@ export const fetchEsHintMeta = async ({ ...@@ -63,8 +63,8 @@ export const fetchEsHintMeta = async ({
if (location) { if (location) {
const fieldMeta = matStore const fieldMeta = matStore
.getZone(location.location) .in(location.location)
.at(location.controler) .of(location.controler)
.at(location.sequence); .at(location.sequence);
if (fieldMeta?.occupant?.text.name) { if (fieldMeta?.occupant?.text.name) {
esHint = esHint.replace("[?]", fieldMeta.occupant.text.name); esHint = esHint.replace("[?]", fieldMeta.occupant.text.name);
......
...@@ -11,7 +11,7 @@ export const fetchOverlayMeta = async ( ...@@ -11,7 +11,7 @@ export const fetchOverlayMeta = async (
overlayCodes.map(async (id) => await fetchCard(id)) overlayCodes.map(async (id) => await fetchCard(id))
); );
const target = matStore.monsters.at(controller)[sequence]; const target = matStore.monsters.of(controller)[sequence];
if (target && target.occupant) { if (target && target.occupant) {
if (append) { if (append) {
target.overlay_materials = (target.overlay_materials || []).concat(metas); target.overlay_materials = (target.overlay_materials || []).concat(metas);
......
...@@ -2,7 +2,7 @@ import type { ygopro } from "@/api"; ...@@ -2,7 +2,7 @@ import type { ygopro } from "@/api";
import { matStore } from "@/valtioStores"; import { matStore } from "@/valtioStores";
export const getCardByLocation = (location: ygopro.CardLocation) => { export const getCardByLocation = (location: ygopro.CardLocation) => {
return matStore.getZone(location.location).at(location.controler)[ return matStore.in(location.location).of(location.controler)[
location.sequence location.sequence
]; ];
}; };
...@@ -39,11 +39,11 @@ const isMe = (player: number): boolean => { ...@@ -39,11 +39,11 @@ const isMe = (player: number): boolean => {
} }
}; };
const genDuel = <T>(obj: T) => { const genDuel = <T>(obj: T): BothSide<T> => {
const res = proxy({ const res = proxy({
me: cloneDeep(obj), me: cloneDeep(obj),
op: cloneDeep(obj), op: cloneDeep(obj),
at: (controller: number) => res[getWhom(controller)], of: (controller: number) => res[getWhom(controller)],
}); });
return res; return res;
}; };
...@@ -106,17 +106,17 @@ const wrap = <T extends DuelFieldState>( ...@@ -106,17 +106,17 @@ const wrap = <T extends DuelFieldState>(
const res: CardsBothSide<T> = proxy({ const res: CardsBothSide<T> = proxy({
...entity, ...entity,
remove: (controller: number, sequence: number) => { remove: (controller: number, sequence: number) => {
res.at(controller).splice(sequence, 1); res.of(controller).splice(sequence, 1);
}, },
insert: async (controller: number, sequence: number, id: number) => { insert: async (controller: number, sequence: number, id: number) => {
const card = await genCard(controller, id); const card = await genCard(controller, id);
res.at(controller).splice(sequence, 0, card); res.of(controller).splice(sequence, 0, card);
}, },
add: async (controller: number, ids: number[]) => { add: async (controller: number, ids: number[]) => {
const cards = await Promise.all( const cards = await Promise.all(
ids.map(async (id) => genCard(controller, id)) ids.map(async (id) => genCard(controller, id))
); );
res.at(controller).splice(res.at(controller).length, 0, ...cards); res.of(controller).splice(res.of(controller).length, 0, ...cards);
}, },
setOccupant: async ( setOccupant: async (
controller: number, controller: number,
...@@ -125,7 +125,7 @@ const wrap = <T extends DuelFieldState>( ...@@ -125,7 +125,7 @@ const wrap = <T extends DuelFieldState>(
position?: ygopro.CardPosition position?: ygopro.CardPosition
) => { ) => {
const meta = await fetchCard(id); const meta = await fetchCard(id);
const target = res.at(controller)[sequence]; const target = res.of(controller)[sequence];
target.occupant = meta; target.occupant = meta;
if (position) { if (position) {
target.location.position = position; target.location.position = position;
...@@ -136,17 +136,17 @@ const wrap = <T extends DuelFieldState>( ...@@ -136,17 +136,17 @@ const wrap = <T extends DuelFieldState>(
sequence: number, sequence: number,
interactivity: CardState["idleInteractivities"][number] interactivity: CardState["idleInteractivities"][number]
) => { ) => {
res.at(controller)[sequence].idleInteractivities.push(interactivity); res.of(controller)[sequence].idleInteractivities.push(interactivity);
}, },
clearIdleInteractivities: (controller: number) => { clearIdleInteractivities: (controller: number) => {
res.at(controller).forEach((card) => (card.idleInteractivities = [])); res.of(controller).forEach((card) => (card.idleInteractivities = []));
}, },
setPlaceInteractivityType: ( setPlaceInteractivityType: (
controller: number, controller: number,
sequence: number, sequence: number,
interactType: InteractType interactType: InteractType
) => { ) => {
res.at(controller)[sequence].placeInteractivity = { res.of(controller)[sequence].placeInteractivity = {
interactType: interactType, interactType: interactType,
response: { response: {
controler: controller, controler: controller,
...@@ -157,7 +157,7 @@ const wrap = <T extends DuelFieldState>( ...@@ -157,7 +157,7 @@ const wrap = <T extends DuelFieldState>(
}, },
clearPlaceInteractivity: (controller: number) => { clearPlaceInteractivity: (controller: number) => {
res res
.at(controller) .of(controller)
.forEach((card) => (card.placeInteractivity = undefined)); .forEach((card) => (card.placeInteractivity = undefined));
}, },
}); });
...@@ -226,6 +226,6 @@ export const matStore: MatState = proxy<MatState>({ ...@@ -226,6 +226,6 @@ export const matStore: MatState = proxy<MatState>({
waiting: false, waiting: false,
unimplemented: 0, unimplemented: 0,
// methods // methods
getZone, in: getZone,
isMe, isMe,
}); });
...@@ -6,12 +6,11 @@ import type { CardMeta } from "@/api/cards"; ...@@ -6,12 +6,11 @@ import type { CardMeta } from "@/api/cards";
export interface BothSide<T> { export interface BothSide<T> {
me: T; me: T;
op: T; op: T;
at: (controller: number) => T; /** 根据controller返回对应的数组,op或者me */
of: (controller: number) => T;
} }
export interface CardsBothSide<T extends DuelFieldState> extends BothSide<T> { export interface CardsBothSide<T extends DuelFieldState> extends BothSide<T> {
/** 根据controller返回对应的数组,op或者me */
at: (controller: number) => T;
/** 移除特定位置的卡片 */ /** 移除特定位置的卡片 */
remove: (player: number, sequence: number) => void; remove: (player: number, sequence: number) => void;
/** 在末尾添加卡片 */ /** 在末尾添加卡片 */
...@@ -81,8 +80,8 @@ export interface MatState { ...@@ -81,8 +80,8 @@ export interface MatState {
unimplemented: number; // 未处理的`Message` unimplemented: number; // 未处理的`Message`
// >>> methods >>> // >>> methods >>>
/** 根据zone获取hands/masters/gy... */ /** 根据zone获取hands/masters/magics... */
getZone: (zone: ygopro.CardZone) => CardsBothSide<DuelFieldState>; in: (zone: ygopro.CardZone) => CardsBothSide<DuelFieldState>;
/** 根据自己的先后手判断是否是自己 */ /** 根据自己的先后手判断是否是自己 */
isMe: (player: number) => boolean; isMe: (player: number) => boolean;
} }
......
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