Commit a1013c25 authored by Chunchi Che's avatar Chunchi Che

add updateCardData

parent bb56f260
Pipeline #20805 passed with stages
in 33 minutes and 24 seconds
......@@ -8,6 +8,7 @@ import { CardMeta } from "../../api/cards";
import { ygopro } from "../../api/ocgcore/idl/ocgcore";
import { fetchCard } from "../../api/cards";
import { DuelState } from "./mod";
import UpdateDataAction = ygopro.StocGameMessage.MsgUpdateData.Action;
export type DuelReducer<T> = CaseReducer<DuelState, PayloadAction<T>>;
......@@ -283,3 +284,45 @@ export function setPosition<T extends DuelFieldState>(
target.location.position = position;
}
}
export function updateCardData<T extends DuelFieldState>(
state: T | undefined,
actions: UpdateDataAction[]
) {
for (const action of actions) {
const payload = action.toObject();
const sequence = payload.location?.sequence;
if (typeof sequence !== "undefined") {
const target = state?.inner.find((_, idx) => idx == sequence);
if (target && target.occupant) {
const occupant = target.occupant;
// 目前只更新以下字段
if (payload.code !== undefined) {
occupant.id = payload.code;
occupant.text.id = payload.code;
}
if (payload.location !== undefined) {
target.location.position = payload.location.position;
}
if (payload.type_ !== undefined) {
occupant.data.type = payload.type_;
}
if (payload.level !== undefined) {
occupant.data.level = payload.level;
}
if (payload.attribute !== undefined) {
occupant.data.attribute = payload.attribute;
}
if (payload.race !== undefined) {
occupant.data.race = payload.race;
}
if (payload.attack !== undefined) {
occupant.data.atk = payload.attack;
}
if (payload.defense !== undefined) {
occupant.data.def = payload.defense;
}
}
}
}
}
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