Ejemplo n.º 1
0
        private async Task RefreshArmiesAsync()
        {
            try {
                Enabled = false;
                armies.Clear();

                var packet = await packetWriter.RefreshPowerAreaAsync(1);

                if (packet == null)
                {
                    return;
                }
                Debug.Assert(packet.CommandId == "33201");

                var powerIds      = new Queue <int>(Parse33201(packet));
                var addedPowerIds = powerIds.ToList();
                while (powerIds.Count > 0)
                {
                    var powerId     = powerIds.Dequeue();
                    var powerPacket = await packetWriter.RefreshPowerAsync(powerId);

                    if (powerPacket == null)
                    {
                        return;
                    }
                    Debug.Assert(powerPacket.CommandId == "33100");

                    var token = JToken.Parse(powerPacket.Message);
                    var power = PowerDetail.Parse(token);
                    if (power == null)
                    {
                        return;
                    }
                    var powerArmy = power.Armies.FirstOrDefault(army => army.Type == ArmyType.Army);
                    if (powerArmy != null)
                    {
                        armies.Add(powerArmy);
                    }
                    foreach (var nextPower in power.NextPowers)
                    {
                        if (addedPowerIds.Contains(nextPower.Id))
                        {
                            continue;
                        }
                        if (nextPower.Attackable)
                        {
                            addedPowerIds.Add(nextPower.Id);
                            powerIds.Enqueue(nextPower.Id);
                        }
                    }
                }
            } finally {
                Enabled = true;
            }
        }