-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerModule.luau
More file actions
619 lines (522 loc) · 17 KB
/
Copy pathServerModule.luau
File metadata and controls
619 lines (522 loc) · 17 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
local Packager = require(game.ReplicatedStorage.Packager)
--[[
ServerModule / Game Loop
Manages the game state and loop
Win condition: Last team with their Capitol (Spawn building) wins
Triggered by Team.SetNodeOwner when a team loses their Capitol
]]
local ServerModule = {
Name = "ServerModule"
}
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- Game state
local GameState = "Waiting" -- "Waiting", "Voting_GameMode", "Voting_Map", "Playing", "Ended"
local NetworkConnection = nil
local CombatConnection = nil
local DisconnectConnection = nil
local WinnerTeam = nil
-- Track player-to-team mapping for disconnect handling
local PlayerTeamMap = {} -- [player] = teamName
-- Voting configuration
local VOTE_DURATION = 4 -- Seconds for each vote
local GameModeOptions = {
{id = 1, name = "Classic", description = "Standard gameplay"},
{id = 2, name = "Fast", description = "Double production speed"},
{id = 3, name = "Large", description = "Bigger map"},
{id = 4, name = "Chaos", description = "More teams, faster combat"},
}
local MapOptions = {} -- Generated during voting
local CurrentVotes = {} -- [player] = optionId
local SelectedGameMode = nil
local SelectedMapSeed = nil
-- Combat configuration
local COMBAT_DAMAGE_RATE = 0.1 -- Troops lost per second per enemy troop present
-- Module references (set in Start)
local MapGeneration = nil
local NetworkBatch = nil
local Team = nil
local Player = nil
local Troops = nil
local Buildings = nil
local PlayerData = nil
-- Remote references (set in Start)
local VoteRemote = nil
local VotingOptionsRemote = nil
local GameStartedRemote = nil
local VoteCountsRemote = nil
local MapOwnershipRemote = nil
local VoteResultRemote = nil
local ClientReadyResponseRemote = nil
local PlayerSettingsRemote = nil
-- Public function for modules to mark changes (kept for compatibility)
function ServerModule.MarkNodeChanged(_nodeId)
-- No-op: we send full state every frame now
end
function ServerModule.MarkTroopMoving(_troopId, _troopData)
-- No-op: we send full state every frame now
end
--[[
Count votes and return winning option id
If tied, picks randomly among tied options
]]
local function TallyVotes()
local voteCounts = {}
for _, optionId in pairs(CurrentVotes) do
voteCounts[optionId] = (voteCounts[optionId] or 0) + 1
end
-- Find highest vote count
local highestCount = 0
for _, count in pairs(voteCounts) do
if count > highestCount then
highestCount = count
end
end
-- Collect all options with the highest count (handles ties)
local tiedOptions = {}
if highestCount > 0 then
for optionId, count in pairs(voteCounts) do
if count == highestCount then
table.insert(tiedOptions, optionId)
end
end
end
-- If no votes or tie, pick randomly
if #tiedOptions == 0 then
return math.random(1, 4)
elseif #tiedOptions == 1 then
return tiedOptions[1]
else
return tiedOptions[math.random(1, #tiedOptions)]
end
end
--[[
Send voting options to a player (includes duration for countdown timer)
]]
local function SendVotingOptions(player, voteType, options)
if VotingOptionsRemote then
VotingOptionsRemote:FireClient(player, voteType, options, VOTE_DURATION)
end
end
--[[
Broadcast current vote counts to all clients
]]
local function BroadcastVoteCounts()
local voteCounts = {0, 0, 0, 0} -- [optionId] = count
for _, optionId in pairs(CurrentVotes) do
voteCounts[optionId] = voteCounts[optionId] + 1
end
if VoteCountsRemote then
VoteCountsRemote:FireAllClients(voteCounts)
end
end
--[[
Run a vote phase
@param voteType - "GameMode" or "Map"
@param options - Array of {id, name, description} or {id, name, seed} for maps
@return winning option id
]]
local function RunVotePhase(voteType, options)
-- Clear previous votes
CurrentVotes = {}
-- Send options to all players
for _, player in ipairs(Players:GetPlayers()) do
SendVotingOptions(player, voteType, options)
end
-- Wait full duration
task.wait(VOTE_DURATION)
-- Tally and return winner
local winnerId = TallyVotes()
-- Send result to all clients
local winningOption = options[winnerId]
if VoteResultRemote and winningOption then
VoteResultRemote:FireAllClients(voteType, winningOption)
end
-- Brief pause so clients can see the result
task.wait(3)
return winnerId
end
--[[
Generate map options for voting
]]
local function GenerateMapOptions()
local maps = {}
for i = 1, 4 do
local seed = math.random(1, 999999)
table.insert(maps, {
id = i,
name = "Map " .. i,
seed = seed,
})
end
return maps
end
function ServerModule.Init()
end
--[[
Send map data to a player
]]
local function SendMapToPlayer(player)
local playerTeam = Player.GetTeam(player)
local visibleMapData = MapGeneration.SerializeForPlayer(player, playerTeam)
NetworkBatch.SendImmediate(NetworkBatch.EventType.MapUpdate, visibleMapData, player)
end
--[[
Combat update - resolves battles on contested nodes
Called every frame via Heartbeat
]]
local function UpdateCombat(deltaTime)
local allNodes = MapGeneration.GetAllNodes()
for _, node in ipairs(allNodes) do
local nodeId = node.id
local allTroops = Team.GetAllNodeTroops(nodeId)
-- Count teams with troops
local teamsPresent = {}
for teamName, count in pairs(allTroops) do
if count > 0 then
table.insert(teamsPresent, {name = teamName, count = count})
end
end
-- If 2+ teams, they fight
if #teamsPresent >= 2 then
-- Calculate total enemy troops for each team
local totalTroops = 0
for _, teamData in ipairs(teamsPresent) do
totalTroops = totalTroops + teamData.count
end
-- Each team takes damage based on enemy troops present
for _, teamData in ipairs(teamsPresent) do
local enemyTroops = totalTroops - teamData.count
local damage = enemyTroops * COMBAT_DAMAGE_RATE * deltaTime
-- Apply damage
local newCount = math.max(0, teamData.count - damage)
Team.SetNodeTroops(nodeId, newCount, teamData.name)
end
end
end
end
--[[
Called when a team is eliminated (loses their Capitol)
Only checks win condition - cleanup is done by ForceEliminateTeam
]]
local function OnTeamEliminated(_eliminatedTeam)
if GameState ~= "Playing" then return end
-- Check if only one team remains
local activeTeams = Team.GetActiveTeams()
if #activeTeams == 1 then
WinnerTeam = activeTeams[1]
GameState = "Ended"
print(`{WinnerTeam} wins!`)
elseif #activeTeams == 0 then
WinnerTeam = "Draw"
GameState = "Ended"
print("Draw - no teams remaining!")
end
end
--[[
Count how many players are currently on a given team
]]
local function CountPlayersOnTeam(teamName)
local count = 0
for _, name in pairs(PlayerTeamMap) do
if name == teamName then
count = count + 1
end
end
return count
end
--[[
Force eliminate a team: clear troops, reset nodes to neutral, trigger elimination
]]
local function ForceEliminateTeam(teamName)
if GameState ~= "Playing" then return end
if Team.IsTeamEliminated(teamName) then return end
print(`Eliminating team {teamName}`)
-- Clear moving troops
Troops.ClearTeamTroops(teamName)
-- Clear troops from all nodes
local allNodes = MapGeneration.GetAllNodes()
for _, node in ipairs(allNodes) do
Team.SetNodeTroops(node.id, 0, teamName)
end
-- Reset all owned nodes to neutral (including spawn, which triggers elimination)
Team.ClearTeamOwnership(teamName)
end
--[[
Remove a player from their team. If they were the last player, eliminate the team.
Used by forfeit and disconnect.
]]
local function RemovePlayerFromTeam(player)
local teamName = PlayerTeamMap[player]
if not teamName then return end
PlayerTeamMap[player] = nil
Player.ClearTeam(player)
-- If no players left on that team, eliminate it
if CountPlayersOnTeam(teamName) == 0 then
ForceEliminateTeam(teamName)
end
end
function ServerModule.Start()
-- Get required modules from Packager
MapGeneration = Packager.Get("MapGeneration")
NetworkBatch = Packager.Get("NetworkBatch")
Team = Packager.Get("Team")
Player = Packager.Get("Player")
Troops = Packager.Get("Troops")
Buildings = Packager.Get("Buildings")
PlayerData = Packager.Get("PlayerData")
-- Register for team elimination events
Team.SetOnTeamEliminated(OnTeamEliminated)
-- Setup remote event handlers (once, outside game loop)
local Remotes = game.ReplicatedStorage:WaitForChild("Remotes")
local clientReadyRemote = Remotes:WaitForChild("ClientReady")
local sendTroopsRemote = Remotes:WaitForChild("SendTroops")
local placeBuildingRemote = Remotes:WaitForChild("PlaceBuilding")
local retreatTroopsRemote = Remotes:WaitForChild("RetreatTroops")
-- Create voting remotes if they don't exist
VoteRemote = Remotes:FindFirstChild("Vote")
if not VoteRemote then
VoteRemote = Instance.new("RemoteEvent")
VoteRemote.Name = "Vote"
VoteRemote.Parent = Remotes
end
VotingOptionsRemote = Remotes:FindFirstChild("VotingOptions")
if not VotingOptionsRemote then
VotingOptionsRemote = Instance.new("RemoteEvent")
VotingOptionsRemote.Name = "VotingOptions"
VotingOptionsRemote.Parent = Remotes
end
GameStartedRemote = Remotes:FindFirstChild("GameStarted")
if not GameStartedRemote then
GameStartedRemote = Instance.new("RemoteEvent")
GameStartedRemote.Name = "GameStarted"
GameStartedRemote.Parent = Remotes
end
VoteCountsRemote = Remotes:FindFirstChild("VoteCounts")
if not VoteCountsRemote then
VoteCountsRemote = Instance.new("RemoteEvent")
VoteCountsRemote.Name = "VoteCounts"
VoteCountsRemote.Parent = Remotes
end
MapOwnershipRemote = Remotes:FindFirstChild("MapOwnership")
if not MapOwnershipRemote then
MapOwnershipRemote = Instance.new("RemoteEvent")
MapOwnershipRemote.Name = "MapOwnership"
MapOwnershipRemote.Parent = Remotes
end
VoteResultRemote = Remotes:FindFirstChild("VoteResult")
if not VoteResultRemote then
VoteResultRemote = Instance.new("RemoteEvent")
VoteResultRemote.Name = "VoteResult"
VoteResultRemote.Parent = Remotes
end
ClientReadyResponseRemote = Remotes:FindFirstChild("ClientReadyResponse")
if not ClientReadyResponseRemote then
ClientReadyResponseRemote = Instance.new("RemoteEvent")
ClientReadyResponseRemote.Name = "ClientReadyResponse"
ClientReadyResponseRemote.Parent = Remotes
end
PlayerSettingsRemote = Remotes:FindFirstChild("PlayerSettings")
if not PlayerSettingsRemote then
PlayerSettingsRemote = Instance.new("RemoteEvent")
PlayerSettingsRemote.Name = "PlayerSettings"
PlayerSettingsRemote.Parent = Remotes
end
local ForfeitRemote = Remotes:FindFirstChild("Forfeit")
if not ForfeitRemote then
ForfeitRemote = Instance.new("RemoteEvent")
ForfeitRemote.Name = "Forfeit"
ForfeitRemote.Parent = Remotes
end
-- Register for ownership change events
Team.SetOnOwnershipChanged(function(ownershipData)
MapOwnershipRemote:FireAllClients(ownershipData)
end)
sendTroopsRemote.OnServerEvent:Connect(function(player, fromNodeIds, toNodeId, amount)
if GameState ~= "Playing" then return end
Troops.SendTroops(player, fromNodeIds, toNodeId, amount)
end)
placeBuildingRemote.OnServerEvent:Connect(function(player, nodeId, buildingType)
if GameState ~= "Playing" then return end
Buildings.PlaceBuilding(player, nodeId, buildingType)
end)
retreatTroopsRemote.OnServerEvent:Connect(function(player, centerX, centerZ, radius)
if GameState ~= "Playing" then return end
Troops.RetreatTroops(player, centerX, centerZ, radius)
end)
-- Handle votes from clients
VoteRemote.OnServerEvent:Connect(function(player, optionId)
if GameState ~= "Voting_GameMode" and GameState ~= "Voting_Map" then return end
if type(optionId) == "number" and optionId >= 1 and optionId <= 4 then
CurrentVotes[player] = optionId
print(`{player.Name} voted for option {optionId}`)
BroadcastVoteCounts()
end
end)
clientReadyRemote.OnServerEvent:Connect(function(player)
-- Wait for PlayerData to load if needed
local settings = PlayerData.GetSettings(player)
if not settings then
local retries = 0
while not settings and retries < 10 do
task.wait(0.5)
settings = PlayerData.GetSettings(player)
retries += 1
end
end
-- Build init payload
local initData = {
gameState = GameState,
settings = settings,
}
if GameState == "Voting_GameMode" then
initData.voteType = "GameMode"
initData.voteOptions = GameModeOptions
initData.voteDuration = VOTE_DURATION
elseif GameState == "Voting_Map" then
initData.voteType = "Map"
initData.voteOptions = MapOptions
initData.voteDuration = VOTE_DURATION
end
if GameState == "Ended" and WinnerTeam then
initData.winnerTeam = WinnerTeam
end
ClientReadyResponseRemote:FireClient(player, initData)
if GameState == "Playing" or GameState == "Ended" then
SendMapToPlayer(player)
end
end)
-- Handle settings updates from clients
PlayerSettingsRemote.OnServerEvent:Connect(function(player, settingName, value)
PlayerData.UpdateSetting(player, settingName, value)
end)
-- Handle forfeit (player gives up)
ForfeitRemote.OnServerEvent:Connect(function(player)
if GameState ~= "Playing" then return end
if not PlayerTeamMap[player] then return end
print(`{player.Name} forfeited`)
RemovePlayerFromTeam(player)
end)
-- Count players who want to play (not spectate)
local function CountActivePlayers()
local count = 0
for _, player in ipairs(Players:GetPlayers()) do
local s = PlayerData.GetSettings(player)
if s and s.playing then
count = count + 1
end
end
return count
end
-- Main game loop
task.spawn(function()
-- Wait a moment for everything to initialize
task.wait(1)
print("Server ready - waiting for players...")
while true do
-- 1. Wait for at least 1 active player
GameState = "Waiting"
WinnerTeam = nil
PlayerTeamMap = {}
while CountActivePlayers() < 1 do
task.wait(1)
end
print("Players joined - starting voting...")
-- 2. Vote for game mode
GameState = "Voting_GameMode"
local winningGameModeId = RunVotePhase("GameMode", GameModeOptions)
SelectedGameMode = GameModeOptions[winningGameModeId]
print(`Game mode selected: {SelectedGameMode.name}`)
-- 3. Generate and vote for map
GameState = "Voting_Map"
MapOptions = GenerateMapOptions()
local winningMapId = RunVotePhase("Map", MapOptions)
SelectedMapSeed = MapOptions[winningMapId].seed
print(`Map selected: {MapOptions[winningMapId].name} (seed: {SelectedMapSeed})`)
-- 4. Assign teams to players BEFORE map generation
local teamNames = {"Red", "Blue", "Green", "Yellow"}
local playerList = Players:GetPlayers()
local activeTeams = {}
-- Only assign teams to players who want to play
local playingPlayers = {}
for _, player in ipairs(playerList) do
local s = PlayerData.GetSettings(player)
if s and s.playing then
table.insert(playingPlayers, player)
end
end
for i, player in ipairs(playingPlayers) do
local teamIndex = ((i - 1) % #teamNames) + 1
local teamName = teamNames[teamIndex]
Player.SetTeam(player, teamName)
PlayerTeamMap[player] = teamName
table.insert(activeTeams, teamName)
print(`Assigned {player.Name} to {teamName} team`)
end
-- 5. Setup new game with only active teams
GameState = "Playing"
Team.ResetAllTeams()
Buildings.ClearAllBuildings()
Troops.ClearAllTroops()
MapGeneration.Generate(1, activeTeams)
Buildings.PlaceSpawnBuildings()
-- 6. Notify clients that game has started (so they can hide voting UI)
GameStartedRemote:FireAllClients(SelectedGameMode, SelectedMapSeed)
-- Send initial map ownership percentages
MapOwnershipRemote:FireAllClients(Team.GetMapOwnership())
-- 7. Send initial map to all players
for _, player in ipairs(playerList) do
SendMapToPlayer(player)
end
print("Game started!")
-- 8. Handle player disconnects - eliminate their team
DisconnectConnection = Players.PlayerRemoving:Connect(function(player)
if GameState ~= "Playing" then return end
if not PlayerTeamMap[player] then return end
print(`{player.Name} disconnected`)
RemovePlayerFromTeam(player)
end)
-- 9. Start combat loop (every frame)
CombatConnection = RunService.Heartbeat:Connect(UpdateCombat)
-- 10. Start network sync (every frame)
NetworkConnection = RunService.Heartbeat:Connect(function()
for _, player in ipairs(Players:GetPlayers()) do
SendMapToPlayer(player)
end
end)
-- 11. Wait for game to end (win condition checked via OnTeamEliminated callback)
while GameState == "Playing" do
task.wait(1)
-- End if no players
if #Players:GetPlayers() == 0 then
GameState = "Ended"
print("All players left - ending game")
end
end
-- 12. Cleanup
if CombatConnection then
CombatConnection:Disconnect()
CombatConnection = nil
end
if NetworkConnection then
NetworkConnection:Disconnect()
NetworkConnection = nil
end
if DisconnectConnection then
DisconnectConnection:Disconnect()
DisconnectConnection = nil
end
PlayerTeamMap = {}
-- 13. Show winner and restart
if WinnerTeam then
print(`Game Over - {WinnerTeam} is victorious!`)
task.wait(5) -- Show winner for 5 seconds
else
print("Game ended")
task.wait(3)
end
print("Restarting...")
end
end)
end
return ServerModule