Commit e28b64d3 authored by chechunchi's avatar chechunchi

add createAsyncRepeatedMeta

parent c082bb70
Pipeline #19526 passed with stages
in 4 minutes and 26 seconds
......@@ -80,6 +80,35 @@ export function createAsyncMetaThunk(name: string): AsyncThunk<
);
}
export function createAsyncRepeatedMetaThunk(
name: string
): AsyncThunk<
{ controler: number; metas: CardMeta[] },
{ controler: number; codes: number[] },
{}
> {
return createAsyncThunk(
name,
async (param: { controler: number; codes: number[] }) => {
const controler = param.controler;
const Ids = param.codes;
const metas = await Promise.all(
Ids.map(async (id) => {
if (id == 0) {
return { id, data: {}, text: {} };
} else {
return await fetchCard(id);
}
})
);
const response = { controler, metas };
return response;
}
);
}
export function extendState<T extends DuelFieldState>(
state: T | undefined,
newState: CardState
......
import {
createAsyncThunk,
ActionReducerMapBuilder,
CaseReducer,
PayloadAction,
} from "@reduxjs/toolkit";
import { DuelState } from "./mod";
import { RootState } from "../../store";
import { fetchCard, CardMeta } from "../../api/cards";
import { judgeSelf } from "./util";
import {
Interactivity,
......@@ -15,31 +13,15 @@ import {
createAsyncMetaThunk,
insertCard,
extendMeta,
createAsyncRepeatedMetaThunk,
} from "./generic";
import { ygopro } from "../../api/ocgcore/idl/ocgcore";
export interface HandState extends DuelFieldState {}
// 增加手牌
export const fetchHandsMeta = createAsyncThunk(
"duel/fetchHandsMeta",
async (param: [number, number[]]) => {
const player = param[0];
const Ids = param[1];
const metas = await Promise.all(
Ids.map(async (id) => {
if (id === 0) {
return { id, data: {}, text: {} };
} else {
return await fetchCard(id);
}
})
);
const response: [number, CardMeta[]] = [player, metas];
return response;
}
export const fetchHandsMeta = createAsyncRepeatedMetaThunk(
"duel/fetchHandsMeta"
);
// 清空手牌互动性
......@@ -95,8 +77,8 @@ export const insertHandMeta = createAsyncMetaThunk("duel/insertHandMeta");
export const handsCase = (builder: ActionReducerMapBuilder<DuelState>) => {
builder.addCase(fetchHandsMeta.pending, (state, action) => {
// Meta结果没返回之前先更新手牌`ID`
const player = action.meta.arg[0];
const ids = action.meta.arg[1];
const player = action.meta.arg.controler;
const ids = action.meta.arg.codes;
const cards = ids.map((id, idx) => {
return {
......@@ -125,8 +107,8 @@ export const handsCase = (builder: ActionReducerMapBuilder<DuelState>) => {
});
builder.addCase(fetchHandsMeta.fulfilled, (state, action) => {
// `Meta`结果回来后更新手牌的`Meta`结果
const player = action.payload[0];
const metas = action.payload[1];
const player = action.payload.controler;
const metas = action.payload.metas;
const hands = judgeSelf(player, state) ? state.meHands : state.opHands;
if (hands) {
......
......@@ -6,5 +6,5 @@ export default (
draw: ygopro.StocGameMessage.MsgDraw,
dispatch: AppDispatch
) => {
dispatch(fetchHandsMeta([draw.player, draw.cards]));
dispatch(fetchHandsMeta({ controler: draw.player, codes: draw.cards }));
};
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