package net.traviangui.hostInterface;
import java.io.File;
import net.traviangui.Translator;
import net.traviangui.Util;
import net.traviangui.model.TroopType;
import net.traviangui.model.TroopUnit;
import net.traviangui.model.Troops;
import net.traviangui.model.Village;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
public class IOTroops
{
public static Troops getTroops( Village village) throws Exception
{
// 1. goto village
// 2. goto inner village
// 3. goto rally point
HtmlCleaner cleaner = new HtmlCleaner();
TagNode node = cleaner.clean( new File("tests/materias.html"));
int numNode = 0;
Object[] domTroops = node.evaluateXPath( "//div[@id='content']//table");
for( Object t : domTroops) {
if( t instanceof TagNode) {
TagNode domTroop = (TagNode) t;
Object[] domTroopTypes = domTroop.evaluateXPath( ".//tbody[@class='units']/tr[position()=1]/td/img/@title");
Object[] domTroopQuantities = node.evaluateXPath( ".//tbody[@class='units']/tr[position()=2]/td/text()");
Object[] domLocations = node.evaluateXPath( "//div[@id='content']//table/thead/tr/td[2]/a/@href");
String codLocation = "<local>";
if( numNode > 0) { // Other villages
codLocation = Util.patternExtract( "\\?d=(\\d+)&", domLocations[numNode].toString(), 1)[0];
}
numNode++;
System.out.println( "* Village=" + codLocation);
Troops troops = new Troops( village);
// Util.printTagNodes( domTroopTypes);
// Util.printTagNodes( domTroopQuantities);
for( int i=0 ; i < domTroopTypes.length ; i++) {
// System.out.printf( "type=%s, quantity=%d\n", domTroopTypes[i].toString(), troopQuantity);
String troopType = Translator.translate( domTroopTypes[i].toString().trim());
int troopQuantity = Integer.parseInt( domTroopQuantities[i].toString().trim());
TroopUnit tu = new TroopUnit( TroopType.getByName( troopType), troopQuantity);
troops.addTroops( tu);
System.out.println( " * " + tu);
}
}
}
return null;
}
}