feat: Implemented chat
This commit is contained in:
@ -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
|
||||
);
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
61
frontend/src/main/java/cz/jzitnik/api/types/Message.java
Normal file
61
frontend/src/main/java/cz/jzitnik/api/types/Message.java
Normal 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";
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package cz.jzitnik.api.types;
|
||||
|
||||
public enum MessageType {
|
||||
CUSTOM,
|
||||
MOVE_TO_ROOM,
|
||||
GOT_ITEM,
|
||||
JOINED,
|
||||
}
|
51
frontend/src/main/java/cz/jzitnik/game/Chat.java
Normal file
51
frontend/src/main/java/cz/jzitnik/game/Chat.java
Normal 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!");
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
};
|
||||
|
@ -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);
|
||||
|
||||
|
Reference in New Issue
Block a user