Skip to content

Commit

Permalink
Merge pull request #115 from RuneLabsxyz/Better-UI
Browse files Browse the repository at this point in the history
Better UI
  • Loading branch information
0xMugen authored Oct 9, 2024
2 parents 17e1d2b + a2ccc60 commit a872927
Show file tree
Hide file tree
Showing 18 changed files with 584 additions and 7,996 deletions.
2 changes: 1 addition & 1 deletion client/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<link rel="icon" href="%sveltekit.assets%/logos/white-logo.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>
Expand Down
32 changes: 27 additions & 5 deletions client/src/lib/games/GameList.svelte
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
<script lang="ts">
import { dojoStore } from '$stores/dojoStore'
import { createEventDispatcher } from 'svelte'
$: ({ clientComponents, torii, client } = $dojoStore)
export let availableSessions
const dispatch = createEventDispatcher()
$: pendingSessions = availableSessions
let showAll = false
function onClick(session: any) {
dispatch('select', session)
}
function toggleShowAll() {
showAll = !showAll
}
$: displayedSessions = showAll
? pendingSessions.slice().reverse()
: pendingSessions.slice(-9).reverse()
</script>

<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 mx-5 gap-3">
{#if pendingSessions}
{#each availableSessions.slice().reverse() as session}
{#each displayedSessions as session}
<div
class="flex justify-between items-center border-4 rounded-lg border-black flex-col w-full card"
>
<p class="flex-grow text-left p-5">{session.value}</p>
<p class="flex-grow text-left p-5">
{session.value}
{#if session.isStarted && session.isYourTurn}
<span class="ml-2 text-green-600 font-bold">Your Turn</span>
{/if}
</p>
<button
class="border-t-4 py-2 w-full border-black hover:bg-gray-300"
on:click={() => onClick(session)}
Expand All @@ -33,6 +44,17 @@
{/if}
</div>

{#if pendingSessions && pendingSessions.length > 9}
<div class="flex justify-center mt-4">
<button
class="border-4 rounded-lg border-black py-2 px-4 hover:bg-gray-300"
on:click={toggleShowAll}
>
{showAll ? 'Show Less' : 'Show All Games'}
</button>
</div>
{/if}

<style>
.card {
transition: all 0.3s;
Expand Down
72 changes: 72 additions & 0 deletions client/src/lib/ui/ControllerModal.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte';
import { goto } from '$app/navigation';
import { username, account, clearAccountStorage } from '$stores/account';
import Button from '$lib/ui/Button.svelte';
export let show = false;
const dispatch = createEventDispatcher();
function closeModal() {
dispatch('close');
}
function logout() {
clearAccountStorage();
closeModal();
goto('/');
}
let showCopied = false;
function compressAddress(address: string): string {
if (!address) return 'Not available';
return `${address.slice(0, 6)}...${address.slice(-4)}`;
}
function copyAddress() {
if ($account && $account.address) {
navigator.clipboard.writeText($account.address)
.then(() => {
showCopied = true;
setTimeout(() => showCopied = false, 2000);
})
.catch(err => console.error('Failed to copy: ', err));
}
}
</script>

{#if show && $account && $username}
<div class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
<div class="bg-white border-4 border-black rounded-lg p-6 w-11/12 max-w-md">
<div class="flex items-center mb-4">
<img src="/logos/controller/controller.png" alt="Controller" class="w-12 h-12 mx-auto" />
</div>
<div class="mb-4 space-y-4">
<div class="flex flex-col items-center space-y-2">
<p class="font-bold text-lg">{$username.toUpperCase()}</p>
<div class="flex items-center">
<span class="break-all">{compressAddress($account?.address)}</span>
<button
on:click={copyAddress}
class="ml-2 px-2 py-1 bg-gray-200 hover:bg-gray-300 rounded text-sm"
>
Copy
</button>
</div>
{#if $account?.address && showCopied}
<span class="text-green-600 text-sm">Copied!</span>
{/if}
<p class="text-sm text-center mt-2">
Top up some STARK on this address to start playing!
</p>
</div>
<slot></slot>
</div>
<div class="flex justify-between">
<Button on:click={logout}>Logout</Button>
<Button on:click={closeModal}>Close</Button>
</div>
</div>
</div>
{/if}
10 changes: 1 addition & 9 deletions client/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import { isSetup } from '$stores/dojoStore'
import { playSoundEffectLoop } from '$lib/3d/utils/audioUtils'
import { onMount } from 'svelte'
import { account } from '$stores/account';
import { connect } from '$lib/controller'
let loading = true
Expand Down Expand Up @@ -44,11 +43,4 @@
</div>
</div>
</div>
</div>

<style>
.wrapper {
background: url('/tiled-design.svg') repeat;
background-size: 450px;
}
</style>
</div>
46 changes: 33 additions & 13 deletions client/src/routes/client/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
<script lang="ts">
import Button from '$lib/ui/Button.svelte'
import ControllerModal from '$lib/ui/ControllerModal.svelte'
import { onMount } from 'svelte';
import { connect } from '$lib/controller'
import { controller } from '$lib/controller'
import { username } from '$stores/account'
let loading = true
let showControllerModal = false
onMount(async () => {
if (await controller.probe()) {
// auto connect
await connect();
}
loading = false;
});
onMount(async () => {
if (await controller.probe()) {
// auto connect
await connect();
}
loading = false;
});
function toggleControllerModal() {
showControllerModal = !showControllerModal;
}
</script>

<div class="wrapper w-screen h-screen flex items-stretch md:flex-row flex-col">
Expand All @@ -20,12 +28,18 @@
>
{#if loading}
<p>Loading</p>
<Button on:click={connect}>Connect</Button>
{/if}
<Button href="/client/games">Play</Button>
<Button href="/client/mapmaker">Maps</Button>
<div class="flex-grow"></div>
<Button href="/">Back to home screen</Button>
{/if}
{#if username}
<Button on:click={toggleControllerModal}>
<img src="/logos/controller/controller.png" alt="Controller" class="inline-block w-8 h-8" />
{$username}
</Button>
{/if}
<Button href="/client/games/openGames">New Game</Button>
<Button href="/client/games/yourGames">Your Games</Button>
<Button href="/client/mapmaker">Maps</Button>
<div class="flex-grow"></div>
<Button href="/">Back to home screen</Button>
</div>
<div class="m-7 md:ml-0 bg-white flex-grow border-4 border-black rounded-lg">
<slot />
Expand All @@ -47,6 +61,12 @@
-->
</div>

<ControllerModal
show={showControllerModal}
on:close={toggleControllerModal}
/>


<style>
.wrapper {
background: url('/tiled-design.svg') repeat;
Expand Down
1 change: 0 additions & 1 deletion client/src/routes/client/games/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { dojoStore } from '$stores/dojoStore'
import { componentValueStore } from '$dojo/componentValueStore'
import GameList from '$lib/games/GameList.svelte'
import { goto } from '$app/navigation'
import { type Entity } from '@dojoengine/recs'
import Button from '$lib/ui/Button.svelte'
import { cn } from '$lib/css/cn'
Expand Down
92 changes: 92 additions & 0 deletions client/src/routes/client/games/openGames/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<script lang="ts">
import { dojoStore } from '$stores/dojoStore'
import { componentValueStore } from '$dojo/componentValueStore'
import GameList from '$lib/games/GameList.svelte'
import { type Entity } from '@dojoengine/recs'
import Button from '$lib/ui/Button.svelte'
import { cn } from '$lib/css/cn'
import { goToSession, joinSession } from '$lib/game'
import { account } from '$stores/account'
let availableSessions: any = null
let currentSessions: any = null
let playerEntity: Entity
$: ({ clientComponents, torii, client } = $dojoStore as any)
$: globalentity = torii.poseidonHash([BigInt(0).toString()])
$: if ($account) playerEntity = torii.poseidonHash([$account?.address])
$: global = componentValueStore(clientComponents.Global, globalentity)
$: player = componentValueStore(clientComponents.Player, playerEntity)
$: if ($global) {
if ($player) {
console.log('player', $player)
currentSessions = $player.games.map((game: { value: any }) => game.value)
let playerGames = new Set(currentSessions)
currentSessions = currentSessions.map((e: any) => ({ value: e }))
availableSessions = $global.pending_sessions.filter(
(session: { value: unknown }) => !playerGames.has(session.value)
)
console.log('currentSessions', currentSessions, currentSessions.length)
console.log(
'availableSessions',
availableSessions,
availableSessions.length
)
} else {
availableSessions = $global.pending_sessions
}
}
</script>

<div class={cn('flex flex-col h-full')}>
<div class="flex p-5 py-2 mb-4 items-center border-b-4 border-black">
<h1 class="text-3xl font-bold">Play</h1>
<span class="flex-grow"></span>
<Button href="/client/games/create">+ New Game</Button>
</div>
<div
class={cn('flex flex-col', {
'justify-center': !availableSessions,
})}
>
{#if availableSessions && availableSessions.length > 0}
<h1 class="text-xl ml-5 mb-3 font-bold">Games available</h1>
<GameList
{availableSessions}
on:select={(session) => joinSession(session.detail)}
/>
{:else}
<div class="self-center align-middle flex flex-col gap-2">
<p>No games are currently available.</p>
<Button href="/client/games/create">+ New Game</Button>
</div>
{/if}
</div>
</div>

<!--
<div class="flex justify-between p-4 fixed bottom-0 left-0 right-0 bg-white">
<button
class="px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-700 transition"
on:click={() => {
window.location.href = '/'
}}>Back</button
>
<button
class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-700 transition"
on:click={() => {
goto('/client/games/create')
}}>Create Game</button
>
</div>
-->
Loading

0 comments on commit a872927

Please sign in to comment.