When loading the configuration, verify that the player names are unique. If they are not, use the default ones. (#96)

This commit is contained in:
lotodore
2012-01-09 18:40:51 +00:00
parent b9793bbb25
commit 92ba846d30
2 changed files with 38 additions and 1 deletions
+34 -1
View File
@@ -31,6 +31,7 @@
#include <sstream>
#include <cstdlib>
#include <fstream>
#include <set>
#include <sys/types.h>
#include <sys/stat.h>
@@ -322,6 +323,7 @@ ConfigFile::ConfigFile(char *argv0, bool readonly) : noWriteAccess(readonly)
}
fillBuffer();
checkAndCorrectBuffer();
}
}
@@ -384,6 +386,37 @@ void ConfigFile::fillBuffer()
}
}
void ConfigFile::checkAndCorrectBuffer()
{
boost::recursive_mutex::scoped_lock lock(m_configMutex);
// For now, only the player names are checked.
checkAndCorrectPlayerNames();
}
void ConfigFile::checkAndCorrectPlayerNames()
{
// Verify that the player names are uniquely set.
set<string> playerNames;
playerNames.insert(readConfigString("MyName"));
for(int i = 1; i <= 9; i++) {
ostringstream opponentVar;
opponentVar << "Opponent" << i << "Name";
playerNames.insert(readConfigString(opponentVar.str()));
}
if (playerNames.size() < 10 || playerNames.find("") != playerNames.end()) {
// The set contains less than 10 players or an empty player name.
// Reset to default player names.
writeConfigString("MyName", "Human Player");
for(int i = 1; i <= 9; i++) {
ostringstream opponentVar;
ostringstream opponentName;
opponentVar << "Opponent" << i << "Name";
opponentName << "Player " << i;
writeConfigString(opponentVar.str(), opponentName.str());
}
}
}
void ConfigFile::writeBuffer() const
{
@@ -527,7 +560,7 @@ void ConfigFile::updateConfig(ConfigState myConfigState)
//test
bool contains = noUpdateElemtsList.find(value) != mylist.end();
//bool contains = noUpdateElemtsList.find(value) != mylist.end();
/////// HIER GEHTS WEITER LOTHAR ;) ///////
+4
View File
@@ -36,6 +36,7 @@ public:
~ConfigFile();
void fillBuffer();
void checkAndCorrectBuffer();
void writeBuffer() const;
void updateConfig(ConfigState);
@@ -51,6 +52,9 @@ public:
void writeConfigIntList(std::string varName, std::list<int> varCont);
void deleteConfigFile();
protected:
void checkAndCorrectPlayerNames();
private:
mutable boost::recursive_mutex m_configMutex;