nih... kasih source aja ya...
compile sendiri & download HtmlUnit dulu (library)
loh udah ga bisa attach file ya?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package travianbot;
import java.io.*;
import java.util.*;
import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;
/**
*
* [at] author Administrator
*/
public class Bot {
private static final int DELAY_MILLISECONDS = 2000;
private WebClient client = new WebClient(BrowserVersion.FIREFOX_2);
private String server;
private String currentVillageId;
private String currentVillageName;
private String lastPageUrl; // for caching
private HtmlPage lastPage;
public Bot(String server) {
this.server = server;
}
// TRAVIAN FUNCTION
public HtmlPage login(String userName, String password) throws IOException {
HtmlPage page = getPage("/login.php");
HtmlForm form = page.getFormByName("snd");
// name of <input> changed every day -_-"
HtmlTextInput inputName = (HtmlTextInput) form.getByXPath("//input[ [at] type='text']").get(0);
HtmlPasswordInput inputPassword = (HtmlPasswordInput) form.getByXPath("//input[ [at] type='password']").get(0);
HtmlImageInput inputLogin = (HtmlImageInput) form.getInputByName("s1");
inputName.setValueAttribute(userName);
inputPassword.setValueAttribute(password);
delay(DELAY_MILLISECONDS);
HtmlPage dorf1 = (HtmlPage) inputLogin.click();
lastPageUrl = "?";
return dorf1; //dorf1.php or login.php(if failed)
}
public void logout() throws IOException {
getPage("/logout.php");
getPage("/login.php?del_cookie");
lastPageUrl = "?";
}
public boolean isLogged(HtmlPage dorf1) {
// check is <div id="lres0"> exists in dorf1.php?
// if not logged, dorf1.php actually contains login.php
boolean result = false;
try {
dorf1.getHtmlElementById("lres0");
result = true;
} catch (ElementNotFoundException ex) {
result = false;
}
return result;
}
public String getCurrentVillageName(HtmlPage dorf1) {
return ((HtmlElement) dorf1.getByXPath("//div[ [at] id='lmid2']/div[1]/h1").get(0)).asText();
}
public List<Village> getVillages(HtmlPage dorf3) {
List<Village> result = new ArrayList<Village>();
List elements = dorf3.getByXPath("//div[ [at] id='lmid2']/p/table/tbody//tr");
for (int i = 2; i < elements.size(); i++) {
HtmlTableRow tr = (HtmlTableRow) elements.get(i);
HtmlAnchor a = (HtmlAnchor) tr.getByXPath("td[1]/a").get(0);
Village v = new Village();
v.id = a.getAttribute("href");
v.id = v.id.substring(v.id.lastIndexOf("=")+1);
v.name = a.asText();
result.add(v);
}
return result;
}
/*
* [at] param page page that contains <div id="lres0">
* eg. dorf1.php, dorf2.php, dorf3.php, karte.php
*/
public String[] getResource(HtmlPage page) {
String wood = page.getElementById("l4").getTextContent();
String clay = page.getElementById("l3").getTextContent();
String iron = page.getElementById("l2").getTextContent();
String crop = page.getElementById("l1").getTextContent();
String cropUsage = ((HtmlElement) page.getByXPath("//div[ [at] id='lres0']/table/tbody/tr/td[9]").get(0)).asText();
return new String[] { wood, clay, iron, crop, cropUsage };
}
public String[] getProduction(HtmlPage dorf1) {
String wood = ((HtmlElement) dorf1.getByXPath("//div[ [at] id='lrpr']/table/tbody/tr[1]/td[3]/b").get(0)).asText();
String clay = ((HtmlElement) dorf1.getByXPath("//div[ [at] id='lrpr']/table/tbody/tr[2]/td[3]/b").get(0)).asText();
String iron = ((HtmlElement) dorf1.getByXPath("//div[ [at] id='lrpr']/table/tbody/tr[3]/td[3]/b").get(0)).asText();
String crop = ((HtmlElement) dorf1.getByXPath("//div[ [at] id='lrpr']/table/tbody/tr[4]/td[3]/b").get(0)).asText();
return new String[] { wood, clay, iron, crop };
}
// MISC FUNCTION
public HtmlPage getPage(String page) throws IOException {
if (lastPageUrl != null && lastPageUrl.equals(page)) {
return lastPage;
} else {
delay(DELAY_MILLISECONDS);
HtmlPage target = (HtmlPage) client.getPage("http://"+server+"/" +page);
lastPageUrl = page;
lastPage = target;
return target;
}
}
public static void delay(int milliseconds) {
try {
long now = System.currentTimeMillis();
long end = now + milliseconds;
// repeat if too fast
do {
Thread.currentThread().sleep(end-now);
now = System.currentTimeMillis();
} while (now < end);
} catch (InterruptedException ex) {
System.out.println("Interrupted");
}
}
}