Files
reallife-gamemode/ReallifeGamemode.Server/Gangwar/Turf.cs
2019-11-30 18:54:11 +01:00

152 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using GTANetworkAPI;
using System.Timers;
using System.Linq;
using ReallifeGamemode.Server.Extensions;
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 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 = 50;
this.Def_Score = 50;
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.playerInside.Where(c => c.GetUser().Faction.Name == this.Owner ).ToArray();
Client[] attackers = this.playerInside.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)
{
playerInArea.TriggerEvent("CLIENT:Turf_Update", this.TurfID, this.status, this.Owner, this.Attacker, this.Color, this.Att_Score, this.Def_Score);
}
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(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;
}
}
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;
}
public void attack(Client client)
{
if(this.status == "normal")
{
if(this.timer != null)
{
this.timer.Stop();
this.timer = null;
}
this.Attacker = client.GetUser().Faction.Name;
this.status = "attack";
foreach(Client playerInArea in this.playerInside)
{
playerInArea.TriggerEvent("CLIENT:Turf_Update", this.TurfID, this.status, this.Owner, this.Attacker, this.Color, this.Att_Score, this.Def_Score);
}
this.TurfTick();
}
}
}
}