Skip to content

Commit bf5cb1e

Browse files
committed
Add preparations for squads update to BotBalancer
1 parent b5122d5 commit bf5cb1e

1 file changed

Lines changed: 57 additions & 57 deletions

File tree

BotBalancer/server/__init__.lua

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -172,56 +172,6 @@ function ResetBots()
172172
end
173173

174174

175-
function randomizeTeams()
176-
if not hasServerInitialized then
177-
return
178-
end
179-
180-
local randomTeamTable = {}
181-
local playerCount = getPlayerCount()
182-
183-
if playerCount < 2 then
184-
Console.Execute("Kyber.Broadcast **KYBER:** Skipped team shuffle. (More than 2 players required)")
185-
return
186-
end
187-
188-
-- Fill randomTeamTable
189-
for i = 1, playerCount - (playerCount // 2), 1 do
190-
table.insert(randomTeamTable, 1)
191-
end
192-
for i = (playerCount - (playerCount // 2)) + 1, playerCount, 1 do
193-
table.insert(randomTeamTable, 2)
194-
end
195-
196-
-- Shuffles inputted table (https://stackoverflow.com/questions/35572435/how-do-you-do-the-fisher-yates-shuffle-in-lua)
197-
local function shuffleTable(t)
198-
for i = #t, 2, -1 do
199-
local j = math.random(i)
200-
t[i], t[j] = t[j], t[i]
201-
end
202-
end
203-
204-
shuffleTable(randomTeamTable)
205-
206-
-- Set teams
207-
local players = PlayerManager.GetPlayers()
208-
local i = 1
209-
for _, player in ipairs(players) do
210-
if player.isBot then
211-
goto continue
212-
end
213-
214-
player:SetTeam(randomTeamTable[i])
215-
print(string.format("Debug: Put player '%s' on team '%d'", player.name, randomTeamTable[i]))
216-
i = i + 1
217-
::continue::
218-
end
219-
220-
-- Disabled message because of chat spam
221-
-- Console.Execute("Kyber.Broadcast Successfully randomized teams!")
222-
223-
end
224-
225175
-- This event is triggered when a level is loaded.
226176
-- The game modes will have been loaded by this point,
227177
-- so we can determine the max player count of the mode.
@@ -252,20 +202,17 @@ EventManager.Listen("Level:Loaded", function(levelName, gameModeId)
252202

253203
print("Disabled traditional team balancing in favor for Kyber team balancing.")
254204

255-
-- Randomizing teams
256-
randomizeTeams()
257-
205+
-- Enable built-in team shuffling
206+
kyberSettings.enableShuffleTeams = true
258207
end)
259208

260-
-- Team balancing
261-
262209
-- Finds the best fit team by looking at both team player counts
263210
-- If equal, set team to first
264211
function balancePlayer(player)
265212
local teamCounts = getTeamCounts()
266213

267-
if teamCounts[1] > teamCounts[2] then
268-
player:SetTeam(2)
214+
if teamCounts[1] > teamCounts[2] then
215+
player:SetTeam(2)
269216
else
270217
-- then teams are either equal (which we want to set to team 1) or
271218
-- Team 2 has more and we want to set to team 1
@@ -292,3 +239,56 @@ EventManager.Listen("Server:PlayerJoined", function(player)
292239

293240
balancePlayer(player)
294241
end)
242+
243+
-- Deprecated: Team shuffling
244+
-- Kyber now has a built-in shuffle teams feature that accounts for parties/squads
245+
-- Keeping for legacy purposes
246+
247+
function randomizeTeams()
248+
if not hasServerInitialized then
249+
return
250+
end
251+
252+
local randomTeamTable = {}
253+
local playerCount = getPlayerCount()
254+
255+
if playerCount < 2 then
256+
Console.Execute("Kyber.Broadcast **KYBER:** Skipped team shuffle. (More than 2 players required)")
257+
return
258+
end
259+
260+
-- Fill randomTeamTable
261+
for i = 1, playerCount - (playerCount // 2), 1 do
262+
table.insert(randomTeamTable, 1)
263+
end
264+
for i = (playerCount - (playerCount // 2)) + 1, playerCount, 1 do
265+
table.insert(randomTeamTable, 2)
266+
end
267+
268+
-- Shuffles inputted table (https://stackoverflow.com/questions/35572435/how-do-you-do-the-fisher-yates-shuffle-in-lua)
269+
local function shuffleTable(t)
270+
for i = #t, 2, -1 do
271+
local j = math.random(i)
272+
t[i], t[j] = t[j], t[i]
273+
end
274+
end
275+
276+
shuffleTable(randomTeamTable)
277+
278+
-- Set teams
279+
local players = PlayerManager.GetPlayers()
280+
local i = 1
281+
for _, player in ipairs(players) do
282+
if player.isBot then
283+
goto continue
284+
end
285+
286+
player:SetTeam(randomTeamTable[i])
287+
print(string.format("Debug: Put player '%s' on team '%d'", player.name, randomTeamTable[i]))
288+
i = i + 1
289+
::continue::
290+
end
291+
292+
-- Disabled message because of chat spam
293+
-- Console.Execute("Kyber.Broadcast Successfully randomized teams!")
294+
end

0 commit comments

Comments
 (0)