Finished faction system, slightly changed client files
This commit is contained in:
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user