File indexing completed on 2024-04-21 04:04:44

0001 /* This file is part of KsirK.
0002    Copyright (C) 2005-2007 Gael de Chalendar <kleag@free.fr>
0003 
0004    KsirK is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public
0006    License as published by the Free Software Foundation, either version 2
0007    of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    General Public License for more details.
0013 
0014    You should have received a copy of the GNU General Public License
0015    along with this program; if not, write to the Free Software
0016    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017    02110-1301, USA
0018 */
0019 
0020 /*    begin                : Mon Feb 07 2005 */
0021 
0022 #include "ksirkgamexmlhandler.h"
0023 #include "GameLogic/country.h"
0024 #include "GameLogic/onu.h"
0025 #include "GameLogic/KMessageParts.h"
0026 
0027 #include "ksirk_debug.h"
0028 #include <KLazyLocalizedString>
0029 #include <KLocalizedString>
0030 #include <KMessageBox>
0031 #include <KStringHandler>
0032 
0033 namespace Ksirk
0034 {
0035 using namespace GameLogic;
0036 
0037 namespace SaveLoad
0038 {
0039 
0040 bool GameXmlHandler::startDocument()
0041 { 
0042   qCDebug(KSIRK_LOG) << "startDocument";
0043   return true;
0044 }
0045 
0046 bool GameXmlHandler::startElement( const QString & namespaceURI, const QString & localName, const QString & qName, const QXmlAttributes & atts ) 
0047 {
0048   Q_UNUSED(namespaceURI);
0049   Q_UNUSED(qName);
0050   qCDebug(KSIRK_LOG) << "startElement " << localName << " / " << qName ;
0051   if (localName == "ksirkSavedGame")
0052   {
0053     QString fv =atts.value("formatVersion");
0054     QString wfv =SAVE_GAME_FILE_FORMAT_VERSION;
0055     if (fv!=wfv)
0056     {
0057       KMessageBox::error(nullptr,
0058           i18n("Wrong save game format. Waited %1 and got %2!",QString(SAVE_GAME_FILE_FORMAT_VERSION),atts.value("formatVersion")),
0059           i18n("KsirK - Cannot load!"));
0060 
0061       return false;
0062     }
0063   }
0064   else if (localName == "game")
0065   {
0066     qCDebug(KSIRK_LOG) << "GameXmlHandler stored game state is: " << atts.value("state");
0067     
0068     m_game.automaton()->skin(atts.value("skin"));
0069     
0070     m_savedState = GameLogic::GameAutomaton::GameState(atts.value("state").toInt());
0071     m_game.automaton()->savedState(m_savedState);
0072   }
0073   else if (localName == "players" && !m_inGoal)
0074   {
0075     int nb = atts.value("nb").toInt();
0076     qCDebug(KSIRK_LOG) << "Setting min-max players to " << nb ;
0077     m_game.automaton()->setMinPlayers(nb);
0078     m_game.automaton()->setMaxPlayers(nb);
0079   }
0080   else if (localName == "player" && !m_inGoal)
0081   {
0082     qCDebug(KSIRK_LOG) << "Reading a player";
0083     m_playersNumber++;
0084     unsigned int nbAvailArmies = atts.value("nbAvailArmies").toInt();
0085     
0086     unsigned int nbCountries = atts.value("nbCountries").toInt();
0087     
0088     QString name = atts.value("name");
0089     
0090     QString nationName = atts.value("nation");
0091     
0092     unsigned int nbAttack = atts.value("nbAttack").toInt();
0093     
0094     unsigned int nbDefense = atts.value("nbDefense").toInt();
0095     
0096     bool isAi = false;
0097     if (atts.value("ai") == "true") isAi = true;
0098     
0099     QString password = KStringHandler::obscure(atts.value("password"));
0100     
0101     bool isLocal = true; // local player by default
0102     if (atts.value("local") == "false") isLocal = false;
0103 
0104     if (isLocal)
0105     {
0106       qCDebug(KSIRK_LOG) << "Adding the read player " << name ;
0107       m_game.addPlayer(name, nbAvailArmies, nbCountries, nationName,
0108                         isAi, password, nbAttack, nbDefense);
0109     }
0110     else
0111     {
0112       qCDebug(KSIRK_LOG) << "Player" << name << "stored in matrix";
0113       PlayerMatrix pm(m_game.automaton());
0114       pm.name = name;
0115       pm.nbAttack = nbAttack;
0116       pm.nbCountries = nbCountries;
0117       pm.nbAvailArmies = nbAvailArmies;
0118       pm.nbDefense = nbDefense;
0119       pm.nation = nationName;
0120       pm.password = password;
0121       pm.isAI = isAi;
0122       foreach (const QString& k, m_ownersMap.keys())
0123       {
0124         if ( m_ownersMap[k] == name )
0125         {
0126           pm.countries.push_back(k);
0127         }
0128       }
0129       m_waitedPlayers.push_back(pm);
0130     }
0131   }
0132   else if (localName == "currentPlayer")
0133   {
0134     Player* currentPlayer = m_game.automaton()->playerNamed(atts.value("name"));
0135     if (currentPlayer)
0136     {
0137 //       qCDebug(KSIRK_LOG) << "Setting current player to " << atts.value("name") << " / " << currentPlayer ;
0138       m_game.automaton()->currentPlayer(currentPlayer);
0139       KMessageParts messageParts;
0140       messageParts << kli18n("Current player is: %1").untranslatedText() << currentPlayer->name();
0141       m_game.broadcastChangeItem(messageParts, ID_STATUS_MSG2);
0142       QByteArray buffer;
0143       QDataStream stream(&buffer, QIODevice::WriteOnly);
0144       stream << currentPlayer->name();
0145       m_game.automaton()->sendMessage(buffer,SetBarFlagButton);
0146     }
0147     m_game.automaton()->savedPlayer(atts.value("name"));
0148   }
0149   else if (localName == "ONU")
0150   {
0151     qCDebug(KSIRK_LOG) << "GameXmlHandler starts new game with ONU file: " << atts.value("file");
0152     if (!(m_game.automaton()->playerList()->isEmpty()))
0153     {
0154       m_game.automaton()->playerList()->clear();
0155       m_game.automaton()->currentPlayer(nullptr);
0156       qCDebug(KSIRK_LOG) << "  playerList size = " << m_game.automaton()->playerList()->count();
0157     }
0158     m_game.automaton()->game()->newSkin(atts.value("file"));
0159   }
0160   else if (localName == "country")
0161   {
0162 //   qCDebug(KSIRK_LOG) << "GameXmlHandler loads country: " << atts.value("name");
0163     Country* country = m_game.theWorld()->countryNamed(atts.value("name"));
0164     unsigned int gotNbArmies = atts.value("nbArmies").toInt();
0165     country->nbArmies(gotNbArmies);
0166     
0167     qCDebug(KSIRK_LOG) << "Storing" << atts.value("owner") << "as owner of" << atts.value("name");
0168     m_ownersMap.insert(atts.value("name"), atts.value("owner"));
0169   }
0170   else if (localName == "goal")
0171   {
0172     qCDebug(KSIRK_LOG) << "loads goal for: " << atts.value("player");
0173     m_goal = new GameLogic::Goal(m_game.automaton());
0174     m_goalPlayerName = atts.value("player");
0175     Player* player = m_game.automaton()->playerNamed(atts.value("player").toUtf8().data());
0176 //     qCDebug(KSIRK_LOG) << "Got player pointer " << player ;
0177     m_goal->player(player);
0178     unsigned int type = atts.value("type").toInt();
0179     m_goal->type(GameLogic::Goal::GoalType(type));
0180     m_goal->description(atts.value("description"));
0181     unsigned int nbCountries = atts.value("nbCountries").toInt();
0182     m_goal->nbCountries(nbCountries);
0183     unsigned int nbArmiesByCountry = atts.value("nbArmiesByCountry").toInt();
0184     m_goal->nbArmiesByCountry(nbArmiesByCountry);
0185     
0186     m_inGoal = true;
0187   }
0188   else if (localName == "player" && m_inGoal)
0189   {
0190     m_goal->players().push_back(atts.value("name"));
0191   }
0192   else if (localName == "continent" && m_inGoal)
0193   {
0194 //     qCDebug(KSIRK_LOG) << "Getting id of continent named " << atts.value("name");
0195     QString id;
0196     if (!atts.value("name").isEmpty())
0197         id = atts.value("name");
0198     m_goal->continents().push_back(id);
0199   }
0200   return true;
0201 }
0202 
0203 bool GameXmlHandler::endElement(const QString& namespaceURI, const QString& localName, const QString& qName)
0204 {
0205   Q_UNUSED(namespaceURI);
0206   Q_UNUSED(qName);
0207 //   qCDebug(KSIRK_LOG) << "endElement " << localName << " / " << qName ;
0208   if (localName == "game")
0209   {
0210     foreach (const QString& k, m_ownersMap.keys())
0211     {
0212 //       qCDebug(KSIRK_LOG) << "Setting owner of " << k << " to " << m_ownersMap[k];
0213       Country* country = m_game.theWorld()->countryNamed(k);
0214       Player* owner = m_game.automaton()->playerNamed(m_ownersMap[k]);
0215       if (owner)
0216       {
0217 //         qCDebug(KSIRK_LOG) << "Setting owner of " << country->name() << " to " << owner->name();
0218         country-> owner(owner);
0219       }
0220       else
0221       {
0222 //         qCDebug(KSIRK_LOG) << "Player" << m_ownersMap[k] << "not found";
0223         QList<GameLogic::PlayerMatrix>::iterator itw,itw_end;
0224         itw = m_waitedPlayers.begin(); itw_end = m_waitedPlayers.end();
0225         for (; itw != itw_end; itw++)
0226         {
0227           if ( (*itw).name == m_ownersMap[k] )
0228           {
0229             (*itw).countries.push_back(k);
0230             break;
0231           }
0232         }
0233       }
0234     }
0235     if (!m_waitedPlayers.empty())
0236     {
0237 //       qCDebug(KSIRK_LOG) << "There is waited players: does not change state nor run game...";
0238       m_waitedPlayers[0].state = m_savedState;
0239     }
0240     else
0241     {
0242 //       qCDebug(KSIRK_LOG) << "GameXmlHandler set game state to: " << m_savedState ;
0243       m_game.automaton()->state(m_savedState);
0244     }
0245   }
0246   else if (localName == "goal")
0247   {
0248     m_inGoal = false;
0249     if (m_goal)
0250     {
0251       if (m_goal->player())
0252       {
0253         m_goal->player()->goal(*m_goal);
0254       }
0255       else
0256       {
0257         QList<GameLogic::PlayerMatrix>::iterator itw,itw_end;
0258         itw = m_waitedPlayers.begin(); itw_end = m_waitedPlayers.end();
0259         for (; itw != itw_end; itw++)
0260         {
0261           if ( (*itw).name == m_goalPlayerName )
0262           {
0263             (*itw).goal = *m_goal;
0264             break;
0265           }
0266         }
0267       }
0268       delete m_goal;
0269     }
0270     m_goal = nullptr;
0271   }
0272   return true;
0273 }
0274 
0275 
0276 } // closing namespace SaveLoad
0277 } // closing namespace Ksirk
0278 
0279