From 29699418d2042cf40499fe0eb45b9f77c5726b47 Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Fri, 25 Sep 2009 12:56:11 +0200 Subject: [PATCH] removed some unneeded code, counter appearance changed --- cockatrice/cockatrice.pro | 4 - cockatrice/src/carditem.cpp | 7 +- cockatrice/src/cardzone.cpp | 9 +- cockatrice/src/cardzone.h | 5 +- cockatrice/src/client.cpp | 22 +-- cockatrice/src/client.h | 16 ++- cockatrice/src/counter.cpp | 16 ++- cockatrice/src/counter.h | 5 +- cockatrice/src/game.cpp | 60 +++++--- cockatrice/src/game.h | 7 +- cockatrice/src/player.cpp | 230 +++++++++++++++++++----------- cockatrice/src/player.h | 25 ++-- cockatrice/src/playerlist.cpp | 11 -- cockatrice/src/playerlist.h | 12 -- cockatrice/src/zonelist.cpp | 11 -- cockatrice/src/zonelist.h | 12 -- cockatrice/src/zoneviewlayout.cpp | 2 +- servatrice/src/counter.h | 6 +- servatrice/src/serversocket.cpp | 69 ++++----- servatrice/src/serversocket.h | 6 +- 20 files changed, 293 insertions(+), 242 deletions(-) delete mode 100644 cockatrice/src/playerlist.cpp delete mode 100644 cockatrice/src/playerlist.h delete mode 100644 cockatrice/src/zonelist.cpp delete mode 100644 cockatrice/src/zonelist.h diff --git a/cockatrice/cockatrice.pro b/cockatrice/cockatrice.pro index 22a16227..5c222ee9 100644 --- a/cockatrice/cockatrice.pro +++ b/cockatrice/cockatrice.pro @@ -13,14 +13,12 @@ HEADERS += src/counter.h \ src/gamesmodel.h \ src/client.h \ src/window_main.h \ - src/zonelist.h \ src/cardzone.h \ src/player.h \ src/cardlist.h \ src/carditem.h \ src/tablezone.h \ src/handzone.h \ - src/playerlist.h \ src/game.h \ src/carddatabase.h \ src/gameview.h \ @@ -53,12 +51,10 @@ SOURCES += src/counter.cpp \ src/gamesmodel.cpp \ src/player.cpp \ src/cardzone.cpp \ - src/zonelist.cpp \ src/cardlist.cpp \ src/carditem.cpp \ src/tablezone.cpp \ src/handzone.cpp \ - src/playerlist.cpp \ src/game.cpp \ src/carddatabase.cpp \ src/gameview.cpp \ diff --git a/cockatrice/src/carditem.cpp b/cockatrice/src/carditem.cpp index ccb21a22..48415bb7 100644 --- a/cockatrice/src/carditem.cpp +++ b/cockatrice/src/carditem.cpp @@ -245,10 +245,11 @@ void CardItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) if (!zone->getPlayer()->getLocal()) return; - if (zone->getName() == "table") - ((TableZone *) zone)->toggleTapped(); + TableZone *tz = qobject_cast(zone); + if (tz) + tz->toggleTapped(); else { - TableZone *table = (TableZone *) zone->getPlayer()->getZones().findZone("table"); + TableZone *table = zone->getPlayer()->getTable(); QPoint gridPoint = table->getFreeGridPoint(info->getTableRow()); table->handleDropEventByGrid(id, zone, gridPoint, false); } diff --git a/cockatrice/src/cardzone.cpp b/cockatrice/src/cardzone.cpp index 524635a3..525d7fb4 100644 --- a/cockatrice/src/cardzone.cpp +++ b/cockatrice/src/cardzone.cpp @@ -132,13 +132,12 @@ void CardZone::setCardAttr(int cardId, const QString &aname, const QString &aval player->client->setCardAttr(name, cardId, aname, avalue); } -void CardZone::setView(ZoneViewZone *_view) +void CardZone::moveAllToZone() { - view = _view; -} + QList data = static_cast(sender())->data().toList(); + QString targetZone = data[0].toString(); + int targetX = data[1].toInt(); -void CardZone::moveAllToZone(const QString &targetZone, int targetX) -{ // Cards need to be moved in reverse order so that the other // cards' list index doesn't change for (int i = cards.size() - 1; i >= 0; i--) diff --git a/cockatrice/src/cardzone.h b/cockatrice/src/cardzone.h index 4c480d0f..f81f1a75 100644 --- a/cockatrice/src/cardzone.h +++ b/cockatrice/src/cardzone.h @@ -29,6 +29,8 @@ protected: virtual void addCardImpl(CardItem *card, int x, int y) = 0; signals: void contentsChanged(); +public slots: + void moveAllToZone(); public: enum { Type = typeZone }; int type() const { return Type; } @@ -52,9 +54,8 @@ public: virtual CardItem *takeCard(int position, int cardId, const QString &cardName, bool canResize = true); void setCardAttr(int cardId, const QString &aname, const QString &avalue); ZoneViewZone *getView() const { return view; } - void setView(ZoneViewZone *_view); + void setView(ZoneViewZone *_view) { view = _view; } virtual void reorganizeCards() = 0; - void moveAllToZone(const QString &targetZone, int targetX); virtual QPointF closestGridPoint(const QPointF &point); }; diff --git a/cockatrice/src/client.cpp b/cockatrice/src/client.cpp index 5885f249..f17c1c25 100644 --- a/cockatrice/src/client.cpp +++ b/cockatrice/src/client.cpp @@ -314,14 +314,14 @@ void Client::readLine() emit protocolError(); } } else if (prefix == "list_counters") { - if (values.size() != 5) { + if (values.size() != 6) { emit protocolError(); continue; } int cmdid = values.takeFirst().toInt(); PendingCommand *pc = pendingCommands.value(cmdid, 0); - int colorValue = values[2].toInt(); - ServerCounter sc(values[0].toInt(), values[1], QColor(colorValue / 65536, (colorValue % 65536) / 256, colorValue % 256), values[3].toInt()); + int colorValue = values[3].toInt(); + ServerCounter sc(values[0].toInt(), values[1].toInt(), values[2], QColor(colorValue / 65536, (colorValue % 65536) / 256, colorValue % 256), values[4].toInt(), values[5].toInt()); PendingCommand_ListCounters *pcLC = qobject_cast(pc); if (pcLC) @@ -508,24 +508,24 @@ PendingCommand *Client::readyStart() return cmd("ready_start"); } -PendingCommand *Client::incCounter(const QString &counter, int delta) +PendingCommand *Client::incCounter(int counterId, int delta) { - return cmd(QString("inc_counter|%1|%2").arg(counter).arg(delta)); + return cmd(QString("inc_counter|%1|%2").arg(counterId).arg(delta)); } -PendingCommand *Client::addCounter(const QString &counter, QColor color, int value) +PendingCommand *Client::addCounter(const QString &counterName, QColor color, int radius, int value) { - return cmd(QString("add_counter|%1|%2|%3").arg(counter).arg(color.red() * 65536 + color.green() * 256 + color.blue()).arg(value)); + return cmd(QString("add_counter|%1|%2|%3|%4").arg(counterName).arg(color.red() * 65536 + color.green() * 256 + color.blue()).arg(radius).arg(value)); } -PendingCommand *Client::setCounter(const QString &counter, int value) +PendingCommand *Client::setCounter(int counterId, int value) { - return cmd(QString("set_counter|%1|%2").arg(counter).arg(value)); + return cmd(QString("set_counter|%1|%2").arg(counterId).arg(value)); } -PendingCommand *Client::delCounter(const QString &counter) +PendingCommand *Client::delCounter(int counterId) { - return cmd(QString("del_counter|%1").arg(counter)); + return cmd(QString("del_counter|%1").arg(counterId)); } PendingCommand_ListCounters *Client::listCounters(int playerId) diff --git a/cockatrice/src/client.h b/cockatrice/src/client.h index 2ae447d8..b9665b34 100644 --- a/cockatrice/src/client.h +++ b/cockatrice/src/client.h @@ -173,15 +173,19 @@ public: class ServerCounter { private: int playerId; + int id; QString name; QColor color; + int radius; int count; public: - ServerCounter(int _playerId, const QString &_name, QColor _color, int _count) - : playerId(_playerId), name(_name), color(_color), count(_count) { } + ServerCounter(int _playerId, int _id, const QString &_name, QColor _color, int _radius, int _count) + : playerId(_playerId), id(_id), name(_name), color(_color), radius(_radius), count(_count) { } int getPlayerId() const { return playerId; } + int getId() const { return id; } QString getName() const { return name; } QColor getColor() const { return color; } + int getRadius() const { return radius; } int getCount() const { return count; } }; @@ -355,10 +359,10 @@ public slots: PendingCommand *createToken(const QString &zone, const QString &name, const QString &powtough, int x, int y); PendingCommand *setCardAttr(const QString &zone, int cardid, const QString &aname, const QString &avalue); PendingCommand *readyStart(); - PendingCommand *incCounter(const QString &counter, int delta); - PendingCommand *addCounter(const QString &counter, QColor color, int value); - PendingCommand *setCounter(const QString &counter, int value); - PendingCommand *delCounter(const QString &counter); + PendingCommand *incCounter(int counterId, int delta); + PendingCommand *addCounter(const QString &counterName, QColor color, int radius, int value); + PendingCommand *setCounter(int counterId, int value); + PendingCommand *delCounter(int counterId); PendingCommand_ListCounters *listCounters(int playerId); PendingCommand *nextTurn(); PendingCommand *setActivePhase(int phase); diff --git a/cockatrice/src/counter.cpp b/cockatrice/src/counter.cpp index c6d7c8b2..a4bb456c 100644 --- a/cockatrice/src/counter.cpp +++ b/cockatrice/src/counter.cpp @@ -3,14 +3,16 @@ #include "client.h" #include -Counter::Counter(Player *_player, const QString &_name, QColor _color, int _value, QGraphicsItem *parent) - : QGraphicsItem(parent), name(_name), color(_color), value(_value), player(_player) +Counter::Counter(Player *_player, int _id, const QString &_name, QColor _color, int _radius, int _value, QGraphicsItem *parent) + : QGraphicsItem(parent), id(_id), name(_name), color(_color), radius(_radius), value(_value), player(_player) { + if (radius > Player::counterAreaWidth / 2) + radius = Player::counterAreaWidth / 2; } QRectF Counter::boundingRect() const { - return QRectF(0, 0, 40, 40); + return QRectF(0, 0, radius * 2, radius * 2); } void Counter::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/) @@ -18,8 +20,8 @@ void Counter::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option* painter->setBrush(QBrush(color)); painter->drawEllipse(boundingRect()); if (value) { - QFont f("Times"); - f.setPixelSize(20); + QFont f("Serif"); + f.setPixelSize(radius * 0.8); f.setWeight(QFont::Bold); painter->setFont(f); painter->drawText(boundingRect(), Qt::AlignCenter, QString::number(value)); @@ -35,7 +37,7 @@ void Counter::setValue(int _value) void Counter::mousePressEvent(QGraphicsSceneMouseEvent *event) { if (event->button() == Qt::LeftButton) - player->client->incCounter(name, 1); + player->client->incCounter(id, 1); else if (event->button() == Qt::RightButton) - player->client->incCounter(name, -1); + player->client->incCounter(id, -1); } diff --git a/cockatrice/src/counter.h b/cockatrice/src/counter.h index 5accc776..aa20b620 100644 --- a/cockatrice/src/counter.h +++ b/cockatrice/src/counter.h @@ -7,17 +7,20 @@ class Player; class Counter : public QGraphicsItem { private: + int id; QString name; QColor color; + int radius; int value; protected: Player *player; void mousePressEvent(QGraphicsSceneMouseEvent *event); public: - Counter(Player *_player, const QString &_name, QColor _color, int _value, QGraphicsItem *parent = 0); + Counter(Player *_player, int _id, const QString &_name, QColor _color, int _radius, int _value, QGraphicsItem *parent = 0); QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + int getId() const { return id; } QString getName() const { return name; } int getValue() const { return value; } void setValue(int _value); diff --git a/cockatrice/src/game.cpp b/cockatrice/src/game.cpp index 340cb143..83b84679 100644 --- a/cockatrice/src/game.cpp +++ b/cockatrice/src/game.cpp @@ -11,6 +11,7 @@ #include "dlg_startgame.h" #include "counter.h" #include "gamescene.h" +#include "player.h" Game::Game(CardDatabase *_db, Client *_client, GameScene *_scene, QMenuBar *menuBar, QObject *parent) : QObject(parent), db(_db), client(_client), scene(_scene), started(false), currentPhase(-1) @@ -83,10 +84,14 @@ Game::Game(CardDatabase *_db, Client *_client, GameScene *_scene, QMenuBar *menu Game::~Game() { qDebug("Game destructor"); - for (int i = 0; i < players.size(); i++) { - emit playerRemoved(players.at(i)); - delete players.at(i); + + QMapIterator i(players); + while (i.hasNext()) { + i.next(); + emit playerRemoved(i.value()); + delete i.value(); } + delete gameMenu; delete cardMenu; } @@ -115,8 +120,9 @@ void Game::retranslateUi() moveMenu->setTitle(tr("&Move to")); - for (int i = 0; i < players.size(); ++i) - players[i]->retranslateUi(); + QMapIterator i(players); + while (i.hasNext()) + i.next().value()->retranslateUi(); } Player *Game::addPlayer(int playerId, const QString &playerName, bool local) @@ -132,7 +138,7 @@ Player *Game::addPlayer(int playerId, const QString &playerName, bool local) connect(newPlayer, SIGNAL(logSetCounter(Player *, QString, int, int)), this, SIGNAL(logSetCounter(Player *, QString, int, int))); connect(newPlayer, SIGNAL(logSetDoesntUntap(Player *, QString, bool)), this, SIGNAL(logSetDoesntUntap(Player *, QString, bool))); - players << newPlayer; + players.insert(playerId, newPlayer); emit playerAdded(newPlayer); return newPlayer; @@ -141,10 +147,11 @@ Player *Game::addPlayer(int playerId, const QString &playerName, bool local) void Game::cardListReceived(QList list) { for (int i = 0; i < list.size(); ++i) { - Player *p = players.findPlayer(list[i].getPlayerId()); + Player *p = players.value(list[i].getPlayerId(), 0); if (!p) continue; - CardZone *zone = p->getZones().findZone(list[i].getZoneName()); + + CardZone *zone = p->getZones().value(list[i].getZoneName(), 0); if (!zone) continue; @@ -161,12 +168,14 @@ void Game::cardListReceived(QList list) void Game::zoneListReceived(QList list) { for (int i = 0; i < list.size(); ++i) { - Player *p = players.findPlayer(list[i].getPlayerId()); + Player *p = players.value(list[i].getPlayerId(), 0); if (!p) continue; - CardZone *zone = p->getZones().findZone(list[i].getName()); + + CardZone *zone = p->getZones().value(list[i].getName(), 0); if (!zone) continue; + zone->clearContents(); if ( (list[i].getType() != ServerZone::PublicZone) @@ -181,13 +190,15 @@ void Game::zoneListReceived(QList list) void Game::counterListReceived(QList list) { - for (int i = 0; i < players.size(); ++i) - players[i]->clearCounters(); + QMapIterator i(players); + while (i.hasNext()) + i.next().value()->clearCounters(); for (int i = 0; i < list.size(); ++i) { - Player *p = players.findPlayer(list[i].getPlayerId()); - if (p) - p->addCounter(list[i].getName(), list[i].getColor(), list[i].getCount()); + Player *p = players.value(list[i].getPlayerId(), 0); + if (!p) + continue; + p->addCounter(list[i].getId(), list[i].getName(), list[i].getColor(), list[i].getRadius(), list[i].getCount()); } } @@ -215,7 +226,7 @@ void Game::restartGameDialog() void Game::gameEvent(const ServerEventData &msg) { qDebug(QString("game::gameEvent: public=%1, player=%2, name=%3, type=%4, data=%5").arg(msg.getPublic()).arg(msg.getPlayerId()).arg(msg.getPlayerName()).arg(msg.getEventType()).arg(msg.getEventData().join("/")).toLatin1()); - Player *p = players.findPlayer(msg.getPlayerId()); + Player *p = players.value(msg.getPlayerId(), 0); if (!msg.getPublic()) { if (!p) return; @@ -287,13 +298,16 @@ void Game::gameEvent(const ServerEventData &msg) case eventSetActivePlayer: { QStringList data = msg.getEventData(); int playerId = data[0].toInt(); - Player *player = players.findPlayer(playerId); + Player *player = players.value(playerId, 0); if (!player) { qDebug(QString("setActivePlayer: invalid player: %1").arg(playerId).toLatin1()); break; } - for (int i = 0; i < players.size(); ++i) - players[i]->setActive(players[i] == player); + QMapIterator i(players); + while (i.hasNext()) { + i.next(); + i.value()->setActive(i.value() == player); + } emit logSetActivePlayer(player); break; } @@ -319,10 +333,10 @@ void Game::gameEvent(const ServerEventData &msg) } case eventDumpZone: { QStringList data = msg.getEventData(); - Player *zoneOwner = players.findPlayer(data[0].toInt()); + Player *zoneOwner = players.value(data[0].toInt(), 0); if (!zoneOwner) break; - CardZone *zone = zoneOwner->getZones().findZone(data[1]); + CardZone *zone = zoneOwner->getZones().value(data[1], 0); if (!zone) break; emit logDumpZone(p, zone, data[2].toInt()); @@ -330,10 +344,10 @@ void Game::gameEvent(const ServerEventData &msg) } case eventStopDumpZone: { QStringList data = msg.getEventData(); - Player *zoneOwner = players.findPlayer(data[0].toInt()); + Player *zoneOwner = players.value(data[0].toInt(), 0); if (!zoneOwner) break; - CardZone *zone = zoneOwner->getZones().findZone(data[1]); + CardZone *zone = zoneOwner->getZones().value(data[1], 0); if (!zone) break; emit logStopDumpZone(p, zone); diff --git a/cockatrice/src/game.h b/cockatrice/src/game.h index 7ce5ee7a..e8b733d0 100644 --- a/cockatrice/src/game.h +++ b/cockatrice/src/game.h @@ -2,8 +2,10 @@ #define GAME_H #include +#include #include -#include "playerlist.h" +#include +#include #include "client.h" class GameScene; @@ -13,6 +15,7 @@ class CardDatabase; class DlgStartGame; class CardItem; class QMenuBar; +class CardZone; class Game : public QObject { Q_OBJECT @@ -32,7 +35,7 @@ private: Client *client; GameScene *scene; QStringList spectatorList; - PlayerList players; + QMap players; bool started; int currentPhase; public slots: diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index 5bd78b0f..0ef99013 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -21,7 +21,7 @@ Player::Player(const QString &_name, int _id, bool _local, CardDatabase *_db, Cl bgPixmap.load(bgPath); setCacheMode(DeviceCoordinateCache); - QPointF base = QPointF(55, 50); + QPointF base = QPointF(counterAreaWidth, 50); PileZone *deck = new PileZone(this, "deck", true, false, this); deck->setPos(base); @@ -41,7 +41,7 @@ Player::Player(const QString &_name, int _id, bool _local, CardDatabase *_db, Cl connect(table, SIGNAL(sizeChanged()), this, SLOT(updateBoundingRect())); hand = new HandZone(this, (int) table->boundingRect().height(), this); - base = QPointF(deck->boundingRect().width() + 60, 0); + base = QPointF(deck->boundingRect().width() + counterAreaWidth + 5, 0); hand->setPos(base); base += QPointF(hand->boundingRect().width(), 0); table->setPos(base); @@ -49,10 +49,31 @@ Player::Player(const QString &_name, int _id, bool _local, CardDatabase *_db, Cl updateBoundingRect(); if (local) { - aMoveHandToTopLibrary = new QAction(this); - connect(aMoveHandToTopLibrary, SIGNAL(triggered()), this, SLOT(actMoveHandToTopLibrary())); - aMoveHandToBottomLibrary = new QAction(this); - connect(aMoveHandToBottomLibrary, SIGNAL(triggered()), this, SLOT(actMoveHandToBottomLibrary())); + aMoveToTopLibrary = new QAction(this); + aMoveToTopLibrary->setData(QList() << "deck" << 0); + aMoveToBottomLibrary = new QAction(this); + aMoveToBottomLibrary->setData(QList() << "deck" << -1); + aMoveToHand = new QAction(this); + aMoveToHand->setData(QList() << "hand" << 0); + aMoveToGraveyard = new QAction(this); + aMoveToGraveyard->setData(QList() << "grave" << 0); + aMoveToRfg = new QAction(this); + aMoveToRfg->setData(QList() << "rfg" << 0); + + connect(aMoveToTopLibrary, SIGNAL(triggered()), hand, SLOT(moveAllToZone())); + connect(aMoveToBottomLibrary, SIGNAL(triggered()), hand, SLOT(moveAllToZone())); + connect(aMoveToGraveyard, SIGNAL(triggered()), hand, SLOT(moveAllToZone())); + connect(aMoveToRfg, SIGNAL(triggered()), hand, SLOT(moveAllToZone())); + + connect(aMoveToTopLibrary, SIGNAL(triggered()), grave, SLOT(moveAllToZone())); + connect(aMoveToBottomLibrary, SIGNAL(triggered()), grave, SLOT(moveAllToZone())); + connect(aMoveToHand, SIGNAL(triggered()), grave, SLOT(moveAllToZone())); + connect(aMoveToRfg, SIGNAL(triggered()), grave, SLOT(moveAllToZone())); + + connect(aMoveToTopLibrary, SIGNAL(triggered()), rfg, SLOT(moveAllToZone())); + connect(aMoveToBottomLibrary, SIGNAL(triggered()), rfg, SLOT(moveAllToZone())); + connect(aMoveToHand, SIGNAL(triggered()), rfg, SLOT(moveAllToZone())); + connect(aMoveToGraveyard, SIGNAL(triggered()), rfg, SLOT(moveAllToZone())); aViewLibrary = new QAction(this); connect(aViewLibrary, SIGNAL(triggered()), this, SLOT(actViewLibrary())); @@ -82,8 +103,10 @@ Player::Player(const QString &_name, int _id, bool _local, CardDatabase *_db, Cl if (local) { handMenu = playerMenu->addMenu(QString()); - handMenu->addAction(aMoveHandToTopLibrary); - handMenu->addAction(aMoveHandToBottomLibrary); + handMenu->addAction(aMoveToTopLibrary); + handMenu->addAction(aMoveToBottomLibrary); + handMenu->addAction(aMoveToGraveyard); + handMenu->addAction(aMoveToRfg); hand->setMenu(handMenu); libraryMenu = playerMenu->addMenu(QString()); @@ -109,6 +132,18 @@ Player::Player(const QString &_name, int _id, bool _local, CardDatabase *_db, Cl rfg->setMenu(rfgMenu, aViewRfg); if (local) { + graveMenu->addSeparator(); + graveMenu->addAction(aMoveToTopLibrary); + graveMenu->addAction(aMoveToBottomLibrary); + graveMenu->addAction(aMoveToHand); + graveMenu->addAction(aMoveToRfg); + + rfgMenu->addSeparator(); + rfgMenu->addAction(aMoveToTopLibrary); + rfgMenu->addAction(aMoveToBottomLibrary); + rfgMenu->addAction(aMoveToHand); + rfgMenu->addAction(aMoveToGraveyard); + sbMenu = playerMenu->addMenu(QString()); sbMenu->addAction(aViewSideboard); sb->setMenu(sbMenu, aViewSideboard); @@ -153,8 +188,9 @@ Player::~Player() { qDebug("Player destructor"); - for (int i = 0; i < zones.size(); i++) - delete zones.at(i); + QMapIterator i(zones); + while (i.hasNext()) + delete i.next().value(); clearCounters(); delete playerMenu; @@ -175,8 +211,11 @@ void Player::retranslateUi() rfgMenu->setTitle(tr("&Exile")); if (local) { - aMoveHandToTopLibrary->setText(tr("Move to &top of library")); - aMoveHandToBottomLibrary->setText(tr("Move to &bottom of library")); + aMoveToTopLibrary->setText(tr("Move to &top of library")); + aMoveToBottomLibrary->setText(tr("Move to &bottom of library")); + aMoveToHand->setText(tr("Move to &hand")); + aMoveToGraveyard->setText(tr("Move to g&raveyard")); + aMoveToRfg->setText(tr("Move to &exile")); aViewLibrary->setText(tr("&View library")); aViewLibrary->setShortcut(tr("F3")); aViewTopCards->setText(tr("View &top cards of library...")); @@ -233,16 +272,6 @@ void Player::initSayMenu() } } -void Player::actMoveHandToTopLibrary() -{ - zones.findZone("hand")->moveAllToZone("deck", 0); -} - -void Player::actMoveHandToBottomLibrary() -{ - zones.findZone("hand")->moveAllToZone("deck", -1); -} - void Player::actViewLibrary() { emit toggleZoneView(this, "deck", -1); @@ -297,20 +326,22 @@ void Player::actUntapAll() void Player::actIncLife() { - client->incCounter("life", 1); + // XXX + client->incCounter(lifeCounter->getId(), 1); } void Player::actDecLife() { - client->incCounter("life", -1); + // XXX + client->incCounter(lifeCounter->getId(), -1); } void Player::actSetLife() { bool ok; - int life = QInputDialog::getInteger(0, tr("Set life"), tr("New life total:"), getCounter("life")->getValue(), 0, 2000000000, 1, &ok); + int life = QInputDialog::getInteger(0, tr("Set life"), tr("New life total:"), lifeCounter->getValue(), 0, 2000000000, 1, &ok); if (ok) - client->setCounter("life", life); + client->setCounter(lifeCounter->getId(), life); } void Player::actRollDie() @@ -336,7 +367,7 @@ void Player::actSayMessage() void Player::addZone(CardZone *z) { - zones << z; + zones.insert(z->getName(), z); } void Player::setCardAttrHelper(CardItem *card, const QString &aname, const QString &avalue, bool allCards) @@ -376,41 +407,44 @@ void Player::gameEvent(const ServerEventData &event) // XXX Fehlerbehandlung // Clean up existing zones first - for (int i = 0; i < zones.size(); i++) { - if (ZoneViewZone *view = zones.at(i)->getView()) + QMapIterator zoneIterator(zones); + while (zoneIterator.hasNext()) { + zoneIterator.next(); + if (ZoneViewZone *view = zoneIterator.value()->getView()) ((ZoneViewWidget *) view->parentItem())->close(); - zones.at(i)->clearContents(); + zoneIterator.value()->clearContents(); } clearCounters(); - CardZone *deck = zones.findZone("deck"); + CardZone *deck = zones.value("deck"); for (; deck_cards; deck_cards--) deck->addCard(new CardItem(db), false, -1); - CardZone *sb = zones.findZone("sb"); + CardZone *sb = zones.value("sb"); for (; sb_cards; sb_cards--) sb->addCard(new CardItem(db), false, -1); - for (int i = 0; i < zones.size(); i++) - zones.at(i)->reorganizeCards(); + zoneIterator.toFront(); + while (zoneIterator.hasNext()) + zoneIterator.next().value()->reorganizeCards(); if (local) { - client->addCounter("life", Qt::white, 20); - client->addCounter("w", QColor(255, 255, 150), 0); - client->addCounter("u", QColor(150, 150, 255), 0); - client->addCounter("b", QColor(150, 150, 150), 0); - client->addCounter("r", QColor(250, 150, 150), 0); - client->addCounter("g", QColor(150, 255, 150), 0); - client->addCounter("x", QColor(255, 255, 255), 0); - client->addCounter("storm", QColor(255, 255, 255), 0); + client->addCounter("life", Qt::white, 25, 20); + client->addCounter("w", QColor(255, 255, 150), 20, 0); + client->addCounter("u", QColor(150, 150, 255), 20, 0); + client->addCounter("b", QColor(150, 150, 150), 20, 0); + client->addCounter("r", QColor(250, 150, 150), 20, 0); + client->addCounter("g", QColor(150, 255, 150), 20, 0); + client->addCounter("x", QColor(255, 255, 255), 20, 0); + client->addCounter("storm", QColor(255, 255, 255), 20, 0); } break; } case eventDraw: { - CardZone *deck = zones.findZone("deck"); - CardZone *hand = zones.findZone("hand"); + CardZone *deck = zones.value("deck"); + CardZone *hand = zones.value("hand"); if (!event.getPublic()) { hand->addCard(deck->takeCard(0, data[0].toInt(), data[1]), true, -1); } else { @@ -428,13 +462,17 @@ void Player::gameEvent(const ServerEventData &event) } int cardId = data[0].toInt(); QString cardName = data[1]; - CardZone *startZone = zones.findZone(data[2]); - if (!startZone) + CardZone *startZone = zones.value(data[2], 0); + if (!startZone) { qDebug(QString("start zone invalid: %1").arg(data[2]).toLatin1()); + break; + } int position = data[3].toInt(); - CardZone *targetZone = zones.findZone(data[4]); - if (!targetZone) + CardZone *targetZone = zones.value(data[4], 0); + if (!targetZone) { qDebug(QString("target zone invalid: %1").arg(data[4]).toLatin1()); + break; + } int x = data[5].toInt(); int y = data[6].toInt(); bool facedown = data[7].toInt(); @@ -467,7 +505,9 @@ void Player::gameEvent(const ServerEventData &event) if (data.size() != 6) { qDebug("error"); } - CardZone *zone = zones.findZone(data[0]); + CardZone *zone = zones.value(data[0], 0); + if (!zone) + break; int cardid = data[1].toInt(); QString cardname = data[2]; QString powtough = data[3]; @@ -485,7 +525,9 @@ void Player::gameEvent(const ServerEventData &event) if (data.size() != 4) { // XXX } - CardZone *zone = zones.findZone(data[0]); + CardZone *zone = zones.value(data[0], 0); + if (!zone) + break; int cardId = data[1].toInt(); QString aname = data[2]; QString avalue = data[3]; @@ -504,23 +546,27 @@ void Player::gameEvent(const ServerEventData &event) break; } case eventAddCounter: { - if (data.size() != 3) { + if (data.size() != 4) { // XXX } - QString counterName = data[0]; - int colorValue = data[1].toInt(); - int value = data[2].toInt(); + int counterId = data[0].toInt(); + QString counterName = data[1]; + int colorValue = data[2].toInt(); + int radius = data[3].toInt(); + int value = data[4].toInt(); QColor color(colorValue / 65536, (colorValue % 65536) / 256, colorValue % 256); - addCounter(counterName, color, value); + addCounter(counterId, counterName, color, radius, value); break; } case eventSetCounter: { if (data.size() != 2) { // XXX } + int counterId = data[0].toInt(); int value = data[1].toInt(); - QString counterName = data[0]; - Counter *c = getCounter(counterName); + Counter *c = counters.value(counterId, 0); + if (!c) + break; int oldValue = c->getValue(); c->setValue(value); emit logSetCounter(this, c->getName(), value, oldValue); @@ -583,46 +629,62 @@ void Player::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/ painter->drawText(QRectF(0, 0, totalWidth, 40), Qt::AlignCenter, nameStr); } -Counter *Player::getCounter(const QString &name, bool remove) +void Player::addCounter(int counterId, const QString &name, QColor color, int radius, int value) { - for (int i = 0; i < counterList.size(); i++) { - Counter *temp = counterList.at(i); - if (temp->getName() == name) { - if (remove) - counterList.removeAt(i); - return temp; - } - } - return 0; -} - -void Player::addCounter(const QString &name, QColor color, int value) -{ - counterList.append(new Counter(this, name, color, value, this)); + Counter *c = new Counter(this, counterId, name, color, radius, value, this); + counters.insert(counterId, c); + if (name == "life") + lifeCounter = c; + // XXX rearrangeCounters(); } -void Player::delCounter(const QString &name) +void Player::delCounter(int counterId) { - delete getCounter(name, true); + Counter *c = counters.value(counterId, 0); + if (!c) + return; + counters.remove(counterId); + delete c; rearrangeCounters(); } void Player::clearCounters() { - for (int i = 0; i < counterList.size(); i++) - delete counterList.at(i); - counterList.clear(); + QMapIterator counterIterator(counters); + while (counterIterator.hasNext()) + delete counterIterator.next().value(); + counters.clear(); } void Player::rearrangeCounters() { - const int counterAreaWidth = 55; - qreal y = 50; - for (int i = 0; i < counterList.size(); i++) { - Counter *temp = counterList.at(i); - QRectF br = temp->boundingRect(); - temp->setPos((counterAreaWidth - br.width()) / 2, y); - y += br.height() + 10; + qreal marginTop = 50; + qreal marginBottom = 15; + + // Determine total height of bounding rectangles + qreal totalHeight = 0; + QMapIterator counterIterator(counters); + while (counterIterator.hasNext()) { + counterIterator.next(); + totalHeight += counterIterator.value()->boundingRect().height(); + } + + // Determine free space between objects + qreal padding = (boundingRect().height() - marginTop - marginBottom - totalHeight) / (counters.size() - 1); + qreal y = boundingRect().y() + marginTop; + + if (counters.size() == 1) { + padding = 0; + y += (boundingRect().height() - marginTop - marginBottom) / 2; + } + + // Place objects + for (counterIterator.toFront(); counterIterator.hasNext(); ) { + Counter *c = counterIterator.next().value(); + + QRectF br = c->boundingRect(); + c->setPos((counterAreaWidth - br.width()) / 2, y); + y += br.height() + padding; } } diff --git a/cockatrice/src/player.h b/cockatrice/src/player.h index 142a6170..16a16df8 100644 --- a/cockatrice/src/player.h +++ b/cockatrice/src/player.h @@ -3,8 +3,9 @@ #include #include -#include "zonelist.h" +#include #include "client.h" +#include "carditem.h" class Client; class CardDatabase; @@ -13,6 +14,7 @@ class QAction; class ZoneViewZone; class Game; class Counter; +class CardZone; class TableZone; class HandZone; @@ -45,9 +47,6 @@ public slots: private slots: void updateBoundingRect(); - void actMoveHandToTopLibrary(); - void actMoveHandToBottomLibrary(); - void actShuffle(); void actDrawCard(); void actDrawCards(); @@ -59,7 +58,7 @@ private slots: void actViewSideboard(); private: QMenu *playerMenu, *handMenu, *graveMenu, *rfgMenu, *libraryMenu, *sbMenu, *sayMenu; - QAction *aMoveHandToTopLibrary, *aMoveHandToBottomLibrary, + QAction *aMoveToTopLibrary, *aMoveToBottomLibrary, *aMoveToHand, *aMoveToGraveyard, *aMoveToRfg, *aViewLibrary, *aViewTopCards, *aViewGraveyard, *aViewRfg, *aViewSideboard, *aDrawCard, *aDrawCards, *aShuffle, *aUntapAll, *aDecLife, *aIncLife, *aSetLife, *aRollDie, *aCreateToken; @@ -70,7 +69,7 @@ private: bool active; bool local; - ZoneList zones; + QMap zones; TableZone *table; HandZone *hand; @@ -80,18 +79,21 @@ private: QPixmap bgPixmap; QRectF bRect; - QList counterList; + QMap counters; + Counter *lifeCounter; + void rearrangeCounters(); void initSayMenu(); public: + static const int counterAreaWidth = 65; + enum { Type = typeOther }; int type() const { return Type; } QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); - Counter *getCounter(const QString &name, bool remove = false); - void addCounter(const QString &name, QColor color, int value); - void delCounter(const QString &name); + void addCounter(int counterId, const QString &name, QColor color, int radius, int value); + void delCounter(int counterId); void clearCounters(); Client *client; @@ -103,7 +105,8 @@ public: int getId() const { return id; } QString getName() const { return name; } bool getLocal() const { return local; } - const ZoneList &getZones() const { return zones; } + const QMap &getZones() const { return zones; } + TableZone *getTable() const { return table; } void gameEvent(const ServerEventData &event); CardDatabase *getDb() const { return db; } void showCardMenu(const QPoint &p); diff --git a/cockatrice/src/playerlist.cpp b/cockatrice/src/playerlist.cpp deleted file mode 100644 index 091fae2c..00000000 --- a/cockatrice/src/playerlist.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "playerlist.h" - -Player *PlayerList::findPlayer(int id) const -{ - for (int i = 0; i < size(); i++) { - Player *temp = at(i); - if (temp->getId() == id) - return temp; - } - return 0; -} diff --git a/cockatrice/src/playerlist.h b/cockatrice/src/playerlist.h deleted file mode 100644 index 3658cf9f..00000000 --- a/cockatrice/src/playerlist.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef PLAYERLIST_H -#define PLAYERLIST_H - -#include "player.h" -#include - -class PlayerList : public QList { -public: - Player *findPlayer(int id) const; -}; - -#endif diff --git a/cockatrice/src/zonelist.cpp b/cockatrice/src/zonelist.cpp deleted file mode 100644 index 4095fd51..00000000 --- a/cockatrice/src/zonelist.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "zonelist.h" - -CardZone *ZoneList::findZone(const QString &name) const -{ - for (int i = 0; i < size(); i++) { - CardZone *temp = at(i); - if (!temp->getName().compare(name)) - return temp; - } - return 0; -} diff --git a/cockatrice/src/zonelist.h b/cockatrice/src/zonelist.h deleted file mode 100644 index ac7174e8..00000000 --- a/cockatrice/src/zonelist.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef ZONELIST_H -#define ZONELIST_H - -#include "cardzone.h" -#include - -class ZoneList : public QList { -public: - CardZone *findZone(const QString &name) const; -}; - -#endif diff --git a/cockatrice/src/zoneviewlayout.cpp b/cockatrice/src/zoneviewlayout.cpp index f3676c4c..a9f322a1 100644 --- a/cockatrice/src/zoneviewlayout.cpp +++ b/cockatrice/src/zoneviewlayout.cpp @@ -45,7 +45,7 @@ void ZoneViewLayout::toggleZoneView(Player *player, const QString &zoneName, int } } - ZoneViewWidget *item = new ZoneViewWidget(db, player, player->getZones().findZone(zoneName), numberCards, this); + ZoneViewWidget *item = new ZoneViewWidget(db, player, player->getZones().value(zoneName), numberCards, this); views.append(item); connect(item, SIGNAL(closePressed(ZoneViewWidget *)), this, SLOT(removeItem(ZoneViewWidget *))); connect(item, SIGNAL(sizeChanged()), this, SLOT(reorganize())); diff --git a/servatrice/src/counter.h b/servatrice/src/counter.h index 8eeed18f..c9d96028 100644 --- a/servatrice/src/counter.h +++ b/servatrice/src/counter.h @@ -24,14 +24,18 @@ class Counter { protected: + int id; QString name; int color; + int radius; int count; public: - Counter(QString _name, int _color, int _count = 0) : name(_name), color(_color), count(_count) { } + Counter(int _id, const QString &_name, int _color, int _radius, int _count = 0) : id(_id), name(_name), color(_color), radius(_radius), count(_count) { } ~Counter() { } + int getId() const { return id; } QString getName() const { return name; } int getColor() const { return color; } + int getRadius() const { return radius; } int getCount() const { return count; } void setCount(int _count) { count = _count; } }; diff --git a/servatrice/src/serversocket.cpp b/servatrice/src/serversocket.cpp index 20c42352..ca22b236 100644 --- a/servatrice/src/serversocket.cpp +++ b/servatrice/src/serversocket.cpp @@ -33,7 +33,7 @@ QHash ServerSocket::commandHash; ServerSocket::ServerSocket(Server *_server, QObject *parent) - : QTcpSocket(parent), server(_server), game(0), spectator(false), PlayerStatus(StatusNormal), authState(PasswordWrong), acceptsGameListChanges(false) + : QTcpSocket(parent), server(_server), game(0), spectator(false), nextCardId(0), PlayerStatus(StatusNormal), authState(PasswordWrong), acceptsGameListChanges(false) { if (commandHash.isEmpty()) { commandHash.insert("ping", CommandProperties(false, false, false, true, QList(), &ServerSocket::cmdPing)); @@ -94,12 +94,13 @@ ServerSocket::ServerSocket(Server *_server, QObject *parent) commandHash.insert("add_counter", CommandProperties(true, true, true, false, QList() << QVariant::String << QVariant::Int + << QVariant::Int << QVariant::Int, &ServerSocket::cmdAddCounter)); commandHash.insert("set_counter", CommandProperties(true, true, true, false, QList() - << QVariant::String + << QVariant::Int << QVariant::Int, &ServerSocket::cmdSetCounter)); commandHash.insert("del_counter", CommandProperties(true, true, true, false, QList() - << QVariant::String, &ServerSocket::cmdDelCounter)); + << QVariant::Int, &ServerSocket::cmdDelCounter)); commandHash.insert("list_counters", CommandProperties(true, true, true, true, QList() << QVariant::Int, &ServerSocket::cmdListCounters)); commandHash.insert("list_zones", CommandProperties(true, true, true, true, QList() @@ -143,6 +144,18 @@ int ServerSocket::newCardId() return nextCardId++; } +int ServerSocket::newCounterId() const +{ + int id = 0; + QMapIterator i(counters); + while (i.hasNext()) { + Counter *c = i.next().value(); + if (c->getId() > id) + id = c->getId(); + } + return id + 1; +} + PlayerZone *ServerSocket::getZone(const QString &name) const { QListIterator ZoneIterator(zones); @@ -154,17 +167,6 @@ PlayerZone *ServerSocket::getZone(const QString &name) const return NULL; } -Counter *ServerSocket::getCounter(const QString &name) const -{ - QListIterator CounterIterator(counters); - while (CounterIterator.hasNext()) { - Counter *temp = CounterIterator.next(); - if (temp->getName() == name) - return temp; - } - return NULL; -} - void ServerSocket::setupZones() { // Delete existing zones and counters @@ -209,8 +211,9 @@ void ServerSocket::clearZones() delete zones.at(i); zones.clear(); - for (int i = 0; i < counters.size(); i++) - delete counters.at(i); + QMapIterator counterIterator(counters); + while (counterIterator.hasNext()) + delete counterIterator.next().value(); counters.clear(); } @@ -588,60 +591,62 @@ ReturnMessage::ReturnCode ServerSocket::cmdSetCardAttr(const QList &pa ReturnMessage::ReturnCode ServerSocket::cmdIncCounter(const QList ¶ms) { - Counter *c = getCounter(params[0].toString()); + Counter *c = counters.value(params[0].toInt(), 0); if (!c) return ReturnMessage::ReturnContextError; int delta = params[1].toInt(); c->setCount(c->getCount() + delta); - emit broadcastEvent(QString("set_counter|%1|%2").arg(c->getName()).arg(c->getCount()), this); + emit broadcastEvent(QString("set_counter|%1|%2").arg(c->getId()).arg(c->getCount()), this); return ReturnMessage::ReturnOk; } ReturnMessage::ReturnCode ServerSocket::cmdAddCounter(const QList ¶ms) { QString name = params[0].toString(); - if (getCounter(name)) - return ReturnMessage::ReturnContextError; int color = params[1].toInt(); - int count = params[2].toInt(); + int radius = params[2].toInt(); + int count = params[3].toInt(); - Counter *c = new Counter(name, color, count); - counters << c; - emit broadcastEvent(QString("add_counter|%1|%2|%3").arg(c->getName()).arg(color).arg(count), this); + Counter *c = new Counter(newCounterId(), name, color, radius, count); + counters.insert(c->getId(), c); + emit broadcastEvent(QString("add_counter|%1|%2|%3|%4|%5").arg(c->getId()).arg(c->getName()).arg(color).arg(radius).arg(count), this); return ReturnMessage::ReturnOk; } ReturnMessage::ReturnCode ServerSocket::cmdSetCounter(const QList ¶ms) { - Counter *c = getCounter(params[0].toString()); + Counter *c = counters.value(params[0].toInt(), 0); if (!c) return ReturnMessage::ReturnContextError; int count = params[1].toInt(); c->setCount(count); - emit broadcastEvent(QString("set_counter|%1|%2").arg(c->getName()).arg(count), this); + emit broadcastEvent(QString("set_counter|%1|%2").arg(c->getId()).arg(count), this); return ReturnMessage::ReturnOk; } ReturnMessage::ReturnCode ServerSocket::cmdDelCounter(const QList ¶ms) { - Counter *c = getCounter(params[0].toString()); + int counterId = params[0].toInt(); + Counter *c = counters.value(counterId, 0); if (!c) return ReturnMessage::ReturnContextError; + counters.remove(counterId); delete c; - counters.removeAt(counters.indexOf(c)); - emit broadcastEvent(QString("del_counter|%1").arg(params[0].toString()), this); + emit broadcastEvent(QString("del_counter|%1").arg(counterId), this); return ReturnMessage::ReturnOk; } QStringList ServerSocket::listCountersHelper(ServerSocket *player) { QStringList result; - const QList &counterList = player->getCounters(); - for (int i = 0; i < counterList.size(); ++i) - result << QString("%1|%2|%3|%4").arg(player->getPlayerId()).arg(counterList[i]->getName()).arg(counterList[i]->getColor()).arg(counterList[i]->getCount()); + QMapIterator i(player->getCounters()); + while (i.hasNext()) { + Counter *c = i.next().value(); + result << QString("%1|%2|%3|%4|%5|%6").arg(player->getPlayerId()).arg(c->getId()).arg(c->getName()).arg(c->getColor()).arg(c->getRadius()).arg(c->getCount()); + } return result; } diff --git a/servatrice/src/serversocket.h b/servatrice/src/serversocket.h index ed51df5b..d427121c 100644 --- a/servatrice/src/serversocket.h +++ b/servatrice/src/serversocket.h @@ -110,14 +110,14 @@ private: QList DeckList; QList SideboardList; QList zones; - QList counters; + QMap counters; int playerId; QString playerName; bool spectator; int nextCardId; int newCardId(); + int newCounterId() const; PlayerZone *getZone(const QString &name) const; - Counter *getCounter(const QString &name) const; void clearZones(); bool parseCommand(QString line); PlayerStatusEnum PlayerStatus; @@ -143,7 +143,7 @@ public: bool getAcceptsGameListChanges() const { return acceptsGameListChanges; } bool getAcceptsChatChannelListChanges() const { return acceptsChatChannelListChanges; } const QList &getZones() const { return zones; } - const QList &getCounters() const { return counters; } + const QMap &getCounters() const { return counters; } void setupZones(); };