diff --git a/asteroids/Model.cc b/asteroids/Model.cc index c364d8b..17e54d3 100644 --- a/asteroids/Model.cc +++ b/asteroids/Model.cc @@ -42,25 +42,6 @@ int Model::OnInit (int argc, char* argv[]) { if (InitLevelList() == 0) 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 // the newest entry. mNewestHighscoreEntryIndex = std::numeric_limits::max(); @@ -79,8 +60,6 @@ int Model::OnInit (int argc, char* argv[]) { Engine::LogError("SDLNet_Init: %s\n", SDLNet_GetError()); } - SubmitGlobalHigscoreEntry ("asteroids_full", 321321321); - Engine::RegisterListener (this, EventAccelerateStart); Engine::RegisterListener (this, EventAccelerateStop); @@ -196,34 +175,53 @@ void Model::ParseHighscoreStream (std::istream &highscore_stream) { void Model::LoadHighscoreList () { 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()) { - Engine::LogMessage ("Retrieving Highscore from server"); + Engine::LogDebug ("Retrieving Highscore from server"); std::stringstream global_highscore_stream; if (PullGlobalHighscore(global_highscore_stream)) { ParseHighscoreStream(global_highscore_stream); return; } + + Engine::LogMessage ("Could not load highscore from server, falling back to local."); } LoadLocalHighscoreList(); + SaveLocalHighscoreList(); } void Model::LoadLocalHighscoreList () { - Engine::LogDebug ("Loading local highscore file"); - boost::filesystem::path highscore_file(Engine::GetUserDirFullPath("/highscore.dat")); + std::string highscore_filename = Engine::GetUserDirFullPath("/highscore.dat").c_str(); + boost::filesystem::path highscore_file(highscore_filename); // if the file does not exist, we create it and write standard values into // it. - if (!boost::filesystem::exists(highscore_file)) - return; - - 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()); + if (!boost::filesystem::exists(highscore_file)) { + Engine::LogDebug ("Local highscore file not found!"); + return; } - std::ifstream score_stream (highscore_file.filename().c_str()); + 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::LogDebug ("Loading highscore file '%s'", highscore_filename.c_str()); + + std::ifstream score_stream (highscore_filename.c_str()); ParseHighscoreStream (score_stream); score_stream.close(); @@ -256,9 +254,14 @@ unsigned int Model::AddLocalHighscoreEntry(const std::string &name, const unsign entry.name = name; entry.points = points; + Engine::LogDebug ("Adding entry %s points = %d", name.c_str(), points); + unsigned int counter = 0; std::list::iterator iter = mHighscoreList.begin(); while (iter != mHighscoreList.end()) { + if (name == iter->name && points == iter->points) + break; + if (points >= iter->points) { mHighscoreList.insert (iter, entry); mNewestHighscoreEntryIndex = counter; @@ -281,10 +284,6 @@ unsigned int Model::AddLocalHighscoreEntry(const std::string &name, const unsign if (counter < 10) return counter; - // if we have all 10 entries then we can save - // the highscore - SaveLocalHighscoreList(); - mNewestHighscoreEntryIndex = 99999; return 99999; @@ -296,7 +295,8 @@ bool Model::PullGlobalHighscore(std::stringstream &highscore_stream) { IPaddress server_address; 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; } @@ -370,7 +370,8 @@ bool Model::SubmitGlobalHigscoreEntry (const std::string &name, const unsigned i IPaddress server_address; 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; } @@ -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) { 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) { diff --git a/asteroids/View.cc b/asteroids/View.cc index 8fddba9..c24df88 100644 --- a/asteroids/View.cc +++ b/asteroids/View.cc @@ -496,6 +496,7 @@ void View::DrawUiMainMenu() { } if (Engine::GUI::Button (3, "Highscores", screen_right * 0.5 - 100, 300, button_width, button_height)) { + GetModel()->LoadHighscoreList(); PushViewState(ViewStateShowHighscore); } diff --git a/asteroids/main.cc b/asteroids/main.cc index b46f6ed..f32f558 100644 --- a/asteroids/main.cc +++ b/asteroids/main.cc @@ -114,7 +114,7 @@ int main (int argc, char* argv[]) { engine.SetPhysics (new asteroids::Physics); engine.SetView (new asteroids::View); - SetLogPrintLevel (Engine::LogLevelMessage); + SetLogPrintLevel (Engine::LogLevelWarning); std::string user_path = "."; std::string game_data_path = "."; @@ -130,6 +130,7 @@ int main (int argc, char* argv[]) { cout << "User Data Dir = " << user_path << endl; engine.SetUserDataPath (user_path); + Engine::SetLogFilename (log_file_path.c_str()); engine.SetGameDataPath (game_data_path); @@ -165,6 +166,7 @@ int main (int argc, char* argv[]) { Engine::PlayMusic (Engine::GetResourceFullPath("/data/sounds/intro_music.ogg")); + // load highscore list (either remote or local, depending on settings) asteroids::GetModel()->LoadHighscoreList(); engine.MainLoop (); diff --git a/engine/Variables.cc b/engine/Variables.cc index 15f790a..6e36ced 100644 --- a/engine/Variables.cc +++ b/engine/Variables.cc @@ -100,11 +100,12 @@ bool Variable::ParseBoolValue (std::string value) { unsigned int i; for (i = 0; i < value.size(); i++) value[i] = tolower (value[i]); -// std::transform (value.begin(), value.end(), value.begin(), ::tolower); if (value == "true" || value == "yes") return true; + + return false; } /*