mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-23 07:03:54 -07:00
new protocol code
This commit is contained in:
parent
ba8d40211c
commit
888a64b0fd
25 changed files with 487 additions and 333 deletions
|
|
@ -56,8 +56,8 @@ void SetList::sortByKey()
|
|||
qSort(begin(), end(), CompareFunctor());
|
||||
}
|
||||
|
||||
CardInfo::CardInfo(CardDatabase *_db, const QString &_name, const QString &_manacost, const QString &_cardtype, const QString &_powtough, const QString &_text, const QStringList &_colors, int _tableRow, const SetList &_sets, const QString &_picURL)
|
||||
: db(_db), name(_name), sets(_sets), manacost(_manacost), cardtype(_cardtype), powtough(_powtough), text(_text), colors(_colors), picURL(_picURL), tableRow(_tableRow), pixmap(NULL)
|
||||
CardInfo::CardInfo(CardDatabase *_db, const QString &_name, const QString &_manacost, const QString &_cardtype, const QString &_powtough, const QString &_text, const QStringList &_colors, bool _cipt, int _tableRow, const SetList &_sets, const QString &_picURL)
|
||||
: db(_db), name(_name), sets(_sets), manacost(_manacost), cardtype(_cardtype), powtough(_powtough), text(_text), colors(_colors), picURL(_picURL), cipt(_cipt), tableRow(_tableRow), pixmap(NULL)
|
||||
{
|
||||
for (int i = 0; i < sets.size(); i++)
|
||||
sets[i]->append(this);
|
||||
|
|
@ -220,6 +220,8 @@ QXmlStreamWriter &operator<<(QXmlStreamWriter &xml, const CardInfo *info)
|
|||
xml.writeTextElement("tablerow", QString::number(info->getTableRow()));
|
||||
xml.writeTextElement("text", info->getText());
|
||||
xml.writeTextElement("picURL", info->getPicURL());
|
||||
if (info->getCipt())
|
||||
xml.writeTextElement("cipt", "1");
|
||||
xml.writeEndElement(); // card
|
||||
|
||||
return xml;
|
||||
|
|
@ -383,6 +385,7 @@ void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml)
|
|||
QStringList colors;
|
||||
SetList sets;
|
||||
int tableRow = 0;
|
||||
bool cipt = false;
|
||||
while (!xml.atEnd()) {
|
||||
if (xml.readNext() == QXmlStreamReader::EndElement)
|
||||
break;
|
||||
|
|
@ -404,8 +407,10 @@ void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml)
|
|||
tableRow = xml.readElementText().toInt();
|
||||
else if (xml.name() == "picURL")
|
||||
picURL = xml.readElementText();
|
||||
else if (xml.name() == "cipt")
|
||||
cipt = (xml.readElementText() == "1");
|
||||
}
|
||||
cardHash.insert(name, new CardInfo(this, name, manacost, type, pt, text, colors, tableRow, sets, picURL));
|
||||
cardHash.insert(name, new CardInfo(this, name, manacost, type, pt, text, colors, cipt, tableRow, sets, picURL));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,6 +46,7 @@ private:
|
|||
QString text;
|
||||
QStringList colors;
|
||||
QString picURL;
|
||||
bool cipt;
|
||||
int tableRow;
|
||||
QPixmap *pixmap;
|
||||
QMap<int, QPixmap *> scaledPixmapCache;
|
||||
|
|
@ -57,6 +58,7 @@ public:
|
|||
const QString &_powtough = QString(),
|
||||
const QString &_text = QString(),
|
||||
const QStringList &_colors = QStringList(),
|
||||
bool cipt = false,
|
||||
int _tableRow = 0,
|
||||
const SetList &_sets = SetList(),
|
||||
const QString &_picURL = QString());
|
||||
|
|
@ -67,6 +69,7 @@ public:
|
|||
const QString &getCardType() const { return cardtype; }
|
||||
const QString &getPowTough() const { return powtough; }
|
||||
const QString &getText() const { return text; }
|
||||
bool getCipt() const { return cipt; }
|
||||
void setText(const QString &_text) { text = _text; }
|
||||
const QStringList &getColors() const { return colors; }
|
||||
const QString &getPicURL() const { return picURL; }
|
||||
|
|
|
|||
|
|
@ -142,12 +142,6 @@ CardItem *CardZone::takeCard(int position, int cardId, const QString &cardName,
|
|||
return c;
|
||||
}
|
||||
|
||||
void CardZone::setCardAttr(int cardId, const QString &aname, const QString &avalue)
|
||||
{
|
||||
if (hasCardAttr)
|
||||
player->sendGameCommand(new Command_SetCardAttr(-1, name, cardId, aname, avalue));
|
||||
}
|
||||
|
||||
void CardZone::moveAllToZone()
|
||||
{
|
||||
QList<QVariant> data = static_cast<QAction *>(sender())->data().toList();
|
||||
|
|
|
|||
|
|
@ -52,7 +52,6 @@ public:
|
|||
CardItem *getCard(int cardId, const QString &cardName);
|
||||
// takeCard() finds a card by position and removes it from the zone and from all of its views.
|
||||
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) { view = _view; }
|
||||
virtual void reorganizeCards() = 0;
|
||||
|
|
|
|||
|
|
@ -90,14 +90,14 @@ void Client::processProtocolItem(ProtocolItem *item)
|
|||
{
|
||||
ProtocolResponse *response = qobject_cast<ProtocolResponse *>(item);
|
||||
if (response) {
|
||||
Command *cmd = pendingCommands.value(response->getCmdId(), 0);
|
||||
if (!cmd)
|
||||
CommandContainer *cmdCont = pendingCommands.value(response->getCmdId(), 0);
|
||||
if (!cmdCont)
|
||||
return;
|
||||
|
||||
pendingCommands.remove(cmd->getCmdId());
|
||||
cmd->processResponse(response);
|
||||
pendingCommands.remove(cmdCont->getCmdId());
|
||||
cmdCont->processResponse(response);
|
||||
delete response;
|
||||
delete cmd;
|
||||
delete cmdCont;
|
||||
|
||||
return;
|
||||
}
|
||||
|
|
@ -114,10 +114,10 @@ void Client::processProtocolItem(ProtocolItem *item)
|
|||
return;
|
||||
}
|
||||
|
||||
GameEvent *gameEvent = qobject_cast<GameEvent *>(item);
|
||||
if (gameEvent) {
|
||||
emit gameEventReceived(gameEvent);
|
||||
delete gameEvent;
|
||||
GameEventContainer *gameEventContainer = qobject_cast<GameEventContainer *>(item);
|
||||
if (gameEventContainer) {
|
||||
emit gameEventContainerReceived(gameEventContainer);
|
||||
delete gameEventContainer;
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -139,8 +139,13 @@ void Client::setStatus(const ClientStatus _status)
|
|||
|
||||
void Client::sendCommand(Command *cmd)
|
||||
{
|
||||
cmd->write(xmlWriter);
|
||||
pendingCommands.insert(cmd->getCmdId(), cmd);
|
||||
sendCommandContainer(new CommandContainer(QList<Command *>() << cmd));
|
||||
}
|
||||
|
||||
void Client::sendCommandContainer(CommandContainer *cont)
|
||||
{
|
||||
cont->write(xmlWriter);
|
||||
pendingCommands.insert(cont->getCmdId(), cont);
|
||||
}
|
||||
|
||||
void Client::connectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password)
|
||||
|
|
@ -162,7 +167,7 @@ void Client::disconnectFromServer()
|
|||
|
||||
timer->stop();
|
||||
|
||||
QList<Command *> pc = pendingCommands.values();
|
||||
QList<CommandContainer *> pc = pendingCommands.values();
|
||||
for (int i = 0; i < pc.size(); i++)
|
||||
delete pc[i];
|
||||
pendingCommands.clear();
|
||||
|
|
@ -174,7 +179,7 @@ void Client::disconnectFromServer()
|
|||
void Client::ping()
|
||||
{
|
||||
int maxTime = 0;
|
||||
QMapIterator<int, Command *> i(pendingCommands);
|
||||
QMapIterator<int, CommandContainer *> i(pendingCommands);
|
||||
while (i.hasNext()) {
|
||||
int time = i.next().value()->tick();
|
||||
if (time > maxTime)
|
||||
|
|
|
|||
|
|
@ -9,14 +9,16 @@
|
|||
|
||||
class QTimer;
|
||||
class Command;
|
||||
class CommandContainer;
|
||||
class QXmlStreamReader;
|
||||
class QXmlStreamWriter;
|
||||
|
||||
class ProtocolItem;
|
||||
class ProtocolResponse;
|
||||
class TopLevelProtocolItem;
|
||||
class CommandContainer;
|
||||
class ChatEvent;
|
||||
class GameEvent;
|
||||
class GameEventContainer;
|
||||
class Event_ListGames;
|
||||
class Event_ServerMessage;
|
||||
class Event_ListChatChannels;
|
||||
|
|
@ -45,7 +47,7 @@ signals:
|
|||
// Chat events
|
||||
void chatEventReceived(ChatEvent *event);
|
||||
// Game events
|
||||
void gameEventReceived(GameEvent *event);
|
||||
void gameEventContainerReceived(GameEventContainer *event);
|
||||
// Generic events
|
||||
void listGamesEventReceived(Event_ListGames *event);
|
||||
void serverMessageEventReceived(Event_ServerMessage *event);
|
||||
|
|
@ -63,7 +65,7 @@ private:
|
|||
static const int maxTimeout = 10;
|
||||
|
||||
QTimer *timer;
|
||||
QMap<int, Command *> pendingCommands;
|
||||
QMap<int, CommandContainer *> pendingCommands;
|
||||
QTcpSocket *socket;
|
||||
QXmlStreamReader *xmlReader;
|
||||
QXmlStreamWriter *xmlWriter;
|
||||
|
|
@ -80,6 +82,7 @@ public:
|
|||
void connectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password);
|
||||
void disconnectFromServer();
|
||||
void sendCommand(Command *cmd);
|
||||
void sendCommandContainer(CommandContainer *cont);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -908,6 +908,11 @@ void Player::sendGameCommand(GameCommand *command)
|
|||
static_cast<TabGame *>(parent())->sendGameCommand(command);
|
||||
}
|
||||
|
||||
void Player::sendCommandContainer(CommandContainer *cont)
|
||||
{
|
||||
static_cast<TabGame *>(parent())->sendCommandContainer(cont);
|
||||
}
|
||||
|
||||
void Player::cardMenuAction()
|
||||
{
|
||||
// Determine the appropriate handler function.
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ class HandZone;
|
|||
class ServerInfo_Player;
|
||||
class ServerInfo_Arrow;
|
||||
class ServerInfo_Counter;
|
||||
class CommandContainer;
|
||||
class GameCommand;
|
||||
class GameEvent;
|
||||
class Event_DeckSelect;
|
||||
|
|
@ -190,6 +191,7 @@ public:
|
|||
void processPlayerInfo(ServerInfo_Player *info);
|
||||
void processGameEvent(GameEvent *event);
|
||||
void sendGameCommand(GameCommand *command);
|
||||
void sendCommandContainer(CommandContainer *cont);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -226,25 +226,30 @@ Player *TabGame::addPlayer(int playerId, const QString &playerName)
|
|||
return newPlayer;
|
||||
}
|
||||
|
||||
void TabGame::processGameEvent(GameEvent *event)
|
||||
void TabGame::processGameEventContainer(GameEventContainer *cont)
|
||||
{
|
||||
switch (event->getItemId()) {
|
||||
case ItemId_Event_GameStateChanged: eventGameStateChanged(qobject_cast<Event_GameStateChanged *>(event)); break;
|
||||
case ItemId_Event_Join: eventJoin(qobject_cast<Event_Join *>(event)); break;
|
||||
case ItemId_Event_Leave: eventLeave(qobject_cast<Event_Leave *>(event)); break;
|
||||
case ItemId_Event_GameClosed: eventGameClosed(qobject_cast<Event_GameClosed *>(event)); break;
|
||||
case ItemId_Event_SetActivePlayer: eventSetActivePlayer(qobject_cast<Event_SetActivePlayer *>(event)); break;
|
||||
case ItemId_Event_SetActivePhase: eventSetActivePhase(qobject_cast<Event_SetActivePhase *>(event)); break;
|
||||
case ItemId_Event_Ping: eventPing(qobject_cast<Event_Ping *>(event)); break;
|
||||
|
||||
default: {
|
||||
Player *player = players.value(event->getPlayerId(), 0);
|
||||
if (!player) {
|
||||
qDebug() << "unhandled game event: invalid player id";
|
||||
break;
|
||||
const QList<GameEvent *> &eventList = cont->getEventList();
|
||||
for (int i = 0; i < eventList.size(); ++i) {
|
||||
GameEvent *event = eventList[i];
|
||||
|
||||
switch (event->getItemId()) {
|
||||
case ItemId_Event_GameStateChanged: eventGameStateChanged(qobject_cast<Event_GameStateChanged *>(event)); break;
|
||||
case ItemId_Event_Join: eventJoin(qobject_cast<Event_Join *>(event)); break;
|
||||
case ItemId_Event_Leave: eventLeave(qobject_cast<Event_Leave *>(event)); break;
|
||||
case ItemId_Event_GameClosed: eventGameClosed(qobject_cast<Event_GameClosed *>(event)); break;
|
||||
case ItemId_Event_SetActivePlayer: eventSetActivePlayer(qobject_cast<Event_SetActivePlayer *>(event)); break;
|
||||
case ItemId_Event_SetActivePhase: eventSetActivePhase(qobject_cast<Event_SetActivePhase *>(event)); break;
|
||||
case ItemId_Event_Ping: eventPing(qobject_cast<Event_Ping *>(event)); break;
|
||||
|
||||
default: {
|
||||
Player *player = players.value(event->getPlayerId(), 0);
|
||||
if (!player) {
|
||||
qDebug() << "unhandled game event: invalid player id";
|
||||
break;
|
||||
}
|
||||
player->processGameEvent(event);
|
||||
emit userEvent();
|
||||
}
|
||||
player->processGameEvent(event);
|
||||
emit userEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -255,6 +260,17 @@ void TabGame::sendGameCommand(GameCommand *command)
|
|||
client->sendCommand(command);
|
||||
}
|
||||
|
||||
void TabGame::sendCommandContainer(CommandContainer *cont)
|
||||
{
|
||||
const QList<Command *> &cmdList = cont->getCommandList();
|
||||
for (int i = 0; i < cmdList.size(); ++i) {
|
||||
GameCommand *cmd = qobject_cast<GameCommand *>(cmdList[i]);
|
||||
if (cmd)
|
||||
cmd->setGameId(gameId);
|
||||
}
|
||||
client->sendCommandContainer(cont);
|
||||
}
|
||||
|
||||
void TabGame::startGame()
|
||||
{
|
||||
currentPhase = -1;
|
||||
|
|
|
|||
|
|
@ -20,8 +20,9 @@ class ZoneViewWidget;
|
|||
class PhasesToolbar;
|
||||
class PlayerListWidget;
|
||||
class ProtocolResponse;
|
||||
class GameEvent;
|
||||
class GameEventContainer;
|
||||
class GameCommand;
|
||||
class CommandContainer;
|
||||
class Event_GameStateChanged;
|
||||
class Event_Join;
|
||||
class Event_Leave;
|
||||
|
|
@ -103,9 +104,10 @@ public:
|
|||
int getGameId() const { return gameId; }
|
||||
QString getTabText() const { return tr("Game %1: %2").arg(gameId).arg(gameDescription); }
|
||||
|
||||
void processGameEvent(GameEvent *event);
|
||||
void processGameEventContainer(GameEventContainer *cont);
|
||||
public slots:
|
||||
void sendGameCommand(GameCommand *command);
|
||||
void sendCommandContainer(CommandContainer *cont);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ void TabSupervisor::start(Client *_client)
|
|||
{
|
||||
client = _client;
|
||||
connect(client, SIGNAL(chatEventReceived(ChatEvent *)), this, SLOT(processChatEvent(ChatEvent *)));
|
||||
connect(client, SIGNAL(gameEventReceived(GameEvent *)), this, SLOT(processGameEvent(GameEvent *)));
|
||||
connect(client, SIGNAL(gameEventContainerReceived(GameEventContainer *)), this, SLOT(processGameEventContainer(GameEventContainer *)));
|
||||
connect(client, SIGNAL(gameJoinedEventReceived(Event_GameJoined *)), this, SLOT(gameJoined(Event_GameJoined *)));
|
||||
connect(client, SIGNAL(maxPingTime(int, int)), this, SLOT(updatePingTime(int, int)));
|
||||
|
||||
|
|
@ -152,12 +152,12 @@ void TabSupervisor::processChatEvent(ChatEvent *event)
|
|||
tab->processChatEvent(event);
|
||||
}
|
||||
|
||||
void TabSupervisor::processGameEvent(GameEvent *event)
|
||||
void TabSupervisor::processGameEventContainer(GameEventContainer *cont)
|
||||
{
|
||||
TabGame *tab = gameTabs.value(event->getGameId());
|
||||
TabGame *tab = gameTabs.value(cont->getGameId());
|
||||
if (tab) {
|
||||
qDebug() << "gameEvent gameId =" << event->getGameId();
|
||||
tab->processGameEvent(event);
|
||||
qDebug() << "gameEvent gameId =" << cont->getGameId();
|
||||
tab->processGameEventContainer(cont);
|
||||
} else
|
||||
qDebug() << "gameEvent: invalid gameId";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ class TabChatChannel;
|
|||
class TabGame;
|
||||
class TabDeckStorage;
|
||||
class ChatEvent;
|
||||
class GameEvent;
|
||||
class GameEventContainer;
|
||||
class Event_GameJoined;
|
||||
|
||||
class TabSupervisor : public QTabWidget {
|
||||
|
|
@ -42,7 +42,7 @@ private slots:
|
|||
void chatChannelLeft(TabChatChannel *tab);
|
||||
void tabUserEvent();
|
||||
void processChatEvent(ChatEvent *event);
|
||||
void processGameEvent(GameEvent *event);
|
||||
void processGameEventContainer(GameEventContainer *cont);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -85,11 +85,13 @@ void TableZone::toggleTapped()
|
|||
tapAll = true;
|
||||
break;
|
||||
}
|
||||
QList<Command *> cmdList;
|
||||
for (int i = 0; i < selectedItems.size(); i++) {
|
||||
CardItem *temp = qgraphicsitem_cast<CardItem *>(selectedItems[i]);
|
||||
if (temp->getTapped() != tapAll)
|
||||
setCardAttr(temp->getId(), "tapped", tapAll ? "1" : "0");
|
||||
cmdList.append(new Command_SetCardAttr(-1, name, temp->getId(), "tapped", tapAll ? "1" : "0"));
|
||||
}
|
||||
player->sendCommandContainer(new CommandContainer(cmdList));
|
||||
}
|
||||
|
||||
CardItem *TableZone::takeCard(int position, int cardId, const QString &cardName, bool canResize)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue