Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.github.tdudzik.testproject;
- import javaslang.Tuple;
- import javaslang.Tuple2;
- import javaslang.collection.HashMap;
- import javaslang.collection.HashSet;
- import javaslang.collection.Map;
- import javaslang.collection.Set;
- import java.util.function.BiFunction;
- import java.util.function.Consumer;
- import java.util.function.Function;
- import static com.google.common.base.Preconditions.checkNotNull;
- class PlayerId {
- private final String id;
- public PlayerId(String id) {
- this.id = checkNotNull(id);
- }
- public String getId() {
- return id;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- PlayerId playerId = (PlayerId) o;
- return id.equals(playerId.id);
- }
- @Override
- public int hashCode() {
- return id.hashCode();
- }
- }
- class Card {
- private final String name;
- public Card(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "Card{" +
- "name='" + name + '\'' +
- '}';
- }
- }
- class Deck {
- public Tuple2<Set<Card>, Deck> drawMany(int n) {
- Set<Card> cards = HashSet.empty();
- for (int i = 0; i < n; i++) {
- cards = cards.add(new Card("Card " + i));
- }
- return new Tuple2<>(cards, this);
- }
- }
- class GameState {
- private final Set<PlayerId> players;
- private final Map<PlayerId, Set<Card>> cardsByPlayer;
- private final Deck deck;
- public GameState(Set<PlayerId> players,
- Map<PlayerId, Set<Card>> cardsByPlayer,
- Deck deck) {
- this.players = checkNotNull(players);
- this.cardsByPlayer = checkNotNull(cardsByPlayer);
- this.deck = deck;
- }
- public GameState map(Function<GameState, GameState> mapper) {
- return mapper.apply(this);
- }
- public Set<PlayerId> getPlayers() {
- return players;
- }
- public Map<PlayerId, Set<Card>> getCardsByPlayer() {
- return cardsByPlayer;
- }
- public Deck getDeck() {
- return deck;
- }
- public GameState setDeck(Deck deck) {
- return new GameState(players, cardsByPlayer, deck);
- }
- }
- interface Event {
- }
- class CardsWasDealtEvent implements Event {
- private final PlayerId playerId;
- private final Set<Card> cards;
- public CardsWasDealtEvent(PlayerId playerId, Set<Card> cards) {
- this.playerId = checkNotNull(playerId);
- this.cards = checkNotNull(cards);
- }
- public PlayerId getPlayerId() {
- return playerId;
- }
- public Set<Card> getCards() {
- return cards;
- }
- }
- class Message<T extends Event> {
- public static <T extends Event> MessageBuilder<T> builder() {
- return new MessageBuilder<>();
- }
- public static class MessageBuilder<T extends Event> {
- public MessageBuilder<T> to(PlayerId... playerIds) {
- return this;
- }
- public MessageBuilder<T> toAll() {
- return this;
- }
- public MessageBuilder<T> withEvent(T event) {
- return this;
- }
- public Message<T> build() {
- return new Message<>();
- }
- }
- public String toString() {
- return "Message";
- }
- }
- interface Command extends BiFunction<GameState, Consumer<Message>, GameState> {
- GameState apply(GameState gameState, Consumer<Message> messageConsumer);
- }
- class DealCardsCommand implements Command {
- @Override
- public GameState apply(GameState gameState, Consumer<Message> messageConsumer) {
- Tuple2<Map<PlayerId, Set<Card>>, GameState> initialState = new Tuple2<>(HashMap.empty(), gameState);
- Tuple2<Map<PlayerId, Set<Card>>, GameState> afterDrawing = gameState.getPlayers()
- .foldLeft(initialState, (currentState, playerId) -> {
- Tuple2<Set<Card>, Deck> drawed = currentState._2.getDeck().drawMany(5);
- Set<Card> playerCards = drawed._1;
- Deck deck = drawed._2;
- return new Tuple2<>(currentState._1.put(playerId, playerCards), gameState.setDeck(deck));
- });
- afterDrawing._1.forEach((playerId, cards) -> {
- Message<CardsWasDealtEvent> message = Message
- .<CardsWasDealtEvent>builder()
- .to(playerId)
- .withEvent(new CardsWasDealtEvent(playerId, cards))
- .build();
- messageConsumer.accept(message);
- });
- return afterDrawing._2;
- }
- }
- public class TestProject {
- public static void main(String[] args) {
- Set<PlayerId> players = HashSet.of(new PlayerId("id-111"), new PlayerId("id-222"), new PlayerId("id-333"));
- Map<PlayerId, Set<Card>> cardsByPlayer = HashMap.ofEntries(
- Tuple.of(new PlayerId("id-111"), HashSet.empty()),
- Tuple.of(new PlayerId("id-222"), HashSet.empty()),
- Tuple.of(new PlayerId("id-333"), HashSet.empty())
- );
- GameState gameState = new GameState(players, cardsByPlayer, new Deck());
- DealCardsCommand dealCardsCommand = new DealCardsCommand();
- dealCardsCommand.apply(gameState, System.out::println);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment