2021-06-21 17:19:27 +02:00
|
|
|
<script lang="ts">
|
2021-07-19 09:22:47 +02:00
|
|
|
import { HtmlUtils } from "../../WebRtc/HtmlUtils";
|
|
|
|
import type { Game } from "../../Phaser/Game/Game";
|
|
|
|
import type { GameManager } from "../../Phaser/Game/GameManager";
|
|
|
|
import { consoleGlobalMessageManagerFocusStore, consoleGlobalMessageManagerVisibleStore } from "../../Stores/ConsoleGlobalMessageManagerStore";
|
|
|
|
import { AdminMessageEventTypes } from "../../Connexion/AdminMessagesService";
|
2021-06-21 17:19:27 +02:00
|
|
|
import uploadFile from "../images/music-file.svg";
|
2021-07-22 16:14:27 +02:00
|
|
|
import type {PlayGlobalMessageInterface} from "../../Connexion/ConnexionModels";
|
2021-06-21 17:19:27 +02:00
|
|
|
|
|
|
|
interface EventTargetFiles extends EventTarget {
|
|
|
|
files: Array<File>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export let game: Game;
|
|
|
|
export let gameManager: GameManager;
|
|
|
|
|
2021-07-15 15:09:35 +02:00
|
|
|
let gameScene = gameManager.getCurrentGameScene(game.findAnyScene());
|
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;
|
|
|
|
throw 'no file selected';
|
|
|
|
}
|
2021-06-21 17:19:27 +02:00
|
|
|
|
2021-07-19 09:22:47 +02:00
|
|
|
const fd = new FormData();
|
|
|
|
fd.append('file', selectedFile);
|
|
|
|
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,
|
|
|
|
broadcastToWorld: broadcast
|
2021-07-19 09:22:47 +02:00
|
|
|
}
|
|
|
|
inputAudio.value = '';
|
2021-07-22 16:14:27 +02:00
|
|
|
gameScene.connection?.emitGlobalMessage(audioGlobalMessage);
|
2021-07-19 09:22:47 +02:00
|
|
|
disableConsole();
|
2021-06-21 17:19:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function inputAudioFile(event: Event) {
|
|
|
|
const eventTarget : EventTargetFiles = (event.target as EventTargetFiles);
|
|
|
|
if(!eventTarget || !eventTarget.files || eventTarget.files.length === 0){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const file = eventTarget.files[0];
|
|
|
|
if(!file) {
|
|
|
|
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) {
|
|
|
|
return number + 'bytes';
|
|
|
|
} else if (number >= 1024 && number < 1048576) {
|
|
|
|
return (number / 1024).toFixed(1) + 'KB';
|
|
|
|
} else if (number >= 1048576) {
|
|
|
|
return (number / 1048576).toFixed(1) + 'MB';
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function disableConsole() {
|
2021-06-22 16:12:53 +02:00
|
|
|
consoleGlobalMessageManagerVisibleStore.set(false);
|
|
|
|
consoleGlobalMessageManagerFocusStore.set(false);
|
2021-06-21 17:19:27 +02:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<section class="section-input-send-audio">
|
2021-07-19 09:22:47 +02:00
|
|
|
<img class="nes-pointer" src="{uploadFile}" alt="Upload a file" on:click|preventDefault={ () => {fileInput.click();}}>
|
|
|
|
{#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}
|
|
|
|
<input type="file" id="input-send-audio" bind:this={fileInput} on:change={(e) => {inputAudioFile(e)}}>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
section.section-input-send-audio {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
|
|
height: 100%;
|
|
|
|
text-align: center;
|
|
|
|
|
|
|
|
img {
|
|
|
|
flex: 1 1 auto;
|
|
|
|
|
|
|
|
max-height: 80%;
|
|
|
|
margin-bottom: 20px;
|
|
|
|
}
|
|
|
|
|
|
|
|
p {
|
|
|
|
flex: 1 1 auto;
|
|
|
|
|
|
|
|
margin-bottom: 5px;
|
|
|
|
|
|
|
|
color: whitesmoke;
|
|
|
|
font-size: 1rem;
|
|
|
|
|
|
|
|
&.err {
|
|
|
|
color: #ce372b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
input {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|