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

update hintSlice.ts

parent 2899ca4e
...@@ -2,6 +2,7 @@ import { createAsyncThunk, ActionReducerMapBuilder } from "@reduxjs/toolkit"; ...@@ -2,6 +2,7 @@ import { createAsyncThunk, ActionReducerMapBuilder } from "@reduxjs/toolkit";
import { DuelState } from "./mod"; import { DuelState } from "./mod";
import { RootState } from "../../store"; import { RootState } from "../../store";
import { fetchStrings } from "../../api/strings"; import { fetchStrings } from "../../api/strings";
import { fetchCard } from "../../api/cards";
import { judgeSelf } from "./util"; import { judgeSelf } from "./util";
export interface HintState { export interface HintState {
...@@ -9,17 +10,26 @@ export interface HintState { ...@@ -9,17 +10,26 @@ export interface HintState {
msg?: string; msg?: string;
} }
export const fetchHintMeta = createAsyncThunk( export const fetchCommonHintMeta = createAsyncThunk(
"duel/fetchHintMeta", "duel/fetchCommonHintMeta",
async (param: [number, number]) => { async (param: [number, number]) => {
const player = param[0]; const player = param[0];
const hintData = param[1]; const hintData = param[1];
// TODO: 可以思考下这里怎么处理比较合理 const hintMeta = await fetchStrings("!system", hintData);
const hintMeta = const response: [number, string] = [player, hintMeta];
hintData < 10000
? await fetchStrings("!system", hintData) return response;
: await fetchStrings("!card", hintData); }
);
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]; const response: [number, string] = [player, hintMeta];
return response; return response;
...@@ -27,7 +37,7 @@ export const fetchHintMeta = createAsyncThunk( ...@@ -27,7 +37,7 @@ export const fetchHintMeta = createAsyncThunk(
); );
export const hintCase = (builder: ActionReducerMapBuilder<DuelState>) => { 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 player = action.meta.arg[0];
const code = action.meta.arg[1]; const code = action.meta.arg[1];
...@@ -37,7 +47,7 @@ export const hintCase = (builder: ActionReducerMapBuilder<DuelState>) => { ...@@ -37,7 +47,7 @@ export const hintCase = (builder: ActionReducerMapBuilder<DuelState>) => {
state.opHint = { code }; state.opHint = { code };
} }
}); });
builder.addCase(fetchHintMeta.fulfilled, (state, action) => { builder.addCase(fetchCommonHintMeta.fulfilled, (state, action) => {
const player = action.payload[0]; const player = action.payload[0];
const hintMeta = action.payload[1]; const hintMeta = action.payload[1];
...@@ -46,6 +56,31 @@ export const hintCase = (builder: ActionReducerMapBuilder<DuelState>) => { ...@@ -46,6 +56,31 @@ export const hintCase = (builder: ActionReducerMapBuilder<DuelState>) => {
hint.msg = hintMeta; 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; export const selectMeHint = (state: RootState) => state.duel.meHint;
......
import { ygopro } from "../../api/ocgcore/idl/ocgcore"; import { ygopro } from "../../api/ocgcore/idl/ocgcore";
import { AppDispatch } from "../../store"; import { AppDispatch } from "../../store";
import { fetchHintMeta } from "../../reducers/duel/hintSlice"; import {
fetchCommonHintMeta,
fetchSelectPlaceHintMeta,
} from "../../reducers/duel/hintSlice";
import MsgHint = ygopro.StocGameMessage.MsgHint; import MsgHint = ygopro.StocGameMessage.MsgHint;
export default (hint: MsgHint, dispatch: AppDispatch) => { export default (hint: MsgHint, dispatch: AppDispatch) => {
const player = hint.player; const player = hint.player;
switch (hint.hint_type) { switch (hint.hint_type) {
case MsgHint.HintType.HINT_EVENT: 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: { case MsgHint.HintType.HINT_SELECTMSG: {
dispatch(fetchHintMeta([player, hint.hint_data])); dispatch(fetchSelectPlaceHintMeta([player, hint.hint_data]));
break; break;
} }
default: { 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