示例#1
0
文件: ClientUI.cs 项目: N-2-L/Tanks
        public ClientUI()
        {
            InitializeComponent();

            //initialize Map
            map = new string[Constant.MAP_SIZE, Constant.MAP_SIZE];
            for (int i = 0; i < Constant.MAP_SIZE; i++)
            {
                for (int j = 0; j < Constant.MAP_SIZE; j++)
                    map[i, j] = Constant.EMPTY;
            }

            mapDetails = new string[Constant.MAP_SIZE, Constant.MAP_SIZE];
            mapLabels = new Label[Constant.MAP_SIZE, Constant.MAP_SIZE];
            for (int i = 0; i < Constant.MAP_SIZE; i++)
            {
                for (int j = 0; j < Constant.MAP_SIZE; j++)
                    mapLabels[i, j] = (Label)tableLayoutPanel2.GetControlFromPosition(i,j);
            }
            //initialize details table
            tableDetails = new string[5,5];
            tableLabels = new Label[5,5];
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                    tableLabels[i, j] = (Label)tableLayoutPanel1.GetControlFromPosition(i+1, j+1);
            }

            //instantiate message passer
            parser = new MsgParser();
            //instantiate network client
            networkClient = new ClientClass(parser);

            //Adding a timer
            timer = new Timer();
            timer.Interval = 100;

            //adding listeners
            timer.Tick += updateMap;
            timer.Tick += updateLabels;
            timer.Tick += updateCommConsole;
            timer.Start();
        }
示例#2
0
        /// <summary>
        /// Will be called before each task to update resources/msg?,villages,quests,hero health, adventures num, gold/silver
        /// </summary>
        /// <param name="acc">Account</param>
        /// <returns>True if successful, false if error</returns>
        private static bool PreTaskRefresh(Account acc)
        {
            var html = acc.Wb.Html;

            try
            {
                //check & update dorf1/dorf2
                if (!UpdateAccountObject.UpdateVillages(html, acc))
                {
                    return(false);                                                //Web browser not initiali
                }
                var activeVill = acc.Villages.FirstOrDefault(x => x.Active);
                //update dorf1/dorf2
                if (acc.Wb.CurrentUrl.Contains("dorf1"))
                {
                    UpdateDorf1Info(acc);
                }
                else if (acc.Wb.CurrentUrl.Contains("dorf2"))
                {
                    UpdateDorf2Info(acc);
                }


                acc.AccInfo.CulturePoints = RightBarParser.GetCulurePoints(html, acc.AccInfo.ServerVersion);

                var villExpansionReady = acc.Villages.FirstOrDefault(x => x.Expansion.ExpensionAvailable);
                if (acc.AccInfo.CulturePoints.MaxVillages > acc.AccInfo.CulturePoints.VillageCount &&
                    villExpansionReady != null)
                {
                    villExpansionReady.Expansion.ExpensionAvailable = false;
                    TaskExecutor.AddTaskIfNotExists(acc, new SendSettlers()
                    {
                        ExecuteAt = DateTime.Now, vill = villExpansionReady
                    });
                }

                acc.AccInfo.Tribe = LeftBarParser.GetAccountTribe(acc, html);
                acc.Quests        = RightBarParser.GetQuests(html);
                var goldSilver = RightBarParser.GetGoldAndSilver(html, acc.AccInfo.ServerVersion);
                acc.AccInfo.Gold        = goldSilver[0];
                acc.AccInfo.Silver      = goldSilver[1];
                acc.AccInfo.PlusAccount = RightBarParser.HasPlusAccount(html, acc.AccInfo.ServerVersion);
                //Check reports/msg count
                if (MsgParser.UnreadMessages(html, acc.AccInfo.ServerVersion) > 0 &&
                    !acc.Wb.CurrentUrl.Contains("messages.php") &&
                    !IsTaskOnQueue(acc, typeof(ReadMessage)))
                {
                    TaskExecutor.AddTask(acc, new ReadMessage()
                    {
                        ExecuteAt = DateTime.Now.AddMilliseconds(AccountHelper.Delay() * 30)
                    });
                }

                //update loyalty of village


                activeVill.Res.FreeCrop = RightBarParser.GetFreeCrop(html);
                activeVill.Res.Capacity = ResourceParser.GetResourceCapacity(html, acc.AccInfo.ServerVersion);
                activeVill.Res.Stored   = ResourceParser.GetResources(html);

                float ratio = (float)activeVill.Res.Stored.Resources.Crop / activeVill.Res.Capacity.GranaryCapacity;
                if (ratio >= 0.99 &&
                    acc.AccInfo.Gold >= 3 &&
                    activeVill.Market.Npc.Enabled &&
                    (activeVill.Market.Npc.NpcIfOverflow || !MarketHelper.NpcWillOverflow(activeVill)))
                {  //npc crop!
                    TaskExecutor.AddTaskIfNotExistInVillage(acc, activeVill, new NPC()
                    {
                        ExecuteAt = DateTime.MinValue,
                        vill      = activeVill
                    });
                }
                if (acc.Settings.AutoActivateProductionBoost && CheckProductionBoost(acc))
                {
                    TaskExecutor.AddTask(acc, new TTWarsPlusAndBoost()
                    {
                        ExecuteAt = DateTime.Now.AddSeconds(1)
                    });
                }

                acc.Hero.AdventureNum    = HeroParser.GetAdventureNum(html, acc.AccInfo.ServerVersion);
                acc.Hero.Status          = HeroParser.HeroStatus(html, acc.AccInfo.ServerVersion);
                acc.Hero.HeroInfo.Health = HeroParser.GetHeroHealth(html, acc.AccInfo.ServerVersion);

                bool heroReady = (acc.Hero.HeroInfo.Health > acc.Hero.Settings.MinHealth &&
                                  acc.Hero.Settings.AutoSendToAdventure &&
                                  acc.Hero.Status == Hero.StatusEnum.Home &&
                                  acc.Hero.NextHeroSend < DateTime.Now);
                // Update adventures
                if (heroReady &&
                    (acc.Hero.AdventureNum != acc.Hero.Adventures.Count() || HeroHelper.AdventureInRange(acc))) //update adventures
                {
                    AddTaskIfNotExists(acc, new StartAdventure()
                    {
                        ExecuteAt = DateTime.Now.AddSeconds(10)
                    });
                }
                if (acc.Hero.AdventureNum == 0 && acc.Hero.Settings.BuyAdventures) //for UNL servers, buy adventures
                {
                    AddTaskIfNotExists(acc, new TTWarsBuyAdventure()
                    {
                        ExecuteAt = DateTime.Now.AddSeconds(5)
                    });
                }
                if (acc.Hero.Status == Hero.StatusEnum.Dead && acc.Hero.Settings.AutoReviveHero) //if hero is dead, revive him
                {
                    AddTaskIfNotExists(acc, new ReviveHero()
                    {
                        ExecuteAt = DateTime.Now.AddSeconds(5), vill = AccountHelper.GetHeroReviveVillage(acc)
                    });
                }
                if (HeroParser.LeveledUp(html) && acc.Hero.Settings.AutoSetPoints)
                {
                    AddTaskIfNotExists(acc, new HeroSetPoints()
                    {
                        ExecuteAt = DateTime.Now
                    });
                }
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error in PreTask " + e.Message + "\n\nStack Trace: " + e.StackTrace + "\n-----------------------");
                return(false);
            }
        }
示例#3
0
        /// <summary>

        /// Gets tasks that should be executed after loading a page
        /// </summary>
        /// <param name="acc">Account</param>
        /// <returns>List of tasks</returns>
        public static List <Action> GetPostLoadTasks(Account acc)
        {
            var html = acc.Wb.Html;

            var ran = new Random();

            //Web browser not initialized
            if (!UpdateAccountObject.UpdateVillages(html, acc))
            {
                return(new List <Action>());
            }

            //Didnt check version yet
            if (acc.AccInfo.ServerSpeed == 0 || acc.AccInfo.MapSize == 0)
            {
                return(new List <Action>());
            }

            var vill = acc.Villages.FirstOrDefault(x => x.Active);

            return(new List <Action>()
            {
                // 1: update info
                () => {
                    if (acc.Wb.CurrentUrl.Contains("dorf1"))
                    {
                        TaskExecutor.UpdateDorf1Info(acc);
                    }
                    else if (acc.Wb.CurrentUrl.Contains("dorf2"))
                    {
                        TaskExecutor.UpdateDorf2Info(acc);
                    }
                },
                // 2: get culture point
                () => acc.AccInfo.CulturePoints = RightBarParser.GetCulturePoints(html, acc.AccInfo.ServerVersion),
                // 3: Village expansion:
                () =>
                {
                    var villExpansionReady = acc.Villages.FirstOrDefault(x => x.Expansion.ExpansionAvailable);
                    if (acc.AccInfo.CulturePoints?.MaxVillages > acc.AccInfo.CulturePoints?.VillageCount &&
                        villExpansionReady != null)
                    {
                        villExpansionReady.Expansion.ExpansionAvailable = false;
                        acc.Tasks.Add(new SendSettlers()
                        {
                            ExecuteAt = DateTime.Now, Vill = villExpansionReady
                        }, true);
                    }
                },
                // 4: claim Beginner Quests:
                () =>
                {
                    if (acc.AccInfo.ServerVersion == Classificator.ServerVersionEnum.T4_5 &&
                        acc.Wb.Html.GetElementbyId("sidebarBoxQuestmaster")?
                        .Descendants()?.FirstOrDefault(x => x.HasClass("newQuestSpeechBubble")) != null &&
                        acc.Wb.Html.GetElementbyId("mentorTaskList") == null &&
                        acc.Quests.ClaimBeginnerQuests)
                    {
                        acc.Tasks.Add(new ClaimBeginnerTask2021()
                        {
                            ExecuteAt = DateTime.Now
                        }, true);
                        return;
                    }

                    acc.Quests.Quests = RightBarParser.GetBeginnerQuests(html, acc.AccInfo.ServerVersion);
                    var claimQuest = acc.Quests?.Quests?.FirstOrDefault(x => x.finished);
                    if (claimQuest != null &&
                        acc.Quests.ClaimBeginnerQuests
                        )
                    {
                        acc.Tasks.Add(new ClaimBeginnerTask()
                        {
                            ExecuteAt = DateTime.Now,
                            QuestToClaim = claimQuest,
                            Vill = VillageHelper.VillageFromId(acc, acc.Quests.VillToClaim)
                        }, true);
                    }
                },
                // 5: claim Daily Quest:
                () =>
                {
                    if (acc.AccInfo.ServerVersion == Classificator.ServerVersionEnum.T4_5 &&
                        RightBarParser.CheckDailyQuest(html) &&
                        acc.Quests.ClaimDailyQuests)
                    {
                        acc.Tasks.Add(new ClaimDailyTask()
                        {
                            ExecuteAt = DateTime.Now,
                            Vill = VillageHelper.VillageFromId(acc, acc.Quests.VillToClaim)
                        }, true);
                    }
                },
                // 6: Parse gold/silver
                () =>
                {
                    var goldSilver = RightBarParser.GetGoldAndSilver(html, acc.AccInfo.ServerVersion);
                    acc.AccInfo.Gold = goldSilver[0];
                    acc.AccInfo.Silver = goldSilver[1];
                },
                // 7: plus acconunt
                () => acc.AccInfo.PlusAccount = RightBarParser.HasPlusAccount(html, acc.AccInfo.ServerVersion),

                // 8: Check msgs:
                () =>
                {
                    if (MsgParser.UnreadMessages(html, acc.AccInfo.ServerVersion) > 0 &&
                        !acc.Wb.CurrentUrl.Contains("messages.php") &&
                        acc.Settings.AutoReadIgms)
                    {
                        acc.Tasks.Add(new ReadMessage()
                        {
                            ExecuteAt = DateTime.Now.AddSeconds(ran.Next(10, 600)), // Read msg in next 10-600 seconds
                            Priority = TaskPriority.Low
                        }, true);
                    }
                },
                // 9: JS resources
                () => {
                    // TODO: cast directly from object to ResourcesJsObject, no de/serialization!
                    var resJson = DriverHelper.GetJsObj <string>(acc, "JSON.stringify(resources);");
                    var resJs = JsonConvert.DeserializeObject <ResourcesJsObject>(resJson);

                    vill.Res.Capacity.GranaryCapacity = resJs.maxStorage.l4;
                    vill.Res.Capacity.WarehouseCapacity = resJs.maxStorage.l1;

                    vill.Res.Stored.Resources = resJs.storage.GetResources();
                    vill.Res.Stored.LastRefresh = DateTime.Now;

                    vill.Res.Production = resJs.production.GetResources();
                    vill.Res.FreeCrop = resJs.production.l5;
                },
                // 10: Check if there are unfinished tasks
                () => ResSpendingHelper.CheckUnfinishedTasks(acc, vill),
                // 11: Donate to ally bonus
                () => DonateToAlly(acc, vill),
                // 12: increase next village refresh time
                () => VillageHelper.SetNextRefresh(acc, vill),
                // 13: NPC:
                () =>
                {
                    float ratio = (float)vill.Res.Stored.Resources.Crop / vill.Res.Capacity.GranaryCapacity;
                    if (0.99 <= ratio &&
                        3 <= acc.AccInfo.Gold &&
                        vill.Market.Npc.Enabled &&
                        (vill.Market.Npc.NpcIfOverflow || !MarketHelper.NpcWillOverflow(vill)))
                    {  //npc crop!
                        acc.Tasks.Add(new NPC()
                        {
                            ExecuteAt = DateTime.MinValue,
                            Vill = vill
                        }, true, vill);
                    }
                },
                // 14: TTwars plus and boost
                () => {
                    if (acc.Settings.AutoActivateProductionBoost && CheckProductionBoost(acc))
                    {
                        acc.Tasks.Add(new TTWarsPlusAndBoost()
                        {
                            ExecuteAt = DateTime.Now.AddSeconds(1)
                        });
                    }
                },
                // 15: Insta upgrade:
                () =>
                {
                    if (vill.Build.InstaBuild &&
                        acc.AccInfo.Gold >= 2 &&
                        vill.Build.CurrentlyBuilding.Count >= (acc.AccInfo.PlusAccount ? 2 : 1) &&
                        vill.Build.CurrentlyBuilding.LastOrDefault().Duration
                        >= DateTime.Now.AddMinutes(vill.Build.InstaBuildMinutes))
                    {
                        acc.Tasks.Add(new InstaUpgrade()
                        {
                            Vill = vill,
                            ExecuteAt = DateTime.Now.AddHours(-1)
                        }, true, vill);
                    }
                },
                // 16: Adventure num
                () => acc.Hero.AdventureNum = HeroParser.GetAdventureNum(html, acc.AccInfo.ServerVersion),
                // 17: status of hero
                () => acc.Hero.Status = HeroParser.HeroStatus(html, acc.AccInfo.ServerVersion),
                // 18: health of hero
                () => acc.Hero.HeroInfo.Health = HeroParser.GetHeroHealth(html, acc.AccInfo.ServerVersion),
                // 19:  Hero:
                () =>
                {
                    bool heroReady = (acc.Hero.HeroInfo.Health > acc.Hero.Settings.MinHealth &&
                                      acc.Hero.Settings.AutoSendToAdventure &&
                                      acc.Hero.Status == Hero.StatusEnum.Home &&
                                      acc.Hero.NextHeroSend < DateTime.Now);

                    var homeVill = HeroHelper.GetHeroHomeVillage(acc);
                    // Update adventures
                    if (homeVill == null)
                    {
                        acc.Tasks.Add(new HeroUpdateInfo()
                        {
                            ExecuteAt = DateTime.Now
                        });
                    }
                    else if (heroReady &&
                             (homeVill.Build.Buildings.Any(x => x.Type == Classificator.BuildingEnum.RallyPoint && 0 < x.Level)) &&
                             (acc.Hero.AdventureNum != acc.Hero.Adventures.Count() || HeroHelper.AdventureInRange(acc)))
                    {
                        // Update adventures
                        acc.Tasks.Add(new StartAdventure()
                        {
                            ExecuteAt = DateTime.Now.AddSeconds(10)
                        }, true);
                    }
                    if (acc.Hero.AdventureNum == 0 && acc.Hero.Settings.BuyAdventures) //for UNL servers, buy adventures
                    {
                        acc.Tasks.Add(new TTWarsBuyAdventure()
                        {
                            ExecuteAt = DateTime.Now.AddSeconds(5)
                        }, true);
                    }
                    if (acc.Hero.Status == Hero.StatusEnum.Dead && acc.Hero.Settings.AutoReviveHero) //if hero is dead, revive him
                    {
                        acc.Tasks.Add(new ReviveHero()
                        {
                            ExecuteAt = DateTime.Now.AddSeconds(5),
                            Vill = AccountHelper.GetHeroReviveVillage(acc)
                        }, true);
                    }
                    if (HeroParser.LeveledUp(html, acc.AccInfo.ServerVersion) && acc.Hero.Settings.AutoSetPoints)
                    {
                        acc.Tasks.Add(new HeroSetPoints()
                        {
                            ExecuteAt = DateTime.Now
                        }, true);
                    }
                },
                // 20: build more storage
                () => AutoExpandStorage(acc, vill),
                // 21: Extend protection
                () => {
                    if (acc.Settings.ExtendProtection &&
                        acc.Wb.Html.GetElementbyId("sidebarBoxInfobox").Descendants("button").Any(x => x.GetAttributeValue("value", "") == "Extend"))
                    {
                        // infoType_25 ?
                        acc.Tasks.Add(new ExtendProtection()
                        {
                            ExecuteAt = DateTime.Now
                        }, true);
                    }
                }
            });
        }