From 9dda9b195de445a3abb0eec9fc7c049f6e4b5ccb Mon Sep 17 00:00:00 2001 From: hydrant Date: Mon, 20 May 2019 21:41:02 +0200 Subject: [PATCH] add option to stop job, enable engine of job vehicle only when player is in job --- ReallifeGamemode.Client/Jobs/main.ts | 3 + .../Entities/ServerVehicle.cs | 3 + ReallifeGamemode.Server/Events/Disconnect.cs | 1 + ReallifeGamemode.Server/Events/VehicleMenu.cs | 8 +++ ReallifeGamemode.Server/Job/JobBase.cs | 17 +++--- .../Managers/JobManager.cs | 56 +++++++++++++++++-- 6 files changed, 75 insertions(+), 13 deletions(-) diff --git a/ReallifeGamemode.Client/Jobs/main.ts b/ReallifeGamemode.Client/Jobs/main.ts index 19053f72..de3ea55a 100644 --- a/ReallifeGamemode.Client/Jobs/main.ts +++ b/ReallifeGamemode.Client/Jobs/main.ts @@ -7,12 +7,15 @@ export default function (globalData: GlobalData) { var menu = new NativeUI.Menu("Job", jobName, new NativeUI.Point(50, 50), null, null); menu.AddItem(new NativeUI.UIMenuItem("Job starten")); + menu.AddItem(new NativeUI.UIMenuItem("Job beenden")); globalData.InMenu = true; menu.ItemSelect.on((item: NativeUI.UIMenuItem, index: number) => { if (index === 0) { // Job starten mp.events.callRemote("CLIENT:Job_StartJob"); + } else if (index === 1) { // Job stoppen + mp.events.callRemote("CLIENT:Job_StopJob"); } menu.Close(); }); diff --git a/ReallifeGamemode.Server/Entities/ServerVehicle.cs b/ReallifeGamemode.Server/Entities/ServerVehicle.cs index c17fdef3..1a09b92d 100644 --- a/ReallifeGamemode.Server/Entities/ServerVehicle.cs +++ b/ReallifeGamemode.Server/Entities/ServerVehicle.cs @@ -30,6 +30,9 @@ namespace ReallifeGamemode.Server.Entities [NotMapped] public Vector3 Position => new Vector3(PositionX, PositionY, PositionZ); + [NotMapped] + public Vehicle Vehicle => VehicleManager.GetVehicleFromServerVehicle(this); + public Vehicle Spawn(Vehicle currentVeh = null) { if (currentVeh != null) VehicleManager.DeleteVehicle(currentVeh); diff --git a/ReallifeGamemode.Server/Events/Disconnect.cs b/ReallifeGamemode.Server/Events/Disconnect.cs index caf6a6bf..ad925d87 100644 --- a/ReallifeGamemode.Server/Events/Disconnect.cs +++ b/ReallifeGamemode.Server/Events/Disconnect.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using GTANetworkAPI; using ReallifeGamemode.Server.Extensions; +using ReallifeGamemode.Server.Job; using ReallifeGamemode.Server.Managers; using ReallifeGamemode.Server.Models; diff --git a/ReallifeGamemode.Server/Events/VehicleMenu.cs b/ReallifeGamemode.Server/Events/VehicleMenu.cs index 1b8af631..9cc2479f 100644 --- a/ReallifeGamemode.Server/Events/VehicleMenu.cs +++ b/ReallifeGamemode.Server/Events/VehicleMenu.cs @@ -52,6 +52,14 @@ namespace ReallifeGamemode.Server.Events return; } } + else if(sV is JobVehicle jV) + { + if(!jV.GetJob().GetUsersInJob().Contains(player) && !u.IsAdmin(AdminLevel.ADMIN3)) + { + player.SendNotification("~r~Du hast keinen Schlüssel."); + return; + } + } } VehicleStreaming.SetEngineState(v, !state); } diff --git a/ReallifeGamemode.Server/Job/JobBase.cs b/ReallifeGamemode.Server/Job/JobBase.cs index e4c6bf59..64fbd844 100644 --- a/ReallifeGamemode.Server/Job/JobBase.cs +++ b/ReallifeGamemode.Server/Job/JobBase.cs @@ -3,6 +3,7 @@ using ReallifeGamemode.Server.Entities; using ReallifeGamemode.Server.Managers; using ReallifeGamemode.Server.Models; using ReallifeGamemode.Server.Services; +using ReallifeGamemode.Server.Util; using System; using System.Collections.Generic; using System.Linq; @@ -12,11 +13,11 @@ namespace ReallifeGamemode.Server.Job { public abstract class JobBase { - protected delegate void JobStartHandler(); - protected delegate void JobStopHandler(); + public delegate void JobStartHandler(); + public delegate void JobStopHandler(); - protected event JobStartHandler JobStart; - protected event JobStopHandler JobStop; + public event JobStartHandler JobStart; + public event JobStopHandler JobStop; private readonly List _inJob = new List(); @@ -30,11 +31,7 @@ namespace ReallifeGamemode.Server.Job { _inJob.Add(player); - if (NeedVehicleToStart && !GetJobVehicles().Any(v => VehicleManager.GetVehicleFromServerVehicle(v).Handle.Value == player.Vehicle?.Handle.Value)) - { - ChatService.Error(player, "Zum Start dieses Jobs musst du in einem Jobfahrzeug sein"); - return; - } + player.SendChatMessage($"~y~[JOB]~s~ Du hast deinen Job ~o~{this.Name}~s~ gestartet."); JobStart?.Invoke(); } @@ -43,6 +40,8 @@ namespace ReallifeGamemode.Server.Job { _inJob.Remove(player); + player.SendChatMessage($"~y~[JOB]~s~ Du hast deinen Job ~o~{this.Name}~s~ beendet."); + JobStop?.Invoke(); } diff --git a/ReallifeGamemode.Server/Managers/JobManager.cs b/ReallifeGamemode.Server/Managers/JobManager.cs index f3ab6c67..807e5943 100644 --- a/ReallifeGamemode.Server/Managers/JobManager.cs +++ b/ReallifeGamemode.Server/Managers/JobManager.cs @@ -4,6 +4,7 @@ using ReallifeGamemode.Server.Extensions; using ReallifeGamemode.Server.Job; using ReallifeGamemode.Server.Models; using ReallifeGamemode.Server.Services; +using ReallifeGamemode.Server.Util; using System; using System.Collections.Generic; using System.Linq; @@ -46,13 +47,13 @@ namespace ReallifeGamemode.Server.Managers [RemoteEvent("CLIENT:JobCenter_CancelJob")] public void CancelJobEvent(Client player) { - using(var dbContext = new DatabaseContext()) + using (var dbContext = new DatabaseContext()) { User u = player.GetUser(dbContext); if (u == null) return; - if(u.JobId == null) + if (u.JobId == null) { ChatService.Error(player, "Du hast momentan keinen Job, den du kündigen könntest."); return; @@ -83,7 +84,7 @@ namespace ReallifeGamemode.Server.Managers JobBase job = JobManager.GetJob(jobId); - if(job == null) + if (job == null) { ChatService.Error(player, "Bei der Job-Annahme ist ein Fehler aufgetretet: Dieser Job wurde nicht gefunden"); return; @@ -118,13 +119,60 @@ namespace ReallifeGamemode.Server.Managers JobBase job = GetJob(u.JobId.Value); - if(job.GetUsersInJob().Contains(player)) + if (job.GetUsersInJob().Contains(player)) { ChatService.Error(player, "Du bist schon in deinem Job aktiv"); return; } + if (job.NeedVehicleToStart && !job.GetJobVehicles().Any(v => v.Vehicle.Handle.Value == player.Vehicle?.Handle.Value)) + { + ChatService.Error(player, "Zum Start dieses Jobs musst du in einem Jobfahrzeug sein"); + return; + } + job.StartJob(player); } + + [RemoteEvent("CLIENT:Job_StopJob")] + public void StopJob(Client player) + { + User u = player.GetUser(); + + if (u.JobId == null) return; + + JobBase job = GetJob(u.JobId.Value); + + if (!job.GetUsersInJob().Contains(player)) + { + ChatService.Error(player, "Du führst deinen Job momentan nicht aus"); + return; + } + + if (job.NeedVehicleToStart && player.Vehicle != null) + { + VehicleStreaming.SetEngineState(player.Vehicle, false); + VehicleStreaming.SetLockStatus(player.Vehicle, false); + player.WarpOutOfVehicle(); + } + + job.StopJob(player); + } + + [ServerEvent(Event.PlayerExitVehicle)] + public void JobPlayerExitVehicle(Client player, Vehicle veh) + { + User u = player.GetUser(); + + if (u.JobId == null) return; + + JobBase job = GetJob(u.JobId.Value); + + if (job.GetUsersInJob().Contains(player) && job.NeedVehicleToStart) + { + job.StopJob(player); + return; + } + } } }