Skip to content

Commit a2975dd

Browse files
✨ feat: handle playerId on metadata level (#14)
1 parent ec36933 commit a2975dd

17 files changed

Lines changed: 115 additions & 36 deletions

File tree

src/main/java/com/dddheroes/heroesofddd/GameConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public class GameConfiguration {
1111

1212
@Bean
1313
public SequencingPolicy<EventMessage<?>> gameIdSequencingPolicy() {
14-
return e -> e.getMetaData().get(GameMetaData.KEY);
14+
return e -> e.getMetaData().get(GameMetaData.GAME_ID_KEY);
1515
}
1616
}

src/main/java/com/dddheroes/heroesofddd/astrologers/automation/whenweekstartedthenproclaimweeksymbol/WhenWeekStartedThenProclaimWeekSymbolProcessor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ class WhenWeekStartedThenProclaimWeekSymbolProcessor {
3131
}
3232

3333
@EventHandler
34-
void react(DayStarted event, @MetaDataValue(GameMetaData.KEY) String gameId) {
34+
void react(
35+
DayStarted event,
36+
@MetaDataValue(GameMetaData.GAME_ID_KEY) String gameId,
37+
@MetaDataValue(GameMetaData.PLAYER_ID_KEY) String playerId
38+
) {
3539
var isWeekStarted = event.day() == FIRST_DAY_OF_THE_WEEK;
3640
if (isWeekStarted) {
3741
var weekSymbol = weekSymbolCalculator.apply(MonthWeek.of(event.month(), event.week()));
@@ -42,7 +46,7 @@ void react(DayStarted event, @MetaDataValue(GameMetaData.KEY) String gameId) {
4246
weekSymbol.weekOf().raw(),
4347
weekSymbol.growth()
4448
);
45-
commandGateway.sendAndWait(command, GameMetaData.withId(gameId));
49+
commandGateway.sendAndWait(command, GameMetaData.with(gameId, playerId));
4650
}
4751
}
4852
}

src/main/java/com/dddheroes/heroesofddd/astrologers/automation/whenweeksymbolproclaimedthenincreasedwellingavailablecreatures/WhenWeekSymbolProclaimedThenIncreaseDwellingAvailableCreaturesProcessor.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,29 @@ class WhenWeekSymbolProclaimedThenIncreaseDwellingAvailableCreaturesProcessor {
3030
}
3131

3232
@EventHandler
33-
void react(WeekSymbolProclaimed event, @MetaDataValue(GameMetaData.KEY) String gameId) {
33+
void react(
34+
WeekSymbolProclaimed event,
35+
@MetaDataValue(GameMetaData.GAME_ID_KEY) String gameId,
36+
@MetaDataValue(GameMetaData.PLAYER_ID_KEY) String playerId
37+
) {
3438
var creature = event.weekOf();
3539
var increaseBy = event.growth();
3640
repository.findAllByGameId(gameId).stream()
3741
.filter(dwelling -> dwelling.getCreatureId().equals(creature))
38-
.forEach(dwelling -> increaseAvailableCreatures(dwelling, increaseBy));
42+
.forEach(dwelling -> increaseAvailableCreatures(dwelling, increaseBy, playerId));
3943
}
4044

41-
private void increaseAvailableCreatures(BuiltDwellingReadModel dwelling, Integer increaseBy) {
45+
private void increaseAvailableCreatures(BuiltDwellingReadModel dwelling, Integer increaseBy, String playerId) {
4246
var command = IncreaseAvailableCreatures.command(
4347
dwelling.getDwellingId(),
4448
dwelling.getCreatureId(),
4549
increaseBy
4650
);
47-
commandGateway.sendAndWait(command, GameMetaData.withId(dwelling.getGameId()));
51+
commandGateway.sendAndWait(command, GameMetaData.with(dwelling.getGameId(), playerId));
4852
}
4953

5054
@EventHandler
51-
void on(DwellingBuilt event, @MetaDataValue(GameMetaData.KEY) String gameId) {
55+
void on(DwellingBuilt event, @MetaDataValue(GameMetaData.GAME_ID_KEY) String gameId) {
5256
var state = new BuiltDwellingReadModel(
5357
gameId,
5458
event.dwellingId(),

src/main/java/com/dddheroes/heroesofddd/creaturerecruitment/automation/WhenCreatureRecruitedThenAddToArmyProcessor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ class WhenCreatureRecruitedThenAddToArmyProcessor {
2222
}
2323

2424
@EventHandler
25-
void react(CreatureRecruited event, @MetaDataValue(GameMetaData.KEY) String gameId) {
25+
void react(
26+
CreatureRecruited event,
27+
@MetaDataValue(GameMetaData.GAME_ID_KEY) String gameId,
28+
@MetaDataValue(GameMetaData.PLAYER_ID_KEY) String playerId
29+
) {
2630
var command = AddCreatureToArmy.command(
2731
event.toArmy(),
2832
event.creatureId(),
2933
event.quantity()
3034
);
3135

32-
commandGateway.sendAndWait(command, GameMetaData.withId(gameId));
36+
commandGateway.sendAndWait(command, GameMetaData.with(gameId, playerId));
3337
}
3438
}

src/main/java/com/dddheroes/heroesofddd/creaturerecruitment/read/DwellingReadModelProjector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class DwellingReadModelProjector {
2020
}
2121

2222
@EventHandler
23-
void on(DwellingBuilt event, @MetaDataValue(GameMetaData.KEY) String gameId) {
23+
void on(DwellingBuilt event, @MetaDataValue(GameMetaData.GAME_ID_KEY) String gameId) {
2424
var state = new DwellingReadModel(
2525
gameId,
2626
event.dwellingId(),

src/main/java/com/dddheroes/heroesofddd/creaturerecruitment/read/getalldwellings/GetAllDwellingsQueryHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ GetAllDwellings.Result handle(GetAllDwellings query) {
3838
}
3939

4040
@EventHandler
41-
void evolve(DwellingBuilt event, @MetaDataValue(GameMetaData.KEY) String gameId) {
41+
void evolve(DwellingBuilt event, @MetaDataValue(GameMetaData.GAME_ID_KEY) String gameId) {
4242
while (cache.size() > 20) {
4343
cache.pollFirst();
4444
}

src/main/java/com/dddheroes/heroesofddd/creaturerecruitment/write/builddwelling/BuildDwellingRestApi.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.dddheroes.heroesofddd.creaturerecruitment.write.builddwelling;
22

33
import com.dddheroes.heroesofddd.shared.GameMetaData;
4+
import com.dddheroes.heroesofddd.shared.restapi.Headers;
45
import org.axonframework.commandhandling.gateway.CommandGateway;
56
import org.springframework.web.bind.annotation.PathVariable;
67
import org.springframework.web.bind.annotation.PutMapping;
78
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.RequestHeader;
810
import org.springframework.web.bind.annotation.RequestMapping;
911
import org.springframework.web.bind.annotation.RestController;
1012

@@ -27,6 +29,7 @@ record Body(String creatureId, Map<String, Integer> costPerTroop) {
2729

2830
@PutMapping("/dwellings/{dwellingId}")
2931
CompletableFuture<Void> putDwellings(
32+
@RequestHeader(Headers.PLAYER_ID) String playerId,
3033
@PathVariable String gameId,
3134
@PathVariable String dwellingId,
3235
@RequestBody Body requestBody
@@ -36,6 +39,6 @@ CompletableFuture<Void> putDwellings(
3639
requestBody.creatureId(),
3740
requestBody.costPerTroop()
3841
);
39-
return commandGateway.send(command, GameMetaData.withId(gameId));
42+
return commandGateway.send(command, GameMetaData.with(gameId, playerId));
4043
}
4144
}

src/main/java/com/dddheroes/heroesofddd/creaturerecruitment/write/changeavailablecreatures/IncreaseAvailableCreaturesRestApi.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.dddheroes.heroesofddd.creaturerecruitment.write.changeavailablecreatures;
22

33
import com.dddheroes.heroesofddd.shared.GameMetaData;
4+
import com.dddheroes.heroesofddd.shared.restapi.Headers;
45
import org.axonframework.commandhandling.gateway.CommandGateway;
56
import org.springframework.web.bind.annotation.PathVariable;
67
import org.springframework.web.bind.annotation.PutMapping;
78
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.RequestHeader;
810
import org.springframework.web.bind.annotation.RequestMapping;
911
import org.springframework.web.bind.annotation.RestController;
1012

@@ -26,6 +28,7 @@ record Body(String creatureId, Integer increaseBy) {
2628

2729
@PutMapping("/dwellings/{dwellingId}/available-creatures-increases")
2830
CompletableFuture<Void> putDwellingAvailableCreaturesIncreases(
31+
@RequestHeader(Headers.PLAYER_ID) String playerId,
2932
@PathVariable String gameId,
3033
@PathVariable String dwellingId,
3134
@RequestBody Body requestBody
@@ -35,6 +38,6 @@ CompletableFuture<Void> putDwellingAvailableCreaturesIncreases(
3538
requestBody.creatureId(),
3639
requestBody.increaseBy()
3740
);
38-
return commandGateway.send(command, GameMetaData.withId(gameId));
41+
return commandGateway.send(command, GameMetaData.with(gameId, playerId));
3942
}
4043
}

src/main/java/com/dddheroes/heroesofddd/creaturerecruitment/write/recruitcreature/RecruitCreatureRestApi.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.dddheroes.heroesofddd.creaturerecruitment.write.recruitcreature;
22

33
import com.dddheroes.heroesofddd.shared.GameMetaData;
4+
import com.dddheroes.heroesofddd.shared.restapi.Headers;
45
import org.axonframework.commandhandling.gateway.CommandGateway;
56
import org.springframework.web.bind.annotation.PathVariable;
67
import org.springframework.web.bind.annotation.PutMapping;
78
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.RequestHeader;
810
import org.springframework.web.bind.annotation.RequestMapping;
911
import org.springframework.web.bind.annotation.RestController;
1012

@@ -26,6 +28,7 @@ record Body(String creatureId, String armyId, Integer quantity) {
2628

2729
@PutMapping("/dwellings/{dwellingId}/creature-recruitments")
2830
CompletableFuture<Void> putDwellingsCreatureRecruitments(
31+
@RequestHeader(Headers.PLAYER_ID) String playerId,
2932
@PathVariable String gameId,
3033
@PathVariable String dwellingId,
3134
@RequestBody Body requestBody
@@ -36,6 +39,6 @@ CompletableFuture<Void> putDwellingsCreatureRecruitments(
3639
requestBody.armyId(),
3740
requestBody.quantity()
3841
);
39-
return commandGateway.send(command, GameMetaData.withId(gameId));
42+
return commandGateway.send(command, GameMetaData.with(gameId, playerId));
4043
}
4144
}

src/main/java/com/dddheroes/heroesofddd/shared/GameMetaData.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44

55
public class GameMetaData {
66

7-
public static final String KEY = "gameId";
7+
public static final String GAME_ID_KEY = "gameId";
8+
public static final String PLAYER_ID_KEY = "playerId";
89

9-
public static MetaData withId(String gameId) {
10-
return MetaData.with(KEY, gameId);
10+
public static MetaData with(String gameId) {
11+
return with(gameId, PlayerId.unknown().raw());
1112
}
1213

13-
public static MetaData withId(GameId gameId) {
14-
return withId(gameId.raw());
14+
public static MetaData with(String gameId, String playerId) {
15+
return MetaData.with(GAME_ID_KEY, gameId)
16+
.and(PLAYER_ID_KEY, playerId);
17+
}
18+
19+
public static MetaData with(GameId gameId, PlayerId playerId) {
20+
return with(gameId.raw(), playerId.raw());
1521
}
1622
}

0 commit comments

Comments
 (0)