Commit 680f4298 authored by Chunchi Che's avatar Chunchi Che

handle selfType and player correctly

parent 426b4473
Pipeline #18322 passed with stages
in 2 minutes and 40 seconds
import { PayloadAction, CaseReducer } from "@reduxjs/toolkit";
import { DuelState } from "./mod";
import { judgeSelf } from "./util";
export interface InitInfo {
playerType?: string;
masterRule?: string;
life: number;
deckSize: number;
......@@ -10,17 +10,17 @@ export interface InitInfo {
}
// 更新自己的初始生命值,卡组信息
export const meInfoInitImpl: CaseReducer<DuelState, PayloadAction<InitInfo>> = (
state,
action
) => {
state.meInitInfo = action.payload;
};
export const infoInitImpl: CaseReducer<
DuelState,
PayloadAction<[number, InitInfo]>
> = (state, action) => {
const player = action.payload[0];
const initInfo = action.payload[1];
const selfType = state.selfType;
// 更新对手的初始生命值,卡组信息
export const opInfoInitImpl: CaseReducer<DuelState, PayloadAction<InitInfo>> = (
state,
action
) => {
state.opInitInfo = action.payload;
if (judgeSelf(player, selfType)) {
state.meInitInfo = initInfo;
} else {
state.opInitInfo = initInfo;
}
};
......@@ -3,8 +3,8 @@
*
* */
import { createSlice } from "@reduxjs/toolkit";
import { InitInfo, meInfoInitImpl, opInfoInitImpl } from "./initInfoSlice";
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { InitInfo, infoInitImpl } from "./initInfoSlice";
import {
Hands,
meAddHandsImpl,
......@@ -16,6 +16,7 @@ import { newPhaseImpl } from "./phaseSlice";
import { RootState } from "../../store";
export interface DuelState {
selfType?: number;
meInitInfo?: InitInfo; // 自己的初始状态
opInitInfo?: InitInfo; // 对手的初始状态
meHands?: Hands; // 自己的手牌
......@@ -30,8 +31,10 @@ const duelSlice = createSlice({
name: "duel",
initialState,
reducers: {
meInfoInit: meInfoInitImpl,
opInfoInit: opInfoInitImpl,
setSelfType: (state, action: PayloadAction<number>) => {
state.selfType = action.payload;
},
infoInit: infoInitImpl,
meAddHands: meAddHandsImpl,
opAddHands: opAddHandsImpl,
updateTurn: newTurnImpl,
......@@ -43,8 +46,8 @@ const duelSlice = createSlice({
});
export const {
meInfoInit,
opInfoInit,
setSelfType,
infoInit,
meAddHands,
opAddHands,
updateTurn,
......
/*
* 对局内状态更新逻辑的一些共用函数
*
* */
/*
* 通过`player`和`selfType`判断是应该处理自己还是对手
* */
export function judgeSelf(
player: number,
selfType: number | undefined
): boolean {
if (selfType === 1) {
// 自己是先攻
return player === 0;
} else if (selfType === 2) {
// 自己是后攻
return player === 1;
} else {
// currently never reach
return false;
}
}
import { ygopro } from "../../api/ocgcore/idl/ocgcore";
import { AppDispatch } from "../../store";
import { meInfoInit, opInfoInit } from "../../reducers/duel/mod";
import { infoInit, setSelfType } from "../../reducers/duel/mod";
export default (
start: ygopro.StocGameMessage.MsgStart,
dispatch: AppDispatch
) => {
dispatch(setSelfType(start.playerType));
dispatch(
meInfoInit({
playerType: start.playerType.toString(),
life: start.life1,
deckSize: start.deckSize1,
extraSize: start.extraSize1,
})
infoInit([
0,
{
life: start.life1,
deckSize: start.deckSize1,
extraSize: start.extraSize1,
},
])
);
dispatch(
opInfoInit({
life: start.life2,
deckSize: start.deckSize2,
extraSize: start.extraSize2,
})
infoInit([
1,
{
life: start.life2,
deckSize: start.deckSize2,
extraSize: start.extraSize2,
},
])
);
};
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