mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-02 19:43:55 -07:00
initial commit for chat channels
This commit is contained in:
parent
0d84de2384
commit
947cd1736c
23 changed files with 408 additions and 66 deletions
85
cockatrice/src/chatwidget.cpp
Normal file
85
cockatrice/src/chatwidget.cpp
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
#include <QtGui>
|
||||
#include "chatwidget.h"
|
||||
#include "client.h"
|
||||
|
||||
ChannelWidget::ChannelWidget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
playerList = new QListWidget;
|
||||
|
||||
textEdit = new QTextEdit;
|
||||
sayEdit = new QLineEdit;
|
||||
|
||||
QVBoxLayout *vbox = new QVBoxLayout;
|
||||
vbox->addWidget(textEdit);
|
||||
vbox->addWidget(sayEdit);
|
||||
|
||||
QHBoxLayout *hbox = new QHBoxLayout;
|
||||
hbox->addLayout(vbox);
|
||||
hbox->addWidget(playerList);
|
||||
|
||||
setLayout(hbox);
|
||||
}
|
||||
|
||||
ChatWidget::ChatWidget(Client *_client, QWidget *parent)
|
||||
: QWidget(parent), client(_client)
|
||||
{
|
||||
channelList = new QTreeWidget;
|
||||
tab = new QTabWidget;
|
||||
|
||||
QHBoxLayout *hbox = new QHBoxLayout;
|
||||
hbox->addWidget(channelList);
|
||||
hbox->addWidget(tab, 1);
|
||||
|
||||
retranslateUi();
|
||||
setLayout(hbox);
|
||||
}
|
||||
|
||||
void ChatWidget::retranslateUi()
|
||||
{
|
||||
QTreeWidgetItem *header = channelList->headerItem();
|
||||
Q_ASSERT(header != 0);
|
||||
header->setText(0, tr("Channel"));
|
||||
header->setText(1, tr("Description"));
|
||||
header->setText(2, tr("Players"));
|
||||
}
|
||||
|
||||
void ChatWidget::enableChat()
|
||||
{
|
||||
connect(client, SIGNAL(chatEvent(const ChatEventData &)), this, SLOT(chatEvent(const ChatEventData &)));
|
||||
client->chatListChannels();
|
||||
show();
|
||||
}
|
||||
|
||||
void ChatWidget::disableChat()
|
||||
{
|
||||
disconnect(client, 0, this, 0);
|
||||
hide();
|
||||
}
|
||||
|
||||
void ChatWidget::chatEvent(const ChatEventData &data)
|
||||
{
|
||||
const QStringList &msg = data.getEventData();
|
||||
switch (data.getEventType()) {
|
||||
case eventChatListChannels: {
|
||||
if (msg.size() != 3)
|
||||
break;
|
||||
channelList->addTopLevelItem(new QTreeWidgetItem(QStringList() << msg[0] << msg[1] << msg[2]));
|
||||
break;
|
||||
}
|
||||
case eventChatJoinChannel: {
|
||||
break;
|
||||
}
|
||||
case eventChatListPlayers: {
|
||||
break;
|
||||
}
|
||||
case eventChatLeaveChannel: {
|
||||
break;
|
||||
}
|
||||
case eventChatSay: {
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
}
|
||||
}
|
||||
}
|
||||
38
cockatrice/src/chatwidget.h
Normal file
38
cockatrice/src/chatwidget.h
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#ifndef CHATWIDGET_H
|
||||
#define CHATWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
#include "servereventdata.h"
|
||||
|
||||
class QListWidget;
|
||||
class QTextEdit;
|
||||
class QLineEdit;
|
||||
class QTreeWidget;
|
||||
class QTabWidget;
|
||||
class Client;
|
||||
|
||||
class ChannelWidget : public QWidget {
|
||||
private:
|
||||
QListWidget *playerList;
|
||||
QTextEdit *textEdit;
|
||||
QLineEdit *sayEdit;
|
||||
public:
|
||||
ChannelWidget(QWidget *parent = 0);
|
||||
};
|
||||
|
||||
class ChatWidget : public QWidget {
|
||||
Q_OBJECT
|
||||
private:
|
||||
QTreeWidget *channelList;
|
||||
QTabWidget *tab;
|
||||
Client *client;
|
||||
private slots:
|
||||
void chatEvent(const ChatEventData &data);
|
||||
public:
|
||||
ChatWidget(Client *_client, QWidget *parent = 0);
|
||||
void retranslateUi();
|
||||
void enableChat();
|
||||
void disableChat();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -110,6 +110,8 @@ void Client::readLine()
|
|||
emit playerIdReceived(id, data[1]);
|
||||
} else
|
||||
emit gameEvent(event);
|
||||
} else if (prefix == "chat") {
|
||||
emit chatEvent(ChatEventData(line));
|
||||
} else if (prefix == "resp") {
|
||||
if (values.size() != 2) {
|
||||
// XXX
|
||||
|
|
@ -231,6 +233,26 @@ void Client::ping()
|
|||
cmd("ping");
|
||||
}
|
||||
|
||||
PendingCommand *Client::chatListChannels()
|
||||
{
|
||||
return cmd("chat_list_channels");
|
||||
}
|
||||
|
||||
PendingCommand *Client::chatJoinChannel(const QString &name)
|
||||
{
|
||||
return cmd(QString("chat_join_channel|%1").arg(name));
|
||||
}
|
||||
|
||||
PendingCommand *Client::chatLeaveChannel(const QString &name)
|
||||
{
|
||||
return cmd(QString("chat_leave_channel|%1").arg(name));
|
||||
}
|
||||
|
||||
PendingCommand *Client::chatSay(const QString &channel, const QString &s)
|
||||
{
|
||||
return cmd(QString("chat_say|%1|%2").arg(channel).arg(s));
|
||||
}
|
||||
|
||||
PendingCommand *Client::listGames()
|
||||
{
|
||||
return cmd("list_games");
|
||||
|
|
|
|||
|
|
@ -57,6 +57,7 @@ signals:
|
|||
void responseReceived(int msgid, ServerResponse resp);
|
||||
void playerIdReceived(int id, QString name);
|
||||
void gameEvent(const ServerEventData &msg);
|
||||
void chatEvent(const ChatEventData &msg);
|
||||
void serverTimeout();
|
||||
void logSocketError(const QString &errorString);
|
||||
void serverError(ServerResponse resp);
|
||||
|
|
@ -90,6 +91,10 @@ public:
|
|||
void connectToServer(const QString &hostname, unsigned int port, const QString &_playerName, const QString &_password);
|
||||
void disconnectFromServer();
|
||||
public slots:
|
||||
PendingCommand *chatListChannels();
|
||||
PendingCommand *chatJoinChannel(const QString &name);
|
||||
PendingCommand *chatLeaveChannel(const QString &name);
|
||||
PendingCommand *chatSay(const QString &name, const QString &s);
|
||||
PendingCommand *listGames();
|
||||
PendingCommand *listPlayers();
|
||||
PendingCommand *createGame(const QString &description, const QString &password, unsigned int maxPlayers);
|
||||
|
|
|
|||
|
|
@ -300,9 +300,12 @@ void Game::gameEvent(const ServerEventData &msg)
|
|||
p->gameEvent(msg);
|
||||
break;
|
||||
}
|
||||
case eventInvalid:
|
||||
case eventInvalid: {
|
||||
qDebug("Unhandled global event");
|
||||
}
|
||||
default: {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,18 +27,13 @@ GameSelector::GameSelector(Client *_client, QWidget *parent)
|
|||
|
||||
connect(createButton, SIGNAL(clicked()), this, SLOT(actCreate()));
|
||||
connect(joinButton, SIGNAL(clicked()), this, SLOT(actJoin()));
|
||||
|
||||
connect(client, SIGNAL(gameListEvent(ServerGame *)), gameListModel, SLOT(updateGameList(ServerGame *)));
|
||||
connect(client, SIGNAL(statusChanged(ProtocolStatus)), this, SLOT(statusChanged(ProtocolStatus)));
|
||||
|
||||
client->listGames();
|
||||
}
|
||||
|
||||
void GameSelector::actCreate()
|
||||
{
|
||||
DlgCreateGame dlg(client, this);
|
||||
if (dlg.exec())
|
||||
deleteLater();
|
||||
disableGameList();
|
||||
}
|
||||
|
||||
void GameSelector::actRefresh()
|
||||
|
|
@ -49,7 +44,7 @@ void GameSelector::actRefresh()
|
|||
void GameSelector::statusChanged(ProtocolStatus status)
|
||||
{
|
||||
if (status == StatusDisconnected)
|
||||
deleteLater();
|
||||
disableGameList();
|
||||
}
|
||||
|
||||
void GameSelector::checkResponse(ServerResponse response)
|
||||
|
|
@ -58,7 +53,7 @@ void GameSelector::checkResponse(ServerResponse response)
|
|||
joinButton->setEnabled(true);
|
||||
|
||||
if (response == RespOk)
|
||||
deleteLater();
|
||||
disableGameList();
|
||||
else {
|
||||
QMessageBox::critical(this, tr("Error"), tr("XXX"));
|
||||
return;
|
||||
|
|
@ -84,3 +79,18 @@ void GameSelector::actJoin()
|
|||
createButton->setEnabled(false);
|
||||
joinButton->setEnabled(false);
|
||||
}
|
||||
|
||||
void GameSelector::enableGameList()
|
||||
{
|
||||
connect(client, SIGNAL(gameListEvent(ServerGame *)), gameListModel, SLOT(updateGameList(ServerGame *)));
|
||||
connect(client, SIGNAL(statusChanged(ProtocolStatus)), this, SLOT(statusChanged(ProtocolStatus)));
|
||||
client->listGames();
|
||||
show();
|
||||
}
|
||||
|
||||
void GameSelector::disableGameList()
|
||||
{
|
||||
disconnect(client, 0, this, 0);
|
||||
hide();
|
||||
gameListModel->cleanList();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ class GameSelector : public QWidget {
|
|||
Q_OBJECT
|
||||
public:
|
||||
GameSelector(Client *_client, QWidget *parent = 0);
|
||||
void enableGameList();
|
||||
void disableGameList();
|
||||
private slots:
|
||||
void actCreate();
|
||||
void actRefresh();
|
||||
|
|
|
|||
|
|
@ -69,8 +69,10 @@ void GamesModel::updateGameList(ServerGame *game)
|
|||
|
||||
void GamesModel::cleanList()
|
||||
{
|
||||
beginRemoveRows(QModelIndex(), 0, gameList.size() - 1);
|
||||
QListIterator<ServerGame *> i(gameList);
|
||||
while (i.hasNext())
|
||||
delete i.next();
|
||||
gameList.clear();
|
||||
endRemoveRows();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,11 +16,11 @@ public:
|
|||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
|
||||
|
||||
ServerGame *getGame(int row);
|
||||
void cleanList();
|
||||
public slots:
|
||||
void updateGameList(ServerGame *game);
|
||||
private:
|
||||
QList<ServerGame *> gameList;
|
||||
void cleanList();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -3,49 +3,57 @@
|
|||
// Message structure for server events:
|
||||
// {"private","public"}|PlayerId|PlayerName|EventType|EventData
|
||||
|
||||
const int event_count = 21;
|
||||
const event_string event_strings[event_count] = {
|
||||
{eventPlayerId, "player_id"},
|
||||
{eventSay, "say"},
|
||||
{eventName, "name"},
|
||||
{eventJoin, "join"},
|
||||
{eventLeave, "leave"},
|
||||
{eventReadyStart, "ready_start"},
|
||||
{eventSetupZones, "setup_zones"},
|
||||
{eventGameStart, "game_start"},
|
||||
{eventShuffle, "shuffle"},
|
||||
{eventRollDice, "roll_dice"},
|
||||
{eventDraw, "draw"},
|
||||
{eventMoveCard, "move_card"},
|
||||
{eventCreateToken, "create_token"},
|
||||
{eventSetCardAttr, "set_card_attr"},
|
||||
{eventAddCounter, "add_counter"},
|
||||
{eventSetCounter, "set_counter"},
|
||||
{eventDelCounter, "del_counter"},
|
||||
{eventSetActivePlayer, "set_active_player"},
|
||||
{eventSetActivePhase, "set_active_phase"},
|
||||
{eventDumpZone, "dump_zone"},
|
||||
{eventStopDumpZone, "stop_dump_zone"}
|
||||
};
|
||||
QHash<QString, ServerEventType> ServerEventData::eventHash;
|
||||
|
||||
ServerEventData::ServerEventData(const QString &line)
|
||||
{
|
||||
QStringList values = line.split("|");
|
||||
if (eventHash.isEmpty()) {
|
||||
eventHash.insert("player_id", eventPlayerId);
|
||||
eventHash.insert("say", eventSay);
|
||||
eventHash.insert("name", eventName);
|
||||
eventHash.insert("join", eventJoin);
|
||||
eventHash.insert("leave", eventLeave);
|
||||
eventHash.insert("ready_start", eventReadyStart);
|
||||
eventHash.insert("setup_zones", eventSetupZones);
|
||||
eventHash.insert("game_start", eventGameStart);
|
||||
eventHash.insert("shuffle", eventShuffle);
|
||||
eventHash.insert("roll_dice", eventRollDice);
|
||||
eventHash.insert("draw", eventDraw);
|
||||
eventHash.insert("move_card", eventMoveCard);
|
||||
eventHash.insert("create_token", eventCreateToken);
|
||||
eventHash.insert("set_card_attr", eventSetCardAttr);
|
||||
eventHash.insert("add_counter", eventAddCounter);
|
||||
eventHash.insert("set_counter", eventSetCounter);
|
||||
eventHash.insert("del_counter", eventDelCounter);
|
||||
eventHash.insert("set_active_player", eventSetActivePlayer);
|
||||
eventHash.insert("set_active_phase", eventSetActivePhase);
|
||||
eventHash.insert("dump_zone", eventDumpZone);
|
||||
eventHash.insert("stop_dump_zone", eventStopDumpZone);
|
||||
}
|
||||
|
||||
QStringList values = line.split('|');
|
||||
|
||||
IsPublic = !values.takeFirst().compare("public");
|
||||
PlayerId = values.takeFirst().toInt();
|
||||
PlayerName = values.takeFirst();
|
||||
|
||||
QString type = values.takeFirst();
|
||||
bool found = false;
|
||||
for (int i = 0; i < event_count; i++)
|
||||
if (!type.compare(event_strings[i].str)) {
|
||||
EventType = event_strings[i].type;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
if (!found)
|
||||
EventType = eventInvalid;
|
||||
|
||||
EventType = eventHash.value(values.takeFirst(), eventInvalid);
|
||||
EventData = values;
|
||||
}
|
||||
|
||||
QHash<QString, ChatEventType> ChatEventData::eventHash;
|
||||
|
||||
ChatEventData::ChatEventData(const QString &line)
|
||||
{
|
||||
if (eventHash.isEmpty()) {
|
||||
eventHash.insert("list_channels", eventChatListChannels);
|
||||
eventHash.insert("join_channel", eventChatJoinChannel);
|
||||
eventHash.insert("list_players", eventChatListPlayers);
|
||||
eventHash.insert("leave_channel", eventChatLeaveChannel);
|
||||
eventHash.insert("say", eventChatSay);
|
||||
}
|
||||
|
||||
QStringList values = line.split('|');
|
||||
values.removeFirst();
|
||||
eventType = eventHash.value(values.takeFirst(), eventChatInvalid);
|
||||
eventData = values;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#define SERVEREVENTDATA_H
|
||||
|
||||
#include <QStringList>
|
||||
#include <QHash>
|
||||
|
||||
enum ServerEventType {
|
||||
eventInvalid,
|
||||
|
|
@ -28,16 +29,10 @@ enum ServerEventType {
|
|||
eventStopDumpZone
|
||||
};
|
||||
|
||||
struct event_string {
|
||||
ServerEventType type;
|
||||
char *str;
|
||||
};
|
||||
|
||||
extern const int event_count;
|
||||
extern const event_string event_strings[];
|
||||
|
||||
class ServerEventData {
|
||||
private:
|
||||
static QHash<QString, ServerEventType> eventHash;
|
||||
|
||||
bool IsPublic;
|
||||
int PlayerId;
|
||||
QString PlayerName;
|
||||
|
|
@ -47,9 +42,30 @@ public:
|
|||
ServerEventData(const QString &line);
|
||||
bool getPublic() const { return IsPublic; }
|
||||
int getPlayerId() const { return PlayerId; }
|
||||
QString getPlayerName() const { return PlayerName; }
|
||||
const QString &getPlayerName() const { return PlayerName; }
|
||||
ServerEventType getEventType() const { return EventType; }
|
||||
QStringList getEventData() const { return EventData; }
|
||||
const QStringList &getEventData() const { return EventData; }
|
||||
};
|
||||
|
||||
enum ChatEventType {
|
||||
eventChatInvalid,
|
||||
eventChatListChannels,
|
||||
eventChatJoinChannel,
|
||||
eventChatListPlayers,
|
||||
eventChatLeaveChannel,
|
||||
eventChatSay
|
||||
};
|
||||
|
||||
class ChatEventData {
|
||||
private:
|
||||
static QHash<QString, ChatEventType> eventHash;
|
||||
|
||||
ChatEventType eventType;
|
||||
QStringList eventData;
|
||||
public:
|
||||
ChatEventData(const QString &line);
|
||||
ChatEventType getEventType() const { return eventType; }
|
||||
const QStringList &getEventData() const { return eventData; }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@
|
|||
#include "zoneviewzone.h"
|
||||
#include "zoneviewwidget.h"
|
||||
#include "zoneviewlayout.h"
|
||||
#include "chatwidget.h"
|
||||
|
||||
void MainWindow::hoverCard(QString name)
|
||||
{
|
||||
|
|
@ -86,8 +87,8 @@ void MainWindow::statusChanged(ProtocolStatus _status)
|
|||
phasesToolbar->setActivePhase(-1);
|
||||
phasesToolbar->setEnabled(false);
|
||||
|
||||
GameSelector *gameSelector = new GameSelector(client);
|
||||
viewLayout->insertWidget(0, gameSelector);
|
||||
gameSelector->enableGameList();
|
||||
chatWidget->enableChat();
|
||||
break;
|
||||
}
|
||||
case StatusPlaying:
|
||||
|
|
@ -211,6 +212,7 @@ void MainWindow::retranslateUi()
|
|||
sayLabel->setText(tr("&Say:"));
|
||||
|
||||
cardInfo->retranslateUi();
|
||||
chatWidget->retranslateUi();
|
||||
}
|
||||
|
||||
void MainWindow::createActions()
|
||||
|
|
@ -272,6 +274,7 @@ MainWindow::MainWindow(QTranslator *_translator, QWidget *parent)
|
|||
|
||||
scene = new QGraphicsScene(0, 0, 1096, 1160, this);
|
||||
view = new GameView(scene);
|
||||
view->hide();
|
||||
|
||||
// view->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
|
||||
|
||||
|
|
@ -285,6 +288,12 @@ MainWindow::MainWindow(QTranslator *_translator, QWidget *parent)
|
|||
sayLabel = new QLabel;
|
||||
sayEdit = new QLineEdit;
|
||||
sayLabel->setBuddy(sayEdit);
|
||||
|
||||
client = new Client(this);
|
||||
gameSelector = new GameSelector(client);
|
||||
gameSelector->hide();
|
||||
chatWidget = new ChatWidget(client);
|
||||
chatWidget->hide();
|
||||
|
||||
QHBoxLayout *hLayout = new QHBoxLayout;
|
||||
hLayout->addWidget(sayLabel);
|
||||
|
|
@ -296,6 +305,8 @@ MainWindow::MainWindow(QTranslator *_translator, QWidget *parent)
|
|||
verticalLayout->addLayout(hLayout);
|
||||
|
||||
viewLayout = new QVBoxLayout;
|
||||
viewLayout->addWidget(gameSelector);
|
||||
viewLayout->addWidget(chatWidget);
|
||||
viewLayout->addWidget(view);
|
||||
|
||||
phasesToolbar = new PhasesToolbar;
|
||||
|
|
@ -312,7 +323,6 @@ MainWindow::MainWindow(QTranslator *_translator, QWidget *parent)
|
|||
|
||||
connect(sayEdit, SIGNAL(returnPressed()), this, SLOT(actSay()));
|
||||
|
||||
client = new Client(this);
|
||||
connect(client, SIGNAL(serverTimeout()), this, SLOT(serverTimeout()));
|
||||
connect(client, SIGNAL(statusChanged(ProtocolStatus)), this, SLOT(statusChanged(ProtocolStatus)));
|
||||
connect(client, SIGNAL(playerIdReceived(int, QString)), this, SLOT(playerIdReceived(int, QString)));
|
||||
|
|
|
|||
|
|
@ -40,6 +40,8 @@ class ServerZoneCard;
|
|||
class ZoneViewLayout;
|
||||
class ZoneViewWidget;
|
||||
class PhasesToolbar;
|
||||
class GameSelector;
|
||||
class ChatWidget;
|
||||
|
||||
class MainWindow : public QMainWindow {
|
||||
Q_OBJECT
|
||||
|
|
@ -80,6 +82,8 @@ private:
|
|||
QLabel *sayLabel;
|
||||
QLineEdit *sayEdit;
|
||||
PhasesToolbar *phasesToolbar;
|
||||
GameSelector *gameSelector;
|
||||
ChatWidget *chatWidget;
|
||||
|
||||
Client *client;
|
||||
QGraphicsScene *scene;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue