forked from Simnation/Main
ed
This commit is contained in:
parent
eec7910283
commit
7def30a2ec
2 changed files with 216 additions and 109 deletions
|
@ -28,10 +28,10 @@ RegisterNetEvent('tdm:createGame', function(gameName, fieldId, gameType, passwor
|
|||
fieldId = fieldId,
|
||||
admin = src,
|
||||
adminName = Player.PlayerData.charinfo.firstname .. ' ' .. Player.PlayerData.charinfo.lastname,
|
||||
gameType = gameType, -- 'public' oder 'private'
|
||||
gameType = gameType,
|
||||
password = password,
|
||||
hasPassword = password ~= nil,
|
||||
status = 'waiting', -- waiting, active, finished
|
||||
status = 'waiting',
|
||||
team1 = {},
|
||||
team2 = {},
|
||||
score = {team1 = 0, team2 = 0},
|
||||
|
@ -43,10 +43,14 @@ RegisterNetEvent('tdm:createGame', function(gameName, fieldId, gameType, passwor
|
|||
local typeText = gameType == 'public' and 'öffentliches' or 'privates'
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Dein ' .. typeText .. ' Spiel "' .. gameName .. '" wurde erstellt!', 'success')
|
||||
|
||||
-- Games Liste an alle senden
|
||||
updateGamesListForAll()
|
||||
end)
|
||||
|
||||
RegisterNetEvent('tdm:requestGamesList', function()
|
||||
local src = source
|
||||
TriggerClientEvent('tdm:updateGamesList', src, activeGames)
|
||||
end)
|
||||
|
||||
RegisterNetEvent('tdm:requestJoinGame', function(gameId, password)
|
||||
local src = source
|
||||
local Player = QBCore.Functions.GetPlayer(src)
|
||||
|
@ -96,17 +100,14 @@ RegisterNetEvent('tdm:requestJoinGame', function(gameId, password)
|
|||
|
||||
-- Join Logic basierend auf Spiel Typ
|
||||
if game.gameType == 'public' then
|
||||
-- Öffentliches Spiel - automatisch beitreten
|
||||
joinPlayerToGame(src, gameId)
|
||||
TriggerClientEvent('QBCore:Notify', src, 'Du bist dem öffentlichen Spiel beigetreten!', 'success')
|
||||
else
|
||||
-- Privates Spiel - Join Request an Admin senden
|
||||
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]
|
||||
|
@ -124,7 +125,6 @@ end)
|
|||
RegisterNetEvent('tdm:leaveGame', function()
|
||||
local src = source
|
||||
|
||||
-- Spieler aus allen Spielen entfernen
|
||||
for gameId, game in pairs(activeGames) do
|
||||
removePlayerFromGame(src, gameId)
|
||||
end
|
||||
|
@ -132,29 +132,50 @@ RegisterNetEvent('tdm:leaveGame', function()
|
|||
TriggerClientEvent('tdm:leaveGame', src)
|
||||
end)
|
||||
|
||||
RegisterNetEvent('tdm:playerWasHit', function(gameId, victimTeam)
|
||||
RegisterNetEvent('tdm:playerWasHit', function(gameId, victimTeam, shooterId)
|
||||
local src = source
|
||||
local game = activeGames[gameId]
|
||||
|
||||
if not game then return end
|
||||
|
||||
-- Punkt für das andere Team
|
||||
local scoringTeam = victimTeam == 'team1' and 'team2' or 'team1'
|
||||
game.score[scoringTeam] = game.score[scoringTeam] + 1
|
||||
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
|
||||
end
|
||||
|
||||
-- Score Update an alle Spieler
|
||||
updateScoreForGame(gameId)
|
||||
|
||||
-- Prüfen ob Spiel gewonnen
|
||||
if game.score[scoringTeam] >= game.maxHits then
|
||||
endGame(gameId, scoringTeam)
|
||||
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
|
||||
end
|
||||
end)
|
||||
|
||||
RegisterNetEvent('tdm:playerDied', function(gameId)
|
||||
local src = source
|
||||
removePlayerFromGame(src, gameId)
|
||||
checkGameEnd(gameId)
|
||||
end)
|
||||
|
||||
-- Funktionen
|
||||
|
@ -167,20 +188,25 @@ function joinPlayerToGame(playerId, gameId)
|
|||
|
||||
table.insert(game[team], playerId)
|
||||
|
||||
-- Spiel starten wenn mindestens 2 Spieler
|
||||
if #game.team1 + #game.team2 >= 2 and game.status == 'waiting' then
|
||||
game.status = 'active'
|
||||
game.startTime = os.time()
|
||||
|
||||
-- Game Timer starten
|
||||
startGameTimer(gameId)
|
||||
end
|
||||
|
||||
TriggerClientEvent('tdm:joinGame', playerId, gameId, team, game.fieldId)
|
||||
|
||||
-- Spiel starten wenn genug Spieler
|
||||
checkGameStart(gameId)
|
||||
|
||||
updateGamesListForAll()
|
||||
updateScoreForGame(gameId)
|
||||
updateGamesListForAll()
|
||||
end
|
||||
|
||||
function removePlayerFromGame(playerId, gameId)
|
||||
local game = activeGames[gameId]
|
||||
if not game then return end
|
||||
|
||||
-- Aus Team 1 entfernen
|
||||
-- Spieler aus Teams entfernen
|
||||
for i, id in ipairs(game.team1) do
|
||||
if id == playerId then
|
||||
table.remove(game.team1, i)
|
||||
|
@ -188,7 +214,6 @@ function removePlayerFromGame(playerId, gameId)
|
|||
end
|
||||
end
|
||||
|
||||
-- Aus Team 2 entfernen
|
||||
for i, id in ipairs(game.team2) do
|
||||
if id == playerId then
|
||||
table.remove(game.team2, i)
|
||||
|
@ -206,90 +231,58 @@ function removePlayerFromGame(playerId, gameId)
|
|||
updateGamesListForAll()
|
||||
end
|
||||
|
||||
function checkGameStart(gameId)
|
||||
local game = activeGames[gameId]
|
||||
if not game then return end
|
||||
|
||||
local totalPlayers = #game.team1 + #game.team2
|
||||
|
||||
if totalPlayers >= 2 and game.status == 'waiting' then
|
||||
startGame(gameId)
|
||||
end
|
||||
end
|
||||
|
||||
function startGame(gameId)
|
||||
local game = activeGames[gameId]
|
||||
if not game then return end
|
||||
|
||||
game.status = 'active'
|
||||
game.startTime = os.time()
|
||||
game.score = {team1 = 0, team2 = 0}
|
||||
|
||||
-- Nachricht an alle Spieler
|
||||
for _, playerId in ipairs(game.team1) do
|
||||
TriggerClientEvent('QBCore:Notify', playerId, 'Spiel gestartet!', 'success')
|
||||
end
|
||||
for _, playerId in ipairs(game.team2) do
|
||||
TriggerClientEvent('QBCore:Notify', playerId, 'Spiel gestartet!', 'success')
|
||||
end
|
||||
|
||||
updateScoreForGame(gameId)
|
||||
updateGamesListForAll()
|
||||
|
||||
-- Game Timer
|
||||
CreateThread(function()
|
||||
while activeGames[gameId] and activeGames[gameId].status == 'active' do
|
||||
Wait(1000)
|
||||
|
||||
local currentTime = os.time()
|
||||
local elapsed = currentTime - game.startTime
|
||||
|
||||
if elapsed >= game.maxTime then
|
||||
local winnerTeam = game.score.team1 > game.score.team2 and 'team1' or 'team2'
|
||||
endGame(gameId, winnerTeam, 'Zeit abgelaufen')
|
||||
break
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function endGame(gameId, winnerTeam, reason)
|
||||
local game = activeGames[gameId]
|
||||
if not game then return end
|
||||
|
||||
game.status = 'finished'
|
||||
|
||||
local message = reason or (winnerTeam and ('Team ' .. winnerTeam .. ' hat gewonnen!') or 'Spiel beendet')
|
||||
|
||||
-- Nachricht an alle Spieler
|
||||
local allPlayers = {}
|
||||
for _, playerId in ipairs(game.team1) do
|
||||
if winnerTeam then
|
||||
TriggerClientEvent('tdm:gameEnded', playerId, winnerTeam, game.score.team1, game.score.team2)
|
||||
else
|
||||
TriggerClientEvent('QBCore:Notify', playerId, message, 'error')
|
||||
TriggerClientEvent('tdm:leaveGame', playerId)
|
||||
end
|
||||
table.insert(allPlayers, playerId)
|
||||
end
|
||||
for _, playerId in ipairs(game.team2) do
|
||||
if winnerTeam then
|
||||
TriggerClientEvent('tdm:gameEnded', playerId, winnerTeam, game.score.team1, game.score.team2)
|
||||
else
|
||||
TriggerClientEvent('QBCore:Notify', playerId, message, 'error')
|
||||
TriggerClientEvent('tdm:leaveGame', playerId)
|
||||
end
|
||||
table.insert(allPlayers, playerId)
|
||||
end
|
||||
|
||||
-- Spiel nach 15 Sekunden löschen
|
||||
CreateThread(function()
|
||||
Wait(15000)
|
||||
-- Game End Event an alle Spieler
|
||||
for _, playerId in ipairs(allPlayers) do
|
||||
TriggerClientEvent('tdm:gameEnded', playerId, winnerTeam, game.score.team1, game.score.team2)
|
||||
end
|
||||
|
||||
-- Spiel nach 10 Sekunden löschen
|
||||
SetTimeout(10000, function()
|
||||
activeGames[gameId] = nil
|
||||
updateGamesListForAll()
|
||||
end)
|
||||
|
||||
if reason then
|
||||
print('[TDM] Spiel ' .. gameId .. ' beendet: ' .. reason)
|
||||
end
|
||||
end
|
||||
|
||||
function startGameTimer(gameId)
|
||||
CreateThread(function()
|
||||
local game = activeGames[gameId]
|
||||
if not game then return end
|
||||
|
||||
local maxTime = game.maxTime
|
||||
local startTime = os.time()
|
||||
|
||||
while game and game.status == 'active' and (os.time() - startTime) < maxTime do
|
||||
Wait(1000)
|
||||
game = activeGames[gameId] -- Refresh game data
|
||||
end
|
||||
|
||||
-- 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
|
||||
endGame(gameId, winnerTeam, 'Zeit abgelaufen')
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function checkGameEnd(gameId)
|
||||
local game = activeGames[gameId]
|
||||
if not game then return end
|
||||
function checkGameEnd(gameId)
|
||||
local game = activeGames[gameId]
|
||||
if not game then return end
|
||||
|
@ -299,7 +292,6 @@ function checkGameEnd(gameId)
|
|||
if totalPlayers < 2 and game.status == 'active' then
|
||||
endGame(gameId, nil, 'Zu wenig Spieler')
|
||||
elseif totalPlayers == 0 then
|
||||
-- Spiel löschen wenn keine Spieler mehr da sind
|
||||
activeGames[gameId] = nil
|
||||
updateGamesListForAll()
|
||||
end
|
||||
|
@ -309,17 +301,20 @@ function updateScoreForGame(gameId)
|
|||
local game = activeGames[gameId]
|
||||
if not game then return end
|
||||
|
||||
-- Score an alle Spieler des Spiels senden
|
||||
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)
|
||||
TriggerClientEvent('tdm:updateScore', playerId, game.score.team1, game.score.team2, gameStats)
|
||||
end
|
||||
for _, playerId in ipairs(game.team2) do
|
||||
TriggerClientEvent('tdm:updateScore', playerId, game.score.team1, game.score.team2)
|
||||
TriggerClientEvent('tdm:updateScore', playerId, game.score.team1, game.score.team2, gameStats)
|
||||
end
|
||||
end
|
||||
|
||||
function updateGamesListForAll()
|
||||
-- Games Liste an alle Spieler senden
|
||||
local players = QBCore.Functions.GetPlayers()
|
||||
for _, playerId in pairs(players) do
|
||||
TriggerClientEvent('tdm:updateGamesList', playerId, activeGames)
|
||||
|
@ -330,7 +325,6 @@ end
|
|||
AddEventHandler('playerDropped', function()
|
||||
local src = source
|
||||
|
||||
-- Spieler aus allen Spielen entfernen
|
||||
for gameId, game in pairs(activeGames) do
|
||||
removePlayerFromGame(src, gameId)
|
||||
end
|
||||
|
@ -341,7 +335,29 @@ AddEventHandler('onResourceStart', function(resourceName)
|
|||
if GetCurrentResourceName() == resourceName then
|
||||
activeGames = {}
|
||||
gameIdCounter = 1
|
||||
print('[TDM] TeamDeathmatch System gestartet!')
|
||||
end
|
||||
end)
|
||||
|
||||
|
||||
|
||||
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 = {}
|
||||
print('[TDM] TeamDeathmatch System gestoppt!')
|
||||
end
|
||||
end)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue