feat: Implemented chat

This commit is contained in:
jzitnik-dev 2024-12-23 14:42:01 +01:00
parent ce1710cfef
commit 4bb9a31ffa
Signed by: jzitnik
GPG Key ID: C577A802A6AF4EF3
18 changed files with 394 additions and 8 deletions

View File

@ -9,6 +9,7 @@ import cz.jzitnik.chronos.payload.responses.TakeItemsResponse;
import cz.jzitnik.chronos.payload.responses.UnifiedResponse;
import cz.jzitnik.chronos.repository.CharacterRepository;
import cz.jzitnik.chronos.repository.PlayerRepository;
import cz.jzitnik.chronos.services.ItemService;
import cz.jzitnik.chronos.utils.anotations.CheckUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@ -28,6 +29,9 @@ public class CharacterController {
@Autowired
private CharacterRepository characterRepository;
@Autowired
private ItemService itemService;
@GetMapping("/interaction")
@CheckUser
public ResponseEntity<UnifiedResponse<Interaction, Error>> getInteractionData(@RequestParam String playerKey, @RequestParam Long characterId) {
@ -99,7 +103,7 @@ public class CharacterController {
for (Item item : character.getInventory()) {
var itemClone = new Item(item.getItemType(), player);
player.addItem(itemClone);
itemService.addItem(player, itemClone);
}
player.getSeenCharacters().add(character);

View File

@ -0,0 +1,58 @@
package cz.jzitnik.chronos.controllers;
import cz.jzitnik.chronos.entities.Message;
import cz.jzitnik.chronos.payload.requests.MessageRequest;
import cz.jzitnik.chronos.payload.responses.UnifiedResponse;
import cz.jzitnik.chronos.repository.MessageRepository;
import cz.jzitnik.chronos.repository.PlayerRepository;
import cz.jzitnik.chronos.utils.anotations.CheckUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/game/chat")
public class ChatController {
@Autowired
private MessageRepository messageRepository;
@Autowired
private PlayerRepository playerRepository;
@GetMapping
@CheckUser
public ResponseEntity<UnifiedResponse<List<Message>, Error>> getMessages(@RequestParam String playerKey) {
var player = playerRepository.findByPlayerKey(playerKey).get();
var gameId = player.getGame().getId();
Pageable pageable = PageRequest.of(0, 20);
var messages = messageRepository.findLastMessagesByGameId(gameId, pageable);
return ResponseEntity.ok(UnifiedResponse.success(messages));
}
@PostMapping
@CheckUser
public ResponseEntity<UnifiedResponse<Object, Error>> sendMessage(@RequestParam String playerKey, @RequestBody MessageRequest messageRequest) {
var player = playerRepository.findByPlayerKey(playerKey).get();
if (messageRequest.getContent().length() > 100 || messageRequest.getContent().isEmpty()) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(
UnifiedResponse.failure(new Error("Message cannot be longer than 100 characters and cannot be empty!"))
);
}
var message = new Message(player, messageRequest.getContent());
messageRepository.save(message);
return ResponseEntity.ok(UnifiedResponse.success(null));
}
}

View File

@ -1,5 +1,6 @@
package cz.jzitnik.chronos.controllers;
import cz.jzitnik.chronos.entities.Item;
import cz.jzitnik.chronos.entities.Player;
import cz.jzitnik.chronos.payload.errors.ItemNotUsableException;
import cz.jzitnik.chronos.payload.errors.NotFoundError;
@ -87,4 +88,17 @@ public class PlayerController {
return ResponseEntity.ok(UnifiedResponse.success(null));
}
@GetMapping("/item")
@CheckUser
public ResponseEntity<UnifiedResponse<Item, Error>> getItemInfo(@RequestParam String playerKey, @RequestParam Long itemId) {
var itemOptional = itemRepository.findById(itemId);
if (itemOptional.isEmpty()) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(UnifiedResponse.failure(new NotFoundError("Item was not found!")));
}
var item = itemOptional.get();
return ResponseEntity.ok(UnifiedResponse.success(item));
}
}

View File

@ -1,6 +1,9 @@
package cz.jzitnik.chronos.controllers;
import cz.jzitnik.chronos.entities.Message;
import cz.jzitnik.chronos.entities.MessageType;
import cz.jzitnik.chronos.entities.Room;
import cz.jzitnik.chronos.payload.errors.NotFoundError;
import cz.jzitnik.chronos.payload.responses.UnifiedResponse;
import cz.jzitnik.chronos.repository.PlayerRepository;
import cz.jzitnik.chronos.repository.RoomRepository;
@ -35,6 +38,30 @@ public class RoomController {
);
}
@GetMapping("/room")
@CheckUser
public ResponseEntity<UnifiedResponse<Room, Error>> getRoom(@RequestParam("playerKey") String playerKey, @RequestParam("roomId") Long roomId) {
var player = playerRepository.findByPlayerKey(playerKey).get();
var roomOptional = roomRepository.findById(roomId);
if (roomOptional.isEmpty()) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(
UnifiedResponse.failure(new NotFoundError("Room with id " + roomId + " was not found!"))
);
}
var room = roomOptional.get();
if (!room.getGame().getId().equals(player.getGame().getId())) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(
UnifiedResponse.failure(new Error("This room is from a different game!"))
);
}
return ResponseEntity.ok(UnifiedResponse.success(room));
}
@GetMapping("/current_room")
@CheckUser
public ResponseEntity<UnifiedResponse<Room, Error>> getCurrentRoom(@RequestParam("playerKey") String playerKey) {
@ -64,6 +91,11 @@ public class RoomController {
player.setCurrentRoom(roomOptional.get());
// Send message to chat
var message = new Message(player, String.valueOf(roomId), MessageType.MOVE_TO_ROOM);
player.getMessages().add(message);
playerRepository.save(player);
return ResponseEntity.ok(UnifiedResponse.success(null));

View File

@ -0,0 +1,36 @@
package cz.jzitnik.chronos.entities;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@Entity
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private Player author;
private String content;
@Enumerated(EnumType.STRING)
private MessageType messageType;
public Message(Player author, String content) {
this.author = author;
this.content = content;
this.messageType = MessageType.CUSTOM;
}
public Message(Player author, String content, MessageType messageType) {
this.author = author;
this.content = content;
this.messageType = messageType;
}
}

View File

@ -0,0 +1,8 @@
package cz.jzitnik.chronos.entities;
public enum MessageType {
CUSTOM,
MOVE_TO_ROOM,
GOT_ITEM,
JOINED,
}

View File

@ -42,6 +42,10 @@ public class Player {
private boolean luck = false;
@OneToMany(mappedBy = "author", cascade = CascadeType.ALL)
@JsonIgnore
private List<Message> messages = new ArrayList<>();
@ManyToMany
@JoinTable(
name = "character_player",

View File

@ -7,11 +7,11 @@ import cz.jzitnik.chronos.entities.Character;
import cz.jzitnik.chronos.payload.responses.InteractionResponse;
import cz.jzitnik.chronos.repository.CharacterRepository;
import cz.jzitnik.chronos.repository.PlayerRepository;
import cz.jzitnik.chronos.services.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Optional;
import java.util.Random;
@Service
@ -22,6 +22,9 @@ public class InteractionService {
@Autowired
private CharacterRepository characterRepository;
@Autowired
private ItemService itemService;
@FunctionalInterface
public interface Function3<T, U, V, W> {
W apply(T t, U u, V v);
@ -51,8 +54,7 @@ public class InteractionService {
if (playerWins) {
var item = new Item(ItemType.KEY_FRAGMENT, player);
player.addItem(item);
playerRepository.save(player);
itemService.addItem(player, item);
character.setInteractedWith(true);
characterRepository.save(character);
@ -72,8 +74,7 @@ public class InteractionService {
return new InteractionResponse(false, "Remíza. Jestli chceš můžeš si se mnou zahrát ještě jednou.", new ArrayList<>());
} else if ((userChoice == 0 && characterChoice == 2) || (userChoice == 1 && characterChoice == 0) || (userChoice == 2 && characterChoice == 1)) {
var item = new Item(ItemType.KEY_FRAGMENT, player);
player.addItem(item);
playerRepository.save(player);
itemService.addItem(player, item);
character.setInteractedWith(true);
characterRepository.save(character);

View File

@ -0,0 +1,10 @@
package cz.jzitnik.chronos.payload.requests;
import lombok.Getter;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@Getter
public class MessageRequest {
private String content;
}

View File

@ -0,0 +1,19 @@
package cz.jzitnik.chronos.repository;
import cz.jzitnik.chronos.entities.Message;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface MessageRepository extends JpaRepository<Message, Long> {
@Query("""
SELECT m FROM Message m
WHERE m.author.game.id = :gameId
ORDER BY m.id DESC
""")
List<Message> findLastMessagesByGameId(@Param("gameId") Long gameId, Pageable pageable);
}

View File

@ -0,0 +1,28 @@
package cz.jzitnik.chronos.services;
import cz.jzitnik.chronos.entities.Message;
import cz.jzitnik.chronos.entities.MessageType;
import cz.jzitnik.chronos.entities.Player;
import cz.jzitnik.chronos.entities.Item;
import cz.jzitnik.chronos.repository.ItemRepository;
import cz.jzitnik.chronos.repository.PlayerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ItemService {
@Autowired
private PlayerRepository playerRepository;
@Autowired
private ItemRepository itemRepository;
public void addItem(Player player, Item item) {
var itemSaved = itemRepository.save(item);
var message = new Message(player, String.valueOf(itemSaved.getId()), MessageType.GOT_ITEM);
player.getMessages().add(message);
playerRepository.save(player);
}
}

View File

@ -1,6 +1,7 @@
package cz.jzitnik.api;
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.UnifiedResponse;
@ -81,4 +82,27 @@ public interface ApiService {
@Query("playerKey") String playerKey,
@Body InteractionRequest interactionRequest
);
@GET("game/chat")
Call<UnifiedResponse<List<Message>, Error>> getMessages(
@Query("playerKey") String playerKey
);
@POST("game/chat")
Call<UnifiedResponse<Object, Error>> sendMessage(
@Query("playerKey") String playerKey,
@Body MessageRequest messageRequest
);
@GET("game/players/item")
Call<UnifiedResponse<Item, Error>> getItemInfo(
@Query("playerKey") String playerKey,
@Query("itemId") Long itemId
);
@GET("game/rooms/room")
Call<UnifiedResponse<Room, Error>> getRoom(
@Query("playerKey") String playerKey,
@Query("roomId") Long roomId
);
}

View File

@ -0,0 +1,12 @@
package cz.jzitnik.api.requests;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Getter
public class MessageRequest {
private String content;
}

View File

@ -0,0 +1,61 @@
package cz.jzitnik.api.types;
import cz.jzitnik.api.ApiService;
import cz.jzitnik.utils.Cli;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.io.IOException;
@Getter
@Setter
@NoArgsConstructor
public class Message {
private Long id;
private Player author;
private String content;
private MessageType messageType;
public String getString(ApiService apiService, String playerKey) {
return switch (messageType) {
case CUSTOM -> Cli.Colors.BLUE + author.getName() + Cli.Colors.RESET + ": " + content;
case JOINED -> "Hráč " + Cli.Colors.BLUE + author.getName() + Cli.Colors.RESET + " se připojil.";
case GOT_ITEM -> {
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 + " získal unknown.";
}
var item = body.getData().get();
yield "Hráč " + Cli.Colors.BLUE + author.getName() + Cli.Colors.RESET + " získal " + Cli.Colors.BLUE + item + Cli.Colors.RESET + ".";
} catch (IOException e) {
yield "Hráč unknown získal unknown";
}
}
case MOVE_TO_ROOM -> {
try {
var response = apiService.getRoom(playerKey, Long.valueOf(content)).execute();
var body = response.body();
if (!body.getSuccess()) {
yield "Hráč " + Cli.Colors.BLUE + author.getName() + Cli.Colors.RESET + " vešel do místnosti unknown.";
}
var room = body.getData().get();
yield "Hráč " + Cli.Colors.BLUE + author.getName() + Cli.Colors.RESET + " vešel do místnosti " + Cli.Colors.BLUE + room + Cli.Colors.RESET + ".";
} catch (IOException e) {
yield "Hráč unknown vešel do místnosti unknown";
}
}
};
}
}

View File

@ -0,0 +1,8 @@
package cz.jzitnik.api.types;
public enum MessageType {
CUSTOM,
MOVE_TO_ROOM,
GOT_ITEM,
JOINED,
}

View File

@ -0,0 +1,51 @@
package cz.jzitnik.game;
import cz.jzitnik.api.ApiService;
import cz.jzitnik.api.requests.MessageRequest;
import cz.jzitnik.api.types.Message;
import cz.jzitnik.utils.Cli;
import lombok.AllArgsConstructor;
import java.io.IOException;
import java.util.Collections;
@AllArgsConstructor
public class Chat {
private ApiService apiService;
private String playerKey;
public void display() throws IOException {
System.out.println();
var response = apiService.getMessages(playerKey).execute();
var messages = response.body().getData().get();
Collections.reverse(messages);
for (Message message : messages) {
System.out.println(message.getString(apiService, playerKey));
}
}
public void sendMessage() throws IOException {
System.out.println();
var console = System.console();
var messageContent = console.readLine("Zadejte zprávu: ").trim();
if (messageContent.isEmpty()) {
Cli.error("Zpráva nesmí být prázná!");
return;
}
if (messageContent.length() > 100) {
Cli.error("Zpráva nesmí být delší jak 100 znaků!");
return;
}
var messageRequest = new MessageRequest(messageContent);
apiService.sendMessage(playerKey, messageRequest).execute();
Cli.success("Zpráva byla odeslána!");
}
}

View File

@ -17,6 +17,8 @@ public class CommandPalette {
CUSTOM,
INVENTORY_OPEN,
USER_PROFILE,
CHAT_OPEN,
CHAT_SEND,
EXIT,
}
@ -55,9 +57,11 @@ public class CommandPalette {
public int displayIndex() throws IOException {
List<Command> allCommands = new ArrayList<>(commands);
// Command that can be used anywhere
// Commands that can be used anywhere
allCommands.add(new Command("Otevřít inventář", CommandType.INVENTORY_OPEN));
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("Odejít ze hry", CommandType.EXIT));
System.out.println("\n");
@ -92,6 +96,18 @@ public class CommandPalette {
System.out.println("\n\n" + me);
yield displayIndex();
}
case CHAT_OPEN -> {
var chat = new Chat(apiService, playerKey);
chat.display();
yield displayIndex();
}
case CHAT_SEND -> {
var chat = new Chat(apiService, playerKey);
chat.sendMessage();
yield displayIndex();
}
};

View File

@ -51,7 +51,7 @@ public class Inventory {
if (!usableItemList.isEmpty()) {
System.out.println("\n\nNyní si můžete vybrat jaký item chcete využít");
var options = new ArrayList<>(usableItemList.stream().map(Item::toString).toList());
options.add("Zavřít inventář");
options.add(Cli.Colors.BLUE + "Zavřít inventář" + Cli.Colors.RESET);
var selectedIndex = Cli.selectOptionIndex(options);