Merge branch 'feature/admin-commands' into develop

Add some basic Admin Commands
Add some Services for preventing redundant code
Add Method to get Account from Client
Add 'Admin-Level' Column to User table
This commit is contained in:
hydrant
2018-09-19 12:51:56 +02:00
8 changed files with 227 additions and 5 deletions

View File

@@ -5,9 +5,16 @@ namespace reallife_gamemode
{
public class Main : Script
{
/**
* @overview Life of German Reallife - Main Class (Main.cs)
* @author VegaZ, hydrant
* @copyright (c) 2008 - 2018 Life of German
*/
[ServerEvent(Event.ResourceStart)]
public void OnResourceStart()
{
NAPI.Server.SetCommandErrorMessage("~r~[FEHLER]~s~ Dieser Command existiert nicht.");
NAPI.Util.ConsoleOutput("reallife-gamemode resource loaded!");
}
}

View File

@@ -2,15 +2,108 @@
using System.Collections.Generic;
using System.Text;
using GTANetworkAPI;
using reallife_gamemode.Server.Extensions;
using reallife_gamemode.Server.Services;
using reallife_gamemode.Server.Util;
namespace reallife_gamemode.Server.Command
namespace reallife_gamemode.Server.Commands
{
public class Admin : Script
{
[Command("o")]
public void sendOChat(Client player, string message)
/**
* @overview Life of German Reallife - Admin Commands (Admin.cs)
* @author VegaZ, hydrant
* @copyright (c) 2008 - 2018 Life of German
*/
[Command("o", "~m~Benutzung: ~s~/o [Nachricht]", GreedyArg = true)]
public void CmdAdminO(Client player, string message)
{
NAPI.Chat.SendChatMessageToAll("~r~" + player.Name + " sagt:~w~ " + message);
if (!player.GetUser()?.IsAdmin(AdminLevel.SUPPORTER) ?? true)
{
ChatService.NotAuthorized(player);
return;
}
string publicMessage = "~b~(( " + player.Name + ": " + message + " ))";
NAPI.Chat.SendChatMessageToAll(publicMessage);
}
[Command("veh", "~m~Benutzung:~s~ /veh [Fahrzeug] (Farbe 1) (Farbe 2)")]
public void CmdAdminVeh(Client player, VehicleHash hash, int color1 = 111, int color2 = 111)
{
if (!player.GetUser()?.IsAdmin(AdminLevel.ADMIN) ?? true)
{
ChatService.NotAuthorized(player);
return;
}
if (player.IsInVehicle) // Man darf keine Autos erstellen, wenn man selbst in einem sitzt (verhindert Bugs)
{
player.SendChatMessage("~r~[FEHLER]~s~ Du sitzt momentan schon in einem Fahrzeug.");
return;
}
Vehicle v = NAPI.Vehicle.CreateVehicle(hash, player.Position, player.Rotation.Z, color1, color2);
player.SetIntoVehicle(v.Handle, -1);
}
[Command("fixveh")]
public void CmdAdminFixveh(Client player)
{
if (!player.GetUser()?.IsAdmin(AdminLevel.ADMIN) ?? true)
{
ChatService.NotAuthorized(player);
return;
}
if (!player.IsInVehicle) // Man darf keine Autos erstellen, wenn man selbst in einem sitzt (verhindert Bugs)
{
player.SendChatMessage("~r~[FEHLER]~s~ Du sitzt momentan nicht in einem Fahrzeug.");
return;
}
player.Vehicle.Repair();
}
[Command("delveh")]
public void CmdAdminDelveh(Client player)
{
if (!player.GetUser()?.IsAdmin(AdminLevel.ADMIN) ?? true)
{
ChatService.NotAuthorized(player);
return;
}
if (!player.IsInVehicle) // Man darf keine Autos erstellen, wenn man selbst in einem sitzt (verhindert Bugs)
{
player.SendChatMessage("~r~[FEHLER]~s~ Du sitzt momentan nicht in einem Fahrzeug.");
return;
}
/* TODO: Checken, ob das Fahrzeug in einem System genutzt wird (Fraktions-Autos / Spieler-Auto)
* Wenn Ja: Abbrechen und mitteilen, dass man den System-spezifischen Befehl zu nutzen hat */
player.Vehicle.Delete();
}
[Command("goto", "~m~Benutzung:~s~ /goto [Name]")]
public void CmdAdminGoto(Client player, string name)
{
if (!player.GetUser()?.IsAdmin(AdminLevel.SUPPORTER) ?? true)
{
ChatService.NotAuthorized(player);
return;
}
Client target = ClientService.GetClientByName(name);
if(target == null)
{
ChatService.PlayerNotFound(player);
return;
}
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using reallife_gamemode.Server.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
@@ -22,5 +23,8 @@ namespace reallife_gamemode.Server.Entities
[EmailAddress]
[StringLength(64)]
public string Email { get; set; }
public AdminLevel AdminLevel { get; set; }
public bool IsAdmin(AdminLevel level) => AdminLevel >= level;
}
}

View File

@@ -0,0 +1,27 @@
using GTANetworkAPI;
using reallife_gamemode.Model;
using reallife_gamemode.Server.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace reallife_gamemode.Server.Extensions
{
public static class ClientExtension
{
/**
* @overview Life of German Reallife - Client Extension (ClientExtension.cs)
* @author VegaZ, hydrant
* @copyright (c) 2008 - 2018 Life of German
*/
public static User GetUser(this Client client)
{
using(DatabaseContext dbContext = new DatabaseContext())
{
return dbContext.Users.FirstOrDefault(u => u.Name == client.Name);
}
}
}
}

10
Server/Managers/.gitkeep Normal file
View File

@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace reallife_gamemode.Server.Managers
{
class _
{
}
}

View File

@@ -0,0 +1,26 @@
using GTANetworkAPI;
using System;
using System.Collections.Generic;
using System.Text;
namespace reallife_gamemode.Server.Services
{
class ChatService
{
/**
* @overview Life of German Reallife - Chat Service (ChatService.cs)
* @author hydrant
* @copyright (c) 2008 - 2018 Life of German
*/
public static void NotAuthorized(Client player)
{
player.SendChatMessage("~r~[FEHLER]~s~ Du kannst diesen Befehl nicht ausführen.");
}
internal static void PlayerNotFound(Client player)
{
player.SendChatMessage("~r~[FEHLER]~s~ Der Spieler wurde nicht gefunden.");
}
}
}

View File

@@ -0,0 +1,36 @@
using GTANetworkAPI;
using System;
using System.Collections.Generic;
using System.Text;
namespace reallife_gamemode.Server.Services
{
class ClientService
{
/**
* @overview Life of German Reallife - Client Service (ClientService.cs)
* @author hydrant
* @copyright (c) 2008 - 2018 Life of German
*/
public static Client GetClientByName(string name)
{
/* Funktionsaufbau: Prüft erst, ob ein Spieler mit exakt diesem Namen online ist
* Wenn Ja: Wird dieser zurückgegeben
* Wenn Nein: Wird der erste Spieler zurückgegeben, dessen Namen mit dem eingegebenen Parameter übereinstimmt
* Gibt "null" zurück, falls kein Client gefunden wurde
*/
Client toReturn = null;
List<Client> playerList = NAPI.Pools.GetAllPlayers();
toReturn = playerList.Find(p => p.Name == name);
if(toReturn == null)
{
toReturn = playerList.Find(p => p.Name.StartsWith(name));
}
return toReturn;
}
}
}

19
Server/Util/AdminLevel.cs Normal file
View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace reallife_gamemode.Server.Util
{
public enum AdminLevel : int
{
/**
* @overview Life of German Reallife - Admin Levels (AdminLevel.cs)
* @author VegaZ, hydrant
* @copyright (c) 2008 - 2018 Life of German
*/
PLAYER,
SUPPORTER,
ADMIN
}
}