feat: Send message to chat when item is used

This commit is contained in:
jzitnik-dev 2024-12-23 18:02:41 +01:00
parent 89d9799ae2
commit 143c4761f5
Signed by: jzitnik
GPG Key ID: C577A802A6AF4EF3
5 changed files with 28 additions and 2 deletions

View File

@ -1,6 +1,9 @@
package cz.jzitnik.chronos.controllers; package cz.jzitnik.chronos.controllers;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.jzitnik.chronos.entities.Item; 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.entities.Player;
import cz.jzitnik.chronos.payload.errors.ItemNotUsableException; import cz.jzitnik.chronos.payload.errors.ItemNotUsableException;
import cz.jzitnik.chronos.payload.errors.NotFoundError; 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())); 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); playerRepository.save(player);
itemRepository.delete(item); item.setOwner(null);
itemRepository.save(item);
return ResponseEntity.ok(UnifiedResponse.success(null)); return ResponseEntity.ok(UnifiedResponse.success(null));
} }

View File

@ -5,5 +5,6 @@ public enum MessageType {
MOVE_TO_ROOM, MOVE_TO_ROOM,
GOT_ITEM, GOT_ITEM,
LOST_ITEM, LOST_ITEM,
ITEM_USED,
JOINED, JOINED,
} }

View File

@ -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."; 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.";
}
}
}; };
} }
} }

View File

@ -5,5 +5,6 @@ public enum MessageType {
MOVE_TO_ROOM, MOVE_TO_ROOM,
GOT_ITEM, GOT_ITEM,
LOST_ITEM, LOST_ITEM,
ITEM_USED,
JOINED, JOINED,
} }

View File

@ -79,7 +79,7 @@ public class CommandPalette {
return switch (command.getCommandType()) { return switch (command.getCommandType()) {
case CUSTOM -> selectedOption; case CUSTOM -> selectedOption;
case EXIT -> { case EXIT -> {
System.out.println("Exiting game..."); Cli.info("Odpojuji se ze hry...");
System.exit(0); System.exit(0);
yield 0; yield 0;