Skip to content

Commit

Permalink
feat: use KV storage
Browse files Browse the repository at this point in the history
  • Loading branch information
zAlweNy26 committed Dec 27, 2024
1 parent 6ad882b commit a3addf0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
6 changes: 6 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default defineNuxtConfig({
},

i18n: {
baseUrl: '',
experimental: {
typedOptionsAndMessages: 'all',
typedPages: true,
Expand Down Expand Up @@ -120,6 +121,11 @@ export default defineNuxtConfig({
},
},

hub: {
analytics: true,
kv: true,
},

vite: {
logLevel: 'info',
},
Expand Down
44 changes: 23 additions & 21 deletions server/routes/_ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,68 @@ import type { Peer } from 'crossws'
interface Game {
id: string
extractions: number[]
host: Peer
clients: Set<Peer>
host: string
clients: string[]
}

const activeGames: Map<string, Game> = new Map()
const getActiveGames = async () => (await hubKV().keys('game')).length

function getGame(peer: Peer) {
async function getGame(peer: Peer) {
const params = new URL(peer.websocket.url!).searchParams
const id = params.get('id')!
const game = activeGames.get(id)
const game = await hubKV().get<Game>(`game:${id}`)
const type = params.get('type')!
if (game) {
if (type === 'host' && game.host.id === peer.id) return game
if (type === 'client' && [...game.clients].find(p => p.id === peer.id)) return game
if (type === 'host' && game.host === peer.id) return game
if (type === 'client' && game.clients.includes(peer.id)) return game
else {
peer.subscribe(id)
game.clients.add(peer)
game.clients.push(peer.id)
await hubKV().set(`game:${id}`, game)
console.log(`[Peer] Client ${peer.id} connected to game ${id}`)
return game
}
}
else if (type === 'host') {
const newGame = { id, extractions: [], host: peer, clients: new Set([]) }
activeGames.set(id, newGame)
const newGame = { id, extractions: [], host: peer.id, clients: [] }
await hubKV().set(`game:${id}`, newGame)
console.log(`[Peer] Host ${peer.id} created game ${id}`)
console.log(`[Peer] Active games: ${activeGames.size}`)
console.log(`[Peer] Active games: ${await getActiveGames()}`)
return newGame
}
}

export default defineWebSocketHandler({
open(peer) {
const game = getGame(peer)
async open(peer) {
const game = await getGame(peer)
if (!game) peer.terminate()
else peer.send(JSON.stringify({ status: 'opened', extractions: game.extractions }))
},
message(peer, message) {
async message(peer, message) {
const { extracted } = message.json<{ extracted: number }>()

const game = getGame(peer)
const game = await getGame(peer)
if (!game) return peer.close(1011, 'Game not found')

if (game.host.id === peer.id) {
if (game.host === peer.id) {
game.extractions.push(extracted)
await hubKV().set(`game:${game.id}`, game)
peer.publish(game.id, JSON.stringify({ status: 'started', extractions: game.extractions }))
}
},
close(peer) {
async close(peer) {
console.log(`[Peer] Connection closed: ${peer}`)

const game = getGame(peer)
const game = await getGame(peer)
if (!game) return
peer.unsubscribe(game.id)

if (peer.id === game.host.id) {
activeGames.delete(game.id)
if (peer.id === game.host) {
await hubKV().del(`game:${game.id}`)
console.log(`[Peer] Host ${peer.id} deleted game ${game.id}`)
peer.publish(game.id, JSON.stringify({ status: 'closed', extractions: game.extractions }))
}

console.log(`[Peer] Active games: ${activeGames.size}`)
console.log(`[Peer] Active games: ${await getActiveGames()}`)
},
})

0 comments on commit a3addf0

Please sign in to comment.