Merge branch 'feature/quests' of github.com:Mineplex-LLC/Minecraft-PC into feature/quests

This commit is contained in:
Sarah 2017-05-11 18:09:11 +02:00
commit 8d8e064125
3 changed files with 44 additions and 12 deletions

View File

@ -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
{

View File

@ -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:";
}

View File

@ -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> 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())