Commit e47d0dd2 authored by chechunchi's avatar chechunchi

migrate to async/await

parent c4303824
Pipeline #21729 passed with stages
in 13 minutes and 23 seconds
import { ygopro } from "@/api"; import { ygopro } from "@/api";
import { sleep } from "@/infra";
import { fetchEsHintMeta, matStore } from "@/stores"; import { fetchEsHintMeta, matStore } from "@/stores";
export default (attack: ygopro.StocGameMessage.MsgAttack) => { export default async (attack: ygopro.StocGameMessage.MsgAttack) => {
fetchEsHintMeta({ fetchEsHintMeta({
originMsg: "「[?]」攻击时", originMsg: "「[?]」攻击时",
location: attack.attacker_location, location: attack.attacker_location,
...@@ -16,7 +17,8 @@ export default (attack: ygopro.StocGameMessage.MsgAttack) => { ...@@ -16,7 +17,8 @@ export default (attack: ygopro.StocGameMessage.MsgAttack) => {
if (attack.direct_attack) { if (attack.direct_attack) {
attacker.directAttack = true; attacker.directAttack = true;
setTimeout(() => (attacker.directAttack = false), 500); await sleep(500);
attacker.directAttack = false;
} else { } else {
const target = matStore const target = matStore
.in(attack.target_location.location) .in(attack.target_location.location)
...@@ -30,7 +32,8 @@ export default (attack: ygopro.StocGameMessage.MsgAttack) => { ...@@ -30,7 +32,8 @@ export default (attack: ygopro.StocGameMessage.MsgAttack) => {
...target, ...target,
}; };
setTimeout(() => (attacker.attackTarget = undefined), 500); await sleep(500);
attacker.attackTarget = undefined;
} }
} }
} }
......
import { ygopro } from "@/api"; import { ygopro } from "@/api";
import { useConfig } from "@/config"; import { useConfig } from "@/config";
import { sleep } from "@/infra";
import { fetchEsHintMeta, matStore } from "@/stores"; import { fetchEsHintMeta, matStore } from "@/stores";
export default (chaining: ygopro.StocGameMessage.MsgChaining) => { export default async (chaining: ygopro.StocGameMessage.MsgChaining) => {
fetchEsHintMeta({ fetchEsHintMeta({
originMsg: "「[?]」被发动时", originMsg: "「[?]」被发动时",
cardID: chaining.code, cardID: chaining.code,
...@@ -10,8 +11,7 @@ export default (chaining: ygopro.StocGameMessage.MsgChaining) => { ...@@ -10,8 +11,7 @@ export default (chaining: ygopro.StocGameMessage.MsgChaining) => {
matStore.setChaining(chaining.location, chaining.code, true); matStore.setChaining(chaining.location, chaining.code, true);
setTimeout(() => { await sleep(useConfig().ui.chainingDelay);
matStore.setChaining(chaining.location, chaining.code, false); matStore.setChaining(chaining.location, chaining.code, false);
// TODO: set chained // TODO: set chained
}, useConfig().ui.chainingDelay);
}; };
...@@ -56,7 +56,7 @@ const ActiveList = [ ...@@ -56,7 +56,7 @@ const ActiveList = [
"select_yes_no", "select_yes_no",
]; ];
export default function handleGameMsg(pb: ygopro.YgoStocMsg) { export default async function handleGameMsg(pb: ygopro.YgoStocMsg) {
const msg = pb.stoc_game_msg; const msg = pb.stoc_game_msg;
if (ActiveList.includes(msg.gameMsg)) { if (ActiveList.includes(msg.gameMsg)) {
...@@ -100,7 +100,7 @@ export default function handleGameMsg(pb: ygopro.YgoStocMsg) { ...@@ -100,7 +100,7 @@ export default function handleGameMsg(pb: ygopro.YgoStocMsg) {
break; break;
} }
case "move": { case "move": {
onMsgMove(msg.move); await onMsgMove(msg.move);
break; break;
} }
...@@ -215,7 +215,7 @@ export default function handleGameMsg(pb: ygopro.YgoStocMsg) { ...@@ -215,7 +215,7 @@ export default function handleGameMsg(pb: ygopro.YgoStocMsg) {
break; break;
} }
case "attack": { case "attack": {
onMsgAttack(msg.attack); await onMsgAttack(msg.attack);
break; break;
} }
...@@ -225,7 +225,7 @@ export default function handleGameMsg(pb: ygopro.YgoStocMsg) { ...@@ -225,7 +225,7 @@ export default function handleGameMsg(pb: ygopro.YgoStocMsg) {
break; break;
} }
case "chaining": { case "chaining": {
onMsgChaining(msg.chaining); await onMsgChaining(msg.chaining);
break; break;
} }
......
...@@ -4,6 +4,7 @@ import { ygopro } from "@/api"; ...@@ -4,6 +4,7 @@ import { ygopro } from "@/api";
import { fetchOverlayMeta, store } from "@/stores"; import { fetchOverlayMeta, store } from "@/stores";
type MsgMove = ygopro.StocGameMessage.MsgMove; type MsgMove = ygopro.StocGameMessage.MsgMove;
import { useConfig } from "@/config"; import { useConfig } from "@/config";
import { sleep } from "@/infra";
import { REASON_MATERIAL } from "../../common"; import { REASON_MATERIAL } from "../../common";
...@@ -12,7 +13,7 @@ const NeosConfig = useConfig(); ...@@ -12,7 +13,7 @@ const NeosConfig = useConfig();
const OVERLAY_STACK: { uuid: string; code: number; sequence: number }[] = []; const OVERLAY_STACK: { uuid: string; code: number; sequence: number }[] = [];
export default (move: MsgMove) => { export default async (move: MsgMove) => {
const code = move.code; const code = move.code;
const from = move.from; const from = move.from;
const to = move.to; const to = move.to;
...@@ -79,12 +80,8 @@ export default (move: MsgMove) => { ...@@ -79,12 +80,8 @@ export default (move: MsgMove) => {
matStore.in(to.location).of(to.controler)[to.sequence].uuid = uuid; matStore.in(to.location).of(to.controler)[to.sequence].uuid = uuid;
} }
setTimeout( await sleep(NeosConfig.ui.moveDelay);
() => matStore.in(to.location).of(to.controler)[to.sequence].focus = false;
(matStore.in(to.location).of(to.controler)[to.sequence].focus =
false),
NeosConfig.ui.moveDelay
);
break; break;
} }
case ygopro.CardZone.REMOVED: case ygopro.CardZone.REMOVED:
...@@ -112,12 +109,11 @@ export default (move: MsgMove) => { ...@@ -112,12 +109,11 @@ export default (move: MsgMove) => {
true true
); );
setTimeout(() => { await sleep(NeosConfig.ui.moveDelay);
// 因为手牌可能会洗牌,sequence就对不上了,所以这里把所有手牌的focus字段都设置成false // 因为手牌可能会洗牌,sequence就对不上了,所以这里把所有手牌的focus字段都设置成false
for (const hand of matStore.in(to.location).of(to.controler)) { for (const hand of matStore.in(to.location).of(to.controler)) {
hand.focus = false; hand.focus = false;
} }
}, NeosConfig.ui.moveDelay);
} }
break; break;
} }
......
...@@ -6,6 +6,7 @@ import { ygopro } from "@/api"; ...@@ -6,6 +6,7 @@ import { ygopro } from "@/api";
import { adaptStoc } from "@/api/ocgcore/ocgAdapter/adapter"; import { adaptStoc } from "@/api/ocgcore/ocgAdapter/adapter";
import { YgoProPacket } from "@/api/ocgcore/ocgAdapter/packet"; import { YgoProPacket } from "@/api/ocgcore/ocgAdapter/packet";
import { useConfig } from "@/config"; import { useConfig } from "@/config";
import { sleep } from "@/infra";
import { matStore } from "@/stores"; import { matStore } from "@/stores";
import handleGameMsg from "./duel/gameMsg"; import handleGameMsg from "./duel/gameMsg";
...@@ -33,81 +34,81 @@ export default async function handleSocketMessage(e: MessageEvent) { ...@@ -33,81 +34,81 @@ export default async function handleSocketMessage(e: MessageEvent) {
const pb = adaptStoc(packet); const pb = adaptStoc(packet);
const delay = handleDelay(pb); const delay = handleDelay(pb);
setTimeout(() => { await sleep(delay);
switch (pb.msg) {
case "stoc_join_game": { switch (pb.msg) {
handleJoinGame(pb); case "stoc_join_game": {
handleJoinGame(pb);
break;
} break;
case "stoc_chat": { }
handleChat(pb); case "stoc_chat": {
handleChat(pb);
break;
} break;
case "stoc_hs_player_change": { }
handleHsPlayerChange(pb); case "stoc_hs_player_change": {
handleHsPlayerChange(pb);
break;
} break;
case "stoc_hs_watch_change": { }
handleHsWatchChange(pb); case "stoc_hs_watch_change": {
handleHsWatchChange(pb);
break;
} break;
case "stoc_hs_player_enter": {
handleHsPlayerEnter(pb);
break;
}
case "stoc_type_change": {
handleTypeChange(pb);
break;
}
case "stoc_select_hand": {
handleSelectHand(pb);
break;
}
case "stoc_hand_result": {
// TODO
console.log("TODO: handle STOC HandResult.");
break;
}
case "stoc_select_tp": {
handleSelectTp(pb);
break;
}
case "stoc_deck_count": {
handleDeckCount(pb);
break;
}
case "stoc_duel_start": {
handleDuelStart(pb);
break;
}
case "stoc_game_msg": {
handleGameMsg(pb);
break;
}
case "stoc_time_limit": {
handleTimeLimit(pb.stoc_time_limit);
break;
}
default: {
console.log(packet);
break;
}
} }
}, delay); case "stoc_hs_player_enter": {
handleHsPlayerEnter(pb);
break;
}
case "stoc_type_change": {
handleTypeChange(pb);
break;
}
case "stoc_select_hand": {
handleSelectHand(pb);
break;
}
case "stoc_hand_result": {
// TODO
console.log("TODO: handle STOC HandResult.");
break;
}
case "stoc_select_tp": {
handleSelectTp(pb);
break;
}
case "stoc_deck_count": {
handleDeckCount(pb);
break;
}
case "stoc_duel_start": {
handleDuelStart(pb);
break;
}
case "stoc_game_msg": {
await handleGameMsg(pb);
break;
}
case "stoc_time_limit": {
handleTimeLimit(pb.stoc_time_limit);
break;
}
default: {
console.log(packet);
break;
}
}
} }
// 该函数用于控频,防止MSG更新太频繁,返回值是延迟的时间戳(毫秒) // 该函数用于控频,防止MSG更新太频繁,返回值是延迟的时间戳(毫秒)
......
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