Files
sor/Client/origin-client/autoloads/Chat.gd
T
52419e2199 Chat module — overlay, friends, presence, DMs, context menus
- Chat.gd autoload: data layer for channels, messages, social graph,
  presence, and notifications; wires to Network/Auth singletons
- ChatOverlay.tscn + chat_overlay.gd: CanvasLayer (layer=100) that
  persists across all scenes; bubble toggle at bottom-right corner
- Six tabs: Friends, Presence, Global, Lobby, Game, DMs
- Message rows show [HH:MM] [source] Name: text with color-coded
  source badges (global/lobby/game/private/party/system)
- Friends list: online/offline sections, right-click context menu
  (Private Message, Set Nickname, Block, Remove Friend)
- Player name click: Add Friend, Invite to Party (stub), Mute/Unmute,
  Hi Scores (disabled placeholder)
- Presence tab: own status selector (Online/Away/Offline) + friends
  presence overview with status dots
- DMs tab: conversation sidebar + message pane, input bar shown when
  a conversation is selected
- Unread notification badge on chat bubble
- Per-session nickname overrides and muting

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
2026-03-05 05:48:09 -05:00

289 lines
9.7 KiB
GDScript

extends Node
## Chat autoload — manages all chat state and server communication.
##
## On auth_success, fetches channels, social graph, and notifications,
## then sets own presence to Online. Adds the ChatOverlay CanvasLayer to
## the viewport root so it persists across scene changes.
##
## Public signals let the overlay (and any other UI) react to changes.
##
## Usage:
## Chat.send_message(channel_id, "Hello!")
## Chat.open_dm(other_account_id)
## Chat.social_updated ← emit when friends / requests / blocked change
# ── Signals ───────────────────────────────────────────────────────────────────
## Emitted when the channels array changes (initial load, DM opened, etc.)
signal channels_updated
## Emitted for every server-pushed or locally echoed message.
## msg is a ChatMessageDto dictionary.
signal message_received(channel_id: String, msg: Dictionary)
## Emitted after friends / pending_requests / blocked_users are refreshed.
signal social_updated
## Emitted after the presence dictionary is updated.
signal presence_updated
## Emitted after notifications array changes.
signal notifications_updated
# ── State ─────────────────────────────────────────────────────────────────────
## Array[Dictionary] — ChatChannelDto {id, name, channelType, createdAt}
var channels: Array = []
## Dictionary[channel_id → Array[Dictionary]] — messages newest-first
var messages: Dictionary = {}
## Array[Dictionary] — FriendDto {accountId, displayName, friendsSince}
var friends: Array = []
## Array[Dictionary] — FriendRequestDto {requestId, otherAccountId, otherDisplayName, createdAt}
var pending_requests: Array = []
## Array[Dictionary] — BlockedUserDto {accountId, displayName, blockedAt}
var blocked_users: Array = []
## Dictionary[account_id → Dictionary] — PresenceDto
var presence: Dictionary = {}
## Array[Dictionary] — NotificationDto
var notifications: Array = []
# ── Private ───────────────────────────────────────────────────────────────────
var _overlay: CanvasLayer = null
# ── Lifecycle ─────────────────────────────────────────────────────────────────
func _ready() -> void:
Auth.auth_success.connect(_on_auth_success)
Auth.session_expired.connect(_clear_state)
Network.message_received.connect(_on_server_push)
# Defer adding the overlay so the scene tree is fully ready.
call_deferred("_add_overlay")
func _add_overlay() -> void:
var scene := load("res://scenes/chat/ChatOverlay.tscn") as PackedScene
if scene == null:
push_error("Chat: could not load res://scenes/chat/ChatOverlay.tscn")
return
_overlay = scene.instantiate() as CanvasLayer
get_tree().get_root().add_child(_overlay)
# ── Auth hooks ────────────────────────────────────────────────────────────────
func _on_auth_success(_uid: String, _dn: String) -> void:
_fetch_social()
_fetch_channels()
_fetch_notifications()
_set_own_presence("Online")
func _clear_state() -> void:
channels = []
messages = {}
friends = []
pending_requests = []
blocked_users = []
presence = {}
notifications = []
channels_updated.emit()
social_updated.emit()
# ── Public API ────────────────────────────────────────────────────────────────
func send_message(channel_id: String, content: String) -> void:
Network.send("chat.send", { "channelId": channel_id, "content": content.strip_edges() })
func fetch_messages(channel_id: String, before_id: String = "") -> void:
var payload: Dictionary = { "channelId": channel_id }
if not before_id.is_empty():
payload["beforeMessageId"] = before_id
Network.request("chat.get_messages", payload,
func(_t: String, p: Variant) -> void:
var msgs: Array = (p as Dictionary).get("messages", [])
if channel_id not in messages:
messages[channel_id] = []
messages[channel_id].append_array(msgs)
message_received.emit(channel_id, {})
,
func(_c: String, _m: String) -> void: pass
)
## Open or retrieve the Direct channel with recipient_account_id.
## Emits channels_updated on success so the DMs tab can refresh.
func open_dm(recipient_account_id: String) -> void:
Network.request("chat.dm_open", { "recipientAccountId": recipient_account_id },
func(_t: String, p: Variant) -> void:
var chan: Dictionary = (p as Dictionary).get("channel", {})
if chan.is_empty():
return
var cid: String = chan.get("id", "")
# Add to local list if not already present.
var found := false
for c: Dictionary in channels:
if c.get("id", "") == cid:
found = true
break
if not found:
channels.push_front(chan)
channels_updated.emit()
,
func(_c: String, _m: String) -> void: pass
)
func send_friend_request(recipient_id: String) -> void:
Network.send("chat.friend_request", { "recipientAccountId": recipient_id })
func respond_friend_request(request_id: String, accept: bool) -> void:
Network.request("chat.friend_respond",
{ "requestId": request_id, "accept": accept },
func(_t: String, _p: Variant) -> void: _fetch_social(),
func(_c: String, _m: String) -> void: pass
)
func remove_friend(friend_account_id: String) -> void:
Network.request("chat.friend_remove",
{ "friendAccountId": friend_account_id },
func(_t: String, _p: Variant) -> void: _fetch_social(),
func(_c: String, _m: String) -> void: pass
)
func block_user(target_account_id: String) -> void:
Network.request("chat.block",
{ "targetAccountId": target_account_id },
func(_t: String, _p: Variant) -> void: _fetch_social(),
func(_c: String, _m: String) -> void: pass
)
func unblock_user(target_account_id: String) -> void:
Network.request("chat.unblock",
{ "targetAccountId": target_account_id },
func(_t: String, _p: Variant) -> void: _fetch_social(),
func(_c: String, _m: String) -> void: pass
)
func set_presence(status: String) -> void:
_set_own_presence(status)
func fetch_presence(account_ids: Array) -> void:
if account_ids.is_empty():
return
Network.request("chat.presence.get", { "accountIds": account_ids },
func(_t: String, p: Variant) -> void:
for pr: Dictionary in (p as Dictionary).get("presences", []):
presence[pr.get("accountId", "")] = pr
presence_updated.emit()
,
func(_c: String, _m: String) -> void: pass
)
func mark_notifications_read(ids: Array) -> void:
if ids.is_empty():
return
Network.send("chat.notifications.read", { "notificationIds": ids })
for n: Dictionary in notifications:
if n.get("id", "") in ids:
n["readAt"] = Time.get_datetime_string_from_system()
notifications_updated.emit()
func get_unread_notification_count() -> int:
return notifications.filter(
func(n: Dictionary) -> bool: return n.get("readAt") == null
).size()
## Returns the first channel of the given ChannelType, or {} if none.
func get_channel_by_type(type_name: String) -> Dictionary:
for c: Dictionary in channels:
if c.get("channelType", "") == type_name:
return c
return {}
## Returns all Direct-type channels.
func get_dm_channels() -> Array:
return channels.filter(
func(c: Dictionary) -> bool: return c.get("channelType", "") == "Direct"
)
# ── Private helpers ───────────────────────────────────────────────────────────
func _set_own_presence(status: String) -> void:
Network.send("chat.presence.set", { "status": status })
func _fetch_social() -> void:
Network.request("chat.get_social", {},
func(_t: String, p: Variant) -> void:
var d := p as Dictionary
friends = d.get("friends", [])
pending_requests = d.get("pendingRequests", [])
blocked_users = d.get("blockedUsers", [])
social_updated.emit()
# Refresh presence for all friends.
var ids: Array = friends.map(func(f: Dictionary) -> String:
return f.get("accountId", ""))
fetch_presence(ids)
,
func(_c: String, _m: String) -> void: pass
)
func _fetch_channels() -> void:
Network.request("chat.get_channels", {},
func(_t: String, p: Variant) -> void:
channels = (p as Dictionary).get("channels", [])
channels_updated.emit()
,
func(_c: String, _m: String) -> void: pass
)
func _fetch_notifications() -> void:
Network.request("chat.notifications.get", { "unreadOnly": false },
func(_t: String, p: Variant) -> void:
notifications = (p as Dictionary).get("notifications", [])
notifications_updated.emit()
,
func(_c: String, _m: String) -> void: pass
)
func _on_server_push(type: String, payload: Variant) -> void:
match type:
"chat.message.push":
var msg: Dictionary = (payload as Dictionary).get("message", {})
var cid: String = msg.get("channelId", "")
if cid.is_empty():
return
if cid not in messages:
messages[cid] = []
messages[cid].push_front(msg)
message_received.emit(cid, msg)
"chat.friend_request.push":
_fetch_social()
"chat.notification.push":
var notif: Dictionary = (payload as Dictionary).get("notification", {})
if not notif.is_empty():
notifications.push_front(notif)
notifications_updated.emit()