Commit c7f3bbcf authored by chechunchi's avatar chechunchi

update hand slice and service

parent e28b64d3
Pipeline #19527 passed with stages
in 3 minutes and 11 seconds
......@@ -209,3 +209,18 @@ export function insertCard<T extends DuelFieldState>(
state.inner.splice(sequence, 0, card);
}
}
export function updateCardMeta<T extends DuelFieldState>(
state: T | undefined,
metas: CardMeta[]
) {
if (state) {
state.inner.forEach((item) => {
metas.forEach((meta) => {
if (item.occupant?.id === meta.id) {
item.occupant = meta;
}
});
});
}
}
......@@ -14,6 +14,7 @@ import {
insertCard,
extendMeta,
createAsyncRepeatedMetaThunk,
updateCardMeta,
} from "./generic";
import { ygopro } from "../../api/ocgcore/idl/ocgcore";
......@@ -74,19 +75,22 @@ export const removeHandImpl: CaseReducer<
export const insertHandMeta = createAsyncMetaThunk("duel/insertHandMeta");
export const updateHandsMeta = createAsyncRepeatedMetaThunk(
"duel/updateHandsMeta"
);
export const handsCase = (builder: ActionReducerMapBuilder<DuelState>) => {
builder.addCase(fetchHandsMeta.pending, (state, action) => {
// Meta结果没返回之前先更新手牌`ID`
const player = action.meta.arg.controler;
const ids = action.meta.arg.codes;
const cards = ids.map((id, idx) => {
const cards = ids.map((id) => {
return {
occupant: { id, data: {}, text: {} },
location: {
controler: player,
location: ygopro.CardZone.HAND,
sequence: idx,
},
idleInteractivities: [],
};
......@@ -111,15 +115,7 @@ export const handsCase = (builder: ActionReducerMapBuilder<DuelState>) => {
const metas = action.payload.metas;
const hands = judgeSelf(player, state) ? state.meHands : state.opHands;
if (hands) {
for (let hand of hands.inner) {
for (let meta of metas) {
if (hand.occupant?.id === meta.id) {
hand.occupant = meta;
}
}
}
}
updateCardMeta(hands, metas);
});
builder.addCase(insertHandMeta.pending, (state, action) => {
......@@ -144,6 +140,35 @@ export const handsCase = (builder: ActionReducerMapBuilder<DuelState>) => {
extendMeta(hands, meta, sequence);
});
builder.addCase(updateHandsMeta.pending, (state, action) => {
const controler = action.meta.arg.controler;
const codes = action.meta.arg.codes;
const metas = codes.map((code) => {
return {
occupant: { id: code, data: {}, text: {} },
location: {
controler,
location: ygopro.CardZone.HAND,
},
idleInteractivities: [],
};
});
const hands = judgeSelf(controler, state) ? state.meHands : state.opHands;
if (hands) {
hands.inner = metas;
}
});
builder.addCase(updateHandsMeta.fulfilled, (state, action) => {
const controler = action.payload.controler;
const metas = action.payload.metas;
const hands = judgeSelf(controler, state) ? state.meHands : state.opHands;
updateCardMeta(hands, metas);
});
};
// 在特定位置增加手牌
......
import { ygopro } from "../../api/ocgcore/idl/ocgcore";
import { updateHandsMeta } from "../../reducers/duel/handsSlice";
import { AppDispatch } from "../../store";
import MsgShuffleHand = ygopro.StocGameMessage.MsgShuffleHand;
export default (shuffleHand: MsgShuffleHand, dispatch: AppDispatch) => {
console.log(shuffleHand);
dispatch(
updateHandsMeta({ controler: shuffleHand.player, codes: shuffleHand.hands })
);
};
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