feat: Hangman interaction
This commit is contained in:
@ -83,6 +83,12 @@ public interface ApiService {
|
||||
@Query("characterId") Long characterId
|
||||
);
|
||||
|
||||
@GET("game/characters/interacting")
|
||||
Call<UnifiedResponse<Boolean, Error>> isInteracting(
|
||||
@Query("playerKey") String playerKey,
|
||||
@Query("characterId") Long characterId
|
||||
);
|
||||
|
||||
@POST("game/characters/interact")
|
||||
Call<UnifiedResponse<InteractionResponse, Error>> interact(
|
||||
@Query("playerKey") String playerKey,
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cz.jzitnik.api.types;
|
||||
|
||||
public enum Interaction {
|
||||
Farmer
|
||||
Farmer,
|
||||
Cashier
|
||||
}
|
||||
|
104
frontend/src/main/java/cz/jzitnik/game/interactions/Hangman.java
Normal file
104
frontend/src/main/java/cz/jzitnik/game/interactions/Hangman.java
Normal file
@ -0,0 +1,104 @@
|
||||
package cz.jzitnik.game.interactions;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import cz.jzitnik.api.ApiService;
|
||||
import cz.jzitnik.api.requests.InteractionRequest;
|
||||
import cz.jzitnik.api.responses.InteractionResponse;
|
||||
import cz.jzitnik.api.types.Character;
|
||||
import cz.jzitnik.utils.Cli;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.io.Console;
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class Hangman {
|
||||
private Character character;
|
||||
private ApiService apiService;
|
||||
private String playerKey;
|
||||
|
||||
@Getter
|
||||
private static class HangmanResponse {
|
||||
public enum Type {
|
||||
GAME_CREATED,
|
||||
GAME_WON,
|
||||
GAME_LOST,
|
||||
GAME_CONTINUE,
|
||||
}
|
||||
|
||||
private Type type;
|
||||
|
||||
private Set<java.lang.Character> correctGuessesSet;
|
||||
private Set<java.lang.Character> incorrectGuessesSet;
|
||||
private String currentProgress;
|
||||
}
|
||||
|
||||
public void play() throws IOException {
|
||||
var response = apiService.isInteracting(playerKey, character.getId()).execute();
|
||||
var interacting = response.body().getData().get();
|
||||
|
||||
if (!interacting) {
|
||||
init();
|
||||
} else {
|
||||
// Get data where we have left of
|
||||
var body = apiService.interact(playerKey, new InteractionRequest("get_data", character.getId())).execute().body();
|
||||
var data = body.getData().get();
|
||||
gameContinue(data);
|
||||
}
|
||||
}
|
||||
|
||||
private void init() throws IOException {
|
||||
var body = apiService.interact(playerKey, new InteractionRequest("", character.getId())).execute().body();
|
||||
var data = body.getData().get();
|
||||
gameContinue(data);
|
||||
}
|
||||
|
||||
private void gameContinue(InteractionResponse data) throws IOException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
var hangmanData = objectMapper.readValue(data.getResponseText(), HangmanResponse.class);
|
||||
|
||||
switch (hangmanData.getType()) {
|
||||
case GAME_WON -> {
|
||||
Cli.type(character, "Vyhrál jsi! Slovo bylo: " + hangmanData.getCurrentProgress());
|
||||
Cli.gotItems(data.getItems());
|
||||
}
|
||||
case GAME_LOST -> Cli.type(character, "Prohrál jsi! Slovo jsi celé neuhodl. Fragment klíče ti nedám!");
|
||||
case GAME_CONTINUE, GAME_CREATED -> {
|
||||
System.out.println("Špatně uhodnuté písmena: " + hangmanData.getIncorrectGuessesSet());
|
||||
System.out.println("Slovo: " + hangmanData.getCurrentProgress());
|
||||
|
||||
// I hate my life
|
||||
var guessingChar = askForChar(Stream.concat(hangmanData.getCorrectGuessesSet().stream(), hangmanData.getIncorrectGuessesSet().stream()).collect(Collectors.toSet()));
|
||||
|
||||
var response = apiService.interact(playerKey, new InteractionRequest(guessingChar, character.getId())).execute();
|
||||
|
||||
var body = response.body().getData().get();
|
||||
|
||||
gameContinue(body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String askForChar(Set<java.lang.Character> guessed) {
|
||||
Console console = System.console();
|
||||
|
||||
String data = console.readLine("Zadejte jedno písmeno: ");
|
||||
|
||||
if (data.length() != 1) {
|
||||
Cli.error("Neplatný vstup!");
|
||||
return askForChar(guessed);
|
||||
}
|
||||
|
||||
if (guessed.contains(data.charAt(0))) {
|
||||
Cli.error("Toto písmeno jste již se snažil uhodnout!");
|
||||
return askForChar(guessed);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
@ -20,9 +20,10 @@ public class Interactions {
|
||||
|
||||
Cli.type(character, interactionData.getStartText());
|
||||
|
||||
// "Object _ =" is just for java compiler to ensure that all interactions are covered.
|
||||
switch (character.getInteraction()) {
|
||||
case Farmer -> {
|
||||
var options = new String[] {"Kámen", "Nůžky", "Papír"};
|
||||
case Farmer -> {
|
||||
var options = new String[]{"Kámen", "Nůžky", "Papír"};
|
||||
var selected = Cli.selectOptionIndex(Arrays.stream(options).toList());
|
||||
|
||||
var requestData = new InteractionRequest(String.valueOf(selected), character.getId());
|
||||
@ -35,6 +36,11 @@ public class Interactions {
|
||||
if (interactionResponse.isSuccess()) {
|
||||
Cli.gotItems(interactionResponse.getItems());
|
||||
}
|
||||
|
||||
}
|
||||
case Cashier -> {
|
||||
Hangman hangman = new Hangman(character, apiService, playerKey);
|
||||
hangman.play();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user