import{o as c,c as f,_ as v,P as T}from"./index.js";const _={class:"audio-player",id:"play-elem"};function M(e,i,t,a,s,o){return c(),f("div",_)}const P={name:"music-player",props:["openParams"],data(){return{params:{sourceURL:"",startTime:void 0,duration:void 0,sourcePosition:[0,0,0],sourceMaxDistance:30,startPosition:[0,0,0],serverUnixTime:0,volume:.5},updateVolumeFromSrv:0,audioHandler:void 0,allowPlay:void 0,defaultURL:"",debugText:"undefined",receiveDataType:{COMMON:0,SERVER_UNIXTIME:1,VOLUME:2},deviceLost:!1}},mounted(){},created(){typeof client<"u"?sendClientEvent(gm.EVENT_EXECUTE_PUBLIC,"MUSIC_OnClientGetServerTime",this.receiveDataType.SERVER_UNIXTIME):this.srv_onReceiveData(Math.floor(Date.now()/1e3),this.receiveDataType.SERVER_UNIXTIME)},beforeUnmount(){this.stopMusic(),this.allowPlay=!1},methods:{updateServerUnixTime(){this.params.serverUnixTime++},getFileName(e){let i=0,t=0;for(;t!=e.length&&typeof e.split("/")[i]<"u";){i++;t++}return e.split("/")[i-1]},FixEncodeCyrillicURL(e){return e.indexOf("%D0%B8%CC%86")>=0&&(e=e.split("%D0%B8%CC%86").join("%D0%B9")),e.indexOf("%D0%B5%CC%88")>=0&&(e=e.split("%D0%B5%CC%88").join("%D1%91")),e.indexOf("%D0%95%CC%88")>=0&&(e=e.split("%D0%95%CC%88").join("%D0%81")),e},isRemotePath(e){return e.indexOf("http")===0},resolvePath(e){return T(()=>import("@/../../public/"+e),[],import.meta.url)},playMusic(e,i,t){if(this.allowPlay!=!0)return;this.audioHandler!=null&&this.stopMusic();let a=this.params.serverUnixTime,s=a-i;if(i!=null&&t!=null&&s>t)return;if(console.log(`[client-side]: startTime: ${i}; unixTime: ${a}; playTime: ${s}; volume: ${this.params.volume}`),s<0){setTimeout(this.playMusic,Math.abs(s*1e3),e,i,t);return}if(e==null||e.length<=5)return;let o=this.getFileName(e),r=this.FixEncodeCyrillicURL(encodeURIComponent(o));e=e.replace(o,r),this.audioHandler=new Audio,this.audioHandler.volume=0,this.audioHandler.src=this.isRemotePath(e)?e:this.resolvePath(e),this.audioHandler.autoplay=!0,i!=null&&s>=2&&(this.audioHandler.currentTime=s),this.onUpdateLocalPosition(this.params.startPosition[0],this.params.startPosition[1],this.params.startPosition[2])},stopMusic(){this.audioHandler&&(this.audioHandler.src="",this.audioHandler.currentTime=0,this.audioHandler.volume=0,this.audioHandler.autoplay=!1,delete this.audioHandler)},onUpdateLocalPosition(e,i,t){if(!this.audioHandler)return;const a=Math.abs(e-this.params.sourcePosition[0])+Math.abs(i-this.params.sourcePosition[1])+Math.abs(t-this.params.sourcePosition[2]);if(a>=this.params.sourceMaxDistance){this.audioHandler.volume=0;return}let s=100-a/this.params.sourceMaxDistance*100,o=this.params.volume/100*s;this.audioHandler.volume=o,this.debugText=`volume: ${o} / distance: ${a} [${s}%]`},convertData(e){return{sourceURL:e[0],startTime:e[1],duration:e[2],sourcePosition:[e[3][0],e[3][1],e[3][2]],sourceMaxDistance:e[4],volume:e[5],startPosition:[e[6][0],e[6][1],e[6][2]]}},srv_onReceiveData(e,i=0){if(i==this.receiveDataType.SERVER_UNIXTIME){this.allowPlay=!0,this.openParams&&(this.params=this.convertData(this.openParams));let t=()=>{this.updateServerUnixTime(),setTimeout(t,1e3)};this.params.serverUnixTime=e,t(),this.openParams?this.playMusic(this.params.sourceURL,this.params.startTime,this.params.duration):this.playMusic(this.params.sourceURL,this.params.startTime,this.params.duration)}else if(i==this.receiveDataType.VOLUME){this.updateVolumeFromSrv++;const t=1450,a=this.params.volume*100,s=e*100,o=Math.round(s-a),r=Math.abs(o);if(r<5){this.params.volume=s/100;return}const l=Math.round(t/r),m=Math.round(t/l),u=o/l;for(let d=0;d!=l;d++){const h=m*d;setTimeout(p=>{if(p!=this.updateVolumeFromSrv)return;let n=this.params.volume*100;n+=u,n<0||n>100?(u<=0?this.params.volume=0:u>0&&(this.params.volume=1),this.updateVolumeFromSrv++):this.params.volume=n/100},h,this.updateVolumeFromSrv)}}},onDeviceLost(){this.deviceLost=!0,this.audioHandler&&(this.audioHandler.volume=0)},onDeviceRestore(){this.deviceLost=!1,this.audioHandler&&(this.audioHandler.volume=this.params.volume)}}},y=v(P,[["render",M]]);export{y as default};
