This commit is contained in:
Max-Wilhelm Bruker 2009-11-25 22:58:20 +01:00
parent 6c93b1e9b7
commit 0d4717f40b
28 changed files with 591 additions and 375 deletions

View file

@ -99,170 +99,6 @@ void Client::readData()
}
}
}
/*
while (socket->canReadLine()) {
QString line = QString(socket->readLine()).trimmed();
if (line.isNull())
break;
qDebug(QString("<< %1").arg(line).toLatin1());
QStringList values = line.split("|");
QString prefix = values.takeFirst();
// prefix is one of {welcome, private, public, resp, list_games, list_players, list_counters, list_zones, dump_zone}
if ((prefix == "private") || (prefix == "public")) {
ServerEventData event(line);
emit gameEvent(event);
} else if (prefix == "chat") {
emit chatEvent(ChatEventData(line));
} else if (prefix == "resp") {
if (values.size() != 2) {
qDebug("Client::parseCommand: Invalid response");
continue;
}
bool ok;
int msgid = values.takeFirst().toInt(&ok);
PendingCommand *pc = pendingCommands.value(msgid, 0);
if (!ok || !pc) {
qDebug("Client::parseCommand: Invalid msgid");
continue;
}
ServerResponse resp;
if (values[0] == "ok")
resp = RespOk;
else if (values[0] == "name_not_found")
resp = RespNameNotFound;
else if (values[0] == "login_needed")
resp = RespLoginNeeded;
else if (values[0] == "syntax")
resp = RespSyntaxError;
else if (values[0] == "context")
resp = RespContextError;
else if (values[0] == "password")
resp = RespPasswordWrong;
else if (values[0] == "spectators_not_allowed")
resp = RespSpectatorsNotAllowed;
else
resp = RespInvalid;
pc->responseReceived(resp);
} else if (prefix == "list_games") {
if (values.size() != 8) {
emit protocolError();
continue;
}
emit gameListEvent(ServerGame(values[0].toInt(), values[5], values[1], values[2].toInt(), values[3].toInt(), values[4].toInt(), values[6].toInt(), values[7].toInt()));
} else if (prefix == "welcome") {
if (values.size() != 2) {
emit protocolError();
disconnectFromServer();
} else if (values[0].toInt() != protocolVersion) {
emit protocolVersionMismatch();
disconnectFromServer();
} else {
emit welcomeMsgReceived(values[1]);
setStatus(StatusLoggingIn);
login(playerName, password);
}
} else if (prefix == "list_players") {
if (values.size() != 4) {
emit protocolError();
continue;
}
int cmdid = values.takeFirst().toInt();
PendingCommand *pc = pendingCommands.value(cmdid, 0);
ServerPlayer sp(values[0].toInt(), values[1], values[2].toInt());
PendingCommand_ListPlayers *pcLP = qobject_cast<PendingCommand_ListPlayers *>(pc);
if (pcLP)
pcLP->addPlayer(sp);
else {
PendingCommand_DumpAll *pcDA = qobject_cast<PendingCommand_DumpAll *>(pc);
if (pcDA)
pcDA->addPlayer(sp);
else
emit protocolError();
}
} else if (prefix == "dump_zone") {
if (values.size() != 11) {
emit protocolError();
continue;
}
int cmdid = values.takeFirst().toInt();
PendingCommand *pc = pendingCommands.value(cmdid, 0);
ServerZoneCard szc(values[0].toInt(), values[1], values[2].toInt(), values[3], values[4].toInt(), values[5].toInt(), values[6].toInt(), values[7] == "1", values[8] == "1", values[9]);
PendingCommand_DumpZone *pcDZ = qobject_cast<PendingCommand_DumpZone *>(pc);
if (pcDZ)
pcDZ->addCard(szc);
else {
PendingCommand_DumpAll *pcDA = qobject_cast<PendingCommand_DumpAll *>(pc);
if (pcDA)
pcDA->addCard(szc);
else
emit protocolError();
}
} else if (prefix == "list_zones") {
if (values.size() != 6) {
emit protocolError();
continue;
}
int cmdid = values.takeFirst().toInt();
PendingCommand *pc = pendingCommands.value(cmdid, 0);
ServerZone::ZoneType type;
if (values[2] == "private")
type = ServerZone::PrivateZone;
else if (values[2] == "hidden")
type = ServerZone::HiddenZone;
else
type = ServerZone::PublicZone;
ServerZone sz(values[0].toInt(), values[1], type, values[3] == "1", values[4].toInt());
PendingCommand_ListZones *pcLZ = qobject_cast<PendingCommand_ListZones *>(pc);
if (pcLZ)
pcLZ->addZone(sz);
else {
PendingCommand_DumpAll *pcDA = qobject_cast<PendingCommand_DumpAll *>(pc);
if (pcDA)
pcDA->addZone(sz);
else
emit protocolError();
}
} else if (prefix == "list_counters") {
if (values.size() != 7) {
emit protocolError();
continue;
}
int cmdid = values.takeFirst().toInt();
PendingCommand *pc = pendingCommands.value(cmdid, 0);
ServerCounter sc(values[0].toInt(), values[1].toInt(), values[2], numberToColor(values[3].toInt()), values[4].toInt(), values[5].toInt());
PendingCommand_ListCounters *pcLC = qobject_cast<PendingCommand_ListCounters *>(pc);
if (pcLC)
pcLC->addCounter(sc);
else {
PendingCommand_DumpAll *pcDA = qobject_cast<PendingCommand_DumpAll *>(pc);
if (pcDA)
pcDA->addCounter(sc);
else
emit protocolError();
}
} else if (prefix == "list_arrows") {
if (values.size() != 10) {
emit protocolError();
continue;
}
int cmdid = values.takeFirst().toInt();
PendingCommand *pc = pendingCommands.value(cmdid, 0);
ServerArrow sa(values[0].toInt(), values[1].toInt(), values[2].toInt(), values[3], values[4].toInt(), values[5].toInt(), values[6], values[7].toInt(), numberToColor(values[8].toInt()));
PendingCommand_DumpAll *pcDA = qobject_cast<PendingCommand_DumpAll *>(pc);
if (pcDA)
pcDA->addArrow(sa);
else
emit protocolError();
} else
emit protocolError();
}
*/
}
void Client::processProtocolItem(ProtocolItem *item)
@ -365,14 +201,3 @@ void Client::ping()
} else
sendCommand(new Command_Ping);
}
/*
QColor Client::numberToColor(int colorValue) const
{
return QColor(colorValue / 65536, (colorValue % 65536) / 256, colorValue % 256);
}
int Client::colorToNumber(const QColor &color) const
{
return color.red() * 65536 + color.green() * 256 + color.blue();
}
*/

View file

@ -128,25 +128,6 @@ void Game::retranslateUi()
i.next().value()->retranslateUi();
}
Player *Game::addPlayer(int playerId, const QString &playerName, bool local)
{
Player *newPlayer = new Player(playerName, playerId, local, client, this);
scene->addPlayer(newPlayer);
connect(newPlayer, SIGNAL(sigShowCardMenu(QPoint)), this, SLOT(showCardMenu(QPoint)));
connect(newPlayer, SIGNAL(logMoveCard(Player *, QString, CardZone *, int, CardZone *, int)), this, SIGNAL(logMoveCard(Player *, QString, CardZone *, int, CardZone *, int)));
connect(newPlayer, SIGNAL(logCreateToken(Player *, QString)), this, SIGNAL(logCreateToken(Player *, QString)));
connect(newPlayer, SIGNAL(logSetCardCounters(Player *, QString, int, int)), this, SIGNAL(logSetCardCounters(Player *, QString, int, int)));
connect(newPlayer, SIGNAL(logSetTapped(Player *, QString, bool)), this, SIGNAL(logSetTapped(Player *, QString, bool)));
connect(newPlayer, SIGNAL(logSetCounter(Player *, QString, int, int)), this, SIGNAL(logSetCounter(Player *, QString, int, int)));
connect(newPlayer, SIGNAL(logCreateArrow(Player *, Player *, QString, Player *, QString)), this, SIGNAL(logCreateArrow(Player *, Player *, QString, Player *, QString)));
connect(newPlayer, SIGNAL(logSetDoesntUntap(Player *, QString, bool)), this, SIGNAL(logSetDoesntUntap(Player *, QString, bool)));
players.insert(playerId, newPlayer);
emit playerAdded(newPlayer);
return newPlayer;
}
/*
void Game::cardListReceived(QList<ServerZoneCard> list)
{
@ -570,4 +551,4 @@ Player *Game::getActiveLocalPlayer() const
return p;
}
return 0;
}
}

View file

@ -103,7 +103,6 @@ public:
void retranslateUi();
void restartGameDialog();
void hoverCardEvent(CardItem *card);
Player *addPlayer(int playerId, const QString &playerName, bool local);
Player *getActiveLocalPlayer() const;
const QMap<int, Player *> &getPlayers() const { return players; }
void queryGameState();

View file

@ -275,9 +275,9 @@ void MessageLogWidget::logSetActivePhase(int phase)
append("<font color=\"green\">" + tr("It is now the %1.").arg(phaseName) + "</font>");
}
void MessageLogWidget::connectToGame(Game *game)
void MessageLogWidget::connectToPlayer(Player *player)
{
connect(game, SIGNAL(logPlayerListReceived(QStringList)), this, SLOT(logPlayerListReceived(QStringList)));
/* connect(game, SIGNAL(logPlayerListReceived(QStringList)), this, SLOT(logPlayerListReceived(QStringList)));
connect(game, SIGNAL(logJoin(Player *)), this, SLOT(logJoin(Player *)));
connect(game, SIGNAL(logLeave(Player *)), this, SLOT(logLeave(Player *)));
connect(game, SIGNAL(logGameClosed()), this, SLOT(logGameClosed()));
@ -310,7 +310,7 @@ void MessageLogWidget::connectToGame(Game *game)
connect(game, SIGNAL(logDraw(Player *, int)), this, SLOT(msgAlert()));
connect(game, SIGNAL(logMoveCard(Player *, QString, CardZone *, int, CardZone *, int)), this, SLOT(msgAlert()));
connect(game, SIGNAL(logGameStart()), this, SLOT(msgAlert()));
}
*/}
void MessageLogWidget::msgAlert()
{

View file

@ -6,7 +6,6 @@
#include "translation.h"
#include "protocol_datastructures.h"
class Game;
class Player;
class CardZone;
@ -49,7 +48,7 @@ private slots:
void logSetActivePhase(int phase);
void msgAlert();
public:
void connectToGame(Game *game);
void connectToPlayer(Player *player);
MessageLogWidget(QWidget *parent = 0);
};

View file

@ -5,16 +5,16 @@
#include "arrowitem.h"
#include "zoneviewzone.h"
#include "zoneviewwidget.h"
#include "game.h"
#include "pilezone.h"
#include "tablezone.h"
#include "handzone.h"
#include "cardlist.h"
#include "tab_game.h"
#include <QSettings>
#include <QPainter>
#include <QMenu>
Player::Player(const QString &_name, int _id, bool _local, Client *_client, Game *_parent)
Player::Player(const QString &_name, int _id, bool _local, Client *_client, TabGame *_parent)
: QObject(_parent), defaultNumberTopCards(3), name(_name), id(_id), active(false), local(_local), client(_client)
{
QSettings settings;
@ -697,6 +697,10 @@ void Player::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/
painter->drawText(QRectF(0, 0, totalWidth, 40), Qt::AlignCenter, nameStr);
}
void Player::processPlayerInfo(ServerInfo_Player *info)
{
}
void Player::addCounter(int counterId, const QString &name, QColor color, int radius, int value)
{
Counter *c = new Counter(this, counterId, name, color, radius, value, this);

View file

@ -12,12 +12,13 @@ class CardDatabase;
class QMenu;
class QAction;
class ZoneViewZone;
class Game;
class TabGame;
class Counter;
class ArrowItem;
class CardZone;
class TableZone;
class HandZone;
class ServerInfo_Player;
class Player : public QObject, public QGraphicsItem {
Q_OBJECT
@ -103,7 +104,7 @@ public:
Client *client;
void addZone(CardZone *z);
Player(const QString &_name, int _id, bool _local, Client *_client, Game *_parent);
Player(const QString &_name, int _id, bool _local, Client *_client, TabGame *_parent);
~Player();
void retranslateUi();
QMenu *getPlayerMenu() const { return playerMenu; }
@ -117,6 +118,8 @@ public:
void showCardMenu(const QPoint &p);
bool getActive() const { return active; }
void setActive(bool _active);
void processPlayerInfo(ServerInfo_Player *info);
};
#endif

View file

@ -51,7 +51,7 @@ void TabChatChannel::processChatEvent(ChatEvent *event)
void TabChatChannel::processListPlayersEvent(Event_ChatListPlayers *event)
{
const QList<ServerPlayerInfo> &players = event->getPlayerList();
const QList<ServerChatUserInfo> &players = event->getPlayerList();
for (int i = 0; i < players.size(); ++i)
playerList->addItem(players[i].getName());
}

View file

@ -18,7 +18,7 @@
#include "main.h"
TabGame::TabGame(Client *_client, int _gameId)
: client(_client), gameId(_gameId)
: client(_client), gameId(_gameId), localPlayerId(-1)
{
zoneLayout = new ZoneViewLayout;
scene = new GameScene(zoneLayout, this);
@ -38,6 +38,8 @@ TabGame::TabGame(Client *_client, int _gameId)
QVBoxLayout *deckViewLayout = new QVBoxLayout;
deckViewLayout->addLayout(buttonHBox);
deckViewLayout->addWidget(deckView);
deckViewContainer = new QWidget;
deckViewContainer->setLayout(deckViewLayout);
cardInfo = new CardInfoWidget(db);
messageLog = new MessageLogWidget;
@ -60,7 +62,7 @@ TabGame::TabGame(Client *_client, int _gameId)
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(phasesToolbar);
mainLayout->addWidget(gameView, 10);
mainLayout->addLayout(deckViewLayout, 10);
mainLayout->addWidget(deckViewContainer, 10);
mainLayout->addLayout(verticalLayout);
aCloseMostRecentZoneView = new QAction(this);
@ -105,9 +107,44 @@ void TabGame::retranslateUi()
aCloseMostRecentZoneView->setShortcut(tr("Esc"));
}
Player *TabGame::addPlayer(int playerId, const QString &playerName)
{
Player *newPlayer = new Player(playerName, playerId, playerId == localPlayerId, client, this);
scene->addPlayer(newPlayer);
messageLog->connectToPlayer(newPlayer);
players.insert(playerId, newPlayer);
emit playerAdded(newPlayer);
return newPlayer;
}
void TabGame::processGameEvent(GameEvent *event)
{
// game->processGameEvent(event);
switch (event->getItemId()) {
case ItemId_Event_GameStart: eventGameStart(qobject_cast<Event_GameStart *>(event)); break;
default: qDebug() << "unhandled game event";
}
}
void TabGame::processGameJoinedEvent(Event_GameJoined *event)
{
localPlayerId = event->getPlayerId();
spectator = event->getSpectator();
const QList<ServerInfo_Player *> &plList = event->getPlayerList();
for (int i = 0; i < plList.size(); ++i) {
ServerInfo_Player *pl = plList[i];
Player *newPlayer = addPlayer(pl->getPlayerId(), pl->getName());
newPlayer->processPlayerInfo(pl);
}
}
void TabGame::eventGameStart(Event_GameStart *event)
{
deckViewContainer->hide();
gameView->show();
}
void TabGame::loadLocalDeck()

View file

@ -2,14 +2,13 @@
#define TAB_GAME_H
#include <QWidget>
#include <QMap>
class Client;
class CardDatabase;
class GameEvent;
class GameView;
class DeckView;
class GameScene;
class Game;
class CardInfoWidget;
class MessageLogWidget;
class QLabel;
@ -19,12 +18,23 @@ class ZoneViewLayout;
class ZoneViewWidget;
class PhasesToolbar;
class ProtocolResponse;
class GameEvent;
class Event_GameJoined;
class Event_GameStart;
class Player;
class CardZone;
class TabGame : public QWidget {
Q_OBJECT
private:
Client *client;
int gameId;
int localPlayerId;
bool spectator;
QStringList spectatorList;
QMap<int, Player *> players;
bool started;
int currentPhase;
QPushButton *loadLocalButton, *loadRemoteButton, *readyStartButton;
CardInfoWidget *cardInfo;
@ -35,9 +45,41 @@ private:
GameScene *scene;
GameView *gameView;
DeckView *deckView;
Game *game;
QWidget *deckViewContainer;
ZoneViewLayout *zoneLayout;
QAction *aCloseMostRecentZoneView;
Player *addPlayer(int playerId, const QString &playerName);
void eventGameStart(Event_GameStart *event);
signals:
void playerAdded(Player *player);
void playerRemoved(Player *player);
// Log events
void logPlayerListReceived(QStringList players);
void logJoin(Player *player);
void logLeave(Player *player);
void logGameClosed();
void logJoinSpectator(QString playerName);
void logLeaveSpectator(QString playerName);
void logReadyStart(Player *player);
void logGameStart();
void logSay(Player *player, QString text);
void logShuffle(Player *player);
void logRollDie(Player *player, int sides, int roll);
void logDraw(Player *player, int number);
void logMoveCard(Player *player, QString cardName, CardZone *startZone, int oldX, CardZone *targetZone, int newX);
void logCreateToken(Player *player, QString cardName);
void logCreateArrow(Player *player, Player *startPlayer, QString startCard, Player *targetPlayer, QString targetCard);
void logSetCardCounters(Player *player, QString cardName, int value, int oldValue);
void logSetTapped(Player *player, QString cardName, bool tapped);
void logSetCounter(Player *player, QString counterName, int value, int oldValue);
void logSetDoesntUntap(Player *player, QString cardName, bool doesntUntap);
void logDumpZone(Player *player, CardZone *zone, int numberCards);
void logStopDumpZone(Player *player, CardZone *zone);
void logSetActivePlayer(Player *player);
void setActivePhase(int phase);
private slots:
void loadLocalDeck();
void loadRemoteDeck();
@ -46,7 +88,9 @@ private slots:
public:
TabGame(Client *_client, int _gameId);
void retranslateUi();
void processGameEvent(GameEvent *event);
void processGameJoinedEvent(Event_GameJoined *event);
};
#endif

View file

@ -104,6 +104,8 @@ void TabSupervisor::gameJoined(Event_GameJoined *event)
TabGame *tab = new TabGame(client, event->getGameId());
addTab(tab, tr("Game %1").arg(event->getGameId()));
gameTabs.insert(event->getGameId(), tab);
tab->processGameJoinedEvent(event);
setCurrentWidget(tab);
}
void TabSupervisor::addChatChannelTab(const QString &channelName)
@ -111,6 +113,7 @@ void TabSupervisor::addChatChannelTab(const QString &channelName)
TabChatChannel *tab = new TabChatChannel(client, channelName);
addTab(tab, channelName);
chatChannelTabs.insert(channelName, tab);
setCurrentWidget(tab);
}
void TabSupervisor::processChatEvent(ChatEvent *event)

View file

@ -41,7 +41,7 @@ void ZoneViewZone::initializeCards()
}
}
void ZoneViewZone::zoneDumpReceived(QList<ServerZoneCard> cards)
void ZoneViewZone::zoneDumpReceived(QList<ServerInfo_Card> cards)
{
for (int i = 0; i < cards.size(); i++) {
CardItem *card = new CardItem(cards[i].getName(), i, this);

View file

@ -29,7 +29,7 @@ public:
public slots:
void setSortingEnabled(int _sortingEnabled);
private slots:
void zoneDumpReceived(QList<ServerZoneCard> cards);
void zoneDumpReceived(QList<ServerInfo_Card> cards);
protected:
void addCardImpl(CardItem *card, int x, int y);
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;