Skip to content

Commit e3ba0c2

Browse files
committed
Some lobby stuff
1 parent c1b0c6f commit e3ba0c2

File tree

7 files changed

+587
-11
lines changed

7 files changed

+587
-11
lines changed

.github/workflows/strip-dev.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Strip dev files for release
2+
3+
on:
4+
push:
5+
branches:
6+
- mqtt-main
7+
8+
jobs:
9+
strip-dev:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: write
13+
14+
steps:
15+
- uses: actions/checkout@v4
16+
with:
17+
ref: mqtt-main
18+
19+
- name: Strip dev/ directory
20+
run: |
21+
if [ -d "dev" ]; then
22+
git config user.name "github-actions[bot]"
23+
git config user.email "github-actions[bot]@users.noreply.github.com"
24+
git rm -r dev/
25+
git commit -m "Strip dev/ directory for main"
26+
git push
27+
else
28+
echo "No dev/ directory found, nothing to strip"
29+
fi

api/lobby.lua

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ local subscribe_all
55
local handle_event
66
local handle_metadata
77
local handle_own_state
8+
local handle_player_info
9+
local populate_initial_players
810
local cleanup_lobby
911

1012
-----------------------------
@@ -71,8 +73,8 @@ MPAPI.join_lobby = function(mod_id, code, opts)
7173
})
7274

7375
_current_lobby = lobby
74-
75-
conn.api:join_lobby(conn.jwt_token, code, )
76+
77+
conn.api:join_lobby(conn.jwt_token, code, join_lobby_callback)
7678

7779
return lobby
7880
end
@@ -97,13 +99,15 @@ MPAPI._internal.create_reconnected_lobby = function(lobby_data)
9799
code = lobby_data.code,
98100
mod_id = lobby_data.modId,
99101
is_host = lobby_data.isHost or false,
102+
max_players = lobby_data.maxPlayers,
100103
player_id = conn.player_id,
101104
mqtt = mqtt,
102105
api = conn.api,
103106
connection = conn,
104107
metadata = lobby_data.metadata or {},
105108
})
106109

110+
populate_initial_players(lobby, lobby_data.players)
107111
subscribe_all(lobby)
108112
_current_lobby = lobby
109113
lobby:_fire('connected')
@@ -119,12 +123,14 @@ join_lobby_callback = function(err, data)
119123
return
120124
end
121125

122-
conn.jwt_token = data.token
126+
_current_lobby._connection.jwt_token = data.token
123127

124128
_current_lobby.code = data.lobby.code
125129
_current_lobby.is_host = data.lobby.isHost or false
126-
lo_current_lobbybby._metadata = data.lobby.metadata or {}
130+
_current_lobby.max_players = data.lobby.maxPlayers or 16
131+
_current_lobby._metadata = data.lobby.metadata or {}
127132

133+
populate_initial_players(_current_lobby, data.lobby.players)
128134
subscribe_all(_current_lobby)
129135
_current_lobby:_fire('connected')
130136
end
@@ -135,12 +141,14 @@ create_lobby_callback = function(err, data)
135141
return
136142
end
137143

138-
conn.jwt_token = data.token
144+
_current_lobby._connection.jwt_token = data.token
139145

140146
_current_lobby.code = data.lobby.code
141147
_current_lobby.is_host = true
148+
_current_lobby.max_players = data.lobby.maxPlayers or 16
142149
_current_lobby._metadata = data.lobby.metadata or {}
143150

151+
populate_initial_players(_current_lobby, data.lobby.players)
144152
subscribe_all(_current_lobby)
145153
_current_lobby:_fire('connected')
146154
end
@@ -150,6 +158,7 @@ create_lobby_object = function(opts)
150158
code = opts.code,
151159
mod_id = opts.mod_id,
152160
is_host = opts.is_host or false,
161+
max_players = opts.max_players or 16,
153162
player_id = opts.player_id,
154163
_mqtt = opts.mqtt,
155164
_api = opts.api,
@@ -265,6 +274,11 @@ subscribe_all = function(lobby)
265274
lobby._mqtt:subscribe(state_topic, 1, function(topic, payload)
266275
handle_own_state(lobby, payload)
267276
end)
277+
278+
local info_topic = lobby._mqtt:lobby_topic(lobby.code, 'players/+/info')
279+
lobby._mqtt:subscribe(info_topic, 1, function(topic, payload)
280+
handle_player_info(lobby, topic, payload)
281+
end)
268282
end
269283

270284
handle_event = function(lobby, payload)
@@ -281,11 +295,10 @@ handle_event = function(lobby, payload)
281295

282296
if event_type == 'player_joined' then
283297
if data.playerId then
284-
lobby._players[data.playerId] = {
285-
id = data.playerId,
286-
displayName = data.displayName,
287-
is_away = false,
288-
}
298+
lobby._players[data.playerId] = lobby._players[data.playerId] or {}
299+
lobby._players[data.playerId].id = data.playerId
300+
lobby._players[data.playerId].displayName = data.displayName
301+
lobby._players[data.playerId].is_away = false
289302
end
290303
lobby:_fire('player_joined', data.playerId)
291304
elseif event_type == 'player_left' then
@@ -346,6 +359,57 @@ handle_own_state = function(lobby, payload)
346359
lobby._player_state = data
347360
end
348361

362+
handle_player_info = function(lobby, topic, payload)
363+
if lobby._destroyed then
364+
return
365+
end
366+
367+
local player_id = topic:match('/players/([^/]+)/info$')
368+
if not player_id then
369+
return
370+
end
371+
372+
if not payload or payload == '' then
373+
if lobby._players[player_id] then
374+
lobby._players[player_id] = nil
375+
lobby:_fire('player_left', player_id)
376+
end
377+
return
378+
end
379+
380+
local ok, data = pcall(MPAPI.json_decode, payload)
381+
if not ok or not data then
382+
return
383+
end
384+
385+
local is_new = not lobby._players[player_id]
386+
lobby._players[player_id] = lobby._players[player_id] or {}
387+
lobby._players[player_id].id = player_id
388+
lobby._players[player_id].displayName = data.displayName
389+
lobby._players[player_id].preferredJoker = data.preferredJoker
390+
391+
if is_new then
392+
lobby:_fire('player_joined', player_id)
393+
end
394+
lobby:_fire('player_info', player_id, lobby._players[player_id])
395+
end
396+
397+
populate_initial_players = function(lobby, players)
398+
if not players then
399+
return
400+
end
401+
for _, p in ipairs(players) do
402+
if p.id then
403+
lobby._players[p.id] = {
404+
id = p.id,
405+
displayName = p.displayName,
406+
preferredJoker = p.preferredJoker,
407+
is_away = false,
408+
}
409+
end
410+
end
411+
end
412+
349413
cleanup_lobby = function(lobby)
350414
if lobby._destroyed then
351415
return
@@ -356,6 +420,7 @@ cleanup_lobby = function(lobby)
356420
lobby._mqtt:unsubscribe(lobby._mqtt:lobby_topic(lobby.code, 'events'))
357421
lobby._mqtt:unsubscribe(lobby._mqtt:lobby_topic(lobby.code, 'metadata'))
358422
lobby._mqtt:unsubscribe(lobby._mqtt:lobby_topic(lobby.code, 'players/' .. lobby.player_id .. '/state'))
423+
lobby._mqtt:unsubscribe(lobby._mqtt:lobby_topic(lobby.code, 'players/+/info'))
359424
end
360425

361426
if _current_lobby == lobby then

core.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ MPAPI.load_mpapi_dir('lib')
107107
MPAPI.load_mpapi_dir('api')
108108
MPAPI.load_mpapi_dir('ui')
109109

110+
-- Load dev overrides if the dev/ directory exists (stripped in release builds)
111+
local dev_init = MPAPI.load_mpapi_file('dev/init.lua')
112+
if dev_init then
113+
MPAPI.sendDebugMessage('Dev overrides loaded')
114+
end
115+
110116
G.E_MANAGER:add_event(Event({
111117
blockable = false,
112118
blocking = false,

dev/init.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- Dev overrides: This file (and the dev/ directory) is stripped from release builds by CI.
2+
local api_client = MPAPI.modules.api_client
3+
local connection = MPAPI.modules.connection
4+
5+
function api_client.authenticate_impersonate(self, steam_name, callback)
6+
if not self.mqtt or not self.mqtt.tx_channel then
7+
callback('MQTT thread not running', nil)
8+
return
9+
end
10+
11+
self:_setup_http_callback(callback)
12+
13+
local json_encode = (json and json.encode) and json.encode or require('json').encode
14+
local body = json_encode({ steamName = steam_name })
15+
self.mqtt:http_post(self.base_url .. '/api/auth/dev/impersonate', body)
16+
end
17+
18+
function connection._try_impersonate_auth(self, steam_name)
19+
self.mqtt:start_thread()
20+
21+
self.api:authenticate_impersonate(steam_name, function(err, data)
22+
if err then
23+
MPAPI.sendWarnMessage('Impersonate failed: ' .. tostring(err) .. ', falling back to Steam')
24+
self:_try_steam_auth()
25+
return
26+
end
27+
28+
self:_handle_auth_success(data)
29+
end)
30+
end
31+
32+
function connection._start_auth(self)
33+
self:_try_impersonate_auth('Bean')
34+
end
35+
36+
MPAPI.sendDebugMessage('Dev auth overrides applied')

localization/en-us.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ return {
2727
b_by_code_cap = 'BY CODE',
2828
b_from_clipboard_cap = 'FROM CLIPBOARD',
2929
b_find_game_cap = 'FIND GAME',
30+
b_open_lobby_cap = { 'OPEN', 'LOBBY' },
31+
b_lobby_options_cap = { 'LOBBY', 'OPTIONS' },
32+
b_leave_lobby_cap = { 'LEAVE', 'LOBBY' },
3033
},
3134
},
3235
}

networking/connection.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,18 @@ function connection:_try_refresh_auth(steam_error)
152152
end)
153153
end
154154

155+
function connection:_start_auth()
156+
self:_try_steam_auth()
157+
end
158+
155159
function connection:connect()
156160
if self.state ~= STATES.DISCONNECTED then
157161
fire(self, self.state, { error = 'Already ' .. self.state })
158162
return
159163
end
160164

161165
set_state(self, STATES.AUTHENTICATING)
162-
self:_try_steam_auth()
166+
self:_start_auth()
163167
end
164168

165169
function connection:_mqtt_connect_with_credentials()

0 commit comments

Comments
 (0)