1
0
Fork 0
forked from Simnation/Main
This commit is contained in:
Nordi98 2025-07-26 21:42:05 +02:00
parent 7fb00900b0
commit e6e25aa23e
2 changed files with 126 additions and 134 deletions

View file

@ -37,7 +37,8 @@ RegisterNetEvent('tdm:createGame', function(gameName, fieldId, gameType, passwor
score = {team1 = 0, team2 = 0},
startTime = nil,
maxTime = Config.maxGameTime,
maxHits = Config.maxHits
maxHits = Config.maxHits,
playerStats = {} -- Spieler-Statistiken initialisieren
}
local typeText = gameType == 'public' and 'öffentliches' or 'privates'
@ -132,44 +133,50 @@ RegisterNetEvent('tdm:leaveGame', function()
TriggerClientEvent('tdm:leaveGame', src)
end)
RegisterNetEvent('tdm:playerWasHit', function(gameId, victimTeam, shooterId)
local src = source
RegisterNetEvent('tdm:playerWasHit', function(gameId, victimTeam, attackerId)
local victim = source
if not activeGames[gameId] then return end
local game = activeGames[gameId]
if not game then return end
local shooterTeam = nil
if shooterId then
for _, playerId in ipairs(game.team1) do
if playerId == shooterId then
shooterTeam = 'team1'
break
end
end
if not shooterTeam then
for _, playerId in ipairs(game.team2) do
if playerId == shooterId then
shooterTeam = 'team2'
break
end
end
end
-- Spieler Stats initialisieren falls nicht vorhanden
if not game.playerStats then
game.playerStats = {}
end
if shooterTeam and shooterTeam ~= victimTeam then
game.score[shooterTeam] = game.score[shooterTeam] + 1
if shooterId then
TriggerClientEvent('tdm:hitRegistered', shooterId)
end
TriggerClientEvent('tdm:deathRegistered', src)
updateScoreForGame(gameId)
if game.score[shooterTeam] >= game.maxHits then
endGame(gameId, shooterTeam)
end
if not game.playerStats[victim] then
game.playerStats[victim] = {hits = 0, deaths = 0}
end
if attackerId and not game.playerStats[attackerId] then
game.playerStats[attackerId] = {hits = 0, deaths = 0}
end
-- Stats updaten
game.playerStats[victim].deaths = (game.playerStats[victim].deaths or 0) + 1
if attackerId then
game.playerStats[attackerId].hits = (game.playerStats[attackerId].hits or 0) + 1
TriggerClientEvent('tdm:hitRegistered', attackerId)
end
-- Team Score erhöhen
if victimTeam == 'team1' then
game.score.team2 = game.score.team2 + 1
else
game.score.team1 = game.score.team1 + 1
end
TriggerClientEvent('tdm:deathRegistered', victim)
-- Score an alle Spieler senden
updateScoreForGame(gameId)
-- Spiel beenden prüfen
if game.score.team1 >= game.maxHits or game.score.team2 >= game.maxHits then
local winnerTeam = game.score.team1 >= game.maxHits and 'team1' or 'team2'
endGame(gameId, winnerTeam)
end
end)
@ -178,6 +185,14 @@ RegisterNetEvent('tdm:playerDied', function(gameId)
removePlayerFromGame(src, gameId)
end)
RegisterNetEvent('tdm:requestScoreUpdate', function(gameId)
local src = source
if activeGames[gameId] then
updateScoreForGame(gameId)
end
end)
-- Funktionen
function joinPlayerToGame(playerId, gameId)
local game = activeGames[gameId]
@ -188,6 +203,13 @@ function joinPlayerToGame(playerId, gameId)
table.insert(game[team], playerId)
-- Spieler-Stats initialisieren
if not game.playerStats then
game.playerStats = {}
end
game.playerStats[playerId] = {hits = 0, deaths = 0}
-- Spiel starten wenn mindestens 2 Spieler
if #game.team1 + #game.team2 >= 2 and game.status == 'waiting' then
game.status = 'active'
@ -301,16 +323,20 @@ function updateScoreForGame(gameId)
local game = activeGames[gameId]
if not game then return end
local gameStats = {
totalHits = game.score.team1 + game.score.team2,
gameTime = game.startTime and (os.time() - game.startTime) or 0
}
for _, playerId in ipairs(game.team1) do
TriggerClientEvent('tdm:updateScore', playerId, game.score.team1, game.score.team2, gameStats)
local playerStats = game.playerStats[playerId] or {hits = 0, deaths = 0}
TriggerClientEvent('tdm:updateScore', playerId, game.score.team1, game.score.team2, {
hits = playerStats.hits or 0,
deaths = playerStats.deaths or 0
})
end
for _, playerId in ipairs(game.team2) do
TriggerClientEvent('tdm:updateScore', playerId, game.score.team1, game.score.team2, gameStats)
local playerStats = game.playerStats[playerId] or {hits = 0, deaths = 0}
TriggerClientEvent('tdm:updateScore', playerId, game.score.team1, game.score.team2, {
hits = playerStats.hits or 0,
deaths = playerStats.deaths or 0
})
end
end
@ -360,88 +386,3 @@ AddEventHandler('onResourceStop', function(resourceName)
print('[TDM] TeamDeathmatch System gestoppt!')
end
end)
-- In server.lua hinzufügen
RegisterNetEvent('tdm:requestScoreUpdate', function(gameId)
local src = source
if activeGames[gameId] then
local game = activeGames[gameId]
-- Score an alle Spieler senden
for _, playerId in pairs(game.team1) do
TriggerClientEvent('tdm:updateScore', playerId, game.team1Score, game.team2Score, {
hits = game.playerStats[playerId] and game.playerStats[playerId].hits or 0,
deaths = game.playerStats[playerId] and game.playerStats[playerId].deaths or 0
})
end
for _, playerId in pairs(game.team2) do
TriggerClientEvent('tdm:updateScore', playerId, game.team1Score, game.team2Score, {
hits = game.playerStats[playerId] and game.playerStats[playerId].hits or 0,
deaths = game.playerStats[playerId] and game.playerStats[playerId].deaths or 0
})
end
end
end)
-- Erweiterte playerWasHit Event
RegisterNetEvent('tdm:playerWasHit', function(gameId, victimTeam, attackerId)
local victim = source
if not activeGames[gameId] then return end
local game = activeGames[gameId]
-- Spieler Stats initialisieren falls nicht vorhanden
if not game.playerStats then
game.playerStats = {}
end
if not game.playerStats[victim] then
game.playerStats[victim] = {hits = 0, deaths = 0}
end
if attackerId and not game.playerStats[attackerId] then
game.playerStats[attackerId] = {hits = 0, deaths = 0}
end
-- Stats updaten
game.playerStats[victim].deaths = game.playerStats[victim].deaths + 1
if attackerId then
game.playerStats[attackerId].hits = game.playerStats[attackerId].hits + 1
TriggerClientEvent('tdm:hitRegistered', attackerId)
end
-- Team Score erhöhen
if victimTeam == 'team1' then
game.team2Score = game.team2Score + 1
else
game.team1Score = game.team1Score + 1
end
TriggerClientEvent('tdm:deathRegistered', victim)
-- Score an alle Spieler senden
local allPlayers = {}
for _, playerId in pairs(game.team1) do
table.insert(allPlayers, playerId)
end
for _, playerId in pairs(game.team2) do
table.insert(allPlayers, playerId)
end
for _, playerId in pairs(allPlayers) do
TriggerClientEvent('tdm:updateScore', playerId, game.team1Score, game.team2Score, {
hits = game.playerStats[playerId] and game.playerStats[playerId].hits or 0,
deaths = game.playerStats[playerId] and game.playerStats[playerId].deaths or 0
})
end
-- Spiel beenden prüfen
if game.team1Score >= Config.maxHits or game.team2Score >= Config.maxHits then
local winnerTeam = game.team1Score >= Config.maxHits and 'team1' or 'team2'
endGame(gameId, winnerTeam)
end
end)