import type { ChatChannel, ChatHistory, ChatMessage } from '~/types/game' interface ChatState { worldId: number | null open: boolean channel: string channels: ChatChannel[] messages: Record more: Record } /** * The chat of the current world, shared by the window and the menu's unread count. The * window (GameChat) listens for `chat:message` while it is mounted. */ export function useChat() { const state = useState('chat', () => ({ worldId: null, open: false, channel: 'world', channels: [], messages: {}, more: {} })) const error = ref(null) const errorText = useErrorText() const unread = computed(() => state.value.channels.reduce((n, c) => n + c.unread, 0)) const channelOf = (name: string) => state.value.channels.find((c) => c.channel === name) async function call(event: string, data: object = {}): Promise { if (!state.value.worldId) return null error.value = null try { return await ws.send(event, { world_id: state.value.worldId, ...data }) } catch (e) { error.value = errorText(e) return null } } async function enter(worldId: number) { if (state.value.worldId !== worldId) state.value = { worldId, open: state.value.open, channel: 'world', channels: [], messages: {}, more: {} } const channels = await call('chat.channels') if (channels) state.value.channels = channels if (state.value.open) await show(state.value.channel) } /** Open a channel: its last messages, read from now on. */ async function show(channel: string) { state.value.channel = channel const history = await call('chat.history', { channel }) if (!history) return state.value.messages[channel] = history.messages state.value.more[channel] = history.more const c = channelOf(channel) if (c) c.unread = 0 } async function older() { const channel = state.value.channel const first = state.value.messages[channel]?.[0] if (!first) return const history = await call('chat.history', { channel, before: first.id }) if (!history) return state.value.messages[channel] = [...history.messages, ...(state.value.messages[channel] ?? [])] state.value.more[channel] = history.more } async function send(body: string) { const message = await call('chat.send', { channel: state.value.channel, body }) if (message) receive(state.value.channel, message, true) return !!message } /** Start a conversation with a player by name. */ async function talkTo(name: string) { const channel = await call('chat.open', { player: name }) if (!channel) return false if (!channelOf(channel.channel)) state.value.channels.push(channel) await show(channel.channel) return true } /** A message arrived (pushed, or our own sent one); shown once, counted unread when not in view. */ function receive(channel: string, message: ChatMessage, own = false) { const list = (state.value.messages[channel] ??= []) if (list.some((m) => m.id === message.id)) return list.push(message) let c = channelOf(channel) if (!c) { c = { channel, with: channel.startsWith('dm:') ? message.player : null, unread: 0 } state.value.channels.push(c) } const seen = state.value.open && state.value.channel === channel if (seen && !own) call('chat.read', { channel }) else if (!seen && !own) c.unread++ } async function toggle() { state.value.open = !state.value.open if (state.value.open) await show(state.value.channel) } return { state, error, unread, enter, show, older, send, talkTo, receive, toggle } }