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

update handSlice

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