Commit ac124f5c authored by Chunchi Che's avatar Chunchi Che

add hint reducer

parent 54891cf8
Pipeline #18788 passed with stages
in 4 minutes and 17 seconds
import { createAsyncThunk, ActionReducerMapBuilder } from "@reduxjs/toolkit";
import { DuelState } from "./mod";
import { RootState } from "../../store";
import { fetchStrings } from "../../api/strings";
import { judgeSelf } from "./util";
export interface HintState {
code: number;
msg?: string;
}
export const fetchHintMeta = createAsyncThunk(
"duel/fetchHintMeta",
async (param: [number, number]) => {
const player = param[0];
const hintData = param[1];
const hintMeta = await fetchStrings("!system", hintData);
const response: [number, string] = [player, hintMeta];
return response;
}
);
export const hintCase = (builder: ActionReducerMapBuilder<DuelState>) => {
builder.addCase(fetchHintMeta.pending, (state, action) => {
const player = action.meta.arg[0];
const code = action.meta.arg[1];
if (judgeSelf(player, state)) {
state.meHint = { code };
} else {
state.opHint = { code };
}
});
builder.addCase(fetchHintMeta.fulfilled, (state, action) => {
const player = action.payload[0];
const hintMeta = action.payload[1];
const hint = judgeSelf(player, state) ? state.meHint : state.opHint;
if (hint) {
hint.msg = hintMeta;
}
});
};
export const selectMeHint = (state: RootState) => state.duel.meHint || "";
export const selectOpHint = (state: RootState) => state.duel.opHint || "";
......@@ -15,6 +15,7 @@ import {
import { newTurnImpl } from "./turnSlice";
import { newPhaseImpl } from "./phaseSlice";
import { RootState } from "../../store";
import { HintState, hintCase } from "./hint";
import {
ModalState,
setCardModalIsOpenImpl,
......@@ -31,6 +32,8 @@ export interface DuelState {
opHands?: Hands; // 对手的手牌
meTimeLimit?: TimeLimit; // 自己的计时
opTimeLimit?: TimeLimit; // 对手的计时
meHint?: HintState; // 自己的提示
opHint?: HintState; // 对手的提示
currentPlayer?: number; // 当前的操作方
currentPhase?: string; // 当前的阶段
......@@ -68,6 +71,7 @@ const duelSlice = createSlice({
},
extraReducers(builder) {
handsCase(builder);
hintCase(builder);
},
});
......
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