Add round stages, respawn protection and match highlights

This commit is contained in:
Leshyi committed 2026-11-29 23:29:29 +05:00
1 parent b87a3770a1
commit d1c42a36b8
28 files changed
+753 -45

No files matched your search

+158 -15
View File
@@ -7,13 +7,21 @@ local Rules = require(ReplicatedStorage:WaitForChild("MatchRules") :: ModuleScri
local Hosting = require(ReplicatedStorage:WaitForChild("HostedArena") :: ModuleScript)
local Objectives = require(script.Parent:WaitForChild("ArenaObjectives") :: ModuleScript)
local SiegeConfig = require(ReplicatedStorage:WaitForChild("CitadelAssaultConfig") :: ModuleScript)
local Hunter = ReplicatedStorage:WaitForChild("Hunter")
local Profiles = require(Hunter:WaitForChild("ModeProfiles") :: ModuleScript)
local Timeline = require(Hunter:WaitForChild("Timeline") :: ModuleScript)
local Stage = require(Hunter:WaitForChild("Stage") :: ModuleScript)
local RoundConfig = require(Hunter:WaitForChild("RoundConfig") :: ModuleScript)
local Strings = require(Hunter:WaitForChild("Strings") :: ModuleScript)
local Runtime = script.Parent:WaitForChild("ModeRuntime")
local RoundPlayer = require(Runtime:WaitForChild("PlayerRoundState") :: ModuleScript)
local Stats = require(Runtime:WaitForChild("Stats") :: ModuleScript)
local ArenaSpawns = require(script.Parent:WaitForChild("ArenaSpawns"))
local SkinCatalog = require(ReplicatedStorage:WaitForChild("RifleSkinCatalog") :: ModuleScript)
local lobbySpawn = workspace:WaitForChild("HunterLobby"):WaitForChild("Arrival Spawn") :: SpawnLocation
local rifleTemplate = ReplicatedStorage:WaitForChild("ArenaEquipment"):WaitForChild("SniperRifle") :: Tool
local ROUND_SECONDS = 300
local BREAK_SECONDS = 15
local VOTE_SECONDS = 15
local TEAM_REVEAL_SECONDS = 1.05
@@ -55,7 +63,7 @@ randomState.Parent = state
type Side = Hosting.Side
type Match = {id: string, state: Folder, blue: Team, orange: Team, phase: string, endsAt: number, order: number,
rule: string, allowModeChange: boolean, engine: Objectives.Match?, eligible: {[Player]: Side}, ballots: {[Player]: string}}
rule: string, startedAt: number?, liveAt: number?, allowModeChange: boolean, engine: Objectives.Match?, eligible: {[Player]: Side}, ballots: {[Player]: string}}
type PendingJoin = {match: Match, side: Side, endsAt: number}
local pendingJoins: {[Player]: PendingJoin} = {}
local matches: {[string]: Match} = {
@@ -161,6 +169,7 @@ local function guardInventory(player: Player, container: Instance)
local function disarmed(): boolean
return player:GetAttribute("InArena") ~= true or player:GetAttribute("ArenaWaiting") == true
or player:GetAttribute("ArenaIntermission") == true
or not Stage.combatAllowed(randomState)
end
container.ChildAdded:Connect(function(child)
if child:IsA("Tool") and disarmed() then child:Destroy() end
@@ -176,10 +185,14 @@ end
local function recordDeath(player: Player, character: Model)
if not roster[player] or placedCharacter[player] ~= character or lastCountedDeath[player] == character then return end
lastCountedDeath[player] = character
Stats.add(player, "deaths", 1)
player:SetAttribute("RoundDeaths", ((player:GetAttribute("RoundDeaths") :: number?) or 0) + 1)
end
local function moveToLobby(player: Player)
RoundPlayer.thaw(player)
RoundPlayer.clearShield(player)
player:SetAttribute("RoundRespawnAt", nil)
placedCharacter[player] = nil
player:SetAttribute("InArena", false)
player.RespawnLocation = lobbySpawn
@@ -198,6 +211,7 @@ local function sendToLobby(player: Player)
roster[player] = nil
joinOrder[player] = nil
lastCountedDeath[player] = nil
Stats.remove(player)
moveToLobby(player)
player:SetAttribute("ArenaWaiting", nil)
player:SetAttribute("ArenaIntermission", nil)
@@ -221,7 +235,15 @@ local function enterArena(player: Player, character: Model, humanoid: Humanoid)
local destination = destinationFor(player)
player.RespawnLocation = destination
ArenaSpawns.place(character, destination)
arm(player, character, humanoid)
if Stage.combatAllowed(randomState) then
RoundPlayer.thaw(player)
arm(player, character, humanoid)
else
clearTools(character)
clearTools(player:FindFirstChildOfClass("Backpack"))
RoundPlayer.clearShield(player)
RoundPlayer.freeze(player, character, humanoid)
end
end
local function deploy(player: Player)
@@ -239,6 +261,7 @@ local function deploy(player: Player)
end
local function stageOnArena(player: Player)
RoundPlayer.clearShield(player)
placedCharacter[player] = nil
player:SetAttribute("InArena", true)
local destination = destinationFor(player)
@@ -258,6 +281,7 @@ local function bench(player: Player)
if character and humanoid and humanoid.Health > 0 then
humanoid.Health = humanoid.MaxHealth
ArenaSpawns.place(character, destination)
if not Stage.combatAllowed(randomState) then RoundPlayer.freeze(player, character, humanoid) end
end
end
@@ -265,6 +289,17 @@ local function enterRound(player: Player, match: Match)
if match.rule == "OneLife" then bench(player) else deploy(player) end
end
local function spawnCharacter(player: Player, previous: Model?)
-- Загружаем персонажа только пока действителен конкретный запрос на возрождение.
for attempt = 1, 2 do
if player.Parent ~= Players or player.Character ~= previous then return end
local ok, issue = pcall(player.LoadCharacterAsync, player)
if ok then return end
warn("Не удалось возродить игрока:", issue)
if attempt == 1 then task.wait(1) end
end
end
local function onCharacter(player: Player, character: Model)
local root = character:WaitForChild("HumanoidRootPart", 10)
local humanoid = character:WaitForChild("Humanoid", 10)
@@ -278,26 +313,42 @@ local function onCharacter(player: Player, character: Model)
humanoid.HealthChanged:Connect(replicateHealth)
replicateHealth()
humanoid.Died:Once(function()
if player.Character ~= character then return end
RoundPlayer.thaw(player)
RoundPlayer.clearShield(player)
local match = matches[Modes.of(player)]
local engine = match.engine
local wasFighting = roster[player] ~= nil and placedCharacter[player] == character
recordDeath(player, character)
if player.Character == character then resetStreak(player) end
if wasFighting and engine and match.phase == "Round" then
local fightingStage = match.phase == "Round" and Stage.combatAllowed(match.state)
local wasFighting = roster[player] ~= nil and placedCharacter[player] == character and fightingStage
local roundRespawn = roster[player] ~= nil and match.phase == "Round" and match.rule ~= "OneLife"
local now = workspace:GetServerTimeNow()
local delay = if roundRespawn then Profiles[match.rule].respawnDelay else Players.RespawnTime
if roundRespawn then player:SetAttribute("RoundRespawnAt", now + delay) end
task.delay(delay, spawnCharacter, player, character)
if fightingStage then recordDeath(player, character) end
resetStreak(player)
if wasFighting and engine then
local position = if root:IsA("BasePart") then root.Position else nil
local now = workspace:GetServerTimeNow()
task.defer(function()
if match.engine == engine and roster[player] and placedCharacter[player] == character then engine.death(player, position, now) end
end)
end
end)
local respawning = player:GetAttribute("RoundRespawnAt") ~= nil
player:SetAttribute("RoundRespawnAt", nil)
if roster[player] and player:GetAttribute("InArena") == true then
if player:GetAttribute("ArenaIntermission") == true or player:GetAttribute("ArenaWaiting") == true then
local destination = player.RespawnLocation or destinationFor(player)
player.RespawnLocation = destination
ArenaSpawns.place(character, destination)
if player:GetAttribute("ArenaIntermission") ~= true and not Stage.combatAllowed(randomState) then
RoundPlayer.freeze(player, character, humanoid)
end
else
enterArena(player, character, humanoid)
if respawning and Stage.combatAllowed(randomState) then
RoundPlayer.shield(player, character, Profiles[matches[Modes.of(player)].rule].spawnProtection)
end
end
else
ArenaSpawns.place(character, lobbySpawn)
@@ -319,9 +370,12 @@ local function onPlayer(player: Player)
if player.Character then
guardInventory(player, player.Character)
task.spawn(onCharacter, player, player.Character)
else
task.defer(spawnCharacter, player, nil)
end
end
Players.CharacterAutoLoads = false
Players.PlayerAdded:Connect(onPlayer)
for _, player in Players:GetPlayers() do onPlayer(player) end
@@ -349,6 +403,9 @@ local function broadcastVotes(match: Match)
end
Players.PlayerRemoving:Connect(function(player)
RoundPlayer.thaw(player)
RoundPlayer.clearShield(player)
Stats.remove(player)
local wasPlaying = roster[player] ~= nil
local match = matches[Modes.of(player)]
local engine = match.engine
@@ -529,8 +586,13 @@ cycleSkin.OnServerEvent:Connect(function(player: Player)
end)
local function publish(match: Match)
local duration = if match.phase == "Round" then ROUND_SECONDS elseif match.phase == "Vote" then VOTE_SECONDS
local duration = if match.phase == "Round" then match.endsAt - (match.startedAt or match.endsAt) elseif match.phase == "Vote" then VOTE_SECONDS
elseif match.phase == "Reveal" then Rules.VoteRevealSeconds else BREAK_SECONDS
if match.phase ~= "Round" then
match.state:SetAttribute("RoundStage", nil)
match.state:SetAttribute("StageEndsAt", nil)
match.state:SetAttribute("LiveAt", nil)
end
match.state:SetAttribute("PhaseDuration", duration)
match.state:SetAttribute("EndsAt", match.endsAt)
match.state:SetAttribute("Phase", match.phase)
@@ -592,8 +654,12 @@ local function startRound(match: Match)
match.state:SetAttribute("MapGeneration", arena:GetAttribute("Generation"))
match.state:SetAttribute("MapLayout", arena:GetAttribute("LayoutVariant"))
local now = workspace:GetServerTimeNow()
local profile = Profiles[match.rule]
local roundDuration = profile.roundSeconds + (if match.rule == "Siege" then SiegeConfig.SWAP_FREEZE else 0)
local liveAt = Timeline.liveAt(now, profile.briefingSeconds, profile.countdownSeconds)
Stats.reset()
local ok, engine = pcall(Objectives.start, {
arena = match.id, rule = match.rule, state = match.state, now = now, duration = ROUND_SECONDS,
arena = match.id, rule = match.rule, state = match.state, now = liveAt, duration = roundDuration,
bench = bench, deploy = deploy,
balanceWaiting = function() balanceWaiting(match) end,
members = function(): {Player}
@@ -609,8 +675,11 @@ local function startRound(match: Match)
return
end
match.engine = engine
local roundDuration = if match.rule == "Siege" then SiegeConfig.MATCH_DURATION + SiegeConfig.SWAP_FREEZE else ROUND_SECONDS
match.phase, match.endsAt = "Round", now + roundDuration
match.startedAt, match.liveAt = now, liveAt
match.phase, match.endsAt = "Round", liveAt + roundDuration
match.state:SetAttribute("LiveAt", liveAt)
match.state:SetAttribute("StageEndsAt", now + profile.briefingSeconds)
match.state:SetAttribute("RoundStage", "Briefing")
match.state:SetAttribute("Ruleset", match.rule)
match.state:SetAttribute("BlueKills", 0)
match.state:SetAttribute("OrangeKills", 0)
@@ -629,7 +698,7 @@ local function finishRound(match: Match)
if team ~= match.blue and team ~= match.orange then continue end
local character = player.Character
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
if character and humanoid and humanoid.Health <= 0 then recordDeath(player, character) end
if character and humanoid and humanoid.Health <= 0 and Stage.combatAllowed(match.state) then recordDeath(player, character) end
table.insert(participants, {player = player, side = if team == match.blue then "Blue" else "Orange"})
table.insert(if team == match.blue then bluePlayers else orangePlayers, {
userId = player.UserId,
@@ -655,11 +724,41 @@ local function finishRound(match: Match)
local winner = if engine then engine.winner() else "Draw"
local siegeState = match.state:FindFirstChild("CitadelMatchState")
local siegeTimes = if siegeState then siegeState:GetAttributes() else nil
local rows: {Stats.Row} = {}
local contributions: {[string]: {[string]: number}} = {}
local names: {[number]: string} = {}
for _, entry in participants do
local player = entry.player
if winner == entry.side then Stats.add(player, "wins", 1) end
local record = Stats.get(player)
contributions[tostring(player.UserId)] = record
names[player.UserId] = player.DisplayName
table.insert(rows, {userId = player.UserId, stats = record})
end
local mvp = Stats.mvp(rows, RoundConfig.mvpWeights)
local lines = {}
for _, key in Strings.round.highlightOrder do
local best: Stats.Row? = nil
for _, row in rows do
local value = row.stats[key] or 0
local bestValue = if best then best.stats[key] or 0 else 0
if value > 0 and (not best or value > bestValue or (value == bestValue and row.userId < best.userId)) then
best = row
end
end
if best then
table.insert(lines, {title = Strings.round.stats[key], name = names[best.userId], value = best.stats[key]})
if #lines == 3 then break end
end
end
local snapshot = {
mode = match.id,
ruleset = match.rule,
allowModeChange = match.allowModeChange,
winner = winner,
reason = match.state:GetAttribute("ResultReason") or (if winner == "Draw" then Strings.round.drawReason else Strings.round.scoreReason),
highlights = {mvpUserId = mvp, mvpName = if mvp then names[mvp] else nil, lines = lines},
contributions = contributions,
siegeReason = if siegeState then match.state:GetAttribute("CitadelResultReason") else nil,
siegeTimes = siegeTimes,
endsAt = match.endsAt,
@@ -673,6 +772,9 @@ local function finishRound(match: Match)
for _, entry in participants do
local player = entry.player
match.eligible[player] = entry.side
RoundPlayer.thaw(player)
RoundPlayer.clearShield(player)
player:SetAttribute("RoundRespawnAt", nil)
player:SetAttribute("ArenaIntermission", true)
local wasInArena = player:GetAttribute("InArena") == true
player:SetAttribute("ArenaWaiting", nil)
@@ -720,6 +822,32 @@ local function finishVote(match: Match, now: number)
startRound(match)
end
local function advanceStage(match: Match, now: number): number
local profile = Profiles[match.rule]
local stage, boundary = Timeline.stage(match.startedAt or now, match.endsAt, profile.briefingSeconds,
profile.countdownSeconds, profile.finalPushSeconds, now)
local previous = match.state:GetAttribute("RoundStage")
if previous == "RoleSwap" then
return (match.state:GetAttribute("StageEndsAt") :: number?) or boundary
end
if previous ~= stage then
match.state:SetAttribute("StageEndsAt", boundary)
match.state:SetAttribute("RoundStage", stage)
if stage == "Live" or stage == "FinalPush" then
for player, team in roster do
if team ~= match.blue and team ~= match.orange then continue end
RoundPlayer.thaw(player)
local character = player.Character
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
if character and humanoid and humanoid.Health > 0 and player:GetAttribute("ArenaWaiting") ~= true then
arm(player, character, humanoid)
end
end
end
end
return boundary
end
local function runMatch(match: Match)
while true do
local now = workspace:GetServerTimeNow()
@@ -737,8 +865,23 @@ local function runMatch(match: Match)
match.rule = choices[modeRandom:NextInteger(1, #choices)]
end
startRound(match)
elseif remaining <= 0 or (engine and engine.step(now)) then finishRound(match)
else task.wait(math.min(remaining, if match.rule == "Siege" then SiegeConfig.CHECK_INTERVAL else .2)) end
elseif remaining <= 0 then
finishRound(match)
else
local boundary = advanceStage(match, now)
local wasSwap = match.state:GetAttribute("RoundStage") == "RoleSwap"
if now >= (match.liveAt or now) and engine and engine.step(now) then
finishRound(match)
else
if wasSwap and match.state:GetAttribute("RoundStage") == "Live" then
-- Режим завершил паузу и разрешил боевой этап.
match.state:SetAttribute("RoundStage", nil)
boundary = advanceStage(match, now)
end
local interval = if match.rule == "Siege" then SiegeConfig.CHECK_INTERVAL else .2
task.wait(math.min(remaining, interval, math.max(.01, boundary - now)))
end
end
elseif remaining > 0 then
task.wait(remaining)
elseif match.phase == "Break" and match.allowModeChange then