chat code
This commit is contained in:
parent
8daa81e462
commit
5d32bb8bc4
14 changed files with 85 additions and 28 deletions
|
@ -62,6 +62,7 @@ CardInfoWidget::CardInfoWidget(CardDatabase *_db, QWidget *parent)
|
||||||
|
|
||||||
retranslateUi();
|
retranslateUi();
|
||||||
setFrameStyle(QFrame::Panel | QFrame::Raised);
|
setFrameStyle(QFrame::Panel | QFrame::Raised);
|
||||||
|
textLabel->setFixedHeight(130);
|
||||||
setFixedSize(sizeHint());
|
setFixedSize(sizeHint());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,23 +2,27 @@
|
||||||
#include "chatwidget.h"
|
#include "chatwidget.h"
|
||||||
#include "client.h"
|
#include "client.h"
|
||||||
|
|
||||||
ChannelWidget::ChannelWidget(Client *_client, const QString &_name, QWidget *parent)
|
ChannelWidget::ChannelWidget(Client *_client, const QString &_name, bool readOnly, QWidget *parent)
|
||||||
: QWidget(parent), client(_client), name(_name)
|
: QWidget(parent), client(_client), name(_name)
|
||||||
{
|
{
|
||||||
playerList = new QListWidget;
|
playerList = new QListWidget;
|
||||||
|
|
||||||
textEdit = new QTextEdit;
|
textEdit = new QTextEdit;
|
||||||
textEdit->setReadOnly(true);
|
textEdit->setReadOnly(true);
|
||||||
sayEdit = new QLineEdit;
|
if (!readOnly) {
|
||||||
connect(sayEdit, SIGNAL(returnPressed()), this, SLOT(sendMessage()));
|
sayEdit = new QLineEdit;
|
||||||
|
connect(sayEdit, SIGNAL(returnPressed()), this, SLOT(sendMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
QVBoxLayout *vbox = new QVBoxLayout;
|
QVBoxLayout *vbox = new QVBoxLayout;
|
||||||
vbox->addWidget(textEdit);
|
vbox->addWidget(textEdit);
|
||||||
vbox->addWidget(sayEdit);
|
if (!readOnly)
|
||||||
|
vbox->addWidget(sayEdit);
|
||||||
|
|
||||||
QHBoxLayout *hbox = new QHBoxLayout;
|
QHBoxLayout *hbox = new QHBoxLayout;
|
||||||
hbox->addLayout(vbox, 1);
|
hbox->addLayout(vbox);
|
||||||
hbox->addWidget(playerList);
|
hbox->addWidget(playerList);
|
||||||
|
playerList->setFixedWidth(100);
|
||||||
|
|
||||||
setLayout(hbox);
|
setLayout(hbox);
|
||||||
}
|
}
|
||||||
|
@ -57,6 +61,11 @@ void ChannelWidget::sayEvent(const QString &playerName, const QString &s)
|
||||||
textEdit->append(QString("<font color=\"red\">%1:</font> %2").arg(playerName).arg(s));
|
textEdit->append(QString("<font color=\"red\">%1:</font> %2").arg(playerName).arg(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ChannelWidget::serverMessageEvent(const QString &s)
|
||||||
|
{
|
||||||
|
textEdit->append(QString("<font color=\"blue\">%1</font>").arg(s));
|
||||||
|
}
|
||||||
|
|
||||||
ChatWidget::ChatWidget(Client *_client, QWidget *parent)
|
ChatWidget::ChatWidget(Client *_client, QWidget *parent)
|
||||||
: QWidget(parent), client(_client)
|
: QWidget(parent), client(_client)
|
||||||
{
|
{
|
||||||
|
@ -110,7 +119,7 @@ void ChatWidget::chatEvent(const ChatEventData &data)
|
||||||
const QStringList &msg = data.getEventData();
|
const QStringList &msg = data.getEventData();
|
||||||
switch (data.getEventType()) {
|
switch (data.getEventType()) {
|
||||||
case eventChatListChannels: {
|
case eventChatListChannels: {
|
||||||
if (msg.size() != 3)
|
if (msg.size() != 4)
|
||||||
break;
|
break;
|
||||||
for (int i = 0; i < channelList->topLevelItemCount(); ++i) {
|
for (int i = 0; i < channelList->topLevelItemCount(); ++i) {
|
||||||
QTreeWidgetItem *twi = channelList->topLevelItem(i);
|
QTreeWidgetItem *twi = channelList->topLevelItem(i);
|
||||||
|
@ -121,6 +130,8 @@ void ChatWidget::chatEvent(const ChatEventData &data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
channelList->addTopLevelItem(new QTreeWidgetItem(QStringList() << msg[0] << msg[1] << msg[2]));
|
channelList->addTopLevelItem(new QTreeWidgetItem(QStringList() << msg[0] << msg[1] << msg[2]));
|
||||||
|
if (msg[3] == "1")
|
||||||
|
joinChannel(msg[0]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case eventChatJoinChannel: {
|
case eventChatJoinChannel: {
|
||||||
|
@ -159,11 +170,33 @@ void ChatWidget::chatEvent(const ChatEventData &data)
|
||||||
w->sayEvent(msg[1], msg[2]);
|
w->sayEvent(msg[1], msg[2]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case eventChatServerMessage: {
|
||||||
|
if (msg.size() != 2)
|
||||||
|
break;
|
||||||
|
ChannelWidget *w;
|
||||||
|
if (msg[0].isEmpty()) {
|
||||||
|
w = getChannel("Server");
|
||||||
|
if (!w) {
|
||||||
|
w = new ChannelWidget(client, "Server", true);
|
||||||
|
tab->addTab(w, "Server");
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
w = getChannel(msg[0]);
|
||||||
|
w->serverMessageEvent(msg[1]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
default: {
|
default: {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ChatWidget::joinChannel(const QString &channelName)
|
||||||
|
{
|
||||||
|
PendingCommand *pc = client->chatJoinChannel(channelName);
|
||||||
|
pc->setExtraData(channelName);
|
||||||
|
connect(pc, SIGNAL(finished(ServerResponse)), this, SLOT(joinFinished(ServerResponse)));
|
||||||
|
}
|
||||||
|
|
||||||
void ChatWidget::joinClicked()
|
void ChatWidget::joinClicked()
|
||||||
{
|
{
|
||||||
QTreeWidgetItem *twi = channelList->currentItem();
|
QTreeWidgetItem *twi = channelList->currentItem();
|
||||||
|
@ -173,9 +206,7 @@ void ChatWidget::joinClicked()
|
||||||
if (getChannel(channelName))
|
if (getChannel(channelName))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
PendingCommand *pc = client->chatJoinChannel(channelName);
|
joinChannel(channelName);
|
||||||
pc->setExtraData(channelName);
|
|
||||||
connect(pc, SIGNAL(finished(ServerResponse)), this, SLOT(joinFinished(ServerResponse)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChatWidget::joinFinished(ServerResponse resp)
|
void ChatWidget::joinFinished(ServerResponse resp)
|
||||||
|
|
|
@ -23,13 +23,14 @@ private:
|
||||||
private slots:
|
private slots:
|
||||||
void sendMessage();
|
void sendMessage();
|
||||||
public:
|
public:
|
||||||
ChannelWidget(Client *_client, const QString &_name, QWidget *parent = 0);
|
ChannelWidget(Client *_client, const QString &_name, bool readOnly = false, QWidget *parent = 0);
|
||||||
const QString &getName() const { return name; }
|
const QString &getName() const { return name; }
|
||||||
|
|
||||||
void joinEvent(const QString &playerName);
|
void joinEvent(const QString &playerName);
|
||||||
void listPlayersEvent(const QString &playerName);
|
void listPlayersEvent(const QString &playerName);
|
||||||
void leaveEvent(const QString &playerName);
|
void leaveEvent(const QString &playerName);
|
||||||
void sayEvent(const QString &playerName, const QString &s);
|
void sayEvent(const QString &playerName, const QString &s);
|
||||||
|
void serverMessageEvent(const QString &s);
|
||||||
};
|
};
|
||||||
|
|
||||||
class ChatWidget : public QWidget {
|
class ChatWidget : public QWidget {
|
||||||
|
@ -41,6 +42,7 @@ private:
|
||||||
Client *client;
|
Client *client;
|
||||||
|
|
||||||
ChannelWidget *getChannel(const QString &name);
|
ChannelWidget *getChannel(const QString &name);
|
||||||
|
void joinChannel(const QString &channelName);
|
||||||
private slots:
|
private slots:
|
||||||
void chatEvent(const ChatEventData &data);
|
void chatEvent(const ChatEventData &data);
|
||||||
void joinClicked();
|
void joinClicked();
|
||||||
|
|
|
@ -42,12 +42,6 @@ void GameSelector::actRefresh()
|
||||||
client->listGames();
|
client->listGames();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameSelector::statusChanged(ProtocolStatus status)
|
|
||||||
{
|
|
||||||
if (status == StatusDisconnected)
|
|
||||||
disableGameList();
|
|
||||||
}
|
|
||||||
|
|
||||||
void GameSelector::checkResponse(ServerResponse response)
|
void GameSelector::checkResponse(ServerResponse response)
|
||||||
{
|
{
|
||||||
createButton->setEnabled(true);
|
createButton->setEnabled(true);
|
||||||
|
@ -84,7 +78,6 @@ void GameSelector::actJoin()
|
||||||
void GameSelector::enableGameList()
|
void GameSelector::enableGameList()
|
||||||
{
|
{
|
||||||
connect(client, SIGNAL(gameListEvent(ServerGame *)), gameListModel, SLOT(updateGameList(ServerGame *)));
|
connect(client, SIGNAL(gameListEvent(ServerGame *)), gameListModel, SLOT(updateGameList(ServerGame *)));
|
||||||
connect(client, SIGNAL(statusChanged(ProtocolStatus)), this, SLOT(statusChanged(ProtocolStatus)));
|
|
||||||
client->listGames();
|
client->listGames();
|
||||||
show();
|
show();
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,6 @@ private slots:
|
||||||
void actRefresh();
|
void actRefresh();
|
||||||
void actJoin();
|
void actJoin();
|
||||||
void checkResponse(ServerResponse response);
|
void checkResponse(ServerResponse response);
|
||||||
void statusChanged(ProtocolStatus status);
|
|
||||||
private:
|
private:
|
||||||
Client *client;
|
Client *client;
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,7 @@ ChatEventData::ChatEventData(const QString &line)
|
||||||
eventHash.insert("list_players", eventChatListPlayers);
|
eventHash.insert("list_players", eventChatListPlayers);
|
||||||
eventHash.insert("leave_channel", eventChatLeaveChannel);
|
eventHash.insert("leave_channel", eventChatLeaveChannel);
|
||||||
eventHash.insert("say", eventChatSay);
|
eventHash.insert("say", eventChatSay);
|
||||||
|
eventHash.insert("server_message", eventChatServerMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList values = line.split('|');
|
QStringList values = line.split('|');
|
||||||
|
|
|
@ -53,7 +53,8 @@ enum ChatEventType {
|
||||||
eventChatJoinChannel,
|
eventChatJoinChannel,
|
||||||
eventChatListPlayers,
|
eventChatListPlayers,
|
||||||
eventChatLeaveChannel,
|
eventChatLeaveChannel,
|
||||||
eventChatSay
|
eventChatSay,
|
||||||
|
eventChatServerMessage
|
||||||
};
|
};
|
||||||
|
|
||||||
class ChatEventData {
|
class ChatEventData {
|
||||||
|
|
|
@ -71,6 +71,8 @@ void MainWindow::statusChanged(ProtocolStatus _status)
|
||||||
aLeaveGame->setEnabled(false);
|
aLeaveGame->setEnabled(false);
|
||||||
phasesToolbar->setActivePhase(-1);
|
phasesToolbar->setActivePhase(-1);
|
||||||
phasesToolbar->hide();
|
phasesToolbar->hide();
|
||||||
|
gameSelector->disableGameList();
|
||||||
|
chatWidget->disableChat();
|
||||||
emit logDisconnected();
|
emit logDisconnected();
|
||||||
break;
|
break;
|
||||||
case StatusLoggingIn:
|
case StatusLoggingIn:
|
||||||
|
|
|
@ -7,3 +7,6 @@ hostname=localhost
|
||||||
database=servatrice
|
database=servatrice
|
||||||
user=servatrice
|
user=servatrice
|
||||||
password=foobar
|
password=foobar
|
||||||
|
|
||||||
|
[messages]
|
||||||
|
login="Example line 1", "Example line 2"
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
#include "chatchannel.h"
|
#include "chatchannel.h"
|
||||||
#include "serversocket.h"
|
#include "serversocket.h"
|
||||||
|
|
||||||
ChatChannel::ChatChannel(const QString &_name, const QString &_description)
|
ChatChannel::ChatChannel(const QString &_name, const QString &_description, bool _autoJoin, const QStringList &_joinMessage)
|
||||||
: name(_name), description(_description)
|
: name(_name), description(_description), autoJoin(_autoJoin), joinMessage(_joinMessage)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,8 @@ void ChatChannel::addPlayer(ServerSocket *player)
|
||||||
|
|
||||||
for (int i = 0; i < size(); ++i)
|
for (int i = 0; i < size(); ++i)
|
||||||
player->msg(QString("chat|list_players|%1|%2").arg(name).arg(at(i)->getPlayerName()));
|
player->msg(QString("chat|list_players|%1|%2").arg(name).arg(at(i)->getPlayerName()));
|
||||||
|
for (int i = 0; i < joinMessage.size(); ++i)
|
||||||
|
player->msg(QString("chat|server_message|%1|%2").arg(name).arg(joinMessage[i]));
|
||||||
|
|
||||||
emit channelInfoChanged();
|
emit channelInfoChanged();
|
||||||
}
|
}
|
||||||
|
@ -40,5 +42,5 @@ void ChatChannel::say(ServerSocket *player, const QString &s)
|
||||||
|
|
||||||
QString ChatChannel::getChannelListLine() const
|
QString ChatChannel::getChannelListLine() const
|
||||||
{
|
{
|
||||||
return QString("chat|list_channels|%1|%2|%3").arg(name).arg(description).arg(size());
|
return QString("chat|list_channels|%1|%2|%3|%4").arg(name).arg(description).arg(size()).arg(autoJoin ? 1 : 0);
|
||||||
}
|
}
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
#include <QStringList>
|
||||||
|
|
||||||
class ServerSocket;
|
class ServerSocket;
|
||||||
|
|
||||||
|
@ -13,10 +14,13 @@ signals:
|
||||||
private:
|
private:
|
||||||
QString name;
|
QString name;
|
||||||
QString description;
|
QString description;
|
||||||
|
bool autoJoin;
|
||||||
|
QStringList joinMessage;
|
||||||
public:
|
public:
|
||||||
ChatChannel(const QString &_name, const QString &_description);
|
ChatChannel(const QString &_name, const QString &_description, bool _autoJoin, const QStringList &_joinMessage);
|
||||||
QString getName() const { return name; }
|
QString getName() const { return name; }
|
||||||
QString getDescription() const { return description; }
|
QString getDescription() const { return description; }
|
||||||
|
bool getAutoJoin() const { return autoJoin; }
|
||||||
void addPlayer(ServerSocket *player);
|
void addPlayer(ServerSocket *player);
|
||||||
void removePlayer(ServerSocket *player);
|
void removePlayer(ServerSocket *player);
|
||||||
void say(ServerSocket *player, const QString &s);
|
void say(ServerSocket *player, const QString &s);
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
|
|
||||||
Server::Server(QObject *parent)
|
Server::Server(QObject *parent)
|
||||||
: QTcpServer(parent), nextGameId(0)
|
: QTcpServer(parent), nextGameId(0)
|
||||||
{
|
{
|
||||||
rng = new RNG_Qt(this);
|
rng = new RNG_Qt(this);
|
||||||
|
|
||||||
|
@ -37,11 +37,20 @@ Server::Server(QObject *parent)
|
||||||
if (dbType == "mysql")
|
if (dbType == "mysql")
|
||||||
openDatabase();
|
openDatabase();
|
||||||
|
|
||||||
chatChannelList << new ChatChannel("channel1", "testchannel 1");
|
int size = settings->beginReadArray("chatchannels");
|
||||||
chatChannelList << new ChatChannel("channel2", "testchannel 2");
|
for (int i = 0; i < size; ++i) {
|
||||||
|
settings->setArrayIndex(i);
|
||||||
|
chatChannelList << new ChatChannel(settings->value("name").toString(),
|
||||||
|
settings->value("description").toString(),
|
||||||
|
settings->value("autojoin").toBool(),
|
||||||
|
settings->value("joinmessage").toStringList());
|
||||||
|
}
|
||||||
|
settings->endArray();
|
||||||
|
|
||||||
for (int i = 0; i < chatChannelList.size(); ++i)
|
for (int i = 0; i < chatChannelList.size(); ++i)
|
||||||
connect(chatChannelList[i], SIGNAL(channelInfoChanged()), this, SLOT(broadcastChannelUpdate()));
|
connect(chatChannelList[i], SIGNAL(channelInfoChanged()), this, SLOT(broadcastChannelUpdate()));
|
||||||
|
|
||||||
|
loginMessage = settings->value("messages/login").toStringList();
|
||||||
}
|
}
|
||||||
|
|
||||||
Server::~Server()
|
Server::~Server()
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
#define SERVER_H
|
#define SERVER_H
|
||||||
|
|
||||||
#include <QTcpServer>
|
#include <QTcpServer>
|
||||||
|
#include <QStringList>
|
||||||
|
|
||||||
class ServerGame;
|
class ServerGame;
|
||||||
class ServerSocket;
|
class ServerSocket;
|
||||||
|
@ -52,12 +53,14 @@ public:
|
||||||
AbstractRNG *getRNG() const { return rng; }
|
AbstractRNG *getRNG() const { return rng; }
|
||||||
void broadcastGameListUpdate(ServerGame *game);
|
void broadcastGameListUpdate(ServerGame *game);
|
||||||
void removePlayer(ServerSocket *player);
|
void removePlayer(ServerSocket *player);
|
||||||
|
const QStringList &getLoginMessage() const { return loginMessage; }
|
||||||
private:
|
private:
|
||||||
void incomingConnection(int SocketId);
|
void incomingConnection(int SocketId);
|
||||||
QList<ServerGame *> games;
|
QList<ServerGame *> games;
|
||||||
QList<ServerSocket *> players;
|
QList<ServerSocket *> players;
|
||||||
QList<ChatChannel *> chatChannelList;
|
QList<ChatChannel *> chatChannelList;
|
||||||
int nextGameId;
|
int nextGameId;
|
||||||
|
QStringList loginMessage;
|
||||||
AbstractRNG *rng;
|
AbstractRNG *rng;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -236,7 +236,13 @@ ReturnMessage::ReturnCode ServerSocket::cmdLogin(const QList<QVariant> ¶ms)
|
||||||
return ReturnMessage::ReturnPasswordWrong;
|
return ReturnMessage::ReturnPasswordWrong;
|
||||||
playerName = params[0].toString();
|
playerName = params[0].toString();
|
||||||
|
|
||||||
return ReturnMessage::ReturnOk;
|
remsg->send(ReturnMessage::ReturnOk);
|
||||||
|
|
||||||
|
QStringList loginMessage = server->getLoginMessage();
|
||||||
|
for (int i = 0; i < loginMessage.size(); ++i)
|
||||||
|
msg("chat|server_message||" + loginMessage[i]);
|
||||||
|
|
||||||
|
return ReturnMessage::ReturnNothing;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReturnMessage::ReturnCode ServerSocket::cmdChatListChannels(const QList<QVariant> &/*params*/)
|
ReturnMessage::ReturnCode ServerSocket::cmdChatListChannels(const QList<QVariant> &/*params*/)
|
||||||
|
|
Loading…
Reference in a new issue