101 lines
3.2 KiB
C#
101 lines
3.2 KiB
C#
using GTANetworkAPI;
|
|
using ReallifeGamemode.Server.Entities;
|
|
using ReallifeGamemode.Server.Extensions;
|
|
using ReallifeGamemode.Server.Job;
|
|
using ReallifeGamemode.Server.Models;
|
|
using ReallifeGamemode.Server.Services;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
|
|
namespace ReallifeGamemode.Server.Managers
|
|
{
|
|
class JobManager : Script
|
|
{
|
|
private static List<JobBase> _jobs = new List<JobBase>();
|
|
|
|
public static void LoadJobs()
|
|
{
|
|
IEnumerable<Type> jobTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsSubclassOf(typeof(JobBase)) && !t.IsAbstract);
|
|
|
|
foreach (var type in jobTypes)
|
|
{
|
|
var instance = Activator.CreateInstance(type) as JobBase;
|
|
if (GetJob(instance.Id) != null)
|
|
{
|
|
throw new InvalidOperationException($"Double Job ID found: {instance.Id} | {instance.Name}");
|
|
}
|
|
_jobs.Add(instance);
|
|
NAPI.Util.ConsoleOutput($"Loading job {instance.Name}");
|
|
}
|
|
|
|
NAPI.Util.ConsoleOutput($"Loaded {_jobs.Count} jobs");
|
|
}
|
|
|
|
public static JobBase GetJob(int id) => _jobs.Where(j => j.Id == id).FirstOrDefault();
|
|
|
|
public static T GetJob<T>() where T : JobBase
|
|
{
|
|
return _jobs.Where(j => j.GetType() == typeof(T)).FirstOrDefault() as T;
|
|
}
|
|
|
|
public static List<JobBase> GetJobs() => _jobs.OrderBy(j => j.Id).ToList();
|
|
|
|
[RemoteEvent("CLIENT:JobCenter_CancelJob")]
|
|
public void CancelJobEvent(Client player)
|
|
{
|
|
using(var dbContext = new DatabaseContext())
|
|
{
|
|
User u = player.GetUser(dbContext);
|
|
|
|
if (u == null) return;
|
|
|
|
if(u.JobId == null)
|
|
{
|
|
ChatService.Error(player, "Du hast momentan keinen Job, den du kündigen könntest.");
|
|
return;
|
|
}
|
|
|
|
u.JobId = null;
|
|
|
|
player.SendChatMessage("~y~[JOBCENTER]~s~ Du hast deinen Job erfolgreich gekündigt.");
|
|
|
|
dbContext.SaveChanges();
|
|
}
|
|
}
|
|
|
|
[RemoteEvent("CLIENT:JobCenter_AcceptJob")]
|
|
public void AcceptJobEvent(Client player, int jobId)
|
|
{
|
|
using (var dbContext = new DatabaseContext())
|
|
{
|
|
User u = player.GetUser(dbContext);
|
|
|
|
if (u == null) return;
|
|
|
|
if (u.JobId != null)
|
|
{
|
|
ChatService.Error(player, "Du musst deinen alten Job kündigen, bevor du einen neuen ausüben kannst");
|
|
return;
|
|
}
|
|
|
|
JobBase job = JobManager.GetJob(jobId);
|
|
|
|
if(job == null)
|
|
{
|
|
ChatService.Error(player, "Bei der Job-Annahme ist ein Fehler aufgetretet: Dieser Job wurde nicht gefunden");
|
|
return;
|
|
}
|
|
|
|
player.SendChatMessage($"~y~[JOBCENTER]~s~ Du hast erfolgreich deinen neuen Job: ~o~{job.Name}~s~ angenommen.");
|
|
|
|
u.JobId = jobId;
|
|
|
|
dbContext.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|