chat code

This commit is contained in:
Max-Wilhelm Bruker 2009-08-23 16:25:10 +02:00
parent 8daa81e462
commit 5d32bb8bc4
14 changed files with 85 additions and 28 deletions

View file

@ -62,6 +62,7 @@ CardInfoWidget::CardInfoWidget(CardDatabase *_db, QWidget *parent)
retranslateUi();
setFrameStyle(QFrame::Panel | QFrame::Raised);
textLabel->setFixedHeight(130);
setFixedSize(sizeHint());
}

View file

@ -2,23 +2,27 @@
#include "chatwidget.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)
{
playerList = new QListWidget;
textEdit = new QTextEdit;
textEdit->setReadOnly(true);
sayEdit = new QLineEdit;
connect(sayEdit, SIGNAL(returnPressed()), this, SLOT(sendMessage()));
if (!readOnly) {
sayEdit = new QLineEdit;
connect(sayEdit, SIGNAL(returnPressed()), this, SLOT(sendMessage()));
}
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(textEdit);
vbox->addWidget(sayEdit);
if (!readOnly)
vbox->addWidget(sayEdit);
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addLayout(vbox, 1);
hbox->addLayout(vbox);
hbox->addWidget(playerList);
playerList->setFixedWidth(100);
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));
}
void ChannelWidget::serverMessageEvent(const QString &s)
{
textEdit->append(QString("<font color=\"blue\">%1</font>").arg(s));
}
ChatWidget::ChatWidget(Client *_client, QWidget *parent)
: QWidget(parent), client(_client)
{
@ -110,7 +119,7 @@ void ChatWidget::chatEvent(const ChatEventData &data)
const QStringList &msg = data.getEventData();
switch (data.getEventType()) {
case eventChatListChannels: {
if (msg.size() != 3)
if (msg.size() != 4)
break;
for (int i = 0; i < channelList->topLevelItemCount(); ++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]));
if (msg[3] == "1")
joinChannel(msg[0]);
break;
}
case eventChatJoinChannel: {
@ -159,11 +170,33 @@ void ChatWidget::chatEvent(const ChatEventData &data)
w->sayEvent(msg[1], msg[2]);
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: {
}
}
}
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()
{
QTreeWidgetItem *twi = channelList->currentItem();
@ -173,9 +206,7 @@ void ChatWidget::joinClicked()
if (getChannel(channelName))
return;
PendingCommand *pc = client->chatJoinChannel(channelName);
pc->setExtraData(channelName);
connect(pc, SIGNAL(finished(ServerResponse)), this, SLOT(joinFinished(ServerResponse)));
joinChannel(channelName);
}
void ChatWidget::joinFinished(ServerResponse resp)

View file

@ -23,13 +23,14 @@ private:
private slots:
void sendMessage();
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; }
void joinEvent(const QString &playerName);
void listPlayersEvent(const QString &playerName);
void leaveEvent(const QString &playerName);
void sayEvent(const QString &playerName, const QString &s);
void serverMessageEvent(const QString &s);
};
class ChatWidget : public QWidget {
@ -41,6 +42,7 @@ private:
Client *client;
ChannelWidget *getChannel(const QString &name);
void joinChannel(const QString &channelName);
private slots:
void chatEvent(const ChatEventData &data);
void joinClicked();

View file

@ -42,12 +42,6 @@ void GameSelector::actRefresh()
client->listGames();
}
void GameSelector::statusChanged(ProtocolStatus status)
{
if (status == StatusDisconnected)
disableGameList();
}
void GameSelector::checkResponse(ServerResponse response)
{
createButton->setEnabled(true);
@ -84,7 +78,6 @@ void GameSelector::actJoin()
void GameSelector::enableGameList()
{
connect(client, SIGNAL(gameListEvent(ServerGame *)), gameListModel, SLOT(updateGameList(ServerGame *)));
connect(client, SIGNAL(statusChanged(ProtocolStatus)), this, SLOT(statusChanged(ProtocolStatus)));
client->listGames();
show();
}

View file

@ -21,7 +21,6 @@ private slots:
void actRefresh();
void actJoin();
void checkResponse(ServerResponse response);
void statusChanged(ProtocolStatus status);
private:
Client *client;

View file

@ -50,6 +50,7 @@ ChatEventData::ChatEventData(const QString &line)
eventHash.insert("list_players", eventChatListPlayers);
eventHash.insert("leave_channel", eventChatLeaveChannel);
eventHash.insert("say", eventChatSay);
eventHash.insert("server_message", eventChatServerMessage);
}
QStringList values = line.split('|');

View file

@ -53,7 +53,8 @@ enum ChatEventType {
eventChatJoinChannel,
eventChatListPlayers,
eventChatLeaveChannel,
eventChatSay
eventChatSay,
eventChatServerMessage
};
class ChatEventData {

View file

@ -71,6 +71,8 @@ void MainWindow::statusChanged(ProtocolStatus _status)
aLeaveGame->setEnabled(false);
phasesToolbar->setActivePhase(-1);
phasesToolbar->hide();
gameSelector->disableGameList();
chatWidget->disableChat();
emit logDisconnected();
break;
case StatusLoggingIn:

View file

@ -7,3 +7,6 @@ hostname=localhost
database=servatrice
user=servatrice
password=foobar
[messages]
login="Example line 1", "Example line 2"

View file

@ -1,8 +1,8 @@
#include "chatchannel.h"
#include "serversocket.h"
ChatChannel::ChatChannel(const QString &_name, const QString &_description)
: name(_name), description(_description)
ChatChannel::ChatChannel(const QString &_name, const QString &_description, bool _autoJoin, const QStringList &_joinMessage)
: name(_name), description(_description), autoJoin(_autoJoin), joinMessage(_joinMessage)
{
}
@ -16,6 +16,8 @@ void ChatChannel::addPlayer(ServerSocket *player)
for (int i = 0; i < size(); ++i)
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();
}
@ -40,5 +42,5 @@ void ChatChannel::say(ServerSocket *player, const QString &s)
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);
}

View file

@ -3,6 +3,7 @@
#include <QList>
#include <QObject>
#include <QStringList>
class ServerSocket;
@ -13,10 +14,13 @@ signals:
private:
QString name;
QString description;
bool autoJoin;
QStringList joinMessage;
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 getDescription() const { return description; }
bool getAutoJoin() const { return autoJoin; }
void addPlayer(ServerSocket *player);
void removePlayer(ServerSocket *player);
void say(ServerSocket *player, const QString &s);

View file

@ -27,7 +27,7 @@
#include <QSettings>
Server::Server(QObject *parent)
: QTcpServer(parent), nextGameId(0)
: QTcpServer(parent), nextGameId(0)
{
rng = new RNG_Qt(this);
@ -37,11 +37,20 @@ Server::Server(QObject *parent)
if (dbType == "mysql")
openDatabase();
chatChannelList << new ChatChannel("channel1", "testchannel 1");
chatChannelList << new ChatChannel("channel2", "testchannel 2");
int size = settings->beginReadArray("chatchannels");
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)
connect(chatChannelList[i], SIGNAL(channelInfoChanged()), this, SLOT(broadcastChannelUpdate()));
loginMessage = settings->value("messages/login").toStringList();
}
Server::~Server()

View file

@ -21,6 +21,7 @@
#define SERVER_H
#include <QTcpServer>
#include <QStringList>
class ServerGame;
class ServerSocket;
@ -52,12 +53,14 @@ public:
AbstractRNG *getRNG() const { return rng; }
void broadcastGameListUpdate(ServerGame *game);
void removePlayer(ServerSocket *player);
const QStringList &getLoginMessage() const { return loginMessage; }
private:
void incomingConnection(int SocketId);
QList<ServerGame *> games;
QList<ServerSocket *> players;
QList<ChatChannel *> chatChannelList;
int nextGameId;
QStringList loginMessage;
AbstractRNG *rng;
};

View file

@ -236,7 +236,13 @@ ReturnMessage::ReturnCode ServerSocket::cmdLogin(const QList<QVariant> &params)
return ReturnMessage::ReturnPasswordWrong;
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*/)