public static Pricelist[] importAllPricelist(String text) throws Exception {
Map<String, LocationProperty> locationsProperties = (Map<String, LocationProperty>) Storage.getStorage().get(Main.STORAGE_LOCATIONSPROPERTIES);
Map<String, Long> weightCache = (Map<String, Long>) Storage.getStorage().get(Main.STORAGE_WEIGHTCACHE);
Pricelist newpls[] = new Pricelist[System.stars() + System.expanses()];
for (int i = 0; i < newpls.length; i++) {
newpls[i] = new Pricelist();
newpls[i].setSystem(System.values()[i]);
newpls[i].setPricelistDate(new java.util.Date());
}
Pricelist curpl = null;
System cursys = null;
String regex1 = "(for sale|wanted) at (\\w+) system (planets|starbases)";
Pattern pat1 = Pattern.compile(regex1, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
text = text.replace("\r", "");
text = text.replace(",", "");
text = text.replace("\t\t\t\t", "\t");
text = text.replace("\t\t\t", "\t");
text = text.replace("\t\t", "\t");
text = text.replace("\t", " ");
boolean forSale = false;
boolean wanted = false;
boolean planet = false;
boolean starbase = false;
java.lang.System.err.println(text);
String rows[] = text.split("\n");
for (String row : rows) {
if (row.length() == 0) {
continue;
}
//compatibility with internet explorer copy
if (row.length() == 1) {
continue;
}
if (row.toLowerCase().contains("item prices")) {
continue;
}
if (row.toLowerCase().contains("adarian systemaltian systembasian")) {
continue;
}
if (row.toLowerCase().contains("year")) {
continue;
}
//end
if (row.toLowerCase().contains("not looking to buy")) {
forSale = false;
wanted = false;
continue;
}
if (row.toLowerCase().contains("have nothing to sell")) {
forSale = false;
wanted = false;
continue;
}
Matcher m1 = pat1.matcher(row);
if (m1.find()) {
cursys = System.valueOf(m1.group(2));
curpl = newpls[cursys.ordinal()];
}
if (row.toLowerCase().contains("for sale at")) {
//if (!columns[0].toLowerCase().contains(galaxy.name().toLowerCase())) {
// throw new Exception("You have pasted a pricelist of a different galaxy.");
//}
forSale = true;
wanted = false;
if (row.toLowerCase().contains("planet")) {
planet = true;
starbase = false;
}
if (row.toLowerCase().contains("starbase")) {
planet = false;
starbase = true;
}
continue;
}
if (row.toLowerCase().contains("wanted at")) {
//if (!columns[0].toLowerCase().contains(galaxy.name().toLowerCase())) {
// throw new Exception("You have pasted a pricelist from a different galaxy.");
//}
forSale = false;
wanted = true;
if (row.toLowerCase().contains("planet")) {
planet = true;
starbase = false;
}
if (row.toLowerCase().contains("starbase")) {
planet = false;
starbase = true;
}
continue;
}
if (row.indexOf("-------") != -1) {
continue;
}
if (row.indexOf("_______") != -1) {
continue;
}
if (forSale) {
try {
int begin = 0;
int end = 0;
ItemLine itemLine = new ItemLine();
itemLine.setItem(new Item());
itemLine.setForSale(true);
begin = 0;
end = row.indexOf("X ", begin);
if (Character.isDigit(row.charAt(end + 2))) {
//the X of the price
itemLine.getItem().setName(row.substring(begin, end).replace('*', ' ').trim());
} else {
//next X
end = row.indexOf("X ", end + 1);
itemLine.getItem().setName(row.substring(begin, end).replace('*', ' ').trim());
}
begin = end + 2;
end = row.indexOf(" (", begin);
String qty = row.substring(begin, end).trim();
itemLine.setQuantity(Long.parseLong(qty));
begin = end + 2;
end = row.indexOf("Isaton");
String weight = row.substring(begin, end).trim();
long w = Long.parseLong(weight);
weightCache.put(itemLine.getItem().getName(), new Long(w));
itemLine.getItem().setWeight(w);
begin = row.indexOf("for $") + 5;
end = row.indexOf(" ", begin);
itemLine.setPrice(Long.parseLong(row.substring(begin, end).trim()));
Location l = new Location();
begin = end + 1;
l.setName(row.substring(begin).replace('*', ' ').trim());
if (planet) {
l.setPlanet(true);
l.setSystem(cursys);
}
if (starbase) {
l.setPlanet(false);
l.setSystem(cursys);
}
// JUST a sec, lets check the coord cache.
LocationProperty lp = locationsProperties.get(itemLine.getItem().getName());
if (lp != null) {
l.setSector(lp.getSector());
l.setGrid(lp.getGrid());
}
itemLine.setLocation(l);
curpl.getForSale().add(itemLine);
} catch (Exception ex) {
throw new Exception("Impossible to import the pricelist. Offending line: " + row, ex);
}
}
if (wanted) {
try {
int begin = 0;
int end = 0;
ItemLine itemLine = new ItemLine();
itemLine.setItem(new Item());
itemLine.setForSale(false);
begin = 0;
end = row.indexOf("X ", begin);
if (Character.isDigit(row.charAt(end + 2))) {
//the X of the price
itemLine.getItem().setName(row.substring(begin, end).replace('*', ' ').trim());
} else {
//next X
end = row.indexOf("X ", end + 1);
itemLine.getItem().setName(row.substring(begin, end).replace('*', ' ').trim());
}
begin = end + 2;
end = row.indexOf(" ", begin);
String qty = row.substring(begin, end).trim();
itemLine.setQuantity(Long.parseLong(qty));
begin = row.indexOf("at $") + 4;
end = row.indexOf(" ", begin);
itemLine.setPrice(Long.parseLong(row.substring(begin, end).trim()));
itemLine.getItem().setWeight(PricelistsTools.loadWeight(itemLine.getItem().getName()));
Location l = new Location();
if (planet) {
begin = row.indexOf("CE") + 2;
l.setName(row.substring(begin).trim());
l.setPlanet(true);
l.setSystem(cursys);
}
if (starbase) {
begin = end + 1;
l.setName(row.substring(begin).trim());
l.setPlanet(false);
l.setSystem(cursys);
}
// JUST a sec, lets check the coord cache.
LocationProperty lp = locationsProperties.get(itemLine.getItem().getName());
if (lp != null) {
l.setSector(lp.getSector());
l.setGrid(lp.getGrid());
}
itemLine.setLocation(l);
curpl.getWanted().add(itemLine);
} catch (Exception ex) {
throw new Exception("Impossible to import the pricelist. Offending line: " + row, ex);
}
}
}