From 53b4e9090e522227c70418c5069e637acef14cd9 Mon Sep 17 00:00:00 2001 From: jzitnik-dev Date: Fri, 27 Dec 2024 08:07:38 +0100 Subject: [PATCH] feat: Started implementing cashier --- .../chronos/interactions/Interaction.java | 3 +- .../interactions/InteractionPlayer.java | 9 ++ .../interactions/InteractionService.java | 77 ++-------------- .../chronos/interactions/list/Hangman.java | 16 ++++ .../interactions/list/RockPaperScissors.java | 88 +++++++++++++++++++ 5 files changed, 121 insertions(+), 72 deletions(-) create mode 100644 backend/src/main/java/cz/jzitnik/chronos/interactions/InteractionPlayer.java create mode 100644 backend/src/main/java/cz/jzitnik/chronos/interactions/list/Hangman.java create mode 100644 backend/src/main/java/cz/jzitnik/chronos/interactions/list/RockPaperScissors.java diff --git a/backend/src/main/java/cz/jzitnik/chronos/interactions/Interaction.java b/backend/src/main/java/cz/jzitnik/chronos/interactions/Interaction.java index 1fe1fc3..d6d3557 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/interactions/Interaction.java +++ b/backend/src/main/java/cz/jzitnik/chronos/interactions/Interaction.java @@ -1,5 +1,6 @@ package cz.jzitnik.chronos.interactions; public enum Interaction { - Farmer + Farmer, + Cashier } \ No newline at end of file diff --git a/backend/src/main/java/cz/jzitnik/chronos/interactions/InteractionPlayer.java b/backend/src/main/java/cz/jzitnik/chronos/interactions/InteractionPlayer.java new file mode 100644 index 0000000..b13b372 --- /dev/null +++ b/backend/src/main/java/cz/jzitnik/chronos/interactions/InteractionPlayer.java @@ -0,0 +1,9 @@ +package cz.jzitnik.chronos.interactions; + +import cz.jzitnik.chronos.entities.Player; +import cz.jzitnik.chronos.entities.Character; +import cz.jzitnik.chronos.payload.responses.InteractionResponse; + +public interface InteractionPlayer { + InteractionResponse play(Player player, Character character, String data); +} diff --git a/backend/src/main/java/cz/jzitnik/chronos/interactions/InteractionService.java b/backend/src/main/java/cz/jzitnik/chronos/interactions/InteractionService.java index cf0c9c9..ec65d85 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/interactions/InteractionService.java +++ b/backend/src/main/java/cz/jzitnik/chronos/interactions/InteractionService.java @@ -1,29 +1,20 @@ package cz.jzitnik.chronos.interactions; -import cz.jzitnik.chronos.entities.Item; -import cz.jzitnik.chronos.entities.ItemType; import cz.jzitnik.chronos.entities.Player; import cz.jzitnik.chronos.entities.Character; +import cz.jzitnik.chronos.interactions.list.Hangman; +import cz.jzitnik.chronos.interactions.list.RockPaperScissors; import cz.jzitnik.chronos.payload.responses.InteractionResponse; -import cz.jzitnik.chronos.repository.CharacterRepository; -import cz.jzitnik.chronos.repository.PlayerRepository; -import cz.jzitnik.chronos.services.ItemService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.Random; - @Service public class InteractionService { @Autowired - private PlayerRepository playerRepository; + private RockPaperScissors rockPaperScissors; @Autowired - private CharacterRepository characterRepository; - - @Autowired - private ItemService itemService; + private Hangman hangman; @FunctionalInterface public interface Function3 { @@ -32,64 +23,8 @@ public class InteractionService { public Function3 mapInteraction(Interaction interaction) { return switch (interaction) { - case Farmer -> ((player, character, data) -> { - Random random = new Random(); - boolean isLucky = player.isLuck(); - - int characterChoice = random.nextInt(3); - - int userChoice = switch (data) { - case "0" -> 0; // Rock - case "1" -> 1; // Paper - case "2" -> 2; // Scissors - default -> throw new IllegalArgumentException("Invalid choice"); - }; - - if (isLucky) { - // 50/50 chance - boolean playerWins = random.nextBoolean(); - - // Reset the luck potion after use - player.setLuck(false); - - if (playerWins) { - var item = new Item(ItemType.KEY_FRAGMENT, player); - itemService.addItem(player, item); - - character.setInteractedWith(true); - characterRepository.save(character); - - var items = new ArrayList(); - items.add(item); - - return new InteractionResponse(true, "Vyhrál jsi, dostáváš ode mě jeden fragment klíče.", items); - } else { - character.setInteractedWith(true); - characterRepository.save(character); - return new InteractionResponse(false, "Prohrál jsi. Žádný fragment klíče ode mě už nedostaneš. Zkus to u někoho jiného.", new ArrayList<>()); - } - } else { - // Standard rock-paper-scissors game logic - if (userChoice == characterChoice) { - return new InteractionResponse(false, "Remíza. Jestli chceš můžeš si se mnou zahrát ještě jednou.", new ArrayList<>()); - } else if ((userChoice == 0 && characterChoice == 2) || (userChoice == 1 && characterChoice == 0) || (userChoice == 2 && characterChoice == 1)) { - var item = new Item(ItemType.KEY_FRAGMENT, player); - itemService.addItem(player, item); - - character.setInteractedWith(true); - characterRepository.save(character); - - var items = new ArrayList(); - items.add(item); - - return new InteractionResponse(true, "Vyhrál jsi, dostáváš ode mě jeden fragment klíče.", items); - } else { - character.setInteractedWith(true); - characterRepository.save(character); - return new InteractionResponse(false, "Prohrál jsi. Žádný fragment klíče ode mě už nedostaneš. Zkus to u někoho jiného.", new ArrayList<>()); - } - } - }); + case Farmer -> rockPaperScissors::play; + case Cashier -> hangman::play; }; } } \ No newline at end of file diff --git a/backend/src/main/java/cz/jzitnik/chronos/interactions/list/Hangman.java b/backend/src/main/java/cz/jzitnik/chronos/interactions/list/Hangman.java new file mode 100644 index 0000000..ce3c544 --- /dev/null +++ b/backend/src/main/java/cz/jzitnik/chronos/interactions/list/Hangman.java @@ -0,0 +1,16 @@ +package cz.jzitnik.chronos.interactions.list; + +import cz.jzitnik.chronos.entities.Character; +import cz.jzitnik.chronos.entities.Player; +import cz.jzitnik.chronos.interactions.InteractionPlayer; +import cz.jzitnik.chronos.payload.responses.InteractionResponse; +import org.springframework.stereotype.Service; + +@Service +public class Hangman implements InteractionPlayer { + + @Override + public InteractionResponse play(Player player, Character character, String data) { + return null; + } +} diff --git a/backend/src/main/java/cz/jzitnik/chronos/interactions/list/RockPaperScissors.java b/backend/src/main/java/cz/jzitnik/chronos/interactions/list/RockPaperScissors.java new file mode 100644 index 0000000..5905d4c --- /dev/null +++ b/backend/src/main/java/cz/jzitnik/chronos/interactions/list/RockPaperScissors.java @@ -0,0 +1,88 @@ +package cz.jzitnik.chronos.interactions.list; + +import cz.jzitnik.chronos.entities.Item; +import cz.jzitnik.chronos.entities.ItemType; +import cz.jzitnik.chronos.entities.Player; +import cz.jzitnik.chronos.entities.Character; +import cz.jzitnik.chronos.interactions.InteractionPlayer; +import cz.jzitnik.chronos.payload.responses.InteractionResponse; +import cz.jzitnik.chronos.repository.CharacterRepository; +import cz.jzitnik.chronos.repository.PlayerRepository; +import cz.jzitnik.chronos.services.ItemService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.Random; + +@Service +public class RockPaperScissors implements InteractionPlayer { + @Autowired + private PlayerRepository playerRepository; + + @Autowired + private CharacterRepository characterRepository; + + @Autowired + private ItemService itemService; + + @Override + public InteractionResponse play(Player player, Character character, String data) { + Random random = new Random(); + boolean isLucky = player.isLuck(); + + int characterChoice = random.nextInt(3); + + int userChoice = switch (data) { + case "0" -> 0; // Rock + case "1" -> 1; // Paper + case "2" -> 2; // Scissors + default -> throw new IllegalArgumentException("Invalid choice"); + }; + + if (isLucky) { + // 50/50 chance + boolean playerWins = random.nextBoolean(); + + // Reset the luck potion after use + player.setLuck(false); + + if (playerWins) { + var item = new Item(ItemType.KEY_FRAGMENT, player); + itemService.addItem(player, item); + + character.setInteractedWith(true); + characterRepository.save(character); + + var items = new ArrayList(); + items.add(item); + + return new InteractionResponse(true, "Vyhrál jsi, dostáváš ode mě jeden fragment klíče.", items); + } else { + character.setInteractedWith(true); + characterRepository.save(character); + return new InteractionResponse(false, "Prohrál jsi. Žádný fragment klíče ode mě už nedostaneš. Zkus to u někoho jiného.", new ArrayList<>()); + } + } else { + // Standard rock-paper-scissors game logic + if (userChoice == characterChoice) { + return new InteractionResponse(false, "Remíza. Jestli chceš můžeš si se mnou zahrát ještě jednou.", new ArrayList<>()); + } else if ((userChoice == 0 && characterChoice == 2) || (userChoice == 1 && characterChoice == 0) || (userChoice == 2 && characterChoice == 1)) { + var item = new Item(ItemType.KEY_FRAGMENT, player); + itemService.addItem(player, item); + + character.setInteractedWith(true); + characterRepository.save(character); + + var items = new ArrayList(); + items.add(item); + + return new InteractionResponse(true, "Vyhrál jsi, dostáváš ode mě jeden fragment klíče.", items); + } else { + character.setInteractedWith(true); + characterRepository.save(character); + return new InteractionResponse(false, "Prohrál jsi. Žádný fragment klíče ode mě už nedostaneš. Zkus to u někoho jiného.", new ArrayList<>()); + } + } + } +}