mirror of
https://github.com/vcmi/vcmi.git
synced 2025-01-12 02:28:11 +02:00
Channels support
This commit is contained in:
parent
ef853ab96d
commit
b0105e8a3a
@ -15,7 +15,7 @@ Chat::Chat(QWidget *parent) :
|
||||
for(auto i : {GLOBAL, ROOM})
|
||||
chatDocuments.push_back(new QTextDocument(this));
|
||||
|
||||
ui->chat->setDocument(chatDocuments[GLOBAL]);
|
||||
setChatId(GLOBAL);
|
||||
}
|
||||
|
||||
Chat::~Chat()
|
||||
@ -31,10 +31,15 @@ void Chat::setUsername(const QString & user)
|
||||
void Chat::setSession(const QString & s)
|
||||
{
|
||||
session = s;
|
||||
if(session.isEmpty())
|
||||
setChatId(GLOBAL);
|
||||
else
|
||||
setChatId(ROOM);
|
||||
|
||||
on_chatSwitch_clicked();
|
||||
}
|
||||
|
||||
void Chat::setChannel(const QString & channel)
|
||||
{
|
||||
static const QMap<QString, ChatId> chatNames{{"global", GLOBAL}, {"room", ROOM}};
|
||||
|
||||
setChatId(chatNames.value(channel));
|
||||
}
|
||||
|
||||
void Chat::addUser(const QString & user)
|
||||
@ -47,7 +52,7 @@ void Chat::clearUsers()
|
||||
ui->listUsers->clear();
|
||||
}
|
||||
|
||||
void Chat::chatMessage(const QString & title, QString body, bool isSystem)
|
||||
void Chat::chatMessage(const QString & title, const QString & channel, QString body, bool isSystem)
|
||||
{
|
||||
const QTextCharFormat regularFormat;
|
||||
const QString boldHtml = "<b>%1</b>";
|
||||
@ -55,7 +60,12 @@ void Chat::chatMessage(const QString & title, QString body, bool isSystem)
|
||||
bool meMentioned = false;
|
||||
bool isScrollBarBottom = (ui->chat->verticalScrollBar()->maximum() - ui->chat->verticalScrollBar()->value() < 24);
|
||||
|
||||
QTextCursor curs(ui->chat->document());
|
||||
static const QMap<QString, ChatId> chatNames{{"global", GLOBAL}, {"room", ROOM}};
|
||||
QTextDocument * doc = ui->chat->document();
|
||||
if(chatNames.contains(channel))
|
||||
doc = chatDocuments[chatNames.value(channel)];
|
||||
|
||||
QTextCursor curs(doc);
|
||||
curs.movePosition(QTextCursor::End);
|
||||
|
||||
QString titleColor = "Olive";
|
||||
@ -99,36 +109,48 @@ void Chat::chatMessage(const QString & title, QString body, bool isSystem)
|
||||
}
|
||||
curs.insertText("\n", regularFormat);
|
||||
|
||||
if(meMentioned || isScrollBarBottom)
|
||||
if(doc == ui->chat->document() && (meMentioned || isScrollBarBottom))
|
||||
{
|
||||
ui->chat->ensureCursorVisible();
|
||||
ui->chat->verticalScrollBar()->setValue(ui->chat->verticalScrollBar()->maximum());
|
||||
}
|
||||
}
|
||||
|
||||
void Chat::chatMessage(const QString & title, QString body, bool isSystem)
|
||||
{
|
||||
chatMessage(title, "", body, isSystem);
|
||||
}
|
||||
|
||||
void Chat::sysMessage(QString body)
|
||||
{
|
||||
chatMessage("System", body, true);
|
||||
}
|
||||
|
||||
void Chat::sendMessage()
|
||||
{
|
||||
QString msg(ui->messageEdit->text());
|
||||
ui->messageEdit->clear();
|
||||
emit messageSent(msg);
|
||||
}
|
||||
|
||||
void Chat::on_messageEdit_returnPressed()
|
||||
{
|
||||
emit messageSent(ui->messageEdit->text());
|
||||
ui->messageEdit->clear();
|
||||
sendMessage();
|
||||
}
|
||||
|
||||
void Chat::on_sendButton_clicked()
|
||||
{
|
||||
emit messageSent(ui->messageEdit->text());
|
||||
ui->messageEdit->clear();
|
||||
sendMessage();
|
||||
}
|
||||
|
||||
void Chat::on_chatSwitch_clicked()
|
||||
{
|
||||
static const QMap<ChatId, QString> chatNames{{GLOBAL, "global"}, {ROOM, "room"}};
|
||||
|
||||
if(chatId == GLOBAL && !session.isEmpty())
|
||||
setChatId(ROOM);
|
||||
emit channelSwitch(chatNames[ROOM]);
|
||||
else
|
||||
setChatId(GLOBAL);
|
||||
emit channelSwitch(chatNames[GLOBAL]);
|
||||
}
|
||||
|
||||
void Chat::setChatId(ChatId _chatId)
|
||||
|
@ -25,6 +25,7 @@ class Chat : public QWidget
|
||||
|
||||
private:
|
||||
void setChatId(ChatId);
|
||||
void sendMessage();
|
||||
|
||||
public:
|
||||
explicit Chat(QWidget *parent = nullptr);
|
||||
@ -32,14 +33,17 @@ public:
|
||||
|
||||
void setUsername(const QString &);
|
||||
void setSession(const QString &);
|
||||
void setChannel(const QString &);
|
||||
|
||||
void clearUsers();
|
||||
void addUser(const QString & user);
|
||||
|
||||
void chatMessage(const QString & title, const QString & channel, QString body, bool isSystem = false);
|
||||
void chatMessage(const QString & title, QString body, bool isSystem = false);
|
||||
|
||||
signals:
|
||||
void messageSent(QString);
|
||||
void channelSwitch(QString);
|
||||
|
||||
public slots:
|
||||
void sysMessage(QString body);
|
||||
|
@ -12,7 +12,7 @@
|
||||
#include <QTcpSocket>
|
||||
#include <QAbstractSocket>
|
||||
|
||||
const unsigned int ProtocolVersion = 4;
|
||||
const unsigned int ProtocolVersion = 5;
|
||||
const std::string ProtocolEncoding = "utf8";
|
||||
|
||||
class ProtocolError: public std::runtime_error
|
||||
@ -24,10 +24,10 @@ public:
|
||||
enum ProtocolConsts
|
||||
{
|
||||
//client consts
|
||||
GREETING, USERNAME, MESSAGE, VERSION, CREATE, JOIN, LEAVE, KICK, READY, FORCESTART, HERE, ALIVE, HOSTMODE,
|
||||
GREETING, USERNAME, MESSAGE, VERSION, CREATE, JOIN, LEAVE, KICK, READY, FORCESTART, HERE, ALIVE, HOSTMODE, SETCHANNEL,
|
||||
|
||||
//server consts
|
||||
SESSIONS, CREATED, JOINED, KICKED, SRVERROR, CHAT, START, STATUS, HOST, MODS, CLIENTMODS, USERS, HEALTH, GAMEMODE
|
||||
SESSIONS, CREATED, JOINED, KICKED, SRVERROR, CHAT, CHATCHANNEL, START, STATUS, HOST, MODS, CLIENTMODS, USERS, HEALTH, GAMEMODE, CHANNEL
|
||||
};
|
||||
|
||||
const QMap<ProtocolConsts, QString> ProtocolStrings
|
||||
@ -88,6 +88,10 @@ const QMap<ProtocolConsts, QString> ProtocolStrings
|
||||
//host sets game mode (new game or load game)
|
||||
//%1: game mode - 0 for new game, 1 for load game
|
||||
{HOSTMODE, "<HOSTMODE>%1"},
|
||||
|
||||
//set new chat channel
|
||||
//%1: channel name
|
||||
{SETCHANNEL, "<CHANNEL>%1"},
|
||||
|
||||
//=== server commands ===
|
||||
//server commands are started from :>>, arguments are enumerated by : symbol
|
||||
@ -149,9 +153,16 @@ const QMap<ProtocolConsts, QString> ProtocolStrings
|
||||
|
||||
//received chat message
|
||||
//arg[0]: sender username
|
||||
//arg[1]: message text
|
||||
//arg[1]: channel
|
||||
//arg[2]: message text
|
||||
{CHAT, "MSG"},
|
||||
|
||||
//received chat message to specific channel
|
||||
//arg[0]: sender username
|
||||
//arg[1]: channel
|
||||
//arg[2]: message text
|
||||
{CHATCHANNEL, "MSGCH"},
|
||||
|
||||
//list of users currently in lobby
|
||||
//arg[0]: amount of players, following arguments depend on it
|
||||
//arg[x]: username
|
||||
@ -164,6 +175,10 @@ const QMap<ProtocolConsts, QString> ProtocolStrings
|
||||
//game mode (new game or load game) set by host
|
||||
//arg[0]: game mode
|
||||
{GAMEMODE, "GAMEMODE"},
|
||||
|
||||
//chat channel changed
|
||||
//arg[0]: channel name
|
||||
{CHANNEL, "CHANNEL"},
|
||||
};
|
||||
|
||||
class ServerCommand
|
||||
|
@ -38,6 +38,7 @@ Lobby::Lobby(QWidget *parent) :
|
||||
connect(&socketLobby, SIGNAL(receive(QString)), this, SLOT(dispatchMessage(QString)));
|
||||
connect(&socketLobby, SIGNAL(disconnect()), this, SLOT(onDisconnected()));
|
||||
connect(ui->chatWidget, SIGNAL(messageSent(QString)), this, SLOT(onMessageSent(QString)));
|
||||
connect(ui->chatWidget, SIGNAL(channelSwitch(QString)), this, SLOT(onChannelSwitch(QString)));
|
||||
|
||||
QString hostString("%1:%2");
|
||||
hostString = hostString.arg(QString::fromStdString(settings["launcher"]["lobbyUrl"].String()));
|
||||
@ -121,7 +122,6 @@ void Lobby::serverCommand(const ServerCommand & command) try
|
||||
hostSession = args[0];
|
||||
session = args[0];
|
||||
ui->chatWidget->setSession(session);
|
||||
ui->chatWidget->sysMessage("new session started");
|
||||
break;
|
||||
|
||||
case SESSIONS:
|
||||
@ -155,14 +155,11 @@ void Lobby::serverCommand(const ServerCommand & command) try
|
||||
protocolAssert(args.size() == 2);
|
||||
session = "";
|
||||
ui->chatWidget->setSession(session);
|
||||
joinStr = (command.command == JOINED ? "%1 joined to the session %2" : "%1 left session %2");
|
||||
|
||||
if(args[1] == username)
|
||||
{
|
||||
hostModsMap.clear();
|
||||
ui->buttonReady->setText("Ready");
|
||||
ui->optNewGame->setChecked(true);
|
||||
ui->chatWidget->sysMessage(joinStr.arg("you", args[0]));
|
||||
session = args[0];
|
||||
ui->chatWidget->setSession(session);
|
||||
bool isHost = command.command == JOINED && hostSession == session;
|
||||
@ -172,6 +169,7 @@ void Lobby::serverCommand(const ServerCommand & command) try
|
||||
}
|
||||
else
|
||||
{
|
||||
joinStr = (command.command == JOINED ? "%1 joined to the session %2" : "%1 left session %2");
|
||||
ui->chatWidget->sysMessage(joinStr.arg(args[1], args[0]));
|
||||
}
|
||||
break;
|
||||
@ -253,6 +251,21 @@ void Lobby::serverCommand(const ServerCommand & command) try
|
||||
break;
|
||||
}
|
||||
|
||||
case CHATCHANNEL: {
|
||||
protocolAssert(args.size() > 2);
|
||||
QString msg;
|
||||
for(int i = 2; i < args.size(); ++i)
|
||||
msg += args[i];
|
||||
ui->chatWidget->chatMessage(args[0], args[1], msg);
|
||||
break;
|
||||
}
|
||||
|
||||
case CHANNEL: {
|
||||
protocolAssert(args.size() == 1);
|
||||
ui->chatWidget->setChannel(args[0]);
|
||||
break;
|
||||
}
|
||||
|
||||
case HEALTH: {
|
||||
socketLobby.send(ProtocolStrings[ALIVE]);
|
||||
break;
|
||||
@ -551,3 +564,8 @@ void Lobby::onMessageSent(QString message)
|
||||
{
|
||||
socketLobby.send(ProtocolStrings[MESSAGE].arg(message));
|
||||
}
|
||||
|
||||
void Lobby::onChannelSwitch(QString channel)
|
||||
{
|
||||
socketLobby.send(ProtocolStrings[SETCHANNEL].arg(channel));
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ private slots:
|
||||
void dispatchMessage(QString);
|
||||
void serverCommand(const ServerCommand &);
|
||||
void onMessageSent(QString message);
|
||||
void onChannelSwitch(QString channel);
|
||||
|
||||
void on_connectButton_toggled(bool checked);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user