|
4 | 4 | import io.github.legendaryforge.legendary.core.api.activation.ActivationAttemptStatus; |
5 | 5 | import io.github.legendaryforge.legendary.core.api.activation.ActivationDecision; |
6 | 6 | import io.github.legendaryforge.legendary.core.api.activation.ActivationService; |
| 7 | +import io.github.legendaryforge.legendary.core.api.activation.session.ActivationSessionBeginResult; |
| 8 | +import io.github.legendaryforge.legendary.core.api.activation.session.ActivationSessionBeginStatus; |
| 9 | +import io.github.legendaryforge.legendary.core.api.activation.session.ActivationSessionService; |
| 10 | +import io.github.legendaryforge.legendary.core.api.encounter.EncounterKey; |
| 11 | +import io.github.legendaryforge.legendary.core.api.gate.ConditionGate; |
| 12 | +import io.github.legendaryforge.legendary.core.api.gate.GateDecision; |
| 13 | +import io.github.legendaryforge.legendary.core.api.gate.GateService; |
7 | 14 | import io.github.legendaryforge.legendary.core.api.id.ResourceId; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Objects; |
8 | 17 | import java.util.Optional; |
9 | 18 |
|
10 | 19 | public final class DefaultActivationService implements ActivationService { |
11 | 20 |
|
| 21 | + private final GateService gates; |
| 22 | + private final ActivationSessionService sessions; |
| 23 | + |
| 24 | + public DefaultActivationService(GateService gates, ActivationSessionService sessions) { |
| 25 | + this.gates = Objects.requireNonNull(gates, "gates"); |
| 26 | + this.sessions = Objects.requireNonNull(sessions, "sessions"); |
| 27 | + } |
| 28 | + |
12 | 29 | @Override |
13 | 30 | public ActivationAttemptResult attemptActivation(ActivationAttemptRequest request) { |
14 | | - // Intentionally inert until Phase 1 · Step 3 wiring (authority + gate + session + encounter creation). |
| 31 | + Objects.requireNonNull(request, "request"); |
| 32 | + |
| 33 | + EncounterKey encounterKey = EncounterKey.of(request.definition(), request.context()); |
| 34 | + |
| 35 | + // Phase 2: evaluate activation gate (if present) |
| 36 | + ActivationDecision decision = request.activationGateKey() |
| 37 | + .map(gateKey -> evaluateGate(gateKey, request, encounterKey)) |
| 38 | + .orElseGet(ActivationDecision::allow); |
| 39 | + |
| 40 | + if (!decision.allowed()) { |
| 41 | + return new ActivationAttemptResult( |
| 42 | + ActivationAttemptStatus.FAILED, |
| 43 | + decision, |
| 44 | + Optional.empty(), |
| 45 | + Optional.empty()); |
| 46 | + } |
| 47 | + |
| 48 | + // Phase 2: begin activation session |
| 49 | + ActivationSessionBeginResult begin = sessions.begin( |
| 50 | + new ActivationSessionService.ActivationSessionBeginRequest( |
| 51 | + request.activatorId(), |
| 52 | + encounterKey, |
| 53 | + request.definition(), |
| 54 | + request.context(), |
| 55 | + request.activationGateKey(), |
| 56 | + decision.attributes() |
| 57 | + ) |
| 58 | + ); |
| 59 | + |
| 60 | + if (begin.status() == ActivationSessionBeginStatus.CREATED || begin.status() == ActivationSessionBeginStatus.EXISTING) { |
| 61 | + return new ActivationAttemptResult( |
| 62 | + ActivationAttemptStatus.SUCCESS, |
| 63 | + decision, |
| 64 | + Optional.of(begin.sessionId()), |
| 65 | + Optional.empty()); |
| 66 | + } |
| 67 | + |
| 68 | + // DENIED => FAILED |
| 69 | + ActivationDecision denied = ActivationDecision.deny(begin.reasonCode(), decision.attributes()); |
15 | 70 | return new ActivationAttemptResult( |
16 | 71 | ActivationAttemptStatus.FAILED, |
17 | | - ActivationDecision.deny(ResourceId.of("legendarycore", "not_wired")), |
| 72 | + denied, |
18 | 73 | Optional.empty(), |
19 | 74 | Optional.empty()); |
20 | 75 | } |
| 76 | + |
| 77 | + private ActivationDecision evaluateGate(ResourceId gateKey, ActivationAttemptRequest request, EncounterKey encounterKey) { |
| 78 | + ConditionGate.GateRequest gateRequest = new ConditionGate.GateRequest( |
| 79 | + gateKey, |
| 80 | + request.activatorId(), |
| 81 | + Optional.of(encounterKey), |
| 82 | + Optional.empty(), |
| 83 | + Optional.empty(), |
| 84 | + Map.of() |
| 85 | + ); |
| 86 | + |
| 87 | + GateDecision gateDecision = gates.evaluate(gateRequest); |
| 88 | + return gateDecision.allowed() |
| 89 | + ? ActivationDecision.allow() |
| 90 | + : ActivationDecision.deny(gateDecision.reasonCode(), gateDecision.attributes()); |
| 91 | + } |
21 | 92 | } |
0 commit comments