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", "version": "1.0.0",
"type": "module", "type": "module",
"scripts": { "scripts": {
"start:ui": "vite preview --host",
"build:ui": "vite build",
"dev:ui": "vite", "dev:ui": "vite",
"dev:server": "NODE_ENV=development tsx watch --clear-screen=false ./src/server/main.ts" "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 actions = reactive<GameAction[]>([])
const auth = useAuth() const auth = useAuth()
const actionsBus = useGameActionsBus() const actionsBus = useGameActionsBus()
actionsBus.on(action => { actionsBus.on(action => {
actions.push(action) actions.push(action)
@ -60,11 +59,15 @@ export const useGame = defineStore("game", () => {
}) })
}) })
}, },
async create() { async reset() {
return await trpcClient.createGame.mutate() window.location.hash = ""
lobbyCode.value = null
state.value = getUninitializedGameState()
actions.splice(0, actions.length)
}, },
start: () => trpcClient.game.start.mutate({ lobbyCode: lobbyCode.value! }), start: () => trpcClient.game.start.mutate({ lobbyCode: lobbyCode.value! }),
hit: () => trpcClient.game.hit.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> <template>
<div class="flex flex-col gap-14"> <div class="flex flex-col gap-14">
<PlayerCards v-for="playerId in reorderedPlayerIds" :key="playerId" :player-id="playerId"/> <PlayerCards v-for="playerId in reorderedPlayerIds" :key="playerId" :player-id="playerId"/>
<div <div class="flex gap-5 justify-end items-center transform transition ease duration-500">
class="flex gap-5 justify-end items-center transform transition ease duration-500" <template v-if="game.state.phase === 'end'">
:class="game.state.phase === 'end' ? 'translate-y-4 opacity-0' : ''" <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"> <div class="font-fat opacity-20 text-3xl pr-2">
<template v-if="isBust">You are bust.</template> <template v-if="isBust">You are bust.</template>
</div> </div>
@ -22,6 +28,7 @@
> >
Stay Stay
</BigButton> </BigButton>
</template>
</div> </div>
<GameEndModal/> <GameEndModal/>
</div> </div>

View file

@ -40,10 +40,11 @@
const usernameInput = ref("") const usernameInput = ref("")
const isLoading = ref(false) const isLoading = ref(false)
function submit() { async function submit() {
if (isLoading.value) return if (isLoading.value) return
isLoading.value = true 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 isLoading.value = false
} }
</script> </script>

View file

@ -85,6 +85,7 @@ export class Game extends EventEmitter<Events> {
addPlayer(id: string, name: string) { addPlayer(id: string, name: string) {
if (this.lobbyPlayerIds.has(id)) return 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.") if (this.lobbyPlayerIds.size >= 3) throw new Error("The game is full.")
this.lobbyPlayerIds.add(id) this.lobbyPlayerIds.add(id)
this.addAction({ this.addAction({

View file

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