Commit 3a6ed8d6 authored by Chunchi Che's avatar Chunchi Che

update hintSlice.ts

parent 2899ca4e
Pipeline #18820 passed with stages
in 3 minutes and 48 seconds
......@@ -2,6 +2,7 @@ import { createAsyncThunk, ActionReducerMapBuilder } from "@reduxjs/toolkit";
import { DuelState } from "./mod";
import { RootState } from "../../store";
import { fetchStrings } from "../../api/strings";
import { fetchCard } from "../../api/cards";
import { judgeSelf } from "./util";
export interface HintState {
......@@ -9,17 +10,26 @@ export interface HintState {
msg?: string;
}
export const fetchHintMeta = createAsyncThunk(
"duel/fetchHintMeta",
export const fetchCommonHintMeta = createAsyncThunk(
"duel/fetchCommonHintMeta",
async (param: [number, number]) => {
const player = param[0];
const hintData = param[1];
// TODO: 可以思考下这里怎么处理比较合理
const hintMeta =
hintData < 10000
? await fetchStrings("!system", hintData)
: await fetchStrings("!card", hintData);
const hintMeta = await fetchStrings("!system", hintData);
const response: [number, string] = [player, hintMeta];
return response;
}
);
export const fetchSelectPlaceHintMeta = createAsyncThunk(
"duel/fetchSelectPlaceHintMeta",
async (param: [number, number]) => {
const player = param[0];
const hintData = param[1];
const hintMeta = (await fetchCard(hintData)).text.name || "";
const response: [number, string] = [player, hintMeta];
return response;
......@@ -27,7 +37,7 @@ export const fetchHintMeta = createAsyncThunk(
);
export const hintCase = (builder: ActionReducerMapBuilder<DuelState>) => {
builder.addCase(fetchHintMeta.pending, (state, action) => {
builder.addCase(fetchCommonHintMeta.pending, (state, action) => {
const player = action.meta.arg[0];
const code = action.meta.arg[1];
......@@ -37,7 +47,7 @@ export const hintCase = (builder: ActionReducerMapBuilder<DuelState>) => {
state.opHint = { code };
}
});
builder.addCase(fetchHintMeta.fulfilled, (state, action) => {
builder.addCase(fetchCommonHintMeta.fulfilled, (state, action) => {
const player = action.payload[0];
const hintMeta = action.payload[1];
......@@ -46,6 +56,31 @@ export const hintCase = (builder: ActionReducerMapBuilder<DuelState>) => {
hint.msg = hintMeta;
}
});
builder.addCase(fetchSelectPlaceHintMeta.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(fetchSelectPlaceHintMeta.fulfilled, (state, action) => {
const player = action.payload[0];
const hintMeta = action.payload[1];
// TODO: 国际化文案
const hintMsg = judgeSelf(player, state)
? `请为我方的<${hintMeta}>选择位置`
: `请为对方的<${hintMeta}>选择位置`;
const hint = judgeSelf(player, state) ? state.meHint : state.opHint;
if (hint) {
hint.msg = hintMsg;
}
});
};
export const selectMeHint = (state: RootState) => state.duel.meHint;
......
import { ygopro } from "../../api/ocgcore/idl/ocgcore";
import { AppDispatch } from "../../store";
import { fetchHintMeta } from "../../reducers/duel/hintSlice";
import {
fetchCommonHintMeta,
fetchSelectPlaceHintMeta,
} from "../../reducers/duel/hintSlice";
import MsgHint = ygopro.StocGameMessage.MsgHint;
export default (hint: MsgHint, dispatch: AppDispatch) => {
const player = hint.player;
switch (hint.hint_type) {
case MsgHint.HintType.HINT_EVENT:
case MsgHint.HintType.HINT_MESSAGE:
case MsgHint.HintType.HINT_MESSAGE: {
dispatch(fetchCommonHintMeta([player, hint.hint_data]));
break;
}
case MsgHint.HintType.HINT_SELECTMSG: {
dispatch(fetchHintMeta([player, hint.hint_data]));
dispatch(fetchSelectPlaceHintMeta([player, hint.hint_data]));
break;
}
default: {
......
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