Beispiel #1
0
        private static async Task CheckWar(TimeSpan callExpire, ClashWar war)
        {
            var Bases = war.Bases;

            for (var i = 0; i < Bases.Count; i++)
            {
                var callUser = Bases[i].CallUser;
                if (callUser == null)
                {
                    continue;
                }
                if ((!Bases[i].BaseDestroyed) && DateTime.UtcNow - Bases[i].TimeAdded >= callExpire)
                {
                    if (Bases[i].Stars != 3)
                    {
                        Bases[i].BaseDestroyed = true;
                    }
                    else
                    {
                        Bases[i] = null;
                    }
                    try
                    {
                        SaveWar(war);
                        await war.Channel.SendErrorAsync($"❗🔰**Claim from @{Bases[i].CallUser} for a war against {war.ShortPrint()} has expired.**").ConfigureAwait(false);
                    }
                    catch { }
                }
            }
        }
Beispiel #2
0
        public static string ToPrettyString(this ClashWar cw)
        {
            var sb = new StringBuilder();

            sb.AppendLine($"🔰**WAR AGAINST `{cw.EnemyClan}` ({cw.Size} v {cw.Size}) INFO:**");
            if (cw.WarState == StateOfWar.Created)
            {
                sb.AppendLine("`not started`");
            }
            var twoHours = new TimeSpan(2, 0, 0);

            for (var i = 0; i < cw.Bases.Count; i++)
            {
                if (cw.Bases[i].CallUser == null)
                {
                    sb.AppendLine($"`{i + 1}.` ❌*unclaimed*");
                }
                else
                {
                    if (cw.Bases[i].BaseDestroyed)
                    {
                        sb.AppendLine($"`{i + 1}.` ✅ `{cw.Bases[i].CallUser}` {new string('⭐', cw.Bases[i].Stars)}");
                    }
                    else
                    {
                        var left = (cw.WarState == StateOfWar.Started) ? twoHours - (DateTime.UtcNow - cw.Bases[i].TimeAdded) : twoHours;
                        sb.AppendLine($"`{i + 1}.` ✅ `{cw.Bases[i].CallUser}` {left.Hours}h {left.Minutes}m {left.Seconds}s left");
                    }
                }
            }
            return(sb.ToString());
        }
Beispiel #3
0
        public static async Task <ClashWar> CreateWar(string enemyClan, int size, ulong serverId, ulong channelId)
        {
            using (var uow = DbHandler.UnitOfWork())
            {
                var cw = new ClashWar
                {
                    EnemyClan = enemyClan,
                    Size      = size,
                    Bases     = new List <ClashCaller>(size),
                    GuildId   = serverId,
                    ChannelId = channelId,
                    Channel   = NadekoBot.Client.GetGuild(serverId)
                                ?.GetTextChannel(channelId)
                };
                cw.Bases.Capacity = size;
                for (int i = 0; i < size; i++)
                {
                    cw.Bases.Add(new ClashCaller()
                    {
                        CallUser       = null,
                        SequenceNumber = i,
                    });
                }
                Console.WriteLine(cw.Bases.Capacity);
                uow.ClashOfClans.Add(cw);
                await uow.CompleteAsync();

                return(cw);
            }
        }
Beispiel #4
0
        private static async Task CheckWar(TimeSpan callExpire, ClashWar war)
        {
            var Bases = war.Bases;

            for (var i = 0; i < Bases.Count; i++)
            {
                var callUser = Bases[i].CallUser;
                if (callUser == null)
                {
                    continue;
                }
                if ((!Bases[i].BaseDestroyed) && DateTime.UtcNow - Bases[i].TimeAdded >= callExpire)
                {
                    if (Bases[i].Stars != 3)
                    {
                        Bases[i].BaseDestroyed = true;
                    }
                    else
                    {
                        Bases[i] = null;
                    }
                    try
                    {
                        SaveWar(war);
                        await war.Channel.SendErrorAsync(GetTextStatic("claim_expired",
                                                                       NadekoBot.Localization.GetCultureInfo(war.Channel.GuildId),
                                                                       typeof(ClashOfClans).Name.ToLowerInvariant(),
                                                                       Format.Bold(Bases[i].CallUser),
                                                                       war.ShortPrint()));
                    }
                    catch { }
                }
            }
        }
Beispiel #5
0
 private static async Task CheckWar(TimeSpan callExpire, ClashWar war)
 {
     var Bases = war.Bases;
     for (var i = 0; i < Bases.Count; i++)
     {
         if (Bases[i].CallUser == null) continue;
         if (!Bases[i].BaseDestroyed && DateTime.UtcNow - Bases[i].TimeAdded >= callExpire)
         {
             Bases[i] = null;
             try { await war.Channel.SendMessageAsync($"❗🔰**Claim from @{Bases[i].CallUser} for a war against {war.ShortPrint()} has expired.**").ConfigureAwait(false); } catch { }
     }
     }
 }
Beispiel #6
0
 public static int Uncall(this ClashWar cw, string user)
 {
     user = user.Trim();
     for (var i = 0; i < cw.Bases.Count; i++)
     {
         if (cw.Bases[i]?.CallUser != user)
         {
             continue;
         }
         cw.Bases[i].CallUser = null;
         return(i);
     }
     throw new InvalidOperationException("You are not participating in that war.");
 }
Beispiel #7
0
 public static int FinishClaim(this ClashWar cw, string user, int stars = 3)
 {
     user = user.Trim();
     for (var i = 0; i < cw.Bases.Count; i++)
     {
         if (cw.Bases[i]?.BaseDestroyed != false || cw.Bases[i]?.CallUser != user)
         {
             continue;
         }
         cw.Bases[i].BaseDestroyed = true;
         cw.Bases[i].Stars         = stars;
         return(i);
     }
     throw new InvalidOperationException(cw.Localize("not_partic_or_destroyed", user));
 }
Beispiel #8
0
 public static int FinishClaim(this ClashWar cw, string user, int stars = 3)
 {
     user = user.Trim();
     for (var i = 0; i < cw.Bases.Count; i++)
     {
         if (cw.Bases[i]?.BaseDestroyed != false || cw.Bases[i]?.CallUser != user)
         {
             continue;
         }
         cw.Bases[i].BaseDestroyed = true;
         cw.Bases[i].Stars         = stars;
         return(i);
     }
     throw new InvalidOperationException($"@{user} You are either not participating in that war, or you already destroyed a base.");
 }
Beispiel #9
0
 public static void Start(this ClashWar cw)
 {
     if (cw.WarState == StateOfWar.Started)
     {
         throw new InvalidOperationException("War already started");
     }
     //if (Started)
     //    throw new InvalidOperationException();
     //Started = true;
     cw.WarState  = StateOfWar.Started;
     cw.StartedAt = DateTime.UtcNow;
     foreach (var b in cw.Bases.Where(b => b.CallUser != null))
     {
         b.ResetTime();
     }
 }
Beispiel #10
0
        private static async Task CheckWar(TimeSpan callExpire, ClashWar war)
        {
            var Bases = war.Bases;

            for (var i = 0; i < Bases.Length; i++)
            {
                if (Bases[i] == null)
                {
                    continue;
                }
                if (!Bases[i].BaseDestroyed && DateTime.UtcNow - Bases[i].TimeAdded >= callExpire)
                {
                    await war.Channel.SendMessage($"❗🔰**Claim from @{Bases[i].CallUser} for a war against {war.ShortPrint()} has expired.**").ConfigureAwait(false);

                    Bases[i] = null;
                }
            }
        }
Beispiel #11
0
        public static void SaveWar(ClashWar cw)
        {
            if (cw.WarState == ClashWar.StateOfWar.Ended)
            {
                using (var uow = DbHandler.UnitOfWork())
                {
                    uow.ClashOfClans.Remove(cw);
                    uow.CompleteAsync();
                }
                return;
            }


            using (var uow = DbHandler.UnitOfWork())
            {
                uow.ClashOfClans.Update(cw);
                uow.CompleteAsync();
            }
        }
Beispiel #12
0
        public static void FinishClaim(this ClashWar cw, int index, int stars = 3)
        {
            if (index < 0 || index > cw.Bases.Count)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }
            var toFinish = cw.Bases[index];

            if (toFinish.BaseDestroyed != false)
            {
                throw new InvalidOperationException("That base is already destroyed.");
            }
            if (toFinish.CallUser == null)
            {
                throw new InvalidOperationException("That base is unclaimed.");
            }
            toFinish.BaseDestroyed = true;
            toFinish.Stars         = stars;
        }
Beispiel #13
0
        public static string ToPrettyString(this ClashWar cw)
        {
            var sb = new StringBuilder();

            if (cw.WarState == StateOfWar.Created)
            {
                sb.AppendLine("`not started`");
            }
            var twoHours = new TimeSpan(2, 0, 0);

            for (var i = 0; i < cw.Bases.Count; i++)
            {
                if (cw.Bases[i].CallUser == null)
                {
                    sb.AppendLine($"`{i + 1}.` ❌*{cw.Localize("not_claimed")}*");
                }
                else
                {
                    if (cw.Bases[i].BaseDestroyed)
                    {
                        sb.AppendLine($"`{i + 1}.` ✅ `{cw.Bases[i].CallUser}` {new string('⭐', cw.Bases[i].Stars)}");
                    }
                    else
                    {
                        var left = (cw.WarState == StateOfWar.Started) ? twoHours - (DateTime.UtcNow - cw.Bases[i].TimeAdded) : twoHours;
                        if (cw.Bases[i].Stars == 3)
                        {
                            sb.AppendLine($"`{i + 1}.` ✅ `{cw.Bases[i].CallUser}` {left.Hours}h {left.Minutes}m {left.Seconds}s left");
                        }
                        else
                        {
                            sb.AppendLine($"`{i + 1}.` ✅ `{cw.Bases[i].CallUser}` {left.Hours}h {left.Minutes}m {left.Seconds}s left {new string('⭐', cw.Bases[i].Stars)} {string.Concat(Enumerable.Repeat("🔸", 3 - cw.Bases[i].Stars))}");
                        }
                    }
                }
            }
            return(sb.ToString());
        }
Beispiel #14
0
        public static void Call(this ClashWar cw, string u, int baseNumber)
        {
            if (baseNumber < 0 || baseNumber >= cw.Bases.Count)
            {
                throw new ArgumentException(cw.Localize("invalid_base_number"));
            }
            if (cw.Bases[baseNumber].CallUser != null && cw.Bases[baseNumber].Stars == 3)
            {
                throw new ArgumentException(cw.Localize("base_already_claimed"));
            }
            for (var i = 0; i < cw.Bases.Count; i++)
            {
                if (cw.Bases[i]?.BaseDestroyed == false && cw.Bases[i]?.CallUser == u)
                {
                    throw new ArgumentException(cw.Localize("claimed_other", u, i + 1));
                }
            }

            var cc = cw.Bases[baseNumber];

            cc.CallUser      = u.Trim();
            cc.TimeAdded     = DateTime.UtcNow;
            cc.BaseDestroyed = false;
        }
Beispiel #15
0
        public static void Call(this ClashWar cw, string u, int baseNumber)
        {
            if (baseNumber < 0 || baseNumber >= cw.Bases.Count)
            {
                throw new ArgumentException("Invalid base number");
            }
            if (cw.Bases[baseNumber].CallUser != null)
            {
                throw new ArgumentException("That base is already claimed.");
            }
            for (var i = 0; i < cw.Bases.Count; i++)
            {
                if (cw.Bases[i]?.BaseDestroyed == false && cw.Bases[i]?.CallUser == u)
                {
                    throw new ArgumentException($"@{u} You already claimed base #{i + 1}. You can't claim a new one.");
                }
            }

            var cc = cw.Bases[baseNumber];

            cc.CallUser      = u.Trim();
            cc.TimeAdded     = DateTime.UtcNow;
            cc.BaseDestroyed = false;
        }
Beispiel #16
0
 public static string Localize(this ClashWar cw, string key, params object[] replacements)
 {
     return(string.Format(cw.Localize(key), replacements));
 }
Beispiel #17
0
 public static string ShortPrint(this ClashWar cw) =>
 $"`{cw.EnemyClan}` ({cw.Size} v {cw.Size})";
Beispiel #18
0
 public static void End(this ClashWar cw)
 {
     //Ended = true;
     cw.WarState = StateOfWar.Ended;
 }
Beispiel #19
0
 public static string Localize(this ClashWar cw, string key)
 {
     return(NadekoTopLevelModule.GetTextStatic(key,
                                               NadekoBot.Localization.GetCultureInfo(cw.Channel?.GuildId),
                                               typeof(ClashOfClans).Name.ToLowerInvariant()));
 }
Beispiel #20
0
 public static async Task<ClashWar> CreateWar(string enemyClan, int size, ulong serverId, ulong channelId)
 {
     using (var uow = DbHandler.UnitOfWork())
     {
         var cw = new ClashWar
         {
             EnemyClan = enemyClan,
             Size = size,
             Bases = new List<ClashCaller>(size),
             GuildId = serverId,
             ChannelId = channelId,
             Channel = NadekoBot.Client.GetGuild(serverId)
                                ?.GetTextChannel(channelId)
         };
         cw.Bases.Capacity = size;
         for (int i = 0; i < size; i++)
         {
             cw.Bases.Add(new ClashCaller()
             {
                 CallUser = null,
                 SequenceNumber = i,
             });
         }
         Console.WriteLine(cw.Bases.Capacity);
         uow.ClashOfClans.Add(cw);
         await uow.CompleteAsync();
         return cw;
     }
 }
Beispiel #21
0
        public override void Install(ModuleManager manager)
        {
            manager.CreateCommands("", cgb =>
            {
                cgb.CreateCommand(Prefix + "createwar")
                .Alias(Prefix + "cw")
                .Description(
                    $"Creates a new war by specifying a size (>10 and multiple of 5) and enemy clan name.\n**Usage**:{Prefix}cw 15 The Enemy Clan")
                .Parameter("size")
                .Parameter("enemy_clan", ParameterType.Unparsed)
                .Do(async e =>
                {
                    if (!e.User.ServerPermissions.ManageChannels)
                    {
                        return;
                    }
                    List <ClashWar> wars;
                    if (!ClashWars.TryGetValue(e.Server.Id, out wars))
                    {
                        wars = new List <ClashWar>();
                        if (!ClashWars.TryAdd(e.Server.Id, wars))
                        {
                            return;
                        }
                    }
                    var enemyClan = e.GetArg("enemy_clan");
                    if (string.IsNullOrWhiteSpace(enemyClan))
                    {
                        return;
                    }
                    int size;
                    if (!int.TryParse(e.GetArg("size"), out size) || size < 10 || size > 50 || size % 5 != 0)
                    {
                        await e.Channel.SendMessage("💢🔰 Not a Valid war size").ConfigureAwait(false);
                        return;
                    }
                    var cw = new ClashWar(enemyClan, size, e);
                    //cw.Start();
                    wars.Add(cw);
                    cw.OnUserTimeExpired += async(u) =>
                    {
                        try
                        {
                            await
                            e.Channel.SendMessage(
                                $"❗🔰**Claim from @{u} for a war against {cw.ShortPrint()} has expired.**")
                            .ConfigureAwait(false);
                        }
                        catch { }
                    };
                    cw.OnWarEnded += async() =>
                    {
                        try
                        {
                            await e.Channel.SendMessage($"❗🔰**War against {cw.ShortPrint()} ended.**").ConfigureAwait(false);
                        }
                        catch { }
                    };
                    await e.Channel.SendMessage($"❗🔰**CREATED CLAN WAR AGAINST {cw.ShortPrint()}**").ConfigureAwait(false);
                    //war with the index X started.
                });

                cgb.CreateCommand(Prefix + "startwar")
                .Alias(Prefix + "sw")
                .Description("Starts a war with a given number.")
                .Parameter("number", ParameterType.Required)
                .Do(async e =>
                {
                    var warsInfo = GetInfo(e);
                    if (warsInfo == null)
                    {
                        await e.Channel.SendMessage("💢🔰 **That war does not exist.**").ConfigureAwait(false);
                        return;
                    }
                    var war = warsInfo.Item1[warsInfo.Item2];
                    try
                    {
                        var startTask = war.Start();
                        await e.Channel.SendMessage($"🔰**STARTED WAR AGAINST {war.ShortPrint()}**").ConfigureAwait(false);
                        await startTask.ConfigureAwait(false);
                    }
                    catch
                    {
                        await e.Channel.SendMessage($"🔰**WAR AGAINST {war.ShortPrint()} IS ALREADY STARTED**").ConfigureAwait(false);
                    }
                });

                cgb.CreateCommand(Prefix + "listwar")
                .Alias(Prefix + "lw")
                .Description($"Shows the active war claims by a number. Shows all wars in a short way if no number is specified.\n**Usage**: {Prefix}lw [war_number] or {Prefix}lw")
                .Parameter("number", ParameterType.Optional)
                .Do(async e =>
                {
                    // if number is null, print all wars in a short way
                    if (string.IsNullOrWhiteSpace(e.GetArg("number")))
                    {
                        //check if there are any wars
                        List <ClashWar> wars = null;
                        ClashWars.TryGetValue(e.Server.Id, out wars);
                        if (wars == null || wars.Count == 0)
                        {
                            await e.Channel.SendMessage("🔰 **No active wars.**").ConfigureAwait(false);
                            return;
                        }

                        var sb = new StringBuilder();
                        sb.AppendLine("🔰 **LIST OF ACTIVE WARS**");
                        sb.AppendLine("**-------------------------**");
                        for (var i = 0; i < wars.Count; i++)
                        {
                            sb.AppendLine($"**#{i + 1}.**  `Enemy:` **{wars[i].EnemyClan}**");
                            sb.AppendLine($"\t\t`Size:` **{wars[i].Size} v {wars[i].Size}**");
                            sb.AppendLine("**-------------------------**");
                        }
                        await e.Channel.SendMessage(sb.ToString()).ConfigureAwait(false);
                        return;
                    }
                    //if number is not null, print the war needed
                    var warsInfo = GetInfo(e);
                    if (warsInfo == null)
                    {
                        await e.Channel.SendMessage("💢🔰 **That war does not exist.**").ConfigureAwait(false);
                        return;
                    }
                    await e.Channel.SendMessage(warsInfo.Item1[warsInfo.Item2].ToString()).ConfigureAwait(false);
                });

                cgb.CreateCommand(Prefix + "claim")
                .Alias(Prefix + "call")
                .Alias(Prefix + "c")
                .Description($"Claims a certain base from a certain war. You can supply a name in the third optional argument to claim in someone else's place. \n**Usage**: {Prefix}call [war_number] [base_number] [optional_other_name]")
                .Parameter("number")
                .Parameter("baseNumber")
                .Parameter("other_name", ParameterType.Unparsed)
                .Do(async e =>
                {
                    var warsInfo = GetInfo(e);
                    if (warsInfo == null || warsInfo.Item1.Count == 0)
                    {
                        await e.Channel.SendMessage("💢🔰 **That war does not exist.**").ConfigureAwait(false);
                        return;
                    }
                    int baseNum;
                    if (!int.TryParse(e.GetArg("baseNumber"), out baseNum))
                    {
                        await e.Channel.SendMessage("💢🔰 **Invalid base number.**").ConfigureAwait(false);
                        return;
                    }
                    var usr =
                        string.IsNullOrWhiteSpace(e.GetArg("other_name")) ?
                        e.User.Name :
                        e.GetArg("other_name");
                    try
                    {
                        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);
                    }
                    catch (Exception ex)
                    {
                        await e.Channel.SendMessage($"💢🔰 {ex.Message}").ConfigureAwait(false);
                    }
                });

                cgb.CreateCommand(Prefix + "claimfinish")
                .Alias(Prefix + "cf")
                .Description($"Finish your claim if you destroyed a base. Optional second argument finishes for someone else.\n**Usage**: {Prefix}cf [war_number] [optional_other_name]")
                .Parameter("number", ParameterType.Required)
                .Parameter("other_name", ParameterType.Unparsed)
                .Do(async e =>
                {
                    var warInfo = GetInfo(e);
                    if (warInfo == null || warInfo.Item1.Count == 0)
                    {
                        await e.Channel.SendMessage("💢🔰 **That war does not exist.**").ConfigureAwait(false);
                        return;
                    }
                    var usr =
                        string.IsNullOrWhiteSpace(e.GetArg("other_name")) ?
                        e.User.Name :
                        e.GetArg("other_name");

                    var war = warInfo.Item1[warInfo.Item2];
                    try
                    {
                        var baseNum = war.FinishClaim(usr);
                        await e.Channel.SendMessage($"❗🔰{e.User.Mention} **DESTROYED** a base #{baseNum + 1} in a war against {war.ShortPrint()}").ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        await e.Channel.SendMessage($"💢🔰 {ex.Message}").ConfigureAwait(false);
                    }
                });

                cgb.CreateCommand(Prefix + "unclaim")
                .Alias(Prefix + "uncall")
                .Alias(Prefix + "uc")
                .Description($"Removes your claim from a certain war. Optional second argument denotes a person in whos place to unclaim\n**Usage**: {Prefix}uc [war_number] [optional_other_name]")
                .Parameter("number", ParameterType.Required)
                .Parameter("other_name", ParameterType.Unparsed)
                .Do(async e =>
                {
                    var warsInfo = GetInfo(e);
                    if (warsInfo == null || warsInfo.Item1.Count == 0)
                    {
                        await e.Channel.SendMessage("💢🔰 **That war does not exist.**").ConfigureAwait(false);
                        return;
                    }
                    var usr =
                        string.IsNullOrWhiteSpace(e.GetArg("other_name")) ?
                        e.User.Name :
                        e.GetArg("other_name");
                    try
                    {
                        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);
                    }
                    catch (Exception ex)
                    {
                        await e.Channel.SendMessage($"💢🔰 {ex.Message}").ConfigureAwait(false);
                    }
                });

                cgb.CreateCommand(Prefix + "endwar")
                .Alias(Prefix + "ew")
                .Description($"Ends the war with a given index.\n**Usage**:{Prefix}ew [war_number]")
                .Parameter("number")
                .Do(async e =>
                {
                    var warsInfo = GetInfo(e);
                    if (warsInfo == null)
                    {
                        await e.Channel.SendMessage("💢🔰 That war does not exist.").ConfigureAwait(false);
                        return;
                    }
                    warsInfo.Item1[warsInfo.Item2].End();

                    var size = warsInfo.Item1[warsInfo.Item2].Size;
                    warsInfo.Item1.RemoveAt(warsInfo.Item2);
                });
            });
        }
Beispiel #22
0
        public static void SaveWar(ClashWar cw)
        {
            if (cw.WarState == ClashWar.StateOfWar.Ended)
            {
                using (var uow = DbHandler.UnitOfWork())
                {
                    uow.ClashOfClans.Remove(cw);
                    uow.CompleteAsync();
                }
                return;
            }


            using (var uow = DbHandler.UnitOfWork())
            {
                uow.ClashOfClans.Update(cw);
                uow.CompleteAsync();
            }
        }