diff --git a/packages/backend/app/src/main/java/tw/waterballsa/gaas/citadels/app/usecases/StartGameUseCase.java b/packages/backend/app/src/main/java/tw/waterballsa/gaas/citadels/app/usecases/StartGameUseCase.java index 5038a409..9482339e 100644 --- a/packages/backend/app/src/main/java/tw/waterballsa/gaas/citadels/app/usecases/StartGameUseCase.java +++ b/packages/backend/app/src/main/java/tw/waterballsa/gaas/citadels/app/usecases/StartGameUseCase.java @@ -5,16 +5,14 @@ import lombok.Data; import lombok.RequiredArgsConstructor; import tw.waterballsa.gaas.citadels.app.repositories.GameRepository; -import tw.waterballsa.gaas.citadels.domain.BuildingCard; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCard; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCardFactory; import tw.waterballsa.gaas.citadels.domain.RoleCard; import tw.waterballsa.gaas.citadels.domain.CitadelsGame; import tw.waterballsa.gaas.citadels.domain.Player; import javax.inject.Named; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; +import java.util.*; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -26,6 +24,7 @@ public class StartGameUseCase { private final GameRepository gameRepository; + private final BuildingCardFactory factory; public void execute(Request request, Presenter presenter) { CitadelsGame citadelsGame = createGame(request); @@ -83,11 +82,34 @@ public static class UserRequest { private List getBuildingCards() { List cards = new ArrayList<>(); Stream.of( - getBuildingCards(BuildingCard.Color.YELLOW, 12), - getBuildingCards(BuildingCard.Color.BLUE, 11), - getBuildingCards(BuildingCard.Color.GREEN, 11), - getBuildingCards(BuildingCard.Color.RED, 11), - getBuildingCards(BuildingCard.Color.PURPLE, 30) + getBuildingCards(BuildingCardFactory.Type.TEMPLE, 3), + getBuildingCards(BuildingCardFactory.Type.CHURCH, 3), + getBuildingCards(BuildingCardFactory.Type.MONASTERY, 3), + getBuildingCards(BuildingCardFactory.Type.CATHEDRAL, 2), + getBuildingCards(BuildingCardFactory.Type.WATCHTOWER, 3), + getBuildingCards(BuildingCardFactory.Type.PRISON, 3), + getBuildingCards(BuildingCardFactory.Type.BATTLEFIELD, 3), + getBuildingCards(BuildingCardFactory.Type.FORTRESS, 2), + getBuildingCards(BuildingCardFactory.Type.MANOR, 5), + getBuildingCards(BuildingCardFactory.Type.CASTLE, 4), + getBuildingCards(BuildingCardFactory.Type.PALACE, 3), + getBuildingCards(BuildingCardFactory.Type.TAVERN, 5), + getBuildingCards(BuildingCardFactory.Type.INN, 3), + getBuildingCards(BuildingCardFactory.Type.MARKET, 4), + getBuildingCards(BuildingCardFactory.Type.BOATHOUSE, 3), + getBuildingCards(BuildingCardFactory.Type.HARBOR, 3), + getBuildingCards(BuildingCardFactory.Type.TOWN_HALL, 2), + getBuildingCards(BuildingCardFactory.Type.LIBRARY, 1), + getBuildingCards(BuildingCardFactory.Type.DRAGON_GATE, 1), + getBuildingCards(BuildingCardFactory.Type.GRAVEYARD, 1), + getBuildingCards(BuildingCardFactory.Type.GHOST_TOWN, 1), + getBuildingCards(BuildingCardFactory.Type.SCHOOL_OF_MAGIC, 1), + getBuildingCards(BuildingCardFactory.Type.LABORATORY, 1), + getBuildingCards(BuildingCardFactory.Type.SMITHY, 1), + getBuildingCards(BuildingCardFactory.Type.OBSERVATORY, 1), + getBuildingCards(BuildingCardFactory.Type.UNIVERSITY, 1), + getBuildingCards(BuildingCardFactory.Type.BASTION, 1), + getBuildingCards(BuildingCardFactory.Type.GREAT_WALL, 1) ) .flatMap(Collection::stream) .forEach(cards::add); @@ -95,10 +117,11 @@ private List getBuildingCards() { return cards; } - private List getBuildingCards(BuildingCard.Color color, int count) { - return IntStream.range(0, count) - .mapToObj(i -> new BuildingCard("card name", 2, color)) - .collect(toList()); + private List getBuildingCards(BuildingCardFactory.Type type, int count) { + List buildingCards = new ArrayList<>(); + for (int i = 0; i < count; i++) { + buildingCards.add(factory.createBuildingCard(type).orElseThrow()); + } + return buildingCards; } - } diff --git a/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/BuildingCard.java b/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/BuildingCard.java deleted file mode 100644 index 3507aa0e..00000000 --- a/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/BuildingCard.java +++ /dev/null @@ -1,27 +0,0 @@ -package tw.waterballsa.gaas.citadels.domain; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -@Getter -@AllArgsConstructor -public class BuildingCard { - private String name; - private int coins; - private Color color; - - public enum Color { - PURPLE("特別地區"), - YELLOW("貴族地區"), - RED("軍事地區"), - BLUE("商業地區"), - GREEN("宗教地區"); - private final String area; - - Color(String area) { - this.area = area; - } - } - -} - diff --git a/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/BuildingCard/BuildingCard.java b/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/BuildingCard/BuildingCard.java new file mode 100644 index 00000000..b9a91a10 --- /dev/null +++ b/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/BuildingCard/BuildingCard.java @@ -0,0 +1,14 @@ +package tw.waterballsa.gaas.citadels.domain.BuildingCard; + +import lombok.AllArgsConstructor; +import lombok.Data; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCardFactory.Color; + + +@Data +@AllArgsConstructor +public class BuildingCard { + String name; + int coins; + Color color; +} diff --git a/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/BuildingCard/BuildingCardFactory.java b/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/BuildingCard/BuildingCardFactory.java new file mode 100644 index 00000000..38a66de4 --- /dev/null +++ b/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/BuildingCard/BuildingCardFactory.java @@ -0,0 +1,81 @@ +package tw.waterballsa.gaas.citadels.domain.BuildingCard; + +import java.util.Optional; + +public interface BuildingCardFactory { + + public enum Type { + TEMPLE, + CHURCH, + MONASTERY, + CATHEDRAL, + WATCHTOWER, + PRISON, + BATTLEFIELD, + FORTRESS, + MANOR, + CASTLE, + PALACE, + TAVERN, + INN, + MARKET, + BOATHOUSE, + HARBOR, + TOWN_HALL, + LIBRARY, + DRAGON_GATE, + GRAVEYARD, + GHOST_TOWN, + SCHOOL_OF_MAGIC, + LABORATORY, + SMITHY, + OBSERVATORY, + UNIVERSITY, + BASTION, + GREAT_WALL; + + public static Optional fromInt(int value) { + for(Type type : Type.values()) { + if(type.ordinal() == value) { + return Optional.ofNullable(type); + } + } + return Optional.empty(); + } + + public static Optional fromString(String str) { + for (Type type : Type.values()) { + if(str.equals(type.toString())) { + return Optional.ofNullable(type); + } + } + return Optional.empty(); + } + } + + public enum Color { + PURPLE("特別地區"), + GOLD("貴族地區"), + RED("軍事地區"), + BLUE("商業地區"), + GREEN("宗教地區"); + private final String area; + + Color(String area) { + this.area = area; + } + + public static Optional fromString(String str) { + for (Color color : Color.values()) { + if(str.equalsIgnoreCase(color.toString())) { + return Optional.ofNullable(color); + } + } + return Optional.empty(); + } + } + + public Optional createBuildingCard(Type type); +} + + diff --git a/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/CitadelsGame.java b/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/CitadelsGame.java index 612290de..b8132745 100644 --- a/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/CitadelsGame.java +++ b/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/CitadelsGame.java @@ -1,5 +1,8 @@ package tw.waterballsa.gaas.citadels.domain; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCard; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCardFactory; + import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -60,11 +63,9 @@ private void distributingCardsAndCoinsToEachPlayer() { player.plusCards(getTwoCards()); player.plusCoins(2); }); - } public List getTwoCards() { - return Arrays.asList(new BuildingCard("test", 3, BuildingCard.Color.BLUE), new BuildingCard("test", 2, BuildingCard.Color.BLUE)); + return Arrays.asList(new BuildingCard("test", 3, BuildingCardFactory.Color.BLUE), new BuildingCard("test", 2, BuildingCardFactory.Color.BLUE)); } - } diff --git a/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/Player.java b/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/Player.java index 0ba28e9c..96a24b6e 100644 --- a/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/Player.java +++ b/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/Player.java @@ -2,6 +2,8 @@ import lombok.AllArgsConstructor; import lombok.Getter; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCard; + import java.util.*; diff --git a/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/BuildingCardFactoryImpl.java b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/BuildingCardFactoryImpl.java new file mode 100644 index 00000000..10c10ebf --- /dev/null +++ b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/BuildingCardFactoryImpl.java @@ -0,0 +1,31 @@ +package tw.waterballsa.gaas.citadels.spring; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.SneakyThrows; +import org.springframework.stereotype.Component; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCard; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCardFactory; +import tw.waterballsa.gaas.citadels.exceptions.NotFoundException; + +import javax.inject.Named; +import java.io.File; +import java.util.Optional; + +@Named +public class BuildingCardFactoryImpl implements BuildingCardFactory { + @SneakyThrows + @Override + public Optional createBuildingCard(Type type) { + String path = "src/main/resources/CardInfo.json"; + ObjectMapper mapper = new ObjectMapper(); + JsonNode nodes = mapper.readTree(new File(path)); + BuildingCard buildingCard = null; + for(JsonNode node : nodes) { + if(type == BuildingCardFactory.Type.fromString(node.get("name").asText()).orElseThrow()) { + buildingCard = new BuildingCard(node.get("name").asText(), node.get("cost").asInt(), BuildingCardFactory.Color.fromString(node.get("color").asText()).orElseThrow(() -> new NotFoundException("NO SUCH COLOR, COLOR=" + node.get("color").asText()))); + } + } + return Optional.ofNullable(buildingCard); + } +} diff --git a/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/controllers/viewmodel/BuildingCardView.java b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/controllers/viewmodel/BuildingCardView.java index 92368e8f..195eacbb 100644 --- a/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/controllers/viewmodel/BuildingCardView.java +++ b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/controllers/viewmodel/BuildingCardView.java @@ -2,7 +2,8 @@ import lombok.AllArgsConstructor; import lombok.Data; -import tw.waterballsa.gaas.citadels.domain.BuildingCard; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCard; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCardFactory; import java.util.List; import java.util.stream.Collectors; @@ -12,7 +13,8 @@ public class BuildingCardView { private String name; private int coins; - private BuildingCard.Color color; + private BuildingCardFactory.Color color; + public static List toViewModels(List buildingCards) { return buildingCards.stream().map(BuildingCardView::toViewModel).collect(Collectors.toList()); diff --git a/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/data/BuildingCardData.java b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/data/BuildingCardData.java index 2154f47b..3ed8ec15 100644 --- a/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/data/BuildingCardData.java +++ b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/data/BuildingCardData.java @@ -4,7 +4,8 @@ import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; -import tw.waterballsa.gaas.citadels.domain.BuildingCard; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCard; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCardFactory; import java.util.List; import java.util.stream.Collectors; @@ -16,7 +17,7 @@ public class BuildingCardData { @Id private String name; private int coins; - private BuildingCard.Color color; + private BuildingCardFactory.Color color; public static BuildingCardData toData(BuildingCard buildingCard) { return new BuildingCardData(buildingCard.getName(), buildingCard.getCoins(), buildingCard.getColor()); diff --git a/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/data/PlayerData.java b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/data/PlayerData.java index be44616d..d685b616 100644 --- a/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/data/PlayerData.java +++ b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/data/PlayerData.java @@ -4,7 +4,7 @@ import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; -import tw.waterballsa.gaas.citadels.domain.BuildingCard; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCard; import tw.waterballsa.gaas.citadels.domain.RoleCard; import tw.waterballsa.gaas.citadels.domain.Player; diff --git a/packages/backend/spring/src/main/resources/CardInfo.json b/packages/backend/spring/src/main/resources/CardInfo.json new file mode 100644 index 00000000..3605337d --- /dev/null +++ b/packages/backend/spring/src/main/resources/CardInfo.json @@ -0,0 +1,170 @@ +[ + { + "index": 0, + "name": "TEMPLE", + "color": "blue", + "cost": 1 + }, + { + "index": 1, + "name": "CHURCH", + "color": "blue", + "cost": 2 + }, + { + "index": 2, + "name": "MONASTERY", + "color": "blue", + "cost": 3 + }, + { + "index": 3, + "name": "CATHEDRAL", + "color": "blue", + "cost": 5 + }, + { + "index": 4, + "name": "WATCHTOWER", + "color": "red", + "cost": 1 + }, + { + "index": 5, + "name": "PRISON", + "color": "red", + "cost": 2 + }, + { + "index": 6, + "name": "BATTLEFIELD", + "color": "red", + "cost": 3 + }, + { + "index": 7, + "name": "FORTRESS", + "color": "red", + "cost": 5 + }, + { + "index": 8, + "name": "MANOR", + "color": "gold", + "cost": 3 + }, + { + "index": 9, + "name": "CASTLE", + "color": "gold", + "cost": 4 + }, + { + "index": 10, + "name": "PALACE", + "color": "gold", + "cost": 5 + }, + { + "index": 11, + "name": "TAVERN", + "color": "green", + "cost": 1 + }, + { + "index": 12, + "name": "INN", + "color": "green", + "cost": 2 + }, + { + "index": 13, + "name": "MARKET", + "color": "green", + "cost": 2 + }, + { + "index": 14, + "name": "BOATHOUSE", + "color": "green", + "cost": 3 + }, + { + "index": 15, + "name": "HARBOR", + "color": "green", + "cost": 4 + }, + { + "index": 16, + "name": "TOWN_HALL", + "color": "green", + "cost": 5 + }, + { + "index": 17, + "name": "LIBRARY", + "color": "purple", + "cost": 6 + }, + { + "index": 18, + "name": "DRAGON_GATE", + "color": "purple", + "cost": 8 + }, + { + "index": 19, + "name": "GRAVEYARD", + "color": "purple", + "cost": 5 + }, + { + "index": 20, + "name": "GHOST_TOWN", + "color": "purple", + "cost": 2 + }, + { + "index": 21, + "name": "SCHOOL_OF_MAGIC", + "color": "purple", + "cost": 6 + }, + { + "index": 22, + "name": "LABORATORY", + "color": "purple", + "cost": 5 + }, + { + "index": 23, + "name": "SMITHY", + "color": "purple", + "cost": 5 + }, + { + "index": 24, + "name": "OBSERVATORY", + "color": "purple", + "cost": 5 + }, + { + "index": 25, + "name": "UNIVERSITY", + "color": "purple", + "cost": 8 + }, + { + "index": 26, + "name": "BASTION", + "color": "purple", + "cost": 3 + }, + { + "index": 27, + "name": "GREAT_WALL", + "color": "purple", + "cost": 6 + } +] \ No newline at end of file