2009-11-29 17:01:31 +01:00
|
|
|
/*******************************************************************
|
|
|
|
* Eliot
|
2012-01-17 00:27:32 +01:00
|
|
|
* Copyright (C) 2009-2012 Olivier Teulière
|
2009-11-29 17:01:31 +01:00
|
|
|
* Authors: Olivier Teulière <ipkiss @@ gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <fstream>
|
2010-03-06 17:54:20 +01:00
|
|
|
#include <cmath>
|
2009-11-29 17:01:31 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2012-10-06 18:18:21 +02:00
|
|
|
#include <boost/format.hpp>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#if ENABLE_NLS
|
|
|
|
# include <libintl.h>
|
|
|
|
# define _(String) gettext(String)
|
|
|
|
#else
|
|
|
|
# define _(String) String
|
|
|
|
#endif
|
2009-11-29 17:01:31 +01:00
|
|
|
|
|
|
|
#include "xml_writer.h"
|
|
|
|
#include "encoding.h"
|
2012-10-06 11:14:01 +02:00
|
|
|
#include "turn.h"
|
2012-10-24 00:28:55 +02:00
|
|
|
#include "turn_data.h"
|
2011-08-27 19:21:26 +02:00
|
|
|
#include "game_params.h"
|
2009-11-29 17:01:31 +01:00
|
|
|
#include "game.h"
|
|
|
|
#include "player.h"
|
|
|
|
#include "ai_percent.h"
|
|
|
|
#include "game_exception.h"
|
2012-10-06 11:14:01 +02:00
|
|
|
#include "turn.h"
|
2012-02-25 23:16:42 +01:00
|
|
|
#include "game_rack_cmd.h"
|
2009-11-29 17:01:31 +01:00
|
|
|
#include "game_move_cmd.h"
|
|
|
|
#include "player_rack_cmd.h"
|
|
|
|
#include "player_move_cmd.h"
|
2012-05-20 21:31:45 +02:00
|
|
|
#include "player_event_cmd.h"
|
2012-01-17 17:43:04 +01:00
|
|
|
#include "master_move_cmd.h"
|
2012-03-22 08:33:19 +01:00
|
|
|
#include "dic.h"
|
|
|
|
#include "header.h"
|
2009-11-29 17:01:31 +01:00
|
|
|
|
2012-04-04 21:53:54 +02:00
|
|
|
// Current version of our save game format. Bump it when it becomes
|
|
|
|
// incompatible (and keep it in sync with xml_reader.cpp)
|
|
|
|
#define CURRENT_XML_VERSION "2"
|
|
|
|
|
2012-10-06 18:18:21 +02:00
|
|
|
#define FMT1(s, a1) (boost::format(s) % (a1)).str()
|
|
|
|
#define FMT2(s, a1, a2) (boost::format(s) % (a1) % (a2)).str()
|
|
|
|
|
|
|
|
|
2009-11-29 17:01:31 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2011-01-30 00:47:20 +01:00
|
|
|
INIT_LOGGER(game, XmlWriter);
|
2009-11-29 17:01:31 +01:00
|
|
|
|
2012-10-06 18:18:21 +02:00
|
|
|
|
2009-11-29 17:01:31 +01:00
|
|
|
static void addIndent(string &s)
|
|
|
|
{
|
|
|
|
s += " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void removeIndent(string &s)
|
|
|
|
{
|
|
|
|
if (s.size() >= 4)
|
|
|
|
s.resize(s.size() - 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static string toUtf8(const wstring &s)
|
|
|
|
{
|
|
|
|
return writeInUTF8(s, "Saving game");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void writeMove(ostream &out, const Move &iMove,
|
2012-03-05 01:27:56 +01:00
|
|
|
const string &iTag, int iPlayerId)
|
2009-11-29 17:01:31 +01:00
|
|
|
{
|
2012-03-29 08:53:58 +02:00
|
|
|
out << "<" << iTag;
|
|
|
|
if (iPlayerId != -1)
|
2012-10-23 23:58:31 +02:00
|
|
|
out << " playerId=\"" << iPlayerId << "\"";
|
2012-03-29 08:53:58 +02:00
|
|
|
out << " points=\"" << iMove.getScore() << "\" type=\"";
|
2012-04-30 21:21:38 +02:00
|
|
|
if (iMove.isValid())
|
2009-11-29 17:01:31 +01:00
|
|
|
{
|
|
|
|
const Round &round = iMove.getRound();
|
|
|
|
out << "valid\" word=\"" << toUtf8(round.getWord())
|
|
|
|
<< "\" coord=\"" << toUtf8(round.getCoord().toString()) << "\" />";
|
|
|
|
}
|
2012-04-30 21:21:38 +02:00
|
|
|
else if (iMove.isInvalid())
|
2009-11-29 17:01:31 +01:00
|
|
|
{
|
|
|
|
out << "invalid\" word=\"" << toUtf8(iMove.getBadWord())
|
|
|
|
<< "\" coord=\"" << toUtf8(iMove.getBadCoord()) << "\" />";
|
|
|
|
}
|
2012-04-30 21:21:38 +02:00
|
|
|
else if (iMove.isChangeLetters())
|
2009-11-29 17:01:31 +01:00
|
|
|
out << "change\" letters=\"" << toUtf8(iMove.getChangedLetters()) << "\" />";
|
2012-04-30 21:21:38 +02:00
|
|
|
else if (iMove.isPass())
|
2009-11-29 17:01:31 +01:00
|
|
|
out << "pass\" />";
|
2012-04-30 21:21:38 +02:00
|
|
|
else if (iMove.isNull())
|
2012-01-17 00:27:32 +01:00
|
|
|
out << "none\" />";
|
2009-11-29 17:01:31 +01:00
|
|
|
else
|
2012-10-06 18:18:21 +02:00
|
|
|
throw SaveGameException(FMT1(_("Unsupported move: %1%"), lfw(iMove.toString())));
|
2009-11-29 17:01:31 +01:00
|
|
|
}
|
|
|
|
|
2012-10-06 18:18:21 +02:00
|
|
|
|
2009-11-29 17:01:31 +01:00
|
|
|
void XmlWriter::write(const Game &iGame, const string &iFileName)
|
|
|
|
{
|
2011-01-30 00:47:20 +01:00
|
|
|
LOG_INFO("Saving game into '" << iFileName << "'");
|
2009-11-29 17:01:31 +01:00
|
|
|
ofstream out(iFileName.c_str());
|
|
|
|
if (!out.is_open())
|
2012-10-06 18:18:21 +02:00
|
|
|
throw SaveGameException(FMT1(_("Cannot open file for writing: '%1%'"), iFileName));
|
2009-11-29 17:01:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl;
|
|
|
|
|
|
|
|
string indent = "";
|
2012-04-04 21:53:54 +02:00
|
|
|
out << indent << "<EliotGame format=\"" << CURRENT_XML_VERSION << "\">" << endl;
|
2009-11-29 17:01:31 +01:00
|
|
|
addIndent(indent);
|
|
|
|
|
|
|
|
// ------------------------
|
2012-03-22 08:33:19 +01:00
|
|
|
// Write the dictionary information
|
|
|
|
out << indent << "<Dictionary>" << endl;
|
|
|
|
addIndent(indent);
|
|
|
|
const Header &header = iGame.getDic().getHeader();
|
|
|
|
out << indent << "<Name>" << toUtf8(header.getName()) << "</Name>" << endl;
|
|
|
|
out << indent << "<Type>";
|
|
|
|
if (header.getType() == Header::kDAWG)
|
|
|
|
out << "dawg";
|
|
|
|
else if (header.getType() == Header::kGADDAG)
|
|
|
|
out << "gaddag";
|
|
|
|
else
|
2012-10-06 18:18:21 +02:00
|
|
|
throw SaveGameException(_("Invalid dictionary type"));
|
2012-03-22 08:33:19 +01:00
|
|
|
out << "</Type>" << endl;
|
2012-03-28 23:21:24 +02:00
|
|
|
// Retrieve the dictionary letters, ans separate them with spaces
|
|
|
|
wstring lettersWithSpaces = header.getLetters();
|
|
|
|
for (size_t i = lettersWithSpaces.size() - 1; i > 0; --i)
|
|
|
|
lettersWithSpaces.insert(i, 1, L' ');
|
|
|
|
// Convert to a display string
|
|
|
|
const wstring &displayLetters =
|
|
|
|
iGame.getDic().convertToDisplay(lettersWithSpaces);
|
|
|
|
out << indent << "<Letters>" << toUtf8(displayLetters) << "</Letters>" << endl;
|
2012-03-22 08:33:19 +01:00
|
|
|
out << indent << "<WordNb>" << header.getNbWords() << "</WordNb>" << endl;
|
|
|
|
removeIndent(indent);
|
|
|
|
out << indent << "</Dictionary>" << endl;
|
|
|
|
// End of dictionary information
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
// ------------------------
|
|
|
|
// Write the game header
|
2009-11-29 17:01:31 +01:00
|
|
|
out << indent << "<Game>" << endl;
|
|
|
|
addIndent(indent);
|
|
|
|
// Game type
|
|
|
|
out << indent << "<Mode>";
|
2011-08-28 18:20:00 +02:00
|
|
|
if (iGame.getMode() == GameParams::kDUPLICATE)
|
2009-11-29 17:01:31 +01:00
|
|
|
out << "duplicate";
|
2011-08-28 18:20:00 +02:00
|
|
|
else if (iGame.getMode() == GameParams::kFREEGAME)
|
2009-11-29 17:01:31 +01:00
|
|
|
out << "freegame";
|
2012-03-05 01:27:56 +01:00
|
|
|
else if (iGame.getMode() == GameParams::kARBITRATION)
|
|
|
|
out << "arbitration";
|
2009-11-29 17:01:31 +01:00
|
|
|
else
|
|
|
|
out << "training";
|
|
|
|
out << "</Mode>" << endl;
|
|
|
|
|
|
|
|
// Game variant
|
2011-08-28 19:24:37 +02:00
|
|
|
if (iGame.getParams().hasVariant(GameParams::kJOKER))
|
2009-11-29 17:01:31 +01:00
|
|
|
out << indent << "<Variant>bingo</Variant>" << endl;
|
2011-08-28 19:24:37 +02:00
|
|
|
if (iGame.getParams().hasVariant(GameParams::kEXPLOSIVE))
|
2009-11-29 17:01:31 +01:00
|
|
|
out << indent << "<Variant>explosive</Variant>" << endl;
|
2011-08-28 19:24:37 +02:00
|
|
|
if (iGame.getParams().hasVariant(GameParams::k7AMONG8))
|
2011-08-28 15:47:31 +02:00
|
|
|
out << indent << "<Variant>7among8</Variant>" << endl;
|
2009-11-29 17:01:31 +01:00
|
|
|
|
|
|
|
// Players
|
|
|
|
for (unsigned int i = 0; i < iGame.getNPlayers(); ++i)
|
|
|
|
{
|
|
|
|
const Player &player = iGame.getPlayer(i);
|
2012-10-24 00:31:21 +02:00
|
|
|
out << indent << "<Player id=\"" << player.getId() << "\">" << endl;
|
2009-11-29 17:01:31 +01:00
|
|
|
addIndent(indent);
|
|
|
|
out << indent << "<Name>" << toUtf8(player.getName()) << "</Name>" << endl;
|
|
|
|
out << indent << "<Type>" << (player.isHuman() ? "human" : "computer") << "</Type>" << endl;
|
|
|
|
if (!player.isHuman())
|
|
|
|
{
|
|
|
|
const AIPercent *ai = dynamic_cast<const AIPercent *>(&player);
|
|
|
|
if (ai == NULL)
|
2012-10-06 20:34:49 +02:00
|
|
|
throw SaveGameException(FMT1(_("Invalid player type for player %1%"), i));
|
2010-03-06 17:54:20 +01:00
|
|
|
out << indent << "<Level>" << lrint(ai->getPercent() * 100) << "</Level>" << endl;
|
2009-11-29 17:01:31 +01:00
|
|
|
}
|
2012-10-22 17:35:29 +02:00
|
|
|
out << indent << "<TableNb>" << player.getTableNb() << "</TableNb>" << endl;
|
2009-11-29 17:01:31 +01:00
|
|
|
removeIndent(indent);
|
|
|
|
out << indent << "</Player>" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Number of turns
|
|
|
|
out << indent << "<Turns>"
|
|
|
|
<< iGame.getNavigation().getNbTurns() << "</Turns>" << endl;
|
|
|
|
|
|
|
|
removeIndent(indent);
|
|
|
|
out << indent << "</Game>" << endl;
|
|
|
|
// End of the header
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
// ------------------------
|
|
|
|
// Write the game history
|
|
|
|
out << indent << "<History>" << endl;
|
|
|
|
addIndent(indent);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
iGame.getNavigation().print();
|
|
|
|
#endif
|
2012-10-06 11:32:00 +02:00
|
|
|
const vector<Turn *> &turnVect = iGame.getNavigation().getTurns();
|
|
|
|
BOOST_FOREACH(const Turn *turn, turnVect)
|
2009-11-29 17:01:31 +01:00
|
|
|
{
|
2012-10-06 11:32:00 +02:00
|
|
|
if (turn->getCommands().empty() && turn == turnVect.back())
|
2009-11-29 17:01:31 +01:00
|
|
|
continue;
|
2012-10-24 00:28:55 +02:00
|
|
|
|
2009-11-29 17:01:31 +01:00
|
|
|
out << indent << "<Turn>" << endl;
|
|
|
|
addIndent(indent);
|
|
|
|
BOOST_FOREACH(const Command *cmd, turn->getCommands())
|
|
|
|
{
|
2012-02-25 23:16:42 +01:00
|
|
|
if (dynamic_cast<const GameRackCmd*>(cmd))
|
|
|
|
{
|
|
|
|
const GameRackCmd *rackCmd = static_cast<const GameRackCmd*>(cmd);
|
|
|
|
out << indent << "<GameRack>"
|
|
|
|
<< toUtf8(rackCmd->getRack().toString())
|
|
|
|
<< "</GameRack>" << endl;
|
|
|
|
}
|
|
|
|
else if (dynamic_cast<const PlayerRackCmd*>(cmd))
|
2009-11-29 17:01:31 +01:00
|
|
|
{
|
|
|
|
const PlayerRackCmd *rackCmd = static_cast<const PlayerRackCmd*>(cmd);
|
2012-10-24 00:31:21 +02:00
|
|
|
unsigned int id = rackCmd->getPlayer().getId();
|
2012-10-23 23:58:31 +02:00
|
|
|
out << indent << "<PlayerRack playerId=\"" << id << "\">"
|
2009-11-29 17:01:31 +01:00
|
|
|
<< toUtf8(rackCmd->getRack().toString())
|
|
|
|
<< "</PlayerRack>" << endl;
|
|
|
|
}
|
|
|
|
else if (dynamic_cast<const PlayerMoveCmd*>(cmd))
|
|
|
|
{
|
|
|
|
const PlayerMoveCmd *moveCmd = static_cast<const PlayerMoveCmd*>(cmd);
|
2012-10-24 00:31:21 +02:00
|
|
|
unsigned int id = moveCmd->getPlayer().getId();
|
2009-11-29 17:01:31 +01:00
|
|
|
out << indent;
|
|
|
|
writeMove(out, moveCmd->getMove(), "PlayerMove", id);
|
|
|
|
out << endl;
|
|
|
|
}
|
|
|
|
else if (dynamic_cast<const GameMoveCmd*>(cmd))
|
|
|
|
{
|
|
|
|
const GameMoveCmd *moveCmd = static_cast<const GameMoveCmd*>(cmd);
|
2012-10-24 00:31:21 +02:00
|
|
|
unsigned int id = moveCmd->getPlayerId();
|
2009-11-29 17:01:31 +01:00
|
|
|
out << indent;
|
|
|
|
writeMove(out, moveCmd->getMove(), "GameMove", id);
|
|
|
|
out << endl;
|
|
|
|
}
|
2012-01-17 17:43:04 +01:00
|
|
|
else if (dynamic_cast<const MasterMoveCmd*>(cmd))
|
|
|
|
{
|
|
|
|
const MasterMoveCmd *moveCmd = static_cast<const MasterMoveCmd*>(cmd);
|
|
|
|
out << indent;
|
|
|
|
writeMove(out, moveCmd->getMove(), "MasterMove", -1);
|
|
|
|
out << endl;
|
|
|
|
|
|
|
|
}
|
2012-05-20 21:31:45 +02:00
|
|
|
else if (dynamic_cast<const PlayerEventCmd*>(cmd))
|
|
|
|
{
|
|
|
|
const PlayerEventCmd *eventCmd = static_cast<const PlayerEventCmd*>(cmd);
|
2012-10-24 00:31:21 +02:00
|
|
|
unsigned int id = eventCmd->getPlayer().getId();
|
2012-05-20 21:31:45 +02:00
|
|
|
int value = eventCmd->getPoints();
|
|
|
|
// Warnings
|
|
|
|
if (eventCmd->getEventType() == PlayerEventCmd::WARNING)
|
|
|
|
{
|
2012-10-23 23:58:31 +02:00
|
|
|
out << indent << "<Warning playerId=\"" << id << "\" />" << endl;
|
2012-05-20 21:31:45 +02:00
|
|
|
}
|
|
|
|
// Penalties
|
|
|
|
else if (eventCmd->getEventType() == PlayerEventCmd::PENALTY)
|
|
|
|
{
|
2012-10-23 23:58:31 +02:00
|
|
|
out << indent << "<Penalty playerId=\"" << id
|
2012-05-20 21:31:45 +02:00
|
|
|
<< "\" points=\"" << value << "\" />" << endl;
|
|
|
|
}
|
|
|
|
// Solos
|
|
|
|
else if (eventCmd->getEventType() == PlayerEventCmd::SOLO)
|
|
|
|
{
|
2012-10-23 23:58:31 +02:00
|
|
|
out << indent << "<Solo playerId=\"" << id
|
2012-05-20 21:31:45 +02:00
|
|
|
<< "\" points=\"" << value << "\" />" << endl;
|
|
|
|
}
|
2012-10-05 17:51:01 +02:00
|
|
|
// End game bonuses (freegame mode)
|
|
|
|
else if (eventCmd->getEventType() == PlayerEventCmd::END_GAME)
|
|
|
|
{
|
2012-10-23 23:58:31 +02:00
|
|
|
out << indent << "<EndGame playerId=\"" << id
|
2012-10-05 17:51:01 +02:00
|
|
|
<< "\" points=\"" << value << "\" />" << endl;
|
|
|
|
}
|
2012-05-20 21:31:45 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_ERROR("Unknown event type: " << eventCmd->getEventType());
|
|
|
|
}
|
|
|
|
}
|
2009-11-29 17:01:31 +01:00
|
|
|
else
|
|
|
|
{
|
2011-08-28 16:33:50 +02:00
|
|
|
LOG_ERROR("Unsupported command: " << lfw(cmd->toString()));
|
2012-10-05 19:52:05 +02:00
|
|
|
out << indent << "<!-- FIXME: Unsupported command: " << lfw(cmd->toString()) << " -->" << endl;
|
2009-11-29 17:01:31 +01:00
|
|
|
// XXX
|
2012-10-06 18:18:21 +02:00
|
|
|
//throw SaveGameException(FMT1(_("Unsupported command: %1%"), lfw(cmd->toString())));
|
2009-11-29 17:01:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
removeIndent(indent);
|
|
|
|
out << indent << "</Turn>" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
removeIndent(indent);
|
|
|
|
out << indent << "</History>" << endl;
|
|
|
|
// End of the game history
|
|
|
|
// ------------------------
|
|
|
|
|
2012-10-24 00:28:55 +02:00
|
|
|
// Statistics
|
|
|
|
out << indent << "<!-- These statistics are simply informative, "
|
|
|
|
<< "they are not used when loading a game. -->" << endl;
|
|
|
|
out << indent << "<Statistics>" << endl;
|
|
|
|
addIndent(indent);
|
|
|
|
|
|
|
|
// Compute the total number of points in the game
|
|
|
|
int gameTotal = 0;
|
|
|
|
for (unsigned i = 0; i < iGame.getHistory().getSize(); ++i)
|
|
|
|
{
|
|
|
|
gameTotal += iGame.getHistory().getTurn(i).getMove().getScore();
|
|
|
|
}
|
|
|
|
|
|
|
|
out << indent << "<GameStats"
|
|
|
|
<< " totalScore=\"" << gameTotal << "\""
|
|
|
|
<< " />" << endl;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < iGame.getNPlayers(); ++i)
|
|
|
|
{
|
|
|
|
const Player &player = iGame.getPlayer(i);
|
|
|
|
const int playerTotal = player.getTotalScore();
|
|
|
|
|
|
|
|
// Compute the percentage, compared to the top
|
|
|
|
long int percentage = lround((double)100 * playerTotal / gameTotal);
|
|
|
|
|
|
|
|
// Compute the rank of the player (naive algorithm)
|
|
|
|
int rank = 1;
|
|
|
|
for (unsigned j = 0; j < iGame.getNPlayers(); ++j)
|
|
|
|
{
|
|
|
|
if (i == j)
|
|
|
|
continue;
|
|
|
|
if (playerTotal < iGame.getPlayer(j).getTotalScore())
|
|
|
|
++rank;
|
|
|
|
}
|
|
|
|
|
|
|
|
out << indent << "<PlayerStats"
|
|
|
|
<< " playerId=\"" << player.getId() << "\"" << endl;
|
|
|
|
addIndent(indent);
|
|
|
|
out << indent << " rawScore=\"" << player.getMovePoints() << "\""
|
|
|
|
<< " warningsNb=\"" << player.getWarningsNb() << "\""
|
|
|
|
<< " penaltiesPoints=\"" << player.getPenaltyPoints() << "\""
|
|
|
|
<< " solosPoints=\"" << player.getSoloPoints() << "\"" << endl;
|
|
|
|
out << indent << " totalScore=\"" << playerTotal << "\""
|
|
|
|
<< " diffWithTop=\"" << (playerTotal - gameTotal) << "\""
|
|
|
|
<< " percentTop=\"" << percentage << "%\""
|
|
|
|
<< " rank=\"" << rank << "\""
|
|
|
|
<< " />" << endl;
|
|
|
|
removeIndent(indent);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeIndent(indent);
|
|
|
|
out << indent << "</Statistics>" << endl;
|
|
|
|
// End of the statistics
|
|
|
|
|
2009-11-29 17:01:31 +01:00
|
|
|
out << "</EliotGame>" << endl;
|
|
|
|
}
|
|
|
|
|