package net.traviangui.hostInterface;
import java.io.File;
import java.util.List;
import java.util.Scanner;
import net.traviangui.Util;
import net.traviangui.model.Village;
import net.traviangui.model.Villages;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
public class IOVillages
{
public static void updateVillages( Villages villages) throws Exception
{
// 1. get overview/innerview pantalla actual
// String page = HttpServer.getHttpServer().httpGetPage( "http://s3.travian.net/dorf1.php");
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties props = cleaner.getProperties();
props.setRecognizeUnicodeChars( true);
TagNode node = cleaner.clean( new File("tests/overview8.html"));
// TagNode node = cleaner.clean( page);
updateVillages2( villages, node);
}
public static void updateVillages2( Villages villages, TagNode node) throws Exception
{
// Object[] dom = node.evaluateXPath( "//div[@id='mid']//div[@id='content']/h1/text()");
Object[] domVillageNames = node.evaluateXPath( "//table[@id='vlist']/tbody//td[@class='link']/a/text()");
// Object[] domVillageNames = node.evaluateXPath( "//div[@id='mid']//div[@id='content']/h1/text()");
Object[] domVillageUrls = node.evaluateXPath( "//table[@id='vlist']/tbody//td[@class='link']/a/@href");
// Object[] domVillageCoordX = node.evaluateXPath( "//table[@id='vlist']/tbody//td[@class='aligned_coords']//div[@class='cox']/text()");
// Object[] domVillageCoordY = node.evaluateXPath( "//table[@id='vlist']/tbody//td[@class='aligned_coords']//div[@class='coy']/text()");
Object[] domVillageCoords = node.evaluateXPath( "//table[@id='vlist']/tbody//td[@class='aligned_coords']/text()");
for( int i=0 ; i<domVillageNames.length ; i++)
{
int id = Integer.parseInt( Util.patternExtract( "\\?newdid=(\\d+)", domVillageUrls[i].toString(), 1)[0]);
if( !villages.exists( id)) {
Village v = new Village( );
v.setName( domVillageNames[i].toString());
// v.setUrl( new URL( domVillageUrls[i].toString()));
v.setId( id);
// v.setCoordX( Integer.parseInt( domVillageCoordX[i].toString().replace( "(", "")));
// v.setCoordY( Integer.parseInt( domVillageCoordY[i].toString().replace( ")", "")));
// System.out.println( domVillageCoords[i].toString());
Scanner s = new Scanner( domVillageCoords[i].toString());
s.useDelimiter( "[ ()|\n\r\f\t]+");
// s.findInLine( "\\s*\\((\\d+)\\s*|\\s*(\\d+)\\)");
// s.findInLine( "\\s*\\((\\d+)\\s*|\\s*(\\d+)\\)");
// String[] data = Util.patternExtract( "\\(([+\\-\\d]+)[^|]*.*|.*([+\\-\\d]+)\\)", domVillageCoords[i].toString(), 2);
//System.out.println( data[0]);
//System.out.println( data[1]);
v.setCoordX( s.nextInt());
v.setCoordY( s.nextInt());
villages.add( v);
}
}
}
}