Merge branch 'feature/faction-system' into develop

This commit is contained in:
Lennart Kampshoff
2018-09-28 17:52:09 +02:00
24 changed files with 720 additions and 10 deletions

View File

@@ -4,12 +4,15 @@ using System.ComponentModel.DataAnnotations.Schema;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Linq;
using GTANetworkAPI;
using Newtonsoft.Json;
using reallife_gamemode.Model;
using reallife_gamemode.Server.Entities;
using reallife_gamemode.Server.Events;
using reallife_gamemode.Server.Extensions;
using reallife_gamemode.Server.Services;
using reallife_gamemode.Server.Util;
using reallife_gamemode.Model;
/**
* @overview Life of German Reallife - Admin Commands (Admin.cs)
@@ -529,6 +532,116 @@ namespace reallife_gamemode.Server.Commands
player.SendChatMessage("~b~Du hast " + peopleInRange.Count + " Spielern eine " + weapon + " mit " + munition + " Munition gegeben");
}
[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 && faction != 0)
{
player.SendChatMessage("~r~[FEHLER]~s~ Diese Fraktion existiert nicht (Liste: ~m~/factionlist).");
return;
}
User u = dbContext.Users.SingleOrDefault(x => x.Name == target.Name);
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.");
}
u.FactionLeader = false;
dbContext.SaveChanges();
}
}
[Command("makeleader", "~m~Benutzung: ~s~/makeleader [Name] [Fraktion]")]
public void CmdAdminMakeleader(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;
}
User u = dbContext.Users.SingleOrDefault(x => x.Name == target.Name);
u.FactionId = f.Id;
u.FactionRankId = dbContext.FactionRanks.
OrderByDescending(x => x.Order)
.FirstOrDefault(r => r.FactionId == f.Id)?.Id ?? null;
u.FactionLeader = true;
player.SendChatMessage("~b~[ADMIN]~s~ Du hast hast den Spieler ~y~" + target.Name + "~s~ zum Leader der Fraktion Fraktion ~o~" + f.Name + "~s~ ernannt.");
target.SendChatMessage("~b~[ADMIN]~s~ Du wurdest von ~y~" + player.Name + "~s~ zum Leader der Fraktion ~o~" + f.Name + "~s~ ernannt.");
dbContext.SaveChanges();
}
}
[Command("save", "~m~Benutzung: ~s~/save [Typ = ~g~Blip, ~r~Marker, Ped, Pickup, TextLabel, ~g~Vehicle")]
public void CmdAdminSave(Client player, string typ)
{
@@ -575,6 +688,7 @@ namespace reallife_gamemode.Server.Commands
Client target = ClientService.GetClientByName(user);
ClientExtension.BanPlayer(admin, target, reason, mins);
}
[Command("unban", "~m~Benutzung: ~s~/unban [User] ", GreedyArg = true)]
public void CmdAdminUnban(Client admin, string user)
{
@@ -592,6 +706,47 @@ namespace reallife_gamemode.Server.Commands
ClientExtension.UnbanPlayer(admin, target);
}
[Command("managefactionranks", "~m~Benutzung: ~s~/managefactionranks [Fraktions-ID]")]
public void CmdFactionManageFactionRanks(Client player, int factionID)
{
if(!player.GetUser()?.IsAdmin(AdminLevel.ADMIN) ?? true)
{
ChatService.NotAuthorized(player);
return;
}
using (var context = new DatabaseContext())
{
Entities.Faction f = context.Factions.FirstOrDefault(id => id.Id == factionID);
if (f == null)
{
player.SendChatMessage("~r~[FEHLER]~s~ Diese Fraktion existiert nicht (Liste: ~m~/factionlist).");
return;
}
List<FactionRank> factionRanks = context.FactionRanks.ToList().FindAll(r => r.FactionId == f.Id).OrderByDescending(o => o.Order).ToList();
List<Rank> rankList = new List<Rank>();
factionRanks.ForEach(r =>
{
rankList.Add(new Rank
{
Id = r.Id,
Name = r.RankName
});
});
FactionRankHelper helper = new FactionRankHelper
{
FactionId = f.Id,
Ranks = rankList
};
string json = JsonConvert.SerializeObject(helper, Formatting.None);
Console.WriteLine(json);
player.TriggerEvent("manageFactionRanks", json);
}
}
//TEST COMMAND
[Command("own")]

View File

@@ -0,0 +1,68 @@
using GTANetworkAPI;
using Newtonsoft.Json;
using reallife_gamemode.Model;
using reallife_gamemode.Server.Entities;
using reallife_gamemode.Server.Extensions;
using reallife_gamemode.Server.Services;
using reallife_gamemode.Server.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/**
* @overview Life of German Reallife - Faction Commands (Faction.cs)
* @author VegaZ, hydrant
* @copyright (c) 2008 - 2018 Life of German
*/
namespace reallife_gamemode.Server.Commands
{
class Faction : Script
{
[Command("f", "~m~Benutzung: ~s~/f [Nachricht]", GreedyArg = true)]
public void CmdFactionF(Client player, string message)
{
Entities.Faction f = player.GetFaction();
if(f == null || f.StateOwned)
{
ChatService.NotAuthorized(player);
return;
}
string broadcastMessage = "!{02FCFF}** " + player.GetUser().GetFactionRank().RankName + " " + player.Name + ": " + message + " )) **";
ChatService.BroadcastFaction(broadcastMessage, f);
}
[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.GetUser().GetFactionRank().RankName + " " + 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.GetUser().GetFactionRank().RankName + " " + player.Name + ": " + message + ", over **";
using(var context = new DatabaseContext())
{
ChatService.BroadcastFaction(broadcastMessage, context.Factions.ToList().FindAll(c => c.StateOwned));
}
}
}
}

View File

@@ -0,0 +1,26 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
/**
* @overview Life of German Reallife - Entities Faction (Faction.cs)
* @author VegaZ, hydrant
* @copyright (c) 2008 - 2018 Life of German
*/
namespace reallife_gamemode.Server.Entities
{
public class Faction
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[StringLength(32)]
public string Name { get; set; }
public int BankAccount { get; set; }
public bool StateOwned { get; set; }
}
}

View File

@@ -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);
}
}
}
}

View File

@@ -1,9 +1,11 @@
using GTANetworkAPI;
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;
/**
@@ -33,6 +35,7 @@ namespace reallife_gamemode.Server.Entities
[StringLength(64)]
public string Email { get; set; }
public AdminLevel AdminLevel { get; set; }
public float PositionX { get; set; }
public float PositionY { get; set; }
public float PositionZ { get; set; }
@@ -41,6 +44,45 @@ namespace reallife_gamemode.Server.Entities
public int? BanId { get; set; }
public Ban Ban { get; set; }
public int? FactionId { get; set; }
public Faction Faction { get; set; }
public bool FactionLeader { get; set; }
public int? FactionRankId { get; set; }
public FactionRank FactionRank { get;set; }
public Faction GetFaction()
{
using(var context = new DatabaseContext())
{
return context.Factions.FirstOrDefault(f => f.Id == FactionId);
}
}
public FactionRank GetFactionRank()
{
using (var context = new DatabaseContext())
{
FactionRank toReturn = context.FactionRanks.FirstOrDefault(fR => fR.Id == FactionRankId);
if(toReturn == null)
{
toReturn = context.FactionRanks.OrderBy(f => f.Order).FirstOrDefault(f => f.FactionId == FactionId);
}
if(toReturn == null)
{
toReturn = new FactionRank
{
RankName = "Rang-Fehler"
};
}
return toReturn;
}
}
public bool IsAdmin(AdminLevel level) => AdminLevel >= level;
}
}

73
Server/Events/Faction.cs Normal file
View File

@@ -0,0 +1,73 @@
using GTANetworkAPI;
using Newtonsoft.Json;
using reallife_gamemode.Model;
using reallife_gamemode.Server.Entities;
using reallife_gamemode.Server.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace reallife_gamemode.Server.Events
{
class Faction : Script
{
[RemoteEvent("OnFactionRanksEdit")]
public void OnFactionRanksEdit(Client player, string jsonData)
{
Console.WriteLine(jsonData);
FactionRankHelper helper = JsonConvert.DeserializeObject<FactionRankHelper>(jsonData);
using(var context = new DatabaseContext())
{
Entities.Faction f = context.Factions.FirstOrDefault(x => x.Id == helper.FactionId);
if (f == null)
{
player.SendChatMessage("~r~[FEHLER]~s~ Bei der Bearbeitung der Ränge ist ein Fehler aufgetreten: Die Fraktion existiert nicht.");
return;
}
List<Rank> ranks = helper.Ranks;
int length = ranks.Count;
List<FactionRank> factionRanks = context.FactionRanks.ToList().FindAll(fR => fR.FactionId == f.Id);
List<int> found = new List<int>();
for(int i = 0; i < ranks.Count; i++)
{
Rank newRank = ranks[i];
if(newRank.Id == 0)
{
Console.WriteLine("Neuer Rang: " + newRank.Name);
context.FactionRanks.Add(new FactionRank
{
RankName = newRank.Name,
FactionId = f.Id,
Order = length - i
});
}
else
{
FactionRank factionRank = factionRanks.Find(r => r.Id == newRank.Id);
Console.WriteLine($"Edited Rang: {factionRank.RankName} -> {newRank.Name}");
factionRank.RankName = newRank.Name;
factionRank.Order = length - i;
found.Add(factionRank.Id);
}
}
for(int i = 0; i < factionRanks.Count; i++)
{
if(!found.Contains(factionRanks[i].Id))
{
Console.WriteLine("removed: " + factionRanks[i].RankName);
context.FactionRanks.Remove(factionRanks[i]);
}
}
player.SendChatMessage("~g~Die Ränge wurden erfolgreich gespeichert.");
context.SaveChanges();
}
}
}
}

View File

@@ -1,4 +1,5 @@
using GTANetworkAPI;
using Microsoft.EntityFrameworkCore;
using reallife_gamemode.Model;
using reallife_gamemode.Server.Entities;
using System;
@@ -8,7 +9,7 @@ using System.Text;
/**
* @overview Life of German Reallife - Client Extension (ClientExtension.cs)
* @author VegaZ, hydrant
* @author hydrant
* @copyright (c) 2008 - 2018 Life of German
*/
@@ -16,11 +17,41 @@ 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)
{
<<<<<<< HEAD
using (DatabaseContext dbContext = new DatabaseContext())
=======
if(context == null)
>>>>>>> feature/faction-system
{
return dbContext.Users.FirstOrDefault(u => u.Name == client.Name);
using (context = new DatabaseContext())
{
return context.Users.FirstOrDefault(u => u.Name == client.Name);
}
}
else
{
return context.Users.FirstOrDefault(u => u.Name == client.Name);
}
}
public static Faction GetFaction(this Client client, DatabaseContext context = null)
{
if(context == null)
{
using(context = new DatabaseContext())
{
User u = client.GetUser();
if (u == null) return null;
return u.GetFaction();
}
}
else
{
User u = client.GetUser();
if (u == null) return null;
return u.GetFaction();
}
}

View File

@@ -1,6 +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;
/**
@@ -18,11 +22,12 @@ namespace reallife_gamemode.Server.Services
player.SendChatMessage("~r~[FEHLER]~s~ Du kannst diesen Befehl nicht ausführen.");
}
internal static void PlayerNotFound(Client player)
public static void PlayerNotFound(Client player)
{
player.SendChatMessage("~r~[FEHLER]~s~ Der Spieler wurde nicht gefunden.");
}
<<<<<<< HEAD
public static void PlayerNotLoggedIn(Client player)
{
player.SendChatMessage("~r~[FEHLER]~s~ Du bist nicht eingeloggt.");
@@ -30,6 +35,29 @@ namespace reallife_gamemode.Server.Services
public static void ErrorMsg(Client player)
{
player.SendChatMessage("~r~[FEHLER]~s~ Die Aktion wurde nicht ausgeführt.");
=======
public static void BroadcastFaction(string message, List<Faction> factions)
{
using (var dbCon = new DatabaseContext())
{
foreach (Client c in NAPI.Pools.GetAllPlayers())
{
Faction f = c.GetFaction();
if (f != null)
{
if (factions.Find(fT => fT.Id == f.Id) != null)
{
c.SendChatMessage(message);
}
}
}
}
}
public static void BroadcastFaction(string message, Faction faction)
{
BroadcastFaction(message, new List<Faction> { faction });
>>>>>>> feature/faction-system
}
}
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace reallife_gamemode.Server.Util
{
class FactionRankHelper
{
public int FactionId { get; set; }
public List<Rank> Ranks { get; set; }
}
class Rank
{
public int Id { get; set; }
public string Name { get; set; }
}
}