|
| 1 | +package io.github.legendaryforge.legendary.core.internal.legendary.arena; |
| 2 | + |
| 3 | +import io.github.legendaryforge.legendary.core.api.encounter.EncounterDefinition; |
| 4 | +import io.github.legendaryforge.legendary.core.api.encounter.EncounterInstance; |
| 5 | +import io.github.legendaryforge.legendary.core.api.encounter.EncounterKey; |
| 6 | +import io.github.legendaryforge.legendary.core.api.encounter.EncounterManager; |
| 7 | +import io.github.legendaryforge.legendary.core.api.encounter.EndReason; |
| 8 | +import io.github.legendaryforge.legendary.core.api.encounter.JoinResult; |
| 9 | +import io.github.legendaryforge.legendary.core.api.encounter.ParticipationRole; |
| 10 | +import io.github.legendaryforge.legendary.core.api.id.ResourceId; |
| 11 | +import java.lang.reflect.Proxy; |
| 12 | +import java.util.Optional; |
| 13 | +import java.util.Set; |
| 14 | +import java.util.UUID; |
| 15 | +import java.util.concurrent.ConcurrentHashMap; |
| 16 | +import java.util.concurrent.atomic.AtomicInteger; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.*; |
| 20 | + |
| 21 | +final class LegendaryRevokedRejoinEnforcingEncounterManagerTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + void deniesParticipantRejoinWhenRevokedAndActive() { |
| 25 | + UUID instanceId = UUID.fromString("00000000-0000-0000-0000-000000000310"); |
| 26 | + UUID playerId = UUID.fromString("00000000-0000-0000-0000-000000000311"); |
| 27 | + |
| 28 | + Set<UUID> legendaryInstances = ConcurrentHashMap.newKeySet(); |
| 29 | + legendaryInstances.add(instanceId); |
| 30 | + |
| 31 | + PhaseGateInvariant phaseGate = new PhaseGateInvariant(); |
| 32 | + phaseGate.onStart(instanceId); |
| 33 | + |
| 34 | + ArenaRevocationTracker revocations = new ArenaRevocationTracker(); |
| 35 | + revocations.revoke(instanceId, playerId); |
| 36 | + |
| 37 | + AtomicInteger delegateCalls = new AtomicInteger(); |
| 38 | + EncounterManager delegate = new EncounterManager() { |
| 39 | + @Override public EncounterInstance create(EncounterDefinition definition, io.github.legendaryforge.legendary.core.api.encounter.EncounterContext context) { return null; } |
| 40 | + @Override public JoinResult join(UUID pid, EncounterInstance inst, ParticipationRole role) { delegateCalls.incrementAndGet(); return JoinResult.SUCCESS; } |
| 41 | + @Override public void leave(UUID pid, EncounterInstance inst) {} |
| 42 | + @Override public void end(EncounterInstance inst, EndReason reason) {} |
| 43 | + @Override public Optional<EncounterInstance> byInstanceId(UUID id) { return Optional.empty(); } |
| 44 | + @Override public Optional<EncounterInstance> byKey(EncounterKey key) { return Optional.empty(); } |
| 45 | + }; |
| 46 | + |
| 47 | + EncounterManager mgr = new LegendaryRevokedRejoinEnforcingEncounterManager( |
| 48 | + delegate, legendaryInstances::contains, phaseGate, revocations); |
| 49 | + |
| 50 | + EncounterInstance instance = proxyEncounterInstance(instanceId); |
| 51 | + |
| 52 | + assertEquals(JoinResult.DENIED_STATE, mgr.join(playerId, instance, ParticipationRole.PARTICIPANT)); |
| 53 | + assertEquals(0, delegateCalls.get()); |
| 54 | + |
| 55 | + assertEquals(JoinResult.SUCCESS, mgr.join(playerId, instance, ParticipationRole.SPECTATOR)); |
| 56 | + assertEquals(1, delegateCalls.get()); |
| 57 | + } |
| 58 | + |
| 59 | + private static EncounterInstance proxyEncounterInstance(UUID instanceId) { |
| 60 | + EncounterDefinition def = proxyEncounterDefinition(); |
| 61 | + return (EncounterInstance) Proxy.newProxyInstance( |
| 62 | + EncounterInstance.class.getClassLoader(), |
| 63 | + new Class<?>[] { EncounterInstance.class }, |
| 64 | + (proxy, method, args) -> { |
| 65 | + return switch (method.getName()) { |
| 66 | + case "instanceId" -> instanceId; |
| 67 | + case "definition" -> def; |
| 68 | + case "key" -> Optional.empty(); |
| 69 | + default -> { |
| 70 | + Class<?> rt = method.getReturnType(); |
| 71 | + if (rt.equals(Optional.class)) yield Optional.empty(); |
| 72 | + if (rt.equals(int.class)) yield 0; |
| 73 | + if (rt.equals(boolean.class)) yield false; |
| 74 | + yield null; |
| 75 | + } |
| 76 | + }; |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + private static EncounterDefinition proxyEncounterDefinition() { |
| 81 | + ResourceId id = ResourceId.parse("test:any"); |
| 82 | + return (EncounterDefinition) Proxy.newProxyInstance( |
| 83 | + EncounterDefinition.class.getClassLoader(), |
| 84 | + new Class<?>[] { EncounterDefinition.class }, |
| 85 | + (proxy, method, args) -> switch (method.getName()) { |
| 86 | + case "id" -> id; |
| 87 | + case "displayName" -> "test"; |
| 88 | + case "accessPolicy" -> io.github.legendaryforge.legendary.core.api.encounter.EncounterAccessPolicy.PUBLIC; |
| 89 | + case "spectatorPolicy" -> io.github.legendaryforge.legendary.core.api.encounter.SpectatorPolicy.ALLOW_VIEW_ONLY; |
| 90 | + case "maxParticipants" -> 0; |
| 91 | + case "maxSpectators" -> 0; |
| 92 | + default -> null; |
| 93 | + }); |
| 94 | + } |
| 95 | +} |
0 commit comments