Skip to content

Commit

Permalink
Merge pull request #48 from extremeCrazyCoder/master
Browse files Browse the repository at this point in the history
Div. Bugfixes
  • Loading branch information
Torridity authored Feb 26, 2018
2 parents 051e7f8 + d74aef9 commit fc825e0
Show file tree
Hide file tree
Showing 33 changed files with 604 additions and 749 deletions.
2 changes: 1 addition & 1 deletion Core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>de.tor.dswb</groupId>
<artifactId>dsworkbench</artifactId>
<version>3.6</version>
<version>3.61</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
184 changes: 4 additions & 180 deletions Core/src/main/java/de/tor/tribes/io/DataHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public boolean loadLiveData() {
if (!GlobalOptions.isOfflineMode()) {
fireDataHolderEvents("Download der aktuellen Weltdaten von die-staemme.de gestartet");
logger.debug(" - Initiating full reload of live data");
if (downloadLiveData()) {
if (downloadData()) {
logger.debug(" - Download succeeded");
fireDataHolderEvents("Download erfolgreich");
recreateLocal = true;
Expand Down Expand Up @@ -554,7 +554,9 @@ public boolean readLocalDataCopy(String pServerDir) {
line = line.replaceAll(",,", ", ,");
Village v = Village.parseFromPlainData(line);
try {
mVillages[v.getX()][v.getY()] = v;
if(v != null) {
mVillages[v.getX()][v.getY()] = v;
}
} catch (Exception e) {
//ignore invalid village
}
Expand Down Expand Up @@ -647,184 +649,6 @@ private Hashtable<Integer, Ally> getAlliesForServer(String pServer, Hashtable<In
* settings)
*/
private boolean downloadData() {

return downloadLiveData();

/*
URL file = null;
String serverID = GlobalOptions.getSelectedServer();
String serverDir = getDataDirectory();
logger.info("Using server dir '" + serverDir + "'");
new File(serverDir).mkdirs();
try {
// <editor-fold defaultstate="collapsed" desc="Server settings check">
//download settings.xml
String sURL = ServerManager.getServerURL(GlobalOptions.getSelectedServer());
logger.debug("Download server settings");
fireDataHolderEvents("Lese Server Einstellungen");
File target = new File(serverDir + "/settings.xml");
if (target.exists()) {
target.delete();
}
file = new URL(sURL + "/interface.php?func=get_config");
downloadDataFile(file, "settings_tmp.xml");
//new File("settings_tmp.xml").renameTo(target);
copyFile(new File("settings_tmp.xml"), target);
if (!serverSupported()) {
return false;
}
//</editor-fold>
String downloadURL = ServerManager.getServerURL(serverID);
if (isDataAvailable()) {
//no update needed
logger.info("No update needed");
return true;
} else {
//full download if no download made yet or diff too large
//load villages
logger.info("Downloading new data version from " + downloadURL);
//clear all data structures
//initialize();
// <editor-fold defaultstate="collapsed" desc=" Load villages ">
fireDataHolderEvents("Lade Dörferliste");
file = new URL(downloadURL + "/map/village.txt.gz");
logger.debug(" + Start reading villages");
downloadDataFile(file, "village.tmp");
logger.debug(" - Finished reading villages");
BufferedReader r = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream("village.tmp"))));
String line = "";
logger.debug(" + Start parsing villages");
while ((line = r.readLine()) != null) {
line = line.replaceAll(",,", ", ,");
Village v = Village.parseFromPlainData(line);
try {
mVillages[v.getX()][v.getY()] = v;
} catch (Exception e) {
//ignore invalid village
}
}
r.close();
logger.debug(" - Finished parsing villages");
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc=" Load tribes ">
fireDataHolderEvents("Lade Spielerliste");
file = new URL(downloadURL + "/map/tribe.txt.gz");
logger.debug(" + Start reading tribes");
downloadDataFile(file, "tribe.tmp");
logger.debug(" - Finished reading tribes");
r = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream("tribe.tmp"))));
line = "";
logger.debug(" + Start parsing tribes");
while ((line = r.readLine()) != null) {
line = line.replaceAll(",,", ", ,");
Tribe t = Tribe.parseFromPlainData(line);
if (t != null && t.getName() != null) {
mTribes.put(t.getId(), t);
mTribesByName.put(t.getName(), t);
}
}
r.close();
logger.debug(" - Finished parsing tribes");
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc=" Load allies ">
fireDataHolderEvents("Lade Stämmeliste");
file = new URL(downloadURL + "/ally.txt.gz");
logger.debug(" + Start reading allies");
downloadDataFile(file, "ally.tmp");
logger.debug(" - Finished reading allies");
r = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream("ally.tmp"))));
line = "";
logger.debug(" + Start parsing allies");
while ((line = r.readLine()) != null) {
line = line.replaceAll(",,", ", ,");
Ally a = Ally.parseFromPlainData(line);
if (a != null && a.getName() != null && a.getTag() != null) {
mAllies.put(a.getId(), a);
mAlliesByName.put(a.getName(), a);
mAlliesByTagName.put(a.getTag(), a);
}
}
logger.debug(" - Finished parsing allies");
r.close();
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc=" Load conquers off ">
fireDataHolderEvents("Lese besiegte Gegner (Angriff)...");
target = new File(serverDir + "/kill_att.txt.gz");
file = new URL(downloadURL + "/kill_att.txt.gz");
logger.debug(" + Downloading conquers (off)");
downloadDataFile(file, "kill_att.tmp");
if (target.exists()) {
target.delete();
}
// new File("kill_att.tmp").renameTo(target);
copyFile(new File("kill_att.tmp"), target);
logger.debug(" - Finished downloading conquers (off)");
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc=" Load conquers def ">
fireDataHolderEvents("Lese besiegte Gegner (Verteidigung)...");
target = new File(serverDir + "/kill_def.txt.gz");
file = new URL(downloadURL + "/kill_def.txt.gz");
logger.debug(" + Downloading conquers (def)");
downloadDataFile(file, "kill_def.tmp");
if (target.exists()) {
target.delete();
}
// new File("kill_def.tmp").renameTo(target);
copyFile(new File("kill_def.tmp"), target);
logger.debug(" - Finished downloading conquers (def)");
// </editor-fold>
}
// <editor-fold defaultstate="collapsed" desc="Direct download from DS-Servers">
//download unit information, but only once
target = new File(serverDir + "/units.xml");
if (!target.exists()) {
logger.debug("Loading unit config file from server");
fireDataHolderEvents("Lade Information über Einheiten");
file = new URL(sURL + "/interface.php?func=get_unit_info");
downloadDataFile(file, "units_tmp.xml");
// new File("units_tmp.xml").renameTo(target);
copyFile(new File("units_tmp.xml"), target);
}
//download building information, but only once
target = new File(serverDir + "/buildings.xml");
if (!target.exists()) {
logger.debug("Loading building config file from server");
fireDataHolderEvents("Lade Information über Gebäude");
file = new URL(sURL + "/interface.php?func=get_building_info");
downloadDataFile(file, "buildings_tmp.xml");
// new File("buildings_tmp.xml").renameTo(target);
copyFile(new File("buildings_tmp.xml"), target);
}
//</editor-fold>
fireDataHolderEvents("Download erfolgreich beendet.");
} catch (Throwable t) {
fireDataHolderEvents("Download fehlgeschlagen.");
logger.error("Failed to download data", t);
return false;
}
return true;*/
}

private boolean downloadLiveData() {
URL file = null;
String serverDir = getDataDirectory();
logger.info("Using server dir '" + serverDir + "'");
Expand Down
5 changes: 5 additions & 0 deletions Core/src/main/java/de/tor/tribes/io/ServerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package de.tor.tribes.io;

import de.tor.tribes.util.Constants;
import de.tor.tribes.dssim.util.ConfigManager;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down Expand Up @@ -138,4 +139,8 @@ public static String[] getServerIDs() {
public static String getServerURL(String pServerID) {
return SERVERS.get(pServerID);
}

public static void giveSimulatorServerList() {
ConfigManager.getSingleton().setServers(SERVERS);
}
}
53 changes: 35 additions & 18 deletions Core/src/main/java/de/tor/tribes/io/TroopAmountDynamic.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,15 @@ public TroopAmountDynamic() {
}

public TroopAmountDynamic(int pAmount) {
this(new TroopAmountElement(DataHolder.getSingleton().getRandomUnit(), pAmount));
fill("" + pAmount);
}

/**
* This ignores the unit set in TroopAmountElement!
* @param pAmount the Amount for !ALL! units to set
*/
public TroopAmountDynamic(TroopAmountElement pAmount) {
amounts = new HashMap<>();
for(UnitHolder unit: DataHolder.getSingleton().getUnits()) {
amounts.put(unit, new TroopAmountElement(unit, pAmount.toString()));
}
fill(pAmount.toString());
}

public TroopAmountDynamic(TroopAmountElement[] pAmounts) {
Expand All @@ -65,7 +62,11 @@ public TroopAmountDynamic(Element pElement) {
}

public TroopAmountElement getElementForUnit(UnitHolder pUnit) {
return amounts.get(pUnit);
TroopAmountElement elm = amounts.get(pUnit);
if(elm == null) {
return new TroopAmountElement(pUnit, -1);
}
return elm;
}

public void setAmount(TroopAmountElement pAmount) {
Expand Down Expand Up @@ -102,7 +103,7 @@ public String toXml() {
StringBuilder xml = new StringBuilder();
boolean first = true;
for(UnitHolder unit: DataHolder.getSingleton().getUnits()) {
TroopAmountElement elm = amounts.get(unit);
TroopAmountElement elm = getElementForUnit(unit);
if(!elm.isFixed() || elm.getTroopsAmount(null) >=0) {
//Information stored in sub element
if(!first)
Expand Down Expand Up @@ -142,7 +143,7 @@ public String toProperty() {
first = false;

prop.append(unit.getPlainName()).append("=");
prop.append(amounts.get(unit).toBase64());
prop.append(getElementForUnit(unit).toBase64());
}
return prop.toString();
}
Expand Down Expand Up @@ -171,8 +172,9 @@ public void addAmount(TroopAmount pAdd) {
if(aElm.isFixed() && aElm.getTroopsAmount(null) < 0) {
aElmStr = "0";
}

elm.setDynamicAmount(elmStr + "+" + aElmStr);
amounts.put(unit, elm);
}
}

Expand All @@ -195,36 +197,44 @@ public void removeAmount(TroopAmount pRemove) {
//no information stored in sublement
if(!rElm.isFixed() || rElm.getTroopsAmount(null) < 0) {
//toRemove element contains information just set to zero
amounts.put(unit, new TroopAmountElement(unit, "0"));
elm = new TroopAmountElement(unit, "0");
}
} else if(elm.isFixed()) {
if(rElm.isFixed() && rElm.getTroopsAmount(null) < 0) {
int amount = elm.getTroopsAmount(null) - rElm.getTroopsAmount(null);
//limit to zero
amount = Math.max(amount, 0);
amounts.put(unit, new TroopAmountElement(unit, amount));
elm = new TroopAmountElement(unit, amount);
} else if(!rElm.isFixed()) {
elm.setDynamicAmount(elm.toString() + "-" + rElm.toString());
}
} else {
//Dynamic information stored in sub element
elm.setDynamicAmount(elm.toString() + "-" + rElm.toString());
}
amounts.put(unit, elm);
}
}

@Override
public void multiplyWith(double factor) {
if(factor <= 0) {
//throw exception also for 0 because fill should be used in that case
if(factor < 0) {
logger.error("Tried to multiply with negative value " + factor);
throw new RuntimeException("Tried to multiply with negative value " + factor);
}
if(factor == 0) {
logger.info("multiplyed with 0, use fill instead");
fill("0");
return;
}

for(UnitHolder unit: amounts.keySet()) {
if(!getElementForUnit(unit).isFixed()
|| getElementForUnit(unit).getTroopsAmount(null) > 0) {
amounts.get(unit).setDynamicAmount("(" + amounts.get(unit).toString() + ")*" + factor);
TroopAmountElement elm = getElementForUnit(unit);

if(!elm.isFixed()
|| elm.getTroopsAmount(null) > 0) {
elm.setDynamicAmount("(" + amounts.get(unit).toString() + ")*" + factor);
amounts.put(unit, elm);
}
}
}
Expand All @@ -235,7 +245,7 @@ protected int getInternalAmountForUnit(UnitHolder pUnit, Village pVillage) {
logger.error("Tried to read fixed troops from Dynamic amount");
throw new IllegalArgumentException("Tried to read fixed troops from Dynamic amount");
}
return amounts.get(pUnit).getTroopsAmount(pVillage);
return getElementForUnit(pUnit).getTroopsAmount(pVillage);
}

public TroopAmountFixed transformToFixed(Village pVillage) {
Expand Down Expand Up @@ -341,7 +351,14 @@ public TroopAmountDynamic clone() {
for(UnitHolder unit: getContainedUnits()) {
clone.setAmount(new TroopAmountElement(unit,
getElementForUnit(unit).toString()));
}
}
return clone;
}

public void fill(String pAmount) {
amounts = new HashMap<>();
for(UnitHolder unit: DataHolder.getSingleton().getUnits()) {
amounts.put(unit, new TroopAmountElement(unit, pAmount));
}
}
}
Loading

0 comments on commit fc825e0

Please sign in to comment.