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