finished getGamesOfUser function

This commit is contained in:
Max-Wilhelm Bruker 2011-07-02 16:43:19 +02:00
parent d5de76ec4a
commit abd5425796
24 changed files with 1221 additions and 1051 deletions

View file

@ -1,8 +1,8 @@
#include "gamesmodel.h"
#include "protocol_datastructures.h"
GamesModel::GamesModel(const QMap<int, QString> &_gameTypes, QObject *parent)
: QAbstractTableModel(parent), gameTypes(_gameTypes)
GamesModel::GamesModel(const QMap<int, QString> &_rooms, const QMap<int, GameTypeMap> &_gameTypes, QObject *parent)
: QAbstractTableModel(parent), rooms(_rooms), gameTypes(_gameTypes)
{
}
@ -30,17 +30,19 @@ QVariant GamesModel::data(const QModelIndex &index, int role) const
ServerInfo_Game *g = gameList[index.row()];
switch (index.column()) {
case 0: return g->getDescription();
case 1: return g->getCreatorInfo()->getName();
case 2: {
case 0: return rooms.value(g->getRoomId());
case 1: return g->getDescription();
case 2: return g->getCreatorInfo()->getName();
case 3: {
QStringList result;
QList<GameTypeId *> gameTypeList = g->getGameTypes();
GameTypeMap gameTypeMap = gameTypes.value(g->getRoomId());
for (int i = 0; i < gameTypeList.size(); ++i)
result.append(gameTypes.value(gameTypeList[i]->getData()));
result.append(gameTypeMap.value(gameTypeList[i]->getData()));
return result.join(", ");
}
case 3: return g->getHasPassword() ? (g->getSpectatorsNeedPassword() ? tr("yes") : tr("yes, free for spectators")) : tr("no");
case 4: {
case 4: return g->getHasPassword() ? (g->getSpectatorsNeedPassword() ? tr("yes") : tr("yes, free for spectators")) : tr("no");
case 5: {
QStringList result;
if (g->getOnlyBuddies())
result.append(tr("buddies only"));
@ -48,8 +50,8 @@ QVariant GamesModel::data(const QModelIndex &index, int role) const
result.append(tr("reg. users only"));
return result.join(", ");
}
case 5: return QString("%1/%2").arg(g->getPlayerCount()).arg(g->getMaxPlayers());
case 6: return g->getSpectatorsAllowed() ? QVariant(g->getSpectatorCount()) : QVariant(tr("not allowed"));
case 6: return QString("%1/%2").arg(g->getPlayerCount()).arg(g->getMaxPlayers());
case 7: return g->getSpectatorsAllowed() ? QVariant(g->getSpectatorCount()) : QVariant(tr("not allowed"));
default: return QVariant();
}
}
@ -59,13 +61,14 @@ QVariant GamesModel::headerData(int section, Qt::Orientation orientation, int ro
if ((role != Qt::DisplayRole) || (orientation != Qt::Horizontal))
return QVariant();
switch (section) {
case 0: return tr("Description");
case 1: return tr("Creator");
case 2: return tr("Game type");
case 3: return tr("Password");
case 4: return tr("Restrictions");
case 5: return tr("Players");
case 6: return tr("Spectators");
case 0: return tr("Room");
case 1: return tr("Description");
case 2: return tr("Creator");
case 3: return tr("Game type");
case 4: return tr("Password");
case 5: return tr("Restrictions");
case 6: return tr("Players");
case 7: return tr("Spectators");
default: return QVariant();
}
}
@ -82,7 +85,7 @@ void GamesModel::updateGameList(ServerInfo_Game *_game)
for (int i = 0; i < oldGameTypeList.size(); ++i)
gameTypeList.append(new GameTypeId(oldGameTypeList[i]->getData()));
ServerInfo_Game *game = new ServerInfo_Game(_game->getGameId(), _game->getDescription(), _game->getHasPassword(), _game->getPlayerCount(), _game->getMaxPlayers(), gameTypeList, new ServerInfo_User(_game->getCreatorInfo()), _game->getOnlyBuddies(), _game->getOnlyRegistered(), _game->getSpectatorsAllowed(), _game->getSpectatorsNeedPassword(), _game->getSpectatorCount());
ServerInfo_Game *game = new ServerInfo_Game(_game->getRoomId(), _game->getGameId(), _game->getDescription(), _game->getHasPassword(), _game->getPlayerCount(), _game->getMaxPlayers(), gameTypeList, new ServerInfo_User(_game->getCreatorInfo()), _game->getOnlyBuddies(), _game->getOnlyRegistered(), _game->getSpectatorsAllowed(), _game->getSpectatorsNeedPassword(), _game->getSpectatorCount());
for (int i = 0; i < gameList.size(); i++)
if (gameList[i]->getGameId() == game->getGameId()) {
if (game->getPlayerCount() == 0) {
@ -92,7 +95,7 @@ void GamesModel::updateGameList(ServerInfo_Game *_game)
} else {
delete gameList[i];
gameList[i] = game;
emit dataChanged(index(i, 0), index(i, 4));
emit dataChanged(index(i, 0), index(i, 7));
}
return;
}

View file

@ -4,6 +4,7 @@
#include <QAbstractTableModel>
#include <QSortFilterProxyModel>
#include <QList>
#include "gametypemap.h"
class ServerInfo_Game;
@ -11,12 +12,13 @@ class GamesModel : public QAbstractTableModel {
Q_OBJECT
private:
QList<ServerInfo_Game *> gameList;
QMap<int, QString> gameTypes;
QMap<int, QString> rooms;
QMap<int, GameTypeMap> gameTypes;
public:
GamesModel(const QMap<int, QString> &_gameTypes, QObject *parent = 0);
GamesModel(const QMap<int, QString> &_rooms, const QMap<int, GameTypeMap> &_gameTypes, QObject *parent = 0);
~GamesModel();
int rowCount(const QModelIndex &parent = QModelIndex()) const { return parent.isValid() ? 0 : gameList.size(); }
int columnCount(const QModelIndex &/*parent*/ = QModelIndex()) const { return 7; }
int columnCount(const QModelIndex &/*parent*/ = QModelIndex()) const { return 8; }
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;

View file

@ -4,127 +4,17 @@
#include <QMenu>
#include <QAction>
#include <QPushButton>
#include <QHeaderView>
#include <QMessageBox>
#include <QCheckBox>
#include <QInputDialog>
#include <QLabel>
#include <QSplitter>
#include "dlg_creategame.h"
#include "tab_supervisor.h"
#include "tab_room.h"
#include "userlist.h"
#include "abstractclient.h"
#include "protocol_items.h"
#include "gamesmodel.h"
#include "chatview.h"
GameSelector::GameSelector(AbstractClient *_client, TabRoom *_room, QWidget *parent)
: QGroupBox(parent), client(_client), room(_room)
{
gameListView = new QTreeView;
gameListModel = new GamesModel(room->getGameTypes(), this);
gameListProxyModel = new GamesProxyModel(this);
gameListProxyModel->setSourceModel(gameListModel);
gameListProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
gameListView->setModel(gameListProxyModel);
gameListView->header()->setResizeMode(0, QHeaderView::ResizeToContents);
gameListView->setSortingEnabled(true);
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((qreal) (gameListView->columnWidth(0) * gameListModel->columnCount()) / 1.5);
setMinimumHeight(200);
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()));
}
void GameSelector::showFullGamesChanged(int state)
{
gameListProxyModel->setFullGamesVisible(state);
}
void GameSelector::actCreate()
{
DlgCreateGame dlg(client, room->getRoomId(), room->getGameTypes(), this);
dlg.exec();
}
void GameSelector::checkResponse(ResponseCode response)
{
createButton->setEnabled(true);
joinButton->setEnabled(true);
spectateButton->setEnabled(true);
switch (response) {
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 RespGameFull: 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;
case RespUserLevelTooLow: QMessageBox::critical(this, tr("Error"), tr("This game is only open to registered users.")); break;
case RespOnlyBuddies: QMessageBox::critical(this, tr("Error"), tr("This game is only open to its creator's buddies.")); break;
case RespInIgnoreList: QMessageBox::critical(this, tr("Error"), tr("You are being ignored by the creator of this game.")); break;
default: ;
}
}
void GameSelector::actJoin()
{
bool spectator = sender() == spectateButton;
QModelIndex ind = gameListView->currentIndex();
if (!ind.isValid())
return;
ServerInfo_Game *game = gameListModel->getGame(ind.data(Qt::UserRole).toInt());
QString password;
if (game->getHasPassword() && !(spectator && !game->getSpectatorsNeedPassword())) {
bool ok;
password = QInputDialog::getText(this, tr("Join game"), tr("Password:"), QLineEdit::Password, QString(), &ok);
if (!ok)
return;
}
Command_JoinGame *commandJoinGame = new Command_JoinGame(room->getRoomId(), 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::processGameInfo(ServerInfo_Game *info)
{
gameListModel->updateGameList(info);
}
#include "gameselector.h"
TabRoom::TabRoom(TabSupervisor *_tabSupervisor, AbstractClient *_client, const QString &_ownName, ServerInfo_Room *info)
: Tab(_tabSupervisor), client(_client), roomId(info->getRoomId()), roomName(info->getName()), ownName(_ownName)
@ -133,7 +23,9 @@ TabRoom::TabRoom(TabSupervisor *_tabSupervisor, AbstractClient *_client, const Q
for (int i = 0; i < gameTypeList.size(); ++i)
gameTypes.insert(gameTypeList[i]->getGameTypeId(), gameTypeList[i]->getDescription());
gameSelector = new GameSelector(client, this);
QMap<int, GameTypeMap> tempMap;
tempMap.insert(info->getRoomId(), gameTypes);
gameSelector = new GameSelector(client, this, QMap<int, QString>(), tempMap);
userList = new UserList(tabSupervisor, client, UserList::RoomList);
connect(userList, SIGNAL(openMessageDialog(const QString &, bool)), this, SIGNAL(openMessageDialog(const QString &, bool)));

View file

@ -2,20 +2,16 @@
#define TAB_ROOM_H
#include "tab.h"
#include "protocol_datastructures.h"
#include <QGroupBox>
#include <QMap>
class AbstractClient;
class UserList;
class QLabel;
class ChatView;
class QLineEdit;
class QTreeView;
class QPushButton;
class QTextTable;
class QCheckBox;
class GamesModel;
class GamesProxyModel;
class RoomEvent;
class ServerInfo_Room;
class ServerInfo_Game;
@ -24,31 +20,7 @@ class Event_JoinRoom;
class Event_LeaveRoom;
class Event_RoomSay;
class ProtocolResponse;
class TabRoom;
class GameSelector : public QGroupBox {
Q_OBJECT
private slots:
void showFullGamesChanged(int state);
void actCreate();
void actJoin();
void checkResponse(ResponseCode response);
signals:
void gameJoined(int gameId);
private:
AbstractClient *client;
TabRoom *room;
QTreeView *gameListView;
GamesModel *gameListModel;
GamesProxyModel *gameListProxyModel;
QPushButton *createButton, *joinButton, *spectateButton;
QCheckBox *showFullGamesCheckBox;
public:
GameSelector(AbstractClient *_client, TabRoom *_room, QWidget *parent = 0);
void retranslateUi();
void processGameInfo(ServerInfo_Game *info);
};
class GameSelector;
class TabRoom : public Tab {
Q_OBJECT

View file

@ -5,6 +5,7 @@
#include "pixmapgenerator.h"
#include "userinfobox.h"
#include "protocol_items.h"
#include "gameselector.h"
#include <QHeaderView>
#include <QVBoxLayout>
#include <QMouseEvent>
@ -210,6 +211,35 @@ void UserList::userClicked(QTreeWidgetItem *item, int /*column*/)
emit openMessageDialog(item->data(2, Qt::UserRole).toString(), true);
}
void UserList::gamesOfUserReceived(ProtocolResponse *resp)
{
Command_GetGamesOfUser *command = static_cast<Command_GetGamesOfUser *>(sender());
Response_GetGamesOfUser *response = qobject_cast<Response_GetGamesOfUser *>(resp);
if (!response)
return;
QMap<int, GameTypeMap> gameTypeMap;
QMap<int, QString> roomMap;
const QList<ServerInfo_Room *> roomList = response->getRoomList();
for (int i = 0; i < roomList.size(); ++i) {
roomMap.insert(roomList[i]->getRoomId(), roomList[i]->getName());
const QList<ServerInfo_GameType *> gameTypeList = roomList[i]->getGameTypeList();
GameTypeMap tempMap;
for (int j = 0; j < gameTypeList.size(); ++j)
tempMap.insert(gameTypeList[j]->getGameTypeId(), gameTypeList[j]->getDescription());
gameTypeMap.insert(roomList[i]->getRoomId(), tempMap);
}
GameSelector *selector = new GameSelector(client, 0, roomMap, gameTypeMap);
const QList<ServerInfo_Game *> gameList = response->getGameList();
for (int i = 0; i < gameList.size(); ++i)
selector->processGameInfo(gameList[i]);
selector->setWindowTitle(tr("%1's games").arg(command->getUserName()));
selector->setAttribute(Qt::WA_DeleteOnClose);
selector->show();
}
void UserList::showContextMenu(const QPoint &pos, const QModelIndex &index)
{
const QString &userName = index.sibling(index.row(), 2).data(Qt::UserRole).toString();
@ -230,6 +260,7 @@ void UserList::showContextMenu(const QPoint &pos, const QModelIndex &index)
menu->addAction(aUserName);
menu->addSeparator();
menu->addAction(aDetails);
menu->addAction(aShowGames);
menu->addAction(aChat);
if ((userLevel & ServerInfo_User::IsRegistered) && (tabSupervisor->getUserLevel() & ServerInfo_User::IsRegistered)) {
menu->addSeparator();
@ -260,7 +291,7 @@ void UserList::showContextMenu(const QPoint &pos, const QModelIndex &index)
client->sendCommand(new Command_RemoveFromList("buddy", userName));
else if (actionClicked == aShowGames) {
Command *cmd = new Command_GetGamesOfUser(userName);
connect(cmd, SIGNAL(responseReceived(ProtocolResponse *)), this, SLOT(gamesOfUserReceived(ProtocolResponse *)));
connect(cmd, SIGNAL(finished(ProtocolResponse *)), this, SLOT(gamesOfUserReceived(ProtocolResponse *)));
client->sendCommand(cmd);
} else if (actionClicked == aAddToIgnoreList)
client->sendCommand(new Command_AddToList("ignore", userName));

View file

@ -12,6 +12,7 @@ class AbstractClient;
class TabSupervisor;
class QSpinBox;
class QPlainTextEdit;
class ProtocolResponse;
class BanDialog : public QDialog {
Q_OBJECT
@ -52,6 +53,7 @@ private:
void setUserOnline(QTreeWidgetItem *user, bool online);
private slots:
void userClicked(QTreeWidgetItem *item, int column);
void gamesOfUserReceived(ProtocolResponse *resp);
signals:
void openMessageDialog(const QString &userName, bool focus);
void addBuddy(const QString &userName);