Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

XmlScenarioSerializer implemented

  • Loading branch information...
commit ae27bca06cc3e025160a8c68111e20d11b0714a9 1 parent e6db445
@vos authored
View
2  canvas.cpp
@@ -81,8 +81,8 @@ Canvas::Canvas(QWidget *parent) :
Canvas::~Canvas()
{
- qDeleteAll(m_planets);
qDeleteAll(m_players);
+ qDeleteAll(m_planets);
}
void Canvas::resizeEvent(QResizeEvent *resizeEvent)
View
35 mainwindow.cpp
@@ -7,7 +7,7 @@
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
-
+#include <QMessageBox>
#include "canvas.h"
#include "computerplayer.h"
@@ -16,6 +16,8 @@
#include "playerpropertyeditor.h"
#include "playerintelligencepropertyeditor.h"
+#include "xmlscenarioserializer.h"
+
#include <QDebug>
#include <QScriptEngineDebugger>
@@ -195,3 +197,34 @@ void MainWindow::on_globalAccessCheckBox_toggled(bool checked)
{
m_canvas->setGlobalAccess(checked);
}
+
+void MainWindow::on_action_openScenario_triggered()
+{
+ QString fileName("scenario0.xml");
+ XmlScenarioSerializer serializer;
+ ScenarioSerializer::Scenario scenario;
+ if (serializer.deserialize(fileName, scenario)) {
+ // TODO set active scenario to canvas
+// qDeleteAll(m_canvas->players());
+// qDeleteAll(m_canvas->planets());
+ statusBar()->showMessage(QString("Scenario \"%1\" successfully loaded").arg(fileName));
+ } else {
+ QMessageBox::warning(this, "Open Scenario Error", QString("Cannot open scenario file \"%1\"").arg(fileName));
+ }
+}
+
+void MainWindow::on_action_saveScenario_triggered()
+{
+ QString fileName("scenario0.xml");
+ XmlScenarioSerializer serializer;
+ ScenarioSerializer::Scenario scenario = {
+ m_canvas->players(),
+ m_canvas->activePlayer(),
+ m_canvas->planets()
+ };
+ if (serializer.serialize(scenario, fileName)) {
+ statusBar()->showMessage(QString("Scenario successfully saved as \"%1\"").arg(fileName));
+ } else {
+ QMessageBox::warning(this, "Save Scenario Error", QString("Cannot save scenario file \"%1\"").arg(fileName));
+ }
+}
View
3  mainwindow.h
@@ -30,6 +30,9 @@ private slots:
void on_addPlayerButton_clicked();
void on_globalAccessCheckBox_toggled(bool checked);
+ void on_action_openScenario_triggered();
+ void on_action_saveScenario_triggered();
+
private:
Ui::MainWindow *ui;
Canvas *m_canvas;
View
27 mainwindow.ui
@@ -23,6 +23,14 @@
<height>21</height>
</rect>
</property>
+ <widget class="QMenu" name="menu_File">
+ <property name="title">
+ <string>&amp;File</string>
+ </property>
+ <addaction name="action_openScenario"/>
+ <addaction name="action_saveScenario"/>
+ </widget>
+ <addaction name="menu_File"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
@@ -31,6 +39,9 @@
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
+ <addaction name="action_openScenario"/>
+ <addaction name="action_saveScenario"/>
+ <addaction name="separator"/>
<addaction name="action_setFocus"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
@@ -83,6 +94,22 @@
<string>Set Focus</string>
</property>
</action>
+ <action name="action_openScenario">
+ <property name="text">
+ <string>&amp;Open Scenario</string>
+ </property>
+ <property name="shortcut">
+ <string>Ctrl+O</string>
+ </property>
+ </action>
+ <action name="action_saveScenario">
+ <property name="text">
+ <string>&amp;Save Scenario</string>
+ </property>
+ <property name="shortcut">
+ <string>Ctrl+S</string>
+ </property>
+ </action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
View
4 player.cpp
@@ -15,6 +15,10 @@ Player::Player(const QString &name, const QColor &color, bool human, QObject *pa
Player::~Player()
{
+ foreach (Planet *planet, m_planets) {
+ planet->setPlayer(NULL);
+ }
+
qDeleteAll(m_ships);
}
View
1  scenarioserializer.h
@@ -12,6 +12,7 @@ class ScenarioSerializer
public:
struct Scenario {
QSet<Player*> players;
+ Player *activePlayer;
QSet<Planet*> planets;
};
View
147 xmlscenarioserializer.cpp
@@ -1,18 +1,153 @@
#include "xmlscenarioserializer.h"
-#include <QtXml>
+#include <QDomDocument>
+#include <QFile>
-#include "player.h"
+#include "canvas.h"
+#include "humanplayer.h"
+#include "computerplayer.h"
+#include "scriptedplayerintelligence.h"
bool XmlScenarioSerializer::serialize(const XmlScenarioSerializer::Scenario &scenario, const QString &fileName)
{
QDomDocument doc;
- // TODO
- return false;
+ QDomElement scenarioElement = doc.createElement("scenario");
+ doc.appendChild(scenarioElement);
+
+ QHash<const Player*, int> playerIdMap;
+ int nextPlayerId = 0;
+
+ QDomElement playersElement = doc.createElement("players");
+ scenarioElement.appendChild(playersElement);
+ foreach (const Player *player, scenario.players) {
+ QDomElement playerElement = doc.createElement("player");
+ playersElement.appendChild(playerElement);
+ int playerId;
+ if (playerIdMap.contains(player)) {
+ playerId = playerIdMap.value(player);
+ } else {
+ playerId = nextPlayerId++;
+ playerIdMap.insert(player, playerId);
+ }
+ playerElement.setAttribute("id", playerId);
+ playerElement.setAttribute("name", player->name());
+ playerElement.setAttribute("color", player->color().name());
+ playerElement.setAttribute("resourceFactor", player->resourceFactor());
+ if (player->isComputer()) {
+ const ComputerPlayer *computerPlayer = static_cast<const ComputerPlayer*>(player);
+ if (computerPlayer->hasIntelligence()) {
+ PlayerIntelligence *intelligence = computerPlayer->intelligence();
+ ScriptedPlayerIntelligence *scriptedIntelligence = dynamic_cast<ScriptedPlayerIntelligence*>(intelligence);
+ if (scriptedIntelligence != NULL && !scriptedIntelligence->intelligenceProgram().isNull()) {
+ playerElement.setAttribute("intelligence", scriptedIntelligence->intelligenceProgram().fileName());
+ }
+ }
+ }
+ }
+
+ scenarioElement.setAttribute("activePlayer", playerIdMap.value(scenario.activePlayer));
+
+ QDomElement planetsElement = doc.createElement("planets");
+ scenarioElement.appendChild(planetsElement);
+ foreach (const Planet *planet, scenario.planets) {
+ QDomElement planetElement = doc.createElement("planet");
+ planetsElement.appendChild(planetElement);
+ planetElement.setAttribute("positionX", int(planet->position().x()));
+ planetElement.setAttribute("positionY", int(planet->position().y()));
+ planetElement.setAttribute("radius", int(planet->radius()));
+ planetElement.setAttribute("resources", int(planet->resources()));
+ planetElement.setAttribute("productionFactor", planet->productionFactor());
+ planetElement.setAttribute("color", planet->color().name());
+ if (planet->hasPlayer()) {
+ planetElement.setAttribute("player", playerIdMap.value(planet->player()));
+ }
+ }
+
+ QFile file(fileName);
+ if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
+ file.write(doc.toByteArray(4));
+ } else {
+ qWarning("cannot open file \"%s\" for writing!", qPrintable(fileName));
+ return false;
+ }
+
+ return true;
}
bool XmlScenarioSerializer::deserialize(const QString &fileName, XmlScenarioSerializer::Scenario &scenario)
{
- // TODO
- return false;
+ QDomDocument doc;
+ QFile file(fileName);
+ if (!file.open(QIODevice::ReadOnly)) {
+ qWarning("cannot open file \"%s\" for reading!", qPrintable(fileName));
+ return false;
+ }
+ QString errorMessage;
+ int errorLine, errorColumn;
+ if (!doc.setContent(&file, &errorMessage, &errorLine, &errorColumn)) {
+ file.close();
+ qWarning("error while parsing file \"%s\": %s at line %d, column %d!",
+ qPrintable(fileName), qPrintable(errorMessage), errorLine, errorColumn);
+ return false;
+ }
+ file.close();
+
+ QDomElement scenarioElement = doc.documentElement();
+ QHash<int, Player*> idPlayerMap;
+
+ QDomElement playersElement = scenarioElement.elementsByTagName("players").item(0).toElement();
+ QDomNodeList playerNodeList = playersElement.elementsByTagName("player");
+ for (uint i = 0; i < playerNodeList.length(); ++i) {
+ QDomElement playerElement = playerNodeList.item(i).toElement();
+ Player *player;
+ QString name = playerElement.attribute("name");
+ QColor color(playerElement.attribute("color"));
+ QString intelligenceFileName = playerElement.attribute("intelligence");
+ if (!intelligenceFileName.isEmpty() && QFile::exists(intelligenceFileName)) {
+ ScriptedPlayerIntelligence *intelligence = new ScriptedPlayerIntelligence(Canvas::Instance->scriptEngine());
+ intelligence->setIntelligenceProgramFile(intelligenceFileName);
+ player = new ComputerPlayer(name, color, intelligence, Canvas::Instance);
+ } else {
+ player = new HumanPlayer(name, color, Canvas::Instance);
+ }
+ player->setResourceFactor(playerElement.attribute("resourceFactor").toDouble());
+ int id = playerElement.attribute("id").toInt();
+ idPlayerMap.insert(id, player);
+ scenario.players.insert(player);
+ }
+
+ int activePlayerId = scenarioElement.attribute("activePlayer").toInt();
+ if (idPlayerMap.contains(activePlayerId)) {
+ scenario.activePlayer = idPlayerMap.value(activePlayerId);
+ } else {
+ qWarning("active player id not found in player list: " + activePlayerId);
+ scenario.activePlayer = NULL;
+ }
+
+ QDomElement planetsElement = scenarioElement.elementsByTagName("planets").item(0).toElement();
+ QDomNodeList planetNodeList = planetsElement.elementsByTagName("planet");
+ for (uint i = 0; i < planetNodeList.length(); ++i) {
+ QDomElement planetElement = playerNodeList.item(i).toElement();
+ int positionX = planetElement.attribute("positionX").toInt();
+ int positionY = planetElement.attribute("positionY").toInt();
+ int radius = planetElement.attribute("radius").toInt();
+ int resources = planetElement.attribute("resources").toInt();
+ QColor color(planetElement.attribute("color"));
+ Planet *planet = new Planet(QVector2D(positionX, positionY), radius, resources, color);
+ qreal productionFactor = planetElement.attribute("productionFactor").toDouble();
+ planet->setProductionFactor(productionFactor);
+ if (planetElement.hasAttribute("player")) {
+ bool ok;
+ int id = planetElement.attribute("player").toInt(&ok);
+ if (ok && idPlayerMap.contains(id)) {
+ Player *player = idPlayerMap.value(id);
+ planet->setPlayer(player);
+ } else {
+ qWarning("player id invalid or not found in player list: " + id);
+ }
+ }
+ scenario.planets.insert(planet);
+ }
+
+ return true;
}
Please sign in to comment.
Something went wrong with that request. Please try again.