Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/StarChatProcessor.hpp
diff options
context:
space:
mode:
authorKae <80987908+Novaenia@users.noreply.github.com>2023-06-20 14:33:09 +1000
committerKae <80987908+Novaenia@users.noreply.github.com>2023-06-20 14:33:09 +1000
commit6352e8e3196f78388b6c771073f9e03eaa612673 (patch)
treee23772f79a7fbc41bc9108951e9e136857484bf4 /source/game/StarChatProcessor.hpp
parent6741a057e5639280d85d0f88ba26f000baa58f61 (diff)
everything everywhere
all at once
Diffstat (limited to 'source/game/StarChatProcessor.hpp')
-rw-r--r--source/game/StarChatProcessor.hpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/source/game/StarChatProcessor.hpp b/source/game/StarChatProcessor.hpp
new file mode 100644
index 0000000..7683c3e
--- /dev/null
+++ b/source/game/StarChatProcessor.hpp
@@ -0,0 +1,86 @@
+#ifndef STAR_CHAT_PROCESSOR_HPP
+#define STAR_CHAT_PROCESSOR_HPP
+
+#include "StarChatTypes.hpp"
+#include "StarSet.hpp"
+#include "StarThread.hpp"
+
+namespace Star {
+
+STAR_CLASS(ChatProcessor);
+
+// Handles all chat routing and command parsing for client / server chat.
+// Thread safe.
+class ChatProcessor {
+public:
+ static char const* ServerNick;
+
+ // CommandHandler is passed the origin connection, the command portion
+ // excluding the '/' character, and the remaining command line in full.
+ typedef function<String(ConnectionId, String, String)> CommandHandler;
+
+ String connectClient(ConnectionId clientId, String nick = "");
+ // Returns any pending messages.
+ List<ChatReceivedMessage> disconnectClient(ConnectionId clientId);
+
+ List<ConnectionId> clients() const;
+ bool hasClient(ConnectionId clientId) const;
+
+ // Clears all clients and channels
+ void reset();
+
+ // Will return nothing if nick is not found.
+ Maybe<ConnectionId> findNick(String const& nick) const;
+ String connectionNick(ConnectionId connectionId) const;
+ String renick(ConnectionId clientId, String const& nick);
+
+ // join / leave return true in the even that the client channel state was
+ // actually changed.
+ bool joinChannel(ConnectionId clientId, String const& channelName);
+ bool leaveChannel(ConnectionId clientId, String const& channelName);
+
+ StringList clientChannels(ConnectionId clientId) const;
+ StringList activeChannels() const;
+
+ void broadcast(ConnectionId sourceConnectionId, String const& text);
+ void message(ConnectionId sourceConnectionId, MessageContext::Mode context, String const& channelName, String const& text);
+ void whisper(ConnectionId sourceConnectionId, ConnectionId targetClientId, String const& text);
+
+ // Shorthand for passing ServerConnectionId as sourceConnectionId to
+ // broadcast / message / whisper
+ void adminBroadcast(String const& text);
+ void adminMessage(MessageContext::Mode context, String const& channelName, String const& text);
+ void adminWhisper(ConnectionId targetClientId, String const& text);
+
+ List<ChatReceivedMessage> pullPendingMessages(ConnectionId clientId);
+
+ void setCommandHandler(CommandHandler commandHandler);
+ void clearCommandHandler();
+
+private:
+ struct ClientInfo {
+ ClientInfo(ConnectionId clientId, String const& nick);
+
+ ConnectionId clientId;
+ String nick;
+ List<ChatReceivedMessage> pendingMessages;
+ };
+
+ String makeNickUnique(String nick);
+
+ // Returns true if message was handled completely and needs no further
+ // processing.
+ bool handleCommand(ChatReceivedMessage& message);
+
+ mutable RecursiveMutex m_mutex;
+
+ HashMap<ConnectionId, ClientInfo> m_clients;
+ StringMap<ConnectionId> m_nicks;
+ StringMap<Set<ConnectionId>> m_channels;
+
+ CommandHandler m_commandHandler;
+};
+
+}
+
+#endif