Commit 848d06a7 authored by Chunchi Che's avatar Chunchi Che

Merge branch 'fix/delay' into 'main'

Fix/delay

See merge request !192
parents 122be474 a216ae8e
Pipeline #21663 failed with stages
in 6 minutes and 2 seconds
import { ygopro } from "@/api";
import { useConfig } from "@/config";
import { matStore } from "@/stores";
import onMsgAttack from "./attack";
......@@ -57,13 +56,7 @@ const ActiveList = [
"select_yes_no",
];
const NeosConfig = useConfig();
export default function handleGameMsg(pb: ygopro.YgoStocMsg) {
// 防止MSG更新太频繁,做下控频
const delay = matStore.delay;
setTimeout(() => {
const msg = pb.stoc_game_msg;
if (ActiveList.includes(msg.gameMsg)) {
......@@ -280,5 +273,4 @@ export default function handleGameMsg(pb: ygopro.YgoStocMsg) {
break;
}
}
}, delay);
}
import { sendTimeConfirm, ygopro } from "@/api";
import { useConfig } from "@/config";
import { matStore } from "@/stores";
export default function handleTimeLimit(timeLimit: ygopro.StocTimeLimit) {
setTimeout(() => {
matStore.timeLimits.set(timeLimit.player, timeLimit.left_time);
if (matStore.isMe(timeLimit.player)) {
sendTimeConfirm();
}, useConfig().ui.commonDelay);
}
}
......@@ -2,8 +2,11 @@
* 长连接消息事件订阅处理逻辑
*
* */
import { ygopro } from "@/api";
import { adaptStoc } from "@/api/ocgcore/ocgAdapter/adapter";
import { YgoProPacket } from "@/api/ocgcore/ocgAdapter/packet";
import { useConfig } from "@/config";
import { matStore } from "@/stores";
import handleGameMsg from "./duel/gameMsg";
import handleTimeLimit from "./duel/timeLimit";
......@@ -18,6 +21,8 @@ import handleHsWatchChange from "./room/hsWatchChange";
import handleJoinGame from "./room/joinGame";
import handleTypeChange from "./room/typeChange";
const NeosConfig = useConfig();
/*
* 先将从长连接中读取到的二进制数据通过Adapter转成protobuf结构体,
* 然后再分发到各个处理函数中去处理。
......@@ -26,7 +31,9 @@ import handleTypeChange from "./room/typeChange";
export default function handleSocketMessage(e: MessageEvent) {
const packet = YgoProPacket.deserialize(e.data);
const pb = adaptStoc(packet);
const delay = handleDelay(pb);
setTimeout(() => {
switch (pb.msg) {
case "stoc_join_game": {
handleJoinGame(pb);
......@@ -100,4 +107,27 @@ export default function handleSocketMessage(e: MessageEvent) {
break;
}
}
}, 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