Commit 6b87ae77 authored by Chunchi Che's avatar Chunchi Che

update handSlice

parent 284661fb
import axios from "axios"; import axios from "axios";
interface CardMeta { export interface CardMeta {
id: number; id: number;
data: { data: {
ot?: number; ot?: number;
......
import { import { createAsyncThunk, ActionReducerMapBuilder } from "@reduxjs/toolkit";
PayloadAction,
CaseReducer,
createAsyncThunk,
ActionReducerMapBuilder,
} from "@reduxjs/toolkit";
import { DuelState } from "./mod"; import { DuelState } from "./mod";
import { RootState } from "../../store"; import { RootState } from "../../store";
import { Card, fetchCard } from "../../api/cards"; import { Card, fetchCard, CardMeta } from "../../api/cards";
import { judgeSelf } from "./util"; import { judgeSelf } from "./util";
import * as UICONFIG from "../../config/ui"; import * as UICONFIG from "../../config/ui";
...@@ -15,16 +10,33 @@ export interface Hands { ...@@ -15,16 +10,33 @@ export interface Hands {
} }
// 增加手牌 // 增加手牌
export const addHandsImpl: CaseReducer< export const fetchHandsMeta = createAsyncThunk(
DuelState, "duel/fetchHandsMeta",
PayloadAction<[number, number[]]> async (param: [number, number[]]) => {
> = (state, action) => { const player = param[0];
const Ids = param[1];
const metas = await Promise.all(
Ids.filter((id) => {
return id !== 0;
}).map(async (id) => {
return await fetchCard(id);
})
);
const response: [number, CardMeta[]] = [player, metas];
return response;
}
);
export const handsCase = (builder: ActionReducerMapBuilder<DuelState>) => {
builder.addCase(fetchHandsMeta.fulfilled, (state, action) => {
const player = action.payload[0]; const player = action.payload[0];
const hands = action.payload[1]; const hands = action.payload[1];
const selfType = state.selfType; const selfType = state.selfType;
const cards = hands.map((id) => { const cards = hands.map((meta) => {
return { meta: { id, data: {}, text: {} }, transform: {} }; return { meta, transform: {} };
}); });
if (judgeSelf(player, selfType)) { if (judgeSelf(player, selfType)) {
if (state.meHands) { if (state.meHands) {
...@@ -40,41 +52,6 @@ export const addHandsImpl: CaseReducer< ...@@ -40,41 +52,6 @@ export const addHandsImpl: CaseReducer<
state.opHands = { cards }; state.opHands = { cards };
} }
} }
};
export const fetchMeHandsMeta = createAsyncThunk(
"duel/fetchMeHandsMeta",
async (Ids: number[]) => {
return await Promise.all(
Ids.map(async (id) => {
return await fetchCard(id);
})
);
}
);
export const meHandsCase = (builder: ActionReducerMapBuilder<DuelState>) => {
builder.addCase(fetchMeHandsMeta.fulfilled, (state, action) => {
// TODO: 合法性校验
const cardMetas = action.payload;
if (state.meHands) {
for (let meta of cardMetas) {
for (let hand of state.meHands.cards) {
if (hand.meta.id === meta.id) {
hand.meta = meta;
}
}
}
} else {
state.meHands = {
cards: cardMetas.map((meta) => {
return { meta, transform: {} };
}),
};
setHandsTransform(state.meHands.cards);
}
}); });
}; };
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit"; import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { InitInfo, infoInitImpl } from "./initInfoSlice"; import { InitInfo, infoInitImpl } from "./initInfoSlice";
import { Hands, addHandsImpl, meHandsCase } from "./handsSlice"; import { Hands, handsCase } from "./handsSlice";
import { newTurnImpl } from "./turnSlice"; import { newTurnImpl } from "./turnSlice";
import { newPhaseImpl } from "./phaseSlice"; import { newPhaseImpl } from "./phaseSlice";
import { RootState } from "../../store"; import { RootState } from "../../store";
...@@ -30,16 +30,15 @@ const duelSlice = createSlice({ ...@@ -30,16 +30,15 @@ const duelSlice = createSlice({
state.selfType = action.payload; state.selfType = action.payload;
}, },
infoInit: infoInitImpl, infoInit: infoInitImpl,
addHands: addHandsImpl,
updateTurn: newTurnImpl, updateTurn: newTurnImpl,
updatePhase: newPhaseImpl, updatePhase: newPhaseImpl,
}, },
extraReducers(builder) { extraReducers(builder) {
meHandsCase(builder); handsCase(builder);
}, },
}); });
export const { setSelfType, infoInit, addHands, updateTurn, updatePhase } = export const { setSelfType, infoInit, updateTurn, updatePhase } =
duelSlice.actions; duelSlice.actions;
export const selectDuelHsStart = (state: RootState) => { export const selectDuelHsStart = (state: RootState) => {
return state.duel.meInitInfo != null; return state.duel.meInitInfo != null;
......
import { ygopro } from "../../api/ocgcore/idl/ocgcore"; import { ygopro } from "../../api/ocgcore/idl/ocgcore";
import { AppDispatch } from "../../store"; import { AppDispatch } from "../../store";
import { addHands } from "../../reducers/duel/mod"; import { fetchHandsMeta } from "../../reducers/duel/handsSlice";
import { fetchMeHandsMeta } from "../../reducers/duel/handsSlice";
export default ( export default (
draw: ygopro.StocGameMessage.MsgDraw, draw: ygopro.StocGameMessage.MsgDraw,
dispatch: AppDispatch dispatch: AppDispatch
) => { ) => {
// FIXME: draw.player 和先后攻有关系 dispatch(fetchHandsMeta([draw.player, draw.cards]));
if (draw.player === 0) {
dispatch(addHands([0, draw.cards]));
dispatch(fetchMeHandsMeta(draw.cards));
} else if (draw.player === 1) {
dispatch(addHands([1, draw.cards]));
} else {
console.log("Currently only support 2v2 mode.");
}
}; };
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