mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-09 15:54:47 -07:00
Initial commit for sideboarding. It's supposed to be fully functional.
This commit is contained in:
parent
2ee4bb834d
commit
b2f83541e7
18 changed files with 474 additions and 87 deletions
|
|
@ -7,6 +7,41 @@
|
|||
#include "decklist.h"
|
||||
#include <QDebug>
|
||||
|
||||
MoveCardToZone::MoveCardToZone(const QString &_cardName, const QString &_startZone, const QString &_targetZone)
|
||||
: SerializableItem_Map("move_card_to_zone")
|
||||
{
|
||||
insertItem(new SerializableItem_String("card_name", _cardName));
|
||||
insertItem(new SerializableItem_String("start_zone", _startZone));
|
||||
insertItem(new SerializableItem_String("target_zone", _targetZone));
|
||||
}
|
||||
|
||||
MoveCardToZone::MoveCardToZone(MoveCardToZone *other)
|
||||
: SerializableItem_Map("move_card_to_zone")
|
||||
{
|
||||
insertItem(new SerializableItem_String("card_name", other->getCardName()));
|
||||
insertItem(new SerializableItem_String("start_zone", other->getStartZone()));
|
||||
insertItem(new SerializableItem_String("target_zone", other->getTargetZone()));
|
||||
}
|
||||
|
||||
SideboardPlan::SideboardPlan(const QString &_name, const QList<MoveCardToZone *> &_moveList)
|
||||
: SerializableItem_Map("sideboard_plan")
|
||||
{
|
||||
insertItem(new SerializableItem_String("name", _name));
|
||||
|
||||
for (int i = 0; i < _moveList.size(); ++i)
|
||||
itemList.append(_moveList[i]);
|
||||
}
|
||||
|
||||
void SideboardPlan::setMoveList(const QList<MoveCardToZone *> &_moveList)
|
||||
{
|
||||
for (int i = 0; i < itemList.size(); ++i)
|
||||
delete itemList[i];
|
||||
itemList.clear();
|
||||
|
||||
for (int i = 0; i < _moveList.size(); ++i)
|
||||
itemList.append(_moveList[i]);
|
||||
}
|
||||
|
||||
AbstractDecklistNode::AbstractDecklistNode(InnerDecklistNode *_parent)
|
||||
: parent(_parent), currentItem(0)
|
||||
{
|
||||
|
|
@ -197,20 +232,57 @@ const QStringList DeckList::fileNameFilters = QStringList()
|
|||
<< QObject::tr("All files (*.*)");
|
||||
|
||||
DeckList::DeckList()
|
||||
: SerializableItem("cockatrice_deck"), currentZone(0)
|
||||
: SerializableItem("cockatrice_deck"), currentZone(0), currentSideboardPlan(0)
|
||||
{
|
||||
root = new InnerDecklistNode;
|
||||
}
|
||||
|
||||
DeckList::DeckList(DeckList *other)
|
||||
: SerializableItem("cockatrice_deck"), currentZone(0)
|
||||
: SerializableItem("cockatrice_deck"), currentZone(0), currentSideboardPlan(0)
|
||||
{
|
||||
root = new InnerDecklistNode(other->getRoot());
|
||||
|
||||
QMapIterator<QString, SideboardPlan *> spIterator(other->getSideboardPlans());
|
||||
while (spIterator.hasNext()) {
|
||||
spIterator.next();
|
||||
QList<MoveCardToZone *> newMoveList;
|
||||
QList<MoveCardToZone *> oldMoveList = spIterator.value()->getMoveList();
|
||||
for (int i = 0; i < oldMoveList.size(); ++i)
|
||||
newMoveList.append(new MoveCardToZone(oldMoveList[i]));
|
||||
sideboardPlans.insert(spIterator.key(), new SideboardPlan(spIterator.key(), newMoveList));
|
||||
}
|
||||
}
|
||||
|
||||
DeckList::~DeckList()
|
||||
{
|
||||
delete root;
|
||||
|
||||
QMapIterator<QString, SideboardPlan *> i(sideboardPlans);
|
||||
while (i.hasNext())
|
||||
delete i.next().value();
|
||||
}
|
||||
|
||||
QList<MoveCardToZone *> DeckList::getCurrentSideboardPlan()
|
||||
{
|
||||
SideboardPlan *current = sideboardPlans.value(QString(), 0);
|
||||
if (!current)
|
||||
return QList<MoveCardToZone *>();
|
||||
else
|
||||
return current->getMoveList();
|
||||
}
|
||||
|
||||
void DeckList::setCurrentSideboardPlan(const QList<MoveCardToZone *> &plan)
|
||||
{
|
||||
SideboardPlan *current = sideboardPlans.value(QString(), 0);
|
||||
if (!current) {
|
||||
current = new SideboardPlan;
|
||||
sideboardPlans.insert(QString(), current);
|
||||
}
|
||||
|
||||
QList<MoveCardToZone *> newList;
|
||||
for (int i = 0; i < plan.size(); ++i)
|
||||
newList.append(new MoveCardToZone(plan[i]));
|
||||
current->setMoveList(newList);
|
||||
}
|
||||
|
||||
bool DeckList::readElement(QXmlStreamReader *xml)
|
||||
|
|
@ -218,6 +290,11 @@ bool DeckList::readElement(QXmlStreamReader *xml)
|
|||
if (currentZone) {
|
||||
if (currentZone->readElement(xml))
|
||||
currentZone = 0;
|
||||
} else if (currentSideboardPlan) {
|
||||
if (currentSideboardPlan->readElement(xml)) {
|
||||
sideboardPlans.insert(currentSideboardPlan->getName(), currentSideboardPlan);
|
||||
currentSideboardPlan = 0;
|
||||
}
|
||||
} else if (xml->isEndElement()) {
|
||||
if (xml->name() == "deckname")
|
||||
name = currentElementText;
|
||||
|
|
@ -227,6 +304,8 @@ bool DeckList::readElement(QXmlStreamReader *xml)
|
|||
currentElementText.clear();
|
||||
} else if (xml->isStartElement() && (xml->name() == "zone"))
|
||||
currentZone = new InnerDecklistNode(xml->attributes().value("name").toString(), root);
|
||||
else if (xml->isStartElement() && (xml->name() == "sideboard_plan"))
|
||||
currentSideboardPlan = new SideboardPlan;
|
||||
else if (xml->isCharacters() && !xml->isWhitespace())
|
||||
currentElementText = xml->text().toString();
|
||||
return SerializableItem::readElement(xml);
|
||||
|
|
@ -240,6 +319,10 @@ void DeckList::writeElement(QXmlStreamWriter *xml)
|
|||
|
||||
for (int i = 0; i < root->size(); i++)
|
||||
root->at(i)->writeElement(xml);
|
||||
|
||||
QMapIterator<QString, SideboardPlan *> i(sideboardPlans);
|
||||
while (i.hasNext())
|
||||
i.next().value()->write(xml);
|
||||
}
|
||||
|
||||
void DeckList::loadFromXml(QXmlStreamReader *xml)
|
||||
|
|
|
|||
|
|
@ -14,6 +14,25 @@ class QXmlStreamWriter;
|
|||
|
||||
class InnerDecklistNode;
|
||||
|
||||
class MoveCardToZone : public SerializableItem_Map {
|
||||
public:
|
||||
MoveCardToZone(const QString &_cardName = QString(), const QString &_startZone = QString(), const QString &_targetZone = QString());
|
||||
MoveCardToZone(MoveCardToZone *other);
|
||||
static SerializableItem *newItem() { return new MoveCardToZone; }
|
||||
QString getCardName() const { return static_cast<SerializableItem_String *>(itemMap.value("card_name"))->getData(); }
|
||||
QString getStartZone() const { return static_cast<SerializableItem_String *>(itemMap.value("start_zone"))->getData(); }
|
||||
QString getTargetZone() const { return static_cast<SerializableItem_String *>(itemMap.value("target_zone"))->getData(); }
|
||||
};
|
||||
|
||||
class SideboardPlan : public SerializableItem_Map {
|
||||
public:
|
||||
SideboardPlan(const QString &_name = QString(), const QList<MoveCardToZone *> &_moveList = QList<MoveCardToZone *>());
|
||||
static SerializableItem *newItem() { return new SideboardPlan; }
|
||||
QString getName() const { return static_cast<SerializableItem_String *>(itemMap.value("name"))->getData(); }
|
||||
QList<MoveCardToZone *> getMoveList() const { return typecastItemList<MoveCardToZone *>(); }
|
||||
void setMoveList(const QList<MoveCardToZone *> &_moveList);
|
||||
};
|
||||
|
||||
class AbstractDecklistNode {
|
||||
protected:
|
||||
InnerDecklistNode *parent;
|
||||
|
|
@ -89,8 +108,10 @@ private:
|
|||
QString name, comments;
|
||||
QString lastFileName;
|
||||
FileFormat lastFileFormat;
|
||||
QMap<QString, SideboardPlan *> sideboardPlans;
|
||||
InnerDecklistNode *root;
|
||||
InnerDecklistNode *currentZone;
|
||||
SideboardPlan *currentSideboardPlan;
|
||||
QString currentElementText;
|
||||
signals:
|
||||
void deckLoaded();
|
||||
|
|
@ -106,6 +127,9 @@ public:
|
|||
QString getComments() const { return comments; }
|
||||
QString getLastFileName() const { return lastFileName; }
|
||||
FileFormat getLastFileFormat() const { return lastFileFormat; }
|
||||
QList<MoveCardToZone *> getCurrentSideboardPlan();
|
||||
void setCurrentSideboardPlan(const QList<MoveCardToZone *> &plan);
|
||||
const QMap<QString, SideboardPlan *> &getSideboardPlans() const { return sideboardPlans; }
|
||||
|
||||
bool readElement(QXmlStreamReader *xml);
|
||||
void writeElement(QXmlStreamWriter *xml);
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ void ProtocolItem::initializeHash()
|
|||
{
|
||||
initializeHashAuto();
|
||||
|
||||
registerSerializableItem("move_card_to_zone", MoveCardToZone::newItem);
|
||||
registerSerializableItem("chat_channel", ServerInfo_ChatChannel::newItem);
|
||||
registerSerializableItem("chat_user", ServerInfo_ChatUser::newItem);
|
||||
registerSerializableItem("game", ServerInfo_Game::newItem);
|
||||
|
|
@ -32,6 +33,7 @@ void ProtocolItem::initializeHash()
|
|||
|
||||
registerSerializableItem("cmddeck_upload", Command_DeckUpload::newItem);
|
||||
registerSerializableItem("cmddeck_select", Command_DeckSelect::newItem);
|
||||
registerSerializableItem("cmdset_sideboard_plan", Command_SetSideboardPlan::newItem);
|
||||
|
||||
registerSerializableItem("resp", ProtocolResponse::newItem);
|
||||
ProtocolResponse::initializeHash();
|
||||
|
|
@ -170,6 +172,18 @@ DeckList *Command_DeckSelect::getDeck() const
|
|||
return static_cast<DeckList *>(itemMap.value("cockatrice_deck"));
|
||||
}
|
||||
|
||||
Command_SetSideboardPlan::Command_SetSideboardPlan(int _gameId, const QList<MoveCardToZone *> &_moveList)
|
||||
: GameCommand("set_sideboard_plan", _gameId)
|
||||
{
|
||||
for (int i = 0; i < _moveList.size(); ++i)
|
||||
itemList.append(_moveList[i]);
|
||||
}
|
||||
|
||||
QList<MoveCardToZone *> Command_SetSideboardPlan::getMoveList() const
|
||||
{
|
||||
return typecastItemList<MoveCardToZone *>();
|
||||
}
|
||||
|
||||
QHash<QString, ResponseCode> ProtocolResponse::responseHash;
|
||||
|
||||
ProtocolResponse::ProtocolResponse(int _cmdId, ResponseCode _responseCode, const QString &_itemName)
|
||||
|
|
|
|||
|
|
@ -17,12 +17,14 @@ class ProtocolResponse;
|
|||
class DeckList;
|
||||
class GameEvent;
|
||||
class GameEventContainer;
|
||||
class MoveCardToZone;
|
||||
|
||||
enum ItemId {
|
||||
ItemId_CommandContainer = ItemId_Other + 50,
|
||||
ItemId_GameEventContainer = ItemId_Other + 51,
|
||||
ItemId_Command_DeckUpload = ItemId_Other + 100,
|
||||
ItemId_Command_DeckSelect = ItemId_Other + 101,
|
||||
ItemId_Command_SetSideboardPlan = ItemId_Other + 102,
|
||||
ItemId_Event_ListChatChannels = ItemId_Other + 200,
|
||||
ItemId_Event_ChatListPlayers = ItemId_Other + 201,
|
||||
ItemId_Event_ListGames = ItemId_Other + 202,
|
||||
|
|
@ -164,6 +166,15 @@ public:
|
|||
int getDeckId() const { return static_cast<SerializableItem_Int *>(itemMap.value("deck_id"))->getData(); }
|
||||
};
|
||||
|
||||
class Command_SetSideboardPlan : public GameCommand {
|
||||
Q_OBJECT
|
||||
public:
|
||||
Command_SetSideboardPlan(int _gameId = -1, const QList<MoveCardToZone *> &_moveList = QList<MoveCardToZone *>());
|
||||
static SerializableItem *newItem() { return new Command_SetSideboardPlan; }
|
||||
int getItemId() const { return ItemId_Command_SetSideboardPlan; }
|
||||
QList<MoveCardToZone *> getMoveList() const;
|
||||
};
|
||||
|
||||
// -----------------
|
||||
// --- RESPONSES ---
|
||||
// -----------------
|
||||
|
|
|
|||
|
|
@ -103,6 +103,34 @@ void Server_Player::setupZones()
|
|||
z->cards.append(new Server_Card(currentCard->getName(), nextCardId++, 0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
const QList<MoveCardToZone *> &sideboardPlan = deck->getCurrentSideboardPlan();
|
||||
for (int i = 0; i < sideboardPlan.size(); ++i) {
|
||||
MoveCardToZone *m = sideboardPlan[i];
|
||||
|
||||
Server_CardZone *start, *target;
|
||||
if (m->getStartZone() == "main")
|
||||
start = deckZone;
|
||||
else if (m->getStartZone() == "side")
|
||||
start = sbZone;
|
||||
else
|
||||
continue;
|
||||
if (m->getTargetZone() == "main")
|
||||
target = deckZone;
|
||||
else if (m->getTargetZone() == "side")
|
||||
target = sbZone;
|
||||
else
|
||||
continue;
|
||||
|
||||
for (int j = 0; j < start->cards.size(); ++j)
|
||||
if (start->cards[j]->getName() == m->getCardName()) {
|
||||
Server_Card *card = start->cards[j];
|
||||
start->cards.removeAt(j);
|
||||
target->cards.append(card);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
deckZone->shuffle();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -85,6 +85,7 @@ ResponseCode Server_ProtocolHandler::processCommandHelper(Command *command, Comm
|
|||
|
||||
switch (command->getItemId()) {
|
||||
case ItemId_Command_DeckSelect: return cmdDeckSelect(qobject_cast<Command_DeckSelect *>(command), cont, game, player);
|
||||
case ItemId_Command_SetSideboardPlan: return cmdSetSideboardPlan(qobject_cast<Command_SetSideboardPlan *>(command), cont, game, player);
|
||||
case ItemId_Command_LeaveGame: return cmdLeaveGame(qobject_cast<Command_LeaveGame *>(command), cont, game, player);
|
||||
case ItemId_Command_ReadyStart: return cmdReadyStart(qobject_cast<Command_ReadyStart *>(command), cont, game, player);
|
||||
case ItemId_Command_Concede: return cmdConcede(qobject_cast<Command_Concede *>(command), cont, game, player);
|
||||
|
|
@ -351,6 +352,16 @@ ResponseCode Server_ProtocolHandler::cmdDeckSelect(Command_DeckSelect *cmd, Comm
|
|||
return RespNothing;
|
||||
}
|
||||
|
||||
ResponseCode Server_ProtocolHandler::cmdSetSideboardPlan(Command_SetSideboardPlan *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player)
|
||||
{
|
||||
DeckList *deck = player->getDeck();
|
||||
if (!deck)
|
||||
return RespContextError;
|
||||
|
||||
deck->setCurrentSideboardPlan(cmd->getMoveList());
|
||||
return RespOk;
|
||||
}
|
||||
|
||||
ResponseCode Server_ProtocolHandler::cmdConcede(Command_Concede * /*cmd*/, CommandContainer *cont, Server_Game *game, Server_Player *player)
|
||||
{
|
||||
player->setConceded(true);
|
||||
|
|
|
|||
|
|
@ -51,6 +51,7 @@ private:
|
|||
ResponseCode cmdConcede(Command_Concede *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player);
|
||||
ResponseCode cmdReadyStart(Command_ReadyStart *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player);
|
||||
ResponseCode cmdDeckSelect(Command_DeckSelect *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player);
|
||||
ResponseCode cmdSetSideboardPlan(Command_SetSideboardPlan *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player);
|
||||
ResponseCode cmdSay(Command_Say *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player);
|
||||
ResponseCode cmdShuffle(Command_Shuffle *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player);
|
||||
ResponseCode cmdMulligan(Command_Mulligan *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue