225 lines
7.3 KiB
C#
225 lines
7.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
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
|
|
{
|
|
public class Turf
|
|
{
|
|
|
|
public int TurfID { get; set; }
|
|
public string TurfName { get; set; }
|
|
public int Color { get; set; }
|
|
public string Owner { get; set; }
|
|
public string Attacker { get; set; }
|
|
public int Att_Score { get; set; }
|
|
public int Def_Score { get; set; }
|
|
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)
|
|
{
|
|
this.TurfID = TurfID;
|
|
this.TurfName = TurfName;
|
|
this.Color = color;
|
|
this.Owner = Owner;
|
|
this.Attacker = null;
|
|
this.Att_Score = 5;
|
|
this.Def_Score = 5;
|
|
this.status = "normal";
|
|
this.playerInside = null;
|
|
this.timer = null;
|
|
}
|
|
|
|
public int getId()
|
|
{
|
|
return this.TurfID;
|
|
}
|
|
|
|
public string getName()
|
|
{
|
|
return this.TurfName;
|
|
}
|
|
|
|
public int getColor()
|
|
{
|
|
return this.Color;
|
|
}
|
|
|
|
public string getOwner()
|
|
{
|
|
return this.TurfName;
|
|
}
|
|
|
|
public string getAttacker()
|
|
{
|
|
return this.Attacker;
|
|
}
|
|
|
|
private void TurfTick()
|
|
{
|
|
this.timer = new System.Timers.Timer(1000);
|
|
|
|
this.timer.Elapsed += Tick;
|
|
this.timer.AutoReset = true;
|
|
this.timer.Enabled = true;
|
|
}
|
|
|
|
private void Tick(object sender, System.Timers.ElapsedEventArgs e)
|
|
{
|
|
if(this.status == "attack")
|
|
{
|
|
update();
|
|
}
|
|
}
|
|
|
|
private void update()
|
|
{
|
|
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 gangwarPlayer in this.playerInGangwar)
|
|
{
|
|
gangwarPlayer.TriggerEvent("CLIENT:ScoreUpdate", JsonConvert.SerializeObject(this.Att_Score), JsonConvert.SerializeObject(this.Def_Score), JsonConvert.SerializeObject(this.TurfID));
|
|
}
|
|
if(this.Def_Score <= 0)
|
|
{
|
|
this.takeOver(this.Attacker);
|
|
}else if(this.Att_Score <= 0)
|
|
{
|
|
this.takeOver(this.Owner);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
public void leave(Client client)
|
|
{
|
|
if(Array.IndexOf(this.playerInside, client) > -1)
|
|
{
|
|
this.playerInside = this.playerInside.Where(c => c != client).ToArray();
|
|
}
|
|
}
|
|
|
|
public void takeOver(string FactionName)
|
|
{
|
|
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(string attacker)
|
|
{
|
|
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<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));
|
|
}
|
|
}
|
|
}
|
|
}
|