This commit is contained in:
Max-Wilhelm Bruker 2009-10-30 13:18:25 +01:00
parent 1c2aa15b22
commit 6923c98dc2
12 changed files with 107 additions and 45 deletions

View file

@ -11,47 +11,47 @@ ProtocolItem::ProtocolItem(const QString &_itemName)
{
}
bool ProtocolItem::read(QXmlStreamReader &xml)
bool ProtocolItem::read(QXmlStreamReader *xml)
{
while (!xml.atEnd()) {
xml.readNext();
if (xml.isStartElement()) {
qDebug() << "startElement: " << xml.name().toString();
} else if (xml.isEndElement()) {
qDebug() << "endElement: " << xml.name().toString();
if (xml.name() == getItemType()) {
while (!xml->atEnd()) {
xml->readNext();
if (xml->isStartElement()) {
qDebug() << "startElement: " << xml->name().toString();
} else if (xml->isEndElement()) {
qDebug() << "endElement: " << xml->name().toString();
if (xml->name() == getItemType()) {
extractParameters();
qDebug() << "FERTIG";
deleteLater();
return true;
} else {
QString tagName = xml.name().toString();
QString tagName = xml->name().toString();
if (!parameters.contains(tagName))
qDebug() << "unrecognized attribute";
else
parameters[tagName] = currentElementText;
}
} else if (xml.isCharacters() && !xml.isWhitespace()) {
currentElementText = xml.text().toString();
} else if (xml->isCharacters() && !xml->isWhitespace()) {
currentElementText = xml->text().toString();
qDebug() << "text: " << currentElementText;
}
}
return false;
}
void ProtocolItem::write(QXmlStreamWriter &xml)
void ProtocolItem::write(QXmlStreamWriter *xml)
{
xml.writeStartElement(getItemType());
xml->writeStartElement(getItemType());
if (!itemName.isEmpty())
xml.writeAttribute("name", itemName);
xml->writeAttribute("name", itemName);
QMapIterator<QString, QString> i(parameters);
while (i.hasNext()) {
i.next();
xml.writeTextElement(i.key(), i.value());
xml->writeTextElement(i.key(), i.value());
}
xml.writeEndElement();
xml->writeEndElement();
}
ProtocolItem *ProtocolItem::getNewItem(const QString &name)
@ -118,6 +118,11 @@ void ProtocolResponse::initializeHash()
responseHash.insert("spectators_not_allowed", RespSpectatorsNotAllowed);
}
GenericEvent::GenericEvent(const QString &_eventName)
: ProtocolItem(_eventName)
{
}
void GameEvent::extractParameters()
{
bool ok;

View file

@ -32,8 +32,8 @@ public:
ProtocolItem(const QString &_itemName);
static void initializeHash();
static ProtocolItem *getNewItem(const QString &name);
virtual bool read(QXmlStreamReader &xml);
virtual void write(QXmlStreamWriter &xml);
virtual bool read(QXmlStreamReader *xml);
virtual void write(QXmlStreamWriter *xml);
};
class Command : public ProtocolItem {
@ -101,6 +101,14 @@ public:
static ProtocolItem *newItem() { return new ProtocolResponse; }
};
class GenericEvent : public ProtocolItem {
Q_OBJECT
protected:
QString getItemType() const { return "generic_event"; }
public:
GenericEvent(const QString &_eventName);
};
class GameEvent : public ProtocolItem {
Q_OBJECT
private:

View file

@ -551,6 +551,16 @@ void Event_StopDumpZone::extractParameters()
zoneOwnerId = parameters["zone_owner_id"].toInt();
zone = parameters["zone"];
}
Event_Welcome::Event_Welcome(const QString &_message)
: GenericEvent("welcome"), message(_message)
{
setParameter("message", message);
}
void Event_Welcome::extractParameters()
{
GenericEvent::extractParameters();
message = parameters["message"];
}
void ProtocolItem::initializeHashAuto()
{
itemNameHash.insert("cmdping", Command_Ping::newItem);
@ -604,4 +614,5 @@ void ProtocolItem::initializeHashAuto()
itemNameHash.insert("game_eventset_active_phase", Event_SetActivePhase::newItem);
itemNameHash.insert("game_eventdump_zone", Event_DumpZone::newItem);
itemNameHash.insert("game_eventstop_dump_zone", Event_StopDumpZone::newItem);
itemNameHash.insert("generic_eventwelcome", Event_Welcome::newItem);
}

View file

@ -49,3 +49,4 @@
3:set_active_phase:i,phase
3:dump_zone:i,zone_owner_id:s,zone:i,number_cards
3:stop_dump_zone:i,zone_owner_id:s,zone
4:welcome:s,message

View file

@ -634,5 +634,16 @@ public:
protected:
void extractParameters();
};
class Event_Welcome : public GenericEvent {
Q_OBJECT
private:
QString message;
public:
Event_Welcome(const QString &_message = QString());
QString getMessage() const { return message; }
static ProtocolItem *newItem() { return new Event_Welcome; }
protected:
void extractParameters();
};
#endif

View file

@ -47,6 +47,13 @@ while (<file>) {
$parentConstructorCall = "$baseClass(\"$name1\", _gameId, _playerId)";
$constructorParamsH = "int _gameId = -1, int _playerId = -1";
$constructorParamsCpp = "int _gameId, int _playerId";
} elsif ($type == 4) {
$type = 'generic_event';
$namePrefix = 'Event';
$baseClass = 'GenericEvent';
$parentConstructorCall = "$baseClass(\"$name1\")";
$constructorParamsH = "";
$constructorParamsCpp = "";
}
$className = $namePrefix . '_' . $name2;
print headerfile "class $className : public $baseClass {\n"

View file

@ -7,6 +7,7 @@
class Server_Player;
class Command;
class ProtocolItem;
class Server_ProtocolHandler : public QObject {
Q_OBJECT
@ -31,6 +32,7 @@ public:
const QString &getPlayerName() const { return playerName; }
void processCommand(Command *command);
virtual void sendProtocolItem(ProtocolItem *item) = 0;
};
#endif