Skip to content

Commit

Permalink
Add BuildingCardFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
chiashengchen committed Feb 4, 2024
1 parent 92f6366 commit c5a1eac
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -83,22 +82,46 @@ public static class UserRequest {
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)
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(BuildingCard.Color color, int count) {
return IntStream.range(0, count)
.mapToObj(i -> new BuildingCard("card name", 2, color))
.collect(toList());
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;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<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);
}


Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -60,11 +63,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));
return Arrays.asList(new BuildingCard("test", 3, BuildingCardFactory.Color.BLUE), new BuildingCard("test", 2, BuildingCardFactory.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
Original file line number Diff line number Diff line change
@@ -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<BuildingCard> 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);
}
}
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 @@ -12,7 +13,8 @@
public class BuildingCardView {
private String name;
private int coins;
private BuildingCard.Color color;
private BuildingCardFactory.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
Expand Up @@ -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;
Expand All @@ -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());
Expand Down
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

0 comments on commit c5a1eac

Please sign in to comment.