This commit is contained in:
Moritz Ruth 2023-04-23 14:24:24 +02:00
parent b76bca9c57
commit 14c4a434f7
Signed by: moritzruth
GPG key ID: C9BBAB79405EE56D
6 changed files with 42 additions and 28 deletions

View file

@ -3,6 +3,8 @@
"version": "1.0.0",
"type": "module",
"scripts": {
"start:ui": "vite preview --host",
"build:ui": "vite build",
"dev:ui": "vite",
"dev:server": "NODE_ENV=development tsx watch --clear-screen=false ./src/server/main.ts"
},

View file

@ -25,7 +25,6 @@ export const useGame = defineStore("game", () => {
const actions = reactive<GameAction[]>([])
const auth = useAuth()
const actionsBus = useGameActionsBus()
actionsBus.on(action => {
actions.push(action)
@ -60,11 +59,15 @@ export const useGame = defineStore("game", () => {
})
})
},
async create() {
return await trpcClient.createGame.mutate()
async reset() {
window.location.hash = ""
lobbyCode.value = null
state.value = getUninitializedGameState()
actions.splice(0, actions.length)
},
start: () => trpcClient.game.start.mutate({ lobbyCode: lobbyCode.value! }),
hit: () => trpcClient.game.hit.mutate({ lobbyCode: lobbyCode.value! }),
stay: () => trpcClient.game.stay.mutate({ lobbyCode: lobbyCode.value! })
stay: () => trpcClient.game.stay.mutate({ lobbyCode: lobbyCode.value! }),
create: () => trpcClient.createGame.mutate()
}
})

View file

@ -1,10 +1,16 @@
<template>
<div class="flex flex-col gap-14">
<PlayerCards v-for="playerId in reorderedPlayerIds" :key="playerId" :player-id="playerId"/>
<div
class="flex gap-5 justify-end items-center transform transition ease duration-500"
:class="game.state.phase === 'end' ? 'translate-y-4 opacity-0' : ''"
<div class="flex gap-5 justify-end items-center transform transition ease duration-500">
<template v-if="game.state.phase === 'end'">
<BigButton
class="bg-gradient-to-br from-gray-600 to-gray-800"
@click="game.reset()"
>
Leave
</BigButton>
</template>
<template v-else>
<div class="font-fat opacity-20 text-3xl pr-2">
<template v-if="isBust">You are bust.</template>
</div>
@ -22,6 +28,7 @@
>
Stay
</BigButton>
</template>
</div>
<GameEndModal/>
</div>

View file

@ -40,10 +40,11 @@
const usernameInput = ref("")
const isLoading = ref(false)
function submit() {
async function submit() {
if (isLoading.value) return
isLoading.value = true
auth.login(usernameInput.value)
await auth.login(usernameInput.value)
window.location.reload() // so that the WebSocket reconnects with the cookie set
isLoading.value = false
}
</script>

View file

@ -85,6 +85,7 @@ export class Game extends EventEmitter<Events> {
addPlayer(id: string, name: string) {
if (this.lobbyPlayerIds.has(id)) return
if (this.state.phase !== "pre-start") throw new Error("The game was already started.")
if (this.lobbyPlayerIds.size >= 3) throw new Error("The game is full.")
this.lobbyPlayerIds.add(id)
this.addAction({

View file

@ -15,7 +15,7 @@ export const trpcClient = createTRPCProxyClient<AppRouter>({
splitLink({
condition: o => o.type === "subscription",
true: wsLink({
client: wsClient,
client: wsClient
}),
false: httpLink({
url: "/trpc"