コード例 #1
0
ファイル: ClanWar.cs プロジェクト: devhl-labs/CocApi
        internal void Initialize(DateTime serverExpiration, string?warTag)
        {
            lock (_initializeLock)
            {
                if (_isInitialized)
                {
                    return;
                }

                ServerExpiration = serverExpiration;

                WarTag = warTag;

                if (State == WarState.NotInWar)
                {
                    return;
                }

                WarClan clan     = Clan;
                WarClan opponent = Opponent;

                SortedList <string, WarClan> sorted = new SortedList <string, WarClan>
                {
                    { clan.Tag, clan },
                    { opponent.Tag, opponent }
                };

                Clan     = sorted.Values.First();
                Opponent = sorted.Values.Skip(1).First();

                foreach (WarClan warClan in Clans.Values)
                {
                    if (AllAttacksAreUsedOrWarIsOver())
                    {
                        WarClan enemy = Clans.First(c => c.Key != warClan.Tag).Value;

                        if (warClan.Stars > enemy.Stars)
                        {
                            warClan.Result = Result.Win;
                        }
                        else if (warClan.Stars < enemy.Stars)
                        {
                            warClan.Result = Result.Lose;
                        }
                        else if (warClan.Stars == enemy.Stars && warClan.DestructionPercentage > enemy.DestructionPercentage)
                        {
                            warClan.Result = Result.Win;
                        }
                        else if (warClan.Stars == enemy.Stars && warClan.DestructionPercentage < enemy.DestructionPercentage)
                        {
                            warClan.Result = Result.Lose;
                        }
                        else
                        {
                            warClan.Result = Result.Tie;
                        }
                    }

                    int mapPosition = 1;

                    foreach (ClanWarMember member in warClan.Members.OrderBy(m => m.RosterPosition))
                    {
                        member.MapPosition = mapPosition;
                        mapPosition++;

                        foreach (ClanWarAttack attack in member.Attacks.EmptyIfNull())
                        {
                            WarClan       defendingClan = Clans.First(c => c.Key != warClan.Tag).Value;
                            ClanWarMember defending     = defendingClan.Members.First(m => m.Tag == attack.DefenderTag);

                            attack.AttackerClanTag     = warClan.Tag;
                            attack.DefenderClanTag     = defendingClan.Tag;
                            attack.AttackerTownHall    = member.TownhallLevel;
                            attack.DefenderTownHall    = defending.TownhallLevel;
                            attack.AttackerMapPosition = member.RosterPosition;
                            attack.DefenderMapPosition = defending.RosterPosition;
                        }
                    }
                }


                foreach (var wc in Clans.Values)
                {
                    var grouped = Attacks.Where(a => a.AttackerClanTag == wc.Tag).GroupBy(a => a.DefenderMapPosition);

                    foreach (var group in grouped)
                    {
                        bool fresh    = true;
                        int  maxStars = 0;

                        foreach (var attack in group.OrderBy(g => g.Order))
                        {
                            attack.Fresh = fresh;
                            fresh        = false;

                            attack.StarsGained = attack.Stars - maxStars;

                            if (attack.StarsGained < 0)
                            {
                                attack.StarsGained = 0;
                            }

                            if (attack.Stars > maxStars)
                            {
                                maxStars = attack.Stars;
                            }
                        }
                    }
                }

                //TimeSpan timeSpan = StartTime - PreparationStartTime;

                //if (timeSpan.TotalHours == 24
                //    || timeSpan.TotalHours == 20
                //    || timeSpan.TotalHours == 16
                //    || timeSpan.TotalHours == 12
                //    || timeSpan.TotalHours == 8
                //    || timeSpan.TotalHours == 6
                //    || timeSpan.TotalHours == 4
                //    || timeSpan.TotalHours == 2
                //    || timeSpan.TotalHours == 1
                //    || timeSpan.TotalMinutes == 30
                //    || timeSpan.TotalMinutes == 15)
                //{
                //    WarType = WarType.Friendly;
                //}

                //if (timeSpan.TotalHours == 23)
                //    WarType = WarType.Random;

                //if (WarTag != null)
                //    WarType = WarType.SCCWL;

                _isInitialized = true;
            }
        }