/******************************************************************* * Eliot * Copyright (C) 2009-2012 Olivier Teulière * Authors: Olivier Teulière * * 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 #include #include #include #include #include "config.h" #if ENABLE_NLS # include # define _(String) gettext(String) #else # define _(String) String #endif #include "xml_writer.h" #include "encoding.h" #include "turn.h" #include "turn_data.h" #include "game_params.h" #include "game.h" #include "player.h" #include "ai_percent.h" #include "game_exception.h" #include "turn.h" #include "game_rack_cmd.h" #include "game_move_cmd.h" #include "player_rack_cmd.h" #include "player_move_cmd.h" #include "player_event_cmd.h" #include "master_move_cmd.h" #include "dic.h" #include "header.h" // 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" #define FMT1(s, a1) (boost::format(s) % (a1)).str() #define FMT2(s, a1, a2) (boost::format(s) % (a1) % (a2)).str() using namespace std; INIT_LOGGER(game, XmlWriter); 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, const string &iTag, int iPlayerId) { out << "<" << iTag; if (iPlayerId != -1) out << " playerId=\"" << iPlayerId << "\""; out << " points=\"" << iMove.getScore() << "\" type=\""; if (iMove.isValid()) { const Round &round = iMove.getRound(); out << "valid\" word=\"" << toUtf8(round.getWord()) << "\" coord=\"" << toUtf8(round.getCoord().toString()) << "\" />"; } else if (iMove.isInvalid()) { out << "invalid\" word=\"" << toUtf8(iMove.getBadWord()) << "\" coord=\"" << toUtf8(iMove.getBadCoord()) << "\" />"; } else if (iMove.isChangeLetters()) out << "change\" letters=\"" << toUtf8(iMove.getChangedLetters()) << "\" />"; else if (iMove.isPass()) out << "pass\" />"; else if (iMove.isNull()) out << "none\" />"; else throw SaveGameException(FMT1(_("Unsupported move: %1%"), lfw(iMove.toString()))); } void XmlWriter::write(const Game &iGame, const string &iFileName) { LOG_INFO("Saving game into '" << iFileName << "'"); ofstream out(iFileName.c_str()); if (!out.is_open()) throw SaveGameException(FMT1(_("Cannot open file for writing: '%1%'"), iFileName)); out << "" << endl; string indent = ""; out << indent << "" << endl; addIndent(indent); // ------------------------ // Write the dictionary information out << indent << "" << endl; addIndent(indent); const Header &header = iGame.getDic().getHeader(); out << indent << "" << toUtf8(header.getName()) << "" << endl; out << indent << ""; if (header.getType() == Header::kDAWG) out << "dawg"; else if (header.getType() == Header::kGADDAG) out << "gaddag"; else throw SaveGameException(_("Invalid dictionary type")); out << "" << endl; // 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 << "" << toUtf8(displayLetters) << "" << endl; out << indent << "" << header.getNbWords() << "" << endl; removeIndent(indent); out << indent << "" << endl; // End of dictionary information // ------------------------ // ------------------------ // Write the game header out << indent << "" << endl; addIndent(indent); // Game type out << indent << ""; if (iGame.getMode() == GameParams::kDUPLICATE) out << "duplicate"; else if (iGame.getMode() == GameParams::kFREEGAME) out << "freegame"; else if (iGame.getMode() == GameParams::kARBITRATION) out << "arbitration"; else out << "training"; out << "" << endl; // Game variant if (iGame.getParams().hasVariant(GameParams::kJOKER)) out << indent << "bingo" << endl; if (iGame.getParams().hasVariant(GameParams::kEXPLOSIVE)) out << indent << "explosive" << endl; if (iGame.getParams().hasVariant(GameParams::k7AMONG8)) out << indent << "7among8" << endl; // Players for (unsigned int i = 0; i < iGame.getNPlayers(); ++i) { const Player &player = iGame.getPlayer(i); out << indent << "" << endl; addIndent(indent); out << indent << "" << toUtf8(player.getName()) << "" << endl; out << indent << "" << (player.isHuman() ? "human" : "computer") << "" << endl; if (!player.isHuman()) { const AIPercent *ai = dynamic_cast(&player); if (ai == NULL) throw SaveGameException(FMT1(_("Invalid player type for player %1%"), i)); out << indent << "" << lrint(ai->getPercent() * 100) << "" << endl; } out << indent << "" << player.getTableNb() << "" << endl; removeIndent(indent); out << indent << "" << endl; } // Number of turns out << indent << "" << iGame.getNavigation().getNbTurns() << "" << endl; removeIndent(indent); out << indent << "" << endl; // End of the header // ------------------------ // ------------------------ // Write the game history out << indent << "" << endl; addIndent(indent); #if 0 iGame.getNavigation().print(); #endif const vector &turnVect = iGame.getNavigation().getTurns(); BOOST_FOREACH(const Turn *turn, turnVect) { if (turn->getCommands().empty() && turn == turnVect.back()) continue; out << indent << "" << endl; addIndent(indent); BOOST_FOREACH(const Command *cmd, turn->getCommands()) { if (dynamic_cast(cmd)) { const GameRackCmd *rackCmd = static_cast(cmd); out << indent << "" << toUtf8(rackCmd->getRack().toString()) << "" << endl; } else if (dynamic_cast(cmd)) { const PlayerRackCmd *rackCmd = static_cast(cmd); unsigned int id = rackCmd->getPlayer().getId(); out << indent << "" << toUtf8(rackCmd->getRack().toString()) << "" << endl; } else if (dynamic_cast(cmd)) { const PlayerMoveCmd *moveCmd = static_cast(cmd); unsigned int id = moveCmd->getPlayer().getId(); out << indent; writeMove(out, moveCmd->getMove(), "PlayerMove", id); out << endl; } else if (dynamic_cast(cmd)) { const GameMoveCmd *moveCmd = static_cast(cmd); unsigned int id = moveCmd->getPlayerId(); out << indent; writeMove(out, moveCmd->getMove(), "GameMove", id); out << endl; } else if (dynamic_cast(cmd)) { const MasterMoveCmd *moveCmd = static_cast(cmd); out << indent; writeMove(out, moveCmd->getMove(), "MasterMove", -1); out << endl; } else if (dynamic_cast(cmd)) { const PlayerEventCmd *eventCmd = static_cast(cmd); unsigned int id = eventCmd->getPlayer().getId(); 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; } // End game bonuses (freegame mode) else if (eventCmd->getEventType() == PlayerEventCmd::END_GAME) { out << indent << "" << endl; } else { LOG_ERROR("Unknown event type: " << eventCmd->getEventType()); } } else { LOG_ERROR("Unsupported command: " << lfw(cmd->toString())); out << indent << "" << endl; // XXX //throw SaveGameException(FMT1(_("Unsupported command: %1%"), lfw(cmd->toString()))); } } removeIndent(indent); out << indent << "" << endl; } removeIndent(indent); out << indent << "" << endl; // End of the game history // ------------------------ // Statistics out << indent << "" << endl; out << indent << "" << 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 << "" << 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 << "" << endl; removeIndent(indent); } removeIndent(indent); out << indent << "" << endl; // End of the statistics out << "" << endl; }