Skip to content

Commit

Permalink
feat: start and get game API #115 (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
timt1028 authored Jan 25, 2024
1 parent 049646a commit 92f6366
Show file tree
Hide file tree
Showing 26 changed files with 842 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tw.waterballsa.gaas.citadels.app.repositories;

import tw.waterballsa.gaas.citadels.domain.CitadelsGame;

import java.util.Optional;

public interface GameRepository {

CitadelsGame createGame(CitadelsGame citadelsGame);

Optional<CitadelsGame> findGameById(String gameId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package tw.waterballsa.gaas.citadels.app.usecases;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import tw.waterballsa.gaas.citadels.app.repositories.GameRepository;
import tw.waterballsa.gaas.citadels.domain.CitadelsGame;
import tw.waterballsa.gaas.citadels.exceptions.NotFoundException;

import javax.inject.Named;
import java.util.Optional;

@Named
@RequiredArgsConstructor
public class GetGameUseCase {

private final GameRepository gameRepository;

public void execute(String gameId, Presenter presenter) {
CitadelsGame citadelsGame = findGameById(gameId);
presenter.setGame(citadelsGame);
}

private CitadelsGame findGameById(String gameId) {
Optional<CitadelsGame> game = gameRepository.findGameById(gameId);
return game.orElseThrow(() -> new NotFoundException("CAN NOT FIND GAME, ID=" + gameId));
}

@Data
@AllArgsConstructor
public static class Request {
private String gameId;
}

public interface Presenter {
void setGame(CitadelsGame citadelsGame);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package tw.waterballsa.gaas.citadels.app.usecases;

import lombok.AllArgsConstructor;
import lombok.Builder;
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.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 static java.util.stream.Collectors.*;


@Named
@RequiredArgsConstructor
public class StartGameUseCase {

private final GameRepository gameRepository;

public void execute(Request request, Presenter presenter) {
CitadelsGame citadelsGame = createGame(request);
presenter.setGame(gameRepository.createGame(citadelsGame));
}

private CitadelsGame createGame(Request request) {
List<Player> players = getPlayers(request.getPlayers());
List<RoleCard> roleCards = getRoleCards();
List<BuildingCard> buildingCards = getBuildingCards();
CitadelsGame citadelsGame = new CitadelsGame(players, roleCards, buildingCards);
citadelsGame.start();
return citadelsGame;
}

private List<RoleCard> getRoleCards() {
return Arrays.asList(new RoleCard(1, "刺客"),
new RoleCard(2, "小偷"),
new RoleCard(3, "魔術師"),
new RoleCard(4, "國王"),
new RoleCard(5, "住持"),
new RoleCard(6, "商人"),
new RoleCard(7, "建築師"),
new RoleCard(8, "領主"));
}

private List<Player> getPlayers(List<UserRequest> playerIds) {
return playerIds.stream()
.map(user -> new Player(user.getId(), user.getName(), user.getImageName()))
.collect(toList());
}

public interface Presenter {
void setGame(CitadelsGame citadelsGame);
}

@Data
@Builder
@AllArgsConstructor
public static class Request {
public String roomId;
public String roomName;
public String holderId;
public List<UserRequest> players;
}

@Data
@AllArgsConstructor
public static class UserRequest {
public String id;
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());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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;
}
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package tw.waterballsa.gaas.citadels.domain;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static java.util.UUID.randomUUID;

public class CitadelsGame {
private final String id;
private final List<Player> players;
private final List<RoleCard> roleCards;
private final List<BuildingCard> buildingCards;
public static final Integer DEFAULT_COINS = 2;
public static final Integer DEFAULT_CARD_QUANTITY = 2;

public CitadelsGame(List<Player> players, List<RoleCard> roleCards, List<BuildingCard> buildingCards) {
this.id = randomUUID().toString();
this.players = players;
this.roleCards = roleCards;
this.buildingCards = buildingCards;
}

public CitadelsGame(String id, List<Player> players, List<RoleCard> roleCards, List<BuildingCard> buildingCards) {
this.id = id;
this.players = players;
this.roleCards = roleCards;
this.buildingCards = buildingCards;
}

public String getId() {
return id;
}

public List<Player> getPlayers() {
return List.copyOf(players);
}

public List<BuildingCard> getBuildingCards() {
return List.copyOf(buildingCards);
}

public List<RoleCard> getRoleCards() {
return List.copyOf(roleCards);
}

public void randomlyAwardCrownToOnePlayer() {
Collections.shuffle(players);
Player kingPlayer = players.get(0);
kingPlayer.acquireCrown();
}

public void start() {
randomlyAwardCrownToOnePlayer();
distributingCardsAndCoinsToEachPlayer();
}

private void distributingCardsAndCoinsToEachPlayer() {
players.forEach(player -> {
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
@@ -0,0 +1,39 @@
package tw.waterballsa.gaas.citadels.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.*;


@Getter
@AllArgsConstructor
public class Player {
private String id;
private String name;
private String imageName;
private Integer coins;
private List<BuildingCard> buildingCards;
private RoleCard roleCard;
private Boolean hasCrown;
static final int DEFAULT_COIN = 0;

public Player(String id, String name, String image) {
this(id, name, image, DEFAULT_COIN, new ArrayList<>(), null, null);
}

public void removeCrown() {
hasCrown = false;
}

public void acquireCrown() {
hasCrown = true;
}

public void plusCoins(Integer coins) {
this.coins = this.coins + coins;
}

public void plusCards(List<BuildingCard> buildingCardList) {
buildingCards.addAll(buildingCardList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package tw.waterballsa.gaas.citadels.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class RoleCard {
private int sequence;
private String name;
}
2 changes: 1 addition & 1 deletion packages/backend/spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<artifactId>citadels-backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>citadels-backend</name>
<description>gaas citadels game web</description>
<description>gaas citadels citadelsGame web</description>

<properties>
<java.version>17</java.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
@AllArgsConstructor
@RequestMapping(value = "/api/citadels")
public class CreateRoomController {

private final CreateRoomUseCase createRoomUseCase;

@PostMapping("/room")
Expand Down
Loading

0 comments on commit 92f6366

Please sign in to comment.