Skip to content

Commit

Permalink
Refactor BuildingCardFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
chiashengchen committed Mar 19, 2024
1 parent c5a1eac commit 964c9f6
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

import javax.inject.Named;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.stream.Collectors.*;

Expand All @@ -34,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 @@ -78,50 +76,4 @@ public static class UserRequest {
public String name;
public String imageName;
}

private List<BuildingCard> getBuildingCards() {
List<BuildingCard> cards = new ArrayList<>();
Stream.of(
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);

return cards;
}

private List<BuildingCard> getBuildingCards(BuildingCardFactory.Type type, int count) {
List<BuildingCard> buildingCards = new ArrayList<>();
for (int i = 0; i < count; i++) {
buildingCards.add(factory.createBuildingCard(type).orElseThrow());
}
return buildingCards;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
package tw.waterballsa.gaas.citadels.domain.BuildingCard;

import lombok.AllArgsConstructor;
import lombok.Data;
import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCardFactory.Color;
import lombok.Value;

import java.util.Optional;

@Data
@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<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
@@ -1,81 +1,10 @@
package tw.waterballsa.gaas.citadels.domain.BuildingCard;

import java.util.List;
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<Type> fromInt(int value) {
for(Type type : Type.values()) {
if(type.ordinal() == value) {
return Optional.ofNullable(type);
}
}
return Optional.empty();
}

public static Optional<Type> 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<Color> fromString(String str) {
for (Color color : Color.values()) {
if(str.equalsIgnoreCase(color.toString())) {
return Optional.ofNullable(color);
}
}
return Optional.empty();
}
}

public Optional<BuildingCard> createBuildingCard(Type type);
public Optional<List<BuildingCard>> createBuildingCards();
}


Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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;
Expand Down Expand Up @@ -66,6 +64,6 @@ private void distributingCardsAndCoinsToEachPlayer() {
}

public List<BuildingCard> getTwoCards() {
return Arrays.asList(new BuildingCard("test", 3, BuildingCardFactory.Color.BLUE), new BuildingCard("test", 2, BuildingCardFactory.Color.BLUE));
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 @@ -3,29 +3,32 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
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.ArrayList;
import java.util.List;
import java.util.Optional;

@Named
public class BuildingCardFactoryImpl implements BuildingCardFactory {
@SneakyThrows
@Override
public Optional<BuildingCard> createBuildingCard(Type type) {
String path = "src/main/resources/CardInfo.json";
public Optional<List<BuildingCard>> createBuildingCards() {
List<BuildingCard> buildingCards = new ArrayList<>();
Resource resource = new ClassPathResource("BuildingCardInfo.json");
File file = resource.getFile();
ObjectMapper mapper = new ObjectMapper();
JsonNode nodes = mapper.readTree(new File(path));
BuildingCard buildingCard = null;
JsonNode nodes = mapper.readTree(file);
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())));
for(int i = 0; i < node.get("number").asInt(); i++) {
buildingCards.add(new BuildingCard(node.get("name").asText(), node.get("cost").asInt(), BuildingCard.Color.fromString(node.get("color").asText()).orElseThrow()));
}
}
return Optional.ofNullable(buildingCard);
return Optional.ofNullable(buildingCards);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class BuildingCardView {
private String name;
private int coins;
private BuildingCardFactory.Color color;
private BuildingCard.Color color;


public static List<BuildingCardView> toViewModels(List<BuildingCard> buildingCards) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class BuildingCardData {
@Id
private String name;
private int coins;
private BuildingCardFactory.Color color;
private BuildingCard.Color color;

public static BuildingCardData toData(BuildingCard buildingCard) {
return new BuildingCardData(buildingCard.getName(), buildingCard.getCoins(), buildingCard.getColor());
Expand Down
Loading

0 comments on commit 964c9f6

Please sign in to comment.