servatrice/cockatrice/src/tab_server.cpp
Max-Wilhelm Bruker 64aa68cd26 Chat works!
2009-11-13 18:27:06 +01:00

329 lines
9.5 KiB
C++

#include <QtGui>
#include "tab_server.h"
#include "gamesmodel.h"
#include "dlg_creategame.h"
#include "client.h"
#include "protocol.h"
#include "protocol_items.h"
GameSelector::GameSelector(Client *_client, QWidget *parent)
: QGroupBox(parent), client(_client)
{
gameListView = new QTreeView;
gameListModel = new GamesModel(this);
gameListProxyModel = new GamesProxyModel(this);
gameListProxyModel->setSourceModel(gameListModel);
gameListView->setModel(gameListProxyModel);
showFullGamesCheckBox = new QCheckBox;
createButton = new QPushButton;
joinButton = new QPushButton;
spectateButton = new QPushButton;
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(showFullGamesCheckBox);
buttonLayout->addStretch();
buttonLayout->addWidget(createButton);
buttonLayout->addWidget(joinButton);
buttonLayout->addWidget(spectateButton);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(gameListView);
mainLayout->addLayout(buttonLayout);
retranslateUi();
setLayout(mainLayout);
setMinimumWidth(gameListView->columnWidth(0) * gameListModel->columnCount());
setMinimumHeight(400);
connect(showFullGamesCheckBox, SIGNAL(stateChanged(int)), this, SLOT(showFullGamesChanged(int)));
connect(createButton, SIGNAL(clicked()), this, SLOT(actCreate()));
connect(joinButton, SIGNAL(clicked()), this, SLOT(actJoin()));
connect(spectateButton, SIGNAL(clicked()), this, SLOT(actJoin()));
connect(client, SIGNAL(listGamesEventReceived(Event_ListGames *)), this, SLOT(processListGamesEvent(Event_ListGames *)));
client->sendCommand(new Command_ListGames);
}
void GameSelector::showFullGamesChanged(int state)
{
gameListProxyModel->setFullGamesVisible(state);
}
void GameSelector::actCreate()
{
DlgCreateGame dlg(client, this);
dlg.exec();
}
void GameSelector::checkResponse(ResponseCode response)
{
createButton->setEnabled(true);
joinButton->setEnabled(true);
spectateButton->setEnabled(true);
switch (response) {
case RespOk: /* HIER CODE FÜR NEUEN GAME_TAB EINFÜGEN */ break;
case RespWrongPassword: QMessageBox::critical(this, tr("Error"), tr("Wrong password.")); break;
case RespSpectatorsNotAllowed: QMessageBox::critical(this, tr("Error"), tr("Spectators are not allowed in this game.")); break;
case RespContextError: QMessageBox::critical(this, tr("Error"), tr("The game is already full.")); break;
case RespNameNotFound: QMessageBox::critical(this, tr("Error"), tr("The game does not exist any more.")); break;
default: ;
}
}
void GameSelector::actJoin()
{
bool spectator = sender() == spectateButton;
QModelIndex ind = gameListView->currentIndex();
if (!ind.isValid())
return;
const ServerGameInfo &game = gameListModel->getGame(ind.data(Qt::UserRole).toInt());
QString password;
if (game.getHasPassword()) {
bool ok;
password = QInputDialog::getText(this, tr("Join game"), tr("Password:"), QLineEdit::Password, QString(), &ok);
if (!ok)
return;
}
Command_JoinGame *commandJoinGame = new Command_JoinGame(game.getGameId(), password, spectator);
connect(commandJoinGame, SIGNAL(finished(ResponseCode)), this, SLOT(checkResponse(ResponseCode)));
client->sendCommand(commandJoinGame);
createButton->setEnabled(false);
joinButton->setEnabled(false);
spectateButton->setEnabled(false);
}
void GameSelector::retranslateUi()
{
setTitle(tr("Games"));
showFullGamesCheckBox->setText(tr("&Show full games"));
createButton->setText(tr("C&reate"));
joinButton->setText(tr("&Join"));
spectateButton->setText(tr("J&oin as spectator"));
}
void GameSelector::processListGamesEvent(Event_ListGames *event)
{
const QList<ServerGameInfo> &gamesToUpdate = event->getGameList();
for (int i = 0; i < gamesToUpdate.size(); ++i)
gameListModel->updateGameList(gamesToUpdate[i]);
}
ChatChannelSelector::ChatChannelSelector(Client *_client, QWidget *parent)
: QGroupBox(parent), client(_client)
{
channelList = new QTreeWidget;
channelList->setRootIsDecorated(false);
joinButton = new QPushButton;
connect(joinButton, SIGNAL(clicked()), this, SLOT(joinClicked()));
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch();
buttonLayout->addWidget(joinButton);
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(channelList);
vbox->addLayout(buttonLayout);
retranslateUi();
setLayout(vbox);
connect(client, SIGNAL(listChatChannelsEventReceived(Event_ListChatChannels *)), this, SLOT(processListChatChannelsEvent(Event_ListChatChannels *)));
client->sendCommand(new Command_ListChatChannels);
}
void ChatChannelSelector::retranslateUi()
{
setTitle(tr("Chat channels"));
joinButton->setText(tr("Joi&n"));
QTreeWidgetItem *header = channelList->headerItem();
header->setText(0, tr("Channel"));
header->setText(1, tr("Description"));
header->setText(2, tr("Players"));
header->setTextAlignment(2, Qt::AlignRight);
}
void ChatChannelSelector::processListChatChannelsEvent(Event_ListChatChannels *event)
{
const QList<ServerChatChannelInfo> &channelsToUpdate = event->getChannelList();
for (int i = 0; i < channelsToUpdate.size(); ++i) {
const ServerChatChannelInfo &channel = channelsToUpdate[i];
for (int j = 0; j < channelList->topLevelItemCount(); ++j) {
QTreeWidgetItem *twi = channelList->topLevelItem(j);
if (twi->text(0) == channel.getName()) {
twi->setText(1, channel.getDescription());
twi->setText(2, QString::number(channel.getPlayerCount()));
return;
}
}
QTreeWidgetItem *twi = new QTreeWidgetItem(QStringList() << channel.getName() << channel.getDescription() << QString::number(channel.getPlayerCount()));
twi->setTextAlignment(2, Qt::AlignRight);
channelList->addTopLevelItem(twi);
channelList->resizeColumnToContents(0);
channelList->resizeColumnToContents(1);
channelList->resizeColumnToContents(2);
if (channel.getAutoJoin())
joinChannel(channel.getName());
}
}
/*
void ChatWidget::chatEvent(const ChatEventData &data)
{
const QStringList &msg = data.getEventData();
switch (data.getEventType()) {
case eventListChatChannels: {
if (msg.size() != 4)
break;
for (int i = 0; i < channelList->topLevelItemCount(); ++i) {
QTreeWidgetItem *twi = channelList->topLevelItem(i);
if (twi->text(0) == msg[0]) {
twi->setToolTip(0, msg[1]);
twi->setText(1, msg[2]);
return;
}
}
QTreeWidgetItem *twi = new QTreeWidgetItem(QStringList() << msg[0] << msg[2]);
twi->setTextAlignment(1, Qt::AlignRight);
twi->setToolTip(0, msg[1]);
channelList->addTopLevelItem(twi);
channelList->resizeColumnToContents(0);
channelList->resizeColumnToContents(1);
if (msg[3] == "1")
joinChannel(msg[0]);
break;
}
case eventChatJoinChannel: {
if (msg.size() != 2)
break;
ChannelWidget *w = getChannel(msg[0]);
if (!w)
break;
w->joinEvent(msg[1]);
break;
}
case eventChatListPlayers: {
if (msg.size() != 2)
break;
ChannelWidget *w = getChannel(msg[0]);
if (!w)
break;
w->listPlayersEvent(msg[1]);
break;
}
case eventChatLeaveChannel: {
if (msg.size() != 2)
break;
ChannelWidget *w = getChannel(msg[0]);
if (!w)
break;
w->leaveEvent(msg[1]);
break;
}
case eventChatSay: {
if (msg.size() != 3)
break;
ChannelWidget *w = getChannel(msg[0]);
if (!w)
break;
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, true);
tab->addTab(w, "Server");
}
} else
w = getChannel(msg[0]);
w->serverMessageEvent(msg[1]);
break;
}
default: {
}
}
}
*/
void ChatChannelSelector::joinChannel(const QString &channelName)
{
Command_ChatJoinChannel *command = new Command_ChatJoinChannel(channelName);
connect(command, SIGNAL(finished(ResponseCode)), this, SLOT(joinFinished(ResponseCode)));
client->sendCommand(command);
}
void ChatChannelSelector::joinClicked()
{
QTreeWidgetItem *twi = channelList->currentItem();
if (!twi)
return;
QString channelName = twi->text(0);
joinChannel(channelName);
}
void ChatChannelSelector::joinFinished(ResponseCode resp)
{
if (resp != RespOk)
return;
Command_ChatJoinChannel *command = qobject_cast<Command_ChatJoinChannel *>(sender());
QString channelName = command->getChannel();
emit channelJoined(channelName);
}
ServerMessageLog::ServerMessageLog(Client *_client, QWidget *parent)
: QGroupBox(parent)
{
textEdit = new QTextEdit;
textEdit->setReadOnly(true);
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(textEdit);
setLayout(vbox);
retranslateUi();
connect(_client, SIGNAL(serverMessageEventReceived(Event_ServerMessage *)), this, SLOT(processServerMessageEvent(Event_ServerMessage *)));
}
void ServerMessageLog::retranslateUi()
{
setTitle(tr("Server messages"));
}
void ServerMessageLog::processServerMessageEvent(Event_ServerMessage *event)
{
textEdit->append(event->getMessage());
}
TabServer::TabServer(Client *_client, QWidget *parent)
: QWidget(parent), client(_client)
{
gameSelector = new GameSelector(client);
chatChannelSelector = new ChatChannelSelector(client);
serverMessageLog = new ServerMessageLog(client);
connect(gameSelector, SIGNAL(gameJoined(int)), this, SIGNAL(gameJoined(int)));
connect(chatChannelSelector, SIGNAL(channelJoined(const QString &)), this, SIGNAL(chatChannelJoined(const QString &)));
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addWidget(chatChannelSelector);
hbox->addWidget(serverMessageLog);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(gameSelector);
mainLayout->addLayout(hbox);
setLayout(mainLayout);
}