View Javadoc

1   package org.inigma.utopia.parser;
2   
3   import java.text.NumberFormat;
4   import java.text.ParseException;
5   import java.util.regex.Matcher;
6   import java.util.regex.Pattern;
7   
8   import org.inigma.utopia.Coordinate;
9   import org.inigma.utopia.Survey;
10  import org.inigma.utopia.utils.CalendarUtils;
11  import org.inigma.utopia.utils.I18NUtil;
12  
13  /**
14   * @author sepatel
15   */
16  public final class SurveyParser extends CommonParser {
17      private static final Pattern ENEMY_SURVEY = Pattern.compile("Our thieves scour the lands of (.+) (\\(\\d+:\\d+\\)) and learn\\.\\.\\.");
18      private static final Pattern IS_SURVEY = Pattern.compile(".*Building Efficiency:\\s+([\\d\\.]+%).*Building Effects(.*)Prisoners of War", Pattern.DOTALL);
19      private static final Pattern BUILDING_EFFECTS = Pattern.compile("(([a-zA-Z ]+)\\s+(\\d+)\\s+([\\d\\.]+%).*?)+");
20      private static final Pattern ANGEL_SURVEY_PATTERN = Pattern.compile(".*Buildings Report of (.+) (\\(\\d+:\\d+\\))");
21      private static final Pattern ANGEL_BUILDINGS = Pattern.compile("\\d+\\. (.+?): (\\d+) \\([\\d\\.%]+\\)");
22  
23      public static SurveyParserData parse(String info) {
24          SurveyParserData parseRaw = parseRaw(info);
25          if (parseRaw == null) {
26              return parseAngel(info);
27          }
28          return parseRaw;
29      }
30  
31      public static SurveyParserData parseAngel(String info) {
32          Matcher matcher = ANGEL_SURVEY_PATTERN.matcher(info);
33          SurveyParserData data = null;
34          Survey survey = null;
35          if (matcher.find()) {
36              data = new SurveyParserData();
37              survey = new Survey();
38              survey.setLastUpdate(CalendarUtils.getCalendar());
39              data.setSurvey(survey);
40              data.setProvinceName(matcher.group(1));
41              data.setCoordinate(new Coordinate(matcher.group(2)));
42  
43              Matcher buildingMatcher = ANGEL_BUILDINGS.matcher(info);
44              while (buildingMatcher.find()) {
45                  int acres = getNumber(buildingMatcher.group(2)).intValue();
46                  String building = buildingMatcher.group(1);
47                  setBuildingInfo(survey, building, acres);
48              }
49          }
50          return data;
51      }
52  
53      public static SurveyParserData parseRaw(String info) {
54          Matcher matcher = IS_SURVEY.matcher(info);
55          SurveyParserData data = null;
56          if (matcher.find()) {
57              data = new SurveyParserData();
58              Matcher checker = ENEMY_SURVEY.matcher(info);
59              if (checker.find()) {
60                  data.setProvinceName(checker.group(1));
61                  data.setCoordinate(new Coordinate(checker.group(2)));
62              }
63  
64              try {
65                  NumberFormat pf = I18NUtil.getPercentageFormat();
66                  Number be = pf.parse(matcher.group(1));
67                  if (data.getSurvey().getEfficiency() == 0 || data.getProvinceName() == null) {
68                      data.getSurvey().setEfficiency(be.floatValue());
69                  }
70              } catch (ParseException e) {
71                  // TODO handle this maybe?
72              }
73  
74              Survey survey = data.getSurvey();
75              survey.setLastUpdate(CalendarUtils.getCalendar());
76              Matcher buildingMatcher = BUILDING_EFFECTS.matcher(matcher.group(2));
77              while (buildingMatcher.find()) {
78                  String building = buildingMatcher.group(2).trim();
79                  int acres = getNumber(buildingMatcher.group(3)).intValue();
80                  setBuildingInfo(survey, building, acres);
81              }
82          }
83          return data;
84      }
85  
86      private static void setBuildingInfo(Survey survey, String building, int acres) {
87          if ("Barren Lands".equals(building)) {
88              survey.setBarren(acres);
89          } else if ("Homes".equals(building)) {
90              survey.setHomes(acres);
91          } else if ("Farms".equals(building)) {
92              survey.setFarms(acres);
93          } else if ("Mills".equals(building)) {
94              survey.setMills(acres);
95          } else if ("Banks".equals(building)) {
96              survey.setBanks(acres);
97          } else if ("Training Grounds".equals(building)) {
98              survey.setTrainingGrounds(acres);
99          } else if ("Armouries".equals(building)) {
100             survey.setArmories(acres);
101         } else if ("Barracks".equals(building)) {
102             survey.setBarracks(acres);
103         } else if ("Forts".equals(building)) {
104             survey.setForts(acres);
105         } else if ("Guard Stations".equals(building)) {
106             survey.setGuardStations(acres);
107         } else if ("Hospitals".equals(building)) {
108             survey.setHospitals(acres);
109         } else if ("Guilds".equals(building)) {
110             survey.setGuilds(acres);
111         } else if ("Towers".equals(building)) {
112             survey.setTowers(acres);
113         } else if ("Thieves' Dens".equals(building)) {
114             survey.setThievesDens(acres);
115         } else if ("Watchtowers".equals(building)) {
116             survey.setWatchtowers(acres);
117         } else if ("Libraries".equals(building)) {
118             survey.setLibraries(acres);
119         } else if ("Schools".equals(building)) {
120             survey.setSchools(acres);
121         } else if ("Stables".equals(building)) {
122             survey.setStables(acres);
123         } else if ("Dungeons".equals(building)) {
124             survey.setDungeons(acres);
125         }
126     }
127 }