2021-06-21 17:19:27 +02:00
|
|
|
<script lang="ts">
|
|
|
|
import {HtmlUtils} from "../../WebRtc/HtmlUtils";
|
2021-06-22 09:43:41 +02:00
|
|
|
import type {Game} from "../../Phaser/Game/Game";
|
2021-06-22 10:10:03 +02:00
|
|
|
import type {GameManager} from "../../Phaser/Game/GameManager";
|
2021-06-22 16:12:53 +02:00
|
|
|
import {consoleGlobalMessageManagerFocusStore, consoleGlobalMessageManagerVisibleStore} from "../../Stores/ConsoleGlobalMessageManagerStore";
|
2021-06-21 17:19:27 +02:00
|
|
|
import {AdminMessageEventTypes} from "../../Connexion/AdminMessagesService";
|
|
|
|
import type {PlayGlobalMessageInterface} from "../../Connexion/ConnexionModels";
|
|
|
|
import uploadFile from "../images/music-file.svg";
|
|
|
|
|
|
|
|
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-06-22 10:10:03 +02:00
|
|
|
let fileinput: HTMLInputElement;
|
2021-06-21 17:19:27 +02:00
|
|
|
let filename: string;
|
|
|
|
let filesize: string;
|
2021-06-22 09:43:41 +02:00
|
|
|
let errorfile: boolean;
|
2021-06-21 17:19:27 +02:00
|
|
|
|
|
|
|
const AUDIO_TYPE = AdminMessageEventTypes.audio;
|
|
|
|
|
|
|
|
|
|
|
|
async function SendAudioMessage() {
|
2021-06-22 10:27:54 +02:00
|
|
|
if (gameScene == undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2021-06-22 09:43:41 +02:00
|
|
|
const inputAudio = HtmlUtils.getElementByIdOrFail<HTMLInputElement>("input-send-audio");
|
2021-06-22 10:22:49 +02:00
|
|
|
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-06-22 10:22:49 +02:00
|
|
|
const fd = new FormData();
|
|
|
|
fd.append('file', selectedFile);
|
2021-06-22 10:43:20 +02:00
|
|
|
const res = await gameScene.connection?.uploadAudio(fd);
|
2021-06-21 17:19:27 +02:00
|
|
|
|
2021-06-22 10:22:49 +02:00
|
|
|
const GlobalMessage: PlayGlobalMessageInterface = {
|
2021-06-21 17:19:27 +02:00
|
|
|
id: (res as { id: string }).id,
|
|
|
|
message: (res as { path: string }).path,
|
|
|
|
type: AUDIO_TYPE
|
|
|
|
}
|
2021-06-22 10:22:49 +02:00
|
|
|
inputAudio.value = '';
|
2021-06-22 10:43:20 +02:00
|
|
|
gameScene.connection?.emitGlobalMessage(GlobalMessage);
|
2021-06-22 10:22:49 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = file.name;
|
|
|
|
filesize = getFileSize(file.size);
|
2021-06-22 09:43:41 +02:00
|
|
|
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">
|
|
|
|
<img src="{uploadFile}" alt="Upload a file" on:click|preventDefault={ () => {fileinput.click();}}>
|
2021-07-15 15:09:35 +02:00
|
|
|
{#if filename !== undefined}
|
2021-06-21 17:19:27 +02:00
|
|
|
<label for="input-send-audio">{filename} : {filesize}</label>
|
|
|
|
{/if}
|
2021-06-22 09:43:41 +02:00
|
|
|
{#if errorfile}
|
|
|
|
<p class="err">No file selected. You need to upload a file before sending it.</p>
|
|
|
|
{/if}
|
2021-06-21 17:19:27 +02:00
|
|
|
<input type="file" id="input-send-audio" bind:this={fileinput} on:change={(e) => {inputAudioFile(e)}}>
|
2021-07-15 15:09:35 +02:00
|
|
|
<div class="footer-btn-action">
|
2021-06-21 17:19:27 +02:00
|
|
|
<button class="nes-btn is-primary" on:click|preventDefault={SendAudioMessage}>Send</button>
|
|
|
|
</div>
|
2021-07-15 15:09:35 +02:00
|
|
|
</section>
|