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

fix: fix MR comment

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