chat.h

main 165 lines · 4.3 KB Raw
Alex Alex Commit Server upload 18/10/2026 23:38
1/**
2 * The Forgotten Server - a free and open-source MMORPG server emulator
3 * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#ifndef FS_CHAT_H_F1574642D0384ABFAB52B7ED906E5628
21#define FS_CHAT_H_F1574642D0384ABFAB52B7ED906E5628
22
23#include "const.h"
24#include "luascript.h"
25
26class Party;
27class Player;
28
29using UsersMap = std::map<uint32_t, Player*>;
30using InvitedMap = std::map<uint32_t, const Player*>;
31
32class ChatChannel
33{
34 public:
35 ChatChannel() = default;
36 ChatChannel(uint16_t channelId, std::string channelName):
37 id{channelId}, name{std::move(channelName)} {}
38
39 virtual ~ChatChannel() = default;
40
41 bool addUser(Player& player);
42 bool removeUser(const Player& player);
43 bool hasUser(const Player& player);
44
45 bool talk(const Player& fromPlayer, SpeakClasses type, const std::string& text);
46 void sendToAll(const std::string& message, SpeakClasses type) const;
47
48 const std::string& getName() const {
49 return name;
50 }
51 uint16_t getId() const {
52 return id;
53 }
54 const UsersMap& getUsers() const {
55 return users;
56 }
57 virtual const InvitedMap* getInvitedUsers() const {
58 return nullptr;
59 }
60
61 virtual uint32_t getOwner() const {
62 return 0;
63 }
64
65 bool isPublicChannel() const { return publicChannel; }
66
67 bool executeOnJoinEvent(const Player& player);
68 bool executeCanJoinEvent(const Player& player);
69 bool executeOnLeaveEvent(const Player& player);
70 bool executeOnSpeakEvent(const Player& player, SpeakClasses& type, const std::string& message);
71
72 protected:
73 UsersMap users;
74
75 uint16_t id;
76
77 private:
78 std::string name;
79
80 int32_t canJoinEvent = -1;
81 int32_t onJoinEvent = -1;
82 int32_t onLeaveEvent = -1;
83 int32_t onSpeakEvent = -1;
84
85 bool publicChannel = false;
86
87 friend class Chat;
88};
89
90class PrivateChatChannel final : public ChatChannel
91{
92 public:
93 PrivateChatChannel(uint16_t channelId, std::string channelName) : ChatChannel(channelId, channelName) {}
94
95 uint32_t getOwner() const override {
96 return owner;
97 }
98 void setOwner(uint32_t owner) {
99 this->owner = owner;
100 }
101
102 bool isInvited(uint32_t guid) const;
103
104 void invitePlayer(const Player& player, Player& invitePlayer);
105 void excludePlayer(const Player& player, Player& excludePlayer);
106
107 bool removeInvite(uint32_t guid);
108
109 void closeChannel() const;
110
111 const InvitedMap* getInvitedUsers() const override {
112 return &invites;
113 }
114
115 private:
116 InvitedMap invites;
117 uint32_t owner = 0;
118};
119
120using ChannelList = std::list<ChatChannel*>;
121
122class Chat
123{
124 public:
125 Chat();
126
127 // non-copyable
128 Chat(const Chat&) = delete;
129 Chat& operator=(const Chat&) = delete;
130
131 bool load();
132
133 ChatChannel* createChannel(const Player& player, uint16_t channelId);
134 bool deleteChannel(const Player& player, uint16_t channelId);
135
136 ChatChannel* addUserToChannel(Player& player, uint16_t channelId);
137 bool removeUserFromChannel(const Player& player, uint16_t channelId);
138 void removeUserFromAllChannels(const Player& player);
139
140 bool talkToChannel(const Player& player, SpeakClasses type, const std::string& text, uint16_t channelId);
141
142 ChannelList getChannelList(const Player& player);
143
144 ChatChannel* getChannel(const Player& player, uint16_t channelId);
145 ChatChannel* getChannelById(uint16_t channelId);
146 ChatChannel* getGuildChannelById(uint32_t guildId);
147 PrivateChatChannel* getPrivateChannel(const Player& player);
148
149 LuaScriptInterface* getScriptInterface() {
150 return &scriptInterface;
151 }
152
153 private:
154 std::map<uint16_t, ChatChannel> normalChannels;
155 std::map<uint16_t, PrivateChatChannel> privateChannels;
156 std::map<Party*, ChatChannel> partyChannels;
157 std::map<uint32_t, ChatChannel> guildChannels;
158
159 LuaScriptInterface scriptInterface;
160
161 PrivateChatChannel dummyPrivate;
162};
163
164#endif
165
Top