Commit a47ee7f8 authored by Chunchi Che's avatar Chunchi Che

refactor ai predict

parent e0e5c7a6
Pipeline #28317 passed with stages
in 11 minutes and 4 seconds
// TODO: 需要将 Schema 和 Convert 逻辑分开成两个文件
import { CardMeta, ygopro } from "@/api";
import { extraCardTypes } from "@/common";
import { CardType } from "@/stores/cardStore";
......@@ -422,22 +423,25 @@ export function convertDeckCard(meta: CardMeta): Card {
export function convertCard(card: CardType, player: number): Card {
// TODO (ygo-agent): opponent's visible facedown cards (confirm_cards)
const { code, location, meta, counters } = card;
return {
code: card.code,
location: cardZoneToLocation(card.location.zone),
sequence: card.location.sequence,
controller: convertController(card.location.controller, player),
position: convertPosition(card.location.position),
overlay_sequence: convertOverlaySequence(card.location),
attribute: numberToAttribute(card.meta.data.attribute ?? 0),
race: numberToRace(card.meta.data.race ?? 0),
level: card.meta.data.level ?? 0,
counter: getCounter(card.counters),
code,
location: cardZoneToLocation(location.zone),
sequence: location.sequence,
controller: convertController(location.controller, player),
position: convertPosition(location.position),
overlay_sequence: convertOverlaySequence(location),
attribute: numberToAttribute(meta.data.attribute ?? 0),
race: numberToRace(meta.data.race ?? 0),
level: meta.data.level ?? 0,
counter: getCounter(counters),
// TODO (ygo-agent): add negated
negated: false,
attack: card.meta.data.atk ?? 0,
defense: card.meta.data.def ?? 0,
types: extraCardTypes(card.meta.data.type ?? 0).map(numberToType),
attack: meta.data.atk ?? 0,
defense: meta.data.def ?? 0,
types: extraCardTypes(meta.data.type ?? 0).map(numberToType),
};
}
......
......@@ -50,7 +50,7 @@ function computeSetDifference(a1: number[], a2: number[]): number[] {
return difference;
}
export function genInput(msg: ygopro.StocGameMessage): Input {
export function genAgentInput(msg: ygopro.StocGameMessage): Input {
// 全局信息可以从 `matStore` 里面拿
const mat = matStore;
// 卡片信息可以从 `cardStore` 里面拿
......@@ -125,6 +125,7 @@ async function sendRequest(req: PredictReq) {
throw new Error("Failed to get predict response");
}
// TODO: 下面这些逻辑不应该在这个函数里面,应该在UI组件里面实现
const preds = resp.predict_results.action_preds;
const actionIdx = argmax(preds, (r) => r.prob);
matStore.prevActionIndex = actionIdx;
......@@ -132,8 +133,9 @@ async function sendRequest(req: PredictReq) {
return pred;
}
// TODO: 这个函数的逻辑也需要拆分下
export async function sendAIPredictAsResponse(msg: ygopro.StocGameMessage) {
const input = genInput(msg);
const input = genAgentInput(msg);
const msgName = input.action_msg.data.msg_type;
const multiSelectMsgs = ["select_card", "select_tribute", "select_sum"];
......
......@@ -50,6 +50,7 @@ export interface MatState {
/** 根据自己的先后手判断是否是自己 */
isMe: (player: number) => boolean;
// 下面其中一些貌似可以封装成为`AgentInfo`
turnCount: number;
duelId: string;
agentIndex: number;
......
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