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-21 17:19:27 +02:00
|
|
|
import {ConsoleGlobalMessageManagerFocusStore, ConsoleGlobalMessageManagerVisibleStore} from "../../Stores/ConsoleGlobalMessageManagerStore";
|
|
|
|
import {AdminMessageEventTypes} from "../../Connexion/AdminMessagesService";
|
|
|
|
import type {PlayGlobalMessageInterface} from "../../Connexion/ConnexionModels";
|
|
|
|
import uploadFile from "../images/music-file.svg";
|
2021-06-22 10:22:49 +02:00
|
|
|
import {LoginSceneName} from "../../Phaser/Login/LoginScene";
|
2021-06-21 17:19:27 +02:00
|
|
|
|
|
|
|
interface EventTargetFiles extends EventTarget {
|
|
|
|
files: Array<File>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export let game: Game;
|
|
|
|
export let gameManager: GameManager;
|
|
|
|
|
2021-06-22 10:22:49 +02:00
|
|
|
let gameScene = gameManager.getCurrentGameScene(game.scene.getScene(LoginSceneName));
|
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);
|
|
|
|
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 = '';
|
|
|
|
gameScene.connection.emitGlobalMessage(GlobalMessage);
|
|
|
|
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() {
|
|
|
|
ConsoleGlobalMessageManagerVisibleStore.set(false);
|
|
|
|
ConsoleGlobalMessageManagerFocusStore.set(false);
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<section class="section-input-send-audio">
|
|
|
|
<div class="input-send-audio">
|
|
|
|
<img src="{uploadFile}" alt="Upload a file" on:click|preventDefault={ () => {fileinput.click();}}>
|
|
|
|
{#if filename != undefined}
|
|
|
|
<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)}}>
|
|
|
|
</div>
|
|
|
|
<div class="btn-action">
|
|
|
|
<button class="nes-btn is-primary" on:click|preventDefault={SendAudioMessage}>Send</button>
|
|
|
|
</div>
|
2021-06-22 09:43:41 +02:00
|
|
|
</section>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
//UploadAudioGlobalMessage
|
|
|
|
.section-input-send-audio {
|
|
|
|
margin: 10px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.section-input-send-audio .input-send-audio {
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
.section-input-send-audio #input-send-audio{
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.section-input-send-audio div.input-send-audio label{
|
|
|
|
color: white;
|
|
|
|
}
|
|
|
|
|
|
|
|
.section-input-send-audio div.input-send-audio p.err {
|
|
|
|
color: #ce372b;
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
.section-input-send-audio div.input-send-audio img{
|
|
|
|
height: 150px;
|
2021-06-22 09:49:04 +02:00
|
|
|
cursor: url('../../../style/images/cursor_pointer.png'), pointer;
|
2021-06-22 09:43:41 +02:00
|
|
|
}
|
|
|
|
</style>
|