diff --git a/backend/src/main/java/cz/jzitnik/chronos/interactions/InteractionPlayer.java b/backend/src/main/java/cz/jzitnik/chronos/interactions/InteractionPlayer.java index b13b372..0de1b4d 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/interactions/InteractionPlayer.java +++ b/backend/src/main/java/cz/jzitnik/chronos/interactions/InteractionPlayer.java @@ -1,9 +1,46 @@ package cz.jzitnik.chronos.interactions; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; 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); +import java.lang.reflect.ParameterizedType; + +public abstract class InteractionPlayer { + private final ObjectMapper objectMapper = new ObjectMapper(); + private final TypeReference typeReference; + + // I hate my life, like wtf is this shit Java. Why I cant just pass T to the objectMapper.readValue + protected InteractionPlayer() { + // Automatically resolve the generic type `T` using reflection + this.typeReference = new TypeReference<>() { + @Override + public java.lang.reflect.Type getType() { + return ((ParameterizedType) InteractionPlayer.this.getClass() + .getGenericSuperclass()) + .getActualTypeArguments()[0]; + } + }; + } + + public T readMemory(String memory) { + try { + return objectMapper.readValue(memory, typeReference); + } catch (Exception e) { + throw new RuntimeException("Failed to deserialize memory string into object", e); + } + } + + public String writeMemory(T memory) { + try { + return objectMapper.writeValueAsString(memory); + } catch (JsonProcessingException e) { + throw new RuntimeException("Failed to serialize object to memory string", e); + } + } + + public abstract InteractionResponse play(Player player, Character character, String data); } diff --git a/backend/src/main/java/cz/jzitnik/chronos/interactions/list/Baker.java b/backend/src/main/java/cz/jzitnik/chronos/interactions/list/Baker.java index 511aed1..432b88e 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/interactions/list/Baker.java +++ b/backend/src/main/java/cz/jzitnik/chronos/interactions/list/Baker.java @@ -15,7 +15,7 @@ import org.springframework.stereotype.Service; import java.util.ArrayList; @Service -public class Baker implements InteractionPlayer { +public class Baker extends InteractionPlayer { @Autowired private ItemService itemService; @Autowired @@ -23,7 +23,6 @@ public class Baker implements InteractionPlayer { @Autowired private ItemRepository itemRepository; - @Override public InteractionResponse play(Player player, Character character, String data) { var playerItems = player.getInventory(); var flours = playerItems.stream().filter(item -> item.getItemType().equals(ItemType.FLOUR)).toList(); 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 index 688543e..c1e8659 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/interactions/list/Hangman.java +++ b/backend/src/main/java/cz/jzitnik/chronos/interactions/list/Hangman.java @@ -23,7 +23,19 @@ import java.util.Random; import java.util.Set; @Service -public class Hangman implements InteractionPlayer { +public class Hangman extends InteractionPlayer { + // Memory + @AllArgsConstructor + @NoArgsConstructor + @Getter + @Setter + protected static class HangmanMemory { + private String word; + private Set correctGuessesSet; + private Set incorrectGuessesSet; + private String currentProgress; + } + @Autowired private CharacterRepository characterRepository; @Autowired @@ -43,34 +55,6 @@ public class Hangman implements InteractionPlayer { "polévka", "zázračný", "papírnictví", "šampaňské", "kinematografie", "obloha", "křišťálový" }; - // Memory - @AllArgsConstructor - @NoArgsConstructor - @Getter - @Setter - private static class HangmanMemory { - private String word; - private Set correctGuessesSet; - private Set incorrectGuessesSet; - private String currentProgress; - } - private static HangmanMemory readMemory(String memory) { - ObjectMapper objectMapper = new ObjectMapper(); - try { - return objectMapper.readValue(memory, HangmanMemory.class); - } catch (Exception e) { - throw new RuntimeException("Somebody just fucked up db."); // If this happens idk what is wrong with my life - } - } - private static String writeMemory(HangmanMemory memory) { - ObjectMapper objectMapper = new ObjectMapper(); - try { - return objectMapper.writeValueAsString(memory); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); // Well hopefully this will never happen - } - } - // Response @AllArgsConstructor @Getter @@ -90,7 +74,6 @@ public class Hangman implements InteractionPlayer { private String currentProgress; } - @Override public InteractionResponse play(Player player, Character character, String data) { if (character.getInteractionData().getPlayer() == null) { // New game diff --git a/backend/src/main/java/cz/jzitnik/chronos/interactions/list/Mayor.java b/backend/src/main/java/cz/jzitnik/chronos/interactions/list/Mayor.java index 292e6b0..6923620 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/interactions/list/Mayor.java +++ b/backend/src/main/java/cz/jzitnik/chronos/interactions/list/Mayor.java @@ -15,7 +15,7 @@ import org.springframework.stereotype.Service; import java.util.ArrayList; @Service -public class Mayor implements InteractionPlayer { +public class Mayor extends InteractionPlayer { @Autowired private ItemService itemService; @Autowired @@ -23,7 +23,6 @@ public class Mayor implements InteractionPlayer { @Autowired private ItemRepository itemRepository; - @Override public InteractionResponse play(Player player, Character character, String data) { var playerItems = player.getInventory(); var goldenWatches = playerItems.stream().filter(item -> item.getItemType().equals(ItemType.GOLDEN_WATCH)).toList(); diff --git a/backend/src/main/java/cz/jzitnik/chronos/interactions/list/NumberGuessingGame.java b/backend/src/main/java/cz/jzitnik/chronos/interactions/list/NumberGuessingGame.java index c248e7f..61a13db 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/interactions/list/NumberGuessingGame.java +++ b/backend/src/main/java/cz/jzitnik/chronos/interactions/list/NumberGuessingGame.java @@ -23,7 +23,16 @@ import java.util.ArrayList; import java.util.Random; @Service -public class NumberGuessingGame implements InteractionPlayer { +public class NumberGuessingGame extends InteractionPlayer { + @AllArgsConstructor + @NoArgsConstructor + @Getter + @Setter + protected static class GuessingGameMemory { + private int targetNumber; + private int attemptsLeft; + } + @Autowired private CharacterRepository characterRepository; @Autowired @@ -37,33 +46,6 @@ public class NumberGuessingGame implements InteractionPlayer { private static final int RANGE_MAX = 100; private static final int MAX_ATTEMPTS = 7; - @AllArgsConstructor - @NoArgsConstructor - @Getter - @Setter - private static class GuessingGameMemory { - private int targetNumber; - private int attemptsLeft; - } - - private static GuessingGameMemory readMemory(String memory) { - ObjectMapper objectMapper = new ObjectMapper(); - try { - return objectMapper.readValue(memory, GuessingGameMemory.class); - } catch (Exception e) { - throw new RuntimeException("Error reading memory from DB."); - } - } - - private static String writeMemory(GuessingGameMemory memory) { - ObjectMapper objectMapper = new ObjectMapper(); - try { - return objectMapper.writeValueAsString(memory); - } catch (JsonProcessingException e) { - throw new RuntimeException("Error writing memory to DB."); - } - } - @AllArgsConstructor @Getter @Setter @@ -80,7 +62,6 @@ public class NumberGuessingGame implements InteractionPlayer { private int attemptsLeft; } - @Override public InteractionResponse play(Player player, Character character, String data) { if (character.getInteractionData().getPlayer() == null) { // New game initialization 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 index a30c31b..77409db 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/interactions/list/RockPaperScissors.java +++ b/backend/src/main/java/cz/jzitnik/chronos/interactions/list/RockPaperScissors.java @@ -16,7 +16,7 @@ import java.util.ArrayList; import java.util.Random; @Service -public class RockPaperScissors implements InteractionPlayer { +public class RockPaperScissors extends InteractionPlayer { @Autowired private PlayerRepository playerRepository; @@ -26,7 +26,6 @@ public class RockPaperScissors implements InteractionPlayer { @Autowired private ItemService itemService; - @Override public InteractionResponse play(Player player, Character character, String data) { Random random = new Random(); boolean isLucky = player.isLuck(); diff --git a/backend/src/main/java/cz/jzitnik/chronos/interactions/list/TicTacToe.java b/backend/src/main/java/cz/jzitnik/chronos/interactions/list/TicTacToe.java index 020106f..4b35054 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/interactions/list/TicTacToe.java +++ b/backend/src/main/java/cz/jzitnik/chronos/interactions/list/TicTacToe.java @@ -21,38 +21,20 @@ import java.util.ArrayList; import java.util.Random; @Service -public class TicTacToe implements InteractionPlayer { - @Autowired - private CharacterRepository characterRepository; - @Autowired - private ItemService itemService; - +public class TicTacToe extends InteractionPlayer { @AllArgsConstructor @NoArgsConstructor @Getter @Setter - private static class TicTacToeMemory { + protected static class TicTacToeMemory { private String[] board; private boolean playerTurn; } - private static TicTacToeMemory readMemory(String memory) { - ObjectMapper objectMapper = new ObjectMapper(); - try { - return objectMapper.readValue(memory, TicTacToeMemory.class); - } catch (Exception e) { - throw new RuntimeException("Error reading memory."); - } - } - - private static String writeMemory(TicTacToeMemory memory) { - ObjectMapper objectMapper = new ObjectMapper(); - try { - return objectMapper.writeValueAsString(memory); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); - } - } + @Autowired + private CharacterRepository characterRepository; + @Autowired + private ItemService itemService; @AllArgsConstructor @Getter @@ -70,7 +52,6 @@ public class TicTacToe implements InteractionPlayer { private boolean playerTurn; } - @Override public InteractionResponse play(Player player, Character character, String data) { if (character.getInteractionData().getPlayer() == null) { try { diff --git a/backend/src/main/java/cz/jzitnik/chronos/interactions/list/wordle/Wordle.java b/backend/src/main/java/cz/jzitnik/chronos/interactions/list/wordle/Wordle.java index 1861b4b..925a387 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/interactions/list/wordle/Wordle.java +++ b/backend/src/main/java/cz/jzitnik/chronos/interactions/list/wordle/Wordle.java @@ -21,7 +21,17 @@ import java.util.ArrayList; import java.util.List; @Service -public class Wordle implements InteractionPlayer { +public class Wordle extends InteractionPlayer { + @AllArgsConstructor + @NoArgsConstructor + @Getter + @Setter + protected static class WordleMemory { + private String word; + private List feedbackHistory; + private int attemptsRemaining; + } + @Autowired private CharacterRepository characterRepository; @Autowired @@ -53,36 +63,6 @@ public class Wordle implements InteractionPlayer { private List feedback; } - // Memory - @AllArgsConstructor - @NoArgsConstructor - @Getter - @Setter - private static class WordleMemory { - private String word; - private List feedbackHistory; - private int attemptsRemaining; - } - - private static WordleMemory readMemory(String memory) { - ObjectMapper objectMapper = new ObjectMapper(); - try { - return objectMapper.readValue(memory, WordleMemory.class); - } catch (Exception e) { - System.out.println(e); - throw new RuntimeException("Error reading memory from DB."); - } - } - - private static String writeMemory(WordleMemory memory) { - ObjectMapper objectMapper = new ObjectMapper(); - try { - return objectMapper.writeValueAsString(memory); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); - } - } - // Response @AllArgsConstructor @Getter @@ -100,7 +80,6 @@ public class Wordle implements InteractionPlayer { private int attemptsRemaining; } - @Override public InteractionResponse play(Player player, Character character, String data) { if (character.getInteractionData().getPlayer() == null) { // New game diff --git a/backend/src/main/java/cz/jzitnik/chronos/services/InitGameService.java b/backend/src/main/java/cz/jzitnik/chronos/services/InitGameService.java index 426687e..88224ae 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/services/InitGameService.java +++ b/backend/src/main/java/cz/jzitnik/chronos/services/InitGameService.java @@ -89,7 +89,7 @@ public class InitGameService { var librarian = new Character( "Knihovnice", library, - "Ahoj, jdeš si vypujčit nějakou knihu? Jakto že ne? Učení je důležité. No to je jedno, co teda chceš? Fragment klíče?" + + "Ahoj, jdeš si vypujčit nějakou knihu? Jakto že ne? Učení je důležité. No to je jedno, co teda chceš? Fragment klíče? " + "Budes si muset semnou zahrát nejdříve wordle na to abych ti ho dala." ); librarian.setInteraction(Interaction.Librarian);