Commit 1fb2bf76 authored by love_飞影's avatar love_飞影 Committed by Chunchi Che

fix: fix MR comment

parent 15484bb6
import { ocgDamageAdapter } from "rust-src";
import { AudioActionType, playEffect } from "@/infra/audio";
import { ygopro } from "../../../idl/ocgcore";
/*
......@@ -11,7 +9,6 @@ import { ygopro } from "../../../idl/ocgcore";
* @param value - 减少的Hp数值
* */
export default (data: Uint8Array) => {
playEffect(AudioActionType.SOUND_DAMAGE);
const damage = ocgDamageAdapter(data);
return new ygopro.StocGameMessage.MsgUpdateHp(damage);
......
......@@ -2,8 +2,6 @@ import { BufferReader } from "rust-src";
import { ygopro } from "../../../idl/ocgcore";
import MsgWin = ygopro.StocGameMessage.MsgWin;
import { AudioActionType, changeScene } from "@/infra/audio";
import { matStore } from "@/stores";
/*
* Msg Win
......@@ -16,12 +14,6 @@ export default (data: Uint8Array) => {
const win_player = reader.readUint8();
const reason = reader.readUint8();
// 双打需要改这里判断
if (matStore.isMe(win_player)) {
changeScene(AudioActionType.BGM_WIN);
} else {
changeScene(AudioActionType.BGM_LOSE);
}
return new MsgWin({
win_player,
......
......@@ -18,13 +18,10 @@ export class NeosAudioContext extends EventEmitter<AudioScheduledSourceNodeEvent
return this._isClosed;
}
public async play(audio: ArrayBuffer) {
const source = this._musicAudioContext.createBufferSource();
const buffer = await this._musicAudioContext.decodeAudioData(audio);
source.buffer = buffer;
source.connect(this._gainNode).connect(this._musicAudioContext.destination);
source.start();
/**
* 触发自动播放
*/
private _triggerAutoPlay() {
if (this.state === "suspended") {
const autoPlay = () => {
document.removeEventListener("click", autoPlay);
......@@ -32,6 +29,12 @@ export class NeosAudioContext extends EventEmitter<AudioScheduledSourceNodeEvent
};
document.addEventListener("click", autoPlay);
}
}
/**
* 监听未启播
*/
private _observerPlayState(source: AudioBufferSourceNode) {
// 50ms 未启播,说明播放失败了,重新尝试播放
const timeout = setTimeout(async () => {
if (source.loop) return;
......@@ -51,6 +54,21 @@ export class NeosAudioContext extends EventEmitter<AudioScheduledSourceNodeEvent
source.addEventListener("ended", Ended);
}
/**
* 播放音频
* @param audio 音频数据
*/
public async play(audio: ArrayBuffer) {
const source = this._musicAudioContext.createBufferSource();
const buffer = await this._musicAudioContext.decodeAudioData(audio);
source.buffer = buffer;
source.connect(this._gainNode).connect(this._musicAudioContext.destination);
source.start();
this._triggerAutoPlay();
this._observerPlayState(source);
}
public async resume() {
if (this.state !== "suspended") return;
return this._musicAudioContext.resume();
......
......@@ -34,5 +34,9 @@ export function changeScene(scene: AudioActionType) {
}
// 初始化音频设置
audioContextManger.updateMusicVolume(settingStore.audio.musicVolume);
audioContextManger.enableBGM = settingStore.audio.enableMusic ?? false;
function initAudioSetting() {
audioContextManger.updateMusicVolume(settingStore.audio.musicVolume);
audioContextManger.enableBGM = settingStore.audio.enableMusic ?? false;
}
initAudioSetting();
......@@ -2,6 +2,7 @@ import { fetchStrings, Region, ygopro } from "@/api";
import { matStore } from "@/stores";
import { displayEndModal } from "@/ui/Duel/Message";
import MsgWin = ygopro.StocGameMessage.MsgWin;
import { AudioActionType, changeScene } from "@/infra/audio";
export default async (win: MsgWin) => {
const { win_player, reason } = win;
......@@ -10,4 +11,10 @@ export default async (win: MsgWin) => {
matStore.isMe(win_player),
fetchStrings(Region.Victory, `0x${reason.toString(16)}`),
);
if (matStore.isMe(win_player)) {
changeScene(AudioActionType.BGM_WIN);
} else {
changeScene(AudioActionType.BGM_LOSE);
}
};
import { ygopro } from "@/api";
import { AudioActionType, playEffect } from "@/infra/audio";
import { chatStore } from "@/stores";
export default function handleChat(pb: ygopro.YgoStocMsg) {
playEffect(AudioActionType.SOUND_CHAT);
const chat = pb.stoc_chat;
chatStore.message = chat.msg;
chatStore.sender = chat.player;
......
......@@ -17,14 +17,13 @@ const DECKERROR_NOTAVAIL = 0x9;
export default async function handleErrorMsg(errorMsg: ygopro.StocErrorMsg) {
const { error_type, error_code } = errorMsg;
playEffect(AudioActionType.SOUND_INFO);
switch (error_type) {
case ErrorType.JOINERROR: {
playEffect(AudioActionType.SOUND_INFO);
roomStore.errorMsg = fetchStrings(Region.System, 1403 + error_code);
break;
}
case ErrorType.DECKERROR: {
playEffect(AudioActionType.SOUND_INFO);
const flag = error_code >> 28;
const code = error_code & 0xfffffff;
const card = fetchCard(code);
......@@ -81,12 +80,10 @@ export default async function handleErrorMsg(errorMsg: ygopro.StocErrorMsg) {
break;
}
case ErrorType.SIDEERROR: {
playEffect(AudioActionType.SOUND_INFO);
roomStore.errorMsg = "更换副卡组失败,请检查卡片张数是否一致。";
break;
}
case ErrorType.VERSIONERROR: {
playEffect(AudioActionType.SOUND_INFO);
roomStore.errorMsg = "版本不匹配,请联系技术人员解决";
break;
}
......
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