feat: Implemented full inv warning

This commit is contained in:
jzitnik-dev 2024-12-23 16:24:11 +01:00
parent 4bb9a31ffa
commit 89d9799ae2
Signed by: jzitnik
GPG Key ID: C577A802A6AF4EF3
10 changed files with 81 additions and 3 deletions

View File

@ -5,6 +5,7 @@ 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;
import cz.jzitnik.chronos.payload.requests.PlayerNameRequest; 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.payload.responses.UnifiedResponse;
import cz.jzitnik.chronos.repository.GameRepository; import cz.jzitnik.chronos.repository.GameRepository;
import cz.jzitnik.chronos.repository.ItemRepository; import cz.jzitnik.chronos.repository.ItemRepository;
@ -59,6 +60,14 @@ public class PlayerController {
return ResponseEntity.ok(UnifiedResponse.success(player)); return ResponseEntity.ok(UnifiedResponse.success(player));
} }
@GetMapping("/me/inv_full")
@CheckUser
public ResponseEntity<UnifiedResponse<InventoryFullResponse, Error>> inventoryIsFull(@RequestParam String playerKey) {
var player = playerRepository.findByPlayerKey(playerKey).get();
return ResponseEntity.ok(UnifiedResponse.success(new InventoryFullResponse(player.getInventory().size() >= 3)));
}
@PostMapping("/use_item") @PostMapping("/use_item")
@CheckUser @CheckUser
public ResponseEntity<UnifiedResponse<Object, Error>> useItem(@RequestParam String playerKey, @RequestParam Long itemId) { public ResponseEntity<UnifiedResponse<Object, Error>> useItem(@RequestParam String playerKey, @RequestParam Long itemId) {

View File

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

View File

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

View File

@ -1,8 +1,6 @@
package cz.jzitnik.chronos.services; package cz.jzitnik.chronos.services;
import cz.jzitnik.chronos.entities.Game; import cz.jzitnik.chronos.entities.*;
import cz.jzitnik.chronos.entities.Player;
import cz.jzitnik.chronos.entities.Room;
import cz.jzitnik.chronos.repository.GameRepository; import cz.jzitnik.chronos.repository.GameRepository;
import cz.jzitnik.chronos.repository.PlayerRepository; import cz.jzitnik.chronos.repository.PlayerRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -72,6 +70,7 @@ public class GameService {
player.setPlayerKey(generateUniquePlayerKey()); player.setPlayerKey(generateUniquePlayerKey());
player.setGame(game); player.setGame(game);
player.setCurrentRoom(game.getDefaultRoom()); player.setCurrentRoom(game.getDefaultRoom());
player.getMessages().add(new Message(player, "", MessageType.JOINED));
if (game.getPlayers().isEmpty()) { if (game.getPlayers().isEmpty()) {
game.setAdminPlayer(player); game.setAdminPlayer(player);

View File

@ -1,5 +1,7 @@
package cz.jzitnik.chronos.services; 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.Message;
import cz.jzitnik.chronos.entities.MessageType; import cz.jzitnik.chronos.entities.MessageType;
import cz.jzitnik.chronos.entities.Player; import cz.jzitnik.chronos.entities.Player;
@ -18,6 +20,23 @@ public class ItemService {
private ItemRepository itemRepository; private ItemRepository itemRepository;
public void addItem(Player player, Item item) { 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 itemSaved = itemRepository.save(item);
var message = new Message(player, String.valueOf(itemSaved.getId()), MessageType.GOT_ITEM); var message = new Message(player, String.valueOf(itemSaved.getId()), MessageType.GOT_ITEM);

View File

@ -4,6 +4,7 @@ import cz.jzitnik.api.requests.InteractionRequest;
import cz.jzitnik.api.requests.MessageRequest; import cz.jzitnik.api.requests.MessageRequest;
import cz.jzitnik.api.requests.PlayerNameRequest; import cz.jzitnik.api.requests.PlayerNameRequest;
import cz.jzitnik.api.responses.InteractionResponse; import cz.jzitnik.api.responses.InteractionResponse;
import cz.jzitnik.api.responses.InventoryFullResponse;
import cz.jzitnik.api.responses.UnifiedResponse; import cz.jzitnik.api.responses.UnifiedResponse;
import cz.jzitnik.api.types.*; import cz.jzitnik.api.types.*;
import retrofit2.Call; import retrofit2.Call;
@ -32,6 +33,11 @@ public interface ApiService {
@Query("playerKey") String playerKey @Query("playerKey") String playerKey
); );
@GET("game/players/me/inv_full")
Call<UnifiedResponse<InventoryFullResponse, Error>> inventoryIsFull(
@Query("playerKey") String playerKey
);
@POST("game/players/use_item") @POST("game/players/use_item")
Call<UnifiedResponse<Object, Error>> useItem( Call<UnifiedResponse<Object, Error>> useItem(
@Query("playerKey") String playerKey, @Query("playerKey") String playerKey,

View File

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

View File

@ -1,5 +1,7 @@
package cz.jzitnik.api.types; 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.api.ApiService;
import cz.jzitnik.utils.Cli; import cz.jzitnik.utils.Cli;
import lombok.Getter; import lombok.Getter;
@ -56,6 +58,16 @@ public class Message {
yield "Hráč unknown vešel do místnosti unknown"; 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.";
}
}
}; };
} }
} }

View File

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

View File

@ -64,7 +64,14 @@ public class CommandPalette {
allCommands.add(new Command("Odeslat zprávu do chatu", CommandType.CHAT_SEND)); allCommands.add(new Command("Odeslat zprávu do chatu", CommandType.CHAT_SEND));
allCommands.add(new Command("Odejít ze hry", CommandType.EXIT)); 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"); 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 selectedOption = Cli.selectOptionIndex(allCommands.stream().map(Command::toString).toList());
var command = allCommands.get(selectedOption); var command = allCommands.get(selectedOption);