Skip to content

Commit

Permalink
Add characters query
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsandow committed Jul 25, 2022
1 parent 7fe5bef commit dde6c34
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
2 changes: 1 addition & 1 deletion client/env.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const uri = process.env?.SERVER_URI || 'http://localhost:3000'
export const username = process.env?.RPG_FOR_BOTS_USERNAME || 'foobar'
export const password = process.env?.RPG_FOR_BOTS_PASSWORD || 'foobar2'
export const password = process.env?.RPG_FOR_BOTS_PASSWORD || 'foobar'
5 changes: 4 additions & 1 deletion client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const start = Date.now()
socket.emit('ping', () => {
// eslint-disable-next-line no-console
console.log(`Ping: ${Date.now() - start}ms`)
})

socket.close()
socket.emit('characterList', (characters) => {
// eslint-disable-next-line no-console
console.debug(characters)
})
20 changes: 20 additions & 0 deletions server/characters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { CharacterList } from '../shared/socket'

import users, { User } from './data/users'

// eslint-disable-next-line import/prefer-default-export
export async function getCharacterList(
username: string,
): Promise<CharacterList> {
const user = users.find((u: User) => u.username === username)
let { characters } = user || {}

const demoUser = users.find((u: User) => u.username === 'demo') as User
if (!user) {
characters = demoUser.characters
}

return (characters || []).map((c) => ({
name: c.name,
}))
}
25 changes: 25 additions & 0 deletions server/data/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export type Character = {
name: string
}

export type User = {
username: string
characters: Character[]
}

export default [
{
username: 'demo',
characters: [
{
name: 'jim',
},
{
name: 'jack',
},
{
name: 'johnny',
},
],
},
] as User[]
18 changes: 10 additions & 8 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { port } from './env'
import { parseToken, validateCredentials } from './aurh'
import logger from './logger'
import { getCharacterList } from './characters'

const server = new Server<
ClientToServerEvents,
Expand Down Expand Up @@ -37,21 +38,22 @@ server.use((socket, next) => {
server.on('connection', (socket) => {
logger.debug(socket, `New connection from ${socket.handshake.address}`)

if (socket.data.username) {
logger.log(socket, `User came online`)
}

socket.on('disconnect', () => {
if (socket.data.username) {
logger.log(socket, `User went offline`)
}

logger.debug(socket, `Connection closed`)
})

socket.on('ping', (callback) => {
callback()
})

socket.on('characterList', (cb) => {
getCharacterList(socket.data.username || '')
.then((data) => cb(data))
.catch((e) => {
logger.error(socket, `Retrieving character list failed: ${e.message}`)
cb([])
})
})
})

server.listen(port)
7 changes: 7 additions & 0 deletions shared/socket.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
export type CharacterShort = {
name: string
}

export type CharacterList = CharacterShort[]

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface ServerToClientEvents {}

export interface ClientToServerEvents {
ping: (callback: () => void) => void
characterList: (cb: (characters: CharacterList) => void) => void
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand Down

0 comments on commit dde6c34

Please sign in to comment.