Finished faction system, slightly changed client files
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using GTANetworkAPI;
|
||||
using Newtonsoft.Json;
|
||||
using reallife_gamemode.Model;
|
||||
using reallife_gamemode.Server.Entities;
|
||||
using reallife_gamemode.Server.Events;
|
||||
@@ -265,6 +267,48 @@ namespace reallife_gamemode.Server.Commands
|
||||
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();
|
||||
}
|
||||
@@ -292,5 +336,46 @@ namespace reallife_gamemode.Server.Commands
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ 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;
|
||||
@@ -29,7 +30,7 @@ namespace reallife_gamemode.Server.Commands
|
||||
return;
|
||||
}
|
||||
|
||||
string broadcastMessage = "!{02FCFF}** " + player.Name + ": " + message + " )) **";
|
||||
string broadcastMessage = "!{02FCFF}** " + player.GetUser().GetFactionRank().RankName + " " + player.Name + ": " + message + " )) **";
|
||||
ChatService.BroadcastFaction(broadcastMessage, f);
|
||||
}
|
||||
|
||||
@@ -43,7 +44,7 @@ namespace reallife_gamemode.Server.Commands
|
||||
return;
|
||||
}
|
||||
|
||||
string broadcastMessage = "!{33AA33}** " + player.Name + ": " + message + ", over **";
|
||||
string broadcastMessage = "!{33AA33}** " + player.GetUser().GetFactionRank().RankName + " " + player.Name + ": " + message + ", over **";
|
||||
ChatService.BroadcastFaction(broadcastMessage, f);
|
||||
}
|
||||
|
||||
@@ -57,30 +58,11 @@ namespace reallife_gamemode.Server.Commands
|
||||
return;
|
||||
}
|
||||
|
||||
string broadcastMessage = "!{CC3333}** " + player.Name + ": " + message + ", over **";
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
[Command("managefactionranks")]
|
||||
public void CmdFactionManageFactionRanks(Client player)
|
||||
{
|
||||
Entities.Faction f = player.GetFaction();
|
||||
if (f == null)
|
||||
{
|
||||
ChatService.NotAuthorized(player);
|
||||
return;
|
||||
}
|
||||
|
||||
using(var context = new DatabaseContext())
|
||||
{
|
||||
List<FactionRank> factionRanks = context.FactionRanks.ToList().FindAll(r => r.FactionId == f.Id).OrderByDescending(o => o.Order).ToList();
|
||||
string json = JsonConvert.SerializeObject(factionRanks, Formatting.None);
|
||||
player.TriggerEvent("manageFactionRanks", json);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,8 @@ namespace reallife_gamemode.Server.Entities
|
||||
|
||||
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; }
|
||||
@@ -52,7 +54,21 @@ namespace reallife_gamemode.Server.Entities
|
||||
{
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
return context.FactionRanks.FirstOrDefault(fR => fR.Id == FactionRankId);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
73
Server/Events/Faction.cs
Normal file
73
Server/Events/Faction.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Server/Util/FactionRankHelper.cs
Normal file
18
Server/Util/FactionRankHelper.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user