refactor: Small rewrite of InteractionPlayer
This commit is contained in:
parent
d2df9ab696
commit
bbf9e3e7a7
@ -1,9 +1,46 @@
|
|||||||
package cz.jzitnik.chronos.interactions;
|
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.Player;
|
||||||
import cz.jzitnik.chronos.entities.Character;
|
import cz.jzitnik.chronos.entities.Character;
|
||||||
import cz.jzitnik.chronos.payload.responses.InteractionResponse;
|
import cz.jzitnik.chronos.payload.responses.InteractionResponse;
|
||||||
|
|
||||||
public interface InteractionPlayer {
|
import java.lang.reflect.ParameterizedType;
|
||||||
InteractionResponse play(Player player, Character character, String data);
|
|
||||||
|
public abstract class InteractionPlayer<T> {
|
||||||
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
private final TypeReference<T> 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);
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class Baker implements InteractionPlayer {
|
public class Baker extends InteractionPlayer<Object> {
|
||||||
@Autowired
|
@Autowired
|
||||||
private ItemService itemService;
|
private ItemService itemService;
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -23,7 +23,6 @@ public class Baker implements InteractionPlayer {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ItemRepository itemRepository;
|
private ItemRepository itemRepository;
|
||||||
|
|
||||||
@Override
|
|
||||||
public InteractionResponse play(Player player, Character character, String data) {
|
public InteractionResponse play(Player player, Character character, String data) {
|
||||||
var playerItems = player.getInventory();
|
var playerItems = player.getInventory();
|
||||||
var flours = playerItems.stream().filter(item -> item.getItemType().equals(ItemType.FLOUR)).toList();
|
var flours = playerItems.stream().filter(item -> item.getItemType().equals(ItemType.FLOUR)).toList();
|
||||||
|
@ -23,7 +23,19 @@ import java.util.Random;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class Hangman implements InteractionPlayer {
|
public class Hangman extends InteractionPlayer<Hangman.HangmanMemory> {
|
||||||
|
// Memory
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
protected static class HangmanMemory {
|
||||||
|
private String word;
|
||||||
|
private Set<java.lang.Character> correctGuessesSet;
|
||||||
|
private Set<java.lang.Character> incorrectGuessesSet;
|
||||||
|
private String currentProgress;
|
||||||
|
}
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CharacterRepository characterRepository;
|
private CharacterRepository characterRepository;
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -43,34 +55,6 @@ public class Hangman implements InteractionPlayer {
|
|||||||
"polévka", "zázračný", "papírnictví", "šampaňské", "kinematografie", "obloha", "křišťálový"
|
"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<java.lang.Character> correctGuessesSet;
|
|
||||||
private Set<java.lang.Character> 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
|
// Response
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Getter
|
@Getter
|
||||||
@ -90,7 +74,6 @@ public class Hangman implements InteractionPlayer {
|
|||||||
private String currentProgress;
|
private String currentProgress;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public InteractionResponse play(Player player, Character character, String data) {
|
public InteractionResponse play(Player player, Character character, String data) {
|
||||||
if (character.getInteractionData().getPlayer() == null) {
|
if (character.getInteractionData().getPlayer() == null) {
|
||||||
// New game
|
// New game
|
||||||
|
@ -15,7 +15,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class Mayor implements InteractionPlayer {
|
public class Mayor extends InteractionPlayer<Object> {
|
||||||
@Autowired
|
@Autowired
|
||||||
private ItemService itemService;
|
private ItemService itemService;
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -23,7 +23,6 @@ public class Mayor implements InteractionPlayer {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ItemRepository itemRepository;
|
private ItemRepository itemRepository;
|
||||||
|
|
||||||
@Override
|
|
||||||
public InteractionResponse play(Player player, Character character, String data) {
|
public InteractionResponse play(Player player, Character character, String data) {
|
||||||
var playerItems = player.getInventory();
|
var playerItems = player.getInventory();
|
||||||
var goldenWatches = playerItems.stream().filter(item -> item.getItemType().equals(ItemType.GOLDEN_WATCH)).toList();
|
var goldenWatches = playerItems.stream().filter(item -> item.getItemType().equals(ItemType.GOLDEN_WATCH)).toList();
|
||||||
|
@ -23,7 +23,16 @@ import java.util.ArrayList;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class NumberGuessingGame implements InteractionPlayer {
|
public class NumberGuessingGame extends InteractionPlayer<NumberGuessingGame.GuessingGameMemory> {
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
protected static class GuessingGameMemory {
|
||||||
|
private int targetNumber;
|
||||||
|
private int attemptsLeft;
|
||||||
|
}
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CharacterRepository characterRepository;
|
private CharacterRepository characterRepository;
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -37,33 +46,6 @@ public class NumberGuessingGame implements InteractionPlayer {
|
|||||||
private static final int RANGE_MAX = 100;
|
private static final int RANGE_MAX = 100;
|
||||||
private static final int MAX_ATTEMPTS = 7;
|
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
|
@AllArgsConstructor
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@ -80,7 +62,6 @@ public class NumberGuessingGame implements InteractionPlayer {
|
|||||||
private int attemptsLeft;
|
private int attemptsLeft;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public InteractionResponse play(Player player, Character character, String data) {
|
public InteractionResponse play(Player player, Character character, String data) {
|
||||||
if (character.getInteractionData().getPlayer() == null) {
|
if (character.getInteractionData().getPlayer() == null) {
|
||||||
// New game initialization
|
// New game initialization
|
||||||
|
@ -16,7 +16,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class RockPaperScissors implements InteractionPlayer {
|
public class RockPaperScissors extends InteractionPlayer<Object> {
|
||||||
@Autowired
|
@Autowired
|
||||||
private PlayerRepository playerRepository;
|
private PlayerRepository playerRepository;
|
||||||
|
|
||||||
@ -26,7 +26,6 @@ public class RockPaperScissors implements InteractionPlayer {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ItemService itemService;
|
private ItemService itemService;
|
||||||
|
|
||||||
@Override
|
|
||||||
public InteractionResponse play(Player player, Character character, String data) {
|
public InteractionResponse play(Player player, Character character, String data) {
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
boolean isLucky = player.isLuck();
|
boolean isLucky = player.isLuck();
|
||||||
|
@ -21,38 +21,20 @@ import java.util.ArrayList;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class TicTacToe implements InteractionPlayer {
|
public class TicTacToe extends InteractionPlayer<TicTacToe.TicTacToeMemory> {
|
||||||
@Autowired
|
|
||||||
private CharacterRepository characterRepository;
|
|
||||||
@Autowired
|
|
||||||
private ItemService itemService;
|
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
private static class TicTacToeMemory {
|
protected static class TicTacToeMemory {
|
||||||
private String[] board;
|
private String[] board;
|
||||||
private boolean playerTurn;
|
private boolean playerTurn;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TicTacToeMemory readMemory(String memory) {
|
@Autowired
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
private CharacterRepository characterRepository;
|
||||||
try {
|
@Autowired
|
||||||
return objectMapper.readValue(memory, TicTacToeMemory.class);
|
private ItemService itemService;
|
||||||
} 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Getter
|
@Getter
|
||||||
@ -70,7 +52,6 @@ public class TicTacToe implements InteractionPlayer {
|
|||||||
private boolean playerTurn;
|
private boolean playerTurn;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public InteractionResponse play(Player player, Character character, String data) {
|
public InteractionResponse play(Player player, Character character, String data) {
|
||||||
if (character.getInteractionData().getPlayer() == null) {
|
if (character.getInteractionData().getPlayer() == null) {
|
||||||
try {
|
try {
|
||||||
|
@ -21,7 +21,17 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class Wordle implements InteractionPlayer {
|
public class Wordle extends InteractionPlayer<Wordle.WordleMemory> {
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
protected static class WordleMemory {
|
||||||
|
private String word;
|
||||||
|
private List<GuessFeedback> feedbackHistory;
|
||||||
|
private int attemptsRemaining;
|
||||||
|
}
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CharacterRepository characterRepository;
|
private CharacterRepository characterRepository;
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -53,36 +63,6 @@ public class Wordle implements InteractionPlayer {
|
|||||||
private List<CharacterFeedback> feedback;
|
private List<CharacterFeedback> feedback;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Memory
|
|
||||||
@AllArgsConstructor
|
|
||||||
@NoArgsConstructor
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
private static class WordleMemory {
|
|
||||||
private String word;
|
|
||||||
private List<GuessFeedback> 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
|
// Response
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Getter
|
@Getter
|
||||||
@ -100,7 +80,6 @@ public class Wordle implements InteractionPlayer {
|
|||||||
private int attemptsRemaining;
|
private int attemptsRemaining;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public InteractionResponse play(Player player, Character character, String data) {
|
public InteractionResponse play(Player player, Character character, String data) {
|
||||||
if (character.getInteractionData().getPlayer() == null) {
|
if (character.getInteractionData().getPlayer() == null) {
|
||||||
// New game
|
// New game
|
||||||
|
@ -89,7 +89,7 @@ public class InitGameService {
|
|||||||
var librarian = new Character(
|
var librarian = new Character(
|
||||||
"Knihovnice",
|
"Knihovnice",
|
||||||
library,
|
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."
|
"Budes si muset semnou zahrát nejdříve wordle na to abych ti ho dala."
|
||||||
);
|
);
|
||||||
librarian.setInteraction(Interaction.Librarian);
|
librarian.setInteraction(Interaction.Librarian);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user