From 9b38558268683e2cf970654914a62ddcb1b2f224 Mon Sep 17 00:00:00 2001 From: Kenny Date: Mon, 8 May 2017 15:24:12 -0400 Subject: [PATCH] Have servers request active quests from quest service on startup --- .../quest/client/RedisQuestSupplier.java | 10 ++++- .../quest/common/redis/PubSubChannels.java | 2 + .../mineplex/quest/daemon/QuestManager.java | 44 ++++++++++++++----- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/Plugins/mineplex-questmanager/src/mineplex/quest/client/RedisQuestSupplier.java b/Plugins/mineplex-questmanager/src/mineplex/quest/client/RedisQuestSupplier.java index 1adf05571..128644bb5 100644 --- a/Plugins/mineplex-questmanager/src/mineplex/quest/client/RedisQuestSupplier.java +++ b/Plugins/mineplex-questmanager/src/mineplex/quest/client/RedisQuestSupplier.java @@ -37,12 +37,20 @@ public class RedisQuestSupplier implements QuestSupplier _plugin = plugin; _pubSub = pubSub; + String serverUniqueId = plugin.getConfig().getString("serverstatus.name"); + + // request current active quests, send server unique id so we can send a response just to this server + _pubSub.publish(PubSubChannels.QUEST_REQUEST_BASE, serverUniqueId); + + // update quests sent specifically to this server when it requests them (like on startup) + _pubSub.subscribe(PubSubChannels.QUEST_REQUEST_BASE + serverUniqueId, this::updateQuests); + // update quests when received _pubSub.subscribe(PubSubChannels.QUEST_SUPPLIER_CHANNEL, this::updateQuests); } private void updateQuests(String channel, String message) - { + { _lock.writeLock().lock(); try { diff --git a/Plugins/mineplex-questmanager/src/mineplex/quest/common/redis/PubSubChannels.java b/Plugins/mineplex-questmanager/src/mineplex/quest/common/redis/PubSubChannels.java index 886250950..f28490ec8 100644 --- a/Plugins/mineplex-questmanager/src/mineplex/quest/common/redis/PubSubChannels.java +++ b/Plugins/mineplex-questmanager/src/mineplex/quest/common/redis/PubSubChannels.java @@ -7,5 +7,7 @@ public class PubSubChannels { public static final String QUEST_SUPPLIER_CHANNEL = "quest-manager"; + + public static final String QUEST_REQUEST_BASE = "quest-manager-request:"; } diff --git a/Plugins/mineplex-questmanager/src/mineplex/quest/daemon/QuestManager.java b/Plugins/mineplex-questmanager/src/mineplex/quest/daemon/QuestManager.java index 03689d005..2dba4ccc1 100644 --- a/Plugins/mineplex-questmanager/src/mineplex/quest/daemon/QuestManager.java +++ b/Plugins/mineplex-questmanager/src/mineplex/quest/daemon/QuestManager.java @@ -10,6 +10,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.Optional; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -80,6 +81,22 @@ public class QuestManager extends Thread QuestDaemon.log("Active quests loaded from file:"); _activeQuests.forEach(quest -> QuestDaemon.log(quest.getName())); } + + // listen for servers requesting active quests on startup + _pubSub.subscribe(PubSubChannels.QUEST_REQUEST_BASE, this::handleQuestRequest); + } + + private void handleQuestRequest(String channel, String message) + { + // first make sure we have some active quests selected + if (_activeQuests.isEmpty()) + { + selectRandomQuests(); + } + + // send active quests to the server + String server = message; + publishActiveQuests(PubSubChannels.QUEST_REQUEST_BASE + server); } /** @@ -106,8 +123,14 @@ public class QuestManager extends Thread for (int i = 1; i < lines.size(); i++) { int uniqueId = Integer.parseInt(lines.get(i)); - Quest quest = getById(uniqueId); - _activeQuests.add(quest); + Optional quest = Quests.fromId(uniqueId); + if (!quest.isPresent()) + { + QuestDaemon.log("Tried to load active quest that doesn't exist: " + uniqueId); + continue; + } + + _activeQuests.add(quest.get()); } } } @@ -139,8 +162,7 @@ public class QuestManager extends Thread selectRandomQuests(); // publish new quests - _pubSub.publish(PubSubChannels.QUEST_SUPPLIER_CHANNEL, - serialize(_activeQuests)); + publishActiveQuests(PubSubChannels.QUEST_SUPPLIER_CHANNEL); } QuestDaemon.log("Done updating active quests."); @@ -155,6 +177,12 @@ public class QuestManager extends Thread QuestDaemon.log(e); } } + + private void publishActiveQuests(String channel) + { + _pubSub.publish(channel, + serialize(_activeQuests)); + } /** * Called on shutdown of this service. Writes the date quests were last updated to a file, so @@ -200,13 +228,7 @@ public class QuestManager extends Thread { _recentlySelectedQuestsRepo.clean(); } - - public Quest getById(int uniqueId) - { - return _quests.values().stream().filter(q -> q.getUniqueId() == uniqueId).findFirst() - .orElse(null); - } - + private void selectRandomQuests() { if (!_activeQuests.isEmpty())