refactor: Small rewrite of InteractionPlayer
This commit is contained in:
parent
d2df9ab696
commit
bbf9e3e7a7
@ -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<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;
|
||||
|
||||
@Service
|
||||
public class Baker implements InteractionPlayer {
|
||||
public class Baker extends InteractionPlayer<Object> {
|
||||
@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();
|
||||
|
@ -23,7 +23,19 @@ import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
@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
|
||||
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<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
|
||||
@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
|
||||
|
@ -15,7 +15,7 @@ import org.springframework.stereotype.Service;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@Service
|
||||
public class Mayor implements InteractionPlayer {
|
||||
public class Mayor extends InteractionPlayer<Object> {
|
||||
@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();
|
||||
|
@ -23,7 +23,16 @@ import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
@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
|
||||
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
|
||||
|
@ -16,7 +16,7 @@ import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
@Service
|
||||
public class RockPaperScissors implements InteractionPlayer {
|
||||
public class RockPaperScissors extends InteractionPlayer<Object> {
|
||||
@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();
|
||||
|
@ -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<TicTacToe.TicTacToeMemory> {
|
||||
@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 {
|
||||
|
@ -21,7 +21,17 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@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
|
||||
private CharacterRepository characterRepository;
|
||||
@Autowired
|
||||
@ -53,36 +63,6 @@ public class Wordle implements InteractionPlayer {
|
||||
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
|
||||
@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
|
||||
|
Loading…
x
Reference in New Issue
Block a user