Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faction Data: Add MUL ID #4344

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
626 changes: 313 additions & 313 deletions megamek/data/forcegenerator/factions.xml

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion megamek/src/megamek/MMConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
public final class MMConstants extends SuiteConstants {
//region General Constants
public static final String PROJECT_NAME = "MegaMek";
public static final String MUL_URL_PREFIX = "http://www.masterunitlist.info/Unit/Details/";
//endregion General Constants

//region GUI Constants
Expand Down
3 changes: 3 additions & 0 deletions megamek/src/megamek/SuiteConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ public abstract class SuiteConstants {
public static final String MM_PREFERENCES_FILE = "mmconf/mm.preferences";
public static final String MML_PREFERENCES_FILE = "mmconf/mml.preferences";
//endregion File Paths

public static final String MUL_URL_UNIT_PREFIX = "http://www.masterunitlist.info/Unit/Details/";
public static final String MUL_URL_FACTION_PREFIX = "http://www.masterunitlist.info/Faction/Details/";
}
97 changes: 64 additions & 33 deletions megamek/src/megamek/client/ratgenerator/FactionRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,18 @@ TechCategory fallthrough() {
}

private String key;
private boolean minor;
private boolean clan;
private boolean periphery;
private int mulId = -1;
private boolean minor = false;
private boolean clan = false;
private boolean periphery = false;
private String name;
private TreeMap<Integer, String> altNames;
private ArrayList<DateRange> yearsActive;
private ArrayList<String> ratingLevels;
private HashMap<Integer, Integer> pctSalvage;
private HashMap<TechCategory, HashMap<Integer, ArrayList<Integer>>> pctTech;
private HashMap<Integer, HashMap<String, Integer>> salvage;
private final TreeMap<Integer, String> altNames = new TreeMap<>();
private final TreeMap<Integer, Integer> altMulIds = new TreeMap<>();
private final ArrayList<DateRange> yearsActive = new ArrayList<>();
private final ArrayList<String> ratingLevels = new ArrayList<>();
private final HashMap<Integer, Integer> pctSalvage = new HashMap<>();
private final HashMap<TechCategory, HashMap<Integer, ArrayList<Integer>>> pctTech = new HashMap<>();
private final HashMap<Integer, HashMap<String, Integer>> salvage = new HashMap<>();
/*
* FM:Updates gives percentage values for omni, Clan, and SL tech. Later manuals are
* less precise, giving omni percentages for Clans and (in FM:3085) upgrade percentage
Expand All @@ -86,12 +88,12 @@ TechCategory fallthrough() {
* get farther from known values. upgradeMargin applies the percentage of units that
* are late-SW IS tech. techMargin applies to both Clan and advanced (SL and post-Clan) tech.
*/
private HashMap<Integer, Integer> omniMargin;
private HashMap<Integer, Integer> techMargin;
private HashMap<Integer, Integer> upgradeMargin;
private final HashMap<Integer, Integer> omniMargin = new HashMap<>();
private final HashMap<Integer, Integer> techMargin = new HashMap<>();
private final HashMap<Integer, Integer> upgradeMargin = new HashMap<>();

private HashMap<Integer, HashMap<Integer, ArrayList<Integer>>> weightDistribution;
private ArrayList<String> parentFactions;
private final HashMap<Integer, HashMap<Integer, ArrayList<Integer>>> weightDistribution = new HashMap<>();
private final ArrayList<String> parentFactions = new ArrayList<>();

public FactionRecord() {
this("Periphery", "Periphery");
Expand All @@ -104,18 +106,6 @@ public FactionRecord(String key) {
public FactionRecord(String key, String name) {
this.key = key;
this.name = name;
minor = clan = periphery = false;
ratingLevels = new ArrayList<>();
altNames = new TreeMap<>();
yearsActive = new ArrayList<>();
pctSalvage = new HashMap<>();
pctTech = new HashMap<>();
omniMargin = new HashMap<>();
upgradeMargin = new HashMap<>();
techMargin = new HashMap<>();
salvage = new HashMap<>();
weightDistribution = new HashMap<>();
parentFactions = new ArrayList<>();
}

@Override
Expand All @@ -133,6 +123,35 @@ public String getKey() {
return key;
}

/** @return The MUL ID of this faction. May vary with the given year when this faction has alternate names and MUL IDs. */
public int getMulId(int year) {
Map.Entry<Integer, Integer> possibleAltMulId = altMulIds.floorEntry(year);
return possibleAltMulId != null ? possibleAltMulId.getValue() : mulId;
}

/** @return The MUL ID of this faction. Always returns the base MUL ID, never any year-dependent alternates. */
public int getMulId() {
return mulId;
}

public void setMulId(int newId) {
mulId = newId;
}

public void setMulId(int year, int mulId) {
altMulIds.put(year, mulId);
}

public void setMulIds(String mulIds) {
String[] fields = mulIds.split(",");
mulId = Integer.parseInt(fields[0]);

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException

Potential uncaught 'java.lang.NumberFormatException'.
altMulIds.clear();
for (int i = 1; i < fields.length; i++) {
String[] entry = fields[i].split(":");
altMulIds.put(Integer.parseInt(entry[0]), Integer.parseInt(entry[1]));

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException

Potential uncaught 'java.lang.NumberFormatException'.

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException

Potential uncaught 'java.lang.NumberFormatException'.
}
}

public boolean isMinor() {
return minor;
}
Expand Down Expand Up @@ -479,6 +498,7 @@ public static FactionRecord createFromXml(Node node) {
FactionRecord retVal = new FactionRecord();
retVal.key = node.getAttributes().getNamedItem("key").getTextContent();
retVal.name = node.getAttributes().getNamedItem("name").getTextContent();
retVal.mulId = Integer.parseInt(node.getAttributes().getNamedItem("mulid").getTextContent());

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException

Potential uncaught 'java.lang.NumberFormatException'.
if (node.getAttributes().getNamedItem("minor") != null) {
retVal.minor = Boolean.parseBoolean(node.getAttributes().getNamedItem("minor").getTextContent());
} else {
Expand All @@ -502,6 +522,8 @@ public static FactionRecord createFromXml(Node node) {
if (wn.getNodeName().equalsIgnoreCase("nameChange")) {
retVal.altNames.put(Integer.parseInt(wn.getAttributes().getNamedItem("year").getTextContent()),
wn.getTextContent());
retVal.altMulIds.put(Integer.parseInt(wn.getAttributes().getNamedItem("year").getTextContent()),

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException

Potential uncaught 'java.lang.NumberFormatException'.
Integer.parseInt(wn.getAttributes().getNamedItem("mulid").getTextContent()));

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException

Potential uncaught 'java.lang.NumberFormatException'.
} else if (wn.getNodeName().equalsIgnoreCase("years")) {
try {
retVal.setYears(wn.getTextContent());
Expand Down Expand Up @@ -586,13 +608,10 @@ public void loadEra(Node node, int era) {
}

public void writeToXml(PrintWriter pw) {
pw.println("\t<faction key='" + key + "' name='"
+ StringEscapeUtils.escapeXml10(name) + "' minor='" + minor
+ "' clan='" + clan
+ "' periphery='" + periphery + "'>");
pw.println("\t<faction key='" + key + "' name='" + StringEscapeUtils.escapeXml10(name)
+ "' mulid='" + mulId + "' minor='" + minor + "' clan='" + clan + "' periphery='" + periphery + "'>");
for (Integer year : altNames.keySet()) {
pw.println("\t\t<nameChange year='" + year + "'>"
+ altNames.get(year) + "</nameChange>");
pw.println("\t\t<nameChange year='" + year + "' mulid='" + altMulIds.get(year) + "'>" + altNames.get(year) + "</nameChange>");
}
pw.print("\t\t<years>");
pw.print(getYearsAsString());
Expand All @@ -601,7 +620,7 @@ public void writeToXml(PrintWriter pw) {
pw.println("\t\t<ratingLevels>" + StringEscapeUtils.escapeXml10(String.join(",", ratingLevels)) + "</ratingLevels>");
}

if ((parentFactions != null) && !parentFactions.isEmpty()) {
if (!parentFactions.isEmpty()) {
pw.println("\t\t<parentFaction>" + StringEscapeUtils.escapeXml10(String.join(",", parentFactions)) + "</parentFaction>");
}
pw.println("\t</faction>");
Expand Down Expand Up @@ -775,6 +794,18 @@ public Object getNamesAsString() {
return retVal.toString();
}

/**
* @return CSV of all MUL IDs of the faction, with the original MUL ID given first followed by
* changes in the format year:MUL ID
*/
public Object getMulIdsAsString() {
StringBuilder retVal = new StringBuilder(mulId+"");
for (Integer y : altMulIds.keySet()) {
retVal.append(",").append(y).append(":").append(altMulIds.get(y));
}
return retVal.toString();
}

/**
* @return CSV String of all date ranges in which the faction is active.
*/
Expand Down
10 changes: 8 additions & 2 deletions megamek/src/megamek/client/ratgenerator/RATDataCSVExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public static void exportToCSV(RATGenerator ratGenerator) {
}
} catch (Exception ex) {
LogManager.getLogger().error("", ex);
JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
}

Expand Down Expand Up @@ -143,7 +144,7 @@ private static void writeModelBaseData(ModelRecord record, StringBuilder csvLine
csvLine.append(record.getMechSummary().getMulId()).append(DELIMITER);
csvLine.append(UnitType.getTypeName(record.getUnitType())).append(DELIMITER);
csvLine.append(record.getMechSummary().getYear()).append(DELIMITER);
csvLine.append("TBD").append(DELIMITER);
csvLine.append(mulId(faction)).append(DELIMITER);
csvLine.append(faction).append(DELIMITER);
}

Expand All @@ -154,10 +155,15 @@ private static void writeChassisBaseData(ChassisRecord record, StringBuilder csv
csvLine.append(DELIMITER);
csvLine.append(UnitType.getTypeName(record.getUnitType())).append(DELIMITER);
csvLine.append(DELIMITER);
csvLine.append("TBD").append(DELIMITER);
csvLine.append(mulId(faction)).append(DELIMITER);
csvLine.append(faction).append(DELIMITER);
}

private static int mulId(String faction) {
FactionRecord factionRecord = RATGenerator.getInstance().getFaction(faction);
return factionRecord != null ? factionRecord.getMulId() : -2;
}

private static RATGenerator initializeRatGenerator() {
RATGenerator ratGenerator = RATGenerator.getInstance();
while (!ratGenerator.isInitialized()) {
Expand Down
13 changes: 3 additions & 10 deletions megamek/src/megamek/client/ui/swing/MechViewPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
package megamek.client.ui.swing;

import megamek.client.ui.swing.util.FluffImageHelper;
import megamek.client.ui.swing.util.UIUtil;
import megamek.client.ui.swing.util.UIUtil.FixedXPanel;
import megamek.common.Entity;
import megamek.common.MechView;
import megamek.common.Report;
import megamek.common.templates.TROView;
import org.apache.logging.log4j.LogManager;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
Expand Down Expand Up @@ -60,15 +60,8 @@ public MechViewPanel(int width, int height, boolean noBorder) {
txtMek.setMinimumSize(new Dimension(width, height));
txtMek.setPreferredSize(new Dimension(width, height));
txtMek.addHyperlinkListener(e -> {
try {
if (HyperlinkEvent.EventType.ACTIVATED == e.getEventType()) {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(e.getURL().toURI());
}
}
} catch (Exception ex) {
LogManager.getLogger().error("", ex);
JOptionPane.showMessageDialog(this, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
if (HyperlinkEvent.EventType.ACTIVATED == e.getEventType()) {
UIUtil.openInBrowser(e.getURL().toString(), null);
}
});
scrMek = new JScrollPane(txtMek);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,20 @@
package megamek.client.ui.swing.alphaStrike;

import megamek.MMConstants;
import megamek.client.ui.Messages;
import megamek.client.ui.dialogs.ASConversionInfoDialog;
import megamek.client.ui.swing.GUIPreferences;
import megamek.client.ui.swing.util.UIUtil;
import megamek.client.ui.Messages;
import megamek.common.alphaStrike.ASCardDisplayable;
import megamek.common.alphaStrike.ASStatsExporter;
import megamek.common.alphaStrike.AlphaStrikeElement;
import megamek.common.alphaStrike.cardDrawer.ASCardPrinter;
import megamek.common.annotations.Nullable;
import org.apache.logging.log4j.LogManager;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.datatransfer.*;
import java.net.URL;
import java.util.List;

/**
Expand Down Expand Up @@ -86,7 +84,7 @@ public ConfigurableASCardPanel(@Nullable ASCardDisplayable element, JFrame paren
copyStatsButton.addActionListener(ev -> copyStats());
printButton.addActionListener(ev -> printCard());

mulButton.addActionListener(ev -> showMUL());
mulButton.addActionListener(ev -> UIUtil.showUnitInMul(mulId, parent));
mulButton.setToolTipText("Show the Master Unit List entry for this unit. Opens a browser window.");

conversionButton.addActionListener(e -> showConversionReport());
Expand Down Expand Up @@ -160,17 +158,6 @@ private void updateSize() {
}
}

private void showMUL() {
try {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(new URL(MMConstants.MUL_URL_PREFIX + mulId).toURI());
}
} catch (Exception ex) {
LogManager.getLogger().error("", ex);
JOptionPane.showMessageDialog(this, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
}

private void showConversionReport() {
if (element instanceof AlphaStrikeElement) {
var dialog = new ASConversionInfoDialog(parent, ((AlphaStrikeElement) element).getConversionReport(), element);
Expand Down
Loading