generated from Game-as-a-Service/Gaas-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: start and get game API #115 #134
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
...ckend/app/src/main/java/tw/waterballsa/gaas/citadels/app/repositories/GameRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
38 changes: 38 additions & 0 deletions
38
...s/backend/app/src/main/java/tw/waterballsa/gaas/citadels/app/usecases/GetGameUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
...backend/app/src/main/java/tw/waterballsa/gaas/citadels/app/usecases/StartGameUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/BuildingCard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} | ||
|
70 changes: 70 additions & 0 deletions
70
packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/CitadelsGame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/Player.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/RoleCard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用 enum