From 01f1b6170cabff7f4e176942526c17fc7eef0b28 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Sat, 30 Oct 2021 14:33:45 +0200 Subject: [PATCH] Get cards and sort them. --- .../cartes/model/api/SortingResult.java | 36 ++++++++++++++++++- .../cartes/rest/GameResource.java | 32 ++++++++++++++++- .../cartes/service/GameService.java | 17 +++++++++ .../cartes/CartesApplicationTests.java | 9 +++++ 4 files changed, 92 insertions(+), 2 deletions(-) diff --git a/src/main/java/fr/aminelouveaau/cartes/model/api/SortingResult.java b/src/main/java/fr/aminelouveaau/cartes/model/api/SortingResult.java index 1a958f2..2290752 100644 --- a/src/main/java/fr/aminelouveaau/cartes/model/api/SortingResult.java +++ b/src/main/java/fr/aminelouveaau/cartes/model/api/SortingResult.java @@ -1,2 +1,36 @@ -package fr.aminelouveaau.cartes.model.api;public class SortingResult { +package fr.aminelouveaau.cartes.model.api; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import fr.aminelouveaau.cartes.model.Card; +import fr.aminelouveaau.cartes.model.Number; +import fr.aminelouveaau.cartes.model.Shape; + +import java.util.List; + +public class SortingResult { + private final List shapeOrder; + private final List numberOrder; + private final List sortedCards; + + @JsonCreator + public SortingResult(@JsonProperty("shapeOrder") List shapeOrder, + @JsonProperty("numberOrder") List numberOrder, + @JsonProperty("sortedCards") List sortedCards) { + this.shapeOrder = shapeOrder; + this.numberOrder = numberOrder; + this.sortedCards = sortedCards; + } + + public List getShapeOrder() { + return shapeOrder; + } + + public List getNumberOrder() { + return numberOrder; + } + + public List getSortedCards() { + return sortedCards; + } } diff --git a/src/main/java/fr/aminelouveaau/cartes/rest/GameResource.java b/src/main/java/fr/aminelouveaau/cartes/rest/GameResource.java index 447f8e2..b034a07 100644 --- a/src/main/java/fr/aminelouveaau/cartes/rest/GameResource.java +++ b/src/main/java/fr/aminelouveaau/cartes/rest/GameResource.java @@ -1,2 +1,32 @@ -package fr.aminelouveaau.cartes.rest;public class GameResource { +package fr.aminelouveaau.cartes.rest; + +import fr.aminelouveaau.cartes.model.Card; +import fr.aminelouveaau.cartes.model.api.SortingResult; +import fr.aminelouveaau.cartes.service.GameService; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping({"/api/game", "/api/game/v0"}) +public class GameResource { + private final GameService gameService; + + + public GameResource(GameService gameService) { + this.gameService = gameService; + } + + @RequestMapping(value = "/card-hand", method = RequestMethod.GET) + @ResponseBody + public List getCardHand() { + return gameService.cardHand(); + } + + @RequestMapping(value = "/card-hand:sort", method = RequestMethod.PUT) + @ResponseBody + public SortingResult sortHand(@RequestBody final List hand) { + return gameService.sortHand(hand); + } + } diff --git a/src/main/java/fr/aminelouveaau/cartes/service/GameService.java b/src/main/java/fr/aminelouveaau/cartes/service/GameService.java index 531dfb0..4f218e5 100644 --- a/src/main/java/fr/aminelouveaau/cartes/service/GameService.java +++ b/src/main/java/fr/aminelouveaau/cartes/service/GameService.java @@ -3,11 +3,13 @@ package fr.aminelouveaau.cartes.service; import fr.aminelouveaau.cartes.model.Card; import fr.aminelouveaau.cartes.model.Number; import fr.aminelouveaau.cartes.model.Shape; +import fr.aminelouveaau.cartes.model.api.SortingResult; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.util.*; +import java.util.stream.Collectors; @Service public class GameService { @@ -57,4 +59,19 @@ public class GameService { .thenComparing(c -> numberOrder.indexOf(c.getNumber())); } + public List cardHand() { + List hand = new ArrayList<>(); + for (int i = 0; i < this.cardHandNumber; i++) { + hand.add(this.deck.pop()); + } + return hand; + } + + public SortingResult sortHand(List hand) { + return new SortingResult( + shapeOrder, + numberOrder, + hand.stream().sorted((o1, o2) -> comparator.compare(o1, o2)).collect(Collectors.toList()) + ); + } } diff --git a/src/test/java/fr/aminelouveaau/cartes/CartesApplicationTests.java b/src/test/java/fr/aminelouveaau/cartes/CartesApplicationTests.java index d51e0b5..8e56d83 100644 --- a/src/test/java/fr/aminelouveaau/cartes/CartesApplicationTests.java +++ b/src/test/java/fr/aminelouveaau/cartes/CartesApplicationTests.java @@ -32,6 +32,15 @@ class CartesApplicationTests { assertThat(deck.size()).isEqualTo(4 * 13).withFailMessage("Deck is not complete"); } + @Test + void testHandGeneration() { + List hand = gameService.cardHand(); + assertThat(hand.size()).isEqualTo(CARDS_PER_HAND).withFailMessage("Hand is not complete"); + List deck = gameService.getDeck(); + assertThat(deck.size()).isNotEqualTo(4 * 13).withFailMessage("Where did the cards come from ?"); + assertThat(deck.size()).isEqualTo((4 * 13) - CARDS_PER_HAND).withFailMessage("Wrong number of cards removed."); + } + @Test void contextLoads() { }