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..2bbd48d3 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,18 +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.stream.IntStream; -import java.util.stream.Stream; +import java.util.*; import static java.util.stream.Collectors.*; @@ -26,6 +22,7 @@ public class StartGameUseCase { private final GameRepository gameRepository; + private final BuildingCardFactory factory; public void execute(Request request, Presenter presenter) { CitadelsGame citadelsGame = createGame(request); @@ -35,7 +32,7 @@ public void execute(Request request, Presenter presenter) { private CitadelsGame createGame(Request request) { List players = getPlayers(request.getPlayers()); List roleCards = getRoleCards(); - List buildingCards = getBuildingCards(); + List buildingCards = factory.createBuildingCards().orElseThrow(); CitadelsGame citadelsGame = new CitadelsGame(players, roleCards, buildingCards); citadelsGame.start(); return citadelsGame; @@ -79,26 +76,4 @@ public static class UserRequest { public String name; public String imageName; } - - 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) - ) - .flatMap(Collection::stream) - .forEach(cards::add); - - 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()); - } - } 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..70e2a2b4 --- /dev/null +++ b/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/BuildingCard/BuildingCard.java @@ -0,0 +1,36 @@ +package tw.waterballsa.gaas.citadels.domain.BuildingCard; + +import lombok.AllArgsConstructor; +import lombok.Value; + +import java.util.Optional; + +@Value +@AllArgsConstructor +public class BuildingCard { + String name; + int coins; + Color color; + + 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(); + } + } +} 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..c77013b4 --- /dev/null +++ b/packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/BuildingCard/BuildingCardFactory.java @@ -0,0 +1,10 @@ +package tw.waterballsa.gaas.citadels.domain.BuildingCard; + +import java.util.List; +import java.util.Optional; + +public interface BuildingCardFactory { + public Optional> createBuildingCards(); +} + + 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..8f301879 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,6 @@ package tw.waterballsa.gaas.citadels.domain; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCard; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -60,11 +61,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)); } - } 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/pom.xml b/packages/backend/pom.xml index f79d6282..9c35a713 100644 --- a/packages/backend/pom.xml +++ b/packages/backend/pom.xml @@ -22,6 +22,7 @@ 1.18.24 1 3.11.0 + 1.6.15 @@ -35,5 +36,11 @@ lombok ${lombok.version} + + org.springdoc + springdoc-openapi-ui + ${springdoc-openapi-ui.version} + + \ No newline at end of file diff --git a/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/InitBuildingCards.java b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/InitBuildingCards.java new file mode 100644 index 00000000..49d9fd42 --- /dev/null +++ b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/InitBuildingCards.java @@ -0,0 +1,38 @@ +package tw.waterballsa.gaas.citadels.spring; + +import org.bson.BsonArray; +import org.bson.BsonValue; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.core.io.ClassPathResource; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.stereotype.Component; + +import org.bson.Document; +import java.nio.file.Files; +import java.nio.file.Paths; + +@Component +public class InitBuildingCards implements CommandLineRunner { + + private final MongoTemplate mongoTemplate; + + @Autowired + public InitBuildingCards(MongoTemplate mongoTemplate) { + this.mongoTemplate = mongoTemplate; + } + + @Override + public void run(String... args) throws Exception { + if(mongoTemplate.collectionExists("buildingCards")) { + return; + } + ClassPathResource resource = new ClassPathResource("BuildingCardInfo.json"); + String jsonContent = new String(Files.readAllBytes(Paths.get(resource.getURI()))); + BsonArray bsonArray = BsonArray.parse(jsonContent); + for(BsonValue bsonValue : bsonArray) { + Document doc = Document.parse(bsonValue.asDocument().toJson()); + mongoTemplate.save(doc, "buildingCards"); + } + } +} diff --git a/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/controllers/GetBuildingCardsSettingController.java b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/controllers/GetBuildingCardsSettingController.java new file mode 100644 index 00000000..567143b3 --- /dev/null +++ b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/controllers/GetBuildingCardsSettingController.java @@ -0,0 +1,30 @@ +package tw.waterballsa.gaas.citadels.spring.controllers; + +import lombok.AllArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import tw.waterballsa.gaas.citadels.spring.repositories.dao.BuildingCardDAO; +import tw.waterballsa.gaas.citadels.spring.repositories.data.BuildingCardSetting; + +import java.util.List; + +import static org.springframework.http.ResponseEntity.status; + +@RestController +@AllArgsConstructor +@RequestMapping(value = "/api/citadels") +public class GetBuildingCardsSettingController { + + private final BuildingCardDAO buildingCardDAO; + + @GetMapping(value = "/building-cards") + public ResponseEntity> getBuildingCardsSetting() { + return status(HttpStatus.OK) + .header("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE) + .body(buildingCardDAO.findAll()); + } +} diff --git a/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/controllers/GetSpecificRoomController.java b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/controllers/GetSpecificRoomController.java index 01aa5bad..8d427564 100644 --- a/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/controllers/GetSpecificRoomController.java +++ b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/controllers/GetSpecificRoomController.java @@ -21,7 +21,7 @@ public class GetSpecificRoomController { private final GetSpecificRoomUseCase getSpecificRoomUseCase; @GetMapping("/rooms/{roomId}") - public ResponseEntity getSpecificRoom(@PathVariable String roomId) { + public ResponseEntity getSpecificRoom(@PathVariable String roomId) { GetSpecificRoomPresenter presenter = new GetSpecificRoomPresenter(); getSpecificRoomUseCase.execute(roomId, presenter); return status(HttpStatus.OK).body(presenter.getSpecificRoomView()); diff --git a/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/controllers/JoinRoomController.java b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/controllers/JoinRoomController.java index e6daa243..81229625 100644 --- a/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/controllers/JoinRoomController.java +++ b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/controllers/JoinRoomController.java @@ -23,7 +23,7 @@ public class JoinRoomController { private final JoinRoomUseCase joinRoomUsecase; @PostMapping("/rooms/{roomId}:join") - public ResponseEntity joinRoom(@PathVariable String roomId, + public ResponseEntity joinRoom(@PathVariable String roomId, @Valid @RequestBody JoinRoomRequest request) { JoinRoomPresenter present = new JoinRoomPresenter(); joinRoomUsecase.execute(request.toRequest(roomId), present); 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..1ba0eb1f 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; @@ -14,6 +15,7 @@ public class BuildingCardView { private int coins; private BuildingCard.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/BuildingCardFactoryImpl.java b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/BuildingCardFactoryImpl.java new file mode 100644 index 00000000..bef58dee --- /dev/null +++ b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/BuildingCardFactoryImpl.java @@ -0,0 +1,24 @@ +package tw.waterballsa.gaas.citadels.spring.repositories; + +import lombok.RequiredArgsConstructor; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCard; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCardFactory; +import tw.waterballsa.gaas.citadels.spring.repositories.dao.BuildingCardDAO; +import tw.waterballsa.gaas.citadels.spring.repositories.data.BuildingCardData; +import tw.waterballsa.gaas.citadels.spring.repositories.data.BuildingCardSetting; + +import javax.inject.Named; +import java.util.List; +import java.util.Optional; + +@Named +@RequiredArgsConstructor +public class BuildingCardFactoryImpl implements BuildingCardFactory { + + private final BuildingCardDAO buildingCardDAO; + + @Override + public Optional> createBuildingCards() { + return Optional.of(BuildingCardSetting.toDomains(buildingCardDAO.findAll())); + } +} diff --git a/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/dao/BuildingCardDAO.java b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/dao/BuildingCardDAO.java new file mode 100644 index 00000000..36b08369 --- /dev/null +++ b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/dao/BuildingCardDAO.java @@ -0,0 +1,9 @@ +package tw.waterballsa.gaas.citadels.spring.repositories.dao; + +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; +import tw.waterballsa.gaas.citadels.spring.repositories.data.BuildingCardSetting; + +@Repository +public interface BuildingCardDAO extends MongoRepository { +} 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..6a798084 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 @@ -2,18 +2,14 @@ import lombok.AllArgsConstructor; 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 java.util.List; import java.util.stream.Collectors; @Data -@Document @AllArgsConstructor public class BuildingCardData { - @Id private String name; private int coins; private BuildingCard.Color color; diff --git a/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/data/BuildingCardSetting.java b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/data/BuildingCardSetting.java new file mode 100644 index 00000000..f875aa19 --- /dev/null +++ b/packages/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/data/BuildingCardSetting.java @@ -0,0 +1,40 @@ +package tw.waterballsa.gaas.citadels.spring.repositories.data; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.data.mongodb.core.mapping.Document; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCard; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +@Data +@Document("buildingCards") +@AllArgsConstructor +@NoArgsConstructor +public class BuildingCardSetting { + private String name; + private int coins; + private BuildingCard.Color color; + private int quantity; + private String description; + private String image; + + public static List toDomain(BuildingCardSetting buildingCardSetting) { + int quantity = buildingCardSetting.getQuantity(); + List buildingCards = new ArrayList<>(); + for (int i = 0; i < quantity; i++) { + BuildingCard buildingCard = new BuildingCard(buildingCardSetting.getName(), buildingCardSetting.getCoins(), buildingCardSetting.getColor()); + buildingCards.add(buildingCard); + } + return buildingCards; + } + + public static List toDomains(List buildingCardSettings) { + return buildingCardSettings.stream(). + flatMap(setting -> toDomain(setting).stream()) + .collect(Collectors.toList()); + } +} 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/BuildingCardInfo.json b/packages/backend/spring/src/main/resources/BuildingCardInfo.json new file mode 100644 index 00000000..859c8a6a --- /dev/null +++ b/packages/backend/spring/src/main/resources/BuildingCardInfo.json @@ -0,0 +1,226 @@ +[ + { + "name": "TEMPLE", + "color": "BLUE", + "coins": 1, + "quantity" : 3, + "description" : "", + "image" : "temple.png" + }, + { + "name": "CHURCH", + "color": "BLUE", + "coins": 2, + "quantity" : 3, + "description" : "", + "image" : "church.png" + }, + { + "name": "MONASTERY", + "color": "BLUE", + "coins": 3, + "quantity" : 3, + "description" : "", + "image" : "monastery.png" + }, + { + "name": "CATHEDRAL", + "color": "BLUE", + "coins": 5, + "quantity" : 2, + "description" : "", + "image" : "cathedral.png" + }, + { + "name": "WATCHTOWER", + "color": "RED", + "coins": 1, + "quantity" : 3, + "description" : "", + "image" : "watchtower.png" + }, + { + "name": "PRISON", + "color": "RED", + "coins": 2, + "quantity" : 3, + "description" : "", + "image" : "prison.png" + }, + { + "name": "BATTLEFIELD", + "color": "RED", + "coins": 3, + "quantity" : 3, + "description" : "", + "image" : "battlefield.png" + }, + { + "name": "FORTRESS", + "color": "RED", + "coins": 5, + "quantity" : 2, + "description" : "", + "image" : "fortress.png" + }, + { + "name": "MANOR", + "color": "GOLD", + "coins": 3, + "quantity" : 5, + "description" : "", + "image" : "manor.png" + }, + { + "name": "CASTLE", + "color": "GOLD", + "coins": 4, + "quantity" : 4, + "description" : "", + "image" : "castle.png" + }, + { + "name": "PALACE", + "color": "GOLD", + "coins": 5, + "quantity" : 3, + "description" : "", + "image" : "palace.png" + }, + { + "name": "TAVERN", + "color": "GREEN", + "coins": 1, + "quantity" : 5, + "description" : "", + "image" : "tavern.png" + }, + { + "name": "INN", + "color": "GREEN", + "coins": 2, + "quantity" : 3, + "description" : "", + "image" : "inn.png" + }, + { + "name": "MARKET", + "color": "GREEN", + "coins": 2, + "quantity" : 4, + "description" : "", + "image" : "market.png" + }, + { + "name": "BOATHOUSE", + "color": "GREEN", + "coins": 3, + "quantity" : 3, + "description" : "", + "image" : "boathouse.png" + }, + { + "name": "HARBOR", + "color": "GREEN", + "coins": 4, + "quantity" : 3, + "description" : "", + "image" : "harbor.png" + }, + { + "name": "TOWN_HALL", + "color": "GREEN", + "coins": 5, + "quantity" : 2, + "description" : "", + "image" : "town_hall.png" + }, + { + "name": "LIBRARY", + "color": "PURPLE", + "coins": 6, + "quantity" : 1, + "description" : "如果你選擇在回合開始時抽牌,你可保留抽到的2張地區卡", + "image" : "library.png" + }, + { + "name": "DRAGON_GATE", + "color": "PURPLE", + "coins": 6, + "quantity" : 1, + "description" : "這是一個名譽的象徵-國家已超過一千年沒見過龍,雖然建築費用為6元,但在遊戲終結時可以8分計算", + "image" : "dragon_gate.png" + }, + { + "name": "GRAVEYARD", + "color": "PURPLE", + "coins": 5, + "quantity" : 1, + "description" : "當軍閥破壞一個地區時,你可以支付1枚金幣將被破壞的地區拿到手上。但如果本身是軍閥,則無法使用此項特殊能力", + "image" : "graveyard.png" + }, + { + "name": "GHOST_TOWN", + "color": "PURPLE", + "coins": 2, + "quantity" : 1, + "description" : "當計算分數時,鬼城可被視為任何顏色的建築物。不過,若在遊戲最後一回合才建設鬼城,則無法使用此項特殊能力", + "image" : "ghost_town.png" + }, + { + "name": "SCHOOL_OF_MAGIC", + "color": "PURPLE", + "coins": 6, + "quantity" : 1, + "description" : "當計算金幣收入時,魔法學院可視為任何顏色的建築物。如果你是國王、主教、商人或軍閥時,金幣收入就會加一", + "image" : "school_of_magic.png" + }, + { + "name": "LABORATORY", + "color": "PURPLE", + "coins": 5, + "quantity" : 1, + "description" : "在你的回合當中,你可以從手上棄掉一張地區卡,然後換取一枚金幣。這項特殊能力每回合只能使用一次", + "image" : "laboratory.png" + }, + { + "name": "SMITHY", + "color": "PURPLE", + "coins": 5, + "quantity" : 1, + "description" : "在你的回合當中,你可以支付3枚金幣,然後抽2張地區卡。這項特殊能力每回合只能使用一次", + "image" : "smithy.png" + }, + { + "name": "OBSERVATORY", + "color": "PURPLE", + "coins": 5, + "quantity" : 1, + "description" : "如果你選擇在回合開始時抽牌,你可保留抽到的3張地區卡,保留1張,然後將其餘2張牌放回置牌庫底下", + "image" : "observatory.png" + }, + { + "name": "UNIVERSITY", + "color": "PURPLE", + "coins": 6, + "quantity" : 1, + "description" : "這是一個名譽的象徵-從來沒有人知道它的作用,雖然建築費用為6元,但在遊戲終結時可以8分計算", + "image" : "university.png" + }, + { + "name": "BASTION", + "color": "PURPLE", + "coins": 3, + "quantity" : 1, + "description" : "堡壘無法被軍閥破壞", + "image" : "bastion.png" + }, + { + "name": "GREAT_WALL", + "color": "PURPLE", + "coins": 6, + "quantity" : 1, + "description" : "軍閥要破壞你的其它地區須多付一枚金幣", + "image" : "great_wall.png" + } +] \ No newline at end of file diff --git a/packages/backend/spring/src/test/java/tw/waterballsa/gaas/citadels/spring/controllers/GetBuildingCardTest.java b/packages/backend/spring/src/test/java/tw/waterballsa/gaas/citadels/spring/controllers/GetBuildingCardTest.java new file mode 100644 index 00000000..916025fd --- /dev/null +++ b/packages/backend/spring/src/test/java/tw/waterballsa/gaas/citadels/spring/controllers/GetBuildingCardTest.java @@ -0,0 +1,34 @@ +package tw.waterballsa.gaas.citadels.spring.controllers; + +import com.fasterxml.jackson.core.type.TypeReference; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.web.servlet.MvcResult; +import tw.waterballsa.gaas.citadels.spring.CitadelsSpringBootTest; +import tw.waterballsa.gaas.citadels.spring.repositories.dao.BuildingCardDAO; +import tw.waterballsa.gaas.citadels.spring.repositories.data.BuildingCardSetting; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +public class GetBuildingCardTest extends CitadelsSpringBootTest { + + @Autowired + private BuildingCardDAO buildingCardDAO; + + @Test + public void getBuildingCardTest() throws Exception { + MvcResult mvcResult = mockMvc.perform(get(API_PREFIX + "/building-cards") + .contentType(APPLICATION_JSON)) + .andExpect(status().isOk()) + .andReturn(); + String content = mvcResult.getResponse().getContentAsString(); + List buildingCardSettings = objectMapper.readValue(content, new TypeReference>() {}); + List expectedBuildingCardSetting = buildingCardDAO.findAll(); + assert buildingCardSettings.equals(expectedBuildingCardSetting); + } +} diff --git a/packages/backend/spring/src/test/java/tw/waterballsa/gaas/citadels/spring/repositories/BuildingCardFactoryTest.java b/packages/backend/spring/src/test/java/tw/waterballsa/gaas/citadels/spring/repositories/BuildingCardFactoryTest.java new file mode 100644 index 00000000..21914b94 --- /dev/null +++ b/packages/backend/spring/src/test/java/tw/waterballsa/gaas/citadels/spring/repositories/BuildingCardFactoryTest.java @@ -0,0 +1,58 @@ +package tw.waterballsa.gaas.citadels.spring.repositories; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCard; +import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCardFactory; +import tw.waterballsa.gaas.citadels.spring.CitadelsSpringBootTest; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.List; +import java.util.Optional; + +public class BuildingCardFactoryTest extends CitadelsSpringBootTest { + @Autowired + private BuildingCardFactory buildingCardFactory; + + @Test + public void CreateBuildingCardTest() throws Exception { + Optional> buildingCards = buildingCardFactory.createBuildingCards(); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "TEMPLE", 3)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "CHURCH", 3)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "MONASTERY", 3)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "CATHEDRAL", 2)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "WATCHTOWER", 3)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "PRISON", 3)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "BATTLEFIELD", 3)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "FORTRESS", 2)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "MANOR", 5)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "CASTLE", 4)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "PALACE", 3)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "TAVERN", 5)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "INN", 3)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "MARKET", 4)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "BOATHOUSE", 3)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "HARBOR", 3)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "TOWN_HALL", 2)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "LIBRARY", 1)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "DRAGON_GATE", 1)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "GRAVEYARD", 1)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "GHOST_TOWN", 1)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "SCHOOL_OF_MAGIC", 1)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "LABORATORY", 1)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "SMITHY", 1)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "OBSERVATORY", 1)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "UNIVERSITY", 1)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "BASTION", 1)); + assertTrue(isBuildingCardNumber(buildingCards.orElseThrow(), "GREAT_WALL", 1)); + } + + private boolean isBuildingCardNumber(List buildingCards, String name, int total) { + int number = 0; + for(BuildingCard buildingCard : buildingCards) { + if(name.equals(buildingCard.getName())) number++; + } + return number == total; + } +}