Skip to content

Commit

Permalink
Improve handling of missing vs inactive campaigns
Browse files Browse the repository at this point in the history
  • Loading branch information
sixlettervariables committed Mar 13, 2020
1 parent 6caccc9 commit ad4b36a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
15 changes: 12 additions & 3 deletions MekHQ/src/mekhq/campaign/CampaignController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package mekhq.campaign;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -122,15 +123,23 @@ public boolean getHostIsGMMode() {
return hostIsGM;
}

public void addRemoteCampaign(UUID id, String name, DateTime date, String locationId, boolean isGMMode) {
public void addActiveRemoteCampaign(UUID id, String name, DateTime date, String locationId, boolean isGMMode) {
addRemoteCampaign(id, name, date, locationId, isGMMode, /*isActive:*/ true);
}

public void addRemoteCampaign(UUID id, String name, DateTime date, String locationId, boolean isGMMode, boolean isActive) {
PlanetarySystem planetarySystem = Systems.getInstance().getSystemById(locationId);
remoteCampaigns.put(id, new RemoteCampaign(id, name, date, planetarySystem, isGMMode, true));
remoteCampaigns.put(id, new RemoteCampaign(id, name, date, planetarySystem, isGMMode, isActive));
}

public Collection<RemoteCampaign> getRemoteCampaigns() {
return remoteCampaigns.values();
return Collections.unmodifiableCollection(remoteCampaigns.values());
}

public Collection<UUID> getRemoteCampaignIds() {
return Collections.unmodifiableSet(remoteCampaigns.keySet());
}

public void setRemoteCampaignDate(UUID campaignId, DateTime campaignDate) {
// We only update this if the remote campaign is actually present
// otherwise the next PING-PONG will catch us up.
Expand Down
23 changes: 17 additions & 6 deletions MekHQ/src/mekhq/online/MekHQClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public void connect() {
knownCampaigns.add(clientId);

controller.addRemoteCampaign(clientId, details.getName(),
dateFormatter.parseDateTime(details.getDate()), details.getLocation(), details.getIsGMMode());
dateFormatter.parseDateTime(details.getDate()), details.getLocation(), details.getIsGMMode(),
details.getIsActive());
}

createMessageBus();
Expand Down Expand Up @@ -222,19 +223,29 @@ protected void handlePong(UUID id, Pong pong) {
controller.setHostLocation(locationId);
controller.setHostIsGMMode(hostCampaign.getIsGMMode());

Set<UUID> foundCampaigns = new HashSet<>();
foundCampaigns.add(id);
// gather the IDs of all of the campaigns we know about
Set<UUID> allCampaigns = new HashSet<>(controller.getRemoteCampaignIds());

for (CampaignDetails campaign : pong.getCampaignsList()) {
UUID clientId = UUID.fromString(campaign.getId());

foundCampaigns.add(clientId);
// remove this campaign from the list we know about
// so that when this ends we'll be left with a set
// of campaigns that our host doesn't know about
// or isn't active anymore. We take into account the
// ACTIVE status from the Host campaign below.
allCampaigns.remove(clientId);

controller.addRemoteCampaign(clientId, campaign.getName(),
dateFormatter.parseDateTime(campaign.getDate()), campaign.getLocation(), campaign.getIsGMMode());
dateFormatter.parseDateTime(campaign.getDate()), campaign.getLocation(), campaign.getIsGMMode(),
campaign.getIsActive());
}

controller.computeInactiveCampaigns(foundCampaigns);
// Account for any missing Campaigns on this Host
// they are all automatically considered INACTIVE.
for (UUID missingCampaignId : allCampaigns) {
controller.removeActiveCampaign(missingCampaignId);
}

MekHQ.triggerEvent(new CampaignListUpdatedEvent());
}
Expand Down
10 changes: 8 additions & 2 deletions MekHQ/src/mekhq/online/MekHQServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public void connect(ConnectionRequest request, StreamObserver<ConnectionResponse
.addAllCampaigns(convert(controller.getRemoteCampaigns())).build();
responseObserver.onNext(response);

controller.addRemoteCampaign(id, clientCampaign.getName(), DateTime.parse(clientCampaign.getDate()),
controller.addActiveRemoteCampaign(id, clientCampaign.getName(), DateTime.parse(clientCampaign.getDate()),
clientCampaign.getLocation(), clientCampaign.getIsGMMode());

responseObserver.onCompleted();
Expand Down Expand Up @@ -245,6 +245,8 @@ public void onNext(ClientMessage message) {
.withCause(e) // This can be attached to the Status locally, but NOT transmitted to the client!
.asRuntimeException());

controller.removeActiveCampaign(clientId);

MekHQ.triggerEvent(new CampaignDisconnectedEvent(clientId));
}
}
Expand All @@ -256,6 +258,8 @@ public void onError(Throwable t) {

messageBus.remove(clientId);

controller.removeActiveCampaign(clientId);

if (clientId != null) {
MekHQ.triggerEvent(new CampaignDisconnectedEvent(clientId));
}
Expand All @@ -266,6 +270,8 @@ public void onCompleted() {
messageBus.remove(clientId);
responseObserver.onCompleted();

controller.removeActiveCampaign(clientId);

if (clientId != null) {
MekHQ.triggerEvent(new CampaignDisconnectedEvent(clientId));
}
Expand Down Expand Up @@ -338,7 +344,7 @@ private void handlePong(StreamObserver<ServerMessage> responseObserver, UUID cam

CampaignDetails clientCampaign = pong.getCampaign();
UUID clientId = UUID.fromString(clientCampaign.getId());
controller.addRemoteCampaign(clientId, clientCampaign.getName(),
controller.addActiveRemoteCampaign(clientId, clientCampaign.getName(),
dateFormatter.parseDateTime(clientCampaign.getDate()), clientCampaign.getLocation(),
clientCampaign.getIsGMMode());

Expand Down

0 comments on commit ad4b36a

Please sign in to comment.