2025-07-26 06:21:34 +02:00
|
|
|
local QBCore = exports['qb-core']:GetCoreObject()
|
|
|
|
|
|
|
|
-- Game Management
|
|
|
|
local activeGames = {}
|
|
|
|
local gameIdCounter = 1
|
|
|
|
|
2025-07-26 22:13:11 +02:00
|
|
|
-- Debug-Funktion für Konsole
|
|
|
|
local function debugPrint(message)
|
|
|
|
print("^2[TDM SERVER]^7 " .. message)
|
|
|
|
end
|
|
|
|
|
2025-07-26 06:21:34 +02:00
|
|
|
-- Events
|
|
|
|
RegisterNetEvent('tdm:createGame', function(gameName, fieldId, gameType, password)
|
|
|
|
local src = source
|
|
|
|
local Player = QBCore.Functions.GetPlayer(src)
|
|
|
|
|
2025-07-26 22:43:27 +02:00
|
|
|
if not Player then
|
|
|
|
debugPrint("Spielerstellung fehlgeschlagen - Spieler nicht gefunden: " .. src)
|
|
|
|
return
|
|
|
|
end
|
2025-07-26 06:21:34 +02:00
|
|
|
|
|
|
|
-- Prüfen ob Spielfeld bereits belegt
|
|
|
|
for gameId, gameData in pairs(activeGames) do
|
|
|
|
if gameData.fieldId == fieldId then
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Spielerstellung abgelehnt - Feld bereits belegt: " .. fieldId)
|
2025-07-26 06:21:34 +02:00
|
|
|
TriggerClientEvent('QBCore:Notify', src, 'Dieses Spielfeld ist bereits belegt!', 'error')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local gameId = 'game_' .. gameIdCounter
|
|
|
|
gameIdCounter = gameIdCounter + 1
|
|
|
|
|
|
|
|
activeGames[gameId] = {
|
|
|
|
id = gameId,
|
|
|
|
name = gameName,
|
|
|
|
fieldId = fieldId,
|
|
|
|
admin = src,
|
|
|
|
adminName = Player.PlayerData.charinfo.firstname .. ' ' .. Player.PlayerData.charinfo.lastname,
|
2025-07-26 06:36:06 +02:00
|
|
|
gameType = gameType,
|
2025-07-26 06:21:34 +02:00
|
|
|
password = password,
|
|
|
|
hasPassword = password ~= nil,
|
2025-07-26 06:36:06 +02:00
|
|
|
status = 'waiting',
|
2025-07-26 06:21:34 +02:00
|
|
|
team1 = {},
|
|
|
|
team2 = {},
|
|
|
|
score = {team1 = 0, team2 = 0},
|
|
|
|
startTime = nil,
|
|
|
|
maxTime = Config.maxGameTime,
|
2025-07-26 21:42:05 +02:00
|
|
|
maxHits = Config.maxHits,
|
|
|
|
playerStats = {} -- Spieler-Statistiken initialisieren
|
2025-07-26 06:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
local typeText = gameType == 'public' and 'öffentliches' or 'privates'
|
|
|
|
TriggerClientEvent('QBCore:Notify', src, 'Dein ' .. typeText .. ' Spiel "' .. gameName .. '" wurde erstellt!', 'success')
|
|
|
|
|
|
|
|
updateGamesListForAll()
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Spiel erstellt: " .. gameId .. " von " .. Player.PlayerData.name .. " (Feld: " .. fieldId .. ")")
|
2025-07-26 06:21:34 +02:00
|
|
|
end)
|
|
|
|
|
2025-07-26 06:36:06 +02:00
|
|
|
RegisterNetEvent('tdm:requestGamesList', function()
|
|
|
|
local src = source
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Spiele-Liste angefordert von: " .. src)
|
2025-07-26 06:36:06 +02:00
|
|
|
TriggerClientEvent('tdm:updateGamesList', src, activeGames)
|
|
|
|
end)
|
|
|
|
|
2025-07-26 06:21:34 +02:00
|
|
|
RegisterNetEvent('tdm:requestJoinGame', function(gameId, password)
|
|
|
|
local src = source
|
|
|
|
local Player = QBCore.Functions.GetPlayer(src)
|
|
|
|
|
2025-07-26 22:43:27 +02:00
|
|
|
if not Player or not activeGames[gameId] then
|
|
|
|
debugPrint("Spielbeitritt fehlgeschlagen - Spieler oder Spiel nicht gefunden: " .. src .. ", " .. gameId)
|
|
|
|
return
|
|
|
|
end
|
2025-07-26 06:21:34 +02:00
|
|
|
|
|
|
|
local game = activeGames[gameId]
|
|
|
|
local playerName = Player.PlayerData.charinfo.firstname .. ' ' .. Player.PlayerData.charinfo.lastname
|
|
|
|
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Beitrittsanfrage von " .. playerName .. " (ID: " .. src .. ") für Spiel " .. gameId)
|
|
|
|
|
2025-07-26 06:21:34 +02:00
|
|
|
-- Passwort prüfen falls vorhanden
|
|
|
|
if game.hasPassword and game.password ~= password then
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Beitritt abgelehnt - Falsches Passwort")
|
2025-07-26 06:21:34 +02:00
|
|
|
TriggerClientEvent('QBCore:Notify', src, 'Falsches Passwort!', 'error')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Spieler bereits im Spiel?
|
|
|
|
for _, playerId in ipairs(game.team1) do
|
|
|
|
if playerId == src then
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Beitritt abgelehnt - Spieler bereits in Team 1")
|
2025-07-26 06:21:34 +02:00
|
|
|
TriggerClientEvent('QBCore:Notify', src, 'Du bist bereits in diesem Spiel!', 'error')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for _, playerId in ipairs(game.team2) do
|
|
|
|
if playerId == src then
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Beitritt abgelehnt - Spieler bereits in Team 2")
|
2025-07-26 06:21:34 +02:00
|
|
|
TriggerClientEvent('QBCore:Notify', src, 'Du bist bereits in diesem Spiel!', 'error')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Max Spieler erreicht?
|
|
|
|
local currentPlayers = #game.team1 + #game.team2
|
|
|
|
local maxPlayers = Config.gameFields[game.fieldId].maxPlayers
|
|
|
|
|
|
|
|
if currentPlayers >= maxPlayers then
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Beitritt abgelehnt - Spiel ist voll")
|
2025-07-26 06:21:34 +02:00
|
|
|
TriggerClientEvent('QBCore:Notify', src, 'Spiel ist voll!', 'error')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Prüfen ob Admin online ist (für private Spiele)
|
|
|
|
if game.gameType == 'private' then
|
|
|
|
local AdminPlayer = QBCore.Functions.GetPlayer(game.admin)
|
|
|
|
if not AdminPlayer then
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Beitritt abgelehnt - Admin nicht online")
|
2025-07-26 06:21:34 +02:00
|
|
|
TriggerClientEvent('QBCore:Notify', src, 'Der Spiel-Admin ist nicht online!', 'error')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Join Logic basierend auf Spiel Typ
|
|
|
|
if game.gameType == 'public' then
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Öffentliches Spiel - Direkter Beitritt")
|
2025-07-26 06:21:34 +02:00
|
|
|
joinPlayerToGame(src, gameId)
|
|
|
|
TriggerClientEvent('QBCore:Notify', src, 'Du bist dem öffentlichen Spiel beigetreten!', 'success')
|
|
|
|
else
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Privates Spiel - Sende Anfrage an Admin")
|
2025-07-26 06:21:34 +02:00
|
|
|
TriggerClientEvent('tdm:joinRequest', game.admin, gameId, playerName, src)
|
|
|
|
TriggerClientEvent('QBCore:Notify', src, 'Join-Anfrage gesendet an ' .. game.adminName, 'info')
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
RegisterNetEvent('tdm:approveJoinRequest', function(gameId, playerId, approved)
|
|
|
|
local src = source
|
|
|
|
local game = activeGames[gameId]
|
|
|
|
|
2025-07-26 22:43:27 +02:00
|
|
|
if not game or game.admin ~= src then
|
|
|
|
debugPrint("Join-Anfrage Bearbeitung fehlgeschlagen - Ungültiges Spiel oder nicht Admin")
|
|
|
|
return
|
|
|
|
end
|
2025-07-26 06:21:34 +02:00
|
|
|
|
|
|
|
if approved then
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Join-Anfrage genehmigt für Spieler " .. playerId .. " in Spiel " .. gameId)
|
2025-07-26 06:21:34 +02:00
|
|
|
joinPlayerToGame(playerId, gameId)
|
|
|
|
TriggerClientEvent('tdm:joinRequestResult', playerId, true, game.name)
|
|
|
|
else
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Join-Anfrage abgelehnt für Spieler " .. playerId .. " in Spiel " .. gameId)
|
2025-07-26 06:21:34 +02:00
|
|
|
TriggerClientEvent('tdm:joinRequestResult', playerId, false, game.name)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
RegisterNetEvent('tdm:leaveGame', function()
|
|
|
|
local src = source
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Spieler " .. src .. " möchte alle Spiele verlassen")
|
2025-07-26 06:21:34 +02:00
|
|
|
|
|
|
|
for gameId, game in pairs(activeGames) do
|
|
|
|
removePlayerFromGame(src, gameId)
|
|
|
|
end
|
|
|
|
|
|
|
|
TriggerClientEvent('tdm:leaveGame', src)
|
|
|
|
end)
|
|
|
|
|
2025-07-26 21:42:05 +02:00
|
|
|
RegisterNetEvent('tdm:playerWasHit', function(gameId, victimTeam, attackerId)
|
|
|
|
local victim = source
|
|
|
|
|
2025-07-26 22:13:11 +02:00
|
|
|
if not activeGames[gameId] then
|
|
|
|
debugPrint("Hit registriert, aber Spiel " .. gameId .. " existiert nicht!")
|
|
|
|
return
|
|
|
|
end
|
2025-07-26 21:42:05 +02:00
|
|
|
|
2025-07-26 06:21:34 +02:00
|
|
|
local game = activeGames[gameId]
|
|
|
|
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Hit registriert - Opfer: " .. victim .. " (Team: " .. victimTeam .. "), Angreifer: " .. (attackerId or "Unbekannt"))
|
|
|
|
|
2025-07-26 21:42:05 +02:00
|
|
|
-- Spieler Stats initialisieren falls nicht vorhanden
|
|
|
|
if not game.playerStats then
|
|
|
|
game.playerStats = {}
|
|
|
|
end
|
2025-07-26 06:21:34 +02:00
|
|
|
|
2025-07-26 21:42:05 +02:00
|
|
|
if not game.playerStats[victim] then
|
|
|
|
game.playerStats[victim] = {hits = 0, deaths = 0}
|
2025-07-26 06:36:06 +02:00
|
|
|
end
|
2025-07-26 06:21:34 +02:00
|
|
|
|
2025-07-28 00:09:15 +02:00
|
|
|
-- Wichtig: Nur wenn ein Angreifer identifiziert wurde, zähle den Kill
|
2025-07-26 21:42:05 +02:00
|
|
|
if attackerId then
|
2025-07-28 00:09:15 +02:00
|
|
|
if 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
|
2025-07-26 21:42:05 +02:00
|
|
|
game.playerStats[attackerId].hits = (game.playerStats[attackerId].hits or 0) + 1
|
2025-07-28 00:09:15 +02:00
|
|
|
|
|
|
|
-- Benachrichtigung an den Angreifer senden
|
2025-07-26 21:42:05 +02:00
|
|
|
TriggerClientEvent('tdm:hitRegistered', attackerId)
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Treffer von " .. attackerId .. " gegen " .. victim .. " registriert")
|
2025-07-28 00:09:15 +02:00
|
|
|
|
|
|
|
-- Team Score erhöhen
|
|
|
|
if victimTeam == 'team1' then
|
|
|
|
game.score.team2 = game.score.team2 + 1
|
|
|
|
debugPrint("Punkt für Team 2 - Neuer Score: " .. game.score.team2)
|
|
|
|
else
|
|
|
|
game.score.team1 = game.score.team1 + 1
|
|
|
|
debugPrint("Punkt für Team 1 - Neuer Score: " .. game.score.team1)
|
|
|
|
end
|
2025-07-26 22:13:11 +02:00
|
|
|
else
|
2025-07-28 00:09:15 +02:00
|
|
|
debugPrint("Treffer gegen " .. victim .. " von unbekanntem Angreifer registriert - Kein Punkt vergeben")
|
2025-07-26 21:42:05 +02:00
|
|
|
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'
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Max Punkte erreicht - Beende Spiel. Gewinner: " .. winnerTeam)
|
2025-07-26 21:42:05 +02:00
|
|
|
endGame(gameId, winnerTeam)
|
2025-07-26 06:21:34 +02:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
RegisterNetEvent('tdm:playerDied', function(gameId)
|
|
|
|
local src = source
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Spieler " .. src .. " ist gestorben in Spiel " .. gameId)
|
2025-07-26 06:21:34 +02:00
|
|
|
removePlayerFromGame(src, gameId)
|
|
|
|
end)
|
|
|
|
|
2025-07-26 21:42:05 +02:00
|
|
|
RegisterNetEvent('tdm:requestScoreUpdate', function(gameId)
|
|
|
|
local src = source
|
|
|
|
|
|
|
|
if activeGames[gameId] then
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Score-Update angefordert von " .. src .. " für Spiel " .. gameId)
|
2025-07-26 21:42:05 +02:00
|
|
|
updateScoreForGame(gameId)
|
2025-07-26 22:43:27 +02:00
|
|
|
else
|
|
|
|
debugPrint("Score-Update fehlgeschlagen - Spiel " .. gameId .. " nicht gefunden")
|
2025-07-26 21:42:05 +02:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2025-07-26 22:13:11 +02:00
|
|
|
RegisterNetEvent('tdm:debugPlayerStats', function(gameId)
|
|
|
|
local src = source
|
|
|
|
if activeGames[gameId] and activeGames[gameId].playerStats and activeGames[gameId].playerStats[src] then
|
|
|
|
local stats = activeGames[gameId].playerStats[src]
|
|
|
|
debugPrint("Stats für Spieler " .. src .. ": Hits=" .. (stats.hits or 0) .. ", Deaths=" .. (stats.deaths or 0))
|
2025-07-26 22:43:27 +02:00
|
|
|
TriggerClientEvent('QBCore:Notify', src, 'Server Stats - Hits: ' .. (stats.hits or 0) .. ', Deaths: ' .. (stats.deaths or 0), 'info')
|
2025-07-26 22:13:11 +02:00
|
|
|
else
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Keine Stats gefunden für Spieler " .. src .. " in Spiel " .. gameId)
|
2025-07-26 22:13:11 +02:00
|
|
|
TriggerClientEvent('QBCore:Notify', src, 'Keine Stats gefunden!', 'error')
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2025-07-26 06:21:34 +02:00
|
|
|
-- Funktionen
|
|
|
|
function joinPlayerToGame(playerId, gameId)
|
|
|
|
local game = activeGames[gameId]
|
2025-07-26 22:43:27 +02:00
|
|
|
if not game then
|
|
|
|
debugPrint("Spielbeitritt fehlgeschlagen - Spiel nicht gefunden: " .. gameId)
|
|
|
|
return
|
|
|
|
end
|
2025-07-26 06:21:34 +02:00
|
|
|
|
|
|
|
-- Team mit weniger Spielern wählen
|
|
|
|
local team = #game.team1 <= #game.team2 and 'team1' or 'team2'
|
|
|
|
|
|
|
|
table.insert(game[team], playerId)
|
|
|
|
|
2025-07-26 21:42:05 +02:00
|
|
|
-- Spieler-Stats initialisieren
|
|
|
|
if not game.playerStats then
|
|
|
|
game.playerStats = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
game.playerStats[playerId] = {hits = 0, deaths = 0}
|
|
|
|
|
2025-07-26 06:36:06 +02:00
|
|
|
-- Spiel starten wenn mindestens 2 Spieler
|
|
|
|
if #game.team1 + #game.team2 >= 2 and game.status == 'waiting' then
|
|
|
|
game.status = 'active'
|
|
|
|
game.startTime = os.time()
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Spiel " .. gameId .. " gestartet - Mindestens 2 Spieler erreicht")
|
2025-07-26 06:36:06 +02:00
|
|
|
|
|
|
|
-- Game Timer starten
|
|
|
|
startGameTimer(gameId)
|
|
|
|
end
|
2025-07-26 06:21:34 +02:00
|
|
|
|
2025-07-26 06:36:06 +02:00
|
|
|
TriggerClientEvent('tdm:joinGame', playerId, gameId, team, game.fieldId)
|
2025-07-26 06:21:34 +02:00
|
|
|
updateScoreForGame(gameId)
|
2025-07-26 06:36:06 +02:00
|
|
|
updateGamesListForAll()
|
2025-07-26 22:13:11 +02:00
|
|
|
|
|
|
|
debugPrint("Spieler " .. playerId .. " ist Spiel " .. gameId .. " beigetreten (Team: " .. team .. ")")
|
2025-07-26 06:21:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function removePlayerFromGame(playerId, gameId)
|
|
|
|
local game = activeGames[gameId]
|
|
|
|
if not game then return end
|
|
|
|
|
2025-07-26 06:36:06 +02:00
|
|
|
-- Spieler aus Teams entfernen
|
2025-07-26 22:43:27 +02:00
|
|
|
local removed = false
|
|
|
|
|
2025-07-26 06:21:34 +02:00
|
|
|
for i, id in ipairs(game.team1) do
|
|
|
|
if id == playerId then
|
|
|
|
table.remove(game.team1, i)
|
2025-07-26 22:13:11 +02:00
|
|
|
debugPrint("Spieler " .. playerId .. " aus Team 1 entfernt")
|
2025-07-26 22:43:27 +02:00
|
|
|
removed = true
|
2025-07-26 06:21:34 +02:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-07-26 22:43:27 +02:00
|
|
|
if not removed then
|
|
|
|
for i, id in ipairs(game.team2) do
|
|
|
|
if id == playerId then
|
|
|
|
table.remove(game.team2, i)
|
|
|
|
debugPrint("Spieler " .. playerId .. " aus Team 2 entfernt")
|
|
|
|
removed = true
|
|
|
|
break
|
|
|
|
end
|
2025-07-26 06:21:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-07-26 22:43:27 +02:00
|
|
|
if not removed then
|
|
|
|
debugPrint("Spieler " .. playerId .. " nicht in Spiel " .. gameId .. " gefunden")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2025-07-26 06:21:34 +02:00
|
|
|
-- Wenn Admin das Spiel verlässt, Spiel beenden
|
|
|
|
if game.admin == playerId then
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Admin hat das Spiel verlassen - Beende Spiel " .. gameId)
|
2025-07-26 06:21:34 +02:00
|
|
|
endGame(gameId, nil, 'Admin hat das Spiel verlassen')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
checkGameEnd(gameId)
|
|
|
|
updateGamesListForAll()
|
|
|
|
end
|
|
|
|
|
2025-07-26 06:36:06 +02:00
|
|
|
function endGame(gameId, winnerTeam, reason)
|
2025-07-26 06:21:34 +02:00
|
|
|
local game = activeGames[gameId]
|
2025-07-26 22:43:27 +02:00
|
|
|
if not game then
|
|
|
|
debugPrint("Spielende fehlgeschlagen - Spiel nicht gefunden: " .. gameId)
|
|
|
|
return
|
|
|
|
end
|
2025-07-26 06:21:34 +02:00
|
|
|
|
2025-07-26 06:36:06 +02:00
|
|
|
game.status = 'finished'
|
2025-07-26 06:21:34 +02:00
|
|
|
|
2025-07-26 06:36:06 +02:00
|
|
|
local allPlayers = {}
|
2025-07-26 06:21:34 +02:00
|
|
|
for _, playerId in ipairs(game.team1) do
|
2025-07-26 06:36:06 +02:00
|
|
|
table.insert(allPlayers, playerId)
|
2025-07-26 06:21:34 +02:00
|
|
|
end
|
|
|
|
for _, playerId in ipairs(game.team2) do
|
2025-07-26 06:36:06 +02:00
|
|
|
table.insert(allPlayers, playerId)
|
2025-07-26 06:21:34 +02:00
|
|
|
end
|
|
|
|
|
2025-07-26 06:36:06 +02:00
|
|
|
-- Game End Event an alle Spieler
|
|
|
|
for _, playerId in ipairs(allPlayers) do
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Sende Spielende-Event an Spieler " .. playerId)
|
2025-07-26 06:36:06 +02:00
|
|
|
TriggerClientEvent('tdm:gameEnded', playerId, winnerTeam, game.score.team1, game.score.team2)
|
|
|
|
end
|
2025-07-26 06:21:34 +02:00
|
|
|
|
2025-07-26 06:36:06 +02:00
|
|
|
-- Spiel nach 10 Sekunden löschen
|
|
|
|
SetTimeout(10000, function()
|
|
|
|
activeGames[gameId] = nil
|
|
|
|
updateGamesListForAll()
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Spiel " .. gameId .. " aus der Liste entfernt")
|
2025-07-26 06:21:34 +02:00
|
|
|
end)
|
2025-07-26 06:36:06 +02:00
|
|
|
|
|
|
|
if reason then
|
2025-07-26 22:13:11 +02:00
|
|
|
debugPrint("Spiel " .. gameId .. " beendet: " .. reason)
|
|
|
|
else
|
|
|
|
debugPrint("Spiel " .. gameId .. " beendet. Gewinner: " .. (winnerTeam or "Unentschieden"))
|
2025-07-26 06:36:06 +02:00
|
|
|
end
|
2025-07-26 06:21:34 +02:00
|
|
|
end
|
|
|
|
|
2025-07-26 06:36:06 +02:00
|
|
|
function startGameTimer(gameId)
|
|
|
|
CreateThread(function()
|
|
|
|
local game = activeGames[gameId]
|
|
|
|
if not game then return end
|
|
|
|
|
|
|
|
local maxTime = game.maxTime
|
|
|
|
local startTime = os.time()
|
|
|
|
|
2025-07-26 22:13:11 +02:00
|
|
|
debugPrint("Timer für Spiel " .. gameId .. " gestartet. Maximale Zeit: " .. maxTime .. " Sekunden")
|
|
|
|
|
2025-07-26 06:36:06 +02:00
|
|
|
while game and game.status == 'active' and (os.time() - startTime) < maxTime do
|
|
|
|
Wait(1000)
|
|
|
|
game = activeGames[gameId] -- Refresh game data
|
2025-07-26 22:13:11 +02:00
|
|
|
|
|
|
|
-- Alle 30 Sekunden Debug-Info
|
|
|
|
if (os.time() - startTime) % 30 == 0 then
|
|
|
|
debugPrint("Spiel " .. gameId .. " läuft seit " .. (os.time() - startTime) .. " Sekunden")
|
|
|
|
end
|
2025-07-26 06:21:34 +02:00
|
|
|
end
|
2025-07-26 06:36:06 +02:00
|
|
|
|
|
|
|
-- Zeit abgelaufen
|
|
|
|
if game and game.status == 'active' then
|
|
|
|
local winnerTeam = game.score.team1 > game.score.team2 and 'team1' or
|
|
|
|
game.score.team2 > game.score.team1 and 'team2' or nil
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Spielzeit abgelaufen - Beende Spiel " .. gameId)
|
2025-07-26 06:36:06 +02:00
|
|
|
endGame(gameId, winnerTeam, 'Zeit abgelaufen')
|
2025-07-26 06:21:34 +02:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function checkGameEnd(gameId)
|
|
|
|
local game = activeGames[gameId]
|
|
|
|
if not game then return end
|
|
|
|
|
|
|
|
local totalPlayers = #game.team1 + #game.team2
|
|
|
|
|
|
|
|
if totalPlayers < 2 and game.status == 'active' then
|
2025-07-26 22:43:27 +02:00
|
|
|
debugPrint("Zu wenig Spieler - Beende Spiel " .. gameId)
|
2025-07-26 06:21:34 +02:00
|
|
|
endGame(gameId, nil, 'Zu wenig Spieler')
|
|
|
|
elseif totalPlayers == 0 then
|
|
|
|
activeGames[gameId] = nil
|
|
|
|
updateGamesListForAll()
|
2025-07-26 22:13:11 +02:00
|
|
|
debugPrint("Spiel " .. gameId .. " gelöscht (keine Spieler)")
|
2025-07-26 06:21:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function updateScoreForGame(gameId)
|
|
|
|
local game = activeGames[gameId]
|
2025-07-26 22:43:27 +02:00
|
|
|
if not game then
|
|
|
|
debugPrint("Score-Update fehlgeschlagen - Spiel nicht gefunden: " .. gameId)
|
|
|
|
return
|
|
|
|
end
|
2025-07-26 06:21:34 +02:00
|
|
|
|
2025-07-26 22:13:11 +02:00
|
|
|
debugPrint("Score Update für Spiel " .. gameId .. ": Team1=" .. game.score.team1 .. ", Team2=" .. game.score.team2)
|
|
|
|
|
2025-07-26 06:21:34 +02:00
|
|
|
for _, playerId in ipairs(game.team1) do
|
2025-07-26 21:42:05 +02:00
|
|
|
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
|
|
|
|
})
|
2025-07-26 06:21:34 +02:00
|
|
|
end
|
2025-07-26 21:42:05 +02:00
|
|
|
|
2025-07-26 06:21:34 +02:00
|
|
|
for _, playerId in ipairs(game.team2) do
|
2025-07-26 21:42:05 +02:00
|
|
|
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
|
|
|
|
})
|
2025-07-26 06:21:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function updateGamesListForAll()
|
|
|
|
local players = QBCore.Functions.GetPlayers()
|
|
|
|
for _, playerId in pairs(players) do
|
|
|
|
TriggerClientEvent('tdm:updateGamesList', playerId, activeGames)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Player Disconnect Handler
|
|
|
|
AddEventHandler('playerDropped', function()
|
|
|
|
local src = source
|
|
|
|
|
|
|
|
for gameId, game in pairs(activeGames) do
|
|
|
|
removePlayerFromGame(src, gameId)
|
|
|
|
end
|
2025-07-26 22:13:11 +02:00
|
|
|
|
|
|
|
debugPrint("Spieler " .. src .. " hat den Server verlassen - aus allen Spielen entfernt")
|
2025-07-26 06:21:34 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
-- Server Start - Games Liste leeren
|
|
|
|
AddEventHandler('onResourceStart', function(resourceName)
|
|
|
|
if GetCurrentResourceName() == resourceName then
|
|
|
|
activeGames = {}
|
|
|
|
gameIdCounter = 1
|
2025-07-26 23:28:00 +02:00
|
|
|
debugPrint("TeamDeathmatch System gestartet!")
|
2025-07-26 06:21:34 +02:00
|
|
|
end
|
|
|
|
end)
|
2025-07-26 06:36:06 +02:00
|
|
|
|
|
|
|
AddEventHandler('onResourceStop', function(resourceName)
|
|
|
|
if GetCurrentResourceName() == resourceName then
|
|
|
|
-- Alle Spieler aus TDM entfernen
|
|
|
|
for gameId, game in pairs(activeGames) do
|
|
|
|
local allPlayers = {}
|
|
|
|
for _, playerId in ipairs(game.team1) do
|
|
|
|
table.insert(allPlayers, playerId)
|
|
|
|
end
|
|
|
|
for _, playerId in ipairs(game.team2) do
|
|
|
|
table.insert(allPlayers, playerId)
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, playerId in ipairs(allPlayers) do
|
|
|
|
TriggerClientEvent('tdm:leaveGame', playerId)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
activeGames = {}
|
2025-07-26 22:13:11 +02:00
|
|
|
debugPrint("TeamDeathmatch System gestoppt!")
|
2025-07-26 06:36:06 +02:00
|
|
|
end
|
|
|
|
end)
|
2025-07-26 22:43:27 +02:00
|
|
|
|
2025-07-26 23:28:00 +02:00
|
|
|
-- Admin-Befehle
|
2025-07-26 22:43:27 +02:00
|
|
|
RegisterCommand('tdmreset', function(source, args)
|
|
|
|
local src = source
|
|
|
|
if src > 0 then -- Spieler
|
|
|
|
local Player = QBCore.Functions.GetPlayer(src)
|
|
|
|
if not Player or not Player.PlayerData.job or Player.PlayerData.job.name ~= 'admin' then
|
|
|
|
TriggerClientEvent('QBCore:Notify', src, 'Du hast keine Berechtigung!', 'error')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Alle Spieler aus TDM entfernen
|
|
|
|
for gameId, game in pairs(activeGames) do
|
|
|
|
local allPlayers = {}
|
|
|
|
for _, playerId in ipairs(game.team1) do
|
|
|
|
table.insert(allPlayers, playerId)
|
|
|
|
end
|
|
|
|
for _, playerId in ipairs(game.team2) do
|
|
|
|
table.insert(allPlayers, playerId)
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, playerId in ipairs(allPlayers) do
|
|
|
|
TriggerClientEvent('tdm:leaveGame', playerId)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
activeGames = {}
|
|
|
|
gameIdCounter = 1
|
|
|
|
|
|
|
|
debugPrint("TeamDeathmatch System zurückgesetzt!")
|
|
|
|
|
|
|
|
if src > 0 then
|
|
|
|
TriggerClientEvent('QBCore:Notify', src, 'TDM System zurückgesetzt!', 'success')
|
|
|
|
end
|
|
|
|
end, true)
|
2025-07-26 23:28:00 +02:00
|
|
|
|
|
|
|
-- Debug-Befehl für Server-Status
|
|
|
|
RegisterCommand('tdmstatus', function(source, args)
|
|
|
|
local src = source
|
|
|
|
if src > 0 then -- Spieler
|
|
|
|
local Player = QBCore.Functions.GetPlayer(src)
|
|
|
|
if not Player or not Player.PlayerData.job or Player.PlayerData.job.name ~= 'admin' then
|
|
|
|
TriggerClientEvent('QBCore:Notify', src, 'Du hast keine Berechtigung!', 'error')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
debugPrint("=== TDM STATUS ===")
|
|
|
|
debugPrint("Aktive Spiele: " .. table.count(activeGames))
|
|
|
|
|
|
|
|
for gameId, game in pairs(activeGames) do
|
|
|
|
debugPrint("Spiel: " .. gameId .. " - " .. game.name)
|
|
|
|
debugPrint(" Status: " .. game.status)
|
|
|
|
debugPrint(" Feld: " .. game.fieldId)
|
|
|
|
debugPrint(" Team 1: " .. #game.team1 .. " Spieler, Score: " .. game.score.team1)
|
|
|
|
debugPrint(" Team 2: " .. #game.team2 .. " Spieler, Score: " .. game.score.team2)
|
|
|
|
end
|
|
|
|
|
|
|
|
if src > 0 then
|
|
|
|
TriggerClientEvent('QBCore:Notify', src, 'TDM Status in Server-Konsole', 'info')
|
|
|
|
end
|
|
|
|
end, true)
|
|
|
|
|
|
|
|
-- Hilfsfunktion für table.count
|
|
|
|
table.count = function(tbl)
|
|
|
|
local count = 0
|
|
|
|
for _, _ in pairs(tbl) do
|
|
|
|
count = count + 1
|
|
|
|
end
|
|
|
|
return count
|
|
|
|
end
|