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

Merge branch 'fix/delay' into 'main'

Fix/delay

See merge request mycard/Neos!192
parents 122be474 a216ae8e
Pipeline #21663 failed with stages
in 6 minutes and 2 seconds
import { ygopro } from "@/api"; import { ygopro } from "@/api";
import { useConfig } from "@/config";
import { matStore } from "@/stores"; import { matStore } from "@/stores";
import onMsgAttack from "./attack"; import onMsgAttack from "./attack";
...@@ -57,228 +56,221 @@ const ActiveList = [ ...@@ -57,228 +56,221 @@ const ActiveList = [
"select_yes_no", "select_yes_no",
]; ];
const NeosConfig = useConfig();
export default function handleGameMsg(pb: ygopro.YgoStocMsg) { export default function handleGameMsg(pb: ygopro.YgoStocMsg) {
// 防止MSG更新太频繁,做下控频 const msg = pb.stoc_game_msg;
const delay = matStore.delay;
if (ActiveList.includes(msg.gameMsg)) {
setTimeout(() => { matStore.waiting = false;
const msg = pb.stoc_game_msg; }
if (ActiveList.includes(msg.gameMsg)) { switch (msg.gameMsg) {
matStore.waiting = false; case "start": {
} onMsgStart(msg.start);
switch (msg.gameMsg) { break;
case "start": { }
onMsgStart(msg.start); case "draw": {
onMsgDraw(msg.draw);
break;
} break;
case "draw": { }
onMsgDraw(msg.draw); case "new_turn": {
onMsgNewTurn(msg.new_turn);
break;
} break;
case "new_turn": { }
onMsgNewTurn(msg.new_turn); case "new_phase": {
onMsgNewPhase(msg.new_phase);
break;
} break;
case "new_phase": { }
onMsgNewPhase(msg.new_phase); case "hint": {
onMsgHint(msg.hint);
break;
} break;
case "hint": { }
onMsgHint(msg.hint); case "select_idle_cmd": {
onMsgSelectIdleCmd(msg.select_idle_cmd);
break;
} break;
case "select_idle_cmd": { }
onMsgSelectIdleCmd(msg.select_idle_cmd); case "select_place": {
onMsgSelectPlace(msg.select_place);
break;
} break;
case "select_place": { }
onMsgSelectPlace(msg.select_place); case "move": {
onMsgMove(msg.move);
break;
} break;
case "move": { }
onMsgMove(msg.move); case "select_card": {
onMsgSelectCard(msg.select_card);
break;
} break;
case "select_card": { }
onMsgSelectCard(msg.select_card); case "select_chain": {
onMsgSelectChain(msg.select_chain);
break;
} break;
case "select_chain": { }
onMsgSelectChain(msg.select_chain); case "select_effect_yn": {
onMsgSelectEffectYn(msg.select_effect_yn);
break;
} break;
case "select_effect_yn": { }
onMsgSelectEffectYn(msg.select_effect_yn); case "select_position": {
onMsgSelectPosition(msg.select_position);
break;
} break;
case "select_position": { }
onMsgSelectPosition(msg.select_position); case "select_option": {
onMsgSelectOption(msg.select_option);
break;
} break;
case "select_option": { }
onMsgSelectOption(msg.select_option); case "shuffle_hand": {
onMsgShuffleHand(msg.shuffle_hand);
break;
} break;
case "shuffle_hand": { }
onMsgShuffleHand(msg.shuffle_hand); case "select_battle_cmd": {
onMsgSelectBattleCmd(msg.select_battle_cmd);
break;
} break;
case "select_battle_cmd": { }
onMsgSelectBattleCmd(msg.select_battle_cmd); case "pos_change": {
onMsgPosChange(msg.pos_change);
break;
} break;
case "pos_change": { }
onMsgPosChange(msg.pos_change); case "select_unselect_card": {
onMsgSelectUnselectCard(msg.select_unselect_card);
break;
} break;
case "select_unselect_card": { }
onMsgSelectUnselectCard(msg.select_unselect_card); case "select_yes_no": {
onMsgSelectYesNo(msg.select_yes_no);
break;
} break;
case "select_yes_no": { }
onMsgSelectYesNo(msg.select_yes_no); case "update_hp": {
onMsgUpdateHp(msg.update_hp);
break;
} break;
case "update_hp": { }
onMsgUpdateHp(msg.update_hp); case "win": {
onMsgWin(msg.win);
break;
} break;
case "win": { }
onMsgWin(msg.win); case "wait": {
onMsgWait(msg.wait);
break;
} break;
case "wait": { }
onMsgWait(msg.wait); case "update_data": {
onMsgUpdateData(msg.update_data);
break;
} break;
case "update_data": { }
onMsgUpdateData(msg.update_data); case "reload_field": {
onMsgReloadField(msg.reload_field);
break;
} break;
case "reload_field": { }
onMsgReloadField(msg.reload_field); case "select_sum": {
onMsgSelectSum(msg.select_sum);
break;
} break;
case "select_sum": { }
onMsgSelectSum(msg.select_sum); case "select_tribute": {
onMsgSelectTribute(msg.select_tribute);
break;
} break;
case "select_tribute": { }
onMsgSelectTribute(msg.select_tribute); case "update_counter": {
onMsgUpdateCounter(msg.update_counter);
break;
} break;
case "update_counter": { }
onMsgUpdateCounter(msg.update_counter); case "select_counter": {
onMsgSelectCounter(msg.select_counter);
break;
} break;
case "select_counter": { }
onMsgSelectCounter(msg.select_counter); case "sort_card": {
onMsgSortCard(msg.sort_card);
break;
} break;
case "sort_card": { }
onMsgSortCard(msg.sort_card); case "set": {
onMsgSet(msg.set);
break;
} break;
case "set": { }
onMsgSet(msg.set); case "swap": {
onMsgSwap(msg.swap);
break;
} break;
case "swap": { }
onMsgSwap(msg.swap); case "attack": {
onMsgAttack(msg.attack);
break;
} break;
case "attack": { }
onMsgAttack(msg.attack); case "attack_disable": {
onMsgAttackDisable(msg.attack_disable);
break;
} break;
case "attack_disable": { }
onMsgAttackDisable(msg.attack_disable); case "chaining": {
onMsgChaining(msg.chaining);
break;
} break;
case "chaining": { }
onMsgChaining(msg.chaining); case "chain_solved": {
onMsgChainSolved(msg.chain_solved);
break;
} break;
case "chain_solved": { }
onMsgChainSolved(msg.chain_solved); case "summoning": {
onMsgSummoning(msg.summoning);
break;
} break;
case "summoning": { }
onMsgSummoning(msg.summoning); case "summoned": {
onMsgSummoned(msg.summoned);
break;
} break;
case "summoned": { }
onMsgSummoned(msg.summoned); case "flip_summoning": {
onMsgFlipSummoning(msg.flip_summoning);
break;
} break;
case "flip_summoning": { }
onMsgFlipSummoning(msg.flip_summoning); case "flip_summoned": {
onMsgFilpSummoned(msg.flip_summoned);
break;
} break;
case "flip_summoned": { }
onMsgFilpSummoned(msg.flip_summoned); case "sp_summoning": {
onMsgSpSummoning(msg.sp_summoning);
break;
} break;
case "sp_summoning": { }
onMsgSpSummoning(msg.sp_summoning); case "sp_summoned": {
onMsgSpSummoned(msg.sp_summoned);
break;
} break;
case "sp_summoned": { }
onMsgSpSummoned(msg.sp_summoned); case "unimplemented": {
onUnimplemented(msg.unimplemented);
break;
} break;
case "unimplemented": { }
onUnimplemented(msg.unimplemented); default: {
break;
break; }
} }
default: {
break;
}
}
}, delay);
} }
import { sendTimeConfirm, ygopro } from "@/api"; import { sendTimeConfirm, ygopro } from "@/api";
import { useConfig } from "@/config";
import { matStore } from "@/stores"; import { matStore } from "@/stores";
export default function handleTimeLimit(timeLimit: ygopro.StocTimeLimit) { export default function handleTimeLimit(timeLimit: ygopro.StocTimeLimit) {
setTimeout(() => { matStore.timeLimits.set(timeLimit.player, timeLimit.left_time);
matStore.timeLimits.set(timeLimit.player, timeLimit.left_time); if (matStore.isMe(timeLimit.player)) {
sendTimeConfirm(); sendTimeConfirm();
}, useConfig().ui.commonDelay); }
} }
...@@ -2,8 +2,11 @@ ...@@ -2,8 +2,11 @@
* 长连接消息事件订阅处理逻辑 * 长连接消息事件订阅处理逻辑
* *
* */ * */
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 { matStore } from "@/stores";
import handleGameMsg from "./duel/gameMsg"; import handleGameMsg from "./duel/gameMsg";
import handleTimeLimit from "./duel/timeLimit"; import handleTimeLimit from "./duel/timeLimit";
...@@ -18,6 +21,8 @@ import handleHsWatchChange from "./room/hsWatchChange"; ...@@ -18,6 +21,8 @@ import handleHsWatchChange from "./room/hsWatchChange";
import handleJoinGame from "./room/joinGame"; import handleJoinGame from "./room/joinGame";
import handleTypeChange from "./room/typeChange"; import handleTypeChange from "./room/typeChange";
const NeosConfig = useConfig();
/* /*
* 先将从长连接中读取到的二进制数据通过Adapter转成protobuf结构体, * 先将从长连接中读取到的二进制数据通过Adapter转成protobuf结构体,
* 然后再分发到各个处理函数中去处理。 * 然后再分发到各个处理函数中去处理。
...@@ -26,78 +31,103 @@ import handleTypeChange from "./room/typeChange"; ...@@ -26,78 +31,103 @@ import handleTypeChange from "./room/typeChange";
export default function handleSocketMessage(e: MessageEvent) { export default function handleSocketMessage(e: MessageEvent) {
const packet = YgoProPacket.deserialize(e.data); const packet = YgoProPacket.deserialize(e.data);
const pb = adaptStoc(packet); const pb = adaptStoc(packet);
const delay = handleDelay(pb);
switch (pb.msg) {
case "stoc_join_game": { setTimeout(() => {
handleJoinGame(pb); switch (pb.msg) {
case "stoc_join_game": {
break; handleJoinGame(pb);
}
case "stoc_chat": { break;
handleChat(pb); }
case "stoc_chat": {
break; handleChat(pb);
}
case "stoc_hs_player_change": { break;
handleHsPlayerChange(pb); }
case "stoc_hs_player_change": {
break; handleHsPlayerChange(pb);
}
case "stoc_hs_watch_change": { break;
handleHsWatchChange(pb); }
case "stoc_hs_watch_change": {
break; handleHsWatchChange(pb);
}
case "stoc_hs_player_enter": { break;
handleHsPlayerEnter(pb); }
case "stoc_hs_player_enter": {
break; handleHsPlayerEnter(pb);
}
case "stoc_type_change": { break;
handleTypeChange(pb); }
case "stoc_type_change": {
break; handleTypeChange(pb);
}
case "stoc_select_hand": { break;
handleSelectHand(pb); }
case "stoc_select_hand": {
break; 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;
}
} }
case "stoc_hand_result": { }, delay);
// 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; // 该函数用于控频,防止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