Commit f600e0f6 authored by Chunchi Che's avatar Chunchi Che

update hands slice

parent 7a36b504
Pipeline #18480 passed with stages
in 4 minutes and 17 seconds
import { createAsyncThunk, ActionReducerMapBuilder } from "@reduxjs/toolkit";
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 { judgeSelf, Card, InteractType, Interactivity } from "./util";
import * as UICONFIG from "../../config/ui";
import { Card } from "./util";
export interface Hands {
// 注意:手牌的位置顺序是有约束的
cards: Card[];
}
......@@ -36,12 +41,11 @@ export const handsCase = (builder: ActionReducerMapBuilder<DuelState>) => {
builder.addCase(fetchHandsMeta.fulfilled, (state, action) => {
const player = action.payload[0];
const hands = action.payload[1];
const selfType = state.selfType;
const cards = hands.map((meta) => {
return { meta, transform: {}, interactivities: [] };
});
if (judgeSelf(player, selfType)) {
if (judgeSelf(player, state)) {
if (state.meHands) {
state.meHands.cards = state.meHands.cards.concat(cards);
} else {
......@@ -79,6 +83,42 @@ function setHandsTransform(hands: Card[]): void {
});
}
// 清空手牌互动性
export const clearHandsInteractivityImpl: CaseReducer<
DuelState,
PayloadAction<number>
> = (state, action) => {
const player = action.payload;
const hands = judgeSelf(player, state) ? state.meHands : state.opHands;
if (hands) {
for (let hand of hands.cards) {
hand.interactivities = [];
}
}
};
// 添加手牌互动性
export const addHandsInteractivityImpl: CaseReducer<
DuelState,
PayloadAction<{
player: number;
index: number;
interactivity: Interactivity;
}>
> = (state, action) => {
const player = action.payload.player;
const hands = judgeSelf(player, state) ? state.meHands : state.opHands;
if (hands) {
const index = action.payload.index;
const interactivity = action.payload.interactivity;
hands.cards[index].interactivities.push(interactivity);
}
};
export const selectMeHands = (state: RootState) =>
state.duel.meHands || { cards: [] };
export const selectOpHands = (state: RootState) =>
......
......@@ -16,9 +16,8 @@ export const infoInitImpl: CaseReducer<
> = (state, action) => {
const player = action.payload[0];
const initInfo = action.payload[1];
const selfType = state.selfType;
if (judgeSelf(player, selfType)) {
if (judgeSelf(player, state)) {
state.meInitInfo = initInfo;
} else {
state.opInitInfo = initInfo;
......
......@@ -5,7 +5,12 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { InitInfo, infoInitImpl } from "./initInfoSlice";
import { Hands, handsCase } from "./handsSlice";
import {
Hands,
handsCase,
clearHandsInteractivityImpl,
addHandsInteractivityImpl,
} from "./handsSlice";
import { newTurnImpl } from "./turnSlice";
import { newPhaseImpl } from "./phaseSlice";
import { RootState } from "../../store";
......@@ -32,6 +37,10 @@ const duelSlice = createSlice({
infoInit: infoInitImpl,
updateTurn: newTurnImpl,
updatePhase: newPhaseImpl,
// 手牌相关`Reducer`
clearHandsInteractivity: clearHandsInteractivityImpl,
addHandsInteractivity: addHandsInteractivityImpl,
},
extraReducers(builder) {
handsCase(builder);
......
......@@ -4,14 +4,14 @@
* */
import { CardMeta } from "../../api/cards";
import { DuelState } from "./mod";
import { Draft } from "@reduxjs/toolkit";
/*
* 通过`player`和`selfType`判断是应该处理自己还是对手
* */
export function judgeSelf(
player: number,
selfType: number | undefined
): boolean {
export function judgeSelf(player: number, state: Draft<DuelState>): boolean {
const selfType = state.selfType;
if (selfType === 1) {
// 自己是先攻
return player === 0;
......@@ -61,8 +61,8 @@ export enum InteractType {
ACTIVATE,
}
interface Interactivity {
export interface Interactivity {
interactType: InteractType;
// 如果`interactType`是`ACTIVATE`,这个字段是对应的效果编号
activateIndex: number;
activateIndex?: number;
}
......@@ -4,7 +4,4 @@ import { AppDispatch } from "../../store";
export default (
selectIdleCmd: ygopro.StocGameMessage.MsgSelectIdleCmd,
dispatch: AppDispatch
) => {
// TODO
console.log(selectIdleCmd);
};
) => {};
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