Skip to content

Commit

Permalink
Fix last update value at Known Villages
Browse files Browse the repository at this point in the history
- Only update it when appropirate
- Use millis timestamp instead of seconds (because everything else uses millis)
- Use time of report / conquer instead of current time
- Create new KnownVillages with timestamp 0 instead of current

Only use Troops of report if the tribe of defender village did not change
Propper implementation of equals for Tribe class (based on ID)
Fix BuildingLevelCellEditor initial value should be same as the value of the Table
  • Loading branch information
extremeCrazyCoder committed Aug 20, 2020
1 parent 4b5ef86 commit e991a20
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1052,5 +1052,6 @@ private int getBuilding(String pName) {

private void setBuilding(String pName, int level) {
kVillage.setBuildingLevelByName(pName, level);
kVillage.updateTime();
}
}
8 changes: 8 additions & 0 deletions Core/src/main/java/de/tor/tribes/types/ext/Tribe.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,14 @@ public Color getMarkerColor() {
}
return DEFAULT;
}

@Override
public boolean equals(Object other) {
if(! (other instanceof Tribe)) return false;
Tribe oTrib = (Tribe) other;

return oTrib.getId() == getId();
}

@Override
public int compareTo(Tribe o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
public class BuildingLevelCellEditor extends DefaultCellEditor {
private final JComboBox editComp;
private final DefaultComboBoxModel editModel;
private int curCol;

public BuildingLevelCellEditor() {
super(new JComboBox());
Expand All @@ -39,7 +38,6 @@ public BuildingLevelCellEditor() {
for(int i = 1; i <= BuildingSettings.getMaxBuildingLevel("main"); i++)
editModel.addElement(i);
editComp.setModel(editModel);
curCol = -1;
}

@Override
Expand All @@ -52,7 +50,7 @@ public Component getTableCellEditorComponent(JTable table, Object value, boolean
for(int i = BuildingSettings.getMinBuildingLevel(name); i <= BuildingSettings.getMaxBuildingLevel(name); i++)
editModel.addElement(i);

curCol = column;
editModel.setSelectedItem(table.getModel().getValueAt(row, column));
}
return editComp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public void setValueAt(Object o, int rowIndex, int columnIndex) {
switch (columnIndex) {
case 2:
v.setChurchLevel((Integer) o);
v.updateTime();
break;
default:
logger.error("Invalid Columnindex " + columnIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ public Object getValueAt(int rowIndex, int columnIndex) {
case 1:
return v;
case 2:
return new Date(v.getLastUpdate() * 1000L);
return new Date(v.getLastUpdate());
default:
return v.getBuildingLevelByName(buildingNames.get(columnIndex - 3));
return v.getBuildingLevelByName(getBuildingNameForColumn(columnIndex));
}
} catch (Exception e) {
return null;
Expand All @@ -126,12 +126,14 @@ public Object getValueAt(int rowIndex, int columnIndex) {

@Override
public void setValueAt(Object o, int rowIndex, int columnIndex) {
if(getValueAt(rowIndex, columnIndex).equals(o)) return;
if(columnIndex < 3) {
logger.error("Invalid Columnindex " + columnIndex);
return;
}
KnownVillage v = (KnownVillage) KnownVillageManager.getSingleton().getAllElements().get(rowIndex);
v.setBuildingLevelById(columnIndex - 3, (Integer) o);
v.setBuildingLevelByName(getBuildingNameForColumn(columnIndex), (Integer) o);
v.updateTime();
KnownVillageManager.getSingleton().revalidate(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public void setValueAt(Object o, int rowIndex, int columnIndex) {
switch (columnIndex) {
case 2:
v.setWatchtowerLevel((Integer) o);
v.updateTime();
break;
default:
logger.error("Invalid Columnindex " + columnIndex);
Expand Down
14 changes: 7 additions & 7 deletions Core/src/main/java/de/tor/tribes/ui/panels/MapPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -519,31 +519,31 @@ public void mouseClicked(MouseEvent e) {
}
case ImageManager.CURSOR_CHURCH_1: {
if (v != null) {
KnownVillageManager.getSingleton().addChurchLevel(v, 1);
KnownVillageManager.getSingleton().addChurchLevel(v, 1, true);
}
break;
}
case ImageManager.CURSOR_CHURCH_2: {
if (v != null) {
KnownVillageManager.getSingleton().addChurchLevel(v, 2);
KnownVillageManager.getSingleton().addChurchLevel(v, 2, true);
}
break;
}
case ImageManager.CURSOR_CHURCH_3: {
if (v != null) {
KnownVillageManager.getSingleton().addChurchLevel(v, 3);
KnownVillageManager.getSingleton().addChurchLevel(v, 3, true);
}
break;
}
case ImageManager.CURSOR_REMOVE_CHURCH: {
if (v != null) {
KnownVillageManager.getSingleton().removeChurch(v);
KnownVillageManager.getSingleton().removeChurch(v, true);
}
break;
}
case ImageManager.CURSOR_WATCHTOWER_1: {
if (v != null) {
KnownVillageManager.getSingleton().addWatchtowerLevel(v, 1);
KnownVillageManager.getSingleton().addWatchtowerLevel(v, 1, true);
}
break;
}
Expand All @@ -562,13 +562,13 @@ public void mouseClicked(MouseEvent e) {
}

if(level > 0)
KnownVillageManager.getSingleton().addWatchtowerLevel(v, level);
KnownVillageManager.getSingleton().addWatchtowerLevel(v, level, true);
}
break;
}
case ImageManager.CURSOR_REMOVE_WATCHTOWER: {
if (v != null) {
KnownVillageManager.getSingleton().removeWatchtower(v);
KnownVillageManager.getSingleton().removeWatchtower(v, true);
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ private void deleteSelection() {
}
jChurchTable.revalidate();
//remove all selected markers and update the view once
KnownVillageManager.getSingleton().removeChurches(toRemove.toArray(new Village[]{}));
KnownVillageManager.getSingleton().removeChurches(toRemove.toArray(new Village[]{}), true);
showSuccess(toRemove.size() + ((toRemove.size() == 1) ? " Kirche gelöscht" : " Kirchen gelöscht"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ private void deleteSelection() {
}
jWatchtowerTable.revalidate();
//remove all selected markers and update the view once
KnownVillageManager.getSingleton().removeWatchtowers(toRemove.toArray(new Village[]{}));
KnownVillageManager.getSingleton().removeWatchtowers(toRemove.toArray(new Village[]{}), true);
showSuccess(toRemove.size() + ((toRemove.size() == 1) ? " Wachturm gelöscht" : " Wachtürme gelöscht"));
}
}
Expand Down
25 changes: 16 additions & 9 deletions Core/src/main/java/de/tor/tribes/util/conquer/ConquerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import de.tor.tribes.util.GlobalOptions;
import de.tor.tribes.util.troops.TroopsManager;
import de.tor.tribes.util.troops.VillageTroopsHolder;
import de.tor.tribes.util.village.KnownVillage;
import de.tor.tribes.util.village.KnownVillageManager;
import de.tor.tribes.util.xml.JDomUtils;
import java.io.BufferedReader;
Expand Down Expand Up @@ -245,7 +246,7 @@ protected void updateConquers() {
String[] data = line.split(",");
//$village_id, $unix_timestamp, $new_owner, $old_owner
int villageID = Integer.parseInt(data[0]);
int timestamp = Integer.parseInt(data[1]);
long timestamp = Long.parseLong(data[1]);
int newOwner = Integer.parseInt(data[2]);
int oldOwner = Integer.parseInt(data[3]);
Tribe loser = DataHolder.getSingleton().getTribes().get(oldOwner);
Expand Down Expand Up @@ -274,21 +275,27 @@ protected void updateConquers() {

try {
//remove troop information for conquered village
VillageTroopsHolder holder = TroopsManager.getSingleton().getTroopsForVillage(v);
if (holder != null) {
//check if troop holder state lays is before conquer
if (holder.getState().getTime() < timestamp) {
//clear troops information for this village due to troop informations are outdated
holder.clear();
holder.setState(new Date(timestamp));
for(TroopsManager.TROOP_TYPE type: TroopsManager.TROOP_TYPE.values()) {
VillageTroopsHolder holder = TroopsManager.getSingleton().getTroopsForVillage(v, type);
if (holder != null) {
//check if troop holder state lays is before conquer
if (holder.getState().getTime() < timestamp) {
//clear troops information for this village due to troop informations are outdated
holder.clear();
holder.setState(new Date(timestamp));
}
}
}
} catch (Exception ignored) {
}

try {
//removing church
KnownVillageManager.getSingleton().removeChurch(v);
KnownVillage knowV = KnownVillageManager.getSingleton().getKnownVillage(v);
if(knowV.hasChurch()) {
knowV.removeChurchInfo();
knowV.setLastUpdate(timestamp);
}
} catch (Exception ignored) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ public void updateInformation(FightReport pReport) {
if(pReport.wasConquered()) return;

Village v = pReport.getTargetVillage();
if(! pReport.getDefender().equals(v.getTribe())) return;

VillageTroopsHolder own = getTroopsForVillage(v, TROOP_TYPE.OWN, true);
VillageTroopsHolder outwards = getTroopsForVillage(v, TROOP_TYPE.OUTWARDS, true);
Expand Down
21 changes: 12 additions & 9 deletions Core/src/main/java/de/tor/tribes/util/village/KnownVillage.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ public class KnownVillage extends ManageableType {

private int[] buildings;
private Village village;
private long lastUpdate;
private long lastUpdate = 0;

public KnownVillage(Village pVillage) {
buildings = new int[BuildingSettings.BUILDING_NAMES.length];
Arrays.fill(buildings, -1);
this.village = pVillage;
updateTime();
}

public KnownVillage(Element e) {
Expand Down Expand Up @@ -159,7 +158,6 @@ public void setChurchLevel(int pLevel) {
return;
}
setBuildingLevelByName("church", pLevel);
updateTime();
}

public void removeChurchInfo() {
Expand All @@ -168,7 +166,6 @@ public void removeChurchInfo() {
return;
}
setBuildingLevelByName("church", -1);
updateTime();
}

public void setWatchtowerLevel(int pLevel) {
Expand All @@ -177,7 +174,6 @@ public void setWatchtowerLevel(int pLevel) {
return;
}
setBuildingLevelByName("watchtower", pLevel);
updateTime();
}

public void removeWatchtowerInfo() {
Expand All @@ -186,15 +182,18 @@ public void removeWatchtowerInfo() {
return;
}
setBuildingLevelByName("watchtower", -1);
updateTime();
}

public void setLastUpdate(Long pLastUpdate) {
lastUpdate = pLastUpdate;
}

public long getLastUpdate() {
return lastUpdate;
}

private void updateTime() {
lastUpdate = System.currentTimeMillis() / 1000L;
public void updateTime() {
lastUpdate = System.currentTimeMillis();
}

/**
Expand Down Expand Up @@ -246,20 +245,24 @@ public String toString() {
}

void updateInformation(FightReport pReport) {
//only use newer information
if(pReport.getTimestamp() < getLastUpdate()) return;

if (pReport.getSpyLevel() >= pReport.SPY_LEVEL_BUILDINGS) {
for(int i = 0; i < buildings.length; i++) {
if(pReport.getBuilding(i) != -1) {
//Building was spyed
if(BuildingSettings.getMaxBuildingLevel(BuildingSettings.BUILDING_NAMES[i]) > 0) {
//Building can be build
setBuildingLevelById(i, pReport.getBuilding(i));
updateTime();
setLastUpdate(pReport.getTimestamp());
}
}
}
} else if (pReport.getWallAfter() != -1) {
// set wall destruction (works also without spying)
setBuildingLevelByName("wall", pReport.getWallAfter());
setLastUpdate(pReport.getTimestamp());
}
}

Expand Down
Loading

0 comments on commit e991a20

Please sign in to comment.