@@ -3,6 +3,7 @@ local M = {}
33M ._client = nil
44M ._connection = nil
55M ._cid_counter = 0
6+ M ._pending = {}
67M ._callbacks = {
78 on_connected = nil ,
89 on_disconnected = nil ,
@@ -17,6 +18,9 @@ M._callbacks = {
1718 on_notification = nil ,
1819 on_matchmaker_matched = nil ,
1920 on_presence_changed = nil ,
21+ on_dm_message = nil ,
22+ on_world_list = nil ,
23+ on_world_joined = nil ,
2024 on_error = nil ,
2125}
2226
@@ -92,6 +96,10 @@ function M.cast_veto(vote_id)
9296 M ._send (" vote.veto" , {vote_id = vote_id })
9397end
9498
99+ function M .send_dm (recipient_id , content )
100+ M ._send (" dm.send" , {recipient_id = recipient_id , content = content })
101+ end
102+
95103function M .update_presence (status )
96104 M ._send (" presence.update" , {status = status or " online" })
97105end
@@ -100,6 +108,32 @@ function M.send_heartbeat()
100108 M ._send_fire_and_forget (" session.heartbeat" , {})
101109end
102110
111+ function M .list_worlds (mode , callback )
112+ local payload = {}
113+ if mode then payload .mode = mode end
114+ M ._send_with_callback (" world.list" , payload , callback )
115+ end
116+
117+ function M .create_world (mode , callback )
118+ M ._send_with_callback (" world.create" , {mode = mode }, callback )
119+ end
120+
121+ function M .join_world (world_id , callback )
122+ M ._send_with_callback (" world.join" , {world_id = world_id }, callback )
123+ end
124+
125+ function M .find_or_create_world (mode , callback )
126+ M ._send_with_callback (" world.find_or_create" , {mode = mode }, callback )
127+ end
128+
129+ function M .send_world_input (input )
130+ M ._send_fire_and_forget (" world.input" , input )
131+ end
132+
133+ function M .leave_world ()
134+ M ._send (" world.leave" , {})
135+ end
136+
103137function M .on (event , callback )
104138 M ._callbacks [" on_" .. event ] = callback
105139end
@@ -111,6 +145,20 @@ function M._send(msg_type, payload)
111145 websocket .send (M ._connection , msg , {type = websocket .DATA_TYPE_TEXT })
112146end
113147
148+ function M ._send_with_callback (msg_type , payload , callback )
149+ if not M ._connection then
150+ if callback then callback (nil , " not connected" ) end
151+ return
152+ end
153+ M ._cid_counter = M ._cid_counter + 1
154+ local cid = tostring (M ._cid_counter )
155+ if callback then
156+ M ._pending [cid ] = callback
157+ end
158+ local msg = json .encode ({type = msg_type , payload = payload , cid = cid })
159+ websocket .send (M ._connection , msg , {type = websocket .DATA_TYPE_TEXT })
160+ end
161+
114162function M ._send_fire_and_forget (msg_type , payload )
115163 if not M ._connection then return end
116164 local msg = json .encode ({type = msg_type , payload = payload })
@@ -123,6 +171,19 @@ function M._handle_message(raw)
123171
124172 local msg_type = msg .type or " "
125173 local payload = msg .payload or {}
174+ local cid = msg .cid
175+
176+ -- Check for pending request-response callbacks
177+ if cid and M ._pending [cid ] then
178+ local cb = M ._pending [cid ]
179+ M ._pending [cid ] = nil
180+ if msg_type == " error" then
181+ cb (nil , payload .reason or " unknown error" )
182+ else
183+ cb (payload , nil )
184+ end
185+ return
186+ end
126187
127188 local handlers = {
128189 [" session.connected" ] = " on_connected" ,
@@ -137,6 +198,10 @@ function M._handle_message(raw)
137198 [" notification.new" ] = " on_notification" ,
138199 [" match.matched" ] = " on_matchmaker_matched" ,
139200 [" presence.changed" ] = " on_presence_changed" ,
201+ [" dm.message" ] = " on_dm_message" ,
202+ [" world.list" ] = " on_world_list" ,
203+ [" world.joined" ] = " on_world_joined" ,
204+ [" world.tick" ] = " on_match_state" ,
140205 [" error" ] = " on_error" ,
141206 }
142207
0 commit comments