Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.58 KB | None | 0 0
  1. package vn.vule.travian.selenium;
  2.  
  3. import com.google.common.base.Function;
  4.  
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.io.Serializable;
  8. import java.net.URISyntaxException;
  9. import java.util.ArrayList;
  10. import java.util.Date;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14.  
  15. import org.apache.log4j.Logger;
  16. import org.openqa.selenium.By;
  17. import org.openqa.selenium.Cookie;
  18. import org.openqa.selenium.JavascriptExecutor;
  19. import org.openqa.selenium.WebDriver;
  20. import org.openqa.selenium.WebElement;
  21. import org.openqa.selenium.firefox.FirefoxDriver;
  22. import org.openqa.selenium.support.ui.ExpectedCondition;
  23. import org.openqa.selenium.support.ui.WebDriverWait;
  24.  
  25. /**
  26. * @author vule
  27. */
  28. public class MainZravianAffGauls {
  29. private final static Logger log = Logger.getLogger(MainZravianAffGauls.class.getName());
  30.  
  31. // private final static String USERNAME = "viki";
  32. // private final static String PASSWORD = "888888";
  33. private final static String USERNAME = "aff";
  34. private final static String PASSWORD = "rabbit123";
  35. private final static String VILLAGE_NAME_BUILD = "aff";
  36. private final static int NUMBER_BUILDING = 3;
  37.  
  38. private static WebDriver webDriver;
  39. private static WebDriverWait wait;
  40. private static long timerCulturePoints = 0;
  41.  
  42. private static List<String> villagesBuildResource = new ArrayList<String>();
  43. private static List<String> villagesTownHall = new ArrayList<String>();
  44.  
  45. public static void main(String[] args) throws InterruptedException, IOException, URISyntaxException {
  46. initialize();
  47. processLogin("https://ss4.zravian.com", USERNAME, PASSWORD);
  48. // show level is ON
  49. Cookie cookie = new Cookie("lvl", "1", "/", new Date(2017, 12, 31));
  50. webDriver.manage().addCookie(cookie);
  51. log.debug("villagesBuildResource length: " + villagesBuildResource.size());
  52. if (villagesBuildResource.size() > 0) {
  53. boolean relogin = false;
  54. while (true) {
  55. try {
  56. if (relogin) {
  57. processLogin("https://ss4.zravian.com", USERNAME, PASSWORD);
  58. }
  59.  
  60. for (String hrefVillage : villagesBuildResource) {
  61. webDriver.navigate().to(hrefVillage);
  62. if (checkBuilding()) continue;
  63. processClickOne();
  64. webDriver.navigate().to("https://ss4.zravian.com/village1.php");
  65. processUpgradeResource();
  66. Thread.sleep(500);
  67. processUpgradeResource();
  68. if (checkBuilding()) continue;
  69. webDriver.navigate().to("https://ss4.zravian.com/village2.php");
  70. log.debug("Processing construction to map for vilage - " + hrefVillage);
  71. Map<Integer, VillageBuild> map = processConstruction2Map();
  72. processUpgradeConstructionFull(map);
  73. log.debug("Sleeping 0.5s ..... for the next village");
  74. Thread.sleep(500);
  75. }
  76.  
  77. if ((villagesTownHall != null) && (!villagesTownHall.isEmpty())) {
  78. for (String hrefVillage : villagesTownHall) {
  79. webDriver.navigate().to(hrefVillage);
  80. try {
  81. WebElement lswitch = webDriver.findElement(By.id("lswitch"));
  82. if (!lswitch.getAttribute("class").equals("on")) {
  83. lswitch.click();
  84. }
  85. } catch (Exception e) {
  86. log.error("ERROR: " + e.getMessage());
  87. e.printStackTrace();
  88. }
  89. processCulturePoints();
  90. // processUpgradeResidenceIfNotExist();
  91. }
  92. // }
  93. }
  94. log.debug("Sleeping 1s ..... for the next cycle");
  95. Thread.sleep(1000);
  96. } catch (Exception e) {
  97. log.debug("ERROR: " + e.getMessage());
  98. e.printStackTrace();
  99. String currentUrl = "";
  100. try {
  101. webDriver.getCurrentUrl();
  102. } catch (Exception ex) {
  103. ex.printStackTrace();
  104. }
  105. log.debug("CurrentUrl: " + currentUrl);
  106. if (currentUrl.isEmpty() || currentUrl.contains("https://zravian.com/?nosess")) {
  107. log.debug("Sleeping 6s ..... for relogin");
  108. Thread.sleep(6000);
  109. relogin = true;
  110. } else {
  111. log.debug("Sleeping 10m ..... for [ERROR]");
  112. Thread.sleep(600000);
  113. }
  114. }
  115. }
  116. }
  117. else {
  118. while (true) {
  119. webDriver.navigate().to("https://ss4.zravian.com/village2.php");
  120. try {
  121. WebElement lswitch = webDriver.findElement(By.id("lswitch"));
  122. if (!lswitch.getAttribute("class").equals("on")) {
  123. lswitch.click();
  124. }
  125. } catch (Exception e) {
  126. log.error("ERROR: " + e.getMessage());
  127. e.printStackTrace();
  128. }
  129. processCulturePoints();
  130. log.debug("Sleeping 72s ..... for the next cycle");
  131. Thread.sleep(72000);
  132. }
  133. }
  134. }
  135.  
  136. private static void waitForPageLoadIsReadyState(final String expectedTitle) {
  137. wait.until(new Function<WebDriver, Boolean>() {
  138. public Boolean apply(WebDriver driver) {
  139. String actualTitle = webDriver.getTitle();
  140. boolean readyState = String.valueOf(((JavascriptExecutor) driver).executeScript("return document.readyState")).equals("complete");
  141. boolean loginPageReady = actualTitle.contains(expectedTitle);
  142. return loginPageReady && readyState;
  143. }
  144. });
  145. }
  146.  
  147. private static void processClickOne() {
  148. processClickOne(true);
  149. }
  150.  
  151. private static void processClickOne(final boolean clickOne) {
  152. // alt="Click here to disable Upgrade Mode"
  153. // title="Click here to disable Upgrade Mode"
  154. final WebElement cone = webDriver.findElement(By.id("cone"));
  155. String titleCone = cone.getAttribute("title");
  156. // log.debug("title cone: " + titleCone);
  157. if (clickOne) {
  158. if (titleCone.contains("enable")) {
  159. // log.debug("cone is disable - click to enable");
  160. cone.click();
  161. }
  162. } else {
  163. if (titleCone.contains("disable")) {
  164. // log.debug("cone is enable - click to disable");
  165. cone.click();
  166. }
  167. }
  168. }
  169.  
  170. private static void processUpgradeResource() {
  171. if (webDriver.findElements(By.id("content")).size() > 0) {
  172. log.debug("Village Name: " + webDriver.findElement(By.id("content")).findElement(By.tagName("h1")).getText());
  173. }
  174. if (webDriver.findElements(By.id("rx")).isEmpty()) {
  175. log.error("Don't processUpgradeResource");
  176. return;
  177. }
  178. final WebElement maprx = webDriver.findElement(By.id("rx"));
  179. final List<WebElement> rxlist = maprx.findElements(By.tagName("area"));
  180. if ((rxlist != null) && (rxlist.size() > 0)) {
  181. String href = null;
  182. int minLevel = 10;
  183. for (WebElement element : rxlist) {
  184. String title = element.getAttribute("title");
  185. // log.debug("Map Resource: " + title + " - " + element.getAttribute("href"));
  186. int index = title.indexOf("level");
  187. if (index > 0) {
  188. // String res = title.substring(0, index -1);
  189. int level = -1;
  190. try {
  191. level = Integer.valueOf(title.substring(index + 6, title.length()));
  192. } catch (java.lang.NumberFormatException nfe) {
  193. }
  194. if ((level >= 0) && (level < minLevel)) {
  195. minLevel = level;
  196. href = element.getAttribute("href");
  197. }
  198. }
  199. }
  200. if ((minLevel < 10) && (href != null)) {
  201. webDriver.navigate().to(href);
  202. }
  203. }
  204. }
  205.  
  206. private static void initialize() throws URISyntaxException {
  207. String path = MainZravianAffGauls.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
  208. File jarFile = new File(path);
  209. String jarDir = jarFile.getParentFile().getPath();
  210. log.debug("DIR PATH: " + jarDir);
  211. System.setProperty("webdriver.gecko.driver", jarDir + java.io.File.separator + "geckodriver");
  212. System.setProperty("webdriver.chrome.driver", jarDir + java.io.File.separator + "chromedriver");
  213. webDriver = new FirefoxDriver();
  214. wait = new WebDriverWait(webDriver, 120);
  215. }
  216.  
  217. private static void processLogin(final String url, final String login, final String pwd) {
  218. log.debug("processLogin: login:" + login + " - pwd: " + pwd);
  219. webDriver.navigate().to("https://ss4.zravian.com/logout.php");
  220. webDriver.get(url);
  221. try {
  222. waitForPageLoadIsReadyState("Zravian");
  223. } catch (Exception e) {
  224. log.debug("[ERROR]", e);
  225. }
  226. WebElement username = webDriver.findElement(By.id("name"));
  227. WebElement password = webDriver.findElement(By.id("pass"));
  228. username.sendKeys(login);
  229. password.sendKeys(pwd);
  230. password.submit();
  231. try {
  232. waitForPageLoadIsReadyState("Zravian ss4");
  233. } catch (Exception e) {
  234. log.error("[ERROR]", e);
  235. throw e;
  236. }
  237. processBuildAllVillage();
  238. }
  239.  
  240. private static void processBuildAllVillage() {
  241. villagesBuildResource = new ArrayList<String>();
  242. villagesTownHall = new ArrayList<String>();
  243. WebElement vlist = null;
  244. try {
  245. vlist = webDriver.findElement(By.id("vlist"));
  246. } catch (Exception e) {
  247.  
  248. }
  249. if (vlist != null) {
  250. List<WebElement> links = vlist.findElements(By.tagName("a"));
  251. if ((links != null) && (links.size() > 0)) {
  252. for (WebElement element : links) {
  253. String href = element.getAttribute("href");
  254. String name = element.getText();
  255. if (name == null) continue;
  256. log.debug("Name: " + name + " - link: " + href);
  257. if (name.contains(VILLAGE_NAME_BUILD)) {
  258. //villagesBuildResource.add(href.replaceAll("village1.php", "village2.php"));
  259. villagesBuildResource.add(href);
  260. villagesTownHall.add(href.replaceAll("village1.php", "village2.php"));
  261. }
  262. }
  263. }
  264. }
  265. }
  266.  
  267. private static boolean checkBuilding() {
  268. boolean isBuilding = false;
  269. if (webDriver.findElements(By.id("building_contract")).size() > 0) {
  270. final WebElement buildingContract = webDriver.findElement(By.id("building_contract"));
  271. if (buildingContract != null) {
  272. final List<WebElement> buildlinks = webDriver.findElements(By.className("buildlink"));
  273. if (buildlinks.size() > 0) {
  274. log.debug("Building ... " + buildlinks.size());
  275. if (buildlinks.size() >= NUMBER_BUILDING) {
  276. isBuilding = true;
  277. }
  278. }
  279. }
  280. } else {
  281. log.debug("NONE Building ...");
  282. }
  283. return isBuilding;
  284. }
  285.  
  286. private static boolean checkConstructionBuilding(final String name) {
  287. boolean isBuilding = false;
  288. if (webDriver.findElements(By.id("building_contract")).size() > 0) {
  289. final WebElement buildingContract = webDriver.findElement(By.id("building_contract"));
  290. if (buildingContract != null) {
  291. final List<WebElement> buildlinks = webDriver.findElements(By.className("buildlink"));
  292. if (buildlinks.size() > 0) {
  293. WebElement buildlink = buildlinks.get(0);
  294. if (buildlink.getText().contains(name)) {
  295. log.debug("Building construction... " + name);
  296. isBuilding = true;
  297. }
  298. }
  299. }
  300. } else {
  301. log.debug("NONE Building ...");
  302. }
  303. return isBuilding;
  304. }
  305.  
  306. private static void processUpgradeConstruction(final Map<Integer, VillageBuild> map) {
  307. if ((map == null) || map.isEmpty()) {
  308. log.error("Don't processUpgradeConstruction");
  309. return;
  310. }
  311.  
  312. if (processUpgradeConstruction(map, "Main Building", 10)) {
  313. return;
  314. }
  315. if (processUpgradeConstruction(map, "Warehouse", 7)) {
  316. return;
  317. }
  318. if (processUpgradeConstruction(map, "Great Warehouse", 7)) {
  319. return;
  320. }
  321. if (processUpgradeConstruction(map, "Granary", 5)) {
  322. return;
  323. }
  324. if (processUpgradeConstruction(map, "Great Granary", 5)) {
  325. return;
  326. }
  327. if (processUpgradeConstruction(map, "Rally Point", 1)) {
  328. return;
  329. }
  330. if (processUpgradeConstruction(map, "Flour Mill", 5)) {
  331. return;
  332. }
  333. if (processUpgradeConstruction(map, "Brickworks", 5)) {
  334. return;
  335. }
  336. if (processUpgradeConstruction(map, "Bakery", 5)) {
  337. return;
  338. }
  339. if (processUpgradeConstruction(map, "Sawmill", 5)) {
  340. return;
  341. }
  342. if (processUpgradeConstruction(map, "Iron Foundry", 5)) {
  343. return;
  344. }
  345. }
  346.  
  347. private static void processUpgradeConstructionFull(final Map<Integer, VillageBuild> map) {
  348. if ((map == null) || map.isEmpty()) {
  349. log.error("Don't processUpgradeConstruction");
  350. return;
  351. }
  352.  
  353. if (processUpgradeConstruction(map, "Main Building")) return;
  354. if (processUpgradeConstruction(map, "Warehouse")) return;
  355. if (processUpgradeConstruction(map, "Great Warehouse")) return;
  356. if (processUpgradeConstruction(map, "Granary")) return;
  357. if (processUpgradeConstruction(map, "Great Granary")) return;
  358. if (processUpgradeConstruction(map, "Rally Point", 1)) return;
  359. if (processUpgradeConstruction(map, "Residence", 10)) return;
  360. if (processUpgradeConstruction(map, "Barracks")) return;
  361. if (processUpgradeConstruction(map, "Academy", 10)) return;
  362. if (processUpgradeConstruction(map, "Blacksmith", 10)) return;
  363. if (processUpgradeConstruction(map, "Stable")) return;
  364. if (processUpgradeConstruction(map, "Town Hall", 10)) return;
  365.  
  366. if (processUpgradeConstruction(map, "Residence")) return;
  367. if (processUpgradeConstruction(map, "Marketplace")) return;
  368. if (processUpgradeConstruction(map, "Academy")) return;
  369. if (processUpgradeConstruction(map, "Town Hall")) return;
  370. if (processUpgradeConstruction(map, "Trade Office")) return;
  371. }
  372.  
  373. private static void processUpgradeConstruction() {
  374. if (webDriver.findElements(By.id("map2")).isEmpty()) {
  375. log.error("Don't processUpgradeConstruction");
  376. return;
  377. }
  378.  
  379. if (processUpgradeConstruction("Main Building")) return;
  380. if (processUpgradeConstruction("Warehouse")) return;
  381. if (processUpgradeConstruction("Granary")) return;
  382. if (processUpgradeConstruction("Residence")) return;
  383. if (processUpgradeConstruction("Marketplace")) return;
  384. if (processUpgradeConstruction("Academy")) return;
  385. if (processUpgradeConstruction("Rally Point", 10)) return;
  386. if (processUpgradeConstruction("Barracks")) return;
  387. if (processUpgradeConstruction("Stable")) return;
  388. if (processUpgradeConstruction("Town Hall")) return;
  389. if (processUpgradeConstruction("Flour Mill", 5)) return;
  390. if (processUpgradeConstruction("Brickworks", 5)) return;
  391. if (processUpgradeConstruction("Bakery", 5)) return;
  392. if (processUpgradeConstruction("Sawmill", 5)) return;
  393. if (processUpgradeConstruction("Iron Foundry", 5)) return;
  394. }
  395.  
  396. private static boolean processUpgradeConstruction(final Map<Integer, VillageBuild> map, final String name) {
  397. boolean ret = false;
  398. if ((map == null) || map.isEmpty()) {
  399. log.error("Don't processUpgradeConstruction");
  400. return false;
  401. }
  402. for (Map.Entry m : map.entrySet()) {
  403. VillageBuild v = (VillageBuild) m.getValue();
  404. if (v.constName.contains(name) && v.level < 20) {
  405. webDriver.navigate().to(v.getHref());
  406. if (checkConstructionBuilding(name)) {
  407. ret = true;
  408. break;
  409. }
  410. }
  411. }
  412. return ret;
  413. }
  414.  
  415. private static boolean processUpgradeConstruction(final String name) {
  416. boolean ret = false;
  417. if (webDriver.findElements(By.id("map2")).isEmpty()) {
  418. log.error("Don't processUpgradeMainBuilding");
  419. return false;
  420. }
  421.  
  422. final WebElement map2 = webDriver.findElement(By.id("map2"));
  423. final List<WebElement> map2AreaList = map2.findElements(By.tagName("area"));
  424. if ((map2AreaList != null) && (map2AreaList.size() > 0)) {
  425. for (WebElement element : map2AreaList) {
  426. String title = null;
  427. try {
  428. title = element.getAttribute("title");
  429. } catch (Exception e) {
  430. log.debug("[ERROR] processUpgradeConstruction - CurrentUrl:" + webDriver.getCurrentUrl());
  431. log.debug("Element: " + element.toString());
  432. log.debug("Construction: " + name);
  433. e.printStackTrace();
  434. return false;
  435. }
  436. String href = element.getAttribute("href");
  437. // System.out.println("Map Constructions: " + title + " - " + href);
  438. int index = title.indexOf("level");
  439. if (index > 0) {
  440. String n = title.substring(0, index - 1);
  441. int level = -1;
  442. try {
  443. level = Integer.valueOf(title.substring(index + 6, title.length()));
  444. } catch (Exception e) {
  445. System.out.println("ERROR:" + e.getMessage());
  446. e.printStackTrace();
  447. return false;
  448. }
  449. if ((level < 20) && n.equals(name)) {
  450. webDriver.navigate().to(href);
  451. if (checkConstructionBuilding(name)) {
  452. ret = true;
  453. break;
  454. } else {
  455. ret = false;
  456. break;
  457. }
  458. }
  459. }
  460. }
  461. }
  462. return ret;
  463. }
  464.  
  465. private static boolean processUpgradeConstruction(final Map<Integer, VillageBuild> map, final String name, final int levelExpectation) {
  466. boolean ret = false;
  467. if ((map == null) || map.isEmpty()) {
  468. log.error("Don't processUpgradeConstruction");
  469. return false;
  470. }
  471. for (Map.Entry m : map.entrySet()) {
  472. VillageBuild v = (VillageBuild) m.getValue();
  473. if (v.constName.contains(name) && v.level < levelExpectation) {
  474. webDriver.navigate().to(v.getHref());
  475. if (checkConstructionBuilding(name)) {
  476. ret = true;
  477. break;
  478. }
  479. }
  480. }
  481. return ret;
  482. }
  483.  
  484. private static boolean processUpgradeConstruction(final String name, final int levelExpectation) {
  485. boolean ret = false;
  486. if (webDriver.findElements(By.id("map2")).isEmpty()) {
  487. log.error("Don't processUpgradeMainBuilding");
  488. return false;
  489. }
  490.  
  491. final WebElement map2 = webDriver.findElement(By.id("map2"));
  492. final List<WebElement> map2AreaList = map2.findElements(By.tagName("area"));
  493. if ((map2AreaList != null) && (map2AreaList.size() > 0)) {
  494. for (WebElement element : map2AreaList) {
  495. String title = null;
  496. try {
  497. title = element.getAttribute("title");
  498. } catch (Exception e) {
  499. log.debug("[ERROR] processUpgradeConstruction - CurrentUrl:" + webDriver.getCurrentUrl());
  500. log.debug(element.toString());
  501. e.printStackTrace();
  502. return false;
  503. }
  504. String href = element.getAttribute("href");
  505. // System.out.println("Map Constructions: " + title + " - " + href);
  506. int index = title.indexOf("level");
  507. if (index > 0) {
  508. String n = title.substring(0, index - 1);
  509. int level = -1;
  510. try {
  511. level = Integer.valueOf(title.substring(index + 6, title.length()));
  512. } catch (Exception e) {
  513. System.out.println("ERROR:" + e.getMessage());
  514. e.printStackTrace();
  515. return false;
  516. }
  517. if ((level < levelExpectation) && n.equals(name)) {
  518. webDriver.navigate().to(href);
  519. if (checkConstructionBuilding(name)) {
  520. ret = true;
  521. break;
  522. }
  523. }
  524. }
  525. }
  526. }
  527. return ret;
  528. }
  529.  
  530. private static boolean processUpgradeResidenceIfNotExist() {
  531. boolean ret = false;
  532. if (webDriver.findElements(By.id("map2")).isEmpty()) {
  533. log.error("Don't processUpgradeMainBuilding");
  534. return false;
  535. }
  536.  
  537. final WebElement map2 = webDriver.findElement(By.id("map2"));
  538. final List<WebElement> map2AreaList = map2.findElements(By.tagName("area"));
  539. if ((map2AreaList != null) && (map2AreaList.size() > 0)) {
  540. boolean residenceExist = false;
  541. String href2Build = "";
  542. for (WebElement element : map2AreaList) {
  543. String title = null;
  544. try {
  545. title = element.getAttribute("title");
  546. } catch (Exception e) {
  547. log.debug("[ERROR] processUpgradeConstruction - CurrentUrl:" + webDriver.getCurrentUrl());
  548. log.debug(element.toString());
  549. e.printStackTrace();
  550. return false;
  551. }
  552. String href = element.getAttribute("href");
  553. // System.out.println("Map Constructions: " + title + " - " + href);
  554. if (title.equals("Empty place")) {
  555. href2Build = href;
  556. }
  557.  
  558. int index = title.indexOf("level");
  559. if (index > 0) {
  560. String n = title.substring(0, index - 1);
  561. if (n.equals("Residence")) {
  562. residenceExist = true;
  563. href2Build = href;
  564. break;
  565. }
  566. }
  567. }
  568. if (!residenceExist && !href2Build.isEmpty()) { // not exist
  569. webDriver.navigate().to(href2Build);
  570. if (!webDriver.findElements(By.id("build")).isEmpty()) {
  571. WebElement divBuild = webDriver.findElement(By.id("build"));
  572. List<WebElement> h2WebElement = divBuild.findElements(By.tagName("h2"));
  573. List<WebElement> newBuildingWebElement = divBuild.findElements(By.className("new_building"));
  574. System.out.println("h2WebElement size: " + h2WebElement.size());
  575. System.out.println("newBuildingWebElement size: " + newBuildingWebElement.size());
  576. for (int i = 0; i < h2WebElement.size(); i++) {
  577. if (h2WebElement.get(i).getText().equals("Residence")) {
  578. if (!newBuildingWebElement.get(i).findElements(By.className("build")).isEmpty()) {
  579. newBuildingWebElement.get(i).findElement(By.className("build")).click();
  580. ret = true;
  581. break;
  582. }
  583. }
  584. }
  585. }
  586. }
  587. }
  588. return ret;
  589. }
  590.  
  591. private static boolean processUpgradeConstruction(final String name, final int levelExpectation, final boolean recursive) {
  592. boolean ret = false;
  593. if (webDriver.findElements(By.id("map2")).isEmpty()) {
  594. log.error("Don't processUpgradeMainBuilding");
  595. return false;
  596. }
  597.  
  598. final WebElement map2 = webDriver.findElement(By.id("map2"));
  599. final List<WebElement> map2AreaList = map2.findElements(By.tagName("area"));
  600. if ((map2AreaList != null) && (map2AreaList.size() > 0)) {
  601. for (WebElement element : map2AreaList) {
  602. String title = null;
  603. try {
  604. title = element.getAttribute("title");
  605. } catch (Exception e) {
  606. e.printStackTrace();
  607. return false;
  608. }
  609. String href = element.getAttribute("href");
  610. // System.out.println("Map Constructions: " + title + " - " + href);
  611. int index = title.indexOf("level");
  612. if (index > 0) {
  613. String n = title.substring(0, index - 1);
  614. int level = -1;
  615. try {
  616. level = Integer.valueOf(title.substring(index + 6, title.length()));
  617. } catch (Exception e) {
  618. System.out.println("ERROR:" + e.getMessage());
  619. e.printStackTrace();
  620. return false;
  621. }
  622. if ((level < levelExpectation) && n.equals(name)) {
  623. webDriver.navigate().to(href);
  624. if (checkConstructionBuilding(name)) {
  625. ret = true;
  626. break;
  627. }
  628. }
  629. }
  630. }
  631. }
  632. return ret;
  633. }
  634.  
  635. private static void processCulturePoints() {
  636. processClickOne(false);
  637. final WebElement map2 = webDriver.findElement(By.id("map2"));
  638. final List<WebElement> map2AreaList = map2.findElements(By.tagName("area"));
  639. if ((map2AreaList != null) && (map2AreaList.size() > 0)) {
  640. for (WebElement element : map2AreaList) {
  641. String title = element.getAttribute("title");
  642. String href = element.getAttribute("href");
  643. // System.out.println("Map Constructions: " + title + " - " + href);
  644. int index = title.indexOf("level");
  645. if (index > 0) {
  646. String n = title.substring(0, index - 1);
  647. int level = -1;
  648. try {
  649. level = Integer.valueOf(title.substring(index + 6, title.length()));
  650. } catch (Exception e) {
  651. System.out.println("ERROR:" + e.getMessage());
  652. if (!n.equals("Town Hall")) {
  653. continue;
  654. }
  655. e.printStackTrace();
  656. break;
  657. }
  658. // Main Building
  659. if ((level == 20) && n.equals("Town Hall")) {
  660. webDriver.navigate().to(href);
  661. if (!webDriver.findElements(By.id("build")).isEmpty()) {
  662. WebElement divBuild = webDriver.findElement(By.id("build"));
  663. if (!divBuild.findElements(By.className("build")).isEmpty()) {
  664. List<WebElement> listLinkBuild = divBuild.findElements(By.className("build"));
  665. if ((listLinkBuild != null) && !listLinkBuild.isEmpty() && listLinkBuild.size() == 2) {
  666. WebElement linkBuild = listLinkBuild.get(1);
  667. // log.debug("href: " + linkBuild.getAttribute("href"));
  668. linkBuild.click();
  669. return;
  670. }
  671. }
  672. }
  673. return;
  674. }
  675. }
  676. }
  677. }
  678. }
  679.  
  680. private static void waitForLoad(WebDriver driver) {
  681. ExpectedCondition<Boolean> pageLoadCondition = new ExpectedCondition<Boolean>() {
  682. public Boolean apply(WebDriver driver) {
  683. return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
  684. }
  685. };
  686. WebDriverWait wait = new WebDriverWait(driver, 30);
  687. wait.until(pageLoadCondition);
  688. }
  689.  
  690. private static Map<Integer, VillageBuild> processConstruction2Map() {
  691. Map<Integer, VillageBuild> map = new HashMap<Integer, VillageBuild>();
  692. if (webDriver.findElements(By.id("map2")).isEmpty()) {
  693. log.error("Don't processUpgradeMainBuilding");
  694. return null;
  695. }
  696.  
  697. final WebElement map2 = webDriver.findElement(By.id("map2"));
  698. final List<WebElement> map2AreaList = map2.findElements(By.tagName("area"));
  699. if ((map2AreaList != null) && (map2AreaList.size() > 0)) {
  700. int slot = 19;
  701. for (WebElement element : map2AreaList) {
  702. String title = null;
  703. try {
  704. title = element.getAttribute("title");
  705. } catch (Exception e) {
  706. e.printStackTrace();
  707. continue;
  708. }
  709. String href = element.getAttribute("href");
  710. // log.debug("Map Constructions: " + title + " - " + href);
  711. if ((title != null) && !title.equals("Empty place")) {
  712. int indexOfLevel = title.indexOf("level");
  713. if (indexOfLevel > 0) {
  714. String n = title.substring(0, indexOfLevel - 1);
  715. int level = -1;
  716. try {
  717. level = Integer.valueOf(title.substring(indexOfLevel + 6, title.length()));
  718. } catch (Exception e) {
  719. // System.out.println("ERROR:" + e.getMessage());
  720. // e.printStackTrace();
  721. }
  722. VillageBuild village = new VillageBuild();
  723. village.setSlot(slot);
  724. village.setLevel(level);
  725. village.setConstName(title);
  726. village.setHref(href);
  727. map.put(slot, village);
  728. }
  729. } else {
  730. VillageBuild village = new VillageBuild();
  731. village.setSlot(slot);
  732. village.setLevel(-1);
  733. village.setConstName(title);
  734. village.setHref(href);
  735. map.put(slot, village);
  736. }
  737. slot++;
  738. }
  739. }
  740. return map;
  741. }
  742.  
  743. static class VillageBuild implements Serializable {
  744.  
  745. /**
  746. * @return the href
  747. */
  748. public String getHref() {
  749. return href;
  750. }
  751.  
  752. /**
  753. * @param href the href to set
  754. */
  755. public void setHref(String href) {
  756. this.href = href;
  757. }
  758.  
  759. /**
  760. * @return the slot
  761. */
  762. public int getSlot() {
  763. return slot;
  764. }
  765.  
  766. /**
  767. * @param slot the slot to set
  768. */
  769. public void setSlot(int slot) {
  770. this.slot = slot;
  771. }
  772.  
  773. /**
  774. * @return the constName
  775. */
  776. public String getConstName() {
  777. return constName;
  778. }
  779.  
  780. /**
  781. * @param constName the constName to set
  782. */
  783. public void setConstName(String constName) {
  784. this.constName = constName;
  785. }
  786.  
  787. /**
  788. * @return the level
  789. */
  790. public int getLevel() {
  791. return level;
  792. }
  793.  
  794. /**
  795. * @param level the level to set
  796. */
  797. public void setLevel(int level) {
  798. this.level = level;
  799. }
  800.  
  801. private int slot;
  802. private String constName;
  803. private int level;
  804. private String href;
  805. }
  806. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement