Get cards and sort them.

This commit is contained in:
aminecmi 2021-10-30 14:33:45 +02:00
parent 9e789cc645
commit 01f1b6170c
4 changed files with 92 additions and 2 deletions

View File

@ -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<Shape> shapeOrder;
private final List<Number> numberOrder;
private final List<Card> sortedCards;
@JsonCreator
public SortingResult(@JsonProperty("shapeOrder") List<Shape> shapeOrder,
@JsonProperty("numberOrder") List<Number> numberOrder,
@JsonProperty("sortedCards") List<Card> sortedCards) {
this.shapeOrder = shapeOrder;
this.numberOrder = numberOrder;
this.sortedCards = sortedCards;
}
public List<Shape> getShapeOrder() {
return shapeOrder;
}
public List<Number> getNumberOrder() {
return numberOrder;
}
public List<Card> getSortedCards() {
return sortedCards;
}
} }

View File

@ -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<Card> getCardHand() {
return gameService.cardHand();
}
@RequestMapping(value = "/card-hand:sort", method = RequestMethod.PUT)
@ResponseBody
public SortingResult sortHand(@RequestBody final List<Card> hand) {
return gameService.sortHand(hand);
}
} }

View File

@ -3,11 +3,13 @@ package fr.aminelouveaau.cartes.service;
import fr.aminelouveaau.cartes.model.Card; import fr.aminelouveaau.cartes.model.Card;
import fr.aminelouveaau.cartes.model.Number; import fr.aminelouveaau.cartes.model.Number;
import fr.aminelouveaau.cartes.model.Shape; import fr.aminelouveaau.cartes.model.Shape;
import fr.aminelouveaau.cartes.model.api.SortingResult;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
@Service @Service
public class GameService { public class GameService {
@ -57,4 +59,19 @@ public class GameService {
.thenComparing(c -> numberOrder.indexOf(c.getNumber())); .thenComparing(c -> numberOrder.indexOf(c.getNumber()));
} }
public List<Card> cardHand() {
List<Card> hand = new ArrayList<>();
for (int i = 0; i < this.cardHandNumber; i++) {
hand.add(this.deck.pop());
}
return hand;
}
public SortingResult sortHand(List<Card> hand) {
return new SortingResult(
shapeOrder,
numberOrder,
hand.stream().sorted((o1, o2) -> comparator.compare(o1, o2)).collect(Collectors.toList())
);
}
} }

View File

@ -32,6 +32,15 @@ class CartesApplicationTests {
assertThat(deck.size()).isEqualTo(4 * 13).withFailMessage("Deck is not complete"); assertThat(deck.size()).isEqualTo(4 * 13).withFailMessage("Deck is not complete");
} }
@Test
void testHandGeneration() {
List<Card> hand = gameService.cardHand();
assertThat(hand.size()).isEqualTo(CARDS_PER_HAND).withFailMessage("Hand is not complete");
List<Card> 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 @Test
void contextLoads() { void contextLoads() {
} }