-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDwelling.java
More file actions
122 lines (104 loc) · 5.07 KB
/
Dwelling.java
File metadata and controls
122 lines (104 loc) · 5.07 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.dddheroes.heroesofddd.creaturerecruitment.write;
import com.dddheroes.heroesofddd.creaturerecruitment.write.builddwelling.BuildDwelling;
import com.dddheroes.heroesofddd.creaturerecruitment.events.DwellingBuilt;
import com.dddheroes.heroesofddd.creaturerecruitment.write.builddwelling.OnlyNotBuiltBuildingCanBeBuild;
import com.dddheroes.heroesofddd.creaturerecruitment.events.AvailableCreaturesChanged;
import com.dddheroes.heroesofddd.creaturerecruitment.write.changeavailablecreatures.IncreaseAvailableCreatures;
import com.dddheroes.heroesofddd.creaturerecruitment.write.changeavailablecreatures.OnlyBuiltDwellingCanHaveAvailableCreatures;
import com.dddheroes.heroesofddd.creaturerecruitment.events.CreatureRecruited;
import com.dddheroes.heroesofddd.creaturerecruitment.write.recruitcreature.RecruitCostCannotDifferThanExpectedCost;
import com.dddheroes.heroesofddd.creaturerecruitment.write.recruitcreature.RecruitCreature;
import com.dddheroes.heroesofddd.creaturerecruitment.write.recruitcreature.RecruitCreaturesNotExceedAvailableCreatures;
import com.dddheroes.heroesofddd.shared.domain.valueobjects.Amount;
import com.dddheroes.heroesofddd.shared.domain.valueobjects.Resources;
import com.dddheroes.heroesofddd.shared.domain.identifiers.CreatureId;
import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.eventsourcing.EventSourcingHandler;
import org.axonframework.modelling.command.AggregateCreationPolicy;
import org.axonframework.modelling.command.AggregateIdentifier;
import org.axonframework.modelling.command.CreationPolicy;
import org.axonframework.spring.stereotype.Aggregate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.axonframework.modelling.command.AggregateLifecycle.*;
@Aggregate(snapshotTriggerDefinition = "dwellingSnapshotTrigger")
public class Dwelling {
private static final Logger logger = LoggerFactory.getLogger(Dwelling.class);
@AggregateIdentifier
public DwellingId dwellingId; // needs to be public for snapshotting
public CreatureId creatureId;
public Resources costPerTroop;
public Amount availableCreatures;
@CommandHandler
@CreationPolicy(AggregateCreationPolicy.CREATE_IF_MISSING) // performance downside in comparison to constructor
void decide(BuildDwelling command) {
new OnlyNotBuiltBuildingCanBeBuild(dwellingId).verify();
apply(
DwellingBuilt.event(
command.dwellingId(),
command.creatureId(),
command.costPerTroop()
)
);
}
@EventSourcingHandler
void evolve(DwellingBuilt event) {
logger.info("🏗️ Dwelling built with ID: {}, creature type: {}", event.dwellingId(), event.creatureId());
this.dwellingId = new DwellingId(event.dwellingId());
this.creatureId = new CreatureId(event.creatureId());
this.costPerTroop = Resources.fromRaw(event.costPerTroop());
this.availableCreatures = Amount.zero();
}
@CommandHandler
void decide(IncreaseAvailableCreatures command) {
new OnlyBuiltDwellingCanHaveAvailableCreatures(dwellingId).verify();
// todo: check creatureId for the dwelling!
apply(
AvailableCreaturesChanged.event(
command.dwellingId(),
command.creatureId(),
availableCreatures.plus(command.increaseBy())
)
);
}
@EventSourcingHandler
void evolve(AvailableCreaturesChanged event) {
logger.info("📈 Available creatures changed for dwelling {}: {} creatures now available",
event.dwellingId(), event.changedTo());
this.availableCreatures = new Amount(event.changedTo());
}
@CommandHandler
void decide(RecruitCreature command) {
new RecruitCreaturesNotExceedAvailableCreatures(
creatureId,
availableCreatures,
command.creatureId(),
command.quantity()
).verify();
var recruitCost = costPerTroop.multiply(command.quantity());
new RecruitCostCannotDifferThanExpectedCost(
recruitCost,
command.expectedCost()
).verify();
apply(
CreatureRecruited.event(
command.dwellingId(),
command.creatureId(),
command.toArmy(),
command.quantity(),
recruitCost
)
);
}
@EventSourcingHandler
void evolve(CreatureRecruited event) {
logger.info("🧙 Recruited {} creatures of type {} from dwelling {} to army {}",
event.quantity(), event.creatureId(), event.dwellingId(), event.toArmy());
// todo: consider if it's OK or RecruitCreature should cause also AvailableCreaturesChanged event
this.availableCreatures = this.availableCreatures.minus(new Amount(event.quantity()));
}
Dwelling() {
logger.info("\uD83D\uDC80 Dwelling non-args constructor");
// required by Axon
}
}