2026-10-28 16:57:43 +00:00
|
|
|
import { createApiClient, type WsClientConfig } from '@blax-software/networking'
|
|
|
|
|
import { createVueWsClient, type VueWsClient } from '@blax-software/networking/vue'
|
2026-10-28 18:26:16 +00:00
|
|
|
import type { AuthResult, GameError } from '~/types/game'
|
2026-10-28 16:57:43 +00:00
|
|
|
import { createMockServer } from '~/mock/server'
|
|
|
|
|
|
|
|
|
|
/** The part of the WS client the game uses. The mock server implements the same surface. */
|
|
|
|
|
export type GameWs = Pick<
|
|
|
|
|
VueWsClient,
|
|
|
|
|
'send' | 'listen' | 'listenWhileMounted' | 'is_setup' | 'is_opened' | 'resetConnection'
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
|
/** HTTP is only for getting the bearer token; everything else goes over `ws`. */
|
|
|
|
|
export interface GameAuth {
|
|
|
|
|
token(): string | null
|
|
|
|
|
login(email: string, password: string): Promise<AuthResult>
|
|
|
|
|
register(name: string, email: string, password: string): Promise<AuthResult>
|
|
|
|
|
logout(): void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface NetworkingOptions {
|
|
|
|
|
serverUrl: string
|
|
|
|
|
wsUrl: string
|
|
|
|
|
appKey: string
|
|
|
|
|
mock: boolean
|
|
|
|
|
notify?: WsClientConfig['notify']
|
|
|
|
|
translate?: WsClientConfig['translate']
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The app's singletons, assigned once by plugins/networking.ts before any page runs.
|
|
|
|
|
export let ws: GameWs
|
|
|
|
|
export let gameAuth: GameAuth
|
|
|
|
|
export let isMockServer = false
|
|
|
|
|
|
|
|
|
|
export function setupNetworking(options: NetworkingOptions) {
|
|
|
|
|
if (options.mock) {
|
|
|
|
|
const mock = createMockServer()
|
|
|
|
|
ws = mock.ws
|
|
|
|
|
gameAuth = mock.auth
|
|
|
|
|
isMockServer = true
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-28 18:26:16 +00:00
|
|
|
// Bearer token only, no cookies, so the API can allow any origin (web and Steam).
|
|
|
|
|
const api = createApiClient({ serverUrl: options.serverUrl, notify: options.notify, withCredentials: false })
|
2026-10-28 16:57:43 +00:00
|
|
|
api.loadBearerFromStorage()
|
|
|
|
|
|
|
|
|
|
const client = createVueWsClient({
|
|
|
|
|
url: options.wsUrl,
|
|
|
|
|
appKey: options.appKey,
|
|
|
|
|
getAuthToken: () => api.bearerToken || null,
|
|
|
|
|
notify: options.notify,
|
|
|
|
|
translate: options.translate,
|
|
|
|
|
sendTimeout: 15000,
|
|
|
|
|
rejectOnDisconnect: true,
|
|
|
|
|
})
|
2026-10-28 18:26:16 +00:00
|
|
|
|
|
|
|
|
// The WS server authenticates a socket from `authtoken` in the message data (as in
|
|
|
|
|
// learn-atc), so every send carries the bearer token.
|
|
|
|
|
const rawSend = client.send.bind(client)
|
|
|
|
|
client.send = ((event: string, data: object = {}, channel?: string | null, progress?: any) =>
|
|
|
|
|
rawSend(event, { ...data, authtoken: api.bearerToken || undefined }, channel, progress)) as typeof client.send
|
|
|
|
|
|
2026-10-28 16:57:43 +00:00
|
|
|
client.setAppReady()
|
|
|
|
|
ws = client
|
|
|
|
|
|
|
|
|
|
const signIn = (result: AuthResult) => {
|
|
|
|
|
api.setBearer(result.token)
|
|
|
|
|
client.resetConnection()
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-28 18:26:16 +00:00
|
|
|
// Pass the server's `{ code, message }` on, like a refused WS intent.
|
|
|
|
|
const refused = (e: any): never => {
|
|
|
|
|
throw { message: e?.response?.data?.message ?? e?.message, code: e?.response?.data?.code } satisfies GameError
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-28 16:57:43 +00:00
|
|
|
gameAuth = {
|
|
|
|
|
token: () => api.bearerToken || null,
|
|
|
|
|
login: async (email, password) =>
|
2026-10-28 18:26:16 +00:00
|
|
|
signIn((await api.post<AuthResult>('auth/login', { email, password }).catch(refused)).data),
|
2026-10-28 16:57:43 +00:00
|
|
|
register: async (name, email, password) =>
|
2026-10-28 18:26:16 +00:00
|
|
|
signIn((await api.post<AuthResult>('auth/register', { name, email, password }).catch(refused)).data),
|
2026-10-28 16:57:43 +00:00
|
|
|
logout() {
|
|
|
|
|
api.setBearer(null)
|
|
|
|
|
client.resetConnection()
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|