public void PlayGame() { using (browser) { Log.InfoFormat("Report reader : {0}, Raid : {1}", report, raid); DefaultPage browserPage = new DefaultPage(browser, server, username, password, defaultVillageId) { DefaultVillageId = 0, }; browserPage.LoginPage().LoginToGame(); isLoggedIn = browserPage.IsLogedIn; if (isLoggedIn) { int repeatCount = 0; int loginCount = 0; Village village = new Village(attackVillageId, attackVillageName); gameData.AddVillage(village); gameData.GameSettings(language); Dorf1 dorf1Page = browserPage.Dorf1Page(gameData); dorf1Page.Village = village; dorf1Page.ClickDorf1Link(); do { if (isLoggedIn) { loginCount = 0; farmList = LoadVillagesFromXml(); if (farmList.Count == 0) { Log.Info("Getting new farm list..."); farmList = dataBase.GetFarmList(); } if (raid) { if (!SleepTime()) { dorf1Page.ClickDorf1Link(); Raid(browserPage); } } if (report) { IReportReader reportReader = browserPage.AttackReport(gameData); reportReader.Collect(); reportReader.Parse(); reportReader.Save(); if (!raid) { int count = 0; foreach (MapCoordinates mapCoordinate in farmList) { Map map = browserPage.MapPage(); if (!map.GetVillageDetails(mapCoordinate)) { dataBase.DeleteVillage(mapCoordinate.VillageId); Log.InfoFormat("Removed village : {0}", mapCoordinate); } else { dataBase.SaveVillageToDb(mapCoordinate); Log.InfoFormat("Updated village : {0}", mapCoordinate); } raidedFarms.Add(mapCoordinate); if (count++ > 5) { break; } } RemoveRaidedFarms(); } } dorf1Page.ClickDorf1Link(); } else { loginCount++; if (!isLoggedIn) { Log.Warn("Not loged in. Sleep for 60 seconds..."); Thread.Sleep(60000); if (loginCount > 3) { Log.Warn("Failed to login!"); break; } } } repeatCount++; if (repeatCount > 100) { repeatCount = 0; } Log.InfoFormat("Sleep for {0}", dorf1Page.NextCheck); Thread.Sleep(dorf1Page.NextCheck); } while (repeatCount < 1000); } else { Log.Warn("Login failed!!!"); } } browser.Dispose(); }
private void RaidFarms(TroopList troopList, DefaultPage browserPage, KeyValuePair<Village, List<TroopList>> unitInVillage, string textBox) { Log.Debug("RaidFarms"); int availableUnits = troopList.Count; foreach (MapCoordinates mapCoordinate in farmList) { Map map = browserPage.MapPage(); if (!map.GetVillageDetails(mapCoordinate)) { dataBase.DeleteVillage(mapCoordinate.VillageId); Log.InfoFormat("Removed village : {0}", mapCoordinate); raidedFarms.Add(mapCoordinate); } else { dataBase.SaveVillageToDb(mapCoordinate); Log.DebugFormat("Updated village : {0}", mapCoordinate); if (mapCoordinate.PlayerEnabled) { Log.InfoFormat("availableUnits : {0}", availableUnits); if (availableUnits >= minUnits4Send) { Random random = new Random(); int randomUnitCount = random.Next(minUnits4Send, maxUnits4Send); if (randomUnitCount > availableUnits) { randomUnitCount = availableUnits; } SendTroops sendTroops = browserPage.SendTroopsPage(); if (sendTroops.Attack(new AttackSettings(mapCoordinate.VillageId, AttackType.Raid, textBox, randomUnitCount.ToString()))) { availableUnits -= randomUnitCount; raidedFarms.Add(mapCoordinate); Log.InfoFormat( "Attacked '{3}' from '{0}' with {1} '{2}'", unitInVillage.Key.Name, randomUnitCount, troopList.TroopClass, mapCoordinate.VillageName); } else { Log.WarnFormat("Attack to '{0}' failed.", mapCoordinate.VillageName); return; } } else { Log.Info("No more units for farming..."); return; } } else { raidedFarms.Add(mapCoordinate); } } } }
private void GetAvailableUnits(KeyValuePair<Village, List<TroopList>> unitInVillage, DefaultPage browserPage) { Log.Debug("GetAvailableUnits"); List<TroopList> troopLists = unitInVillage.Value; foreach (TroopList troopList in troopLists) { Log.DebugFormat("Class of troop : '{0}'", troopList.TroopClass); if (playerFarmUnits.IndexOf(troopList.TroopClass) > -1) { if (troopList.Count >= minUnits4Send) { RaidFarms(troopList, browserPage, unitInVillage, GetUnitTextBox(playerTribe, troopList.TroopClass)); } } } }
private void Raid(DefaultPage browserPage) { Log.DebugFormat("Raid : [{0} troops available]", gameData.TroopList4Village.Count); foreach ( KeyValuePair<Village, List<TroopList>> unitInVillage in gameData.TroopList4Village) { GetAvailableUnits(unitInVillage, browserPage); } RemoveRaidedFarms(); }
private DefaultPage Login() { DefaultPage browserPage = new DefaultPage(browser, server, username, password, defaultVillageId); browserPage.LoginPage().LoginToGame(); isLoggedIn = browserPage.IsLogedIn; return browserPage; }