/******************************************************************* * 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 "xml_writer.h" #include "encoding.h" #include "turn_cmd.h" #include "game_params.h" #include "game.h" #include "player.h" #include "ai_percent.h" #include "game_exception.h" #include "turn_cmd.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" 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("Unsupported move: " + 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("Cannot open file for writing: '" + 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("Invalid player type for player " + i); out << indent << "" << lrint(ai->getPercent() * 100) << "" << 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 &turnCmdVect = iGame.getNavigation().getTurns(); BOOST_FOREACH(const TurnCmd *turn, turnCmdVect) { if (turn->getCommands().empty() && turn == turnCmdVect.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() + 1; out << indent << "" << toUtf8(rackCmd->getRack().toString()) << "" << endl; } else if (dynamic_cast(cmd)) { const PlayerMoveCmd *moveCmd = static_cast(cmd); unsigned int id = moveCmd->getPlayer().getId() + 1; 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() + 1; 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() + 1; 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())); // XXX //throw SaveGameException("Unsupported command: " + lfw(cmd->toString())); } // TODO } removeIndent(indent); out << indent << "" << endl; } removeIndent(indent); out << indent << "" << endl; // End of the game history // ------------------------ out << "" << endl; }