From 4631f4bb75218343eed2f7e8f05dcb7befad1ab7 Mon Sep 17 00:00:00 2001 From: hydrant Date: Sun, 23 Sep 2018 13:34:02 +0200 Subject: [PATCH] Add some Faction commands, Try EF error fix --- Main.cs | 6 ---- Server/Commands/Admin.cs | 54 ++++++++++++++++++++++++++++ Server/Commands/Faction.cs | 7 +++- Server/Extensions/ClientExtension.cs | 27 ++++++++++---- 4 files changed, 81 insertions(+), 13 deletions(-) diff --git a/Main.cs b/Main.cs index e0f4c9a4..8018ab39 100644 --- a/Main.cs +++ b/Main.cs @@ -43,11 +43,5 @@ namespace reallife_gamemode } } } - - [ServerEvent(Event.ResourceStop)] - public void OnResourceStop() - { - NAPI.Chat.SendChatMessageToAll("on stop"); - } } } \ No newline at end of file diff --git a/Server/Commands/Admin.cs b/Server/Commands/Admin.cs index 852fb227..34517d8c 100644 --- a/Server/Commands/Admin.cs +++ b/Server/Commands/Admin.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; using GTANetworkAPI; +using reallife_gamemode.Model; using reallife_gamemode.Server.Extensions; using reallife_gamemode.Server.Services; using reallife_gamemode.Server.Util; @@ -199,5 +201,57 @@ namespace reallife_gamemode.Server.Commands target.GiveWeapon(wHash, ammo); } + + [Command("factionlist", "~m~Benutzung: ~s~/factionlist")] + public void CmdAdminFactionlist(Client player) + { + if (!player.GetUser()?.IsAdmin(AdminLevel.ADMIN) ?? true) + { + ChatService.NotAuthorized(player); + return; + } + + using(var dbContext = new DatabaseContext()) + { + player.SendChatMessage("~m~__________ ~s~Fraktionen ~m~__________"); + foreach(Entities.Faction f in dbContext.Factions) + { + player.SendChatMessage(f.Id.ToString().PadRight(3) + " | " + f.Name); + } + } + } + + [Command("ainvite", "~m~Benutzung: ~s~/ainvite [Name] [Fraktion]")] + public void CmdAdminAinvite(Client player, string name, int faction) + { + if (!player.GetUser()?.IsAdmin(AdminLevel.ADMIN) ?? true) + { + ChatService.NotAuthorized(player); + return; + } + + Client target = ClientService.GetClientByName(name); + if (target == null) + { + ChatService.PlayerNotFound(player); + return; + } + + using (var dbContext = new DatabaseContext()) + { + Entities.Faction f = dbContext.Factions.FirstOrDefault(x => x.Id == faction); + if(f == null) + { + player.SendChatMessage("~r~[FEHLER]~s~ Diese Fraktion existiert nicht (Liste: ~m~/factionlist)."); + return; + } + + target.GetUser(dbContext).Faction = f; + 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."); + } + } } } diff --git a/Server/Commands/Faction.cs b/Server/Commands/Faction.cs index 1804baa5..f333dae7 100644 --- a/Server/Commands/Faction.cs +++ b/Server/Commands/Faction.cs @@ -20,10 +20,15 @@ namespace reallife_gamemode.Server.Commands public void CmdFactionF(Client player, string message) { User u = player.GetUser(); + if(u.Faction == null) + { + ChatService.NotAuthorized(player); + return; + } string broadcastMessage = "** " + player.Name + ": " + message + " )) **"; - ChatService.BroadcastFaction(broadcastMessage, player.GetFaction()); + ChatService.BroadcastFaction(broadcastMessage, u.Faction); } } } diff --git a/Server/Extensions/ClientExtension.cs b/Server/Extensions/ClientExtension.cs index 9029a9e0..24035737 100644 --- a/Server/Extensions/ClientExtension.cs +++ b/Server/Extensions/ClientExtension.cs @@ -1,4 +1,5 @@ using GTANetworkAPI; +using Microsoft.EntityFrameworkCore; using reallife_gamemode.Model; using reallife_gamemode.Server.Entities; using System; @@ -16,19 +17,33 @@ namespace reallife_gamemode.Server.Extensions { public static class ClientExtension { - public static User GetUser(this Client client) + public static User GetUser(this Client client, DatabaseContext context = null) { - using(DatabaseContext dbContext = new DatabaseContext()) + if(context == null) { - return dbContext.Users.FirstOrDefault(u => u.Name == client.Name); + using (DatabaseContext dbContext = new DatabaseContext()) + { + return dbContext.Users.FirstOrDefault(u => u.Name == client.Name); + } + } + else + { + return context.Users.FirstOrDefault(u => u.Name == client.Name); } } - public static Faction GetFaction(this Client client) + public static Faction GetFaction(this Client client, DatabaseContext context = null) { - using (DatabaseContext dbContext = new DatabaseContext()) + if(context == null) { - return dbContext.Factions.Find(client.GetUser().Faction); + using (DatabaseContext dbContext = new DatabaseContext()) + { + return dbContext.Factions.Find(client.GetUser(context).Faction); + } + } + else + { + return context.Factions.Find(client.GetUser(context).Faction); } } }