diff --git a/backend/src/main/java/cz/jzitnik/chronos/controllers/GameController.java b/backend/src/main/java/cz/jzitnik/chronos/controllers/GameController.java index 65426db..feec699 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/controllers/GameController.java +++ b/backend/src/main/java/cz/jzitnik/chronos/controllers/GameController.java @@ -81,4 +81,13 @@ public class GameController { return ResponseEntity.ok(UnifiedResponse.success(TestGameKeyResponse.WORKING)); } + + @GetMapping("/winnable") + @CheckUser + public ResponseEntity> winnable(@RequestParam String playerKey) { + var player = playerRepository.findByPlayerKey(playerKey).get(); + var game = player.getGame(); + + return ResponseEntity.ok(UnifiedResponse.success(game.winnable())); + } } diff --git a/backend/src/main/java/cz/jzitnik/chronos/entities/Game.java b/backend/src/main/java/cz/jzitnik/chronos/entities/Game.java index 280d3cd..540c7bc 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/entities/Game.java +++ b/backend/src/main/java/cz/jzitnik/chronos/entities/Game.java @@ -44,4 +44,33 @@ public class Game { public void addPlayer(Player player) { this.players.add(player); } + + @JsonIgnore + public boolean winnable() { + var ownedFragments = 0; + for (Player player : players) { + for (Item item : player.getInventory()) { + if (item.getItemType() == ItemType.KEY_FRAGMENT) { + ownedFragments++; + } + } + } + + + var roomItems = 0; + var characters = new ArrayList(); + + for (Room room : rooms) { + characters.addAll(room.getCharacters()); + for (Item item : room.getItems()) { + if (item.getItemType() == ItemType.KEY_FRAGMENT) { + roomItems++; + } + } + } + + var unplayedCharacters = characters.stream().filter(character -> !character.isInteractedWith()).toList().size(); + + return roomItems + unplayedCharacters + ownedFragments >= 5; + } } 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 40b1c6f..688543e 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 @@ -152,7 +152,7 @@ public class Hangman implements InteractionPlayer { var incorrectGuessesSet = interactionMemory.getIncorrectGuessesSet(); StringBuilder currentProgress = new StringBuilder(interactionMemory.getCurrentProgress()); - if (data == null || data.isEmpty()) { + if (data.isEmpty()) { return new InteractionResponse(false, "invalid_input", new ArrayList<>()); } 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 00997e5..031db0d 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 @@ -7,6 +7,7 @@ import cz.jzitnik.chronos.entities.Player; import cz.jzitnik.chronos.interactions.InteractionPlayer; import cz.jzitnik.chronos.payload.responses.InteractionResponse; import cz.jzitnik.chronos.repository.CharacterRepository; +import cz.jzitnik.chronos.repository.ItemRepository; import cz.jzitnik.chronos.services.ItemService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -19,6 +20,8 @@ public class Mayor implements InteractionPlayer { private ItemService itemService; @Autowired private CharacterRepository characterRepository; + @Autowired + private ItemRepository itemRepository; @Override public InteractionResponse play(Player player, Character character, String data) { @@ -30,10 +33,12 @@ public class Mayor implements InteractionPlayer { } var golderWatch = golderWatches.getFirst(); + golderWatch.setOwner(null); + itemRepository.save(golderWatch); playerItems.remove(golderWatch); var item = new Item(ItemType.KEY_FRAGMENT, player); - itemService.addItem(player, item); // itemRepository.save is ran by this function so we don't need to save the player when we removed the item from his inv + itemService.addItem(player, item); character.setInteractedWith(true); characterRepository.save(character); diff --git a/frontend/src/main/java/cz/jzitnik/api/ApiService.java b/frontend/src/main/java/cz/jzitnik/api/ApiService.java index ef80582..ea2c500 100644 --- a/frontend/src/main/java/cz/jzitnik/api/ApiService.java +++ b/frontend/src/main/java/cz/jzitnik/api/ApiService.java @@ -22,6 +22,11 @@ public interface ApiService { @POST("game/new") Call> newGame(); + @GET("game/winnable") + Call> winnable( + @Query("playerKey") String playerKey + ); + @POST("game/players/new") Call> newPlayer( @Query("gameKey") String gameKey, diff --git a/frontend/src/main/java/cz/jzitnik/game/Chronos.java b/frontend/src/main/java/cz/jzitnik/game/Chronos.java index 7ab1289..923b9fb 100644 --- a/frontend/src/main/java/cz/jzitnik/game/Chronos.java +++ b/frontend/src/main/java/cz/jzitnik/game/Chronos.java @@ -237,9 +237,8 @@ public class Chronos { var characters = room.getCharacters(); if (characters.isEmpty()) { Cli.type("V místnosti se nenachází jediná duše..."); - } else { - talk(characters); } + talk(characters); var responseRooms = apiService.getAllRooms(localData.getUserSecret()).execute(); var rooms = responseRooms.body().getData().get().stream().filter(rm -> !rm.getId().equals(room.getId())).toList(); diff --git a/frontend/src/main/java/cz/jzitnik/game/CommandPalette.java b/frontend/src/main/java/cz/jzitnik/game/CommandPalette.java index 32c8bec..97773a3 100644 --- a/frontend/src/main/java/cz/jzitnik/game/CommandPalette.java +++ b/frontend/src/main/java/cz/jzitnik/game/CommandPalette.java @@ -19,6 +19,7 @@ public class CommandPalette { USER_PROFILE, CHAT_OPEN, CHAT_SEND, + CHECK_WINNABLE, EXIT, } @@ -62,6 +63,7 @@ public class CommandPalette { allCommands.add(new Command("Otevřít profil uživatele", CommandType.USER_PROFILE)); allCommands.add(new Command("Otevřít chat", CommandType.CHAT_OPEN)); allCommands.add(new Command("Odeslat zprávu do chatu", CommandType.CHAT_SEND)); + allCommands.add(new Command("Zkontrolovat vyhratelnost hry", CommandType.CHECK_WINNABLE)); allCommands.add(new Command("Odejít ze hry", CommandType.EXIT)); var responseInvFull = apiService.inventoryIsFull(playerKey).execute(); @@ -115,6 +117,19 @@ public class CommandPalette { var chat = new Chat(apiService, playerKey); chat.sendMessage(); + yield displayIndex(); + } + case CHECK_WINNABLE -> { + var response = apiService.winnable(playerKey).execute(); + var winnable = response.body().getData().get(); + + System.out.println(Cli.Colors.RED + "Experimentální!!!" + Cli.Colors.RESET + " Toto není 100% jestli je hra vyhratelná."); + if (winnable) { + Cli.info("Hra je dost možná vyhratelná!"); + } else { + Cli.info("Hra je dost možná nevyhratelná!"); + } + yield displayIndex(); } }; diff --git a/frontend/src/main/java/cz/jzitnik/utils/Cli.java b/frontend/src/main/java/cz/jzitnik/utils/Cli.java index 7babf8b..9363931 100644 --- a/frontend/src/main/java/cz/jzitnik/utils/Cli.java +++ b/frontend/src/main/java/cz/jzitnik/utils/Cli.java @@ -169,7 +169,7 @@ public class Cli { if (choice >= 1 && choice <= options.size()) { return choice - 1; } else { - System.out.println("Neplatní možnost, vyberte číslo mezi 1 a " + options.size()); + System.out.println("Neplatná možnost, vyberte číslo mezi 1 a " + options.size()); } } else { System.out.println("Neplatný vstup, vložte číslo.");