Skip to content

Commit

Permalink
Add transmission of TOEs between campaigns
Browse files Browse the repository at this point in the history
  • Loading branch information
sixlettervariables committed Mar 13, 2020
1 parent 24e5684 commit b97c54a
Show file tree
Hide file tree
Showing 11 changed files with 646 additions and 73 deletions.
47 changes: 47 additions & 0 deletions MekHQ/src/main/proto/Service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,50 @@ message LogEntry {
// The daily log entry.
string entry = 1;
}

// Represents a message sent when the TOE for a Campaign
// is updated.
//
// This message may be sent by any Campaign, and will
// be forwarded by the Host Campaign to everyone in
// the current session.
//
// This message may be restricted to campaigns on the
// same team.
message TOEUpdated {
// The unique identifier of the Campaign.
string id = 1;

// The top level Force in the Campaign.
Force force = 2;
}

// Represents a Force within a Campaign's TOE.
message Force {
// The unique identifier of the Force.
int32 id = 1;

// The name of the force.
string name = 2;

// Zero or more sub-forces assigned
// to this Force.
repeated Force sub_forces = 3;

// Zero or more units attached directly
// to this Force.
repeated ForceUnit units = 4;
}

// Represents a unit within a Force in a Campaign's
// TOE.
message ForceUnit {
// The unique identifier of the Unit.
string id = 1;

// The name of the Unit.
string name = 2;

// The name of the Unit's commander.
string commander = 3;
}
11 changes: 11 additions & 0 deletions MekHQ/src/mekhq/campaign/CampaignController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@
import mekhq.campaign.universe.PlanetarySystem;
import mekhq.campaign.universe.Systems;
import mekhq.online.events.WaitingToAdvanceDayEvent;
import mekhq.online.forces.RemoteForce;
import mekhq.online.forces.RemoteTOE;

/**
* Manages the timeline of a {@link Campaign}.
*/
public class CampaignController {
private final Campaign localCampaign;
private final ConcurrentHashMap<UUID, RemoteCampaign> remoteCampaigns = new ConcurrentHashMap<>();
private final ConcurrentHashMap<UUID, RemoteTOE> remoteTOEs = new ConcurrentHashMap<>();

private boolean isHost;
private UUID host;
Expand Down Expand Up @@ -183,4 +186,12 @@ public boolean advanceDay() {
}
}
}

public void updateTOE(UUID campaignId, RemoteForce forces) {
remoteTOEs.put(campaignId, new RemoteTOE(forces));
}

public RemoteTOE getTOE(UUID campaignId) {
return remoteTOEs.get(campaignId);
}
}
23 changes: 23 additions & 0 deletions MekHQ/src/mekhq/gui/OnlineTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.UUID;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.SwingUtilities;

import megamek.common.event.Subscribe;
Expand All @@ -41,7 +43,9 @@
import mekhq.campaign.CampaignController;
import mekhq.campaign.RemoteCampaign;
import mekhq.gui.model.OnlineCampaignsTableModel;
import mekhq.gui.model.RemoteOrgTreeModel;
import mekhq.online.events.CampaignListUpdatedEvent;
import mekhq.online.forces.RemoteTOE;
import mekhq.preferences.PreferencesNode;

public final class OnlineTab extends CampaignGuiTab implements ActionListener {
Expand All @@ -55,6 +59,8 @@ public final class OnlineTab extends CampaignGuiTab implements ActionListener {
private OnlineCampaignsTableModel campaignsTableModel;
private JTable campaignsTable;

private JTree selectedCampaignToeTree;

OnlineTab(CampaignGUI gui, String name) {
super(gui, name);
MekHQ.registerHandler(this);
Expand Down Expand Up @@ -83,6 +89,10 @@ public void initTab() {

hostCampaignTableModel = new OnlineCampaignsTableModel(getHostCampaignAsList());
hostCampaignTable = new JTable(hostCampaignTableModel);
hostCampaignTable.getSelectionModel().addListSelectionListener(e -> {
RemoteTOE toe = getCampaignController().getTOE(getCampaignController().getHost());
selectedCampaignToeTree.setModel(new RemoteOrgTreeModel(toe != null ? toe : RemoteTOE.EMPTY_TOE));
});

JScrollPane hostCampaignScrollPane = new JScrollPane(hostCampaignTable);
JPanel hostCampaignTablePanel = new JPanel(new GridLayout(0, 1));
Expand All @@ -92,6 +102,14 @@ public void initTab() {

campaignsTableModel = new OnlineCampaignsTableModel(getCampaignController().getRemoteCampaigns());
campaignsTable = new JTable(campaignsTableModel);
campaignsTable.getSelectionModel().addListSelectionListener(e -> {
int row = e.getFirstIndex();
if (row >= 0) {
UUID id = (UUID)campaignsTableModel.getValueAt(row, OnlineCampaignsTableModel.COL_ID);
RemoteTOE toe = getCampaignController().getTOE(id);
selectedCampaignToeTree.setModel(new RemoteOrgTreeModel(toe != null ? toe : RemoteTOE.EMPTY_TOE));
}
});

JScrollPane campaignsTableScrollPane = new JScrollPane(campaignsTable);
JPanel campaignsTablePanel = new JPanel(new GridLayout(0, 1));
Expand Down Expand Up @@ -120,6 +138,11 @@ public void initTab() {

JPanel campaignDetailsPanel = new JPanel();

selectedCampaignToeTree = new JTree();
selectedCampaignToeTree.setCellRenderer(new RemoteForceRenderer());

campaignDetailsPanel.add(selectedCampaignToeTree);

JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topPanel,
campaignDetailsPanel);
splitPane.setOneTouchExpandable(true);
Expand Down
72 changes: 72 additions & 0 deletions MekHQ/src/mekhq/gui/RemoteForceRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2020 The MegaMek Team.
*
* 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/>.
*/
package mekhq.gui;

import java.awt.Component;

import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultTreeCellRenderer;

import mekhq.online.forces.RemoteForce;
import mekhq.online.forces.RemoteUnit;

public class RemoteForceRenderer extends DefaultTreeCellRenderer {

private static final long serialVersionUID = 1L;

public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus) {

super.getTreeCellRendererComponent(
tree, value, sel,
expanded, leaf, row,
hasFocus);
setBackground(UIManager.getColor("Tree.background"));
setForeground(UIManager.getColor("Tree.textForeground"));
if (sel) {
setBackground(UIManager.getColor("Tree.selectionBackground"));
setForeground(UIManager.getColor("Tree.selectionForeground"));
}

if (value instanceof RemoteUnit) {
RemoteUnit u = (RemoteUnit)value;
String name = u.getCommander();
if (name == null) {
name = "<font color='red'>No Crew</font>";
}
String uname = "<i>" + u.getName() + "</i>";

setText("<html>" + name + ", " + uname + "</html>");
}
if (value instanceof RemoteForce) {
RemoteForce f = (RemoteForce)value;

setText("<html>" + f.getName() + "</html>");
}

return this;
}
}
95 changes: 95 additions & 0 deletions MekHQ/src/mekhq/gui/model/RemoteOrgTreeModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2020 The MegaMek Team.
*
* 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/>.
*/
package mekhq.gui.model;

import java.util.Vector;

import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import mekhq.online.forces.RemoteForce;
import mekhq.online.forces.RemoteTOE;
import mekhq.online.forces.RemoteUnit;

public class RemoteOrgTreeModel implements TreeModel {

private RemoteForce rootForce;
private Vector<TreeModelListener> listeners = new Vector<TreeModelListener>();

public RemoteOrgTreeModel() {
rootForce = RemoteForce.emptyForce();
}

public RemoteOrgTreeModel(RemoteTOE toe) {
rootForce = toe.getForces();
}

@Override
public Object getChild(Object parent, int index) {
if(parent instanceof RemoteForce) {
return ((RemoteForce)parent).getAllChildren().get(index);
}
return null;
}

@Override
public int getChildCount(Object parent) {
if(parent instanceof RemoteForce) {
return ((RemoteForce)parent).getAllChildren().size();
}
return 0;
}

@Override
public int getIndexOfChild(Object parent, Object child) {
if(parent instanceof RemoteForce) {
return ((RemoteForce)parent).getAllChildren().indexOf(child);
}
return 0;
}

@Override
public Object getRoot() {
return rootForce;
}

@Override
public boolean isLeaf(Object node) {
return node instanceof RemoteUnit
|| (node instanceof RemoteForce && ((RemoteForce)node).getAllChildren().size() == 0);
}

@Override
public void valueForPathChanged(TreePath arg0, Object arg1) {
// TODO Auto-generated method stub
}

public void addTreeModelListener( TreeModelListener listener ) {
if ( listener != null && !listeners.contains( listener ) ) {
listeners.addElement( listener );
}
}

public void removeTreeModelListener( TreeModelListener listener ) {
if ( listener != null ) {
listeners.removeElement( listener );
}
}
}
Loading

0 comments on commit b97c54a

Please sign in to comment.