tdudzik

Untitled

Jan 17th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.43 KB | None | 0 0
  1. package io.github.tdudzik.testproject;
  2.  
  3. import javaslang.Tuple;
  4. import javaslang.Tuple2;
  5. import javaslang.collection.HashMap;
  6. import javaslang.collection.HashSet;
  7. import javaslang.collection.Map;
  8. import javaslang.collection.Set;
  9.  
  10. import java.util.function.BiFunction;
  11. import java.util.function.Consumer;
  12. import java.util.function.Function;
  13.  
  14. import static com.google.common.base.Preconditions.checkNotNull;
  15.  
  16. class PlayerId {
  17.  
  18.     private final String id;
  19.  
  20.     public PlayerId(String id) {
  21.         this.id = checkNotNull(id);
  22.     }
  23.  
  24.     public String getId() {
  25.         return id;
  26.     }
  27.  
  28.     @Override
  29.     public boolean equals(Object o) {
  30.         if (this == o) return true;
  31.         if (o == null || getClass() != o.getClass()) return false;
  32.  
  33.         PlayerId playerId = (PlayerId) o;
  34.  
  35.         return id.equals(playerId.id);
  36.  
  37.     }
  38.  
  39.     @Override
  40.     public int hashCode() {
  41.         return id.hashCode();
  42.     }
  43.  
  44. }
  45.  
  46. class Card {
  47.  
  48.     private final String name;
  49.  
  50.     public Card(String name) {
  51.         this.name = name;
  52.     }
  53.  
  54.     @Override
  55.     public String toString() {
  56.         return "Card{" +
  57.                 "name='" + name + '\'' +
  58.                 '}';
  59.     }
  60.  
  61. }
  62.  
  63. class Deck {
  64.  
  65.     public Tuple2<Set<Card>, Deck> drawMany(int n) {
  66.         Set<Card> cards = HashSet.empty();
  67.         for (int i = 0; i < n; i++) {
  68.             cards = cards.add(new Card("Card " + i));
  69.         }
  70.         return new Tuple2<>(cards, this);
  71.     }
  72.  
  73. }
  74.  
  75. class GameState {
  76.  
  77.     private final Set<PlayerId> players;
  78.     private final Map<PlayerId, Set<Card>> cardsByPlayer;
  79.     private final Deck deck;
  80.  
  81.     public GameState(Set<PlayerId> players,
  82.                      Map<PlayerId, Set<Card>> cardsByPlayer,
  83.                      Deck deck) {
  84.         this.players = checkNotNull(players);
  85.         this.cardsByPlayer = checkNotNull(cardsByPlayer);
  86.         this.deck = deck;
  87.     }
  88.  
  89.     public GameState map(Function<GameState, GameState> mapper) {
  90.         return mapper.apply(this);
  91.     }
  92.  
  93.     public Set<PlayerId> getPlayers() {
  94.         return players;
  95.     }
  96.  
  97.     public Map<PlayerId, Set<Card>> getCardsByPlayer() {
  98.         return cardsByPlayer;
  99.     }
  100.  
  101.     public Deck getDeck() {
  102.         return deck;
  103.     }
  104.  
  105.     public GameState setDeck(Deck deck) {
  106.         return new GameState(players, cardsByPlayer, deck);
  107.     }
  108.  
  109. }
  110.  
  111. interface Event {
  112. }
  113.  
  114. class CardsWasDealtEvent implements Event {
  115.  
  116.     private final PlayerId playerId;
  117.     private final Set<Card> cards;
  118.  
  119.     public CardsWasDealtEvent(PlayerId playerId, Set<Card> cards) {
  120.         this.playerId = checkNotNull(playerId);
  121.         this.cards = checkNotNull(cards);
  122.     }
  123.  
  124.     public PlayerId getPlayerId() {
  125.         return playerId;
  126.     }
  127.  
  128.     public Set<Card> getCards() {
  129.         return cards;
  130.     }
  131.  
  132. }
  133.  
  134. class Message<T extends Event> {
  135.  
  136.     public static <T extends Event> MessageBuilder<T> builder() {
  137.         return new MessageBuilder<>();
  138.     }
  139.  
  140.     public static class MessageBuilder<T extends Event> {
  141.  
  142.         public MessageBuilder<T> to(PlayerId... playerIds) {
  143.             return this;
  144.         }
  145.  
  146.         public MessageBuilder<T> toAll() {
  147.             return this;
  148.         }
  149.  
  150.         public MessageBuilder<T> withEvent(T event) {
  151.             return this;
  152.         }
  153.  
  154.         public Message<T> build() {
  155.             return new Message<>();
  156.         }
  157.  
  158.     }
  159.  
  160.     public String toString() {
  161.         return "Message";
  162.     }
  163.  
  164. }
  165.  
  166. interface Command extends BiFunction<GameState, Consumer<Message>, GameState> {
  167.  
  168.     GameState apply(GameState gameState, Consumer<Message> messageConsumer);
  169.  
  170. }
  171.  
  172. class DealCardsCommand implements Command {
  173.  
  174.     @Override
  175.     public GameState apply(GameState gameState, Consumer<Message> messageConsumer) {
  176.         Tuple2<Map<PlayerId, Set<Card>>, GameState> initialState = new Tuple2<>(HashMap.empty(), gameState);
  177.         Tuple2<Map<PlayerId, Set<Card>>, GameState> afterDrawing = gameState.getPlayers()
  178.                 .foldLeft(initialState, (currentState, playerId) -> {
  179.                     Tuple2<Set<Card>, Deck> drawed = currentState._2.getDeck().drawMany(5);
  180.                     Set<Card> playerCards = drawed._1;
  181.                     Deck deck = drawed._2;
  182.                     return new Tuple2<>(currentState._1.put(playerId, playerCards), gameState.setDeck(deck));
  183.                 });
  184.         afterDrawing._1.forEach((playerId, cards) -> {
  185.             Message<CardsWasDealtEvent> message = Message
  186.                     .<CardsWasDealtEvent>builder()
  187.                     .to(playerId)
  188.                     .withEvent(new CardsWasDealtEvent(playerId, cards))
  189.                     .build();
  190.             messageConsumer.accept(message);
  191.         });
  192.  
  193.         return afterDrawing._2;
  194.     }
  195.  
  196. }
  197.  
  198. public class TestProject {
  199.  
  200.     public static void main(String[] args) {
  201.         Set<PlayerId> players = HashSet.of(new PlayerId("id-111"), new PlayerId("id-222"), new PlayerId("id-333"));
  202.         Map<PlayerId, Set<Card>> cardsByPlayer = HashMap.ofEntries(
  203.                 Tuple.of(new PlayerId("id-111"), HashSet.empty()),
  204.                 Tuple.of(new PlayerId("id-222"), HashSet.empty()),
  205.                 Tuple.of(new PlayerId("id-333"), HashSet.empty())
  206.         );
  207.         GameState gameState = new GameState(players, cardsByPlayer, new Deck());
  208.  
  209.         DealCardsCommand dealCardsCommand = new DealCardsCommand();
  210.         dealCardsCommand.apply(gameState, System.out::println);
  211.     }
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment