From 3c298041480a003c2252470f11560d2593a820c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Teuli=C3=A8re?= Date: Sun, 20 May 2012 21:31:45 +0200 Subject: [PATCH] Savegames: save/load properly the warnings, penalties and solos --- game/xml_reader.cpp | 34 ++++++++++++++++++++++++++++++---- game/xml_writer.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/game/xml_reader.cpp b/game/xml_reader.cpp index 4d610ff..7dd0f0e 100644 --- a/game/xml_reader.cpp +++ b/game/xml_reader.cpp @@ -49,6 +49,7 @@ #include "player_rack_cmd.h" #include "player_move_cmd.h" #include "player_points_cmd.h" +#include "player_event_cmd.h" #include "master_move_cmd.h" #include "navigation.h" #include "header.h" @@ -165,7 +166,7 @@ void XmlReader::startElement(const string& namespaceURI, { (void) namespaceURI; (void) qName; - LOG_DEBUG("Start Element: " << (localName.empty() ? qName : namespaceURI + ":" + localName)); + LOG_TRACE("Start Element: " << (localName.empty() ? qName : namespaceURI + ":" + localName)); m_data.clear(); const string &tag = localName; @@ -197,7 +198,8 @@ void XmlReader::startElement(const string& namespaceURI, } } else if (tag == "GameRack" || tag == "PlayerRack" || - tag == "PlayerMove" || tag == "GameMove" || tag == "MasterMove") + tag == "PlayerMove" || tag == "GameMove" || tag == "MasterMove" || + tag == "Warning" || tag == "Penalty" || tag == "Solo") { m_attributes.clear(); for (int i = 0; i < atts.getLength(); ++i) @@ -220,7 +222,7 @@ void XmlReader::endElement(const string& namespaceURI, const string&) { (void) namespaceURI; - LOG_DEBUG("endElement: " << namespaceURI << ":" << localName << "(" << m_data << ")"); + LOG_TRACE("endElement: " << namespaceURI << ":" << localName << "(" << m_data << ")"); const string &tag = localName; @@ -388,13 +390,37 @@ void XmlReader::endElement(const string& namespaceURI, m_game->accessNavigation().addAndExecute(cmd); } + else if (tag == "Warning") + { + Player &p = getPlayer(m_players, m_attributes["playerid"]); + PlayerEventCmd *cmd = new PlayerEventCmd(p, PlayerEventCmd::WARNING); + m_game->accessNavigation().addAndExecute(cmd); + } + + else if (tag == "Penalty") + { + Player &p = getPlayer(m_players, m_attributes["playerid"]); + int points = toInt(m_attributes["points"]); + LOG_ERROR("points=" << points); + PlayerEventCmd *cmd = new PlayerEventCmd(p, PlayerEventCmd::PENALTY, points); + m_game->accessNavigation().addAndExecute(cmd); + } + + else if (tag == "Solo") + { + Player &p = getPlayer(m_players, m_attributes["playerid"]); + int points = toInt(m_attributes["points"]); + PlayerEventCmd *cmd = new PlayerEventCmd(p, PlayerEventCmd::SOLO, points); + m_game->accessNavigation().addAndExecute(cmd); + } + } void XmlReader::characters(const string& ch) { m_data += ch; - LOG_DEBUG("Characters: " << ch); + LOG_TRACE("Characters: " << ch); } diff --git a/game/xml_writer.cpp b/game/xml_writer.cpp index 580b2ca..edcadbb 100644 --- a/game/xml_writer.cpp +++ b/game/xml_writer.cpp @@ -37,6 +37,7 @@ #include "player_rack_cmd.h" #include "player_move_cmd.h" #include "player_points_cmd.h" +#include "player_event_cmd.h" #include "master_move_cmd.h" #include "dic.h" #include "header.h" @@ -250,6 +251,33 @@ void XmlWriter::write(const Game &iGame, const string &iFileName) out << endl; } + else if (dynamic_cast(cmd)) + { + const PlayerEventCmd *eventCmd = static_cast(cmd); + unsigned int id = eventCmd->getPlayer().getId() + 1; + int value = eventCmd->getPoints(); + // Warnings + if (eventCmd->getEventType() == PlayerEventCmd::WARNING) + { + out << indent << "" << endl; + } + // Penalties + else if (eventCmd->getEventType() == PlayerEventCmd::PENALTY) + { + out << indent << "" << endl; + } + // Solos + else if (eventCmd->getEventType() == PlayerEventCmd::SOLO) + { + out << indent << "" << endl; + } + else + { + LOG_ERROR("Unknown event type: " << eventCmd->getEventType()); + } + } else { LOG_ERROR("Unsupported command: " << lfw(cmd->toString()));