feat(succession): transfer tyrant reign on death
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package games.dmg.spigottyrant;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
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;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
final class BukkitTyrantPresentationTest {
|
||||
@Test
|
||||
void givesCurrentTyrantRedNameAndGlowingOutline() {
|
||||
UUID tyrantId = UUID.fromString("11111111-1111-1111-1111-111111111111");
|
||||
Server server = mock(Server.class);
|
||||
ScoreboardManager manager = mock(ScoreboardManager.class);
|
||||
Scoreboard scoreboard = mock(Scoreboard.class);
|
||||
Team team = mock(Team.class);
|
||||
Player tyrant = mock(Player.class);
|
||||
when(server.getScoreboardManager()).thenReturn(manager);
|
||||
when(manager.getMainScoreboard()).thenReturn(scoreboard);
|
||||
when(scoreboard.getTeam("tyrant-red")).thenReturn(null);
|
||||
when(scoreboard.registerNewTeam("tyrant-red")).thenReturn(team);
|
||||
when(server.getPlayer(tyrantId)).thenReturn(tyrant);
|
||||
when(tyrant.getName()).thenReturn("TyrantPlayer");
|
||||
BukkitTyrantPresentation presentation = new BukkitTyrantPresentation(server);
|
||||
|
||||
presentation.reconcile(Optional.of(tyrantId));
|
||||
|
||||
verify(team).setColor(ChatColor.RED);
|
||||
verify(team).addEntry("TyrantPlayer");
|
||||
verify(tyrant).setGlowing(true);
|
||||
verify(tyrant).setPlayerListName(ChatColor.RED + "TyrantPlayer");
|
||||
verify(tyrant).setDisplayName(ChatColor.RED + "TyrantPlayer");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package games.dmg.spigottyrant;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
final class TyrantDeathListenerTest {
|
||||
@Test
|
||||
void tyrantDeathAppliesSuccessionAndPresentation() {
|
||||
UUID tyrantId = UUID.fromString("11111111-1111-1111-1111-111111111111");
|
||||
UUID killerId = UUID.fromString("22222222-2222-2222-2222-222222222222");
|
||||
GameState game = new GameState(
|
||||
GameLifecycle.RUNNING, Optional.of(tyrantId), Optional.empty(),
|
||||
Optional.empty(), Optional.empty(), Optional.empty(), Duration.ZERO,
|
||||
0, 1, Set.of()
|
||||
);
|
||||
PersistentState before = new PersistentState(game, Map.of());
|
||||
GameState succeededGame = new GameState(
|
||||
GameLifecycle.RUNNING, Optional.of(killerId), Optional.empty(),
|
||||
Optional.empty(), Optional.empty(), Optional.empty(), Duration.ZERO,
|
||||
0, 1, Set.of()
|
||||
);
|
||||
LifecycleState succeeded = new LifecycleState(succeededGame, Map.of());
|
||||
TyrantStateManager manager = mock(TyrantStateManager.class);
|
||||
when(manager.game()).thenReturn(game);
|
||||
when(manager.snapshot()).thenReturn(before);
|
||||
TyrantSuccessionService succession = mock(TyrantSuccessionService.class);
|
||||
when(succession.succeed(eq(before), eq(Optional.of(killerId)), any(), any()))
|
||||
.thenReturn(succeeded);
|
||||
TyrantPresentation presentation = mock(TyrantPresentation.class);
|
||||
Server server = mock(Server.class);
|
||||
Player victim = mock(Player.class);
|
||||
when(victim.getUniqueId()).thenReturn(tyrantId);
|
||||
Player killer = mock(Player.class);
|
||||
when(killer.getUniqueId()).thenReturn(killerId);
|
||||
when(victim.getKiller()).thenReturn(killer);
|
||||
doReturn(List.of(victim, killer)).when(server).getOnlinePlayers();
|
||||
PlayerDeathEvent event = mock(PlayerDeathEvent.class);
|
||||
when(event.getEntity()).thenReturn(victim);
|
||||
TyrantDeathListener listener = new TyrantDeathListener(
|
||||
manager,
|
||||
succession,
|
||||
presentation,
|
||||
server,
|
||||
Clock.fixed(Instant.parse("2026-08-14T12:00:00Z"), ZoneOffset.UTC)
|
||||
);
|
||||
|
||||
listener.onDeath(event);
|
||||
|
||||
verify(manager).replaceState(succeeded);
|
||||
verify(presentation).reconcile(Optional.of(killerId));
|
||||
verify(killer).sendMessage(org.mockito.ArgumentMatchers.contains("new Tyrant"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package games.dmg.spigottyrant;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.random.RandomGenerator;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
final class TyrantSuccessionServiceTest {
|
||||
private static final UUID OLD_TYRANT = UUID.fromString("11111111-1111-1111-1111-111111111111");
|
||||
private static final UUID OLD_VIGILANTE = UUID.fromString("22222222-2222-2222-2222-222222222222");
|
||||
private static final UUID KILLER = UUID.fromString("33333333-3333-3333-3333-333333333333");
|
||||
private static final UUID OTHER = UUID.fromString("44444444-4444-4444-4444-444444444444");
|
||||
private static final Instant NOW = Instant.parse("2026-08-14T12:00:00Z");
|
||||
private final TyrantSuccessionService service = new TyrantSuccessionService(
|
||||
new RoleCandidateSelector(),
|
||||
Duration.ofHours(24),
|
||||
Duration.ofHours(24),
|
||||
RandomGenerator.of("L64X128MixRandom")
|
||||
);
|
||||
|
||||
@Test
|
||||
void playerKillerBecomesTyrantAndEntireReignIsReset() {
|
||||
PersistentState before = progressedState();
|
||||
|
||||
LifecycleState after = service.succeed(
|
||||
before, Optional.of(KILLER), Set.of(KILLER, OTHER), NOW
|
||||
);
|
||||
|
||||
assertEquals(Optional.of(KILLER), after.game().tyrantId());
|
||||
assertEquals(0, after.game().tyrantLevel());
|
||||
assertEquals(1, after.game().unspentChoices());
|
||||
assertEquals(Set.of(), after.game().purchases());
|
||||
assertNotEquals(after.game().tyrantId(), after.game().vigilanteId());
|
||||
for (PlayerState player : after.players().values()) {
|
||||
assertEquals(TyrantClass.NONE, player.tyrantClass());
|
||||
assertEquals(Optional.empty(), player.followerOf());
|
||||
assertEquals(Map.of(), player.cooldownEnds());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void environmentalDeathRandomlySelectsRecentPlayerInsteadOfFormerTyrant() {
|
||||
PersistentState before = progressedState();
|
||||
|
||||
LifecycleState after = service.succeed(before, Optional.empty(), Set.of(OTHER), NOW);
|
||||
|
||||
UUID selected = after.game().tyrantId().orElseGet(() ->
|
||||
after.game().pendingTyrant().orElseThrow().candidateId()
|
||||
);
|
||||
assertNotEquals(OLD_TYRANT, selected);
|
||||
}
|
||||
|
||||
private static PersistentState progressedState() {
|
||||
GameState game = new GameState(
|
||||
GameLifecycle.RUNNING, Optional.of(OLD_TYRANT), Optional.of(OLD_VIGILANTE),
|
||||
Optional.empty(), Optional.empty(), Optional.empty(), Duration.ZERO,
|
||||
5, 2, Set.of(TyrantUnlock.ASSASSIN, TyrantUnlock.STRENGTH)
|
||||
);
|
||||
Map<UUID, PlayerState> players = Map.of(
|
||||
OLD_TYRANT, active(new PlayerState(
|
||||
OLD_TYRANT, "OldTyrant", Optional.empty(), Optional.empty(),
|
||||
TyrantClass.NONE, Optional.empty(),
|
||||
Map.of(Ability.ROSTER_INTELLIGENCE, NOW.plusSeconds(20)),
|
||||
Set.of(), java.util.List.of()
|
||||
)),
|
||||
OLD_VIGILANTE, active(new PlayerState(
|
||||
OLD_VIGILANTE, "OldVigilante", Optional.empty(), Optional.empty(),
|
||||
TyrantClass.ASSASSIN, Optional.empty(),
|
||||
Map.of(Ability.ASSASSIN_INVISIBILITY, NOW.plusSeconds(20)),
|
||||
Set.of(Ability.ASSASSIN_INVISIBILITY), java.util.List.of()
|
||||
)),
|
||||
KILLER, active(PlayerState.newPlayer(KILLER, "Killer")),
|
||||
OTHER, active(new PlayerState(
|
||||
OTHER, "Other", Optional.empty(), Optional.empty(), TyrantClass.FIXER,
|
||||
Optional.of(OLD_VIGILANTE), Map.of(), Set.of(), java.util.List.of()
|
||||
))
|
||||
);
|
||||
return new PersistentState(game, players);
|
||||
}
|
||||
|
||||
private static PlayerState active(PlayerState player) {
|
||||
return new PlayerState(
|
||||
player.playerId(), player.latestName(), Optional.of(NOW.minusSeconds(60)),
|
||||
player.optedOutUntil(), player.tyrantClass(), player.followerOf(),
|
||||
player.cooldownEnds(), player.readyAbilityItems(), player.capturedMobs()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user