Skip to content

Commit

Permalink
WIP: switch to UUID for mission IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
sixlettervariables committed Mar 13, 2020
1 parent b97c54a commit b626bea
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 69 deletions.
12 changes: 5 additions & 7 deletions MekHQ/src/mekhq/campaign/Campaign.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ public class Campaign implements Serializable, ITechManager {
private Map<UUID, Ancestors> ancestors = new LinkedHashMap<>();
private TreeMap<Integer, Part> parts = new TreeMap<>();
private TreeMap<Integer, Force> forceIds = new TreeMap<>();
private TreeMap<Integer, Mission> missions = new TreeMap<>();
private TreeMap<Integer, Scenario> scenarios = new TreeMap<>();
private TreeMap<UUID, Mission> missions = new TreeMap<>();
private TreeMap<UUID, Scenario> scenarios = new TreeMap<>();
private Map<UUID, List<Kill>> kills = new HashMap<>();

private Map<String, Integer> duplicateNameHash = new HashMap<>();
Expand All @@ -222,8 +222,6 @@ public class Campaign implements Serializable, ITechManager {

private int lastPartId;
private int lastForceId;
private int lastMissionId;
private int lastScenarioId;

// indicates whether or not the campaign should be gzipped, if possible.
private boolean preferGzippedOutput;
Expand Down Expand Up @@ -982,7 +980,7 @@ public ArrayList<Mission> getSortedMissions() {
* @param id the <code>int</code> id of the team
* @return a <code>SupportTeam</code> object
*/
public Mission getMission(int id) {
public Mission getMission(UUID id) {
return missions.get(id);
}

Expand Down Expand Up @@ -3721,7 +3719,7 @@ public void removeAllTechJobsFor(Person tech) {
}
}

public void removeScenario(int id) {
public void removeScenario(UUID id) {
Scenario scenario = getScenario(id);
scenario.clearAllForcesAndPersonnel(this);
Mission mission = getMission(scenario.getMissionId());
Expand All @@ -3732,7 +3730,7 @@ public void removeScenario(int id) {
MekHQ.triggerEvent(new ScenarioChangedEvent(scenario));
}

public void removeMission(int id) {
public void removeMission(UUID id) {
Mission mission = getMission(id);

// Loop through scenarios here! We need to remove them as well.
Expand Down
6 changes: 3 additions & 3 deletions MekHQ/src/mekhq/campaign/force/Lance.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class Lance implements Serializable, MekHqXmlSerializable {
public static final int NO_MISSION = -1;

private int forceId;
private int missionId;
private UUID missionId;
private int role;
private UUID commanderId;

Expand All @@ -106,7 +106,7 @@ public Lance() {}
public Lance(int fid, Campaign c) {
forceId = fid;
role = ROLE_UNASSIGNED;
missionId = -1;
missionId = null;
for (Mission m : c.getSortedMissions()) {
if (!m.isActive()) {
break;
Expand All @@ -126,7 +126,7 @@ public int getForceId() {
return forceId;
}

public int getMissionId() {
public UUID getMissionId() {
return missionId;
}

Expand Down
6 changes: 3 additions & 3 deletions MekHQ/src/mekhq/campaign/mission/AtBContract.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ public class AtBContract extends Contract implements Serializable {
protected int nextWeekBattleTypeMod;

protected AtBContract() {
this(null);
this(UUID.randomUUID(), null);
}

public AtBContract(String name) {
super(name, "Independent");
public AtBContract(UUID id, String name) {
super(id, name, "Independent");
employerCode = "IND";
enemyCode = "IND";

Expand Down
20 changes: 11 additions & 9 deletions MekHQ/src/mekhq/campaign/mission/Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.UUID;

import mekhq.campaign.finances.Money;
import org.apache.commons.text.CharacterPredicate;
Expand Down Expand Up @@ -94,7 +95,7 @@ public class Contract extends Mission implements Serializable, MekHqXmlSerializa
// runs through initial calculations, as the same jump path is referenced multiple times
// and calculating it each time is expensive. No need to preserve it in save date.
private JumpPath cachedJumpPath;

// need to keep track of total value salvaged for salvage rights
private Money salvagedByUnit = Money.zero();
private Money salvagedByEmployer = Money.zero();
Expand All @@ -110,11 +111,12 @@ public class Contract extends Mission implements Serializable, MekHqXmlSerializa
private Money feeAmount = Money.zero();

public Contract() {
this(null, null);
this(UUID.randomUUID(), null, null);
}

public Contract(String name, String employer) {
super(name);
public Contract(UUID id, String name, String employer) {
super(id, name);

this.employer = employer;

this.nMonths = 12;
Expand Down Expand Up @@ -397,28 +399,28 @@ public void setSystemId(String n) {
super.setSystemId(n);
cachedJumpPath = null;
}

/**
* Gets the currently calculated jump path for this contract,
* only recalculating if it's not valid any longer or hasn't been calculated yet.
*/
public JumpPath getJumpPath(Campaign c) {
// if we don't have a cached jump path, or if the jump path's starting/ending point
// if we don't have a cached jump path, or if the jump path's starting/ending point
// no longer match the campaign's current location or contract's destination
if((cachedJumpPath == null) ||
(cachedJumpPath.size() == 0) ||
!cachedJumpPath.getFirstSystem().getId().equals(c.getCurrentSystem().getId()) ||
!cachedJumpPath.getLastSystem().getId().equals(getSystem().getId())) {
cachedJumpPath = c.calculateJumpPath(c.getCurrentSystem(), getSystem());
}

return cachedJumpPath;
}

public void setJumpPath(JumpPath path) {
cachedJumpPath = path;
}

public Money getMonthlyPayOut() {
if (getLength() <= 0) {
return Money.zero();
Expand Down
76 changes: 40 additions & 36 deletions MekHQ/src/mekhq/campaign/mission/Mission.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/*
* Mission.java
*
*
* Copyright (c) 2011 Jay Lawson <jaylawson39 at yahoo.com>. All rights reserved.
*
*
* This file is part of MekHQ.
*
*
* MekHQ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* MekHQ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with MekHQ. If not, see <http://www.gnu.org/licenses/>.
*/
Expand All @@ -24,6 +24,7 @@
import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.UUID;

import org.joda.time.DateTime;
import org.w3c.dom.NamedNodeMap;
Expand All @@ -41,15 +42,15 @@

/**
* Missions are primarily holder objects for a set of scenarios.
*
*
* The really cool stuff will happen when we subclass this into Contract
*
*
* @author Jay Lawson <jaylawson39 at yahoo.com>
*/
public class Mission implements Serializable, MekHqXmlSerializable {

/**
*
*
*/
private static final long serialVersionUID = -5692134027829715149L;

Expand All @@ -59,20 +60,22 @@ public class Mission implements Serializable, MekHqXmlSerializable {
public static final int S_BREACH = 3;
public static final int S_NUM = 4;

private UUID id;
private String name;
protected String systemId;
private int status;
private String desc;
private String type;
private ArrayList<Scenario> scenarios;
private int id = -1;
private int legacyId = -1;
private String legacyPlanetName;

public Mission() {
this(null);
this(UUID.randomUUID(), null);
}

public Mission(String n) {
public Mission(UUID id, String n) {
this.id = id;
this.name = n;
this.systemId = "Unknown System";
this.desc = "";
Expand Down Expand Up @@ -124,9 +127,9 @@ public void setSystemId(String n) {
public PlanetarySystem getSystem() {
return Systems.getInstance().getSystemById(systemId);
}

/**
* Convenience property to return the name of the current planet.
* Convenience property to return the name of the current planet.
* Sometimes, the "current planet" doesn't match up with an existing planet in our planet database,
* in which case we return whatever was stored.
* @return
Expand All @@ -135,10 +138,10 @@ public String getSystemName(DateTime when) {
if(getSystem() == null) {
return legacyPlanetName;
}

return getSystem().getName(when);
}

public void setLegacyPlanetName(String name) {
legacyPlanetName = name;
}
Expand Down Expand Up @@ -170,39 +173,36 @@ public ArrayList<Scenario> getScenarios() {
/**
* Don't use this method directly as it will not add an id to the added
* scenario. Use Campaign#AddScenario instead
*
*
* @param s
*/
public void addScenario(Scenario s) {
s.setMissionId(getId());
scenarios.add(s);
}

public int getId() {
public UUID getId() {
return id;
}

public void setId(int i) {
this.id = i;
public void setId(UUID id) {
this.id = id;
}

public int getLegacyId() {
return legacyId;
}

public void setLegacyId(int i) {
this.legacyId = i;
}

public boolean isActive() {
return status == S_ACTIVE;
}

public void removeScenario(int id) {
int idx = 0;
boolean found = false;
for (Scenario s : getScenarios()) {
if (s.getId() == id) {
found = true;
break;
}
idx++;
}
if (found) {
scenarios.remove(idx);
}
public void removeScenario(UUID id) {
scenarios.removeIf(s -> s.getId().equals(id));
}

public void clearScenarios() {
Expand All @@ -228,8 +228,8 @@ protected void writeToXmlBegin(PrintWriter pw1, int indent) {
pw1.println(MekHqXmlUtil.indentStr(indent) + "<mission id=\"" + id + "\" type=\"" + this.getClass().getName() + "\">");
pw1.println(MekHqXmlUtil.indentStr(indent + 1) + "<name>" + MekHqXmlUtil.escape(name) + "</name>");
pw1.println(MekHqXmlUtil.indentStr(indent + 1) + "<type>" + MekHqXmlUtil.escape(type) + "</type>");
if(systemId != null) {

if(systemId != null) {
pw1.println(MekHqXmlUtil.indentStr(indent + 1) + "<systemId>" + MekHqXmlUtil.escape(systemId) + "</systemId>");
} else {
pw1.println(MekHqXmlUtil.indentStr(indent + 1) + "<planetName>" + MekHqXmlUtil.escape(legacyPlanetName) + "</planetName>");
Expand Down Expand Up @@ -278,7 +278,7 @@ public static Mission generateInstanceFromXML(Node wn, Campaign c, Version versi
retVal.systemId = wn2.getTextContent();
} else if (wn2.getNodeName().equalsIgnoreCase("planetName")) {
PlanetarySystem system = c.getSystemByName(wn2.getTextContent());

if(system != null) {
retVal.systemId = c.getSystemByName(wn2.getTextContent()).getId();
} else {
Expand All @@ -287,7 +287,11 @@ public static Mission generateInstanceFromXML(Node wn, Campaign c, Version versi
} else if (wn2.getNodeName().equalsIgnoreCase("status")) {
retVal.status = Integer.parseInt(wn2.getTextContent());
} else if (wn2.getNodeName().equalsIgnoreCase("id")) {
retVal.id = Integer.parseInt(wn2.getTextContent());
try {
retVal.id = UUID.fromString(wn2.getTextContent());
} catch (IllegalArgumentException ex) {
retVal.legacyId = Integer.parseInt(wn2.getTextContent());
}
} else if (wn2.getNodeName().equalsIgnoreCase("desc")) {
retVal.setDesc(wn2.getTextContent());
} else if (wn2.getNodeName().equalsIgnoreCase("type")) {
Expand Down
Loading

0 comments on commit b626bea

Please sign in to comment.