From f91ef8960ced0fc699d72e0a5bafc4ab1eb0d91b Mon Sep 17 00:00:00 2001 From: Lennart Kampshoff Date: Sun, 23 Sep 2018 15:59:40 +0200 Subject: [PATCH] Fixed EF error, edit commands, fix faction broadcast --- Main.cs | 11 -------- Server/Commands/Admin.cs | 6 ++--- Server/Commands/Faction.cs | 40 +++++++++++++++++++++++++--- Server/Entities/User.cs | 20 +++++++++++++- Server/Extensions/ClientExtension.cs | 14 ++++++---- Server/Services/ChatService.cs | 18 ++++++++++--- 6 files changed, 82 insertions(+), 27 deletions(-) diff --git a/Main.cs b/Main.cs index 8018ab39..7d0b7900 100644 --- a/Main.cs +++ b/Main.cs @@ -30,17 +30,6 @@ namespace reallife_gamemode context.Factions.FirstOrDefault(); context.Users.FirstOrDefault(); context.SaveChanges(); - - foreach(Faction f in context.Factions) - { - NAPI.Util.ConsoleOutput(f.Name); - } - - foreach(User u in context.Users.Include(x => x.Faction)) - { - Faction f = u.Faction; - NAPI.Util.ConsoleOutput("User: " + u.Name + " | Faction: " + (f == null ? "null" : f.Name)); - } } } } diff --git a/Server/Commands/Admin.cs b/Server/Commands/Admin.cs index 34517d8c..27210f53 100644 --- a/Server/Commands/Admin.cs +++ b/Server/Commands/Admin.cs @@ -246,11 +246,11 @@ namespace reallife_gamemode.Server.Commands return; } - target.GetUser(dbContext).Faction = f; + target.GetUser(dbContext).FactionId = f.Id; dbContext.SaveChanges(); - player.SendChatMessage("~b~[ADMIN]~y~ " + player.Name + "~s~ hat den Spieler ~y~" + target.Name + "~s~ administrativ in die Fraktion ~o~" + f.Name + "~s~ eingeladen."); - target.SendChatMessage("~b~[ADMIN]~y~ Du wurdest von ~y~" + player.Name + "~s~ administrativ in die Fraktion ~o~" + f.Name + "~s~ eingeladen."); + 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."); } } } diff --git a/Server/Commands/Faction.cs b/Server/Commands/Faction.cs index f333dae7..5142c8e1 100644 --- a/Server/Commands/Faction.cs +++ b/Server/Commands/Faction.cs @@ -1,9 +1,11 @@ using GTANetworkAPI; +using reallife_gamemode.Model; using reallife_gamemode.Server.Entities; using reallife_gamemode.Server.Extensions; using reallife_gamemode.Server.Services; using System; using System.Collections.Generic; +using System.Linq; using System.Text; /** @@ -19,16 +21,46 @@ namespace reallife_gamemode.Server.Commands [Command("f", "~m~Benutzung: ~s~/f [Nachricht]", GreedyArg = true)] public void CmdFactionF(Client player, string message) { - User u = player.GetUser(); - if(u.Faction == null) + Entities.Faction f = player.GetFaction(); + if(f == null || f.StateOwned) { ChatService.NotAuthorized(player); return; } - string broadcastMessage = "** " + player.Name + ": " + message + " )) **"; + string broadcastMessage = "!{02FCFF}** " + player.Name + ": " + message + " )) **"; + ChatService.BroadcastFaction(broadcastMessage, f); + } - ChatService.BroadcastFaction(broadcastMessage, u.Faction); + [Command("r", "~m~Benutzung: ~s~/r [Nachricht]", GreedyArg = true)] + public void CmdFactionR(Client player, string message) + { + Entities.Faction f = player.GetFaction(); + if (f == null || !f.StateOwned) + { + ChatService.NotAuthorized(player); + return; + } + + string broadcastMessage = "!{33AA33}** " + player.Name + ": " + message + ", over **"; + ChatService.BroadcastFaction(broadcastMessage, f); + } + + [Command("d", "~m~Benutzung: ~s~/d [Nachricht]", GreedyArg = true)] + public void CmdFactionD(Client player, string message) + { + Entities.Faction f = player.GetFaction(); + if (f == null || !f.StateOwned) + { + ChatService.NotAuthorized(player); + return; + } + + string broadcastMessage = "!{CC3333}** " + player.Name + ": " + message + ", over **"; + using(var context = new DatabaseContext()) + { + ChatService.BroadcastFaction(broadcastMessage, context.Factions.ToList().FindAll(c => c.StateOwned)); + } } } } diff --git a/Server/Entities/User.cs b/Server/Entities/User.cs index 1e13474b..09d42de0 100644 --- a/Server/Entities/User.cs +++ b/Server/Entities/User.cs @@ -1,8 +1,10 @@ -using reallife_gamemode.Server.Util; +using reallife_gamemode.Model; +using reallife_gamemode.Server.Util; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; using System.Text; /** @@ -31,8 +33,24 @@ namespace reallife_gamemode.Server.Entities [StringLength(64)] public string Email { get; set; } public AdminLevel AdminLevel { get; set; } + + public int? FactionId { get; set; } public Faction Faction { get; set; } public bool IsAdmin(AdminLevel level) => AdminLevel >= level; + public Faction GetFaction(DatabaseContext context = null) + { + if(context == null) + { + using(context = new DatabaseContext()) + { + return context.Factions.FirstOrDefault(f => f.Id == FactionId); + } + } + else + { + return context.Factions.FirstOrDefault(f => f.Id == FactionId); + } + } } } diff --git a/Server/Extensions/ClientExtension.cs b/Server/Extensions/ClientExtension.cs index 24035737..a9222cea 100644 --- a/Server/Extensions/ClientExtension.cs +++ b/Server/Extensions/ClientExtension.cs @@ -21,9 +21,9 @@ namespace reallife_gamemode.Server.Extensions { if(context == null) { - using (DatabaseContext dbContext = new DatabaseContext()) + using (context = new DatabaseContext()) { - return dbContext.Users.FirstOrDefault(u => u.Name == client.Name); + return context.Users.FirstOrDefault(u => u.Name == client.Name); } } else @@ -36,14 +36,18 @@ namespace reallife_gamemode.Server.Extensions { if(context == null) { - using (DatabaseContext dbContext = new DatabaseContext()) + using(context = new DatabaseContext()) { - return dbContext.Factions.Find(client.GetUser(context).Faction); + User u = client.GetUser(); + if (u == null) return null; + return context.Factions.FirstOrDefault(f => f.Id == u.FactionId); } } else { - return context.Factions.Find(client.GetUser(context).Faction); + User u = client.GetUser(); + if (u == null) return null; + return context.Factions.FirstOrDefault(f => f.Id == u.FactionId); } } } diff --git a/Server/Services/ChatService.cs b/Server/Services/ChatService.cs index 12e75864..272b91f9 100644 --- a/Server/Services/ChatService.cs +++ b/Server/Services/ChatService.cs @@ -1,8 +1,10 @@ using GTANetworkAPI; using reallife_gamemode.Model; using reallife_gamemode.Server.Entities; +using reallife_gamemode.Server.Extensions; using System; using System.Collections.Generic; +using System.Linq; using System.Text; /** @@ -27,12 +29,22 @@ namespace reallife_gamemode.Server.Services public static void BroadcastFaction(string message, List factions) { - using(var dbCon = new DatabaseContext()) + using (var dbCon = new DatabaseContext()) { - foreach(User u in dbCon.Users) + foreach (User u in dbCon.Users) { Client c = ClientService.GetClientByName(u.Name); - if (c != null && factions.Contains(u.Faction)) c.SendChatMessage(message); + if (c != null) + { + Faction f = c.GetFaction(); + if (f != null) + { + if (factions.Find(fT => fT.Id == f.Id) != null) + { + c.SendChatMessage(message); + } + } + } } } }