-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWhenCreatureRecruitedThenAddToArmyProcessor.java
More file actions
47 lines (42 loc) · 1.88 KB
/
WhenCreatureRecruitedThenAddToArmyProcessor.java
File metadata and controls
47 lines (42 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.dddheroes.heroesofddd.creaturerecruitment.automation;
import com.dddheroes.heroesofddd.armies.write.addcreature.AddCreatureToArmy;
import com.dddheroes.heroesofddd.creaturerecruitment.write.changeavailablecreatures.IncreaseAvailableCreatures;
import com.dddheroes.heroesofddd.creaturerecruitment.events.CreatureRecruited;
import com.dddheroes.heroesofddd.shared.application.GameMetaData;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.axonframework.config.ProcessingGroup;
import org.axonframework.eventhandling.DisallowReplay;
import org.axonframework.eventhandling.EventHandler;
import org.axonframework.messaging.annotation.MetaDataValue;
import org.springframework.stereotype.Component;
@ProcessingGroup("Automation_WhenCreatureRecruitedThenAddToArmy_Processor")
@DisallowReplay
@Component
class WhenCreatureRecruitedThenAddToArmyProcessor {
private final CommandGateway commandGateway;
WhenCreatureRecruitedThenAddToArmyProcessor(CommandGateway commandGateway) {
this.commandGateway = commandGateway;
}
@EventHandler
void react(
CreatureRecruited event,
@MetaDataValue(GameMetaData.GAME_ID_KEY) String gameId,
@MetaDataValue(GameMetaData.PLAYER_ID_KEY) String playerId
) {
try {
var command = AddCreatureToArmy.command(
event.toArmy(),
event.creatureId(),
event.quantity()
);
commandGateway.sendAndWait(command, GameMetaData.with(gameId, playerId));
} catch (Exception e) {
var compensatingAction = IncreaseAvailableCreatures.command(
event.dwellingId(),
event.creatureId(),
event.quantity()
);
commandGateway.sendAndWait(compensatingAction, GameMetaData.with(gameId, playerId));
}
}
}