Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BuildingCardFactory #148

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -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);
Expand All @@ -35,7 +32,7 @@ public void execute(Request request, Presenter presenter) {
private CitadelsGame createGame(Request request) {
List<Player> players = getPlayers(request.getPlayers());
List<RoleCard> roleCards = getRoleCards();
List<BuildingCard> buildingCards = getBuildingCards();
List<BuildingCard> buildingCards = factory.createBuildingCards().orElseThrow();
CitadelsGame citadelsGame = new CitadelsGame(players, roleCards, buildingCards);
citadelsGame.start();
return citadelsGame;
Expand Down Expand Up @@ -79,26 +76,4 @@ public static class UserRequest {
public String name;
public String imageName;
}

private List<BuildingCard> getBuildingCards() {
List<BuildingCard> 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<BuildingCard> getBuildingCards(BuildingCard.Color color, int count) {
return IntStream.range(0, count)
.mapToObj(i -> new BuildingCard("card name", 2, color))
.collect(toList());
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此BuildingCard 類別可能要封裝屬性只能Get ,因為建築卡應該是不會被改變的~

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以使用 @value 對嗎

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<Color> fromString(String str) {
for (Color color : Color.values()) {
if(str.equalsIgnoreCase(color.toString())) {
return Optional.ofNullable(color);
}
}
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package tw.waterballsa.gaas.citadels.domain.BuildingCard;

import java.util.List;
import java.util.Optional;

public interface BuildingCardFactory {
public Optional<List<BuildingCard>> createBuildingCards();
}


Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -60,11 +61,9 @@ private void distributingCardsAndCoinsToEachPlayer() {
player.plusCards(getTwoCards());
player.plusCoins(2);
});

}

public List<BuildingCard> getTwoCards() {
return Arrays.asList(new BuildingCard("test", 3, BuildingCard.Color.BLUE), new BuildingCard("test", 2, BuildingCard.Color.BLUE));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCard;

import java.util.*;


Expand Down
7 changes: 7 additions & 0 deletions packages/backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<lombok.version>1.18.24</lombok.version>
<javax.inject.version>1</javax.inject.version>
<org.mockito.version>3.11.0</org.mockito.version>
<springdoc-openapi-ui.version>1.6.15</springdoc-openapi-ui.version>
</properties>

<dependencies>
Expand All @@ -35,5 +36,11 @@
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>${springdoc-openapi-ui.version}</version>
</dependency>
<!--swagger: http://localhost:8080/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config-->
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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 static org.springframework.http.ResponseEntity.status;

@RestController
@AllArgsConstructor
@RequestMapping(value = "/api/citadels")
public class GetBuildingCardsSettingController {

BuildingCardDAO buildingCardDAO;

@GetMapping(value = "/building-cards")
public ResponseEntity<?> getBuildingCardsSetting() {
return status(HttpStatus.OK)
.header("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE)
.body(buildingCardDAO.findAll());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,6 +15,7 @@ public class BuildingCardView {
private int coins;
private BuildingCard.Color color;


public static List<BuildingCardView> toViewModels(List<BuildingCard> buildingCards) {
return buildingCards.stream().map(BuildingCardView::toViewModel).collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<BuildingCard>> createBuildingCards() {
return Optional.of(BuildingCardSetting.toDomains(buildingCardDAO.findAll()));
}
}
Original file line number Diff line number Diff line change
@@ -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<BuildingCardSetting, String> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BuildingCard> toDomain(BuildingCardSetting buildingCardSetting) {
int quantity = buildingCardSetting.getQuantity();
List<BuildingCard> 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<BuildingCard> toDomains(List<BuildingCardSetting> buildingCardSettings) {
return buildingCardSettings.stream().
flatMap(setting -> toDomain(setting).stream())
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading
Loading