~neon/ksirk/master

1 by Gaël de Chalendar
Imported KsirK to Playground/games
1
/* This file is part of KsirK.
7 by Gaël de Chalendar
Corrected many problems reported by the ebn
2
   Copyright (C) 2005-2007 Gael de Chalendar <kleag@free.fr>
1 by Gaël de Chalendar
Imported KsirK to Playground/games
3
4
   KsirK is free software; you can redistribute it and/or
5
   modify it under the terms of the GNU General Public
681 by Gael de Chalendar
Update license headers to allow (L)GPL2+
6
   License as published by the Free Software Foundation, either version 2
7
   of the License, or (at your option) any later version.
1 by Gaël de Chalendar
Imported KsirK to Playground/games
8
9
   This program is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
   General Public License for more details.
13
14
   You should have received a copy of the GNU General Public License
7 by Gaël de Chalendar
Corrected many problems reported by the ebn
15
   along with this program; if not, write to the Free Software
16
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17
   02110-1301, USA
1 by Gaël de Chalendar
Imported KsirK to Playground/games
18
*/
19
20
/*    begin                : Mon Feb 07 2005 */
21
57 by Gaël de Chalendar
Most EBN errors corrected (remains check number 17, Qt classes to KDE classes
22
#include "ksirkgamexmlhandler.h"
1 by Gaël de Chalendar
Imported KsirK to Playground/games
23
#include "GameLogic/country.h"
24
#include "GameLogic/onu.h"
25
#include "GameLogic/KMessageParts.h"
26
641.1.45 by Luigi Toscano
Port to categorized logging most of ksirk
27
#include "ksirk_debug.h"
641.1.6 by Montel Laurent
Fix includes
28
#include <KLocalizedString>
756 by Laurent Montel
Use camelcase include (scripted)
29
#include <KMessageBox>
532 by Gaël de Chalendar
Store an obscured version of the players passwords in saved games
30
#include <KStringHandler>
1 by Gaël de Chalendar
Imported KsirK to Playground/games
31
32
namespace Ksirk
33
{
34
using namespace GameLogic;
35
36
namespace SaveLoad
37
{
38
39
bool GameXmlHandler::startDocument()
40
{ 
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
41
  qCDebug(KSIRK_LOG) << "startDocument";
1 by Gaël de Chalendar
Imported KsirK to Playground/games
42
  return true;
43
}
44
45
bool GameXmlHandler::startElement( const QString & namespaceURI, const QString & localName, const QString & qName, const QXmlAttributes & atts ) 
46
{
30 by Gaël de Chalendar
SVN_SILENT some compilation warnings removed
47
  Q_UNUSED(namespaceURI);
48
  Q_UNUSED(qName);
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
49
  qCDebug(KSIRK_LOG) << "startElement " << localName << " / " << qName ;
1 by Gaël de Chalendar
Imported KsirK to Playground/games
50
  if (localName == "ksirkSavedGame")
51
  {
52
    QString fv =atts.value("formatVersion");
53
    QString wfv =SAVE_GAME_FILE_FORMAT_VERSION;
54
    if (fv!=wfv)
55
    {
56
      KMessageBox::sorry(0, 
443 by Pino Toscano
i18n fixes
57
          i18n("Wrong save game format. Waited %1 and got %2!",QString(SAVE_GAME_FILE_FORMAT_VERSION),atts.value("formatVersion")),
58
          i18n("KsirK - Cannot load!"));
1 by Gaël de Chalendar
Imported KsirK to Playground/games
59
60
      return false;
61
    }
62
  }
63
  else if (localName == "game")
64
  {
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
65
    qCDebug(KSIRK_LOG) << "GameXmlHandler stored game state is: " << atts.value("state");
1 by Gaël de Chalendar
Imported KsirK to Playground/games
66
    
431 by Gaël de Chalendar
Use a bubble instead of a message box to inform of a player elimination;
67
    m_game.automaton()->skin(atts.value("skin"));
1 by Gaël de Chalendar
Imported KsirK to Playground/games
68
    
431 by Gaël de Chalendar
Use a bubble instead of a message box to inform of a player elimination;
69
    m_savedState = GameLogic::GameAutomaton::GameState(atts.value("state").toInt());
46 by Gaël de Chalendar
- Local game works
70
    m_game.automaton()->savedState(m_savedState);
1 by Gaël de Chalendar
Imported KsirK to Playground/games
71
  }
72
  else if (localName == "players" && !m_inGoal)
73
  {
431 by Gaël de Chalendar
Use a bubble instead of a message box to inform of a player elimination;
74
    int nb = atts.value("nb").toInt();
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
75
    qCDebug(KSIRK_LOG) << "Setting min-max players to " << nb ;
46 by Gaël de Chalendar
- Local game works
76
    m_game.automaton()->setMinPlayers(nb);
77
    m_game.automaton()->setMaxPlayers(nb);
1 by Gaël de Chalendar
Imported KsirK to Playground/games
78
  }
79
  else if (localName == "player" && !m_inGoal)
80
  {
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
81
    qCDebug(KSIRK_LOG) << "Reading a player";
1 by Gaël de Chalendar
Imported KsirK to Playground/games
82
    m_playersNumber++;
431 by Gaël de Chalendar
Use a bubble instead of a message box to inform of a player elimination;
83
    unsigned int nbAvailArmies = atts.value("nbAvailArmies").toInt();
1 by Gaël de Chalendar
Imported KsirK to Playground/games
84
    
431 by Gaël de Chalendar
Use a bubble instead of a message box to inform of a player elimination;
85
    unsigned int nbCountries = atts.value("nbCountries").toInt();
1 by Gaël de Chalendar
Imported KsirK to Playground/games
86
    
87
    QString name = atts.value("name");
88
    
89
    QString nationName = atts.value("nation");
90
    
431 by Gaël de Chalendar
Use a bubble instead of a message box to inform of a player elimination;
91
    unsigned int nbAttack = atts.value("nbAttack").toInt();
92
    
93
    unsigned int nbDefense = atts.value("nbDefense").toInt();
1 by Gaël de Chalendar
Imported KsirK to Playground/games
94
    
95
    bool isAi = false;
96
    if (atts.value("ai") == "true") isAi = true;
97
    
532 by Gaël de Chalendar
Store an obscured version of the players passwords in saved games
98
    QString password = KStringHandler::obscure(atts.value("password"));
1 by Gaël de Chalendar
Imported KsirK to Playground/games
99
    
228 by Gaël de Chalendar
- Changed the saving of network players to allow empty passwords;
100
    bool isLocal = true; // local player by default
101
    if (atts.value("local") == "false") isLocal = false;
102
103
    if (isLocal)
1 by Gaël de Chalendar
Imported KsirK to Playground/games
104
    {
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
105
      qCDebug(KSIRK_LOG) << "Adding the read player " << name ;
50 by Gaël de Chalendar
Pending changes.
106
      m_game.addPlayer(name, nbAvailArmies, nbCountries, nationName,
107
                        isAi, password, nbAttack, nbDefense);
1 by Gaël de Chalendar
Imported KsirK to Playground/games
108
    }
109
    else
110
    {
641.1.45 by Luigi Toscano
Port to categorized logging most of ksirk
111
      qCDebug(KSIRK_LOG) << "Player" << name << "stored in matrix";
228 by Gaël de Chalendar
- Changed the saving of network players to allow empty passwords;
112
      PlayerMatrix pm(m_game.automaton());
1 by Gaël de Chalendar
Imported KsirK to Playground/games
113
      pm.name = name;
114
      pm.nbAttack = nbAttack;
115
      pm.nbCountries = nbCountries;
116
      pm.nbAvailArmies = nbAvailArmies;
117
      pm.nbDefense = nbDefense;
118
      pm.nation = nationName;
119
      pm.password = password;
120
      pm.isAI = isAi;
363 by Gaël de Chalendar
Replaced numeric ids by names;
121
      foreach (const QString& k, m_ownersMap.keys())
1 by Gaël de Chalendar
Imported KsirK to Playground/games
122
      {
363 by Gaël de Chalendar
Replaced numeric ids by names;
123
        if ( m_ownersMap[k] == name )
1 by Gaël de Chalendar
Imported KsirK to Playground/games
124
        {
363 by Gaël de Chalendar
Replaced numeric ids by names;
125
          pm.countries.push_back(k);
1 by Gaël de Chalendar
Imported KsirK to Playground/games
126
        }
127
      }
252.1.6 by Gaël de Chalendar
* [DONE] crash when loading the target player when the goal is to destroy it
128
      m_waitedPlayers.push_back(pm);
1 by Gaël de Chalendar
Imported KsirK to Playground/games
129
    }
130
  }
131
  else if (localName == "currentPlayer")
132
  {
46 by Gaël de Chalendar
- Local game works
133
    Player* currentPlayer = m_game.automaton()->playerNamed(atts.value("name"));
1 by Gaël de Chalendar
Imported KsirK to Playground/games
134
    if (currentPlayer)
135
    {
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
136
//       qCDebug(KSIRK_LOG) << "Setting current player to " << atts.value("name") << " / " << currentPlayer ;
46 by Gaël de Chalendar
- Local game works
137
      m_game.automaton()->currentPlayer(currentPlayer);
1 by Gaël de Chalendar
Imported KsirK to Playground/games
138
      KMessageParts messageParts;
139
      messageParts << I18N_NOOP("Current player is: %1") << currentPlayer->name();
140
      m_game.broadcastChangeItem(messageParts, ID_STATUS_MSG2);
141
      QByteArray buffer;
142
      QDataStream stream(&buffer, QIODevice::WriteOnly);
143
      stream << currentPlayer->name();
46 by Gaël de Chalendar
- Local game works
144
      m_game.automaton()->sendMessage(buffer,SetBarFlagButton);
1 by Gaël de Chalendar
Imported KsirK to Playground/games
145
    }
46 by Gaël de Chalendar
- Local game works
146
    m_game.automaton()->savedPlayer(atts.value("name"));
1 by Gaël de Chalendar
Imported KsirK to Playground/games
147
  }
148
  else if (localName == "ONU")
149
  {
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
150
    qCDebug(KSIRK_LOG) << "GameXmlHandler starts new game with ONU file: " << atts.value("file");
46 by Gaël de Chalendar
- Local game works
151
    if (!(m_game.automaton()->playerList()->isEmpty()))
1 by Gaël de Chalendar
Imported KsirK to Playground/games
152
    {
46 by Gaël de Chalendar
- Local game works
153
      m_game.automaton()->playerList()->clear();
154
      m_game.automaton()->currentPlayer(0);
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
155
      qCDebug(KSIRK_LOG) << "  playerList size = " << m_game.automaton()->playerList()->count();
1 by Gaël de Chalendar
Imported KsirK to Playground/games
156
    }
46 by Gaël de Chalendar
- Local game works
157
    m_game.automaton()->game()->newSkin(atts.value("file"));
1 by Gaël de Chalendar
Imported KsirK to Playground/games
158
  }
159
  else if (localName == "country")
160
  {
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
161
//   qCDebug(KSIRK_LOG) << "GameXmlHandler loads country: " << atts.value("name");
1 by Gaël de Chalendar
Imported KsirK to Playground/games
162
    Country* country = m_game.theWorld()->countryNamed(atts.value("name"));
431 by Gaël de Chalendar
Use a bubble instead of a message box to inform of a player elimination;
163
    unsigned int gotNbArmies = atts.value("nbArmies").toInt();
1 by Gaël de Chalendar
Imported KsirK to Playground/games
164
    country->nbArmies(gotNbArmies);
165
    
641.1.45 by Luigi Toscano
Port to categorized logging most of ksirk
166
    qCDebug(KSIRK_LOG) << "Storing" << atts.value("owner") << "as owner of" << atts.value("name");
363 by Gaël de Chalendar
Replaced numeric ids by names;
167
    m_ownersMap.insert(atts.value("name"), atts.value("owner"));
1 by Gaël de Chalendar
Imported KsirK to Playground/games
168
  }
169
  else if (localName == "goal")
170
  {
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
171
    qCDebug(KSIRK_LOG) << "loads goal for: " << atts.value("player");
46 by Gaël de Chalendar
- Local game works
172
    m_goal = new GameLogic::Goal(m_game.automaton());
1 by Gaël de Chalendar
Imported KsirK to Playground/games
173
    m_goalPlayerName = atts.value("player");
46 by Gaël de Chalendar
- Local game works
174
    Player* player = m_game.automaton()->playerNamed(atts.value("player").toUtf8().data());
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
175
//     qCDebug(KSIRK_LOG) << "Got player pointer " << player ;
1 by Gaël de Chalendar
Imported KsirK to Playground/games
176
    m_goal->player(player);
431 by Gaël de Chalendar
Use a bubble instead of a message box to inform of a player elimination;
177
    unsigned int type = atts.value("type").toInt();
1 by Gaël de Chalendar
Imported KsirK to Playground/games
178
    m_goal->type(GameLogic::Goal::GoalType(type));
179
    m_goal->description(atts.value("description"));
431 by Gaël de Chalendar
Use a bubble instead of a message box to inform of a player elimination;
180
    unsigned int nbCountries = atts.value("nbCountries").toInt();
1 by Gaël de Chalendar
Imported KsirK to Playground/games
181
    m_goal->nbCountries(nbCountries);
431 by Gaël de Chalendar
Use a bubble instead of a message box to inform of a player elimination;
182
    unsigned int nbArmiesByCountry = atts.value("nbArmiesByCountry").toInt();
1 by Gaël de Chalendar
Imported KsirK to Playground/games
183
    m_goal->nbArmiesByCountry(nbArmiesByCountry);
184
    
185
    m_inGoal = true;
186
  }
187
  else if (localName == "player" && m_inGoal)
188
  {
252.1.6 by Gaël de Chalendar
* [DONE] crash when loading the target player when the goal is to destroy it
189
    m_goal->players().push_back(atts.value("name"));
1 by Gaël de Chalendar
Imported KsirK to Playground/games
190
  }
191
  else if (localName == "continent" && m_inGoal)
192
  {
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
193
//     qCDebug(KSIRK_LOG) << "Getting id of continent named " << atts.value("name");
363 by Gaël de Chalendar
Replaced numeric ids by names;
194
    QString id;
195
    if (!atts.value("name").isEmpty())
196
        id = atts.value("name");
197
    m_goal->continents().push_back(id);
1 by Gaël de Chalendar
Imported KsirK to Playground/games
198
  }
199
  return true;
200
}
201
202
bool GameXmlHandler::endElement(const QString& namespaceURI, const QString& localName, const QString& qName)
203
{
30 by Gaël de Chalendar
SVN_SILENT some compilation warnings removed
204
  Q_UNUSED(namespaceURI);
205
  Q_UNUSED(qName);
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
206
//   qCDebug(KSIRK_LOG) << "endElement " << localName << " / " << qName ;
1 by Gaël de Chalendar
Imported KsirK to Playground/games
207
  if (localName == "game")
208
  {
363 by Gaël de Chalendar
Replaced numeric ids by names;
209
    foreach (const QString& k, m_ownersMap.keys())
1 by Gaël de Chalendar
Imported KsirK to Playground/games
210
    {
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
211
//       qCDebug(KSIRK_LOG) << "Setting owner of " << k << " to " << m_ownersMap[k];
363 by Gaël de Chalendar
Replaced numeric ids by names;
212
      Country* country = m_game.theWorld()->countryNamed(k);
213
      Player* owner = m_game.automaton()->playerNamed(m_ownersMap[k]);
1 by Gaël de Chalendar
Imported KsirK to Playground/games
214
      if (owner)
215
      {
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
216
//         qCDebug(KSIRK_LOG) << "Setting owner of " << country->name() << " to " << owner->name();
1 by Gaël de Chalendar
Imported KsirK to Playground/games
217
        country-> owner(owner);
218
      }
219
      else
220
      {
641.1.45 by Luigi Toscano
Port to categorized logging most of ksirk
221
//         qCDebug(KSIRK_LOG) << "Player" << m_ownersMap[k] << "not found";
363 by Gaël de Chalendar
Replaced numeric ids by names;
222
        QList<GameLogic::PlayerMatrix>::iterator itw,itw_end;
1 by Gaël de Chalendar
Imported KsirK to Playground/games
223
        itw = m_waitedPlayers.begin(); itw_end = m_waitedPlayers.end();
224
        for (; itw != itw_end; itw++)
225
        {
363 by Gaël de Chalendar
Replaced numeric ids by names;
226
          if ( (*itw).name == m_ownersMap[k] )
1 by Gaël de Chalendar
Imported KsirK to Playground/games
227
          {
363 by Gaël de Chalendar
Replaced numeric ids by names;
228
            (*itw).countries.push_back(k);
1 by Gaël de Chalendar
Imported KsirK to Playground/games
229
            break;
230
          }
231
        }
232
      }
233
    }
234
    if (!m_waitedPlayers.empty())
235
    {
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
236
//       qCDebug(KSIRK_LOG) << "There is waited players: does not change state nor run game...";
1 by Gaël de Chalendar
Imported KsirK to Playground/games
237
      m_waitedPlayers[0].state = m_savedState;
238
    }
239
    else
240
    {
734 by Laurent Montel
Remove endl; at end of qDebug. It already adds "\n"
241
//       qCDebug(KSIRK_LOG) << "GameXmlHandler set game state to: " << m_savedState ;
46 by Gaël de Chalendar
- Local game works
242
      m_game.automaton()->state(m_savedState);
1 by Gaël de Chalendar
Imported KsirK to Playground/games
243
    }
244
  }
245
  else if (localName == "goal")
246
  {
247
    m_inGoal = false;
248
    if (m_goal)
249
    {
250
      if (m_goal->player())
251
      {
40 by Gaël de Chalendar
Avoid a crash but does not solve it. It reappears later
252
        m_goal->player()->goal(*m_goal);
1 by Gaël de Chalendar
Imported KsirK to Playground/games
253
      }
254
      else
255
      {
363 by Gaël de Chalendar
Replaced numeric ids by names;
256
        QList<GameLogic::PlayerMatrix>::iterator itw,itw_end;
1 by Gaël de Chalendar
Imported KsirK to Playground/games
257
        itw = m_waitedPlayers.begin(); itw_end = m_waitedPlayers.end();
258
        for (; itw != itw_end; itw++)
259
        {
260
          if ( (*itw).name == m_goalPlayerName )
261
          {
40 by Gaël de Chalendar
Avoid a crash but does not solve it. It reappears later
262
            (*itw).goal = *m_goal;
1 by Gaël de Chalendar
Imported KsirK to Playground/games
263
            break;
264
          }
265
        }
266
      }
267
      delete m_goal;
268
    }
269
    m_goal = 0;
270
  }
271
  return true;
272
}
273
274
275
} // closing namespace SaveLoad
276
} // closing namespace Ksirk
277
278