Commit 8dcf8165 authored by Max-Wilhelm Bruker's avatar Max-Wilhelm Bruker
Browse files

decklist transfer code

parent 63f9206e
...@@ -66,6 +66,7 @@ void ProtocolItem::initializeHash() ...@@ -66,6 +66,7 @@ void ProtocolItem::initializeHash()
initializeHashAuto(); initializeHashAuto();
itemNameHash.insert("cmddeck_upload", Command_DeckUpload::newItem); itemNameHash.insert("cmddeck_upload", Command_DeckUpload::newItem);
itemNameHash.insert("cmddeck_select", Command_DeckSelect::newItem);
itemNameHash.insert("resp", ProtocolResponse::newItem); itemNameHash.insert("resp", ProtocolResponse::newItem);
ProtocolResponse::initializeHash(); ProtocolResponse::initializeHash();
...@@ -102,17 +103,12 @@ void Command::processResponse(ProtocolResponse *response) ...@@ -102,17 +103,12 @@ void Command::processResponse(ProtocolResponse *response)
emit finished(response->getResponseCode()); emit finished(response->getResponseCode());
} }
Command_DeckUpload::Command_DeckUpload(int _cmdId, DeckList *_deck, const QString &_path) Command_DeckUpload::Command_DeckUpload(DeckList *_deck, const QString &_path)
: Command("deck_upload", _cmdId), deck(_deck), path(_path), readFinished(false) : Command("deck_upload"), deck(_deck), path(_path), readFinished(false)
{ {
setParameter("path", path); setParameter("path", path);
} }
Command_DeckUpload::~Command_DeckUpload()
{
delete deck;
}
void Command_DeckUpload::extractParameters() void Command_DeckUpload::extractParameters()
{ {
Command::extractParameters(); Command::extractParameters();
...@@ -144,6 +140,45 @@ void Command_DeckUpload::writeElement(QXmlStreamWriter *xml) ...@@ -144,6 +140,45 @@ void Command_DeckUpload::writeElement(QXmlStreamWriter *xml)
deck->writeElement(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; QHash<QString, ResponseCode> ProtocolResponse::responseHash;
......
...@@ -17,13 +17,14 @@ class ProtocolResponse; ...@@ -17,13 +17,14 @@ class ProtocolResponse;
class DeckList; class DeckList;
enum ItemId { enum ItemId {
ItemId_Command_DeckUpload = ItemId_Other + 1, ItemId_Command_DeckUpload = ItemId_Other + 100,
ItemId_Event_ListChatChannels = ItemId_Other + 2, ItemId_Command_DeckSelect = ItemId_Other + 101,
ItemId_Event_ChatListPlayers = ItemId_Other + 3, ItemId_Event_ListChatChannels = ItemId_Other + 200,
ItemId_Event_ListGames = ItemId_Other + 4, ItemId_Event_ChatListPlayers = ItemId_Other + 201,
ItemId_Response_DeckList = ItemId_Other + 5, ItemId_Event_ListGames = ItemId_Other + 202,
ItemId_Response_DeckDownload = ItemId_Other + 6, ItemId_Response_DeckList = ItemId_Other + 300,
ItemId_Response_DeckUpload = ItemId_Other + 7 ItemId_Response_DeckDownload = ItemId_Other + 301,
ItemId_Response_DeckUpload = ItemId_Other + 302
}; };
class ProtocolItem : public QObject { class ProtocolItem : public QObject {
...@@ -138,14 +139,31 @@ protected: ...@@ -138,14 +139,31 @@ protected:
bool readElement(QXmlStreamReader *xml); bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml); void writeElement(QXmlStreamWriter *xml);
public: public:
Command_DeckUpload(int _cmdId = -1, DeckList *_deck = 0, const QString &_path = QString()); Command_DeckUpload(DeckList *_deck = 0, const QString &_path = QString());
~Command_DeckUpload();
static ProtocolItem *newItem() { return new Command_DeckUpload; } static ProtocolItem *newItem() { return new Command_DeckUpload; }
int getItemId() const { return ItemId_Command_DeckUpload; } int getItemId() const { return ItemId_Command_DeckUpload; }
DeckList *getDeck() const { return deck; } DeckList *getDeck() const { return deck; }
QString getPath() const { return path; } 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 --- // --- RESPONSES ---
// ----------------- // -----------------
......
...@@ -33,10 +33,10 @@ ItemId_Command_SetActivePhase = 1031, ...@@ -33,10 +33,10 @@ ItemId_Command_SetActivePhase = 1031,
ItemId_Command_DumpZone = 1032, ItemId_Command_DumpZone = 1032,
ItemId_Command_StopDumpZone = 1033, ItemId_Command_StopDumpZone = 1033,
ItemId_Command_DumpAll = 1034, ItemId_Command_DumpAll = 1034,
ItemId_Command_SubmitDeck = 1035, ItemId_Event_Say = 1035,
ItemId_Event_Say = 1036, ItemId_Event_Join = 1036,
ItemId_Event_Join = 1037, ItemId_Event_Leave = 1037,
ItemId_Event_Leave = 1038, ItemId_Event_DeckSelect = 1038,
ItemId_Event_GameClosed = 1039, ItemId_Event_GameClosed = 1039,
ItemId_Event_ReadyStart = 1040, ItemId_Event_ReadyStart = 1040,
ItemId_Event_SetupZones = 1041, ItemId_Event_SetupZones = 1041,
......
...@@ -347,10 +347,6 @@ Command_DumpAll::Command_DumpAll(int _gameId) ...@@ -347,10 +347,6 @@ Command_DumpAll::Command_DumpAll(int _gameId)
: GameCommand("dump_all", _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) Event_Say::Event_Say(int _gameId, int _playerId, const QString &_message)
: GameEvent("say", _gameId, _playerId), message(_message) : GameEvent("say", _gameId, _playerId), message(_message)
{ {
...@@ -377,6 +373,16 @@ Event_Leave::Event_Leave(int _gameId, int _playerId) ...@@ -377,6 +373,16 @@ Event_Leave::Event_Leave(int _gameId, int _playerId)
: GameEvent("leave", _gameId, _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) Event_GameClosed::Event_GameClosed(int _gameId, int _playerId)
: GameEvent("game_closed", _gameId, _playerId) : GameEvent("game_closed", _gameId, _playerId)
{ {
...@@ -687,10 +693,10 @@ void ProtocolItem::initializeHashAuto() ...@@ -687,10 +693,10 @@ void ProtocolItem::initializeHashAuto()
itemNameHash.insert("cmddump_zone", Command_DumpZone::newItem); itemNameHash.insert("cmddump_zone", Command_DumpZone::newItem);
itemNameHash.insert("cmdstop_dump_zone", Command_StopDumpZone::newItem); itemNameHash.insert("cmdstop_dump_zone", Command_StopDumpZone::newItem);
itemNameHash.insert("cmddump_all", Command_DumpAll::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_eventsay", Event_Say::newItem);
itemNameHash.insert("game_eventjoin", Event_Join::newItem); itemNameHash.insert("game_eventjoin", Event_Join::newItem);
itemNameHash.insert("game_eventleave", Event_Leave::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_eventgame_closed", Event_GameClosed::newItem);
itemNameHash.insert("game_eventready_start", Event_ReadyStart::newItem); itemNameHash.insert("game_eventready_start", Event_ReadyStart::newItem);
itemNameHash.insert("game_eventsetup_zones", Event_SetupZones::newItem); itemNameHash.insert("game_eventsetup_zones", Event_SetupZones::newItem);
......
...@@ -32,10 +32,10 @@ ...@@ -32,10 +32,10 @@
2:dump_zone:i,player_id:s,zone_name:i,number_cards 2:dump_zone:i,player_id:s,zone_name:i,number_cards
2:stop_dump_zone:i,player_id:s,zone_name 2:stop_dump_zone:i,player_id:s,zone_name
2:dump_all 2:dump_all
2:submit_deck
3:say:s,message 3:say:s,message
3:join:s,player_name:b,spectator 3:join:s,player_name:b,spectator
3:leave 3:leave
3:deck_select:i,deck_id
3:game_closed 3:game_closed
3:ready_start 3:ready_start
3:setup_zones:i,deck_size:i,sb_size 3:setup_zones:i,deck_size:i,sb_size
......
...@@ -437,14 +437,6 @@ public: ...@@ -437,14 +437,6 @@ public:
static ProtocolItem *newItem() { return new Command_DumpAll; } static ProtocolItem *newItem() { return new Command_DumpAll; }
int getItemId() const { return ItemId_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 { class Event_Say : public GameEvent {
Q_OBJECT Q_OBJECT
private: private:
...@@ -479,6 +471,18 @@ public: ...@@ -479,6 +471,18 @@ public:
static ProtocolItem *newItem() { return new Event_Leave; } static ProtocolItem *newItem() { return new Event_Leave; }
int getItemId() const { return ItemId_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 { class Event_GameClosed : public GameEvent {
Q_OBJECT Q_OBJECT
private: private:
......
...@@ -7,9 +7,10 @@ ...@@ -7,9 +7,10 @@
#include "server_protocolhandler.h" #include "server_protocolhandler.h"
#include "protocol.h" #include "protocol.h"
#include "protocol_items.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) 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() ...@@ -51,10 +52,10 @@ void Server_Player::setupZones()
// ------------------------------------------------------------------ // ------------------------------------------------------------------
// Create zones // Create zones
Server_CardZone *deck = new Server_CardZone(this, "deck", false, Server_CardZone::HiddenZone); Server_CardZone *deckZone = new Server_CardZone(this, "deck", false, Server_CardZone::HiddenZone);
addZone(deck); addZone(deckZone);
Server_CardZone *sb = new Server_CardZone(this, "sb", false, Server_CardZone::HiddenZone); Server_CardZone *sbZone = new Server_CardZone(this, "sb", false, Server_CardZone::HiddenZone);
addZone(sb); addZone(sbZone);
addZone(new Server_CardZone(this, "table", true, Server_CardZone::PublicZone)); 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, "hand", false, Server_CardZone::PrivateZone));
addZone(new Server_CardZone(this, "grave", false, Server_CardZone::PublicZone)); addZone(new Server_CardZone(this, "grave", false, Server_CardZone::PublicZone));
...@@ -63,20 +64,30 @@ void Server_Player::setupZones() ...@@ -63,20 +64,30 @@ void Server_Player::setupZones()
// ------------------------------------------------------------------ // ------------------------------------------------------------------
// Assign card ids and create deck from decklist // Assign card ids and create deck from decklist
QListIterator<QString> DeckIterator(DeckList); InnerDecklistNode *listRoot = deck->getRoot();
int i = 0; nextCardId = 0;
while (DeckIterator.hasNext()) for (int i = 0; i < listRoot->size(); ++i) {
deck->cards.append(new Server_Card(DeckIterator.next(), i++, 0, 0)); InnerDecklistNode *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
deck->shuffle(); Server_CardZone *z;
if (currentZone->getName() == "main")
QListIterator<QString> SBIterator(SideboardList); z = deckZone;
while (SBIterator.hasNext()) else if (currentZone->getName() == "side")
sb->cards.append(new Server_Card(SBIterator.next(), i++, 0, 0)); 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();
nextCardId = i;
PlayerStatus = StatusPlaying; 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() void Server_Player::clearZones()
...@@ -97,6 +108,12 @@ void Server_Player::clearZones() ...@@ -97,6 +108,12 @@ void Server_Player::clearZones()
arrows.clear(); arrows.clear();
} }
void Server_Player::setDeck(DeckList *_deck)
{
delete deck;
deck = _deck;
}
void Server_Player::addZone(Server_CardZone *zone) void Server_Player::addZone(Server_CardZone *zone)
{ {
zones.insert(zone->getName(), zone); zones.insert(zone->getName(), zone);
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <QList> #include <QList>
#include <QMap> #include <QMap>
class DeckList;
class Server_Game; class Server_Game;
class Server_CardZone; class Server_CardZone;
class Server_Counter; class Server_Counter;
...@@ -20,6 +21,7 @@ class Server_Player : public QObject { ...@@ -20,6 +21,7 @@ class Server_Player : public QObject {
private: private:
Server_Game *game; Server_Game *game;
Server_ProtocolHandler *handler; Server_ProtocolHandler *handler;
DeckList *deck;
QMap<QString, Server_CardZone *> zones; QMap<QString, Server_CardZone *> zones;
QMap<int, Server_Counter *> counters; QMap<int, Server_Counter *> counters;
QMap<int, Server_Arrow *> arrows; QMap<int, Server_Arrow *> arrows;
...@@ -30,11 +32,6 @@ private: ...@@ -30,11 +32,6 @@ private:
void clearZones(); void clearZones();
PlayerStatusEnum PlayerStatus; PlayerStatusEnum PlayerStatus;
public: 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); Server_Player(Server_Game *_game, int _playerId, const QString &_playerName, bool _spectator, Server_ProtocolHandler *_handler);
void setProtocolHandler(Server_ProtocolHandler *_handler) { handler = _handler; } void setProtocolHandler(Server_ProtocolHandler *_handler) { handler = _handler; }
...@@ -44,6 +41,8 @@ public: ...@@ -44,6 +41,8 @@ public:
int getPlayerId() const { return playerId; } int getPlayerId() const { return playerId; }
bool getSpectator() const { return spectator; } bool getSpectator() const { return spectator; }
QString getPlayerName() const { return playerName; } 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<QString, Server_CardZone *> &getZones() const { return zones; }
const QMap<int, Server_Counter *> &getCounters() const { return counters; } const QMap<int, Server_Counter *> &getCounters() const { return counters; }
const QMap<int, Server_Arrow *> &getArrows() const { return arrows; } const QMap<int, Server_Arrow *> &getArrows() const { return arrows; }
......
...@@ -67,6 +67,7 @@ void Server_ProtocolHandler::processCommand(Command *command) ...@@ -67,6 +67,7 @@ void Server_ProtocolHandler::processCommand(Command *command)
Server_Player *player = gamePair.second; Server_Player *player = gamePair.second;
switch (command->getItemId()) { 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_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_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; case ItemId_Command_Shuffle: response = cmdShuffle(qobject_cast<Command_Shuffle *>(command), game, player); break;
...@@ -87,7 +88,6 @@ void Server_ProtocolHandler::processCommand(Command *command) ...@@ -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_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_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_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 { } else {
qDebug() << "received generic Command"; qDebug() << "received generic Command";
...@@ -245,6 +245,27 @@ ResponseCode Server_ProtocolHandler::cmdLeaveGame(Command_LeaveGame * /*cmd*/, S ...@@ -245,6 +245,27 @@ ResponseCode Server_ProtocolHandler::cmdLeaveGame(Command_LeaveGame * /*cmd*/, S
return RespOk; 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) ResponseCode Server_ProtocolHandler::cmdSay(Command_Say *cmd, Server_Game *game, Server_Player *player)
{ {
game->sendGameEvent(new Event_Say(-1, player->getPlayerId(), cmd->getMessage())); game->sendGameEvent(new Event_Say(-1, player->getPlayerId(), cmd->getMessage()));
...@@ -468,6 +489,9 @@ ResponseCode Server_ProtocolHandler::cmdSetCardAttr(Command_SetCardAttr *cmd, Se ...@@ -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) ResponseCode Server_ProtocolHandler::cmdReadyStart(Command_ReadyStart * /*cmd*/, Server_Game *game, Server_Player *player)
{ {
if (!player->getDeck())
return RespContextError;
player->setStatus(StatusReadyStart); player->setStatus(StatusReadyStart);
game->sendGameEvent(new Event_ReadyStart(-1, player->getPlayerId())); game->sendGameEvent(new Event_ReadyStart(-1, player->getPlayerId()));
game->startGameIfReady(); game->startGameIfReady();
...@@ -570,8 +594,3 @@ ResponseCode Server_ProtocolHandler::cmdDumpAll(Command_DumpAll *cmd, Server_Gam ...@@ -570,8 +594,3 @@ ResponseCode Server_ProtocolHandler::cmdDumpAll(Command_DumpAll *cmd, Server_Gam
{ {
return RespOk; return RespOk;
} }
ResponseCode Server_ProtocolHandler::cmdSubmitDeck(Command_SubmitDeck *cmd, Server_Game *game, Server_Player *player)
{
return RespOk;
}
...@@ -27,6 +27,8 @@ protected: ...@@ -27,6 +27,8 @@ protected:
private: private:
QList<ProtocolItem *> itemQueue; QList<ProtocolItem *> itemQueue;
virtual DeckList *getDeckFromDatabase(int deckId) = 0;
ResponseCode cmdPing(Command_Ping *cmd); ResponseCode cmdPing(Command_Ping *cmd);
ResponseCode cmdLogin(Command_Login *cmd); ResponseCode cmdLogin(Command_Login *cmd);
virtual ResponseCode cmdDeckList(Command_DeckList *cmd) = 0; virtual ResponseCode cmdDeckList(Command_DeckList *cmd) = 0;
...@@ -43,6 +45,7 @@ private: ...@@ -43,6 +45,7 @@ private:
ResponseCode cmdCreateGame(Command_CreateGame *cmd); ResponseCode cmdCreateGame(Command_CreateGame *cmd);
ResponseCode cmdJoinGame(Command_JoinGame *cmd); ResponseCode cmdJoinGame(Command_JoinGame *cmd);
ResponseCode cmdLeaveGame(Command_LeaveGame *cmd, Server_Game *game, Server_Player *player); 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 cmdSay(Command_Say *cmd, Server_Game *game, Server_Player *player);
ResponseCode cmdShuffle(Command_Shuffle *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); ResponseCode cmdRollDie(Command_RollDie *cmd, Server_Game *game, Server_Player *player);
...@@ -62,7 +65,6 @@ private: ...@@ -62,7 +65,6 @@ private:
ResponseCode cmdDumpZone(Command_DumpZone *cmd, Server_Game *game, Server_Player *player); ResponseCode cmdDumpZone(Command_DumpZone *cmd, Server_Game *game, Server_Player *player);
ResponseCode cmdStopDumpZone(Command_StopDumpZone *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 cmdDumpAll(Command_DumpAll *cmd, Server_Game *game, Server_Player *player);
ResponseCode cmdSubmitDeck(Command_SubmitDeck *cmd, Server_Game *game, Server_Player *player);
public: public:
Server_ProtocolHandler(Server *_server, QObject *parent = 0); Server_ProtocolHandler(Server *_server, QObject *parent = 0);
~Server_ProtocolHandler(); ~Server_ProtocolHandler();
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "protocol.h" #include "protocol.h"
#include "protocol_items.h" #include "protocol_items.h"
#include "decklist.h" #include "decklist.h"
#include "server_player.h"
ServerSocketInterface::ServerSocketInterface(Servatrice *_server, QTcpSocket *_socket, QObject *parent) ServerSocketInterface::ServerSocketInterface(Servatrice *_server, QTcpSocket *_socket, QObject *parent)
: Server_ProtocolHandler(_server, parent), servatrice(_server), socket(_socket), currentItem(0) : Server_ProtocolHandler(_server, parent), servatrice(_server), socket(_socket), currentItem(0)
...@@ -279,31 +280,41 @@ ResponseCode ServerSocketInterface::cmdDeckUpload(Command_DeckUpload *cmd) ...@@ -279,31 +280,41 @@ ResponseCode ServerSocketInterface::cmdDeckUpload(Command_DeckUpload *cmd)
query.bindValue(":content", deckContents); query.bindValue(":content", deckContents);
servatrice->execSqlQuery(query); servatrice->execSqlQuery(query);
delete cmd->getDeck();
sendProtocolItem(new Response_DeckUpload(cmd->getCmdId(), RespOk, new DeckList_File(deckName, query.lastInsertId().toInt(), QDateTime::currentDateTime()))); sendProtocolItem(new Response_DeckUpload(cmd->getCmdId(), RespOk, new DeckList_File(deckName, query.lastInsertId().toInt(), QDateTime::currentDateTime())));
return RespNothing; return RespNothing;
} }
ResponseCode ServerSocketInterface::cmdDeckDownload(Command_DeckDownload *cmd) DeckList *ServerSocketInterface::getDeckFromDatabase(int deckId)
{ {
servatrice->checkSql(); servatrice->checkSql();
QSqlQuery query; QSqlQuery query;
query.prepare("select content from decklist_files where id = :id and user = :user"); query.prepare("select content from decklist_files where id = :id and user = :user");
query.bindValue(":id", cmd->getDeckId()); query.bindValue(":id", deckId);
query.bindValue(":user", playerName); query.bindValue(":user", playerName);
servatrice->execSqlQuery(query); servatrice->execSqlQuery(query);
if (!query.next()) if (!query.next())
return RespNameNotFound; throw RespNameNotFound;
QXmlStreamReader deckReader(query.value(0).toString()); QXmlStreamReader deckReader(query.value(0).toString());
DeckList *deck = new DeckList; DeckList *deck = new DeckList;
if (!deck->loadFromXml(&deckReader)) { if (!deck->loadFromXml(&deckReader))
delete deck; throw RespInvalidData;
deck = 0;
return RespInvalidData;
}
return deck;
}
ResponseCode ServerSocketInterface::cmdDeckDownload(Command_DeckDownload *cmd)
{
DeckList *deck;
try {
deck = getDeckFromDatabase(cmd->getDeckId());
} catch(ResponseCode r) {
return r;
}
sendProtocolItem(new Response_DeckDownload(cmd->getCmdId(), RespOk, deck)); sendProtocolItem(new Response_DeckDownload(cmd->getCmdId(), RespOk, deck));
return RespNothing; return RespNothing;
} }
...@@ -27,6 +27,7 @@ class QTcpSocket; ...@@ -27,6 +27,7 @@ class QTcpSocket;
class Servatrice; class Servatrice;
class QXmlStreamReader; class QXmlStreamReader;
class QXmlStreamWriter; class QXmlStreamWriter;
class DeckList;
class ServerSocketInterface : public Server_ProtocolHandler class ServerSocketInterface : public Server_ProtocolHandler
{ {
...@@ -50,6 +51,7 @@ private: ...@@ -50,6 +51,7 @@ private:
ResponseCode cmdDeckDelDir(Command_DeckDelDir *cmd); ResponseCode cmdDeckDelDir(Command_DeckDelDir *cmd);
ResponseCode cmdDeckDel(Command_DeckDel *cmd); ResponseCode cmdDeckDel(Command_DeckDel *cmd);
ResponseCode cmdDeckUpload(Command_DeckUpload *cmd); ResponseCode cmdDeckUpload(Command_DeckUpload *cmd);
DeckList *getDeckFromDatabase(int deckId);
ResponseCode cmdDeckDownload(Command_DeckDownload *cmd); ResponseCode cmdDeckDownload(Command_DeckDownload *cmd);
void itemFinishedReading(); void itemFinishedReading();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment