示例#1
0
        public static async Task <string> GetOrderedMembersAsync(ClashClient clash, string clanTag)
        {
            var clanMembers = await clash.GetClanMembersAsync(clanTag);

            var currentWar = await clash.GetCurrentWarAsync(clanTag);

            if (currentWar.Clan.Members is null)
            {
                return("");
            }

            var warMembers = currentWar.Clan.Members;

            var membersByDonations = clanMembers.OrderByDescending(x => x.Donations);

            var i = 1;

            var donationList = string.Join('\n', membersByDonations.Select(x => $"{i++}: {Format.Sanitize(x.Name)} - **{x.Donations}**"));

            var sb = new StringBuilder();

            sb.AppendLine($"__**Members Ordered By Donations**__: \n{donationList}");

            if (currentWar.State == WarState.Ended)
            {
                var missedAttackers = warMembers.Where(x => x.Attacks.Count == 0);

                var missedList = string.Join('\n', missedAttackers.Select(x => Format.Sanitize(x.Name)));

                sb.AppendLine($"\n\n__**Missed Attackers**__:\n{missedList}");
            }

            return(sb.ToString());
        }
示例#2
0
        public async Task StartRemindersAsync()
        {
            var dbGuild = _database.Guild;

            var channel = _client.GetChannel(dbGuild.WarChannelId) as SocketTextChannel;
            var guild   = channel.Guild;

            IRole warRole = null;

            while (true)
            {
                try
                {
                    var currentWar = await _clash.GetCurrentWarAsync(dbGuild.ClanTag);

                    if (currentWar is null || currentWar.State == WarState.Default || currentWar.State == WarState.Ended)
                    {
                        await Task.Delay(TimeSpan.FromMinutes(10), _maintenanceCts.Token);

                        continue;
                    }

                    async Task RunRemindersAsync(bool hasMatched)
                    {
                        TimeSpan threshold;

                        if (hasMatched)
                        {
                            currentWar = await _clash.GetCurrentWarAsync(dbGuild.ClanTag);

                            threshold = DateTimeOffset.UtcNow - currentWar.StartTime;
                        }

                        else
                        {
                            threshold = DateTimeOffset.UtcNow - currentWar.PreparationTime;
                        }

                        if (threshold < TimeSpan.FromMinutes(60))
                        {
                            if (!hasMatched)
                            {
                                warRole = await WarMatchAsync(channel, dbGuild, guild, currentWar);

                                var startTime = currentWar.StartTime - DateTimeOffset.UtcNow;

                                await Task.Delay(startTime, _maintenanceCts.Token);
                            }

                            await channel.SendMessageAsync($"{warRole?.Mention} war has started!");

                            _warTcs.SetResult(true);

                            var beforeEnd = currentWar.EndTime - DateTimeOffset.UtcNow.AddHours(1);

                            await Task.Delay(beforeEnd, _maintenanceCts.Token);

                            currentWar = await _clash.GetCurrentWarAsync(dbGuild.ClanTag);

                            await NeedToAttackAsync(channel, dbGuild.GuildMembers, currentWar);

                            await Task.Delay(TimeSpan.FromHours(1).Add(TimeSpan.FromMinutes(10)));

                            currentWar = await _clash.GetCurrentWarAsync(dbGuild.ClanTag);

                            await WarEndedAsync(channel, dbGuild.GuildMembers, currentWar);

                            await warRole.DeleteAsync();
                        }
                    }

                    switch (currentWar.State)
                    {
                    case WarState.Preparation:
                        await RunRemindersAsync(false);

                        break;

                    case WarState.InWar:
                        await RunRemindersAsync(true);

                        break;
                    }

                    await Task.Delay(TimeSpan.FromMinutes(10), _maintenanceCts.Token);
                }
                catch (TaskCanceledException)
                {
                    await channel.SendMessageAsync("Maintenance break");

                    try
                    {
                        await Task.Delay(-1, _noMaintenanceCts.Token);
                    }
                    catch (TaskCanceledException) { }

                    await channel.SendMessageAsync("Maintenance ended");
                }
                catch (Exception ex)
                {
                    await _logger.LogAsync(Source.Reminder, Severity.Error, string.Empty, ex);
                }
                finally
                {
                    _warTcs = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
                }
            }
        }
示例#3
0
        public static async Task <BaseLottoResult> CalculateWarWinnerAsync(ClashClient client, string clanTag)
        {
            var currentWar = await client.GetCurrentWarAsync(clanTag);

            if (currentWar is null)
            {
                return(new LottoFailed
                {
                    Reason = "API maintenance"
                });
            }

            if (currentWar.Size == 0)
            {
                return(new LottoFailed
                {
                    Reason = "API hasn't updated yet"
                });
            }

            clanTag = currentWar.Clan.Tag;
            var opponentTag = currentWar.Opponent.Tag;

            var opponentWarLog = (await client.GetWarLogAsync(opponentTag, 10)).Entity;

            if (opponentWarLog.Count == 0)
            {
                return(new LottoFailed
                {
                    Reason = "Opposition war log is private"
                });
            }

            var clanWarLog = (await client.GetWarLogAsync(clanTag, 10)).Entity;

            var trimmedClanLog     = TrimWarLog(clanWarLog).ToArray();
            var trimmedOpponentLog = TrimWarLog(opponentWarLog).ToArray();

            var clanTotal     = trimmedClanLog.Count(x => x.Result == WarResult.Lose);
            var opponentTotal = trimmedOpponentLog.Count(x => x.Result == WarResult.Lose);

            var comparison = BuildWarLongComparison(trimmedClanLog, trimmedOpponentLog);

            var draw = clanTotal == opponentTotal;

            if (!draw)
            {
                return(new LottoResult
                {
                    ClanName = currentWar.Clan.Name,
                    ClanTag = clanTag,
                    OpponentName = currentWar.Opponent.Name,
                    OpponentTag = opponentTag,
                    ClanWin = clanTotal > opponentTotal,
                    WarLogComparison = comparison
                });
            }

            var chars  = "#0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            var result = 0;

            for (var i = 0; i < chars.Length; i++)
            {
                if (Array.IndexOf(chars, clanTag[i]) == Array.IndexOf(chars, opponentTag[i]))
                {
                    continue;
                }
                if (Array.IndexOf(chars, clanTag[i]) > Array.IndexOf(chars, opponentTag[i]))
                {
                    result = -1;
                    break;
                }
                result = 1;
                break;
            }

            return(new LottoDraw
            {
                ClanName = currentWar.Clan.Name,
                ClanTag = clanTag,
                OpponentName = currentWar.Opponent.Name,
                OpponentTag = opponentTag,
                HighSyncWinnerTag = result < 0 ? clanTag : opponentTag,
                WarLogComparison = comparison
            });
        }