[+] Added Gangwar playability

This commit is contained in:
Lukas Moungos
2019-12-01 15:57:37 +01:00
parent ba58c97182
commit a810a5abf8
6 changed files with 278 additions and 51 deletions

View File

@@ -6,6 +6,8 @@ using System.Linq;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Database.Entities;
using Newtonsoft.Json;
using ReallifeGamemode.Server.Services;
using ReallifeGamemode.Server.Extensions;
namespace ReallifeGamemode.Server.Gangwar
{
@@ -40,12 +42,55 @@ namespace ReallifeGamemode.Server.Gangwar
NAPI.ClientEvent.TriggerClientEventForAll("GangAreas:Create", JsonConvert.SerializeObject(turfs.ToArray()));
}
public void GangwarKill(Client killer, Client victim)
{
if (killer.HasData("GotInsideOfTurf") && victim.HasData("GotInsideOfTurf"))
{
foreach (var turf in getTurfs())
{
if (turf.getId() == victim.GetData("inGangWar"))
{
turf.setKill(victim.GetUser().Faction.Name);
return;
}
}
}
}
public Turf[] getTurfs()
{
return _loadedTurfs;
}
[RemoteEvent("Gangarea:Enter")]
public void RmtEvent_TurfEnter(Client client, string jsonId)
{
int id = JsonConvert.DeserializeObject<int>(jsonId);
foreach (var turf in getTurfs())
{
if (turf.getId() == id)
{
turf.enter(client);
return;
}
}
}
[RemoteEvent("Gangarea:Leave")]
public void RmtEvent_TurfLeave(Client client, string jsonId)
{
int id = JsonConvert.DeserializeObject<int>(jsonId);
foreach (var turf in getTurfs())
{
if (turf.getId() == id)
{
turf.leave(client);
return;
}
}
}
[RemoteEvent("SERVER:SetTurf")]
public void RmtEvent_SetTurf(Client client, string jsonX, string jsonY, string jsonRot, string jsonRange, string Name)
@@ -65,13 +110,66 @@ namespace ReallifeGamemode.Server.Gangwar
Owner = "Neutral",
Color = 0
};
using(var dbContext = new DatabaseContext())
using (var dbContext = new DatabaseContext())
{
dbContext.Turfs.Add(newTurf);
dbContext.SaveChanges();
}
loadTurfs();
loadTurfs_ToAllClients()
loadTurfs_ToAllClients();
}
[RemoteEvent("SERVER:DeleteTurf")]
public void RmtEvent_DeleteTurf(Client client, string jsonId)
{
int id = JsonConvert.DeserializeObject<int>(jsonId);
if (id == -1)
{
ChatService.ErrorMessage(client, "Du befindest dich in keinem Gebiet");
return;
}
using (var dbContext = new DatabaseContext())
{
Turfs dturf = dbContext.Turfs.Where(t => t.Id == id).FirstOrDefault();
if (dturf != null)
{
dbContext.Turfs.Remove(dturf);
dbContext.SaveChanges();
loadTurfs();
loadTurfs_ToAllClients();
}
}
}
[Command("/gwstart")]
public void StartGangwar(Client client)
{
if (!client.GetUser().FactionLeader)
return;
foreach (var turf in getTurfs())
{
if (turf.status == "attack")
{
client.SendChatMessage("Ein GW läuft grad bruder"); // DEBUG
return;
}
}
if(client.GetUser().Faction.Name == "Ballas" || client.GetUser().Faction.Name == "Grove")
{
foreach (var turf in getTurfs())
{
foreach(var u in turf.playerInside)
{
if(u == client)
{
if (turf.Owner != client.GetUser().Faction.Name)
turf.attack(client.GetUser().Faction.Name);
}
}
}
}
}
}

View File

@@ -5,6 +5,10 @@ using GTANetworkAPI;
using System.Timers;
using System.Linq;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Server.Services;
using Newtonsoft.Json;
namespace ReallifeGamemode.Server.Gangwar
{
@@ -21,6 +25,7 @@ namespace ReallifeGamemode.Server.Gangwar
public string status { get; set; }
public Client[] playerInside { get; set; }
public Timer timer { get; set; }
public Client[] playerInGangwar { get; set; }
public Turf(int TurfID, string TurfName, int color, string Owner)
@@ -30,8 +35,8 @@ namespace ReallifeGamemode.Server.Gangwar
this.Color = color;
this.Owner = Owner;
this.Attacker = null;
this.Att_Score = 50;
this.Def_Score = 50;
this.Att_Score = 5;
this.Def_Score = 5;
this.status = "normal";
this.playerInside = null;
this.timer = null;
@@ -81,17 +86,17 @@ namespace ReallifeGamemode.Server.Gangwar
private void update()
{
Client[] owners = this.playerInside.Where(c => c.GetUser().Faction.Name == this.Owner ).ToArray();
Client[] attackers = this.playerInside.Where(c => c.GetUser().Faction.Name == this.Attacker).ToArray();
Client[] owners = this.playerInGangwar.Where(c => c.GetUser().Faction.Name == this.Owner ).ToArray();
Client[] attackers = this.playerInGangwar.Where(c => c.GetUser().Faction.Name == this.Attacker).ToArray();
/*
if (owners.Length > attackers.Length)
this.Att_Score -= owners.Length - attackers.Length;
if(owners.Length < attackers.Length)
this.Def_Score -= attackers.Length - owners.Length;
*/
foreach (Client playerInArea in this.playerInside)
foreach (Client gangwarPlayer in this.playerInGangwar)
{
playerInArea.TriggerEvent("CLIENT:Turf_Update", this.TurfID, this.status, this.Owner, this.Attacker, this.Color, this.Att_Score, this.Def_Score);
gangwarPlayer.TriggerEvent("CLIENT:ScoreUpdate", JsonConvert.SerializeObject(this.Att_Score), JsonConvert.SerializeObject(this.Def_Score), JsonConvert.SerializeObject(this.TurfID));
}
if(this.Def_Score <= 0)
{
@@ -104,11 +109,29 @@ namespace ReallifeGamemode.Server.Gangwar
public void enter(Client client)
{
if (this.status == "attack")
{
if (client.GetUser().Faction.Name != getOwner() && client.GetUser().Faction.Name != getAttacker())
return;
Client gClient = playerInGangwar.Where(c => c.GetUser().Id == client.GetUser().Id).FirstOrDefault();
if (gClient == null)
{
using (var dbContext = new DatabaseContext())
{
ChatService.BroadcastFaction("~y~[GANGWAR] ~r~" + client.Name + "~w~ ist nicht im Gangwar beteiligt !", dbContext.Factions.Where(f => f.Name == getOwner()).FirstOrDefault());
ChatService.BroadcastFaction("~y~[GANGWAR] ~r~" + client.Name + "~w~ ist nicht im Gangwar beteiligt !", dbContext.Factions.Where(f => f.Name == getAttacker()).FirstOrDefault());
}
return;
}
}
if(Array.IndexOf(this.playerInside, client) == -1) {
Client[] refObj = this.playerInside;
Array.Resize(ref refObj, this.playerInside.Length + 1);
this.playerInside = refObj;
this.playerInside[this.playerInside.GetUpperBound(0)] = client;
if (status == "attack")
client.SetData("GotInsideOfTurf", true);
}
}
@@ -124,11 +147,36 @@ namespace ReallifeGamemode.Server.Gangwar
{
this.timer.Stop();
this.timer = null;
using (var dbContext = new DatabaseContext())
{
if (getOwner() == FactionName)
{
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());
}
else if (getOwner() != FactionName)
{
ChatService.BroadcastFaction("~y~[GANGWAR] ~w~Deine Fraktion konnte das Gebiet " + getName() + " nicht verteidigen.", dbContext.Factions.Where(f => f.Name == getAttacker()).FirstOrDefault());
this.Owner = FactionName;
Turfs turf = dbContext.Turfs.Where(t => t.Id == getId()).FirstOrDefault();
turf.Owner = this.Owner;
turf.FactionId = dbContext.Factions.Where(f => f.Name == this.Owner).FirstOrDefault().Id;
dbContext.SaveChanges();
}
ChatService.BroadcastFaction("~y~[GANGWAR] ~w~Deine Fraktion hat erfolgreich das Gebiet " + getName() + " erobert.", dbContext.Factions.Where(f => f.Name == this.Owner).FirstOrDefault());
}
this.Attacker = null;
foreach(var c in playerInGangwar)
{
c.ResetData("inGangWar");
c.ResetData("GotInsideOfTurf");
}
this.playerInGangwar = null;
Gangwar.loadTurfs();
Gangwar.loadTurfs_ToAllClients();
}
public void attack(Client client)
public void attack(string attacker)
{
if(this.status == "normal")
{
@@ -137,15 +185,40 @@ namespace ReallifeGamemode.Server.Gangwar
this.timer.Stop();
this.timer = null;
}
this.Attacker = client.GetUser().Faction.Name;
this.Attacker = attacker;
this.status = "attack";
foreach(Client playerInArea in this.playerInside)
using (var context = new DatabaseContext())
{
playerInArea.TriggerEvent("CLIENT:Turf_Update", this.TurfID, this.status, this.Owner, this.Attacker, this.Color, this.Att_Score, this.Def_Score);
List<Client> clientsInGangwar = new List<Client>();
User[] usersInGangwar = context.Users.Where(c => c.Faction.Name == getOwner() || c.Faction.Name == getAttacker()).ToArray();
foreach(var u in usersInGangwar)
{
u.Client.SetData("inGangWar", getId());
clientsInGangwar.Add(u.Client);
}
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();
}
}
public void setKill(string FactionName)
{
if (getOwner() == FactionName)
{
Def_Score -= 1;
}
else if (getAttacker() == FactionName)
{
Att_Score -= 1;
}
foreach (Client gangwarPlayer in this.playerInGangwar)
{
gangwarPlayer.TriggerEvent("CLIENT:ScoreUpdate1", JsonConvert.SerializeObject(this.Att_Score), JsonConvert.SerializeObject(this.Def_Score), JsonConvert.SerializeObject(this.TurfID));
}
}
}
}