feat: Baker, Blacksmith and winning game
This commit is contained in:
@ -3,6 +3,7 @@ package cz.jzitnik.api;
|
||||
import cz.jzitnik.api.requests.InteractionRequest;
|
||||
import cz.jzitnik.api.requests.MessageRequest;
|
||||
import cz.jzitnik.api.requests.PlayerNameRequest;
|
||||
import cz.jzitnik.api.responses.GameWonResponse;
|
||||
import cz.jzitnik.api.responses.InteractionResponse;
|
||||
import cz.jzitnik.api.responses.InventoryFullResponse;
|
||||
import cz.jzitnik.api.responses.UnifiedResponse;
|
||||
@ -127,4 +128,14 @@ public interface ApiService {
|
||||
@Query("playerKey") String playerKey,
|
||||
@Query("roomId") Long roomId
|
||||
);
|
||||
|
||||
@POST("game/fragments")
|
||||
Call<UnifiedResponse<Object, Error>> putKeyFragments(
|
||||
@Query("playerKey") String playerKey
|
||||
);
|
||||
|
||||
@GET("game/won")
|
||||
Call<UnifiedResponse<GameWonResponse, Error>> gameWon(
|
||||
@Query("playerKey") String playerKey
|
||||
);
|
||||
}
|
||||
|
@ -0,0 +1,10 @@
|
||||
package cz.jzitnik.api.responses;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class GameWonResponse {
|
||||
private boolean won;
|
||||
|
||||
private String msg;
|
||||
}
|
@ -5,5 +5,7 @@ public enum Interaction {
|
||||
Cashier,
|
||||
Librarian,
|
||||
Innkeeper,
|
||||
Mayor
|
||||
Mayor,
|
||||
Blacksmith,
|
||||
Baker
|
||||
}
|
||||
|
@ -6,7 +6,10 @@ import java.util.Set;
|
||||
public enum ItemType {
|
||||
KEY_FRAGMENT,
|
||||
LUCK_POTION,
|
||||
GOLDEN_WATCH;
|
||||
GOLDEN_WATCH,
|
||||
COAL,
|
||||
WATER,
|
||||
FLOUR;
|
||||
|
||||
private static final Set<ItemType> usableItems = new HashSet<>();
|
||||
|
||||
@ -24,6 +27,9 @@ public enum ItemType {
|
||||
case LUCK_POTION -> "Lektvar štěstí";
|
||||
case KEY_FRAGMENT -> "Fragment klíče";
|
||||
case GOLDEN_WATCH -> "Zlaté hodinky";
|
||||
case COAL -> "Uhlí";
|
||||
case WATER -> "Láhev vody";
|
||||
case FLOUR -> "Mouka";
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -65,7 +65,7 @@ public class Message {
|
||||
|
||||
yield "Hráč " + Cli.Colors.BLUE + author.getName() + Cli.Colors.RESET + " sice získal " + Cli.Colors.BLUE + item + Cli.Colors.RESET + ", ale měl plný inventář a tím pádem ho " + Cli.Colors.RED + "navždy ztatil" + Cli.Colors.RESET + ".";
|
||||
} catch (JsonProcessingException e) {
|
||||
yield "Hráč " + author.getName() + " sice získal item, ale měl plný inventář a tím pádem ho navždy ztratil.";
|
||||
yield "Hráč " + Cli.Colors.BLUE + author.getName() + Cli.Colors.RESET + " sice získal item, ale měl plný inventář a tím pádem ho navždy ztratil.";
|
||||
}
|
||||
}
|
||||
case ITEM_USED -> {
|
||||
@ -85,6 +85,7 @@ public class Message {
|
||||
yield "Hráč " + Cli.Colors.BLUE + author.getName() + Cli.Colors.RESET + " využil item unknown.";
|
||||
}
|
||||
}
|
||||
case KEY_FRAGMENT_HANDED_OVER -> "Hráč " + Cli.Colors.BLUE + author.getName() + Cli.Colors.RESET + " odevzdal " + Cli.Colors.BLUE + "Fragment klíče" + Cli.Colors.RESET + ".";
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -6,5 +6,6 @@ public enum MessageType {
|
||||
GOT_ITEM,
|
||||
LOST_ITEM,
|
||||
ITEM_USED,
|
||||
KEY_FRAGMENT_HANDED_OVER,
|
||||
JOINED,
|
||||
}
|
||||
|
@ -238,7 +238,7 @@ public class Chronos {
|
||||
if (characters.isEmpty()) {
|
||||
Cli.type("V místnosti se nenachází jediná duše...");
|
||||
}
|
||||
talk(characters);
|
||||
talk(characters, room.getName().equals("Outside"));
|
||||
|
||||
var responseRooms = apiService.getAllRooms(localData.getUserSecret()).execute();
|
||||
var rooms = responseRooms.body().getData().get().stream().filter(rm -> !rm.getId().equals(room.getId())).toList();
|
||||
@ -251,12 +251,35 @@ public class Chronos {
|
||||
visit(rooms.get(selectedIndex).getId(), true);
|
||||
}
|
||||
|
||||
public void talk(List<Character> characters) throws IOException {
|
||||
public void talk(List<Character> characters, boolean isOutside) throws IOException {
|
||||
List<String> commands = new ArrayList<>(characters.stream().map(chachar -> "Promluvit si s " + chachar.getName()).toList());
|
||||
if (isOutside) {
|
||||
commands.add(Cli.Colors.GREEN + "Odevzdat fragmenty klíče" + Cli.Colors.RESET);
|
||||
}
|
||||
commands.add("Přejít do jiné místnosti");
|
||||
CommandPalette commandPalette = new CommandPalette(commands, apiService, localData.getUserSecret());
|
||||
int selectedIndex = commandPalette.displayIndex();
|
||||
|
||||
if (isOutside && selectedIndex == commands.size() - 2) {
|
||||
// Odevzdat fragmenty klíče
|
||||
var response = apiService.getMe(localData.getUserSecret()).execute();
|
||||
var me = response.body().getData().get();
|
||||
var keyFragments = me.getInventory().stream().filter(item -> item.getItemType().equals(ItemType.KEY_FRAGMENT)).toList();
|
||||
|
||||
if (keyFragments.isEmpty()) {
|
||||
Cli.error("Nemáte žádné fragmenty klíče v inventáři!");
|
||||
talk(characters, false);
|
||||
return;
|
||||
}
|
||||
|
||||
apiService.putKeyFragments(getLocalData().getUserSecret()).execute();
|
||||
|
||||
Cli.success("Odevzdal jsi " + keyFragments.size() + "x fragment klíče.");
|
||||
|
||||
talk(characters, false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectedIndex == commands.size() - 1) {
|
||||
// Přejít do jiné místnosti
|
||||
return;
|
||||
@ -287,6 +310,6 @@ public class Chronos {
|
||||
var res = apiService.getCurrentRoom(localData.getUserSecret()).execute();
|
||||
var room = res.body().getData().get();
|
||||
|
||||
talk(room.getCharacters());
|
||||
talk(room.getCharacters(), isOutside);
|
||||
}
|
||||
}
|
@ -2,10 +2,12 @@ package cz.jzitnik.game;
|
||||
|
||||
import cz.jzitnik.api.ApiService;
|
||||
import cz.jzitnik.utils.Cli;
|
||||
import cz.jzitnik.utils.ConfigPathProvider;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -56,6 +58,22 @@ public class CommandPalette {
|
||||
}
|
||||
|
||||
public int displayIndex() throws IOException {
|
||||
var res = apiService.gameWon(playerKey).execute();
|
||||
var content = res.body().getData().get();
|
||||
|
||||
if (content.isWon()) {
|
||||
System.out.println("\n\n");
|
||||
Cli.printHeader("Hra byla vyhrána");
|
||||
Cli.typeSkritek(content.getMsg());
|
||||
|
||||
var configPath = ConfigPathProvider.getPath();
|
||||
|
||||
File file = new File(configPath);
|
||||
file.delete();
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
List<Command> allCommands = new ArrayList<>(commands);
|
||||
|
||||
// Commands that can be used anywhere
|
||||
|
@ -12,7 +12,7 @@ public class Interactions {
|
||||
var response = apiService.getInteractionData(playerKey, character.getId()).execute();
|
||||
var interactionData = response.body().getData().get();
|
||||
|
||||
if (character.isInteractedWith()) {
|
||||
if (character.isInteractedWith() && !interactionData.getInteractedWithText().isBlank()) {
|
||||
Cli.type(character, interactionData.getInteractedWithText());
|
||||
return;
|
||||
}
|
||||
@ -42,6 +42,14 @@ public class Interactions {
|
||||
Mayor mayor = new Mayor(character, apiService, playerKey);
|
||||
mayor.play();
|
||||
}
|
||||
case Blacksmith -> {
|
||||
NumberGuessingGame numberGuessingGame = new NumberGuessingGame(character, apiService, playerKey);
|
||||
numberGuessingGame.play();
|
||||
}
|
||||
case Baker -> {
|
||||
Baker baker = new Baker(character, apiService, playerKey);
|
||||
baker.play();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,43 @@
|
||||
package cz.jzitnik.game.interactions.list;
|
||||
|
||||
import cz.jzitnik.api.ApiService;
|
||||
import cz.jzitnik.api.requests.InteractionRequest;
|
||||
import cz.jzitnik.api.types.Character;
|
||||
import cz.jzitnik.api.types.ItemType;
|
||||
import cz.jzitnik.utils.Cli;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
@AllArgsConstructor
|
||||
public class Baker {
|
||||
private Character character;
|
||||
private ApiService apiService;
|
||||
private String playerKey;
|
||||
|
||||
public void play() throws IOException {
|
||||
var response = apiService.getMe(playerKey).execute();
|
||||
var me = response.body().getData().get();
|
||||
|
||||
var hasWater = me.getInventory().stream().anyMatch(item -> item.getItemType().equals(ItemType.WATER));
|
||||
var hasFlour = me.getInventory().stream().anyMatch(item -> item.getItemType().equals(ItemType.FLOUR));
|
||||
|
||||
if (!hasWater || !hasFlour) {
|
||||
// Player does not have water or flour
|
||||
return;
|
||||
}
|
||||
|
||||
var options = new String[]{"Dát pekařovi vodu a mouku", "Nedat pekařovi vodu a mouku"};
|
||||
var selected = Cli.selectOptionIndex(Arrays.stream(options).toList());
|
||||
|
||||
if (selected == 0) {
|
||||
var res = apiService.interact(playerKey, new InteractionRequest("", character.getId())).execute();
|
||||
var data = res.body().getData().get();
|
||||
|
||||
Cli.type(character, data.getResponseText());
|
||||
Cli.gotItems(data.getItems());
|
||||
}
|
||||
}
|
||||
}
|
@ -21,12 +21,12 @@ public class Mayor {
|
||||
var response = apiService.getMe(playerKey).execute();
|
||||
var me = response.body().getData().get();
|
||||
|
||||
if (!me.getInventory().stream().anyMatch(item -> item.getItemType().equals(ItemType.GOLDEN_WATCH))) {
|
||||
if (me.getInventory().stream().noneMatch(item -> item.getItemType().equals(ItemType.GOLDEN_WATCH))) {
|
||||
// Player does not have golden watch
|
||||
return;
|
||||
}
|
||||
|
||||
var options = new String[]{"Dát starostovi zlaté hodinky", "Nedát starostovi zlaté hodinky"};
|
||||
var options = new String[]{"Dát starostovi zlaté hodinky", "Nedat starostovi zlaté hodinky"};
|
||||
var selected = Cli.selectOptionIndex(Arrays.stream(options).toList());
|
||||
|
||||
if (selected == 0) {
|
||||
|
@ -0,0 +1,116 @@
|
||||
package cz.jzitnik.game.interactions.list;
|
||||
|
||||
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.api.types.ItemType;
|
||||
import cz.jzitnik.utils.Cli;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.io.Console;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class NumberGuessingGame {
|
||||
private Character character;
|
||||
private ApiService apiService;
|
||||
private String playerKey;
|
||||
|
||||
@Getter
|
||||
private static class NumberGuessingResponse {
|
||||
public enum Type {
|
||||
GAME_CREATED,
|
||||
GAME_WON,
|
||||
GAME_LOST,
|
||||
GUESS_FEEDBACK
|
||||
}
|
||||
|
||||
private Type type;
|
||||
private String message;
|
||||
private int attemptsLeft;
|
||||
}
|
||||
|
||||
public void play() throws IOException {
|
||||
var response = apiService.isInteracting(playerKey, character.getId()).execute();
|
||||
var interacting = response.body().getData().get();
|
||||
|
||||
if (!interacting) {
|
||||
init();
|
||||
} else {
|
||||
// Resume the game where it was left off
|
||||
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 response = apiService.getMe(playerKey).execute();
|
||||
var me = response.body().getData().get();
|
||||
|
||||
if (me.getInventory().stream().noneMatch(item -> item.getItemType().equals(ItemType.COAL))) {
|
||||
// Hráč nemá uhlí
|
||||
return;
|
||||
}
|
||||
|
||||
var options = new String[] { "Dát kovářovi uhlí", "Nedat kovářovi uhlí" };
|
||||
var index = Cli.selectOptionIndex(Arrays.asList(options));
|
||||
|
||||
if (index == 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
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 gameData = objectMapper.readValue(data.getResponseText(), NumberGuessingResponse.class);
|
||||
|
||||
switch (gameData.getType()) {
|
||||
case GAME_WON -> {
|
||||
Cli.type(character, gameData.getMessage());
|
||||
Cli.gotItems(data.getItems());
|
||||
}
|
||||
case GAME_LOST -> Cli.type(character, gameData.getMessage());
|
||||
case GUESS_FEEDBACK, GAME_CREATED -> {
|
||||
Cli.type(character, gameData.getMessage());
|
||||
System.out.println("Počet pokusů: " + gameData.getAttemptsLeft());
|
||||
|
||||
var guess = askForGuess();
|
||||
|
||||
var response = apiService.interact(playerKey, new InteractionRequest(guess, character.getId())).execute();
|
||||
|
||||
var body = response.body().getData().get();
|
||||
|
||||
gameContinue(body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String askForGuess() {
|
||||
Console console = System.console();
|
||||
|
||||
String data = console.readLine("Zadejte číslo: ");
|
||||
|
||||
try {
|
||||
int num = Integer.parseInt(data);
|
||||
|
||||
if (num < 1 || num > 100) {
|
||||
Cli.error("Číslo musí být v rozmezí 1-100");
|
||||
return askForGuess();
|
||||
}
|
||||
|
||||
return data;
|
||||
} catch (NumberFormatException e) {
|
||||
Cli.error("Neplatný vstup! Zadejte platné číslo.");
|
||||
return askForGuess();
|
||||
}
|
||||
}
|
||||
}
|
@ -126,24 +126,27 @@ public class Cli {
|
||||
}
|
||||
|
||||
var itemsMapped = items.stream().map(Item::toString).toList();
|
||||
Cli.info("Dostal jste: " + String.join(", ", itemsMapped));
|
||||
info("Dostal jste: " + String.join(", ", itemsMapped));
|
||||
}
|
||||
|
||||
public static void type(Character character, String text) {
|
||||
Cli.type(Cli.Colors.YELLOW + character.getName() + ": " + Cli.Colors.RESET + text);
|
||||
type(Colors.YELLOW + character.getName() + ": " + Colors.RESET + text);
|
||||
}
|
||||
public static void type(String text) {
|
||||
Cli.typeText(Cli.wrapText(text));
|
||||
typeText(wrapText(text));
|
||||
}
|
||||
public static void typeSkritek(String text) {
|
||||
type(Colors.YELLOW + "Skřítek: " + Colors.RESET + text);
|
||||
}
|
||||
|
||||
public static void info(String text) {
|
||||
System.out.println(Colors.BLUE + "Info: " + Colors.RESET + Cli.wrapText(text, 6));
|
||||
System.out.println(Colors.BLUE + "Info: " + Colors.RESET + wrapText(text, 6));
|
||||
}
|
||||
public static void success(String text) {
|
||||
System.out.println(Colors.GREEN + "Úspěch: " + Colors.RESET + Cli.wrapText(text, 8));
|
||||
System.out.println(Colors.GREEN + "Úspěch: " + Colors.RESET + wrapText(text, 8));
|
||||
}
|
||||
public static void error(String text) {
|
||||
System.out.println(Colors.RED + "Chyba: " + Colors.RESET + Cli.wrapText(text, 7));
|
||||
System.out.println(Colors.RED + "Chyba: " + Colors.RESET + wrapText(text, 7));
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user