converted jobs to same system as businesses, add option to choose job at cityhall

This commit is contained in:
hydrant
2019-05-16 14:52:19 +02:00
parent 8fd4041b3c
commit aa81fa95a4
18 changed files with 1422 additions and 59 deletions

View File

@@ -15,11 +15,14 @@ export default function (globalData: GlobalData) {
var keyBound = false;
var menu: NativeUI.Menu = null;
var joblist: string[] = [];
mp.events.add("SERVER:CityHall_ShowHelpText", () => {
mp.events.add("SERVER:CityHall_ShowHelpText", (joblista) => {
mp.game.ui.setTextComponentFormat('STRING');
mp.game.ui.addTextComponentSubstringPlayerName('Drücke ~INPUT_CONTEXT~, um das Stadthallen-Menü zu öffnen');
mp.game.ui.displayHelpTextFromStringLabel(0, true, true, -1);
joblist = JSON.parse(joblista);
if (!keyBound) {
keyBound = true;
@@ -29,6 +32,7 @@ export default function (globalData: GlobalData) {
mp.events.add("SERVER:CityHall_ClearHelpText", () => {
mp.game.ui.clearHelp(false);
if (menu) menu.Close();
if (keyBound) {
mp.keys.unbind(0x45, false, keyPressHandler);
keyBound = false;
@@ -45,11 +49,32 @@ export default function (globalData: GlobalData) {
var jobItem = new UIMenuItem("Jobs", "Jobcenter");
menu.AddItem(jobItem);
var jobMenu = new Menu("Jobcenter", "Hartz 4 und der Tag gehoert dir.", new Point(50, 50), null, null);
jobMenu.Visible = false;
var jobMenu = new Menu("Jobcenter", "Suche dir einen neuen Job aus", new Point(50, 50), null, null);
for (var i = 0; i < joblist.length; i++) {
var job = joblist[i];
jobMenu.AddItem(new UIMenuItem(job));
}
var cancelJobItem = new UIMenuItem("Job kuendigen");
cancelJobItem.BackColor = new Color(213, 0, 0);
cancelJobItem.HighlightedBackColor = new Color(229, 57, 53);
jobMenu.AddItem(cancelJobItem);
menu.BindMenuToItem(jobMenu, jobItem);
jobMenu.ItemSelect.on((item: NativeUI.UIMenuItem, index: number) => {
if (item === cancelJobItem) { // Job kündigen
mp.events.callRemote("CLIENT:JobCenter_CancelJob");
} else {
mp.events.callRemote("CLIENT:JobCenter_AcceptJob", index + 1);
}
menu.Close();
});
jobMenu.Visible = false;
var groupCreateItem = new UIMenuItem("Gruppe erstellen", "Erstelle eine neue Gruppe");
groupCreateItem.SetRightLabel("~g~50.000$");
menu.AddItem(groupCreateItem);
@@ -59,6 +84,8 @@ export default function (globalData: GlobalData) {
cancelItem.HighlightedBackColor = new Color(229, 57, 53);
menu.AddItem(cancelItem);
menu.Open();
menu.ItemSelect.on((item: NativeUI.UIMenuItem, index) => {
if (item === groupCreateItem) { // Gruppe
var input = new InputHelper("Wie soll die Gruppe heißen?", globalData);

View File

@@ -10,7 +10,6 @@ namespace ReallifeGamemode.Server.Business
public abstract class BusinessBase : IBankAccountOwner
{
private TextLabel _informationLabel;
private Marker _marker;
private ColShape _colShape;
public abstract int Id { get; }
@@ -36,7 +35,7 @@ namespace ReallifeGamemode.Server.Business
public void Setup()
{
_informationLabel = NAPI.TextLabel.CreateTextLabel(Name, Position.Add(new Vector3(0, 0, 0.5)), 20.0f, 1.3f, 0, new Color(255, 255, 255));
_marker = NAPI.Marker.CreateMarker(MarkerType.VerticalCylinder, Position.Subtract(new Vector3(0, 0, 1.5)), new Vector3(), new Vector3(), 1f, new Color(255, 255, 255));
NAPI.Marker.CreateMarker(MarkerType.VerticalCylinder, Position.Subtract(new Vector3(0, 0, 1.5)), new Vector3(), new Vector3(), 1f, new Color(255, 255, 255));
_colShape = NAPI.ColShape.CreateSphereColShape(Position.Subtract(new Vector3(0, 0, 1.5)), 3f);
_colShape.OnEntityEnterColShape += EntityEnterBusinessColShape;

View File

@@ -1,17 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace ReallifeGamemode.Server.Entities
{
public class Job
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
}
}

View File

@@ -1,4 +1,6 @@
using Microsoft.EntityFrameworkCore;
using ReallifeGamemode.Server.Job;
using ReallifeGamemode.Server.Managers;
using ReallifeGamemode.Server.Models;
using System;
using System.Collections.Generic;
@@ -9,11 +11,14 @@ namespace ReallifeGamemode.Server.Entities
{
public class JobVehicle : ServerVehicle
{
public virtual Job Job { get; set; }
public int JobId { get; set; }
public JobBase GetJob() => JobManager.GetJob(JobId);
public override string ToString()
{
using (var dbContext = new DatabaseContext())
return "Job Fahrzeug | Job: " + dbContext.JobVehicles.Include(j => j.Job).First(j => j.Id == this.Id).Job.Name;
return "Job Fahrzeug | Job: " + this.GetJob().Name;
}
}
}

View File

@@ -1,5 +1,6 @@
using GTANetworkAPI;
using ReallifeGamemode.Server.Business;
using ReallifeGamemode.Server.Managers;
using ReallifeGamemode.Server.Util;
using System;
using System.Collections.Generic;
@@ -23,7 +24,7 @@ namespace ReallifeGamemode.Server.Entities
public override string ToString()
{
return "ShopVehicle";
return "ShopVehicle | Shop: " + BusinessManager.GetBusiness(BusinessId).Name;
}
}
}

View File

@@ -62,7 +62,7 @@ namespace ReallifeGamemode.Server.Entities
public GroupRank GroupRank { get; set; }
public Job Job { get; set; }
public int? JobId { get; set; }
public FactionRank GetFactionRank()
{

View File

@@ -31,7 +31,6 @@ namespace ReallifeGamemode.Server.Extensions
.Include(u => u.Faction)
.Include(u => u.FactionRank)
.Include(u => u.Group)
.Include(u => u.Job)
.Where(u => u.Name == client.Name)
.FirstOrDefault();
}

View File

@@ -0,0 +1,47 @@
using GTANetworkAPI;
using ReallifeGamemode.Server.Entities;
using ReallifeGamemode.Server.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReallifeGamemode.Server.Job
{
public abstract class JobBase
{
protected delegate void JobStartHandler();
protected delegate void JobStopHandler();
protected event JobStartHandler JobStart;
protected event JobStopHandler JobStop;
private readonly List<Client> _inJob = new List<Client>();
public abstract int Id { get; }
public abstract string Name { get; }
public void StartJob(Client player)
{
_inJob.Add(player);
JobStart();
}
public void StopJob(Client player)
{
_inJob.Remove(player);
JobStop();
}
public List<JobVehicle> GetJobVehicles()
{
using(var dbContext = new DatabaseContext())
{
return dbContext.JobVehicles.Where(j => j.JobId == Id).ToList();
}
}
public List<Client> GetUsersInJob() => _inJob;
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace ReallifeGamemode.Server.Job
{
class PilotJob : JobBase
{
public override int Id => 3;
public override string Name => "Pilot";
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace ReallifeGamemode.Server.Job
{
public class RefuseCollectorJob : JobBase
{
public override int Id => 2;
public override string Name => "Müllmann";
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
using GTANetworkAPI;
namespace ReallifeGamemode.Server.Job
{
class TaxiDriverJob : JobBase
{
public override int Id => 1;
public override string Name => "Taxifahrer";
}
}

View File

@@ -47,6 +47,7 @@ namespace ReallifeGamemode.Server
DoorManager.LoadDoors();
ATMManager.InitATMs();
CityHallManager.LoadCityHall();
JobManager.LoadJobs();
TempBlip tempBlip = new TempBlip()

View File

@@ -1,4 +1,5 @@
using GTANetworkAPI;
using Newtonsoft.Json;
using ReallifeGamemode.Server.Entities;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Models;
@@ -22,7 +23,8 @@ namespace ReallifeGamemode.Server.Managers
var colShape = NAPI.ColShape.CreateSphereColShape(_cityHallPosition, 1.0f);
colShape.OnEntityEnterColShape += (s, c) =>
{
c.TriggerEvent("SERVER:CityHall_ShowHelpText");
var jobs = JobManager.GetJobs().Select(j => j.Name).ToArray();
c.TriggerEvent("SERVER:CityHall_ShowHelpText", JsonConvert.SerializeObject(jobs));
};
colShape.OnEntityExitColShape += (s, c) =>

View File

@@ -0,0 +1,100 @@
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();
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,73 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
namespace ReallifeGamemode.Migrations
{
public partial class JobFix : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_ServerVehicles_Jobs_JobId",
table: "ServerVehicles");
migrationBuilder.DropForeignKey(
name: "FK_Users_Jobs_JobId",
table: "Users");
migrationBuilder.DropTable(
name: "Jobs");
migrationBuilder.DropIndex(
name: "IX_Users_JobId",
table: "Users");
migrationBuilder.DropIndex(
name: "IX_ServerVehicles_JobId",
table: "ServerVehicles");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Jobs",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Jobs", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_Users_JobId",
table: "Users",
column: "JobId");
migrationBuilder.CreateIndex(
name: "IX_ServerVehicles_JobId",
table: "ServerVehicles",
column: "JobId");
migrationBuilder.AddForeignKey(
name: "FK_ServerVehicles_Jobs_JobId",
table: "ServerVehicles",
column: "JobId",
principalTable: "Jobs",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_Users_Jobs_JobId",
table: "Users",
column: "JobId",
principalTable: "Jobs",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
}
}

View File

@@ -420,18 +420,6 @@ namespace ReallifeGamemode.Migrations
b.ToTable("Interiors");
});
modelBuilder.Entity("ReallifeGamemode.Server.Entities.Job", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Name");
b.HasKey("Id");
b.ToTable("Jobs");
});
modelBuilder.Entity("ReallifeGamemode.Server.Entities.Logs.BankAccountTransactionHistory", b =>
{
b.Property<int>("Id")
@@ -827,8 +815,6 @@ namespace ReallifeGamemode.Migrations
b.HasIndex("GroupId");
b.HasIndex("JobId");
b.ToTable("Users");
});
@@ -935,9 +921,7 @@ namespace ReallifeGamemode.Migrations
{
b.HasBaseType("ReallifeGamemode.Server.Entities.ServerVehicle");
b.Property<int?>("JobId");
b.HasIndex("JobId");
b.Property<int>("JobId");
b.HasDiscriminator().HasValue("JobVehicle");
});
@@ -957,8 +941,6 @@ namespace ReallifeGamemode.Migrations
b.Property<int>("Price");
b.Property<int?>("ShopId");
b.ToTable("ShopVehicles");
b.HasDiscriminator().HasValue("ShopVehicle");
@@ -1079,10 +1061,6 @@ namespace ReallifeGamemode.Migrations
b.HasOne("ReallifeGamemode.Server.Entities.Group", "Group")
.WithMany()
.HasForeignKey("GroupId");
b.HasOne("ReallifeGamemode.Server.Entities.Job", "Job")
.WithMany()
.HasForeignKey("JobId");
});
modelBuilder.Entity("ReallifeGamemode.Server.Entities.UserBankAccount", b =>
@@ -1123,13 +1101,6 @@ namespace ReallifeGamemode.Migrations
.HasForeignKey("GroupId");
});
modelBuilder.Entity("ReallifeGamemode.Server.Entities.JobVehicle", b =>
{
b.HasOne("ReallifeGamemode.Server.Entities.Job", "Job")
.WithMany()
.HasForeignKey("JobId");
});
modelBuilder.Entity("ReallifeGamemode.Server.Entities.UserVehicle", b =>
{
b.HasOne("ReallifeGamemode.Server.Entities.User", "User")

View File

@@ -101,7 +101,6 @@ namespace ReallifeGamemode.Server.Models
public DbSet<Entities.GroupVehicle> GroupVehicles { get; set; }
// Jobs
public DbSet<Entities.Job> Jobs { get; set; }
public DbSet<Entities.JobVehicle> JobVehicles { get; set; }
}
}