Commit a216ae8e authored by Chunchi Che's avatar Chunchi Che

fix delay

parent 954fb8be
Pipeline #21661 passed with stages
in 12 minutes and 15 seconds
import { ygopro } from "@/api";
import { useConfig } from "@/config";
import { matStore } from "@/stores";
import onMsgAttack from "./attack";
......@@ -57,8 +56,6 @@ const ActiveList = [
"select_yes_no",
];
const NeosConfig = useConfig();
export default function handleGameMsg(pb: ygopro.YgoStocMsg) {
const msg = pb.stoc_game_msg;
......@@ -104,7 +101,6 @@ export default function handleGameMsg(pb: ygopro.YgoStocMsg) {
}
case "move": {
onMsgMove(msg.move);
matStore.delay = NeosConfig.ui.moveDelay + 500;
break;
}
......@@ -230,7 +226,6 @@ export default function handleGameMsg(pb: ygopro.YgoStocMsg) {
}
case "chaining": {
onMsgChaining(msg.chaining);
matStore.delay = NeosConfig.ui.chainingDelay;
break;
}
......
......@@ -2,6 +2,7 @@
* 长连接消息事件订阅处理逻辑
*
* */
import { ygopro } from "@/api";
import { adaptStoc } from "@/api/ocgcore/ocgAdapter/adapter";
import { YgoProPacket } from "@/api/ocgcore/ocgAdapter/packet";
import { useConfig } from "@/config";
......@@ -30,11 +31,7 @@ const NeosConfig = useConfig();
export default function handleSocketMessage(e: MessageEvent) {
const packet = YgoProPacket.deserialize(e.data);
const pb = adaptStoc(packet);
// 防止MSG更新太频繁,做下控频
const delay = matStore.delay;
// 重置`delay`
matStore.delay = NeosConfig.ui.commonDelay;
const delay = handleDelay(pb);
setTimeout(() => {
switch (pb.msg) {
......@@ -112,3 +109,25 @@ export default function handleSocketMessage(e: MessageEvent) {
}
}, delay);
}
// 该函数用于控频,防止MSG更新太频繁,返回值是延迟的时间戳(毫秒)
//
// 对于一般的MSG,我们会延迟200ms执行处理逻辑;
// 当处理一些带有动画效果的MSG时,比如`MSG_MOVE`,`MSG_CHAINING`,我们会设置下一次执行处理逻辑的延迟,确保动画完整
function handleDelay(stoc: ygopro.YgoStocMsg): number {
const delay = matStore.delay;
// 重置下次`delay`
matStore.delay = NeosConfig.ui.commonDelay;
// 对特定的`MSG`,设置特化的`delay`
if (stoc.has_stoc_game_msg) {
if (stoc.stoc_game_msg.gameMsg == "move") {
matStore.delay = NeosConfig.ui.moveDelay + 500;
} else if (stoc.stoc_game_msg.gameMsg == "chaining") {
matStore.delay = NeosConfig.ui.chainingDelay;
}
}
return delay;
}
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