2021-06-21 17:19:27 +02:00
|
|
|
<script lang="ts">
|
2021-07-19 09:22:47 +02:00
|
|
|
import { HtmlUtils } from "../../WebRtc/HtmlUtils";
|
2021-08-26 12:01:07 +02:00
|
|
|
import { gameManager } from "../../Phaser/Game/GameManager";
|
2021-07-19 09:22:47 +02:00
|
|
|
import { AdminMessageEventTypes } from "../../Connexion/AdminMessagesService";
|
2021-06-21 17:19:27 +02:00
|
|
|
import uploadFile from "../images/music-file.svg";
|
2021-08-26 12:01:07 +02:00
|
|
|
import type { PlayGlobalMessageInterface } from "../../Connexion/ConnexionModels";
|
2021-06-21 17:19:27 +02:00
|
|
|
|
|
|
|
interface EventTargetFiles extends EventTarget {
|
|
|
|
files: Array<File>;
|
|
|
|
}
|
|
|
|
|
2021-08-16 11:43:29 +02:00
|
|
|
let gameScene = gameManager.getCurrentGameScene();
|
2021-07-19 09:22:47 +02:00
|
|
|
let fileInput: HTMLInputElement;
|
|
|
|
let fileName: string;
|
|
|
|
let fileSize: string;
|
|
|
|
let errorFile: boolean;
|
2021-06-21 17:19:27 +02:00
|
|
|
|
|
|
|
const AUDIO_TYPE = AdminMessageEventTypes.audio;
|
|
|
|
|
2021-07-19 09:22:47 +02:00
|
|
|
export const handleSending = {
|
2021-07-22 16:14:27 +02:00
|
|
|
async sendAudioMessage(broadcast: boolean) {
|
2021-07-19 09:22:47 +02:00
|
|
|
if (gameScene == undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const inputAudio = HtmlUtils.getElementByIdOrFail<HTMLInputElement>("input-send-audio");
|
|
|
|
const selectedFile = inputAudio.files ? inputAudio.files[0] : null;
|
|
|
|
if (!selectedFile) {
|
|
|
|
errorFile = true;
|
2021-12-06 16:12:37 +01:00
|
|
|
throw "no file selected";
|
2021-07-19 09:22:47 +02:00
|
|
|
}
|
2021-06-21 17:19:27 +02:00
|
|
|
|
2021-07-19 09:22:47 +02:00
|
|
|
const fd = new FormData();
|
2021-12-06 16:12:37 +01:00
|
|
|
fd.append("file", selectedFile);
|
2021-07-19 09:22:47 +02:00
|
|
|
const res = await gameScene.connection?.uploadAudio(fd);
|
2021-06-21 17:19:27 +02:00
|
|
|
|
2021-07-22 16:14:27 +02:00
|
|
|
const audioGlobalMessage: PlayGlobalMessageInterface = {
|
|
|
|
content: (res as { path: string }).path,
|
|
|
|
type: AUDIO_TYPE,
|
2021-12-06 16:12:37 +01:00
|
|
|
broadcastToWorld: broadcast,
|
|
|
|
};
|
|
|
|
inputAudio.value = "";
|
2021-07-22 16:14:27 +02:00
|
|
|
gameScene.connection?.emitGlobalMessage(audioGlobalMessage);
|
2021-12-06 16:12:37 +01:00
|
|
|
},
|
|
|
|
};
|
2021-06-21 17:19:27 +02:00
|
|
|
|
|
|
|
function inputAudioFile(event: Event) {
|
2021-12-06 16:12:37 +01:00
|
|
|
const eventTarget: EventTargetFiles = event.target as EventTargetFiles;
|
|
|
|
if (!eventTarget || !eventTarget.files || eventTarget.files.length === 0) {
|
2021-06-21 17:19:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const file = eventTarget.files[0];
|
2021-12-06 16:12:37 +01:00
|
|
|
if (!file) {
|
2021-06-21 17:19:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-19 09:22:47 +02:00
|
|
|
fileName = file.name;
|
|
|
|
fileSize = getFileSize(file.size);
|
|
|
|
errorFile = false;
|
2021-06-21 17:19:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getFileSize(number: number) {
|
|
|
|
if (number < 1024) {
|
2021-12-06 16:12:37 +01:00
|
|
|
return number + "bytes";
|
2021-06-21 17:19:27 +02:00
|
|
|
} else if (number >= 1024 && number < 1048576) {
|
2021-12-06 16:12:37 +01:00
|
|
|
return (number / 1024).toFixed(1) + "KB";
|
2021-06-21 17:19:27 +02:00
|
|
|
} else if (number >= 1048576) {
|
2021-12-06 16:12:37 +01:00
|
|
|
return (number / 1048576).toFixed(1) + "MB";
|
2021-06-21 17:19:27 +02:00
|
|
|
} else {
|
2021-12-06 16:12:37 +01:00
|
|
|
return "";
|
2021-06-21 17:19:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<section class="section-input-send-audio">
|
2021-12-06 16:12:37 +01:00
|
|
|
<img
|
|
|
|
class="nes-pointer"
|
|
|
|
src={uploadFile}
|
|
|
|
alt="Upload a file"
|
|
|
|
on:click|preventDefault={() => {
|
|
|
|
fileInput.click();
|
|
|
|
}}
|
|
|
|
/>
|
2021-07-19 09:22:47 +02:00
|
|
|
{#if fileName !== undefined}
|
|
|
|
<p>{fileName} : {fileSize}</p>
|
|
|
|
{/if}
|
|
|
|
{#if errorFile}
|
|
|
|
<p class="err">No file selected. You need to upload a file before sending it.</p>
|
|
|
|
{/if}
|
2021-12-06 16:12:37 +01:00
|
|
|
<input
|
|
|
|
type="file"
|
|
|
|
id="input-send-audio"
|
|
|
|
bind:this={fileInput}
|
|
|
|
on:change={(e) => {
|
|
|
|
inputAudioFile(e);
|
|
|
|
}}
|
|
|
|
/>
|
2021-07-19 09:22:47 +02:00
|
|
|
</section>
|
|
|
|
|
|
|
|
<style lang="scss">
|
2021-12-06 16:12:37 +01:00
|
|
|
section.section-input-send-audio {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
2021-07-19 09:22:47 +02:00
|
|
|
|
2021-12-06 16:12:37 +01:00
|
|
|
height: 100%;
|
|
|
|
text-align: center;
|
2021-07-19 09:22:47 +02:00
|
|
|
|
2021-12-06 16:12:37 +01:00
|
|
|
img {
|
|
|
|
flex: 1 1 auto;
|
|
|
|
max-height: 80%;
|
|
|
|
margin-bottom: 20px;
|
|
|
|
}
|
|
|
|
p {
|
|
|
|
margin-bottom: 5px;
|
|
|
|
color: whitesmoke;
|
|
|
|
font-size: 1rem;
|
|
|
|
&.err {
|
|
|
|
color: #ce372b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
input {
|
|
|
|
display: none;
|
|
|
|
}
|
2021-07-19 09:22:47 +02:00
|
|
|
}
|
2021-12-06 16:12:37 +01:00
|
|
|
</style>
|