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
}
public enum WarState
{
Started, Ended, Created
}
[System.Serializable]
internal class Caller
{
public string CallUser { get; }
public string CallUser { get; set; }
public DateTime TimeAdded { get; set; }
@ -25,6 +29,7 @@ namespace NadekoBot.Classes.ClashOfClans
public int Stars { get; set; } = 3;
public Caller() { }
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);
public string EnemyClan { get; }
public int Size { get; }
public string EnemyClan { get; set; }
public int Size { get; set; }
public Caller[] Bases { get; }
public bool Started { get; set; } = false;
public Caller[] Bases { get; set; }
public WarState WarState { get; set; } = WarState.Created;
//public bool Started { get; set; } = false;
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 ChannelId { get; set; }
[JsonIgnore]
public Discord.Channel Channel { get; internal set; }
@ -74,12 +80,13 @@ namespace NadekoBot.Classes.ClashOfClans
this.Bases = new Caller[size];
this.ServerId = serverId;
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()
{
Ended = true;
//Ended = true;
WarState = WarState.Ended;
}
internal void Call(string u, int baseNumber)
@ -99,9 +106,12 @@ namespace NadekoBot.Classes.ClashOfClans
internal void Start()
{
if (Started)
throw new InvalidOperationException();
Started = true;
if (WarState == WarState.Started)
throw new InvalidOperationException("War already started");
//if (Started)
// throw new InvalidOperationException();
//Started = true;
WarState = WarState.Started;
StartedAt = DateTime.Now;
foreach (var b in Bases.Where(b => b != null))
{
@ -129,7 +139,7 @@ namespace NadekoBot.Classes.ClashOfClans
var sb = new StringBuilder();
sb.AppendLine($"🔰**WAR AGAINST `{EnemyClan}` ({Size} v {Size}) INFO:**");
if (!Started)
if (WarState == WarState.Created)
sb.AppendLine("`not started`");
for (var i = 0; i < Bases.Length; i++)
{
@ -145,7 +155,7 @@ namespace NadekoBot.Classes.ClashOfClans
}
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");
}
}

View File

@ -58,6 +58,7 @@ namespace NadekoBot.Modules.ClashOfClans
//Can't this be disabled if the modules is disabled too :)
var callExpire = new TimeSpan(2, 0, 0);
var warExpire = new TimeSpan(23, 0, 0);
bool changed = false;
while (true)
{
try
@ -72,21 +73,29 @@ namespace NadekoBot.Modules.ClashOfClans
List<ClashWar> newVal = new List<ClashWar>();
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);
}
}
}
//var newVal = cw.Value.Where(w => !(w.Ended || DateTime.Now - w.StartedAt >= warExpire)).ToList();
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;
ClashWars.TryRemove(cw.Key, out obj);
} else
{
ClashWars.AddOrUpdate(cw.Key, newVal, (x, s) => newVal);
}
}
if (hash != ClashWars.GetHashCode()) //something changed
@ -182,7 +191,7 @@ namespace NadekoBot.Modules.ClashOfClans
cgb.CreateCommand(Prefix + "startwar")
.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)
.Do(async e =>
{
@ -202,6 +211,7 @@ namespace NadekoBot.Modules.ClashOfClans
{
await e.Channel.SendMessage($"🔰**WAR AGAINST {war.ShortPrint()} HAS ALREADY STARTED**").ConfigureAwait(false);
}
Save();
});
cgb.CreateCommand(Prefix + "listwar")
@ -233,6 +243,7 @@ namespace NadekoBot.Modules.ClashOfClans
}
await e.Channel.SendMessage(sb.ToString()).ConfigureAwait(false);
return;
}
//if number is not null, print the war needed
var warsInfo = GetInfo(e);
@ -274,6 +285,7 @@ namespace NadekoBot.Modules.ClashOfClans
var war = warsInfo.Item1[warsInfo.Item2];
war.Call(usr, baseNum - 1);
await e.Channel.SendMessage($"🔰**{usr}** claimed a base #{baseNum} for a war against {war.ShortPrint()}").ConfigureAwait(false);
Save();
}
catch (Exception ex)
{
@ -327,6 +339,7 @@ namespace NadekoBot.Modules.ClashOfClans
var war = warsInfo.Item1[warsInfo.Item2];
var baseNumber = war.Uncall(usr);
await e.Channel.SendMessage($"🔰 @{usr} has **UNCLAIMED** a base #{baseNumber + 1} from a war against {war.ShortPrint()}").ConfigureAwait(false);
Save();
}
catch (Exception ex)
{
@ -351,6 +364,7 @@ namespace NadekoBot.Modules.ClashOfClans
var size = warsInfo.Item1[warsInfo.Item2].Size;
warsInfo.Item1.RemoveAt(warsInfo.Item2);
Save();
});
});
@ -376,6 +390,7 @@ namespace NadekoBot.Modules.ClashOfClans
{
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);
Save();
}
catch (Exception ex)
{