Merge branch 'feature/quests' of github.com:Mineplex-LLC/Minecraft-PC into feature/quests
This commit is contained in:
commit
8d8e064125
@ -37,6 +37,14 @@ 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);
|
||||
}
|
||||
|
@ -8,4 +8,6 @@ public class PubSubChannels
|
||||
|
||||
public static final String QUEST_SUPPLIER_CHANNEL = "quest-manager";
|
||||
|
||||
public static final String QUEST_REQUEST_BASE = "quest-manager-request:";
|
||||
|
||||
}
|
||||
|
@ -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.");
|
||||
@ -156,6 +178,12 @@ public class QuestManager extends Thread
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
* this service will know whether to update them or not on the next startup. This is all that's
|
||||
@ -201,12 +229,6 @@ 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())
|
||||
|
Loading…
Reference in New Issue
Block a user