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 { PayloadAction, CaseReducer } from "@reduxjs/toolkit";
import { DuelState } from "./mod"; import { DuelState } from "./mod";
import { judgeSelf } from "./util";
export interface InitInfo { export interface InitInfo {
playerType?: string;
masterRule?: string; masterRule?: string;
life: number; life: number;
deckSize: number; deckSize: number;
...@@ -10,17 +10,17 @@ export interface InitInfo { ...@@ -10,17 +10,17 @@ export interface InitInfo {
} }
// 更新自己的初始生命值,卡组信息 // 更新自己的初始生命值,卡组信息
export const meInfoInitImpl: CaseReducer<DuelState, PayloadAction<InitInfo>> = ( export const infoInitImpl: CaseReducer<
state, DuelState,
action PayloadAction<[number, InitInfo]>
) => { > = (state, action) => {
state.meInitInfo = action.payload; const player = action.payload[0];
}; const initInfo = action.payload[1];
const selfType = state.selfType;
// 更新对手的初始生命值,卡组信息 if (judgeSelf(player, selfType)) {
export const opInfoInitImpl: CaseReducer<DuelState, PayloadAction<InitInfo>> = ( state.meInitInfo = initInfo;
state, } else {
action state.opInitInfo = initInfo;
) => { }
state.opInitInfo = action.payload;
}; };
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
* *
* */ * */
import { createSlice } from "@reduxjs/toolkit"; import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { InitInfo, meInfoInitImpl, opInfoInitImpl } from "./initInfoSlice"; import { InitInfo, infoInitImpl } from "./initInfoSlice";
import { import {
Hands, Hands,
meAddHandsImpl, meAddHandsImpl,
...@@ -16,6 +16,7 @@ import { newPhaseImpl } from "./phaseSlice"; ...@@ -16,6 +16,7 @@ import { newPhaseImpl } from "./phaseSlice";
import { RootState } from "../../store"; import { RootState } from "../../store";
export interface DuelState { export interface DuelState {
selfType?: number;
meInitInfo?: InitInfo; // 自己的初始状态 meInitInfo?: InitInfo; // 自己的初始状态
opInitInfo?: InitInfo; // 对手的初始状态 opInitInfo?: InitInfo; // 对手的初始状态
meHands?: Hands; // 自己的手牌 meHands?: Hands; // 自己的手牌
...@@ -30,8 +31,10 @@ const duelSlice = createSlice({ ...@@ -30,8 +31,10 @@ const duelSlice = createSlice({
name: "duel", name: "duel",
initialState, initialState,
reducers: { reducers: {
meInfoInit: meInfoInitImpl, setSelfType: (state, action: PayloadAction<number>) => {
opInfoInit: opInfoInitImpl, state.selfType = action.payload;
},
infoInit: infoInitImpl,
meAddHands: meAddHandsImpl, meAddHands: meAddHandsImpl,
opAddHands: opAddHandsImpl, opAddHands: opAddHandsImpl,
updateTurn: newTurnImpl, updateTurn: newTurnImpl,
...@@ -43,8 +46,8 @@ const duelSlice = createSlice({ ...@@ -43,8 +46,8 @@ const duelSlice = createSlice({
}); });
export const { export const {
meInfoInit, setSelfType,
opInfoInit, infoInit,
meAddHands, meAddHands,
opAddHands, opAddHands,
updateTurn, 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 { ygopro } from "../../api/ocgcore/idl/ocgcore";
import { AppDispatch } from "../../store"; import { AppDispatch } from "../../store";
import { meInfoInit, opInfoInit } from "../../reducers/duel/mod"; import { infoInit, setSelfType } from "../../reducers/duel/mod";
export default ( export default (
start: ygopro.StocGameMessage.MsgStart, start: ygopro.StocGameMessage.MsgStart,
dispatch: AppDispatch dispatch: AppDispatch
) => { ) => {
dispatch(setSelfType(start.playerType));
dispatch( dispatch(
meInfoInit({ infoInit([
playerType: start.playerType.toString(), 0,
life: start.life1, {
deckSize: start.deckSize1, life: start.life1,
extraSize: start.extraSize1, deckSize: start.deckSize1,
}) extraSize: start.extraSize1,
},
])
); );
dispatch( dispatch(
opInfoInit({ infoInit([
life: start.life2, 1,
deckSize: start.deckSize2, {
extraSize: start.extraSize2, 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