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 9e36c06..49846af 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/controllers/PlayerController.java +++ b/backend/src/main/java/cz/jzitnik/chronos/controllers/PlayerController.java @@ -1,6 +1,9 @@ package cz.jzitnik.chronos.controllers; +import com.fasterxml.jackson.databind.ObjectMapper; import cz.jzitnik.chronos.entities.Item; +import cz.jzitnik.chronos.entities.Message; +import cz.jzitnik.chronos.entities.MessageType; import cz.jzitnik.chronos.entities.Player; import cz.jzitnik.chronos.payload.errors.ItemNotUsableException; import cz.jzitnik.chronos.payload.errors.NotFoundError; @@ -91,9 +94,13 @@ public class PlayerController { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(UnifiedResponse.failure(e.toError())); } + player.getMessages().add(new Message(player, String.valueOf(item.getId()), MessageType.ITEM_USED)); + playerRepository.save(player); - itemRepository.delete(item); + item.setOwner(null); + + itemRepository.save(item); return ResponseEntity.ok(UnifiedResponse.success(null)); } 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 9abef6c..026422a 100644 --- a/backend/src/main/java/cz/jzitnik/chronos/entities/MessageType.java +++ b/backend/src/main/java/cz/jzitnik/chronos/entities/MessageType.java @@ -5,5 +5,6 @@ public enum MessageType { MOVE_TO_ROOM, GOT_ITEM, LOST_ITEM, + ITEM_USED, JOINED, } 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 decc1ea..cb62268 100644 --- a/frontend/src/main/java/cz/jzitnik/api/types/Message.java +++ b/frontend/src/main/java/cz/jzitnik/api/types/Message.java @@ -68,6 +68,23 @@ public class Message { yield "Hráč " + author.getName() + " sice získal item, ale měl plný inventář a tím pádem ho navždy ztratil."; } } + case ITEM_USED -> { + try { + var response = apiService.getItemInfo(playerKey, Long.valueOf(content)).execute(); + + var body = response.body(); + + if (!body.getSuccess()) { + yield "Hráč " + Cli.Colors.BLUE + author.getName() + Cli.Colors.RESET + " využil item unknown."; + } + + var item = body.getData().get(); + + yield "Hráč " + Cli.Colors.BLUE + author.getName() + Cli.Colors.RESET + " využil item " + Cli.Colors.BLUE + item + Cli.Colors.RESET + "."; + } catch (IOException e) { + yield "Hráč " + Cli.Colors.BLUE + author.getName() + Cli.Colors.RESET + " využil item unknown."; + } + } }; } } 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 82a4f10..5b06336 100644 --- a/frontend/src/main/java/cz/jzitnik/api/types/MessageType.java +++ b/frontend/src/main/java/cz/jzitnik/api/types/MessageType.java @@ -5,5 +5,6 @@ public enum MessageType { MOVE_TO_ROOM, GOT_ITEM, LOST_ITEM, + ITEM_USED, JOINED, } diff --git a/frontend/src/main/java/cz/jzitnik/game/CommandPalette.java b/frontend/src/main/java/cz/jzitnik/game/CommandPalette.java index ed8a126..32c8bec 100644 --- a/frontend/src/main/java/cz/jzitnik/game/CommandPalette.java +++ b/frontend/src/main/java/cz/jzitnik/game/CommandPalette.java @@ -79,7 +79,7 @@ public class CommandPalette { return switch (command.getCommandType()) { case CUSTOM -> selectedOption; case EXIT -> { - System.out.println("Exiting game..."); + Cli.info("Odpojuji se ze hry..."); System.exit(0); yield 0;