Skip to content

Commit

Permalink
Merge pull request #792 from firefly2442/historical-daily-log
Browse files Browse the repository at this point in the history
Fix #775
  • Loading branch information
NickAragua committed Jul 30, 2018
2 parents e5dab02 + e385a39 commit e5bce56
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 7 deletions.
46 changes: 45 additions & 1 deletion MekHQ/src/mekhq/campaign/Campaign.java
Expand Up @@ -43,6 +43,7 @@
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -219,6 +220,7 @@
import mekhq.campaign.work.IAcquisitionWork;
import mekhq.campaign.work.IPartWork;
import mekhq.gui.GuiTabType;
import mekhq.gui.dialog.HistoricalDailyReportDialog;
import mekhq.gui.utilities.PortraitFileFactory;
import mekhq.module.atb.AtBEventProcessor;

Expand Down Expand Up @@ -288,6 +290,10 @@ public class Campaign implements Serializable, ITechManager {
private transient String currentReportHTML;
private transient List<String> newReports;

//this is updated and used per gaming session, it is enabled/disabled via the Campaign options
//we're re-using the LogEntry class that is used to store Personnel entries
public LinkedList<LogEntry> inMemoryLogHistory = new LinkedList<LogEntry>();

private boolean overtime;
private boolean gmMode;
private transient boolean overviewLoadingValue = true;
Expand Down Expand Up @@ -2670,7 +2676,7 @@ public boolean newDay() {
currentReport.clear();
currentReportHTML = "";
newReports.clear();
addReport("<b>" + getDateAsString() + "</b>");
beginReport("<b>" + getDateAsString() + "</b>");

if (calendar.get(Calendar.DAY_OF_YEAR) == 1) {
reloadNews();
Expand Down Expand Up @@ -3142,7 +3148,45 @@ public void setRetainerEmployerCode(String code) {
retainerEmployerCode = code;
}

private void addInMemoryLogHistory(LogEntry le) {
if (inMemoryLogHistory.size() != 0) {
long diff = le.getDate().getTime() - inMemoryLogHistory.get(0).getDate().getTime();
while ((diff / (1000 * 60 * 60 * 24)) > HistoricalDailyReportDialog.MAX_DAYS_HISTORY) {
//we've hit the max size for the in-memory based on the UI display limit
//prune the oldest entry
inMemoryLogHistory.remove(0);
diff = le.getDate().getTime() - inMemoryLogHistory.get(0).getDate().getTime();
}
}
inMemoryLogHistory.add(le);
}

/**
* Starts a new day for the daily log
* @param r - the report String
*/
public void beginReport(String r) {
if (this.getCampaignOptions().historicalDailyLog()) {
//add the new items to our in-memory cache
addInMemoryLogHistory(new LogEntry(getDate(), ""));
}
addReportInternal(r);
}

/**
* Adds a report to the daily log
* @param r - the report String
*/
public void addReport(String r) {
if (this.getCampaignOptions().historicalDailyLog()) {
String htmlStripped = r.replaceAll("\\<[^>]*>",""); //remote HTML tags
//add the new items to our in-memory cache
addInMemoryLogHistory(new LogEntry(getDate(), htmlStripped));
}
addReportInternal(r);
}

private void addReportInternal(String r) {
currentReport.add(r);
if( currentReportHTML.length() > 0 ) {
currentReportHTML = currentReportHTML + REPORT_LINEBREAK + r;
Expand Down
16 changes: 16 additions & 0 deletions MekHQ/src/mekhq/campaign/CampaignOptions.java
Expand Up @@ -292,6 +292,9 @@ public class CampaignOptions implements Serializable {
private boolean massRepairUseAssignedTechsFirst;
private boolean massRepairReplacePod;
private List<MassRepairOption> massRepairOptions;

//Miscellaneous
private boolean historicalDailyLog;

public CampaignOptions() {
clanPriceModifier = 1.0;
Expand Down Expand Up @@ -521,6 +524,8 @@ public CampaignOptions() {
for (int i = 0; i < MassRepairOption.VALID_REPAIR_TYPES.length; i++) {
massRepairOptions.add(new MassRepairOption(MassRepairOption.VALID_REPAIR_TYPES[i]));
}

historicalDailyLog = false;
}

public UnitRatingMethod getUnitRatingMethod() {
Expand Down Expand Up @@ -1174,6 +1179,14 @@ public void setAdminXPPeriod(int m) {
adminXPPeriod = m;
}

public boolean historicalDailyLog() {
return historicalDailyLog;
}

public void setHistoricalDailyLog(boolean b) {
this.historicalDailyLog = b;
}

public int getEdgeCost() {
return edgeCost;
}
Expand Down Expand Up @@ -2243,6 +2256,7 @@ public void writeToXml(PrintWriter pw1, int indent) {
MekHqXmlUtil.writeSimpleXmlTag(pw1, indent + 1, "allowOpforLocalUnits", allowOpforLocalUnits);
MekHqXmlUtil.writeSimpleXmlTag(pw1, indent + 1, "opforAeroChance", opforAeroChance);
MekHqXmlUtil.writeSimpleXmlTag(pw1, indent + 1, "opforLocalUnitChance", opforLocalUnitChance);
MekHqXmlUtil.writeSimpleXmlTag(pw1, indent + 1, "historicalDailyLog", historicalDailyLog);

//Mass Repair/Salvage Options
MekHqXmlUtil.writeSimpleXmlTag(pw1, indent + 1, "massRepairUseExtraTime", massRepairUseExtraTime);
Expand Down Expand Up @@ -2741,6 +2755,8 @@ public static CampaignOptions generateCampaignOptionsFromXml(Node wn) {
retVal.opforAeroChance = Integer.parseInt(wn2.getTextContent().trim());
} else if (wn2.getNodeName().equalsIgnoreCase("opforLocalUnitChance")) {
retVal.opforLocalUnitChance = Integer.parseInt(wn2.getTextContent().trim());
} else if (wn2.getNodeName().equalsIgnoreCase("historicalDailyLog")) {
retVal.historicalDailyLog = Boolean.parseBoolean(wn2.getTextContent().trim());
} else if (wn2.getNodeName().equalsIgnoreCase("rats")) {
retVal.rats = MekHqXmlUtil.unEscape(wn2.getTextContent().trim()).split(",");
} else if (wn2.getNodeName().equalsIgnoreCase("staticRATs")) {
Expand Down
6 changes: 5 additions & 1 deletion MekHQ/src/mekhq/campaign/LogEntry.java
Expand Up @@ -65,7 +65,11 @@ public void setDate(Date d) {
public Date getDate() {
return date;
}


public String getDateString() {
return date.toString();
}

public void setDesc(String d) {
this.desc = Objects.requireNonNull(d);
}
Expand Down
20 changes: 20 additions & 0 deletions MekHQ/src/mekhq/gui/CampaignGUI.java
Expand Up @@ -140,6 +140,7 @@
import mekhq.gui.dialog.DataLoadingDialog;
import mekhq.gui.dialog.GMToolsDialog;
import mekhq.gui.dialog.HireBulkPersonnelDialog;
import mekhq.gui.dialog.HistoricalDailyReportDialog;
import mekhq.gui.dialog.MaintenanceReportDialog;
import mekhq.gui.dialog.MassMothballDialog;
import mekhq.gui.dialog.MekHQAboutBox;
Expand Down Expand Up @@ -183,6 +184,7 @@ public class CampaignGUI extends JPanel {
/* For the menu bar */
private JMenuBar menuBar;
private JMenu menuThemes;
private JMenuItem miHistoricalDailyReportDialog;
private JMenuItem miDetachLog;
private JMenuItem miAttachLog;
private JMenuItem miContractMarket;
Expand Down Expand Up @@ -211,6 +213,7 @@ public class CampaignGUI extends JPanel {

ReportHyperlinkListener reportHLL;

private HistoricalDailyReportDialog histDailyReportDialog;
private DailyReportLogDialog logDialog;
private AdvanceDaysDialog advanceDaysDialog;
private BloodnameDialog bloodnameDialog;
Expand All @@ -229,6 +232,12 @@ public void showAboutBox() {
aboutBox.setVisible(true);
}

private void showHistoricalDailyReportDialog() {
histDailyReportDialog = new HistoricalDailyReportDialog(getFrame(), this);
histDailyReportDialog.setVisible(true);
histDailyReportDialog.dispose();
}

private void showDailyReportDialog() {
mainPanel.remove(panLog);
miDetachLog.setEnabled(false);
Expand Down Expand Up @@ -1034,6 +1043,17 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
// menuBar.add(menuCommunity);

JMenu menuView = new JMenu("View"); // NOI18N

miHistoricalDailyReportDialog = new JMenuItem(resourceMap.getString("miShowHistoricalReportLog.text")); // NOI18N
miHistoricalDailyReportDialog.setEnabled(true);
miHistoricalDailyReportDialog.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
showHistoricalDailyReportDialog();
}
});
menuView.add(miHistoricalDailyReportDialog);

miDetachLog = new JMenuItem("Detach Daily Report Log"); // NOI18N
miDetachLog.addActionListener(new ActionListener() {
@Override
Expand Down
9 changes: 6 additions & 3 deletions MekHQ/src/mekhq/gui/DailyReportLogPanel.java
Expand Up @@ -50,10 +50,8 @@
*/
public class DailyReportLogPanel extends JPanel {

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

JTextPane txtLog;
String logText = new String();

Expand Down Expand Up @@ -84,6 +82,11 @@ private void initComponents() {
add(scrLog, BorderLayout.CENTER);
}

public void clearLogPanel() {
logText = "";
txtLog.setText("");
}

public void refreshLog(String s) {
if(logText.equals(s)) {
return;
Expand Down
25 changes: 24 additions & 1 deletion MekHQ/src/mekhq/gui/dialog/CampaignOptionsDialog.java
Expand Up @@ -440,6 +440,10 @@ public class CampaignOptionsDialog extends javax.swing.JDialog {
private JCheckBox chkUnitMarketReportRefresh;

private JSpinner spnStartGameDelay;

// Miscellaneous tab
private JPanel panMisc;
private JCheckBox chkHistoricalDailyLog;

/**
* Creates new form CampaignOptionsDialog
Expand Down Expand Up @@ -631,6 +635,9 @@ private void initComponents() {
reverseQualityNames = new JCheckBox();

chkSupportStaffOnly = new JCheckBox();

panMisc = new JPanel();
chkHistoricalDailyLog = new JCheckBox();

ResourceBundle resourceMap = ResourceBundle.getBundle("mekhq.resources.CampaignOptionsDialog", new EncodeControl()); //$NON-NLS-1$
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
Expand Down Expand Up @@ -4131,7 +4138,22 @@ public void run() {
scrSPA.getVerticalScrollBar().setValue(0);
scrAtB.getVerticalScrollBar().setValue(0);
}
});
});

JScrollPane scrMisc = new JScrollPane(panMisc);
scrMisc.setPreferredSize(new java.awt.Dimension(500, 400));

chkHistoricalDailyLog.setText(resourceMap.getString("chkShowHistoricalDailyReport.text")); // NOI18N
chkHistoricalDailyLog.setToolTipText(resourceMap.getString("chkShowHistoricalDailyReport.toolTipText")); // NOI18N
chkHistoricalDailyLog.setName("chkHistoricalDailyLog"); // NOI18N
chkHistoricalDailyLog.setSelected(options.historicalDailyLog());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
panMisc.add(chkHistoricalDailyLog, gridBagConstraints);

tabOptions.addTab(resourceMap.getString("misc.TabConstraints.tabTitle"), scrMisc);

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
Expand Down Expand Up @@ -4502,6 +4524,7 @@ private void updateOptions() {
options.setIsAcquisitionPenalty((Integer) spnAcquireIsPenalty.getModel().getValue());
options.setMaxAcquisitions(Integer.parseInt(txtMaxAcquisitions.getText()));

options.setHistoricalDailyLog(chkHistoricalDailyLog.isSelected());

options.setNDiceTransitTime((Integer) spnNDiceTransitTime.getModel().getValue());
options.setConstantTransitTime((Integer) spnConstantTransitTime.getModel().getValue());
Expand Down
2 changes: 1 addition & 1 deletion MekHQ/src/mekhq/gui/dialog/DataLoadingDialog.java
Expand Up @@ -267,7 +267,7 @@ public Void doInBackground() {
campaign.generateNewPersonnelMarket();
campaign.reloadNews();
campaign.readNews();
campaign.addReport("<b>" + campaign.getDateAsString() + "</b>");
campaign.beginReport("<b>" + campaign.getDateAsString() + "</b>");
if (campaign.getCampaignOptions().getUseAtB()) {
RandomFactionGenerator.getInstance().updateTables(campaign.getDate(),
campaign.getLocation().getCurrentPlanet(),
Expand Down

0 comments on commit e5bce56

Please sign in to comment.