From 17cdc5bd60633b6a53b5c99b56f0e9a493ad0ac1 Mon Sep 17 00:00:00 2001 From: Siga Date: Tue, 24 Mar 2020 19:45:09 +0100 Subject: [PATCH] Add limitation to start Gangwar @Turf.cs --- ReallifeGamemode.Server/Gangwar/Turf.cs | 87 ++++++++++++++++--------- 1 file changed, 56 insertions(+), 31 deletions(-) diff --git a/ReallifeGamemode.Server/Gangwar/Turf.cs b/ReallifeGamemode.Server/Gangwar/Turf.cs index 9daa5798..67db83f5 100644 --- a/ReallifeGamemode.Server/Gangwar/Turf.cs +++ b/ReallifeGamemode.Server/Gangwar/Turf.cs @@ -9,12 +9,13 @@ using ReallifeGamemode.Database.Models; using ReallifeGamemode.Database.Entities; using ReallifeGamemode.Server.Services; using Newtonsoft.Json; +using Microsoft.EntityFrameworkCore; namespace ReallifeGamemode.Server.Gangwar { public class Turf { - + public int TurfID { get; set; } public string TurfName { get; set; } public int Color { get; set; } @@ -68,7 +69,7 @@ namespace ReallifeGamemode.Server.Gangwar { return this.Attacker; } - + private void TurfTick() { this.timer = new System.Timers.Timer(1000); @@ -81,7 +82,7 @@ namespace ReallifeGamemode.Server.Gangwar private void Tick(object sender, System.Timers.ElapsedEventArgs e) { - if(this.status == "attack") + if (this.status == "attack") { update(); } @@ -122,14 +123,15 @@ namespace ReallifeGamemode.Server.Gangwar if (this.Def_Score <= 0) { this.takeOver(this.Attacker); - } else if (this.Att_Score <= 0) + } + else if (this.Att_Score <= 0) { this.takeOver(this.Owner); } timerCount += 1; - if(timerCount >= 900) //change to 900 (seconds) before release for testing reasons change to whatever you like + if (timerCount >= 900) //change to 900 (seconds) before release for testing reasons change to whatever you like { - if(this.Def_Score > this.Att_Score) + if (this.Def_Score > this.Att_Score) { foreach (Player gangwarPlayer in this.playerInGangwar) { @@ -137,7 +139,8 @@ namespace ReallifeGamemode.Server.Gangwar } this.takeOver(this.Owner); this.Att_Score = 0; - }else if(this.Def_Score < this.Att_Score) + } + else if (this.Def_Score < this.Att_Score) { foreach (Player gangwarPlayer in this.playerInGangwar) { @@ -147,7 +150,7 @@ namespace ReallifeGamemode.Server.Gangwar this.Def_Score = 0; return; } - else if(this.Def_Score == this.Att_Score) + else if (this.Def_Score == this.Att_Score) { foreach (Player gangwarPlayer in this.playerInGangwar) { @@ -155,7 +158,7 @@ namespace ReallifeGamemode.Server.Gangwar } this.takeOver(this.Attacker); this.Def_Score = 0; - + return; } } @@ -178,8 +181,9 @@ namespace ReallifeGamemode.Server.Gangwar return; } } - - if(playerInside.Find(c => c == client) == null) { + + if (playerInside.Find(c => c == client) == null) + { playerInside.Add(client); client.SetData("GotInsideOfTurf", true); } @@ -187,10 +191,10 @@ namespace ReallifeGamemode.Server.Gangwar public void leave(Player client) { - if(playerInside.Find(c => c == client) != null) + if (playerInside.Find(c => c == client) != null) { this.playerInside = this.playerInside.Where(c => c != client).ToList(); - if(this.status != "attack") + if (this.status != "attack") client.ResetData("GotInsideOfTurf"); } } @@ -214,7 +218,7 @@ namespace ReallifeGamemode.Server.Gangwar a.TriggerEvent("CLIENT:loose"); } ChatService.BroadcastFaction("~y~[GANGWAR] ~w~Deine Fraktion hat erfolgreich das Gebiet ~g~" + getName() + "~w~ verteidigt.", dbContext.Factions.Where(f => f.Name == getOwner()).FirstOrDefault()); - ChatService.BroadcastFaction("~y~[GANGWAR] ~w~Deine Fraktion hat den Angrif auf das Gebiet ~r~" + getName() + "~w~ verloren.", dbContext.Factions.Where(f => f.Name == getAttacker()).FirstOrDefault()); + ChatService.BroadcastFaction("~y~[GANGWAR] ~w~Deine Fraktion hat den Angrif auf das Gebiet ~r~" + getName() + "~w~ verloren.", dbContext.Factions.Where(f => f.Name == getAttacker()).FirstOrDefault()); } else if (getOwner() != FactionName) { @@ -236,10 +240,10 @@ namespace ReallifeGamemode.Server.Gangwar turf.FactionId = dbContext.Factions.Where(f => f.Name == this.Owner).FirstOrDefault().Id; dbContext.SaveChanges(); } - + } this.Attacker = null; - foreach(var c in playerInGangwar) + foreach (var c in playerInGangwar) { c.TriggerEvent("CLIENT:setAttackBlip", false, TurfID); c.ResetData("inGangWar"); @@ -252,30 +256,51 @@ namespace ReallifeGamemode.Server.Gangwar public void attack(string attacker) { - if(this.status == "normal") + Player[] usersInGangwar; + using (var context = new DatabaseContext()) { - if(this.timer != null) + var onlinePlayers = NAPI.Pools.GetAllPlayers().Where(c => c.IsLoggedIn()).Select(c => c.Name); + + List ownersInGangwar = context.Users.Include(u => u.Faction).Where(u => onlinePlayers.Contains(u.Name) && u.Faction.Name == getOwner()).Select(u => u.Player).ToList(); + List attackersInGangwar = context.Users.Include(u => u.Faction).Where(u => onlinePlayers.Contains(u.Name) && u.Faction.Name == getAttacker()).Select(u => u.Player).ToList(); + + if (ownersInGangwar.Count < 5 && attackersInGangwar.Count < 5) + { + List leaders = context.Users.Include(u => u.Faction).Where(u => onlinePlayers.Contains(u.Name) && u.Faction.Name == getAttacker() && u.FactionLeader).Select(u => u.Player).ToList(); + foreach(var l in leaders) + { + ChatService.ErrorMessage(l, "Gangwarstart ist nicht möglich, da die beteiligten Fraktionen nicht genügend Mitglieder online haben."); + } + + return; + } + List inGangwar = new List(); + inGangwar.AddRange(ownersInGangwar); + inGangwar.AddRange(attackersInGangwar); + + usersInGangwar = inGangwar.ToArray(); + } + if (this.status == "normal") + { + if (this.timer != null) { this.timer.Stop(); this.timer = null; } this.Attacker = attacker; this.status = "attack"; - using (var context = new DatabaseContext()) - { - List clientsInGangwar = new List(); - Player[] usersInGangwar = NAPI.Pools.GetAllPlayers().Where(c => c.IsLoggedIn() && (c.GetUser().Faction.Name == getOwner() || c.GetUser().Faction.Name == getAttacker())).ToArray(); - foreach (var u in usersInGangwar) - { - u.TriggerEvent("CLIENT:setAttackBlip", true, TurfID); - u.TriggerEvent("GangwarScore", this.Attacker, this.Owner, this.Att_Score, this.Def_Score); - u.SetData("inGangWar", getId()); - ChatService.SendMessage(u, "~y~[GANGWAR]~w~ Die " + getAttacker() + " haben das Gebiet ~y~"+ TurfName +"~w~ der "+ getOwner() +" angegriffen."); - clientsInGangwar.Add(u); - } - playerInGangwar = clientsInGangwar.ToArray(); + List clientsInGangwar = new List(); + + foreach (var u in usersInGangwar) + { + u.TriggerEvent("CLIENT:setAttackBlip", true, TurfID); + u.TriggerEvent("GangwarScore", this.Attacker, this.Owner, this.Att_Score, this.Def_Score); + u.SetData("inGangWar", getId()); + ChatService.SendMessage(u, "~y~[GANGWAR]~w~ Die " + getAttacker() + " haben das Gebiet ~y~" + TurfName + "~w~ der " + getOwner() + " angegriffen."); + clientsInGangwar.Add(u); } + playerInGangwar = clientsInGangwar.ToArray(); NAPI.ClientEvent.TriggerClientEventForAll("CLIENT:Turf_Update", JsonConvert.SerializeObject(this.TurfID), JsonConvert.SerializeObject(this.status), JsonConvert.SerializeObject(this.Owner), JsonConvert.SerializeObject(this.Attacker)); this.TurfTick(); }