From 34ed25e9e9d70021e84d1141b9b2d7a35d545fb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Teuli=C3=A8re?= Date: Wed, 24 Oct 2012 00:28:55 +0200 Subject: [PATCH] Saved games: save some statistics (not used when loading the game) --- game/xml_writer.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/game/xml_writer.cpp b/game/xml_writer.cpp index aee0abf..dc31524 100644 --- a/game/xml_writer.cpp +++ b/game/xml_writer.cpp @@ -35,6 +35,7 @@ #include "xml_writer.h" #include "encoding.h" #include "turn.h" +#include "turn_data.h" #include "game_params.h" #include "game.h" #include "player.h" @@ -216,6 +217,7 @@ void XmlWriter::write(const Game &iGame, const string &iFileName) { if (turn->getCommands().empty() && turn == turnVect.back()) continue; + out << indent << "" << endl; addIndent(indent); BOOST_FOREACH(const Command *cmd, turn->getCommands()) @@ -309,6 +311,60 @@ void XmlWriter::write(const Game &iGame, const string &iFileName) // 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; }