decklist transfer code

This commit is contained in:
Max-Wilhelm Bruker 2009-11-22 00:34:31 +01:00
parent 63f9206eb4
commit 8dcf81654e
32 changed files with 694 additions and 260 deletions

View file

@ -26,14 +26,19 @@ InnerDecklistNode::~InnerDecklistNode()
clearTree();
}
QString InnerDecklistNode::getVisibleName() const
QString InnerDecklistNode::visibleNameFromName(const QString &_name)
{
if (name == "main")
if (_name == "main")
return QObject::tr("Maindeck");
else if (name == "side")
else if (_name == "side")
return QObject::tr("Sideboard");
else
return getName();
return _name;
}
QString InnerDecklistNode::getVisibleName() const
{
return visibleNameFromName(name);
}
void InnerDecklistNode::clearTree()

View file

@ -39,6 +39,7 @@ public:
virtual ~InnerDecklistNode();
QString getName() const { return name; }
void setName(const QString &_name) { name = _name; }
static QString visibleNameFromName(const QString &_name);
virtual QString getVisibleName() const;
void clearTree();
AbstractDecklistNode *findChild(const QString &name);

View file

@ -66,6 +66,7 @@ void ProtocolItem::initializeHash()
initializeHashAuto();
itemNameHash.insert("cmddeck_upload", Command_DeckUpload::newItem);
itemNameHash.insert("cmddeck_select", Command_DeckSelect::newItem);
itemNameHash.insert("resp", ProtocolResponse::newItem);
ProtocolResponse::initializeHash();
@ -102,17 +103,12 @@ void Command::processResponse(ProtocolResponse *response)
emit finished(response->getResponseCode());
}
Command_DeckUpload::Command_DeckUpload(int _cmdId, DeckList *_deck, const QString &_path)
: Command("deck_upload", _cmdId), deck(_deck), path(_path), readFinished(false)
Command_DeckUpload::Command_DeckUpload(DeckList *_deck, const QString &_path)
: Command("deck_upload"), deck(_deck), path(_path), readFinished(false)
{
setParameter("path", path);
}
Command_DeckUpload::~Command_DeckUpload()
{
delete deck;
}
void Command_DeckUpload::extractParameters()
{
Command::extractParameters();
@ -144,6 +140,45 @@ void Command_DeckUpload::writeElement(QXmlStreamWriter *xml)
deck->writeElement(xml);
}
Command_DeckSelect::Command_DeckSelect(int _gameId, DeckList *_deck, int _deckId)
: GameCommand("deck_upload", _gameId), deck(_deck), deckId(_deckId), readFinished(false)
{
setParameter("deck_id", _deckId);
}
void Command_DeckSelect::extractParameters()
{
GameCommand::extractParameters();
bool ok;
deckId = parameters["deck_id"].toInt(&ok);
if (!ok)
deckId = -1;
}
bool Command_DeckSelect::readElement(QXmlStreamReader *xml)
{
if (readFinished)
return false;
if (!deck) {
if (xml->isStartElement() && (xml->name() == "cockatrice_deck")) {
deck = new DeckList;
return true;
}
return false;
}
if (deck->readElement(xml))
readFinished = true;
return true;
}
void Command_DeckSelect::writeElement(QXmlStreamWriter *xml)
{
if (deck)
deck->writeElement(xml);
}
QHash<QString, ResponseCode> ProtocolResponse::responseHash;

View file

@ -17,13 +17,14 @@ class ProtocolResponse;
class DeckList;
enum ItemId {
ItemId_Command_DeckUpload = ItemId_Other + 1,
ItemId_Event_ListChatChannels = ItemId_Other + 2,
ItemId_Event_ChatListPlayers = ItemId_Other + 3,
ItemId_Event_ListGames = ItemId_Other + 4,
ItemId_Response_DeckList = ItemId_Other + 5,
ItemId_Response_DeckDownload = ItemId_Other + 6,
ItemId_Response_DeckUpload = ItemId_Other + 7
ItemId_Command_DeckUpload = ItemId_Other + 100,
ItemId_Command_DeckSelect = ItemId_Other + 101,
ItemId_Event_ListChatChannels = ItemId_Other + 200,
ItemId_Event_ChatListPlayers = ItemId_Other + 201,
ItemId_Event_ListGames = ItemId_Other + 202,
ItemId_Response_DeckList = ItemId_Other + 300,
ItemId_Response_DeckDownload = ItemId_Other + 301,
ItemId_Response_DeckUpload = ItemId_Other + 302
};
class ProtocolItem : public QObject {
@ -138,14 +139,31 @@ protected:
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
public:
Command_DeckUpload(int _cmdId = -1, DeckList *_deck = 0, const QString &_path = QString());
~Command_DeckUpload();
Command_DeckUpload(DeckList *_deck = 0, const QString &_path = QString());
static ProtocolItem *newItem() { return new Command_DeckUpload; }
int getItemId() const { return ItemId_Command_DeckUpload; }
DeckList *getDeck() const { return deck; }
QString getPath() const { return path; }
};
class Command_DeckSelect : public GameCommand {
Q_OBJECT
private:
DeckList *deck;
int deckId;
bool readFinished;
protected:
void extractParameters();
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
public:
Command_DeckSelect(int _gameId = -1, DeckList *_deck = 0, int _deckId = -1);
static ProtocolItem *newItem() { return new Command_DeckSelect; }
int getItemId() const { return ItemId_Command_DeckSelect; }
DeckList *getDeck() const { return deck; }
int getDeckId() const { return deckId; }
};
// -----------------
// --- RESPONSES ---
// -----------------

View file

@ -33,10 +33,10 @@ ItemId_Command_SetActivePhase = 1031,
ItemId_Command_DumpZone = 1032,
ItemId_Command_StopDumpZone = 1033,
ItemId_Command_DumpAll = 1034,
ItemId_Command_SubmitDeck = 1035,
ItemId_Event_Say = 1036,
ItemId_Event_Join = 1037,
ItemId_Event_Leave = 1038,
ItemId_Event_Say = 1035,
ItemId_Event_Join = 1036,
ItemId_Event_Leave = 1037,
ItemId_Event_DeckSelect = 1038,
ItemId_Event_GameClosed = 1039,
ItemId_Event_ReadyStart = 1040,
ItemId_Event_SetupZones = 1041,

View file

@ -347,10 +347,6 @@ Command_DumpAll::Command_DumpAll(int _gameId)
: GameCommand("dump_all", _gameId)
{
}
Command_SubmitDeck::Command_SubmitDeck(int _gameId)
: GameCommand("submit_deck", _gameId)
{
}
Event_Say::Event_Say(int _gameId, int _playerId, const QString &_message)
: GameEvent("say", _gameId, _playerId), message(_message)
{
@ -377,6 +373,16 @@ Event_Leave::Event_Leave(int _gameId, int _playerId)
: GameEvent("leave", _gameId, _playerId)
{
}
Event_DeckSelect::Event_DeckSelect(int _gameId, int _playerId, int _deckId)
: GameEvent("deck_select", _gameId, _playerId), deckId(_deckId)
{
setParameter("deck_id", deckId);
}
void Event_DeckSelect::extractParameters()
{
GameEvent::extractParameters();
deckId = parameters["deck_id"].toInt();
}
Event_GameClosed::Event_GameClosed(int _gameId, int _playerId)
: GameEvent("game_closed", _gameId, _playerId)
{
@ -687,10 +693,10 @@ void ProtocolItem::initializeHashAuto()
itemNameHash.insert("cmddump_zone", Command_DumpZone::newItem);
itemNameHash.insert("cmdstop_dump_zone", Command_StopDumpZone::newItem);
itemNameHash.insert("cmddump_all", Command_DumpAll::newItem);
itemNameHash.insert("cmdsubmit_deck", Command_SubmitDeck::newItem);
itemNameHash.insert("game_eventsay", Event_Say::newItem);
itemNameHash.insert("game_eventjoin", Event_Join::newItem);
itemNameHash.insert("game_eventleave", Event_Leave::newItem);
itemNameHash.insert("game_eventdeck_select", Event_DeckSelect::newItem);
itemNameHash.insert("game_eventgame_closed", Event_GameClosed::newItem);
itemNameHash.insert("game_eventready_start", Event_ReadyStart::newItem);
itemNameHash.insert("game_eventsetup_zones", Event_SetupZones::newItem);

View file

@ -32,10 +32,10 @@
2:dump_zone:i,player_id:s,zone_name:i,number_cards
2:stop_dump_zone:i,player_id:s,zone_name
2:dump_all
2:submit_deck
3:say:s,message
3:join:s,player_name:b,spectator
3:leave
3:deck_select:i,deck_id
3:game_closed
3:ready_start
3:setup_zones:i,deck_size:i,sb_size

View file

@ -437,14 +437,6 @@ public:
static ProtocolItem *newItem() { return new Command_DumpAll; }
int getItemId() const { return ItemId_Command_DumpAll; }
};
class Command_SubmitDeck : public GameCommand {
Q_OBJECT
private:
public:
Command_SubmitDeck(int _gameId = -1);
static ProtocolItem *newItem() { return new Command_SubmitDeck; }
int getItemId() const { return ItemId_Command_SubmitDeck; }
};
class Event_Say : public GameEvent {
Q_OBJECT
private:
@ -479,6 +471,18 @@ public:
static ProtocolItem *newItem() { return new Event_Leave; }
int getItemId() const { return ItemId_Event_Leave; }
};
class Event_DeckSelect : public GameEvent {
Q_OBJECT
private:
int deckId;
public:
Event_DeckSelect(int _gameId = -1, int _playerId = -1, int _deckId = -1);
int getDeckId() const { return deckId; }
static ProtocolItem *newItem() { return new Event_DeckSelect; }
int getItemId() const { return ItemId_Event_DeckSelect; }
protected:
void extractParameters();
};
class Event_GameClosed : public GameEvent {
Q_OBJECT
private:

View file

@ -7,9 +7,10 @@
#include "server_protocolhandler.h"
#include "protocol.h"
#include "protocol_items.h"
#include "decklist.h"
Server_Player::Server_Player(Server_Game *_game, int _playerId, const QString &_playerName, bool _spectator, Server_ProtocolHandler *_handler)
: game(_game), handler(_handler), playerId(_playerId), playerName(_playerName), spectator(_spectator), nextCardId(0), PlayerStatus(StatusNormal)
: game(_game), handler(_handler), deck(0), playerId(_playerId), playerName(_playerName), spectator(_spectator), nextCardId(0), PlayerStatus(StatusNormal)
{
}
@ -51,10 +52,10 @@ void Server_Player::setupZones()
// ------------------------------------------------------------------
// Create zones
Server_CardZone *deck = new Server_CardZone(this, "deck", false, Server_CardZone::HiddenZone);
addZone(deck);
Server_CardZone *sb = new Server_CardZone(this, "sb", false, Server_CardZone::HiddenZone);
addZone(sb);
Server_CardZone *deckZone = new Server_CardZone(this, "deck", false, Server_CardZone::HiddenZone);
addZone(deckZone);
Server_CardZone *sbZone = new Server_CardZone(this, "sb", false, Server_CardZone::HiddenZone);
addZone(sbZone);
addZone(new Server_CardZone(this, "table", true, Server_CardZone::PublicZone));
addZone(new Server_CardZone(this, "hand", false, Server_CardZone::PrivateZone));
addZone(new Server_CardZone(this, "grave", false, Server_CardZone::PublicZone));
@ -63,20 +64,30 @@ void Server_Player::setupZones()
// ------------------------------------------------------------------
// Assign card ids and create deck from decklist
QListIterator<QString> DeckIterator(DeckList);
int i = 0;
while (DeckIterator.hasNext())
deck->cards.append(new Server_Card(DeckIterator.next(), i++, 0, 0));
deck->shuffle();
InnerDecklistNode *listRoot = deck->getRoot();
nextCardId = 0;
for (int i = 0; i < listRoot->size(); ++i) {
InnerDecklistNode *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
Server_CardZone *z;
if (currentZone->getName() == "main")
z = deckZone;
else if (currentZone->getName() == "side")
z = sbZone;
else
continue;
for (int j = 0; j < currentZone->size(); ++j) {
DecklistCardNode *currentCard = dynamic_cast<DecklistCardNode *>(currentZone->at(j));
if (!currentCard)
continue;
for (int k = 0; k < currentCard->getNumber(); ++k)
z->cards.append(new Server_Card(currentCard->getName(), nextCardId++, 0, 0));
}
}
deckZone->shuffle();
QListIterator<QString> SBIterator(SideboardList);
while (SBIterator.hasNext())
sb->cards.append(new Server_Card(SBIterator.next(), i++, 0, 0));
nextCardId = i;
PlayerStatus = StatusPlaying;
game->sendGameEvent(new Event_SetupZones(-1, playerId, deck->cards.size(), sb->cards.size()));
game->sendGameEvent(new Event_SetupZones(-1, playerId, deckZone->cards.size(), sbZone->cards.size()));
}
void Server_Player::clearZones()
@ -97,6 +108,12 @@ void Server_Player::clearZones()
arrows.clear();
}
void Server_Player::setDeck(DeckList *_deck)
{
delete deck;
deck = _deck;
}
void Server_Player::addZone(Server_CardZone *zone)
{
zones.insert(zone->getName(), zone);

View file

@ -6,6 +6,7 @@
#include <QList>
#include <QMap>
class DeckList;
class Server_Game;
class Server_CardZone;
class Server_Counter;
@ -20,6 +21,7 @@ class Server_Player : public QObject {
private:
Server_Game *game;
Server_ProtocolHandler *handler;
DeckList *deck;
QMap<QString, Server_CardZone *> zones;
QMap<int, Server_Counter *> counters;
QMap<int, Server_Arrow *> arrows;
@ -30,11 +32,6 @@ private:
void clearZones();
PlayerStatusEnum PlayerStatus;
public:
// Pfusch
QList<QString> DeckList;
QList<QString> SideboardList;
// Pfusch Ende
Server_Player(Server_Game *_game, int _playerId, const QString &_playerName, bool _spectator, Server_ProtocolHandler *_handler);
void setProtocolHandler(Server_ProtocolHandler *_handler) { handler = _handler; }
@ -44,6 +41,8 @@ public:
int getPlayerId() const { return playerId; }
bool getSpectator() const { return spectator; }
QString getPlayerName() const { return playerName; }
void setDeck(DeckList *_deck);
DeckList *getDeck() const { return deck; }
const QMap<QString, Server_CardZone *> &getZones() const { return zones; }
const QMap<int, Server_Counter *> &getCounters() const { return counters; }
const QMap<int, Server_Arrow *> &getArrows() const { return arrows; }

View file

@ -67,6 +67,7 @@ void Server_ProtocolHandler::processCommand(Command *command)
Server_Player *player = gamePair.second;
switch (command->getItemId()) {
case ItemId_Command_DeckSelect: response = cmdDeckSelect(qobject_cast<Command_DeckSelect *>(command), game, player); break;
case ItemId_Command_LeaveGame: response = cmdLeaveGame(qobject_cast<Command_LeaveGame *>(command), game, player); break;
case ItemId_Command_Say: response = cmdSay(qobject_cast<Command_Say *>(command), game, player); break;
case ItemId_Command_Shuffle: response = cmdShuffle(qobject_cast<Command_Shuffle *>(command), game, player); break;
@ -87,7 +88,6 @@ void Server_ProtocolHandler::processCommand(Command *command)
case ItemId_Command_DumpZone: response = cmdDumpZone(qobject_cast<Command_DumpZone *>(command), game, player); break;
case ItemId_Command_StopDumpZone: response = cmdStopDumpZone(qobject_cast<Command_StopDumpZone *>(command), game, player); break;
case ItemId_Command_DumpAll: response = cmdDumpAll(qobject_cast<Command_DumpAll *>(command), game, player); break;
case ItemId_Command_SubmitDeck: response = cmdSubmitDeck(qobject_cast<Command_SubmitDeck *>(command), game, player); break;
}
} else {
qDebug() << "received generic Command";
@ -245,6 +245,27 @@ ResponseCode Server_ProtocolHandler::cmdLeaveGame(Command_LeaveGame * /*cmd*/, S
return RespOk;
}
ResponseCode Server_ProtocolHandler::cmdDeckSelect(Command_DeckSelect *cmd, Server_Game *game, Server_Player *player)
{
DeckList *deck;
if (cmd->getDeckId() == -1) {
if (!cmd->getDeck())
return RespInvalidData;
deck = cmd->getDeck();
} else {
try {
deck = getDeckFromDatabase(cmd->getDeckId());
} catch(ResponseCode r) {
return r;
}
}
player->setDeck(deck);
game->sendGameEvent(new Event_DeckSelect(-1, player->getPlayerId(), cmd->getDeckId()));
return RespOk;
}
ResponseCode Server_ProtocolHandler::cmdSay(Command_Say *cmd, Server_Game *game, Server_Player *player)
{
game->sendGameEvent(new Event_Say(-1, player->getPlayerId(), cmd->getMessage()));
@ -468,6 +489,9 @@ ResponseCode Server_ProtocolHandler::cmdSetCardAttr(Command_SetCardAttr *cmd, Se
ResponseCode Server_ProtocolHandler::cmdReadyStart(Command_ReadyStart * /*cmd*/, Server_Game *game, Server_Player *player)
{
if (!player->getDeck())
return RespContextError;
player->setStatus(StatusReadyStart);
game->sendGameEvent(new Event_ReadyStart(-1, player->getPlayerId()));
game->startGameIfReady();
@ -570,8 +594,3 @@ ResponseCode Server_ProtocolHandler::cmdDumpAll(Command_DumpAll *cmd, Server_Gam
{
return RespOk;
}
ResponseCode Server_ProtocolHandler::cmdSubmitDeck(Command_SubmitDeck *cmd, Server_Game *game, Server_Player *player)
{
return RespOk;
}

View file

@ -27,6 +27,8 @@ protected:
private:
QList<ProtocolItem *> itemQueue;
virtual DeckList *getDeckFromDatabase(int deckId) = 0;
ResponseCode cmdPing(Command_Ping *cmd);
ResponseCode cmdLogin(Command_Login *cmd);
virtual ResponseCode cmdDeckList(Command_DeckList *cmd) = 0;
@ -43,6 +45,7 @@ private:
ResponseCode cmdCreateGame(Command_CreateGame *cmd);
ResponseCode cmdJoinGame(Command_JoinGame *cmd);
ResponseCode cmdLeaveGame(Command_LeaveGame *cmd, Server_Game *game, Server_Player *player);
ResponseCode cmdDeckSelect(Command_DeckSelect *cmd, Server_Game *game, Server_Player *player);
ResponseCode cmdSay(Command_Say *cmd, Server_Game *game, Server_Player *player);
ResponseCode cmdShuffle(Command_Shuffle *cmd, Server_Game *game, Server_Player *player);
ResponseCode cmdRollDie(Command_RollDie *cmd, Server_Game *game, Server_Player *player);
@ -62,7 +65,6 @@ private:
ResponseCode cmdDumpZone(Command_DumpZone *cmd, Server_Game *game, Server_Player *player);
ResponseCode cmdStopDumpZone(Command_StopDumpZone *cmd, Server_Game *game, Server_Player *player);
ResponseCode cmdDumpAll(Command_DumpAll *cmd, Server_Game *game, Server_Player *player);
ResponseCode cmdSubmitDeck(Command_SubmitDeck *cmd, Server_Game *game, Server_Player *player);
public:
Server_ProtocolHandler(Server *_server, QObject *parent = 0);
~Server_ProtocolHandler();