From 89d9799ae221dfd9ad38bb7843d07ec84a6f03f5 Mon Sep 17 00:00:00 2001 From: jzitnik-dev Date: Mon, 23 Dec 2024 16:24:11 +0100 Subject: [PATCH] feat: Implemented full inv warning --- .../chronos/controllers/PlayerController.java | 9 +++++++++ .../jzitnik/chronos/entities/MessageType.java | 1 + .../responses/InventoryFullResponse.java | 12 ++++++++++++ .../jzitnik/chronos/services/GameService.java | 5 ++--- .../jzitnik/chronos/services/ItemService.java | 19 +++++++++++++++++++ .../main/java/cz/jzitnik/api/ApiService.java | 6 ++++++ .../api/responses/InventoryFullResponse.java | 12 ++++++++++++ .../java/cz/jzitnik/api/types/Message.java | 12 ++++++++++++ .../cz/jzitnik/api/types/MessageType.java | 1 + .../java/cz/jzitnik/game/CommandPalette.java | 7 +++++++ 10 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 backend/src/main/java/cz/jzitnik/chronos/payload/responses/InventoryFullResponse.java create mode 100644 frontend/src/main/java/cz/jzitnik/api/responses/InventoryFullResponse.java diff --git a/backend/src/main/java/cz/jzitnik/chronos/controllers/PlayerController.java b/backend/src/main/java/cz/jzitnik/chronos/controllers/PlayerController.java index afa904e..9e36c06 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/controllers/PlayerController.java +++ b/backend/src/main/java/cz/jzitnik/chronos/controllers/PlayerController.java @@ -5,6 +5,7 @@ import cz.jzitnik.chronos.entities.Player; import cz.jzitnik.chronos.payload.errors.ItemNotUsableException; import cz.jzitnik.chronos.payload.errors.NotFoundError; import cz.jzitnik.chronos.payload.requests.PlayerNameRequest; +import cz.jzitnik.chronos.payload.responses.InventoryFullResponse; import cz.jzitnik.chronos.payload.responses.UnifiedResponse; import cz.jzitnik.chronos.repository.GameRepository; import cz.jzitnik.chronos.repository.ItemRepository; @@ -59,6 +60,14 @@ public class PlayerController { return ResponseEntity.ok(UnifiedResponse.success(player)); } + @GetMapping("/me/inv_full") + @CheckUser + public ResponseEntity> inventoryIsFull(@RequestParam String playerKey) { + var player = playerRepository.findByPlayerKey(playerKey).get(); + + return ResponseEntity.ok(UnifiedResponse.success(new InventoryFullResponse(player.getInventory().size() >= 3))); + } + @PostMapping("/use_item") @CheckUser public ResponseEntity> useItem(@RequestParam String playerKey, @RequestParam Long itemId) { diff --git a/backend/src/main/java/cz/jzitnik/chronos/entities/MessageType.java b/backend/src/main/java/cz/jzitnik/chronos/entities/MessageType.java index 033d509..9abef6c 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/entities/MessageType.java +++ b/backend/src/main/java/cz/jzitnik/chronos/entities/MessageType.java @@ -4,5 +4,6 @@ public enum MessageType { CUSTOM, MOVE_TO_ROOM, GOT_ITEM, + LOST_ITEM, JOINED, } diff --git a/backend/src/main/java/cz/jzitnik/chronos/payload/responses/InventoryFullResponse.java b/backend/src/main/java/cz/jzitnik/chronos/payload/responses/InventoryFullResponse.java new file mode 100644 index 0000000..7f19508 --- /dev/null +++ b/backend/src/main/java/cz/jzitnik/chronos/payload/responses/InventoryFullResponse.java @@ -0,0 +1,12 @@ +package cz.jzitnik.chronos.payload.responses; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@AllArgsConstructor +@NoArgsConstructor +@Getter +public class InventoryFullResponse { + private boolean full; +} diff --git a/backend/src/main/java/cz/jzitnik/chronos/services/GameService.java b/backend/src/main/java/cz/jzitnik/chronos/services/GameService.java index 23bf20f..464cd93 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/services/GameService.java +++ b/backend/src/main/java/cz/jzitnik/chronos/services/GameService.java @@ -1,8 +1,6 @@ package cz.jzitnik.chronos.services; -import cz.jzitnik.chronos.entities.Game; -import cz.jzitnik.chronos.entities.Player; -import cz.jzitnik.chronos.entities.Room; +import cz.jzitnik.chronos.entities.*; import cz.jzitnik.chronos.repository.GameRepository; import cz.jzitnik.chronos.repository.PlayerRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -72,6 +70,7 @@ public class GameService { player.setPlayerKey(generateUniquePlayerKey()); player.setGame(game); player.setCurrentRoom(game.getDefaultRoom()); + player.getMessages().add(new Message(player, "", MessageType.JOINED)); if (game.getPlayers().isEmpty()) { game.setAdminPlayer(player); diff --git a/backend/src/main/java/cz/jzitnik/chronos/services/ItemService.java b/backend/src/main/java/cz/jzitnik/chronos/services/ItemService.java index e8abe94..0f29abb 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/services/ItemService.java +++ b/backend/src/main/java/cz/jzitnik/chronos/services/ItemService.java @@ -1,5 +1,7 @@ package cz.jzitnik.chronos.services; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import cz.jzitnik.chronos.entities.Message; import cz.jzitnik.chronos.entities.MessageType; import cz.jzitnik.chronos.entities.Player; @@ -18,6 +20,23 @@ public class ItemService { private ItemRepository itemRepository; public void addItem(Player player, Item item) { + if (player.getInventory().size() >= 3) { + // Inv full + ObjectMapper objectMapper = new ObjectMapper(); + try { + String json = objectMapper.writeValueAsString(item); + + var message = new Message(player, json, MessageType.LOST_ITEM); + + player.getMessages().add(message); + playerRepository.save(player); + + return; + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + var itemSaved = itemRepository.save(item); var message = new Message(player, String.valueOf(itemSaved.getId()), MessageType.GOT_ITEM); diff --git a/frontend/src/main/java/cz/jzitnik/api/ApiService.java b/frontend/src/main/java/cz/jzitnik/api/ApiService.java index 7c329b7..ac3f633 100644 --- a/frontend/src/main/java/cz/jzitnik/api/ApiService.java +++ b/frontend/src/main/java/cz/jzitnik/api/ApiService.java @@ -4,6 +4,7 @@ import cz.jzitnik.api.requests.InteractionRequest; import cz.jzitnik.api.requests.MessageRequest; import cz.jzitnik.api.requests.PlayerNameRequest; import cz.jzitnik.api.responses.InteractionResponse; +import cz.jzitnik.api.responses.InventoryFullResponse; import cz.jzitnik.api.responses.UnifiedResponse; import cz.jzitnik.api.types.*; import retrofit2.Call; @@ -32,6 +33,11 @@ public interface ApiService { @Query("playerKey") String playerKey ); + @GET("game/players/me/inv_full") + Call> inventoryIsFull( + @Query("playerKey") String playerKey + ); + @POST("game/players/use_item") Call> useItem( @Query("playerKey") String playerKey, diff --git a/frontend/src/main/java/cz/jzitnik/api/responses/InventoryFullResponse.java b/frontend/src/main/java/cz/jzitnik/api/responses/InventoryFullResponse.java new file mode 100644 index 0000000..06392c9 --- /dev/null +++ b/frontend/src/main/java/cz/jzitnik/api/responses/InventoryFullResponse.java @@ -0,0 +1,12 @@ +package cz.jzitnik.api.responses; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@AllArgsConstructor +@NoArgsConstructor +@Getter +public class InventoryFullResponse { + private boolean full; +} diff --git a/frontend/src/main/java/cz/jzitnik/api/types/Message.java b/frontend/src/main/java/cz/jzitnik/api/types/Message.java index 983f691..decc1ea 100644 --- a/frontend/src/main/java/cz/jzitnik/api/types/Message.java +++ b/frontend/src/main/java/cz/jzitnik/api/types/Message.java @@ -1,5 +1,7 @@ package cz.jzitnik.api.types; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import cz.jzitnik.api.ApiService; import cz.jzitnik.utils.Cli; import lombok.Getter; @@ -56,6 +58,16 @@ public class Message { yield "Hráč unknown vešel do místnosti unknown"; } } + case LOST_ITEM -> { + ObjectMapper objectMapper = new ObjectMapper(); + try { + Item item = objectMapper.readValue(content, Item.class); + + 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."; + } + } }; } } diff --git a/frontend/src/main/java/cz/jzitnik/api/types/MessageType.java b/frontend/src/main/java/cz/jzitnik/api/types/MessageType.java index 50a0052..82a4f10 100644 --- a/frontend/src/main/java/cz/jzitnik/api/types/MessageType.java +++ b/frontend/src/main/java/cz/jzitnik/api/types/MessageType.java @@ -4,5 +4,6 @@ public enum MessageType { CUSTOM, MOVE_TO_ROOM, GOT_ITEM, + LOST_ITEM, JOINED, } diff --git a/frontend/src/main/java/cz/jzitnik/game/CommandPalette.java b/frontend/src/main/java/cz/jzitnik/game/CommandPalette.java index 3827a08..ed8a126 100644 --- a/frontend/src/main/java/cz/jzitnik/game/CommandPalette.java +++ b/frontend/src/main/java/cz/jzitnik/game/CommandPalette.java @@ -64,7 +64,14 @@ public class CommandPalette { allCommands.add(new Command("Odeslat zprávu do chatu", CommandType.CHAT_SEND)); allCommands.add(new Command("Odejít ze hry", CommandType.EXIT)); + var responseInvFull = apiService.inventoryIsFull(playerKey).execute(); + var inv_full = responseInvFull.body().getData().get().isFull(); + System.out.println("\n"); + + if (inv_full) { + System.out.println(Cli.Colors.RED + "Váš inventář je plný! Jakýkoliv předmět, který dostanete, bude ztracen!" + Cli.Colors.RESET); + } var selectedOption = Cli.selectOptionIndex(allCommands.stream().map(Command::toString).toList()); var command = allCommands.get(selectedOption);