clarified highscore submission when internet connection fails

main
Martin Felis (berta) 2011-03-16 23:21:22 +01:00
parent 21239c9962
commit e2fd3393d3
4 changed files with 53 additions and 43 deletions

View File

@ -42,25 +42,6 @@ int Model::OnInit (int argc, char* argv[]) {
if (InitLevelList() == 0) if (InitLevelList() == 0)
Engine::LogError ("No levels found!"); Engine::LogError ("No levels found!");
// First we reset the highscore list
mHighscoreList.clear();
// then we try to load values from the file
// LoadHighscoreList();
// if we have less than the usual number of entries we add default values
if (mHighscoreList.size() < 10) {
AddLocalHighscoreEntry ("Imperator", 1000000);
AddLocalHighscoreEntry ("Darth Vader", 800000);
AddLocalHighscoreEntry ("Luke Skywalker", 600000);
AddLocalHighscoreEntry ("Han Solo", 400000);
AddLocalHighscoreEntry ("Princess Leia", 200000);
AddLocalHighscoreEntry ("C3PO", 100000);
AddLocalHighscoreEntry ("R2-D2", 50000);
AddLocalHighscoreEntry ("Chewy", 10000);
AddLocalHighscoreEntry ("Mr. Ewok", 5000);
AddLocalHighscoreEntry ("Jabba the Hutt", 1000);
}
// Reset the newest highscore entry index which may be used for highlighting // Reset the newest highscore entry index which may be used for highlighting
// the newest entry. // the newest entry.
mNewestHighscoreEntryIndex = std::numeric_limits<unsigned int>::max(); mNewestHighscoreEntryIndex = std::numeric_limits<unsigned int>::max();
@ -79,8 +60,6 @@ int Model::OnInit (int argc, char* argv[]) {
Engine::LogError("SDLNet_Init: %s\n", SDLNet_GetError()); Engine::LogError("SDLNet_Init: %s\n", SDLNet_GetError());
} }
SubmitGlobalHigscoreEntry ("asteroids_full", 321321321);
Engine::RegisterListener (this, EventAccelerateStart); Engine::RegisterListener (this, EventAccelerateStart);
Engine::RegisterListener (this, EventAccelerateStop); Engine::RegisterListener (this, EventAccelerateStop);
@ -196,34 +175,53 @@ void Model::ParseHighscoreStream (std::istream &highscore_stream) {
void Model::LoadHighscoreList () { void Model::LoadHighscoreList () {
mHighscoreList.clear(); mHighscoreList.clear();
if (mHighscoreList.size() < 10) {
AddLocalHighscoreEntry ("Imperator", 1000000);
AddLocalHighscoreEntry ("Darth Vader", 800000);
AddLocalHighscoreEntry ("Luke Skywalker", 600000);
AddLocalHighscoreEntry ("Han Solo", 400000);
AddLocalHighscoreEntry ("Princess Leia", 200000);
AddLocalHighscoreEntry ("C3PO", 100000);
AddLocalHighscoreEntry ("R2-D2", 50000);
AddLocalHighscoreEntry ("Chewy", 10000);
AddLocalHighscoreEntry ("Mr. Ewok", 5000);
AddLocalHighscoreEntry ("Jabba the Hutt", 1000);
}
if (Model::UseServerHighscore.GetBoolValue()) { if (Model::UseServerHighscore.GetBoolValue()) {
Engine::LogMessage ("Retrieving Highscore from server"); Engine::LogDebug ("Retrieving Highscore from server");
std::stringstream global_highscore_stream; std::stringstream global_highscore_stream;
if (PullGlobalHighscore(global_highscore_stream)) { if (PullGlobalHighscore(global_highscore_stream)) {
ParseHighscoreStream(global_highscore_stream); ParseHighscoreStream(global_highscore_stream);
return; return;
} }
Engine::LogMessage ("Could not load highscore from server, falling back to local.");
} }
LoadLocalHighscoreList(); LoadLocalHighscoreList();
SaveLocalHighscoreList();
} }
void Model::LoadLocalHighscoreList () { void Model::LoadLocalHighscoreList () {
Engine::LogDebug ("Loading local highscore file"); std::string highscore_filename = Engine::GetUserDirFullPath("/highscore.dat").c_str();
boost::filesystem::path highscore_file(Engine::GetUserDirFullPath("/highscore.dat")); boost::filesystem::path highscore_file(highscore_filename);
// if the file does not exist, we create it and write standard values into // if the file does not exist, we create it and write standard values into
// it. // it.
if (!boost::filesystem::exists(highscore_file)) if (!boost::filesystem::exists(highscore_file)) {
Engine::LogDebug ("Local highscore file not found!");
return; return;
}
if (!boost::filesystem::is_regular_file(highscore_file)) { if (!boost::filesystem::is_regular_file(highscore_file)) {
Engine::LogError ("Could not load highscore file: %s is not a regular file!", highscore_file.filename().c_str()); Engine::LogError ("Could not load highscore file: %s is not a regular file!", highscore_file.filename().c_str());
} }
std::ifstream score_stream (highscore_file.filename().c_str()); Engine::LogDebug ("Loading highscore file '%s'", highscore_filename.c_str());
std::ifstream score_stream (highscore_filename.c_str());
ParseHighscoreStream (score_stream); ParseHighscoreStream (score_stream);
score_stream.close(); score_stream.close();
@ -256,9 +254,14 @@ unsigned int Model::AddLocalHighscoreEntry(const std::string &name, const unsign
entry.name = name; entry.name = name;
entry.points = points; entry.points = points;
Engine::LogDebug ("Adding entry %s points = %d", name.c_str(), points);
unsigned int counter = 0; unsigned int counter = 0;
std::list<HighscoreEntry>::iterator iter = mHighscoreList.begin(); std::list<HighscoreEntry>::iterator iter = mHighscoreList.begin();
while (iter != mHighscoreList.end()) { while (iter != mHighscoreList.end()) {
if (name == iter->name && points == iter->points)
break;
if (points >= iter->points) { if (points >= iter->points) {
mHighscoreList.insert (iter, entry); mHighscoreList.insert (iter, entry);
mNewestHighscoreEntryIndex = counter; mNewestHighscoreEntryIndex = counter;
@ -281,10 +284,6 @@ unsigned int Model::AddLocalHighscoreEntry(const std::string &name, const unsign
if (counter < 10) if (counter < 10)
return counter; return counter;
// if we have all 10 entries then we can save
// the highscore
SaveLocalHighscoreList();
mNewestHighscoreEntryIndex = 99999; mNewestHighscoreEntryIndex = 99999;
return 99999; return 99999;
@ -296,7 +295,8 @@ bool Model::PullGlobalHighscore(std::stringstream &highscore_stream) {
IPaddress server_address; IPaddress server_address;
if (SDLNet_ResolveHost (&server_address, Model::HighscoreServerName.GetStringValue().c_str(), 80) == -1) { if (SDLNet_ResolveHost (&server_address, Model::HighscoreServerName.GetStringValue().c_str(), 80) == -1) {
Engine::LogWarning ("SDL_net resolve host: %s", SDLNet_GetError()); Engine::LogWarning ("Could not retrieve global highscore list: host name resolution for server %s failed!",
Model::HighscoreServerName.GetStringValue().c_str());
return false; return false;
} }
@ -370,7 +370,8 @@ bool Model::SubmitGlobalHigscoreEntry (const std::string &name, const unsigned i
IPaddress server_address; IPaddress server_address;
if (SDLNet_ResolveHost (&server_address, Model::HighscoreServerName.GetStringValue().c_str(), 80) == -1) { if (SDLNet_ResolveHost (&server_address, Model::HighscoreServerName.GetStringValue().c_str(), 80) == -1) {
Engine::LogWarning ("SDL_net resolve host: %s", SDLNet_GetError()); Engine::LogWarning ("Cannot submit highscore entry: host name resolution for server %s failed!",
Model::HighscoreServerName.GetStringValue().c_str());
return false; return false;
} }
@ -461,12 +462,17 @@ bool Model::SubmitGlobalHigscoreEntry (const std::string &name, const unsigned i
void Model::SubmitHighscoreEntry (const std::string &name, const unsigned int points) { void Model::SubmitHighscoreEntry (const std::string &name, const unsigned int points) {
if (Model::UseServerHighscore.GetBoolValue()) { if (Model::UseServerHighscore.GetBoolValue()) {
Engine::LogMessage ("Sending highscore entry to the server"); if (SubmitGlobalHigscoreEntry (name, points)) {
return;
SubmitGlobalHigscoreEntry (name, points);
} else {
AddLocalHighscoreEntry (name, points);
} }
Engine::LogWarning ("Could not send highscore entry to server!");
}
// if it did not succeed or we do not want to use the global highscore
// fall back to the local highscore system.
AddLocalHighscoreEntry (name, points);
SaveLocalHighscoreList();
} }
int Model::DoLoadLevel (const char* filename) { int Model::DoLoadLevel (const char* filename) {

View File

@ -496,6 +496,7 @@ void View::DrawUiMainMenu() {
} }
if (Engine::GUI::Button (3, "Highscores", screen_right * 0.5 - 100, 300, button_width, button_height)) { if (Engine::GUI::Button (3, "Highscores", screen_right * 0.5 - 100, 300, button_width, button_height)) {
GetModel()->LoadHighscoreList();
PushViewState(ViewStateShowHighscore); PushViewState(ViewStateShowHighscore);
} }

View File

@ -114,7 +114,7 @@ int main (int argc, char* argv[]) {
engine.SetPhysics (new asteroids::Physics); engine.SetPhysics (new asteroids::Physics);
engine.SetView (new asteroids::View); engine.SetView (new asteroids::View);
SetLogPrintLevel (Engine::LogLevelMessage); SetLogPrintLevel (Engine::LogLevelWarning);
std::string user_path = "."; std::string user_path = ".";
std::string game_data_path = "."; std::string game_data_path = ".";
@ -130,6 +130,7 @@ int main (int argc, char* argv[]) {
cout << "User Data Dir = " << user_path << endl; cout << "User Data Dir = " << user_path << endl;
engine.SetUserDataPath (user_path); engine.SetUserDataPath (user_path);
Engine::SetLogFilename (log_file_path.c_str()); Engine::SetLogFilename (log_file_path.c_str());
engine.SetGameDataPath (game_data_path); engine.SetGameDataPath (game_data_path);
@ -165,6 +166,7 @@ int main (int argc, char* argv[]) {
Engine::PlayMusic (Engine::GetResourceFullPath("/data/sounds/intro_music.ogg")); Engine::PlayMusic (Engine::GetResourceFullPath("/data/sounds/intro_music.ogg"));
// load highscore list (either remote or local, depending on settings)
asteroids::GetModel()->LoadHighscoreList(); asteroids::GetModel()->LoadHighscoreList();
engine.MainLoop (); engine.MainLoop ();

View File

@ -100,11 +100,12 @@ bool Variable::ParseBoolValue (std::string value) {
unsigned int i; unsigned int i;
for (i = 0; i < value.size(); i++) for (i = 0; i < value.size(); i++)
value[i] = tolower (value[i]); value[i] = tolower (value[i]);
// std::transform (value.begin(), value.end(), value.begin(), ::tolower);
if (value == "true" if (value == "true"
|| value == "yes") || value == "yes")
return true; return true;
return false;
} }
/* /*