package net.traviangui.hostInterface;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import net.traviangui.Translator;
import net.traviangui.Util;
import net.traviangui.model.Player;
import net.traviangui.model.ResourceType;
import net.traviangui.model.Terrain;
import net.traviangui.model.Village;
import net.traviangui.model.MapSquareType;
import net.traviangui.model.Villages;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
public class IOVillage
{
public static void updateVillageOverview( Village village) throws Exception
{
HtmlCleaner cleaner = new HtmlCleaner();
// CleanerProperties props = cleaner.getProperties();
// props.setRecognizeUnicodeChars( true);
TagNode node = cleaner.clean( new File("tests/overview3.html"));
updateTerrains(village, node);
updateStorage( village, node);
updateProductionRate( village, node);
updateServerTimestamp( village, node);
updateConstructionQueue( village, node);
}
static private void updateServerTimestamp( Village village, TagNode node) throws Exception
{
String strServerTime = node.evaluateXPath( "//span[@id='tp1']/text()")[0].toString( );
// System.out.printf( "Time=%s\n", serverTimeString);
// Calendar localTime = new GregorianCalendar( );
// localTime.set(Calendar.YEAR, 1970);
// localTime.set(Calendar.MONTH, Calendar.JANUARY);
// localTime.set(Calendar.DAY_OF_MONTH, 1);
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");;
Date serverTime = timeFormat.parse( strServerTime);
long serverTimeDelta = new Date().getTime() - serverTime.getTime();
// TODO village.setOverviewServerTimestamp( new ServerTimestamp( serverTime, serverTimeDelta));
// System.out.printf( "Time=%s, diff=%d\n", serverTimeString, serverTimeMillisDelta);
}
private static void updateTerrains( Village village, TagNode node) throws Exception
{
Object nodes[] = node.evaluateXPath( "//div[@class='village1']/map/area[@alt!='']");
List<Terrain> terrains = new ArrayList<Terrain>();
for( Object o : nodes) {
if( o instanceof TagNode) {
TagNode n = (TagNode) o;
String title = n.getAttributeByName( "title");
// if( title.contains( levelKey)) {
String[] data = Util.patternExtract( "([^(]+) "+Translator.get(Translator.LEVEL)+" (\\d+)", title, 2);
String terrainType = Translator.translate( data[0].trim());
int level = Integer.parseInt( data[1].trim());
// System.out.println( terrainType);
Terrain terrain = new Terrain(
new URL( n.getAttributeByName( "href")), terrainType, level);
terrains.add( terrain);
// System.out.println( terrain);
// System.out.println( resourceName);
// }
}
}
village.setTerrains( terrains);
}
static private void updateStorage( Village village, TagNode node) throws Exception
{
// node = node.evaluateXPath( "//div[@id='res']//td[@id='14']");
Object[] values = node.evaluateXPath( "//div[@id='res']//td/text()");
Map<ResourceType,Integer> currentStorage = new Hashtable<ResourceType,Integer>();
Map<ResourceType,Integer> maximumStorage = new Hashtable<ResourceType,Integer>();
String[] wood = Util.patternExtract( "(\\d+)/(\\d+)", values[1].toString(), 2);
String[] clay = Util.patternExtract( "(\\d+)/(\\d+)", values[3].toString(), 2);
String[] iron = Util.patternExtract( "(\\d+)/(\\d+)", values[5].toString(), 2);
String[] crop = Util.patternExtract( "(\\d+)/(\\d+)", values[7].toString(), 2);
currentStorage.put( ResourceType.WOOD, Integer.parseInt( wood[0]));
currentStorage.put( ResourceType.CLAY, Integer.parseInt( clay[0]));
currentStorage.put( ResourceType.IRON, Integer.parseInt( iron[0]));
currentStorage.put( ResourceType.CROP, Integer.parseInt( crop[0]));
maximumStorage.put( ResourceType.WOOD, Integer.parseInt( wood[1]));
maximumStorage.put( ResourceType.CLAY, Integer.parseInt( clay[1]));
maximumStorage.put( ResourceType.IRON, Integer.parseInt( iron[1]));
maximumStorage.put( ResourceType.CROP, Integer.parseInt( crop[1]));
// System.out.printf( "wood=%s, clay=%s, iron=%s, crop=%s\n", wood, clay, iron, crop);
village.setCurrentStorage( currentStorage);
village.setMaximumStorage( maximumStorage);
}
static private void updateProductionRate( Village village, TagNode node) throws Exception
{
Map<ResourceType,Integer> productionRate = new Hashtable<ResourceType,Integer>();
Object[] productionRates = node.evaluateXPath( "//table[@id='production']//td[@class='num']/text()");
if( productionRates.length == 4) {
productionRate.put( ResourceType.WOOD, Integer.parseInt( productionRates[0].toString()));
productionRate.put( ResourceType.CLAY, Integer.parseInt( productionRates[1].toString()));
productionRate.put( ResourceType.IRON, Integer.parseInt( productionRates[2].toString()));
productionRate.put( ResourceType.CROP, Integer.parseInt( productionRates[3].toString()));
}
village.setProductionRate( productionRate);
}
static private void updateConstructionQueue( Village village, TagNode node) throws Exception
{
Object[] artifacts = node.evaluateXPath( "//tr[.//img/@class='del']");
ArrayList<ArrayList<Object>> constructionQueue = new ArrayList<ArrayList<Object>>( );
for( Object o : artifacts) {
if( o instanceof TagNode) {
TagNode n = (TagNode) o;
String desc = n.evaluateXPath( "td[position()=2]/text()")[0].toString();
String time = n.evaluateXPath( "td[position()=3]/span/text()")[0].toString();
String ready = n.evaluateXPath( "td[position()=4]/text()")[0].toString();
// System.out.printf( "Desc='%s', time='%s', ready='%s'\n", desc, time, ready);
String[] data = Util.patternExtract( "([^(]+)\\("+Translator.get(Translator.LEVEL)+" (\\d+)\\)", desc, 2);
String artifact = Translator.translate( data[0].trim());
int level = Integer.parseInt( data[1].trim());
data = Util.patternExtract( "(\\d+:\\d+)", ready, 1);
ready = data[0];
ArrayList<Object> construction = new ArrayList<Object>( );
construction.add( artifact);
construction.add( level);
construction.add( time);
construction.add( ready);
constructionQueue.add( construction);
// System.out.printf( "artifact='%s', level='%s', time='%s', ready='%s'\n", artifact, level, time, ready);
}
}
village.setConstructionQueue( constructionQueue);
}
public static void updateMapVillages( Player player) throws Exception
{
// 1. get overview/innerview pantalla actual
String page = HttpServer.getHttpServer().httpGetPage( "http://s3.travian.net/spieler.php?uid=" + player.getId());
Thread.sleep( (long) (1000 + 3000*Math.random()));
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties props = cleaner.getProperties();
props.setRecognizeUnicodeChars( true);
// TagNode node = cleaner.clean( new File("tests/perfil.html"));
TagNode node = cleaner.clean( page);
updateMapVillages2( player, node);
}
public static void updateMapVillages2( Player player, TagNode node) throws Exception
{
// Object[] dom = node.evaluateXPath( "//div[@id='mid']//div[@id='content']/h1/text()");
Object[] domVillageNames = node.evaluateXPath( "//td[@class='nam']/a/text()");
Object[] domVillageUrls = node.evaluateXPath( "//td[@class='nam']/a/@href");
Object[] domHabitants = node.evaluateXPath( "//td[@class='hab']/text()");
Object[] domVillageCoords = node.evaluateXPath( "//td[@class='aligned_coords']/text()");
for( int i=0 ; i<domVillageNames.length ; i++)
{
// String[] data = Util.patternExtract( "\\?newdid=(\\d+)", domVillageUrls[i].toString(), 2);
// int codD = 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( "http://s3.travian.net/" + 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());
player.addVillage( v);
// }
}
}
}