more work on clashofclans by Dragon

This commit is contained in:
Kwoth 2016-07-26 16:05:08 +02:00
parent c3130ba428
commit 135c00823d
2 changed files with 44 additions and 19 deletions

View File

@ -14,10 +14,14 @@ namespace NadekoBot.Classes.ClashOfClans
{ {
One, Two, Three One, Two, Three
} }
public enum WarState
{
Started, Ended, Created
}
[System.Serializable] [System.Serializable]
internal class Caller internal class Caller
{ {
public string CallUser { get; } public string CallUser { get; set; }
public DateTime TimeAdded { get; set; } public DateTime TimeAdded { get; set; }
@ -25,6 +29,7 @@ namespace NadekoBot.Classes.ClashOfClans
public int Stars { get; set; } = 3; public int Stars { get; set; } = 3;
public Caller() { }
public Caller(string callUser, DateTime timeAdded, bool baseDestroyed) public Caller(string callUser, DateTime timeAdded, bool baseDestroyed)
{ {
@ -48,17 +53,18 @@ namespace NadekoBot.Classes.ClashOfClans
{ {
private static TimeSpan callExpire => new TimeSpan(2, 0, 0); private static TimeSpan callExpire => new TimeSpan(2, 0, 0);
public string EnemyClan { get; } public string EnemyClan { get; set; }
public int Size { get; } public int Size { get; set; }
public Caller[] Bases { get; } public Caller[] Bases { get; set; }
public bool Started { get; set; } = false; public WarState WarState { get; set; } = WarState.Created;
//public bool Started { get; set; } = false;
public DateTime StartedAt { get; private set; } public DateTime StartedAt { get; private set; }
public bool Ended { get; private set; } = false; //public bool Ended { get; private set; } = false;
public ulong ServerId { get; set; } public ulong ServerId { get; set; }
public ulong ChannelId { get; set; } public ulong ChannelId { get; set; }
[JsonIgnore] [JsonIgnore]
public Discord.Channel Channel { get; internal set; } public Discord.Channel Channel { get; internal set; }
@ -74,12 +80,13 @@ namespace NadekoBot.Classes.ClashOfClans
this.Bases = new Caller[size]; this.Bases = new Caller[size];
this.ServerId = serverId; this.ServerId = serverId;
this.ChannelId = channelId; this.ChannelId = channelId;
this.Channel = NadekoBot.Client.Servers.FirstOrDefault(s => s.Id == serverId)?.TextChannels.FirstOrDefault(c => c.Id == channelId); this.Channel = NadekoBot.Client.Servers.FirstOrDefault(s=>s.Id == serverId)?.TextChannels.FirstOrDefault(c => c.Id == channelId);
} }
internal void End() internal void End()
{ {
Ended = true; //Ended = true;
WarState = WarState.Ended;
} }
internal void Call(string u, int baseNumber) internal void Call(string u, int baseNumber)
@ -99,9 +106,12 @@ namespace NadekoBot.Classes.ClashOfClans
internal void Start() internal void Start()
{ {
if (Started) if (WarState == WarState.Started)
throw new InvalidOperationException(); throw new InvalidOperationException("War already started");
Started = true; //if (Started)
// throw new InvalidOperationException();
//Started = true;
WarState = WarState.Started;
StartedAt = DateTime.Now; StartedAt = DateTime.Now;
foreach (var b in Bases.Where(b => b != null)) foreach (var b in Bases.Where(b => b != null))
{ {
@ -129,7 +139,7 @@ namespace NadekoBot.Classes.ClashOfClans
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.AppendLine($"🔰**WAR AGAINST `{EnemyClan}` ({Size} v {Size}) INFO:**"); sb.AppendLine($"🔰**WAR AGAINST `{EnemyClan}` ({Size} v {Size}) INFO:**");
if (!Started) if (WarState == WarState.Created)
sb.AppendLine("`not started`"); sb.AppendLine("`not started`");
for (var i = 0; i < Bases.Length; i++) for (var i = 0; i < Bases.Length; i++)
{ {
@ -145,7 +155,7 @@ namespace NadekoBot.Classes.ClashOfClans
} }
else else
{ {
var left = Started ? callExpire - (DateTime.Now - Bases[i].TimeAdded) : callExpire; var left =(WarState == WarState.Started) ? callExpire - (DateTime.Now - Bases[i].TimeAdded) : callExpire;
sb.AppendLine($"`{i + 1}.` ✅ `{Bases[i].CallUser}` {left.Hours}h {left.Minutes}m {left.Seconds}s left"); sb.AppendLine($"`{i + 1}.` ✅ `{Bases[i].CallUser}` {left.Hours}h {left.Minutes}m {left.Seconds}s left");
} }
} }

View File

@ -58,6 +58,7 @@ namespace NadekoBot.Modules.ClashOfClans
//Can't this be disabled if the modules is disabled too :) //Can't this be disabled if the modules is disabled too :)
var callExpire = new TimeSpan(2, 0, 0); var callExpire = new TimeSpan(2, 0, 0);
var warExpire = new TimeSpan(23, 0, 0); var warExpire = new TimeSpan(23, 0, 0);
bool changed = false;
while (true) while (true)
{ {
try try
@ -72,21 +73,29 @@ namespace NadekoBot.Modules.ClashOfClans
List<ClashWar> newVal = new List<ClashWar>(); List<ClashWar> newVal = new List<ClashWar>();
foreach (var w in cw.Value) foreach (var w in cw.Value)
{ {
if (!w.Ended && (DateTime.Now - w.StartedAt <= warExpire)) //We add when A: the war is not ended
if (w.WarState != WarState.Ended)
{ {
//and B: the war has not expired
if ((w.WarState == WarState.Started && DateTime.Now - w.StartedAt <= warExpire) || w.WarState == WarState.Created)
{
newVal.Add(w); newVal.Add(w);
}
} }
} }
//var newVal = cw.Value.Where(w => !(w.Ended || DateTime.Now - w.StartedAt >= warExpire)).ToList(); //var newVal = cw.Value.Where(w => !(w.Ended || DateTime.Now - w.StartedAt >= warExpire)).ToList();
foreach (var exWar in cw.Value.Except(newVal)) foreach (var exWar in cw.Value.Except(newVal))
{ {
await exWar.Channel.SendMessage($"War against {exWar.EnemyClan} ({exWar.Size}v{exWar.Size})has ended."); await exWar.Channel.SendMessage($"War against {exWar.EnemyClan} has ended");
} }
ClashWars.AddOrUpdate(cw.Key, newVal, (x, s) => newVal);
if (cw.Value.Count == 0) if (newVal.Count == 0)
{ {
List<ClashWar> obj; List<ClashWar> obj;
ClashWars.TryRemove(cw.Key, out obj); ClashWars.TryRemove(cw.Key, out obj);
} else
{
ClashWars.AddOrUpdate(cw.Key, newVal, (x, s) => newVal);
} }
} }
if (hash != ClashWars.GetHashCode()) //something changed if (hash != ClashWars.GetHashCode()) //something changed
@ -182,7 +191,7 @@ namespace NadekoBot.Modules.ClashOfClans
cgb.CreateCommand(Prefix + "startwar") cgb.CreateCommand(Prefix + "startwar")
.Alias(Prefix + "sw") .Alias(Prefix + "sw")
.Description($"Starts a war with a given number. | `{Prefix}sw 1`") .Description("Starts a war with a given number.")
.Parameter("number", ParameterType.Required) .Parameter("number", ParameterType.Required)
.Do(async e => .Do(async e =>
{ {
@ -202,6 +211,7 @@ namespace NadekoBot.Modules.ClashOfClans
{ {
await e.Channel.SendMessage($"🔰**WAR AGAINST {war.ShortPrint()} HAS ALREADY STARTED**").ConfigureAwait(false); await e.Channel.SendMessage($"🔰**WAR AGAINST {war.ShortPrint()} HAS ALREADY STARTED**").ConfigureAwait(false);
} }
Save();
}); });
cgb.CreateCommand(Prefix + "listwar") cgb.CreateCommand(Prefix + "listwar")
@ -233,6 +243,7 @@ namespace NadekoBot.Modules.ClashOfClans
} }
await e.Channel.SendMessage(sb.ToString()).ConfigureAwait(false); await e.Channel.SendMessage(sb.ToString()).ConfigureAwait(false);
return; return;
} }
//if number is not null, print the war needed //if number is not null, print the war needed
var warsInfo = GetInfo(e); var warsInfo = GetInfo(e);
@ -274,6 +285,7 @@ namespace NadekoBot.Modules.ClashOfClans
var war = warsInfo.Item1[warsInfo.Item2]; var war = warsInfo.Item1[warsInfo.Item2];
war.Call(usr, baseNum - 1); war.Call(usr, baseNum - 1);
await e.Channel.SendMessage($"🔰**{usr}** claimed a base #{baseNum} for a war against {war.ShortPrint()}").ConfigureAwait(false); await e.Channel.SendMessage($"🔰**{usr}** claimed a base #{baseNum} for a war against {war.ShortPrint()}").ConfigureAwait(false);
Save();
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -327,6 +339,7 @@ namespace NadekoBot.Modules.ClashOfClans
var war = warsInfo.Item1[warsInfo.Item2]; var war = warsInfo.Item1[warsInfo.Item2];
var baseNumber = war.Uncall(usr); var baseNumber = war.Uncall(usr);
await e.Channel.SendMessage($"🔰 @{usr} has **UNCLAIMED** a base #{baseNumber + 1} from a war against {war.ShortPrint()}").ConfigureAwait(false); await e.Channel.SendMessage($"🔰 @{usr} has **UNCLAIMED** a base #{baseNumber + 1} from a war against {war.ShortPrint()}").ConfigureAwait(false);
Save();
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -351,6 +364,7 @@ namespace NadekoBot.Modules.ClashOfClans
var size = warsInfo.Item1[warsInfo.Item2].Size; var size = warsInfo.Item1[warsInfo.Item2].Size;
warsInfo.Item1.RemoveAt(warsInfo.Item2); warsInfo.Item1.RemoveAt(warsInfo.Item2);
Save();
}); });
}); });
@ -376,6 +390,7 @@ namespace NadekoBot.Modules.ClashOfClans
{ {
var baseNum = war.FinishClaim(usr, stars); var baseNum = war.FinishClaim(usr, stars);
await e.Channel.SendMessage($"❗🔰{e.User.Mention} **DESTROYED** a base #{baseNum + 1} in a war against {war.ShortPrint()}").ConfigureAwait(false); await e.Channel.SendMessage($"❗🔰{e.User.Mention} **DESTROYED** a base #{baseNum + 1} in a war against {war.ShortPrint()}").ConfigureAwait(false);
Save();
} }
catch (Exception ex) catch (Exception ex)
{ {