/******************************************************************* * 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_move_cmd.h" #include "player_rack_cmd.h" #include "player_move_cmd.h" #include "player_points_cmd.h" #include "master_move_cmd.h" #include "mark_played_cmd.h" 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, unsigned int iPlayerId) { out << "<" << iTag << " playerid=\"" << iPlayerId << "\" type=\""; if (iMove.getType() == Move::VALID_ROUND) { const Round &round = iMove.getRound(); out << "valid\" word=\"" << toUtf8(round.getWord()) << "\" coord=\"" << toUtf8(round.getCoord().toString()) << "\" />"; } else if (iMove.getType() == Move::INVALID_WORD) { out << "invalid\" word=\"" << toUtf8(iMove.getBadWord()) << "\" coord=\"" << toUtf8(iMove.getBadCoord()) << "\" />"; } else if (iMove.getType() == Move::CHANGE_LETTERS) out << "change\" letters=\"" << toUtf8(iMove.getChangedLetters()) << "\" />"; else if (iMove.getType() == Move::PASS) out << "pass\" />"; else if (iMove.getType() == Move::NO_MOVE) 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 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 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().getCommands(); 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 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 PlayerPointsCmd *pointsCmd = static_cast(cmd); unsigned int id = pointsCmd->getPlayer().getId() + 1; out << indent << "" << pointsCmd->getPoints() << "" << 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)) { // Ignore this command, as it is an implementation detail } 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; }