boost::statechart::result WaitingForMPJoinAck::react(const JoinGame& msg) { TraceLogger(FSM) << "(HumanClientFSM) WaitingForMPJoinAck.JoinGame"; try { int player_id; boost::uuids::uuid cookie; ExtractJoinAckMessageData(msg.m_message, player_id, cookie); if (!cookie.is_nil()) { try { std::string cookie_option = "network.server.cookie." + Client().Networking().Destination(); GetOptionsDB().Remove(cookie_option); GetOptionsDB().Add(cookie_option, "OPTIONS_DB_SERVER_COOKIE", boost::uuids::to_string(cookie)); GetOptionsDB().Commit(); } catch(const std::exception& err) { WarnLogger() << "Cann't save cookie for server " << Client().Networking().Destination() << ": " << err.what(); // ignore } } Client().Networking().SetPlayerID(player_id); return transit<MPLobby>(); } catch (const boost::bad_lexical_cast& ex) { ErrorLogger(FSM) << "WaitingForMPJoinAck::react(const JoinGame& msg) Host id " << msg.m_message.Text() << " is not a number: " << ex.what(); return transit<IntroMenu>(); } }
boost::statechart::result QuittingGame::react(const ShutdownServer& u) { TraceLogger(FSM) << "(HumanClientFSM) QuittingGame.ShutdownServer"; if (!m_server_process) { ErrorLogger(FSM) << "m_server_process is nullptr"; post_event(TerminateServer()); return discard_event(); } if (m_server_process->Empty()) { if (Client().Networking().IsTxConnected()) { WarnLogger(FSM) << "Disconnecting from server that is already killed."; Client().Networking().DisconnectFromServer(); } post_event(TerminateServer()); return discard_event(); } if (Client().Networking().IsTxConnected()) { DebugLogger(FSM) << "Sending server shutdown message."; Client().Networking().SendMessage(ShutdownServerMessage()); post_event(WaitForDisconnect()); } else { post_event(TerminateServer()); } return discard_event(); }
void ClientApp::VerifyCheckSum(const Message& msg) { std::map<std::string, unsigned int> server_checksums; ExtractContentCheckSumMessageData(msg, server_checksums); auto client_checksums = CheckSumContent(); if (server_checksums == client_checksums) { InfoLogger() << "Checksum received from server matches client checksum."; } else { WarnLogger() << "Checksum received from server does not match client checksum."; for (const auto& name_and_checksum : server_checksums) { const auto& name = name_and_checksum.first; const auto client_checksum = client_checksums[name]; if (client_checksum != name_and_checksum.second) WarnLogger() << "Checksum for " << name << " on server " << name_and_checksum.second << " != client " << client_checksum; } } }
start_rule_payload statistics(const boost::filesystem::path& path) { const lexer lexer; start_rule_payload all_stats; for (const boost::filesystem::path& file : ListScripts(path)) { start_rule_payload stats_; if (/*auto success =*/ detail::parse_file<grammar, start_rule_payload>(lexer, file, stats_)) { for (auto&& stat : stats_) { auto maybe_inserted = all_stats.emplace(stat.first, std::move(stat.second)); if (!maybe_inserted.second) { WarnLogger() << "Addition of second statistic with name " << maybe_inserted.first->first << " failed. Keeping first statistic found."; } } } } return all_stats; }
std::string Planet::CardinalSuffix() const { std::string retval = ""; // Early return for invalid ID if (ID() == INVALID_OBJECT_ID) { WarnLogger() << "Planet " << Name() << " has invalid ID"; return retval; } auto cur_system = GetSystem(SystemID()); // Early return for no system if (!cur_system) { ErrorLogger() << "Planet " << Name() << "(" << ID() << ") not assigned to a system"; return retval; } // Early return for unknown orbit if (cur_system->OrbitOfPlanet(ID()) < 0) { WarnLogger() << "Planet " << Name() << "(" << ID() << ") " << "has no current orbit"; retval.append(RomanNumber(1)); return retval; } int num_planets_lteq = 0; // number of planets at this orbit or smaller int num_planets_total = 0; bool prior_current_planet = true; for (int sys_orbit : cur_system->PlanetIDsByOrbit()) { if (sys_orbit == INVALID_OBJECT_ID) continue; // all other planets are in further orbits if (sys_orbit == ID()) { prior_current_planet = false; ++num_planets_total; ++num_planets_lteq; continue; } PlanetType other_planet_type = GetPlanet(sys_orbit)->Type(); if (other_planet_type == INVALID_PLANET_TYPE) continue; // only increment suffix for non-asteroid planets if (Type() != PT_ASTEROIDS) { if (other_planet_type != PT_ASTEROIDS) { ++num_planets_total; if (prior_current_planet) ++num_planets_lteq; } } else { // unless the planet being named is an asteroid // then only increment suffix for asteroid planets if (other_planet_type == PT_ASTEROIDS) { ++num_planets_total; if (prior_current_planet) ++num_planets_lteq; } } } // Planets are grouped into asteroids, and non-asteroids if (Type() != PT_ASTEROIDS) { retval.append(RomanNumber(num_planets_lteq)); } else { // Asteroids receive a localized prefix retval.append(UserString("NEW_ASTEROIDS_SUFFIX")); // If no other asteroids in this system, do not append an ordinal if (num_planets_total > 1) retval.append(" " + RomanNumber(num_planets_lteq)); } return retval; }
void OptionsDB::SetFromCommandLine(const std::vector<std::string>& args) { //bool option_changed = false; for (unsigned int i = 1; i < args.size(); ++i) { std::string current_token(args[i]); if (current_token.find("--") == 0) { std::string option_name = current_token.substr(2); std::map<std::string, Option>::iterator it = m_options.find(option_name); if (it == m_options.end() || !it->second.recognized) { // unrecognized option: may be registered later on so we'll store it for now // Check for more parameters (if this is the last one, assume that it is a flag). std::string value_str("-"); if (i + 1 < static_cast<unsigned int>(args.size())) { value_str = args[i + 1]; // copy assignment StripQuotation(value_str); } if (value_str.at(0) == '-') { // this is either the last parameter or the next parameter is another option, assume this one is a flag m_options[option_name] = Option(static_cast<char>(0), option_name, true, boost::lexical_cast<std::string>(false), "", 0, false, true, false); } else { // the next parameter is the value, store it as a string to be parsed later m_options[option_name] = Option(static_cast<char>(0), option_name, value_str, value_str, "", new Validator<std::string>(), false, false, false); // don't attempt to store options that have only been specified on the command line } WarnLogger() << "Option \"" << option_name << "\", was specified on the command line but was not recognized. It may not be registered yet or could be a typo."; } else { Option& option = it->second; if (option.value.empty()) throw std::runtime_error("The value member of option \"--" + option.name + "\" is undefined."); if (!option.flag) { // non-flag try { // ensure a parameter exists... if (i + 1 >= static_cast<unsigned int>(args.size())) throw std::runtime_error("the option \"" + option.name + "\" was specified, at the end of the list, with no parameter value."); // get parameter value std::string value_str(args[++i]); StripQuotation(value_str); // ensure parameter is actually a parameter, and not the next option name (which would indicate // that the option was specified without a parameter value, as if it was a flag) if (value_str.at(0) == '-') throw std::runtime_error("the option \"" + option.name + "\" was followed by the parameter \"" + value_str + "\", which appears to be an option flag, not a parameter value, because it begins with a \"-\" character."); m_dirty |= option.SetFromString(value_str); } catch (const std::exception& e) { throw std::runtime_error("OptionsDB::SetFromCommandLine() : the following exception was caught when attempting to set option \"" + option.name + "\": " + e.what() + "\n\n"); } } else { // flag option.value = true; } } //option_changed = true; } else if (current_token.find('-') == 0 #ifdef FREEORION_MACOSX && current_token.find("-psn") != 0 // Mac OS X passes a process serial number to all applications using Carbon or Cocoa, it should be ignored here #endif ) { std::string single_char_options = current_token.substr(1); if (single_char_options.size() == 0) { throw std::runtime_error("A \'-\' was given with no options."); } else { for (unsigned int j = 0; j < single_char_options.size(); ++j) { std::map<char, std::string>::iterator short_name_it = Option::short_names.find(single_char_options[j]); if (short_name_it == Option::short_names.end()) throw std::runtime_error(std::string("Unknown option \"-") + single_char_options[j] + "\" was given."); std::map<std::string, Option>::iterator name_it = m_options.find(short_name_it->second); if (name_it == m_options.end()) throw std::runtime_error("Option \"--" + short_name_it->second + "\", abbreviated as \"-" + short_name_it->first + "\", could not be found."); Option& option = name_it->second; if (option.value.empty()) throw std::runtime_error("The value member of option \"--" + option.name + "\" is undefined."); if (!option.flag) { if (j < single_char_options.size() - 1) throw std::runtime_error(std::string("Option \"-") + single_char_options[j] + "\" was given with no parameter."); else m_dirty |= option.SetFromString(args[++i]); } else { option.value = true; } } } } } }