smo-proximity-chat/src/routes/+page.svelte

72 lines
1.8 KiB
Svelte
Raw Normal View History

2024-12-17 19:52:13 -05:00
<script lang="ts">
import { onMount } from "svelte";
2024-12-21 06:21:14 -05:00
import { Client, Player } from "$lib/client";
import { AudioManager } from "$lib/audio";
import { identity, localStore } from "$lib/stores";
import { readonly, type Writable } from "svelte/store";
2024-12-17 19:52:13 -05:00
2024-12-21 06:21:14 -05:00
let name = localStore("name", "", identity, identity);
let useMusic = localStore(
"useMusic",
false,
(v) => v == "true",
(v) => v.toString()
);
2024-12-20 05:33:50 -05:00
let client: Client = $state(undefined as any);
2024-12-21 06:21:14 -05:00
let players: Writable<Record<number, Player>> = undefined as any;
2024-12-20 05:33:50 -05:00
let ac: AudioManager;
let wyrm: HTMLAudioElement;
let outputAudio: HTMLDivElement;
async function audioPrep() {
2024-12-21 06:21:14 -05:00
let media = $useMusic
2024-12-20 05:33:50 -05:00
? wyrm
: await navigator.mediaDevices.getUserMedia({
audio: true,
video: false,
});
ac = new AudioManager(media);
}
async function connect() {
await audioPrep();
2024-12-21 06:21:14 -05:00
client = await Client.connect(readonly(name), ac, outputAudio);
players = client.players;
2024-12-17 19:52:13 -05:00
console.log(client);
}
</script>
2024-12-21 06:21:14 -05:00
<div bind:this={outputAudio}>audio outputs</div>
2024-12-20 05:33:50 -05:00
2024-12-17 19:52:13 -05:00
<div>
2024-12-20 05:33:50 -05:00
I am <input
type="text"
maxlength="32"
list="connectedPlayers"
2024-12-21 06:21:14 -05:00
bind:value={$name}
2024-12-20 05:33:50 -05:00
/>
<label for="useMusic">Use Music</label>
2024-12-21 06:21:14 -05:00
<input id="useMusic" type="checkbox" bind:checked={$useMusic} />
2024-12-20 05:33:50 -05:00
<button onclick={() => connect()}>Connect</button>
<audio bind:this={wyrm} src="/wyrmjewelbox.wav" autoplay={false} controls
>audio of the wyrm's houseki box</audio
>
2024-12-17 19:52:13 -05:00
</div>
2024-12-20 05:33:50 -05:00
{#if client !== undefined}
<datalist>
2024-12-21 06:21:14 -05:00
{#each Object.values($players) as player}
2024-12-20 05:33:50 -05:00
<option>{player.name}</option>
{/each}
</datalist>
<div>
2024-12-21 06:21:14 -05:00
Connected players - {Object.values($players).length}<br />
{#each Object.values($players) as player}
2024-12-20 05:33:50 -05:00
<pre class="name">{player.name}</pre>
{/each}
</div>
{/if}
2024-12-17 19:52:13 -05:00
<style>
.name {
font-style: italic;
}
</style>