Commit 284661fb authored by Chunchi Che's avatar Chunchi Che

update handSlice

parent 680f4298
......@@ -7,42 +7,38 @@ import {
import { DuelState } from "./mod";
import { RootState } from "../../store";
import { Card, fetchCard } from "../../api/cards";
import { judgeSelf } from "./util";
import * as UICONFIG from "../../config/ui";
export interface Hands {
cards: Card[];
}
// 自己增加手牌
export const meAddHandsImpl: CaseReducer<DuelState, PayloadAction<number[]>> = (
state,
action
) => {
const cards = action.payload.map((id) => {
return { meta: { id, data: {}, text: {} }, transform: {} };
});
if (state.meHands) {
state.meHands.cards = state.meHands.cards.concat(cards);
} else {
state.meHands = { cards };
}
setHandsTransform(state.meHands.cards);
};
// 增加手牌
export const addHandsImpl: CaseReducer<
DuelState,
PayloadAction<[number, number[]]>
> = (state, action) => {
const player = action.payload[0];
const hands = action.payload[1];
const selfType = state.selfType;
// 对手增加手牌
export const opAddHandsImpl: CaseReducer<DuelState, PayloadAction<number[]>> = (
state,
action
) => {
const cards = action.payload.map((id) => {
const cards = hands.map((id) => {
return { meta: { id, data: {}, text: {} }, transform: {} };
});
if (state.opHands) {
state.opHands.cards = state.opHands.cards.concat(cards);
if (judgeSelf(player, selfType)) {
if (state.meHands) {
state.meHands.cards = state.meHands.cards.concat(cards);
} else {
state.meHands = { cards };
}
setHandsTransform(state.meHands.cards);
} else {
state.opHands = { cards };
if (state.opHands) {
state.opHands.cards = state.opHands.cards.concat(cards);
} else {
state.opHands = { cards };
}
}
};
......@@ -83,6 +79,8 @@ export const meHandsCase = (builder: ActionReducerMapBuilder<DuelState>) => {
};
// 更新手牌的位置和旋转信息
//
// TODO: 兼容对方手牌
function setHandsTransform(hands: Card[]): void {
const groundShape = UICONFIG.GroundShape();
const handShape = UICONFIG.HandShape();
......
......@@ -5,12 +5,7 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { InitInfo, infoInitImpl } from "./initInfoSlice";
import {
Hands,
meAddHandsImpl,
opAddHandsImpl,
meHandsCase,
} from "./handsSlice";
import { Hands, addHandsImpl, meHandsCase } from "./handsSlice";
import { newTurnImpl } from "./turnSlice";
import { newPhaseImpl } from "./phaseSlice";
import { RootState } from "../../store";
......@@ -35,8 +30,7 @@ const duelSlice = createSlice({
state.selfType = action.payload;
},
infoInit: infoInitImpl,
meAddHands: meAddHandsImpl,
opAddHands: opAddHandsImpl,
addHands: addHandsImpl,
updateTurn: newTurnImpl,
updatePhase: newPhaseImpl,
},
......@@ -45,14 +39,8 @@ const duelSlice = createSlice({
},
});
export const {
setSelfType,
infoInit,
meAddHands,
opAddHands,
updateTurn,
updatePhase,
} = duelSlice.actions;
export const { setSelfType, infoInit, addHands, updateTurn, updatePhase } =
duelSlice.actions;
export const selectDuelHsStart = (state: RootState) => {
return state.duel.meInitInfo != null;
};
......
import { ygopro } from "../../api/ocgcore/idl/ocgcore";
import { AppDispatch } from "../../store";
import { meAddHands, opAddHands } from "../../reducers/duel/mod";
import { addHands } from "../../reducers/duel/mod";
import { fetchMeHandsMeta } from "../../reducers/duel/handsSlice";
export default (
......@@ -9,10 +9,10 @@ export default (
) => {
// FIXME: draw.player 和先后攻有关系
if (draw.player === 0) {
dispatch(meAddHands(draw.cards));
dispatch(addHands([0, draw.cards]));
dispatch(fetchMeHandsMeta(draw.cards));
} else if (draw.player === 1) {
dispatch(opAddHands(draw.cards));
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