|
20 | 20 | local config = require 'config.queue'
|
21 | 21 | local maxPlayers = GlobalState.MaxPlayers
|
22 | 22 |
|
23 |
| ----Player license to queue position map. |
24 |
| ----@type table<string, integer> |
25 |
| -local playerPositions = {} |
26 |
| -local queueSize = 0 |
| 23 | +---@class SubQueue : SubQueueConfig |
| 24 | +---@field positions table<string, number> Player license to sub-queue position map. |
| 25 | +---@field size number |
| 26 | + |
| 27 | +---@type SubQueue[] |
| 28 | +local subQueues = {} |
| 29 | +for i = 1, #config.subQueues do |
| 30 | + subQueues[i] = { |
| 31 | + name = config.subQueues[i].name, |
| 32 | + predicate = config.subQueues[i].predicate, |
| 33 | + positions = {}, |
| 34 | + size = 0, |
| 35 | + } |
| 36 | +end |
| 37 | + |
| 38 | +---@class PlayerQueueData |
| 39 | +---@field subQueueIndex number |
| 40 | +---@field globalPos number |
| 41 | + |
| 42 | +---Player license to queue data map. |
| 43 | +---@type table<string, PlayerQueueData> |
| 44 | +local playerDatas = {} |
| 45 | +local totalQueueSize = 0 |
27 | 46 |
|
28 | 47 | ---@param license string
|
29 |
| -local function enqueue(license) |
30 |
| - queueSize += 1 |
31 |
| - playerPositions[license] = queueSize |
| 48 | +---@param subQueueIndex number |
| 49 | +local function enqueue(license, subQueueIndex) |
| 50 | + local subQueue = subQueues[subQueueIndex] |
| 51 | + |
| 52 | + subQueue.size += 1 |
| 53 | + subQueue.positions[license] = subQueue.size |
| 54 | + |
| 55 | + local globalPos = subQueue.size |
| 56 | + -- increase set the global position of the current player by the sizes of sub-queues the player comes after |
| 57 | + for i = 1, subQueueIndex - 1 do |
| 58 | + globalPos += subQueues[i].size |
| 59 | + end |
| 60 | + |
| 61 | + totalQueueSize += 1 |
| 62 | + playerDatas[license] = { |
| 63 | + subQueueIndex = subQueueIndex, |
| 64 | + globalPos = globalPos, |
| 65 | + } |
| 66 | + |
| 67 | + -- inrease the global positions of players who are in sub-queues that come after the current player |
| 68 | + for i = subQueueIndex + 1, #subQueues do |
| 69 | + for k in pairs(subQueues[i].positions) do |
| 70 | + playerDatas[k].globalPos += 1 |
| 71 | + end |
| 72 | + end |
32 | 73 | end
|
33 | 74 |
|
34 | 75 | ---@param license string
|
35 | 76 | local function dequeue(license)
|
36 |
| - local pos = playerPositions[license] |
| 77 | + local subQueueIndex = playerDatas[license].subQueueIndex |
| 78 | + local subQueue = subQueues[subQueueIndex] |
| 79 | + local subQueuePos = subQueue.positions[license] |
37 | 80 |
|
38 |
| - queueSize -= 1 |
39 |
| - playerPositions[license] = nil |
| 81 | + subQueue.size -= 1 |
| 82 | + subQueue.positions[license] = nil |
40 | 83 |
|
41 |
| - -- decrease the positions of players who are after the current player in queue |
42 |
| - for k, v in pairs(playerPositions) do |
43 |
| - if v > pos then |
44 |
| - playerPositions[k] -= 1 |
| 84 | + totalQueueSize -= 1 |
| 85 | + playerDatas[license] = nil |
| 86 | + |
| 87 | + -- decrease the positions of players who are after the current player in the same sub-queue |
| 88 | + for k, v in pairs(subQueue.positions) do |
| 89 | + if v > subQueuePos then |
| 90 | + subQueue.positions[k] -= 1 |
| 91 | + playerDatas[k].globalPos -= 1 |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + -- decrease the global positions of players who are in sub-queues that come after the current player |
| 96 | + for i = subQueueIndex + 1, #subQueues do |
| 97 | + for k in pairs(subQueues[i].positions) do |
| 98 | + playerDatas[k].globalPos -= 1 |
45 | 99 | end
|
46 | 100 | end
|
47 | 101 | end
|
@@ -133,21 +187,40 @@ local function awaitPlayerQueue(source, license, deferrals)
|
133 | 187 | end
|
134 | 188 |
|
135 | 189 | local playerTimingOut = isPlayerTimingOut(license)
|
| 190 | + local data = playerDatas[license] |
136 | 191 |
|
137 |
| - if playerPositions[license] and not playerTimingOut then |
| 192 | + if data and not playerTimingOut then |
138 | 193 | deferrals.done(Lang:t('error.already_in_queue'))
|
139 | 194 | return
|
140 | 195 | end
|
141 | 196 |
|
142 | 197 | if not playerTimingOut then
|
143 |
| - enqueue(license) |
| 198 | + local subQueueIndex |
| 199 | + for i = 1, #subQueues do |
| 200 | + local predicate = subQueues[i].predicate |
| 201 | + if not predicate or predicate(source) then |
| 202 | + subQueueIndex = i |
| 203 | + break |
| 204 | + end |
| 205 | + end |
| 206 | + |
| 207 | + if not subQueueIndex then |
| 208 | + deferrals.done(Lang:t('error.no_subqueue')) |
| 209 | + return |
| 210 | + end |
| 211 | + |
| 212 | + enqueue(license, subQueueIndex) |
| 213 | + data = playerDatas[license] |
144 | 214 | end
|
145 | 215 |
|
| 216 | + local subQueue = subQueues[data.subQueueIndex] |
| 217 | + |
146 | 218 | -- wait until the player disconnected or until there are available slots and the player is first in queue
|
147 |
| - while DoesPlayerExist(source --[[@as string]]) and ((GetNumPlayerIndices() + joiningPlayerCount) >= maxPlayers or playerPositions[license] > 1) do |
| 219 | + while DoesPlayerExist(source --[[@as string]]) and ((GetNumPlayerIndices() + joiningPlayerCount) >= maxPlayers or data.globalPos > 1) do |
148 | 220 | deferrals.update(Lang:t('info.in_queue', {
|
149 |
| - queuePos = playerPositions[license], |
150 |
| - queueSize = queueSize, |
| 221 | + queuePos = data.globalPos, |
| 222 | + queueSize = totalQueueSize, |
| 223 | + subQueue = subQueue.name, |
151 | 224 | }))
|
152 | 225 |
|
153 | 226 | Wait(1000)
|
|
0 commit comments