-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharena.lua
More file actions
463 lines (314 loc) · 9.62 KB
/
arena.lua
File metadata and controls
463 lines (314 loc) · 9.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
--!strict
--[[
Arena Server System
Author: dtblinux
Purpose
-------
Server authoritative team arena controller responsible for:
• Player queueing
• Pad interaction
• Team assignment
• Teleportation
• Weapon distribution
• Score tracking
• Match lifecycle
• Cleanup
Design Goals
------------
1. Server authoritative gameplay
2. Predictable lifecycle state machine
3. Clear separation of runtime state
4. Defensive programming against player disconnects
5. Maintainable and reviewable architecture
State Machine
-------------
WAITING → COUNTDOWN → IN_PROGRESS → RESET
]]
--------------------------------------------------------------------
-- SERVICES
--------------------------------------------------------------------
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
--------------------------------------------------------------------
-- MODULES
--------------------------------------------------------------------
local Modules = ServerStorage:WaitForChild("Modules")
local Janitor = _G.Require("Janitor")
local Zone = require(Modules.Zone)
--------------------------------------------------------------------
-- CONSTANTS
--------------------------------------------------------------------
local ARENA_TAG = "BaseArena"
local THUMBNAIL_TYPE = Enum.ThumbnailType.HeadShot
local THUMBNAIL_SIZE = Enum.ThumbnailSize.Size420x420
local DEFAULT_ICON = "rbxassetid://9319891706"
local EMPTY_NAME = "..."
local PAD_EMPTY_COLOR = Color3.new(1,1,1)
local PAD_OCCUPIED_COLOR = Color3.new(1,0,0)
local STATUS_WAITING = "Waiting..."
local STATUS_STARTING = "Starting in: "
local STATUS_ACTIVE = "In Progress..."
local CLASSIC_SWORD = "ClassicSword"
--------------------------------------------------------------------
-- TYPE DEFINITIONS
--------------------------------------------------------------------
type WaitingPlayer = {
Player: Player,
PadIndex: number
}
type ArenaSettings = {
MaxPlayers: number,
StartCountDown: number,
GameDuration: number,
WinCondition: number
}
--------------------------------------------------------------------
-- CLASS DECLARATION
--------------------------------------------------------------------
local Arena = {}
Arena.__index = Arena
Arena.CachedArenas = {}
--------------------------------------------------------------------
-- INTERNAL UTILITIES
--------------------------------------------------------------------
local function getHumanoid(player: Player): Humanoid?
local character = player.Character
if not character then
return nil
end
return character:FindFirstChildOfClass("Humanoid")
end
local function getRoot(player: Player): BasePart?
local character = player.Character
if not character then
return nil
end
return character.PrimaryPart
end
--------------------------------------------------------------------
-- UI HELPERS
--------------------------------------------------------------------
local function getPadFrame(zone)
return zone.container.Board.SurfaceGui.Frame
end
local function resetPadVisual(zone)
local frame = getPadFrame(zone)
frame.PlayerIcon.Image = DEFAULT_ICON
frame.PlayerName.Text = EMPTY_NAME
zone.container.Union.Color = PAD_EMPTY_COLOR
zone.HasPlayer = false
end
local function setPadVisual(zone, player, thumbnail)
local frame = getPadFrame(zone)
frame.PlayerIcon.Image = thumbnail
frame.PlayerName.Text = player.Name
zone.container.Union.Color = PAD_OCCUPIED_COLOR
zone.HasPlayer = true
end
--------------------------------------------------------------------
-- PLAYER LOOKUP
--------------------------------------------------------------------
function Arena:_findWaitingPlayer(player: Player)
for i,data in ipairs(self.PlayersWaiting) do
if data.Player == player then
return i,data.PadIndex
end
end
return nil,nil
end
--------------------------------------------------------------------
-- CONSTRUCTOR
--------------------------------------------------------------------
function Arena.new(folder: Folder)
local self = setmetatable({},Arena)
self.Folder = folder
self.Janitor = Janitor.new()
self.Settings = folder:GetAttributes() :: ArenaSettings
self.InProgress = false
self.Countdown = false
self.TimerRunning = true
self.Zones = {}
self.Teleports = {}
self.PlayersWaiting = {} :: {WaitingPlayer}
self.Team1 = {}
self.Team2 = {}
self.Team1Score = 0
self.Team2Score = 0
self:_validate()
self:_registerPads()
self:_connectEvents()
Arena.CachedArenas[folder] = self
return self
end
--------------------------------------------------------------------
-- VALIDATION
--------------------------------------------------------------------
function Arena:_validate()
local max = self.Settings.MaxPlayers
assert(max >= 2,"MaxPlayers must be >= 2")
assert(max % 2 == 0,"MaxPlayers must be even")
end
--------------------------------------------------------------------
-- PAD REGISTRATION
--------------------------------------------------------------------
function Arena:_registerPads()
local pads = self.Folder:WaitForChild("Pads")
local teleports = self.Folder:WaitForChild("Teleports")
for i=1,self.Settings.MaxPlayers do
local pad = pads:FindFirstChild(tostring(i))
local tp = teleports:FindFirstChild(tostring(i))
assert(pad,"Missing pad "..i)
assert(tp,"Missing teleport "..i)
local zone = Zone.new(pad)
zone.HasPlayer = false
table.insert(self.Zones,zone)
table.insert(self.Teleports,tp)
end
end
--------------------------------------------------------------------
-- ZONE EVENTS
--------------------------------------------------------------------
function Arena:_connectEvents()
for i,zone in ipairs(self.Zones) do
self.Janitor:Add(
zone.playerEntered:Connect(function(player)
self:_playerEntered(player,i,zone)
end)
)
self.Janitor:Add(
zone.playerExited:Connect(function(player)
self:_playerExited(player,i,zone)
end)
)
end
Players.PlayerRemoving:Connect(function(player)
self:_playerLeaving(player)
end)
end
--------------------------------------------------------------------
-- ZONE ENTRY
--------------------------------------------------------------------
function Arena:_playerEntered(player: Player,index: number,zone)
if self.InProgress then
return
end
if zone.HasPlayer then
return
end
if #self.PlayersWaiting >= self.Settings.MaxPlayers then
return
end
local exists = self:_findWaitingPlayer(player)
if exists then
return
end
table.insert(self.PlayersWaiting,{
Player = player,
PadIndex = index
})
local thumb = DEFAULT_ICON
local ok,result = pcall(function()
return Players:GetUserThumbnailAsync(player.UserId,THUMBNAIL_TYPE,THUMBNAIL_SIZE)
end)
if ok then
thumb = result
end
setPadVisual(zone,player,thumb)
if #self.PlayersWaiting == self.Settings.MaxPlayers then
self:_startCountdown()
end
end
--------------------------------------------------------------------
-- TEAM ASSIGNMENT
--------------------------------------------------------------------
function Arena:_assignTeams()
table.sort(self.PlayersWaiting,function(a,b)
return a.PadIndex < b.PadIndex
end)
local mid = #self.PlayersWaiting / 2
for i=1,mid do
table.insert(self.Team1,self.PlayersWaiting[i])
end
for i=mid+1,#self.PlayersWaiting do
table.insert(self.Team2,self.PlayersWaiting[i])
end
end
--------------------------------------------------------------------
-- TELEPORT
--------------------------------------------------------------------
function Arena:_teleportPlayers()
for _,data in ipairs(self.PlayersWaiting) do
local root = getRoot(data.Player)
local spawn = self.Teleports[data.PadIndex]
if root then
root.CFrame = spawn.CFrame
end
end
end
--------------------------------------------------------------------
-- COUNTDOWN
--------------------------------------------------------------------
function Arena:_startCountdown()
self.Countdown = true
for i=self.Settings.StartCountDown,1,-1 do
self:_setStatus(STATUS_STARTING..i)
task.wait(1)
if #self.PlayersWaiting < self.Settings.MaxPlayers then
self:_setStatus(STATUS_WAITING)
self.Countdown = false
return
end
end
self:_startMatch()
end
--------------------------------------------------------------------
-- MATCH START
--------------------------------------------------------------------
function Arena:_startMatch()
self.InProgress = true
self:_assignTeams()
self:_teleportPlayers()
self:_setStatus(STATUS_ACTIVE)
end
--------------------------------------------------------------------
-- STATUS UI
--------------------------------------------------------------------
function Arena:_setStatus(text)
self.Folder.MainBoard.SurfaceGui.Frame.Status.Text = text
end
--------------------------------------------------------------------
-- RESET
--------------------------------------------------------------------
function Arena:Reset()
for _,data in ipairs(self.PlayersWaiting) do
local player = data.Player
if player and player.Parent then
player:LoadCharacter()
end
end
self.PlayersWaiting = {}
self.Team1 = {}
self.Team2 = {}
self.Team1Score = 0
self.Team2Score = 0
self.InProgress = false
self.Countdown = false
self:_setStatus(STATUS_WAITING)
for _,zone in ipairs(self.Zones) do
resetPadVisual(zone)
end
end
--------------------------------------------------------------------
-- ARENA MANAGER
--------------------------------------------------------------------
local function init()
for _,arenaFolder in ipairs(CollectionService:GetTagged(ARENA_TAG)) do
if arenaFolder:IsDescendantOf(workspace) then
Arena.new(arenaFolder)
end
end
end
init()
return Arena