Code:
using System.Collections.Generic;
using wServer.networking.svrPackets;
using wServer.realm.entities;
using wServer.realm.terrain;
using System.Linq;
using System;
using System.Threading.Tasks;
using System.Diagnostics;
namespace wServer.realm.worlds
{
public class Horde : World
{
public int blueScore = 0;
public int redScore = 0;
public int Time = 60;
public int BlueWins;
public int RedWins;
public List<string> blueNames = new List<string>();
public List<string> redNames = new List<string>();
public List<string> dyeList = new List<string>();
public bool started = false;
public bool spawned = false;
public bool ready = false;
public bool spawn = false;
public bool finished = false;
public bool complete = false;
public bool counting = false;
public bool rewarding = false;
public int round = 0;
public List<string> roundDamageDone = new List<string>();
private readonly string[] FirstEnemies =
{
"Lizard God", "Minotaur", "Ogre King", "Undead Dwarf God", "Flayer God",
};
private readonly string[] SecondEnemies =
{
"White Demon", "Sprite God", "Medusa", "Ent God", "Beholder", "Flying Brain", "Slime God", "Ghost God", "Djinn"
};
private readonly string[] ThirdEnemies =
{
"Limon the Sprite God", "Archdemon Malphas", "Septavius the Ghost God"
};
public bool hasEntitiesRemaining;
public Horde()
{
Id = HORDE_ID;
Name = "Horde";
Background = 0;
Difficulty = 5;
AllowTeleport = true;
SetMusic("JumpIntoBattle");
RayDungeon = true;
}
protected override void Init()
{
LoadMap("wServer.realm.worlds.dungeons.Horde.jm", MapType.Json);
}
public override void Tick(RealmTime time)
{
base.Tick(time);
if (this == null) return;
if (Players.Count <= 0 && started) //if 0 players and the matchs started, will remove world
{
Manager.RemoveWorld(this);
return;
}
CheckInSafeZone();
if (round == 5 || RedWins == 3 || BlueWins == 3)
complete = true;
if (complete && !rewarding)
{
if (this == null) return;
if (Players.Count <= 0) return;
var fameBase = 500;
rewarding = true;
foreach (var i in Players.Values)
{
i.Client.SendPacket(new HordeDataPacket()
{
BlueScore = blueScore,
RedScore = redScore,
Time = 60
});
var def = new List<IntPoint>();
for (var x = 0; x < Map.Width; x++)
for (var y = 0; y < Map.Height; y++)
if (Map[x, y].Region == TileRegion.DungArea_3)
def.Add(new IntPoint(x, y));
var tile = def[new Random().Next(0, def.Count)];
i.Owner.BroadcastPacket(new GotoPacket
{
ObjectId = i.Id,
Position = new Position
{
X = tile.X,
Y = tile.Y
}
}, null);
}
if (BlueWins > RedWins)
{
foreach (var i in Players.Values)
{
i.SendInfo("~~~~~~~~~~~~~~~~~~~~~");
i.SendInfo("Blue team wins the Game!");
i.SendInfo("Score:");
i.SendInfo("Blue : Red");
i.SendInfo(BlueWins + " : " + RedWins);
i.SendInfo("~~~~~~~~~~~~~~~~~~~~~");
i.SendInfo("The winners all recieve 500 fame!");
if (blueNames.Contains(i.Name))
{
i.Manager.Data.AddDatabaseOperation(db =>
{
var donorRank = i.Client.Account.DonatorRank;
var playerFame = fameBase * (donorRank == 6 ? 5 : (donorRank == 5 ? 3 : (1 + 0.25 * donorRank)));
i.CurrentFame = i.Client.Account.Stats.Fame = db.UpdateFame(i.Client.Account, (int)playerFame);
});
}
Task.Factory.StartNew(() =>
{
while (!worldTimer(5000)) { }
if (this == null) return;
if (Players.Count <= 0) return;
if (i == null) return;
i.Client.Reconnect(new ReconnectPacket
{
Host = "",
Port = 2050,
GameId = NEXUS_ID,
Name = "Nexus",
Key = Empty<byte>.Array,
});
});
}
}
else
{
foreach (var i in Players.Values)
{
i.SendInfo("~~~~~~~~~~~~~~~~~~~~~");
i.SendInfo("Red team wins the Game!");
i.SendInfo("Score:");
i.SendInfo("Blue : Red");
i.SendInfo(BlueWins + " : " + RedWins);
i.SendInfo("~~~~~~~~~~~~~~~~~~~~~");
i.SendInfo("The winners all recieve 500 fame!");
if (redNames.Contains(i.Name))
{
i.Manager.Data.AddDatabaseOperation(db =>
{
var donorRank = i.Client.Account.DonatorRank;
var playerFame = fameBase * (donorRank == 6 ? 5 : (donorRank == 5 ? 3 : (1 + 0.25 * donorRank)));
i.CurrentFame = i.Client.Account.Stats.Fame = db.UpdateFame(i.Client.Account, (int)playerFame);
});
}
Task.Factory.StartNew(() =>
{
while (!worldTimer(5000)) { }
if (this == null) return;
if (Players.Count <= 0) return;
if (i == null) return;
i.Client.Reconnect(new ReconnectPacket
{
Host = "",
Port = 2050,
GameId = NEXUS_ID,
Name = "Nexus",
Key = Empty<byte>.Array,
});
});
}
}
}
if ((Enemies.Count == 0 && spawned) || (Time == 0 && spawned))
{
if (this == null) return;
if (Players.Count <= 0) return;
spawned = false;
round++;
var listToKick = new List<Player>();
var def = new List<IntPoint>();
for (var x = 0; x < Map.Width; x++)
for (var y = 0; y < Map.Height; y++)
if (Map[x, y].Region == TileRegion.DungArea_3)
def.Add(new IntPoint(x, y));
var tile = def[new Random().Next(0, def.Count)];
foreach (var i in Players.Values)
{
var dmgExsts = false;
for (var j = 0; j < roundDamageDone.Count; j++)
{
if (roundDamageDone[j].Split(':')[0] == i.Name)
{
if (roundDamageDone[j].Split(':')[1] == "1")
dmgExsts = true;
roundDamageDone[j] = $"{i.Name}:0";
}
}
if (!dmgExsts && (Map[(int)i.X, (int)i.Y].Region != TileRegion.Spawn && Map[(int)i.X, (int)i.Y].Region != TileRegion.DungArea_3))
listToKick.Add(i);
i.Client.SendPacket(new HordeDataPacket()
{
BlueScore = 0,
RedScore = 0,
Time = 60
});
i.Owner.BroadcastPacket(new GotoPacket
{
ObjectId = i.Id,
Position = new Position
{
X = tile.X,
Y = tile.Y
}
}, null);
}
if (blueScore > redScore)
{
BlueWins++;
foreach (var i in Players.Values)
{
i.SendInfo("~~~~~~~~~~~~~~~~~~~~~");
i.SendInfo("Blue team wins that round!");
i.SendInfo("Current Score:");
i.SendInfo("Blue : Red");
i.SendInfo(BlueWins + " : " + RedWins);
i.SendInfo("~~~~~~~~~~~~~~~~~~~~~");
i.SendInfo("Next round in 5 seconds!");
}
}
else
{
RedWins++;
foreach (var i in Players.Values)
{
i.SendInfo("~~~~~~~~~~~~~~~~~~~~~");
i.SendInfo("Red team wins that round!");
i.SendInfo("Current Score:");
i.SendInfo("Blue : Red");
i.SendInfo(BlueWins + " : " + RedWins);
i.SendInfo("~~~~~~~~~~~~~~~~~~~~~");
i.SendInfo("Next round in 5 seconds!");
}
}
//Timers.Add(new WorldTimer(2500, (world, t) =>
Task.Factory.StartNew(() =>
{
while (!worldTimer(2500)) { }
if (this == null) return;
if (Players.Count <= 0) return;
foreach (var i in Players.Values)
{
if (i == null) return;
if (listToKick.Contains(i))
{
foreach (var j in Players.Values)
{
if (j != i)
{
j.SendInfo($"{i.Name} has been kicked from Horde due to insufficient damage!");
}
}
i.GoToNexus();
}
if (i.Boost == null) i.CalculateBoost();
i.HP = i.Stats[0] + i.Boost[0];
i.MP = i.Stats[1] + i.Boost[1];
i.Client.SendPacket(new HordeDataPacket()
{
BlueScore = 0,
RedScore = 0,
Time = 60
});
}
});
//Timers.Add(new WorldTimer(5000, (world, t) =>
Task.Factory.StartNew(() =>
{
while (!worldTimer(5000)) { }
if (this == null) return;
if (Players.Count <= 0) return;
ready = true;
blueScore = 0;
redScore = 0;
Time = 60;
});
}
if (spawned && !counting)
{
if (this == null) return;
if (Players.Count <= 0) return;
counting = true;
//Timers.Add(new WorldTimer(1000, (world, t) =>
Task.Factory.StartNew(() =>
{
while (!worldTimer(1000)) { }
if (this == null) return;
if (Players.Count <= 0) return;
Time--;
counting = false;
foreach (var i in Players.Values)
{
if (i == null) return;
i.Client.SendPacket(new HordeDataPacket()
{
BlueScore = blueScore,
RedScore = redScore,
Time = Time
});
}
});
}
if (!started && Players.Count > 1) //when 2 players join, game will be started
{
started = true;
if (this == null) return;
if (Players.Count <= 0) return;
//Timers.Add(new WorldTimer(15000, (world, t) =>
Task.Factory.StartNew(() =>
{
while (!worldTimer(15000)) { }
if (this == null) return;
if (Players.Count <= 0) return;
foreach (var i in Players.Values)
{
i.SendInfo("~~~~~~~~~~~~~~~~~~~~~");
i.SendInfo("Welcome to the Horde!");
i.SendInfo("~~~~~~~~~~~~~~~~~~~~~");
i.SendInfo("Aim : To get more kills than the other team.");
i.SendInfo("Last person to hit the mob before death gets the kill.");
i.SendInfo("Idlers will be kicked!");
i.SendInfo("Each Round lasts 60 seconds.");
i.SendInfo("This round will start in 10 seconds, good luck!");
}
ready = true;
});
}
if (ready)
{
if (this == null) return;
if (Players.Count <= 0) return;
ready = false;
//Timers.Add(new WorldTimer(10000, (world, t) =>
Task.Factory.StartNew(() =>
{
while (!worldTimer(10000)) { }
if (this == null) return;
if (Players.Count <= 0) return;
foreach (var i in Players.Values)
{
i.SendInfo("~~~~~~~~~~~~~~~~~~~~~");
i.SendInfo($"Round {round + 1} begins.");
i.SendInfo("~~~~~~~~~~~~~~~~~~~~~");
if (blueNames.Contains(i.Name))
{
var def = new List<IntPoint>();
for (var x = 0; x < Map.Width; x++)
for (var y = 0; y < Map.Height; y++)
if (Map[x, y].Region == TileRegion.DungArea_1)
def.Add(new IntPoint(x, y));
var tile = def[new Random().Next(0, def.Count)];
i.Owner.BroadcastPacket(new GotoPacket
{
ObjectId = i.Id,
Position = new Position
{
X = tile.X,
Y = tile.Y
}
}, null);
} else if (redNames.Contains(i.Name))
{
var def = new List<IntPoint>();
for (var x = 0; x < Map.Width; x++)
for (var y = 0; y < Map.Height; y++)
if (Map[x, y].Region == TileRegion.DungArea_2)
def.Add(new IntPoint(x, y));
var tile = def[new Random().Next(0, def.Count)];
i.Owner.BroadcastPacket(new GotoPacket
{
ObjectId = i.Id,
Position = new Position
{
X = tile.X,
Y = tile.Y
}
}, null);
}
}
spawn = true;
});
}
if (spawn)
{
if (this == null) return;
if (Players.Count <= 0) return;
spawn = false;
//Timers.Add(new WorldTimer(2000, (world, t) =>
Task.Factory.StartNew(() =>
{
while (!worldTimer(2000)) { }
if (this == null) return;
if (Players.Count <= 0) return;
startSpawn();
});
}
}
private void CheckInSafeZone()
{
foreach (var i in Enemies.Values)
{
var xloc = (int)i.X;
var yloc = (int)i.Y;
if (Map[xloc, yloc].Region != TileRegion.DungArea_4)
{
i.Move(i.SpawnPoint.X, i.SpawnPoint.Y);
}
}
}
private void startSpawn()
{
var enems = new List<string>();
var r = new Random();
if (round == 0)
{
for (int i = 0; i < r.Next(15) + 10; i++)
{
enems.Add(FirstEnemies[r.Next(0, FirstEnemies.Length)]);
}
for (int i = 0; i < r.Next(4); i++)
{
enems.Add(SecondEnemies[r.Next(0, SecondEnemies.Length)]);
}
}
if (round == 1)
{
for (int i = 0; i < r.Next(15) + 5; i++)
{
enems.Add(FirstEnemies[r.Next(0, FirstEnemies.Length)]);
}
for (int i = 0; i < r.Next(10) + 5; i++)
{
enems.Add(SecondEnemies[r.Next(0, SecondEnemies.Length)]);
}
}
if (round == 2)
{
for (int i = 0; i < r.Next(15) + 5; i++)
{
enems.Add(FirstEnemies[r.Next(0, FirstEnemies.Length)]);
}
for (int i = 0; i < r.Next(15) + 15; i++)
{
enems.Add(SecondEnemies[r.Next(0, SecondEnemies.Length)]);
}
}
if (round == 3)
{
for (int i = 0; i < r.Next(15) + 15; i++)
{
enems.Add(FirstEnemies[r.Next(0, FirstEnemies.Length)]);
}
for (int i = 0; i < r.Next(15) + 20; i++)
{
enems.Add(SecondEnemies[r.Next(0, SecondEnemies.Length)]);
}
}
if (round == 4)
{
for (int i = 0; i < r.Next(5) + 25; i++)
{
enems.Add(SecondEnemies[r.Next(0, SecondEnemies.Length)]);
}
for (int i = 0; i < r.Next(5) + 5; i++)
{
enems.Add(ThirdEnemies[r.Next(0, ThirdEnemies.Length)]);
}
}
for (var i = 0; i < enems.Count; i++)
{
if (this == null) break;
if (Players.Count <= 0) break;
var def = new List<IntPoint>();
for (var x = 0; x < Map.Width; x++)
for (var y = 0; y < Map.Height; y++)
if (Map[x, y].Region == TileRegion.DungArea_4)
def.Add(new IntPoint(x, y));
var tile = def[r.Next(0, def.Count)];
var id = Manager.GameData.IdToObjectType[enems[i]];
var enemy = Entity.Resolve(Manager, id);
enemy.Move(tile.X, tile.Y);
EnterWorld(enemy);
}
spawned = true;
}
public override int EnterWorld(Entity entity)
{
base.EnterWorld(entity);
if (entity is Player && entity.Owner is Horde)
{
var i = (entity as Player);
var exstsDmg = false;
for (var j = 0; j < roundDamageDone.Count; j++)
{
if (roundDamageDone[j].Split(':')[0] == i.Name)
{
exstsDmg = true;
}
}
if (!exstsDmg)
roundDamageDone.Add($"{i.Name}:0");
if (!blueNames.Contains(i.Client.Account.Name) && !redNames.Contains(i.Client.Account.Name))
{
var blueCount = blueNames.Count;
var redCount = redNames.Count;
if (blueCount > redCount)
{
redNames.Add(i.Name);
var oldTex1 = i.Texture1;
var oldTex2 = i.Texture2;
i.Manager.Data.AddDatabaseOperation(db =>
{
db.SaveOldTextures(i.Client.Account, i.Client.Character, oldTex1, oldTex2);
});
//dyeList.Add($"{i.Name}:{i.Texture1}:{i.Texture2}");
i.Texture1 = 0x01FF0000;
i.Texture2 = 0x01FF0000;
foreach (var k in Players.Values)
{
if (k == null) break;
k.SendInfo(i.Name + " has joined the Red Team");
}
} else if (redCount > blueCount)
{
blueNames.Add(i.Name);
var oldTex1 = i.Texture1;
var oldTex2 = i.Texture2;
i.Manager.Data.AddDatabaseOperation(db =>
{
db.SaveOldTextures(i.Client.Account, i.Client.Character, oldTex1, oldTex2);
});
//dyeList.Add($"{i.Name}:{i.Texture1}:{i.Texture2}");
i.Texture1 = 0x010000FF;
i.Texture2 = 0x010000FF;
foreach (var k in Players.Values)
{
if (k == null) break;
k.SendInfo(i.Name + " has joined the Blue Team");
}
} else
{
var rand = new Random();
if (rand.Next(100) >= 50)
{
redNames.Add(i.Name);
var oldTex1 = i.Texture1;
var oldTex2 = i.Texture2;
i.Manager.Data.AddDatabaseOperation(db =>
{
db.SaveOldTextures(i.Client.Account, i.Client.Character, oldTex1, oldTex2);
});
//dyeList.Add($"{i.Name}:{i.Texture1}:{i.Texture2}");
i.Texture1 = 0x01FF0000;
i.Texture2 = 0x01FF0000;
foreach (var k in Players.Values)
{
if (k == null) break;
k.SendInfo(i.Name + " has joined the Red Team");
}
}
else
{
blueNames.Add(i.Name);
var oldTex1 = i.Texture1;
var oldTex2 = i.Texture2;
i.Manager.Data.AddDatabaseOperation(db =>
{
db.SaveOldTextures(i.Client.Account, i.Client.Character, oldTex1, oldTex2);
});
//dyeList.Add(i.Name + ":" + i.Texture1 + ":" + i.Texture2);
i.Texture1 = 0x010000FF;
i.Texture2 = 0x010000FF;
foreach (var k in Players.Values)
{
if (k == null) break;
k.SendInfo(i.Name + " has joined the Blue Team");
}
}
}
}
}
return entity.Id;
}
private bool worldTimer(int time)
{
var timer = new Stopwatch();
timer.Start();
var timeElapsed = false;
while (!timeElapsed)
if (timer.ElapsedMilliseconds > time)
{
timer.Stop();
timeElapsed = true;
}
return true;
}
public override void LeaveWorld(Entity entity)
{
if (entity is Enemy && entity.Owner is Horde)
{
var enemy = (entity as Enemy);
if (enemy.DamageCounter.LastHitter == null) return;
if (enemy.DamageCounter == null) return;
if (blueNames.Contains(enemy.DamageCounter.LastHitter .Name))
{
if (ThirdEnemies.Contains(enemy.Name))
blueScore += 5;
else if (SecondEnemies.Contains(enemy.Name))
blueScore += 3;
else
blueScore++;
}
else if (redNames.Contains(enemy.DamageCounter.LastHitter. Name))
{
if (ThirdEnemies.Contains(enemy.Name))
redScore += 5;
else if (SecondEnemies.Contains(enemy.Name))
redScore += 3;
else
redScore++;
}
foreach (var i in Players.Values) {
i.Client.SendPacket(new HordeDataPacket()
{
BlueScore = blueScore,
RedScore = redScore,
Time = Time
});
var doneDamage = false;
foreach (var j in enemy.DamageCounter.GetPlayerData())
{
if (j.Item1.Name == i.Name)
doneDamage = true;
}
if (doneDamage)
{
for (var h = 0; h < roundDamageDone.Count; h++)
{
var hSplit = roundDamageDone[h].Split(':');
if (hSplit[0] == i.Name && hSplit[1] != "1")
{
roundDamageDone[h] = $"{i.Name}:1";
}
}
}
}
}
if (entity is Player && entity.Owner is Horde)
{
var i = (entity as Player);
if (blueNames.Contains(i.Client.Account.Name))
{
blueNames.Remove(i.Client.Account.Name);
foreach (var k in Players.Values)
{
if (k == null) break;
k.SendInfo(i.Name + " has left the Blue Team");
}
}
if (redNames.Contains(i.Client.Account.Name))
{
redNames.Remove(i.Client.Account.Name);
foreach (var k in Players.Values)
{
if (k == null) break;
k.SendInfo(i.Name + " has left the Red Team");
}
}
//i.Manager.Data.DoActionAsync(db =>
//{
// var textures = db.GetOldTextures(i.Client.Account, i.Client.Character);
// if (textures[0] != -1)
// i.Texture1 = textures[0];
// if (textures[1] != -1)
// i.Texture2 = textures[1];
// i.SaveToCharacter();
//});
//i.UpdateCount++;
}
base.LeaveWorld(entity);
}
}
}