Skip to content

Commit f829c5a

Browse files
committed
Privileged jokers
1 parent 1ccf422 commit f829c5a

4 files changed

Lines changed: 46 additions & 24 deletions

File tree

api/connection.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ MPAPI.connection_state = {
2020
is_temp = false,
2121
use_discord_name = false,
2222
preferred_joker = 'j_joker',
23+
privileges = {},
2324
}
2425

2526
local _mqtt_instance = nil
@@ -137,6 +138,10 @@ MPAPI.is_ready = function()
137138
return _ready
138139
end
139140

141+
MPAPI.get_privileges = function()
142+
return MPAPI.connection_state.privileges
143+
end
144+
140145
-----------------------------
141146
-- INTERNAL FUNCTIONS
142147
-----------------------------
@@ -274,6 +279,7 @@ connection_on_state_change = function(new_state, context)
274279
MPAPI.connection_state.is_temp = _connection.is_temp or false
275280
MPAPI.connection_state.use_discord_name = _connection.use_discord_name or false
276281
MPAPI.connection_state.preferred_joker = _connection.preferred_joker or 'j_joker'
282+
MPAPI.connection_state.privileges = _connection.privileges
277283
end
278284
set_connection_state_status_text()
279285

@@ -306,6 +312,7 @@ reset_connection_state_variables = function()
306312
MPAPI.connection_state.is_temp = false
307313
MPAPI.connection_state.use_discord_name = false
308314
MPAPI.connection_state.preferred_joker = 'j_joker'
315+
MPAPI.connection_state.privileges = nil
309316
end
310317

311318
set_connection_state_status_text = function()

lib/util.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,22 @@ MPAPI.shallow_copy = function(t)
66
end
77
return out
88
end
9+
10+
-- Merges two tables with unique values, preserves order
11+
MPAPI.merge_unique = function(a, b)
12+
local seen = {}
13+
local out = {}
14+
for _, v in ipairs(a) do
15+
if not seen[v] then
16+
seen[v] = true
17+
out[#out + 1] = v
18+
end
19+
end
20+
for _, v in ipairs(b) do
21+
if not seen[v] then
22+
seen[v] = true
23+
out[#out + 1] = v
24+
end
25+
end
26+
return out
27+
end

networking/connection.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function connection.new(opts)
2222
display_name = nil,
2323
use_discord_name = false,
2424
preferred_joker = 'j_joker',
25+
privileges = {},
2526
steam_id = nil,
2627
discord_name = nil,
2728
is_temp = false,
@@ -69,6 +70,9 @@ function connection:_handle_auth_success(data)
6970
self.display_name = data.player.displayName or self.steam_name
7071
self.use_discord_name = data.player.useDiscordName or false
7172
self.preferred_joker = data.player.preferredJoker or 'j_joker'
73+
if data.player.privileges then
74+
self.privileges = data.player.privileges
75+
end
7276
end
7377

7478
self.lobby_data = data.lobby or nil

ui/avatar_selection.lua

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,15 @@ local AVATAR_JOKERS = {
4646
'j_idol',
4747
'j_matador',
4848
'j_stuntman',
49-
'j_hack',
50-
'j_hologram',
51-
'j_vagabond',
52-
'j_photograph',
53-
'j_hallucination',
54-
'j_baseball',
55-
'j_sock_and_buskin',
56-
'j_blueprint',
57-
'j_invisible',
58-
'j_brainstorm',
59-
'j_caino',
60-
'j_triboulet',
61-
'j_yorick',
62-
'j_chicot',
63-
'j_perkeo',
6449
}
6550

66-
local avatar_centers = {}
67-
for _, id in ipairs(AVATAR_JOKERS) do
68-
if G.P_CENTERS[id] then
69-
avatar_centers[#avatar_centers + 1] = G.P_CENTERS[id]
70-
end
71-
end
72-
7351
-----------------------------
7452
-- STATE VARIABLES
7553
-----------------------------
7654

7755
local _card_rows = {}
7856
local _joker_tables = {}
57+
local _avatar_centers = {}
7958

8059
-----------------------------
8160
-- UI FUNCTIONS
@@ -84,10 +63,11 @@ local _joker_tables = {}
8463
local create_UIBox_avatar_selection = function()
8564
_joker_tables = {}
8665

66+
calculate_avatar_centers(avatar_jokers_to_use)
8767
create_card_rows()
8868
populate_page(1)
8969

90-
local total_pages = math.ceil(#avatar_centers / CARDS_PER_PAGE)
70+
local total_pages = math.ceil(#_avatar_centers / CARDS_PER_PAGE)
9171
local page_options = {}
9272
for i = 1, total_pages do
9373
page_options[#page_options + 1] = localize('k_page') .. ' ' .. tostring(i) .. '/' .. tostring(total_pages)
@@ -122,6 +102,18 @@ local create_UIBox_avatar_selection = function()
122102
})
123103
end
124104

105+
calculate_avatar_centers = function()
106+
local privilege_jokers = MPAPI.get_privileges()
107+
local avatar_jokers_to_use = privilege_jokers.jokers and MPAPI.merge_unique(AVATAR_JOKERS, privilege_jokers.jokers) or AVATAR_JOKERS
108+
109+
_avatar_centers = {}
110+
for _, id in ipairs(avatar_jokers_to_use) do
111+
if G.P_CENTERS[id] then
112+
_avatar_centers[#_avatar_centers + 1] = G.P_CENTERS[id]
113+
end
114+
end
115+
end
116+
125117
create_card_rows = function()
126118
_card_rows = {}
127119
for j = 1, ROWS do
@@ -150,7 +142,7 @@ populate_page = function(page)
150142
local page_offset = CARDS_PER_PAGE * (page - 1)
151143
for i = 1, COLS do
152144
for j = 1, ROWS do
153-
local center = avatar_centers[i + (j - 1) * COLS + page_offset]
145+
local center = _avatar_centers[i + (j - 1) * COLS + page_offset]
154146
if not center then
155147
break
156148
end

0 commit comments

Comments
 (0)