From e28fbdd01918aefb4e0fbd49c417491e05aec537 Mon Sep 17 00:00:00 2001 From: Benau Date: Fri, 11 May 2018 10:37:31 +0800 Subject: [PATCH] Increase vote timeout to 20 second, and offer a way to end vote early After half voting time and all entries in voting table has > 50% agreement out of current connected peers, go on --- src/config/user_config.hpp | 2 +- src/network/protocols/server_lobby.cpp | 41 ++++++++++++++++++++++---- src/network/protocols/server_lobby.hpp | 2 +- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/config/user_config.hpp b/src/config/user_config.hpp index 727bb6ab6..8e8c09a39 100644 --- a/src/config/user_config.hpp +++ b/src/config/user_config.hpp @@ -715,7 +715,7 @@ namespace UserConfigParams &m_network_group, "Enable chatting in networking lobby, if off than " "no chat message will be displayed from any players.")); PARAM_PREFIX FloatUserConfigParam m_voting_timeout - PARAM_DEFAULT(FloatUserConfigParam(10.0f, "voting-timeout", + PARAM_DEFAULT(FloatUserConfigParam(20.0f, "voting-timeout", &m_network_group, "Timeout in seconds for voting tracks in server.")); PARAM_PREFIX IntUserConfigParam m_server_max_players PARAM_DEFAULT(IntUserConfigParam(12, "server_max_players", diff --git a/src/network/protocols/server_lobby.cpp b/src/network/protocols/server_lobby.cpp index 285eea670..2b4e7282e 100644 --- a/src/network/protocols/server_lobby.cpp +++ b/src/network/protocols/server_lobby.cpp @@ -330,10 +330,16 @@ void ServerLobby::asynchronousUpdate() } break; case SELECTING: - if (m_timeout.load() < (float)StkTime::getRealTime()) + { + auto result = handleVote(); + if (m_timeout.load() < (float)StkTime::getRealTime() || + (std::get<3>(result) && + m_timeout.load() - (UserConfigParams::m_voting_timeout / 2.0f) < + (float)StkTime::getRealTime())) { + m_game_setup->setRace(std::get<0>(result), std::get<1>(result), + std::get<2>(result)); std::lock_guard lock(m_connection_mutex); - auto result = handleVote(); // Remove disconnected player (if any) one last time m_game_setup->update(true); m_game_setup->sortPlayersForGrandPrix(); @@ -370,6 +376,7 @@ void ServerLobby::asynchronousUpdate() delete load_world; } break; + } default: break; } @@ -1183,7 +1190,7 @@ void ServerLobby::playerVote(Event* event) } // playerVote // ---------------------------------------------------------------------------- -std::tuple ServerLobby::handleVote() +std::tuple ServerLobby::handleVote() { // Default settings if no votes at all RandomGenerator rg; @@ -1196,6 +1203,20 @@ std::tuple ServerLobby::handleVote() std::map tracks; std::map laps; std::map reverses; + + float cur_players = 0.0f; + auto peers = STKHost::get()->getPeers(); + for (auto peer : peers) + { + if (peer->hasPlayerProfiles()) + cur_players += 1.0f; + } + if (cur_players == 0.0f) + return std::make_tuple(final_track, final_laps, final_reverse, false); + float tracks_rate = 0.0f; + float laps_rate = 0.0f; + float reverses_rate = 0.0f; + for (auto p : m_peers_votes) { if (p.first.expired()) @@ -1228,7 +1249,10 @@ std::tuple ServerLobby::handleVote() } } if (track_vote != tracks.end()) + { final_track = track_vote->first; + tracks_rate = float(track_vote->second) / cur_players; + } vote = 0; auto lap_vote = laps.begin(); @@ -1241,7 +1265,10 @@ std::tuple ServerLobby::handleVote() } } if (lap_vote != laps.end()) + { final_laps = lap_vote->first; + laps_rate = float(lap_vote->second) / cur_players; + } vote = 0; auto reverse_vote = reverses.begin(); @@ -1254,10 +1281,14 @@ std::tuple ServerLobby::handleVote() } } if (reverse_vote != reverses.end()) + { final_reverse = reverse_vote->first; + reverses_rate = float(reverse_vote->second) / cur_players; + } - m_game_setup->setRace(final_track, final_laps, final_reverse); - return std::make_tuple(final_track, final_laps, final_reverse); + return std::make_tuple(final_track, final_laps, final_reverse, + tracks_rate > 0.5f && laps_rate > 0.5f && reverses_rate > 0.5f ? + true : false); } // handleVote // ---------------------------------------------------------------------------- diff --git a/src/network/protocols/server_lobby.hpp b/src/network/protocols/server_lobby.hpp index 7594ee901..a31fea1a1 100644 --- a/src/network/protocols/server_lobby.hpp +++ b/src/network/protocols/server_lobby.hpp @@ -123,7 +123,7 @@ private: } } } - std::tuple handleVote(); + std::tuple handleVote(); void stopCurrentRace(); public: