From 73bb9b6fda1beeeb84e91ecf479ca7918c8e1f40 Mon Sep 17 00:00:00 2001 From: Lennart Kampshoff Date: Sun, 23 Sep 2018 21:10:42 +0200 Subject: [PATCH] Add Faction Ranks --- Model/DatabaseContext.cs | 1 + Server/Commands/Admin.cs | 27 ++++++++++++++++++++----- Server/Entities/Faction.cs | 3 ++- Server/Entities/FactionRank.cs | 30 ++++++++++++++++++++++++++++ Server/Entities/User.cs | 25 +++++++++++++---------- Server/Extensions/ClientExtension.cs | 4 ++-- 6 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 Server/Entities/FactionRank.cs diff --git a/Model/DatabaseContext.cs b/Model/DatabaseContext.cs index c64e4582..509e43d2 100644 --- a/Model/DatabaseContext.cs +++ b/Model/DatabaseContext.cs @@ -35,6 +35,7 @@ namespace reallife_gamemode.Model } public DbSet Factions { get; set; } + public DbSet FactionRanks { get; set; } public DbSet Users { get; set; } } } diff --git a/Server/Commands/Admin.cs b/Server/Commands/Admin.cs index 27210f53..5fa3dd81 100644 --- a/Server/Commands/Admin.cs +++ b/Server/Commands/Admin.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using GTANetworkAPI; using reallife_gamemode.Model; +using reallife_gamemode.Server.Entities; using reallife_gamemode.Server.Extensions; using reallife_gamemode.Server.Services; using reallife_gamemode.Server.Util; @@ -240,17 +241,33 @@ namespace reallife_gamemode.Server.Commands using (var dbContext = new DatabaseContext()) { Entities.Faction f = dbContext.Factions.FirstOrDefault(x => x.Id == faction); - if(f == null) + if(f == null && faction != 0) { player.SendChatMessage("~r~[FEHLER]~s~ Diese Fraktion existiert nicht (Liste: ~m~/factionlist)."); return; } - target.GetUser(dbContext).FactionId = f.Id; - dbContext.SaveChanges(); + User u = dbContext.Users.SingleOrDefault(x => x.Name == target.Name); - player.SendChatMessage("~b~[ADMIN]~s~ Du hast hast den Spieler ~y~" + target.Name + "~s~ administrativ in die Fraktion ~o~" + f.Name + "~s~ eingeladen."); - target.SendChatMessage("~b~[ADMIN]~s~ Du wurdest von ~y~" + player.Name + "~s~ administrativ in die Fraktion ~o~" + f.Name + "~s~ eingeladen."); + if(faction != 0) + { + u.FactionId = f.Id; + u.FactionRankId = dbContext.FactionRanks. + OrderBy(x => x.Order) + .FirstOrDefault(r => r.FactionId == f.Id)?.Id ?? null; + + player.SendChatMessage("~b~[ADMIN]~s~ Du hast hast den Spieler ~y~" + target.Name + "~s~ administrativ in die Fraktion ~o~" + f.Name + "~s~ eingeladen."); + target.SendChatMessage("~b~[ADMIN]~s~ Du wurdest von ~y~" + player.Name + "~s~ administrativ in die Fraktion ~o~" + f.Name + "~s~ eingeladen."); + } + else + { + u.FactionId = null; + u.FactionRankId = null; + player.SendChatMessage("~b~[ADMIN]~s~ Du hast hast den Spieler ~y~" + target.Name + "~s~ administrativ aus seiner Fraktion geworfen."); + target.SendChatMessage("~b~[ADMIN]~s~ Du wurdest von ~y~" + player.Name + "~s~ administrativ aus deiner Fraktion geworfen."); + } + + dbContext.SaveChanges(); } } } diff --git a/Server/Entities/Faction.cs b/Server/Entities/Faction.cs index 32820d5d..88db5333 100644 --- a/Server/Entities/Faction.cs +++ b/Server/Entities/Faction.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; diff --git a/Server/Entities/FactionRank.cs b/Server/Entities/FactionRank.cs new file mode 100644 index 00000000..c0aca96c --- /dev/null +++ b/Server/Entities/FactionRank.cs @@ -0,0 +1,30 @@ +using reallife_gamemode.Model; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; + +namespace reallife_gamemode.Server.Entities +{ + public class FactionRank + { + [Key] + public int Id { get; set; } + public string RankName { get; set; } + public int Order { get; set; } + + + public int FactionId { get; set; } + public Faction Faction { get; set; } + + public Faction GetFaction() + { + using (var context = new DatabaseContext()) + { + return context.Factions.FirstOrDefault(f => f.Id == FactionId); + } + } + } +} diff --git a/Server/Entities/User.cs b/Server/Entities/User.cs index 09d42de0..7f272321 100644 --- a/Server/Entities/User.cs +++ b/Server/Entities/User.cs @@ -36,21 +36,26 @@ namespace reallife_gamemode.Server.Entities public int? FactionId { get; set; } public Faction Faction { get; set; } + + public int? FactionRankId { get; set; } + public FactionRank FactionRank { get;set; } - public bool IsAdmin(AdminLevel level) => AdminLevel >= level; - public Faction GetFaction(DatabaseContext context = null) + public Faction GetFaction() { - if(context == null) - { - using(context = new DatabaseContext()) - { - return context.Factions.FirstOrDefault(f => f.Id == FactionId); - } - } - else + using(var context = new DatabaseContext()) { return context.Factions.FirstOrDefault(f => f.Id == FactionId); } } + + public FactionRank GetFactionRank() + { + using (var context = new DatabaseContext()) + { + return context.FactionRanks.FirstOrDefault(fR => fR.Id == FactionRankId); + } + } + + public bool IsAdmin(AdminLevel level) => AdminLevel >= level; } } diff --git a/Server/Extensions/ClientExtension.cs b/Server/Extensions/ClientExtension.cs index a9222cea..6eb5d57c 100644 --- a/Server/Extensions/ClientExtension.cs +++ b/Server/Extensions/ClientExtension.cs @@ -40,14 +40,14 @@ namespace reallife_gamemode.Server.Extensions { User u = client.GetUser(); if (u == null) return null; - return context.Factions.FirstOrDefault(f => f.Id == u.FactionId); + return u.GetFaction(); } } else { User u = client.GetUser(); if (u == null) return null; - return context.Factions.FirstOrDefault(f => f.Id == u.FactionId); + return u.GetFaction(); } } }