feat(succession): transfer tyrant reign on death
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package games.dmg.spigottyrant;
|
||||
|
||||
public final class BukkitManagedRoleEffects implements ManagedRoleEffects {
|
||||
private final TyrantStateManager stateManager;
|
||||
private final TyrantPresentation presentation;
|
||||
|
||||
public BukkitManagedRoleEffects(
|
||||
TyrantStateManager stateManager,
|
||||
TyrantPresentation presentation
|
||||
) {
|
||||
this.stateManager = stateManager;
|
||||
this.presentation = presentation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void suppressAll() {
|
||||
presentation.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restoreAll() {
|
||||
if (stateManager.game().lifecycle() == GameLifecycle.RUNNING) {
|
||||
presentation.reconcile(stateManager.game().tyrantId());
|
||||
} else {
|
||||
presentation.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package games.dmg.spigottyrant;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scoreboard.Scoreboard;
|
||||
import org.bukkit.scoreboard.ScoreboardManager;
|
||||
import org.bukkit.scoreboard.Team;
|
||||
|
||||
public final class BukkitTyrantPresentation implements TyrantPresentation {
|
||||
private static final String TEAM_NAME = "tyrant-red";
|
||||
private final Server server;
|
||||
private final Team team;
|
||||
private Optional<UUID> presentedTyrant = Optional.empty();
|
||||
|
||||
public BukkitTyrantPresentation(Server server) {
|
||||
this.server = server;
|
||||
ScoreboardManager manager = Objects.requireNonNull(
|
||||
server.getScoreboardManager(), "Scoreboard manager is unavailable"
|
||||
);
|
||||
Scoreboard scoreboard = manager.getMainScoreboard();
|
||||
Team existing = scoreboard.getTeam(TEAM_NAME);
|
||||
team = existing == null ? scoreboard.registerNewTeam(TEAM_NAME) : existing;
|
||||
team.setColor(ChatColor.RED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reconcile(Optional<UUID> tyrantId) {
|
||||
clear();
|
||||
presentedTyrant = tyrantId;
|
||||
tyrantId.map(server::getPlayer).ifPresent(player -> {
|
||||
team.addEntry(player.getName());
|
||||
player.setGlowing(true);
|
||||
player.setPlayerListName(ChatColor.RED + player.getName());
|
||||
player.setDisplayName(ChatColor.RED + player.getName());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
presentedTyrant.map(server::getPlayer).ifPresent(player -> {
|
||||
player.setGlowing(false);
|
||||
player.setPlayerListName(player.getName());
|
||||
player.setDisplayName(player.getName());
|
||||
});
|
||||
for (String entry : new HashSet<>(team.getEntries())) {
|
||||
team.removeEntry(entry);
|
||||
}
|
||||
presentedTyrant = Optional.empty();
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,20 @@ import org.bukkit.event.player.PlayerJoinEvent;
|
||||
public final class PlayerJoinListener implements Listener {
|
||||
private final TyrantStateManager stateManager;
|
||||
private final Clock clock;
|
||||
private final TyrantPresentation presentation;
|
||||
|
||||
public PlayerJoinListener(TyrantStateManager stateManager, Clock clock) {
|
||||
this(stateManager, clock, null);
|
||||
}
|
||||
|
||||
public PlayerJoinListener(
|
||||
TyrantStateManager stateManager,
|
||||
Clock clock,
|
||||
TyrantPresentation presentation
|
||||
) {
|
||||
this.stateManager = stateManager;
|
||||
this.clock = clock;
|
||||
this.presentation = presentation;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@@ -29,5 +39,11 @@ public final class PlayerJoinListener implements Listener {
|
||||
if (!reminder.isEmpty()) {
|
||||
event.getPlayer().sendMessage(reminder);
|
||||
}
|
||||
if (presentation != null
|
||||
&& stateManager.game().lifecycle() == GameLifecycle.RUNNING
|
||||
&& stateManager.game().tyrantId()
|
||||
.filter(event.getPlayer().getUniqueId()::equals).isPresent()) {
|
||||
presentation.reconcile(stateManager.game().tyrantId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
public final class SpigotTyrantPlugin extends JavaPlugin {
|
||||
private PluginSettings settings;
|
||||
private TyrantStateManager stateManager;
|
||||
private TyrantPresentation tyrantPresentation;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
@@ -27,6 +28,11 @@ public final class SpigotTyrantPlugin extends JavaPlugin {
|
||||
return;
|
||||
}
|
||||
|
||||
tyrantPresentation = new BukkitTyrantPresentation(getServer());
|
||||
ManagedRoleEffects managedEffects = new BukkitManagedRoleEffects(
|
||||
stateManager, tyrantPresentation
|
||||
);
|
||||
managedEffects.restoreAll();
|
||||
Objects.requireNonNull(getCommand("tyrant"), "Missing tyrant command metadata")
|
||||
.setExecutor(new TyrantCommand(stateManager, new TyrantProgressionService()));
|
||||
Objects.requireNonNull(getCommand("tyrantadmin"), "Missing tyrantadmin metadata")
|
||||
@@ -34,7 +40,7 @@ public final class SpigotTyrantPlugin extends JavaPlugin {
|
||||
stateManager,
|
||||
new GameLifecycleService(),
|
||||
new BukkitOnlinePlayerDirectory(),
|
||||
new NoopManagedRoleEffects(),
|
||||
managedEffects,
|
||||
Clock.systemUTC(),
|
||||
new RoleCandidateSelector(),
|
||||
settings.candidateActivityWindow(),
|
||||
@@ -42,7 +48,22 @@ public final class SpigotTyrantPlugin extends JavaPlugin {
|
||||
java.util.random.RandomGenerator.getDefault()
|
||||
));
|
||||
getServer().getPluginManager().registerEvents(
|
||||
new PlayerJoinListener(stateManager, Clock.systemUTC()),
|
||||
new PlayerJoinListener(stateManager, Clock.systemUTC(), tyrantPresentation),
|
||||
this
|
||||
);
|
||||
getServer().getPluginManager().registerEvents(
|
||||
new TyrantDeathListener(
|
||||
stateManager,
|
||||
new TyrantSuccessionService(
|
||||
new RoleCandidateSelector(),
|
||||
settings.candidateActivityWindow(),
|
||||
settings.pendingSelectionTimeout(),
|
||||
java.util.random.RandomGenerator.getDefault()
|
||||
),
|
||||
tyrantPresentation,
|
||||
getServer(),
|
||||
Clock.systemUTC()
|
||||
),
|
||||
this
|
||||
);
|
||||
getServer().getScheduler().runTaskTimer(this, stateManager::saveIfDirty, 600L, 600L);
|
||||
@@ -51,6 +72,9 @@ public final class SpigotTyrantPlugin extends JavaPlugin {
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
if (tyrantPresentation != null) {
|
||||
tyrantPresentation.clear();
|
||||
}
|
||||
if (stateManager != null) {
|
||||
stateManager.saveIfDirty();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package games.dmg.spigottyrant;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
|
||||
public final class TyrantDeathListener implements Listener {
|
||||
private final TyrantStateManager stateManager;
|
||||
private final TyrantSuccessionService succession;
|
||||
private final TyrantPresentation presentation;
|
||||
private final Server server;
|
||||
private final Clock clock;
|
||||
|
||||
public TyrantDeathListener(
|
||||
TyrantStateManager stateManager,
|
||||
TyrantSuccessionService succession,
|
||||
TyrantPresentation presentation,
|
||||
Server server,
|
||||
Clock clock
|
||||
) {
|
||||
this.stateManager = stateManager;
|
||||
this.succession = succession;
|
||||
this.presentation = presentation;
|
||||
this.server = server;
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onDeath(PlayerDeathEvent event) {
|
||||
UUID victimId = event.getEntity().getUniqueId();
|
||||
GameState game = stateManager.game();
|
||||
if (game.lifecycle() != GameLifecycle.RUNNING
|
||||
|| game.tyrantId().filter(victimId::equals).isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Optional<UUID> killerId = Optional.ofNullable(event.getEntity().getKiller())
|
||||
.map(Player::getUniqueId);
|
||||
Set<UUID> online = server.getOnlinePlayers().stream()
|
||||
.map(Player::getUniqueId)
|
||||
.collect(Collectors.toUnmodifiableSet());
|
||||
LifecycleState succeeded = succession.succeed(
|
||||
stateManager.snapshot(), killerId, online, clock.instant()
|
||||
);
|
||||
stateManager.replaceState(succeeded);
|
||||
stateManager.saveIfDirty();
|
||||
presentation.reconcile(succeeded.game().tyrantId());
|
||||
String successor = succeeded.game().tyrantId()
|
||||
.map(id -> {
|
||||
Player player = server.getPlayer(id);
|
||||
return player == null ? id.toString() : player.getName();
|
||||
})
|
||||
.orElse("pending selection");
|
||||
String message = ChatColor.RED + successor + " is the new Tyrant!";
|
||||
server.getOnlinePlayers().forEach(player -> player.sendMessage(message));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package games.dmg.spigottyrant;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface TyrantPresentation {
|
||||
void reconcile(Optional<UUID> tyrantId);
|
||||
|
||||
void clear();
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package games.dmg.spigottyrant;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.random.RandomGenerator;
|
||||
|
||||
public final class TyrantSuccessionService {
|
||||
private final RoleCandidateSelector candidates;
|
||||
private final Duration activityWindow;
|
||||
private final Duration pendingTimeout;
|
||||
private final RandomGenerator random;
|
||||
|
||||
public TyrantSuccessionService(
|
||||
RoleCandidateSelector candidates,
|
||||
Duration activityWindow,
|
||||
Duration pendingTimeout,
|
||||
RandomGenerator random
|
||||
) {
|
||||
this.candidates = candidates;
|
||||
this.activityWindow = activityWindow;
|
||||
this.pendingTimeout = pendingTimeout;
|
||||
this.random = random;
|
||||
}
|
||||
|
||||
public LifecycleState succeed(
|
||||
PersistentState state,
|
||||
Optional<UUID> killerId,
|
||||
Set<UUID> onlinePlayerIds,
|
||||
Instant now
|
||||
) {
|
||||
if (state.game().lifecycle() != GameLifecycle.RUNNING
|
||||
|| state.game().tyrantId().isEmpty()) {
|
||||
throw new IllegalStateException("a running Tyrant reign is required");
|
||||
}
|
||||
UUID formerTyrant = state.game().tyrantId().orElseThrow();
|
||||
Optional<UUID> formerVigilante = state.game().vigilanteId();
|
||||
Map<UUID, PlayerState> clearedPlayers = clearAssignments(state.players());
|
||||
Optional<UUID> selectedTyrant = killerId.isPresent()
|
||||
? killerId
|
||||
: candidates.select(
|
||||
clearedPlayers,
|
||||
Set.of(),
|
||||
Optional.of(formerTyrant),
|
||||
now,
|
||||
activityWindow,
|
||||
random
|
||||
);
|
||||
Optional<UUID> activeTyrant = selectedTyrant
|
||||
.filter(playerId -> killerId.isPresent() || onlinePlayerIds.contains(playerId));
|
||||
Optional<PendingSelection> pendingTyrant = selectedTyrant
|
||||
.filter(playerId -> activeTyrant.isEmpty())
|
||||
.map(playerId -> new PendingSelection(playerId, now.plus(pendingTimeout)));
|
||||
|
||||
Optional<UUID> selectedVigilante = activeTyrant.flatMap(tyrantId ->
|
||||
candidates.select(
|
||||
clearedPlayers,
|
||||
Set.of(tyrantId),
|
||||
formerVigilante,
|
||||
now,
|
||||
activityWindow,
|
||||
random
|
||||
)
|
||||
);
|
||||
Optional<UUID> activeVigilante = selectedVigilante
|
||||
.filter(onlinePlayerIds::contains);
|
||||
Optional<PendingSelection> pendingVigilante = selectedVigilante
|
||||
.filter(playerId -> activeVigilante.isEmpty())
|
||||
.map(playerId -> new PendingSelection(playerId, now.plus(pendingTimeout)));
|
||||
|
||||
GameState game = new GameState(
|
||||
GameLifecycle.RUNNING,
|
||||
activeTyrant,
|
||||
activeVigilante,
|
||||
pendingTyrant,
|
||||
pendingVigilante,
|
||||
Optional.empty(),
|
||||
state.game().accumulatedPausedTime(),
|
||||
0,
|
||||
1,
|
||||
Set.of()
|
||||
);
|
||||
return new LifecycleState(game, clearedPlayers);
|
||||
}
|
||||
|
||||
private static Map<UUID, PlayerState> clearAssignments(
|
||||
Map<UUID, PlayerState> players
|
||||
) {
|
||||
Map<UUID, PlayerState> cleared = new HashMap<>();
|
||||
for (PlayerState player : players.values()) {
|
||||
cleared.put(player.playerId(), new PlayerState(
|
||||
player.playerId(),
|
||||
player.latestName(),
|
||||
player.lastLogin(),
|
||||
player.optedOutUntil(),
|
||||
TyrantClass.NONE,
|
||||
Optional.empty(),
|
||||
Map.of(),
|
||||
Set.of(),
|
||||
player.capturedMobs()
|
||||
));
|
||||
}
|
||||
return Map.copyOf(cleared);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user