Improved chat organization and color codes for team and private chat.

This commit is contained in:
Andrew Lalis 2021-06-29 22:08:00 +02:00
parent 93e623d5d4
commit 10eed8e8cd
9 changed files with 46 additions and 25 deletions

View File

@ -1,6 +1,7 @@
package nl.andrewlalis.aos_client;
import nl.andrewlalis.aos_core.net.chat.ChatMessage;
import nl.andrewlalis.aos_core.net.chat.ChatType;
import nl.andrewlalis.aos_core.net.chat.PlayerChatMessage;
import nl.andrewlalis.aos_core.net.data.Sound;
import nl.andrewlalis.aos_core.net.data.SoundType;
@ -8,6 +9,15 @@ import nl.andrewlalis.aos_core.net.data.SoundType;
import java.util.LinkedList;
import java.util.List;
/**
* This chat manager is responsible for storing the list of recent messages that
* the client has received, so that they can be displayed in the user interface.
* <p>
* It is also responsible for managing the client's "chatting" state (if the
* client is chatting or not), and uses a provided {@link MessageTransceiver}
* to send chat messages to the server.
* </p>
*/
public class ChatManager {
public static final int MAX_CHAT_MESSAGES = 10;
@ -34,7 +44,7 @@ public class ChatManager {
public synchronized void addChatMessage(ChatMessage message) {
this.chatMessages.add(message);
if (message.getClass() == PlayerChatMessage.class) {
if (message instanceof PlayerChatMessage) {
this.soundManager.play(new Sound(null, 1.0f, SoundType.CHAT));
}
while (this.chatMessages.size() > MAX_CHAT_MESSAGES) {
@ -43,6 +53,7 @@ public class ChatManager {
}
public ChatMessage[] getLatestChatMessages() {
if (this.chatMessages.isEmpty()) return new ChatMessage[0];
return this.chatMessages.toArray(new ChatMessage[0]);
}
@ -70,7 +81,7 @@ public class ChatManager {
public void sendChat() {
String message = this.chatBuffer.toString().trim();
if (!message.isBlank() && !message.equals("/") && this.messageTransceiver != null) {
this.messageTransceiver.send(new ChatMessage(message));
this.messageTransceiver.send(new ChatMessage(message, ChatType.PUBLIC_PLAYER_CHAT));
}
this.setChatting(false);
}

View File

@ -1,10 +1,11 @@
package nl.andrewlalis.aos_client.view;
import nl.andrewlalis.aos_client.ChatManager;
import nl.andrewlalis.aos_client.Client;
import nl.andrewlalis.aos_core.model.*;
import nl.andrewlalis.aos_core.model.tools.Gun;
import nl.andrewlalis.aos_core.net.chat.ChatMessage;
import nl.andrewlalis.aos_core.net.chat.PlayerChatMessage;
import nl.andrewlalis.aos_core.net.chat.ChatType;
import nl.andrewlalis.aos_core.net.chat.SystemChatMessage;
import javax.swing.*;
@ -51,7 +52,7 @@ public class GamePanel extends JPanel {
drawWorld(g2, world);
drawStatus(g2, world);
}
drawChat(g2, world);
drawChat(g2);
}
private void drawWorld(Graphics2D g2, World world) {
@ -186,7 +187,7 @@ public class GamePanel extends JPanel {
}
}
private void drawChat(Graphics2D g2, World world) {
private void drawChat(Graphics2D g2) {
int height = g2.getFontMetrics().getHeight();
int y = height;
var cm = this.client.getChatManager();
@ -201,13 +202,12 @@ public class GamePanel extends JPanel {
} else if (sysMsg.getLevel() == SystemChatMessage.Level.SEVERE) {
color = Color.RED;
}
} else if (message instanceof PlayerChatMessage pcm) {
String author = Integer.toString(pcm.getPlayerId());
if (world != null) {
Player p = world.getPlayers().get(pcm.getPlayerId());
if (p != null) author = p.getName();
} else {
if (message.getChatType() == ChatType.TEAM_PLAYER_CHAT) {
color = Color.GREEN;
} else if (message.getChatType() == ChatType.PRIVATE_PLAYER_CHAT) {
color = Color.CYAN;
}
text = author + ": " + text;
}
g2.setColor(color);
g2.drawString(text, 5, y);
@ -216,7 +216,7 @@ public class GamePanel extends JPanel {
if (cm.isChatting()) {
g2.setColor(Color.WHITE);
g2.drawString("> " + cm.getCurrentChatBuffer(), 5, height * 11);
g2.drawString("> " + cm.getCurrentChatBuffer(), 5, height * (ChatManager.MAX_CHAT_MESSAGES + 1));
}
}

View File

@ -5,13 +5,19 @@ import nl.andrewlalis.aos_core.net.Type;
public class ChatMessage extends Message {
private final String text;
private final ChatType chatType;
public ChatMessage(String text) {
public ChatMessage(String text, ChatType chatType) {
super(Type.CHAT);
this.text = text;
this.chatType = chatType;
}
public String getText() {
return text;
}
public ChatType getChatType() {
return chatType;
}
}

View File

@ -0,0 +1,8 @@
package nl.andrewlalis.aos_core.net.chat;
public enum ChatType {
PUBLIC_PLAYER_CHAT,
TEAM_PLAYER_CHAT,
PRIVATE_PLAYER_CHAT,
SYSTEM_MESSAGE
}

View File

@ -1,7 +0,0 @@
package nl.andrewlalis.aos_core.net.chat;
public class CommandMessage extends PlayerChatMessage {
public CommandMessage(int id, String text) {
super(id, text);
}
}

View File

@ -3,8 +3,8 @@ package nl.andrewlalis.aos_core.net.chat;
public class PlayerChatMessage extends ChatMessage {
private final int playerId;
public PlayerChatMessage(int id, String text) {
super(text);
public PlayerChatMessage(int id, String text, ChatType chatType) {
super(text, chatType);
this.playerId = id;
}

View File

@ -6,7 +6,7 @@ public class SystemChatMessage extends ChatMessage {
private final Level level;
public SystemChatMessage(Level level, String text) {
super(text);
super(text, ChatType.SYSTEM_MESSAGE);
this.level = level;
}

View File

@ -2,6 +2,7 @@ package nl.andrewlalis.aos_server;
import nl.andrewlalis.aos_core.model.Player;
import nl.andrewlalis.aos_core.net.chat.ChatMessage;
import nl.andrewlalis.aos_core.net.chat.ChatType;
import nl.andrewlalis.aos_core.net.chat.PlayerChatMessage;
import nl.andrewlalis.aos_server.command.GunsCommand;
import nl.andrewlalis.aos_server.command.ResetCommand;
@ -44,7 +45,7 @@ public class ChatManager {
cmd.execute(handler, player, Arrays.copyOfRange(words, 1, words.length));
}
} else {
this.server.broadcastMessage(new PlayerChatMessage(player.getId(), msg.getText()));
this.server.broadcastMessage(new PlayerChatMessage(player.getId(), player.getName() + ": "+ msg.getText(), ChatType.PUBLIC_PLAYER_CHAT));
}
}
}

View File

@ -1,6 +1,7 @@
package nl.andrewlalis.aos_server.command.chat;
import nl.andrewlalis.aos_core.model.Player;
import nl.andrewlalis.aos_core.net.chat.ChatType;
import nl.andrewlalis.aos_core.net.chat.PlayerChatMessage;
import nl.andrewlalis.aos_core.net.chat.SystemChatMessage;
import nl.andrewlalis.aos_server.ClientHandler;
@ -15,6 +16,7 @@ public class TeamChatCommand implements ChatCommand {
handler.send(new SystemChatMessage(SystemChatMessage.Level.WARNING, "You're not in a team, so you can't send team chat messages."));
return;
}
handler.getServer().sendTeamMessage(player.getTeam(), new PlayerChatMessage(player.getId(), String.join(" ", args)));
var msg = new PlayerChatMessage(player.getId(), player.getName() + ": " + String.join(" ", args), ChatType.TEAM_PLAYER_CHAT);
handler.getServer().sendTeamMessage(player.getTeam(), msg);
}
}