From c9cb74a9e5a51bd5c81305df78bef8e7dee4cf32 Mon Sep 17 00:00:00 2001 From: hilnius Date: Sat, 21 Sep 2013 18:18:50 +0000 Subject: [PATCH] fixing bugs, adding comments, structures and functions for the map vote system git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@14122 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- data/gui/karts_online.stkgui | 29 +++ sources.cmake | 2 + src/network/game_setup.cpp | 3 +- src/network/game_setup.hpp | 4 + src/network/protocols/lobby_room_protocol.hpp | 6 - src/network/race_config.cpp | 169 ++++++++++++++++++ src/network/race_config.hpp | 58 ++++++ 7 files changed, 264 insertions(+), 7 deletions(-) create mode 100644 data/gui/karts_online.stkgui create mode 100644 src/network/race_config.cpp create mode 100644 src/network/race_config.hpp diff --git a/data/gui/karts_online.stkgui b/data/gui/karts_online.stkgui new file mode 100644 index 000000000..0fc910355 --- /dev/null +++ b/data/gui/karts_online.stkgui @@ -0,0 +1,29 @@ + + +
+ +
+ + + + + + + + + + + + + + + +
+ + + +
\ No newline at end of file diff --git a/sources.cmake b/sources.cmake index 0032a92ad..8d098ca24 100644 --- a/sources.cmake +++ b/sources.cmake @@ -170,6 +170,7 @@ src/network/protocols/start_game_protocol.cpp src/network/protocols/start_server.cpp src/network/protocols/stop_server.cpp src/network/protocols/synchronization_protocol.cpp +src/network/race_config.cpp src/network/server_network_manager.cpp src/network/stk_host.cpp src/network/stk_peer.cpp @@ -475,6 +476,7 @@ src/network/protocols/start_game_protocol.hpp src/network/protocols/start_server.hpp src/network/protocols/stop_server.hpp src/network/protocols/synchronization_protocol.hpp +src/network/race_config.hpp src/network/remote_kart_info.hpp src/network/server_network_manager.hpp src/network/singleton.hpp diff --git a/src/network/game_setup.cpp b/src/network/game_setup.cpp index c67929c12..59d64ba9f 100644 --- a/src/network/game_setup.cpp +++ b/src/network/game_setup.cpp @@ -20,13 +20,14 @@ #include "karts/abstract_kart.hpp" #include "modes/world.hpp" +#include "race/race_manager.hpp" #include "utils/log.hpp" - //----------------------------------------------------------------------------- GameSetup::GameSetup() { + m_race_config = new RaceConfig(); } //----------------------------------------------------------------------------- diff --git a/src/network/game_setup.hpp b/src/network/game_setup.hpp index f9816c390..420c8290d 100644 --- a/src/network/game_setup.hpp +++ b/src/network/game_setup.hpp @@ -23,6 +23,7 @@ #define GAME_SETUP_HPP #include "online/profile.hpp" +#include "network/race_config.hpp" #include #include @@ -89,9 +90,12 @@ class GameSetup */ bool isKartAllowed(std::string kart_name) {return true; } + RaceConfig* getRaceConfig() { return m_race_config; } + protected: std::vector m_players; //!< Information about players NetworkPlayerProfile m_self_profile; //!< Information about self (client only) + RaceConfig* m_race_config; }; #endif // GAME_SETUP_HPP diff --git a/src/network/protocols/lobby_room_protocol.hpp b/src/network/protocols/lobby_room_protocol.hpp index e288eaaaf..d132359dd 100644 --- a/src/network/protocols/lobby_room_protocol.hpp +++ b/src/network/protocols/lobby_room_protocol.hpp @@ -24,12 +24,6 @@ #include "network/game_setup.hpp" #include "network/network_string.hpp" - -class RaceConfig -{ - int m_world_type; -}; - /*! * \class LobbyRoomProtocol * \brief Class used while the game is being prepared. diff --git a/src/network/race_config.cpp b/src/network/race_config.cpp new file mode 100644 index 000000000..fa8db7da3 --- /dev/null +++ b/src/network/race_config.cpp @@ -0,0 +1,169 @@ +#include "network/race_config.hpp" + +#include "race/race_manager.hpp" + +template +S getHighestInHistogram(std::map* histogram) +{ + S best_item; + int highest_count; + for (typename std::map::iterator it = histogram->begin(); + it != histogram->end(); it++) + { + if (it->second > highest_count) + { + highest_count = it->second; + best_item = it->first; + } + } + return best_item; +} + +//----------------------------------------------------------------------------- +TrackVote::TrackVote() +{ + has_voted_laps = false; + has_voted_track = false; + has_voted_reversed = false; +} +//----------------------------------------------------------------------------- +void TrackVote::voteTrack(std::string track) +{ + track_info.track = track; + has_voted_track = true; +} +//----------------------------------------------------------------------------- +void TrackVote::voteReversed(bool reversed) +{ + track_info.reversed = reversed; + has_voted_reversed = true; +} +//----------------------------------------------------------------------------- +void TrackVote::voteLaps(int laps) +{ + track_info.laps = laps; + has_voted_laps = true; +} +//----------------------------------------------------------------------------- + +int GPRaceInfo::getMajorMode() +{ + return RaceManager::MAJOR_MODE_GRAND_PRIX; +} + +//----------------------------------------------------------------------------- + +int SingleRaceInfo::getMajorMode() +{ + return RaceManager::MAJOR_MODE_SINGLE; +} + +//----------------------------------------------------------------------------- + +RaceConfig::RaceConfig() +{ + m_max_players = 0; +} + +//----------------------------------------------------------------------------- + +void RaceConfig::setPlayerCount(int count) +{ + m_max_players = count; + m_votes.resize(m_max_players); +} + +//----------------------------------------------------------------------------- + +void RaceConfig::setPlayerTrackVote(int player_id, std::string track_name) +{ + m_votes[player_id].voteTrack(track_name); +} + +//----------------------------------------------------------------------------- + +void RaceConfig::setPlayerReverseVote(int player_id, bool reversed) +{ + m_votes[player_id].voteReversed(reversed); +} + +//----------------------------------------------------------------------------- + +void RaceConfig::setPlayerLapVote(int player_id, int lap_count) +{ + m_votes[player_id].voteLaps(lap_count); +} + +//----------------------------------------------------------------------------- + +void RaceConfig::computeNextTrack() +{ + // first create histograms of the votes + std::map tracks_histogram; + std::map reversed_histogram; + std::map laps_histogram; + for (unsigned int i = 0; i < m_max_players; i++) + { + // increase the count of votes + if (m_votes[i].has_voted_track) + { + try // maps + { + tracks_histogram.at(m_votes[i].track_info.track) ++; + } + catch (const std::out_of_range& oor) // doesn't exist in the map : add it + { + tracks_histogram[m_votes[i].track_info.track] = 1; + } + } + else if (m_votes[i].has_voted_reversed) + { + try // reversed + { + reversed_histogram.at(m_votes[i].track_info.reversed) ++; + } + catch (const std::out_of_range& oor) // doesn't exist in the map : add it + { + reversed_histogram[m_votes[i].track_info.reversed] = 1; + } + } + else if (m_votes[i].has_voted_laps) + { + try // laps + { + laps_histogram.at(m_votes[i].track_info.laps) ++; + } + catch (const std::out_of_range& oor) // doesn't exist in the map : add it + { + laps_histogram[m_votes[i].track_info.laps] = 1; + } + } + } + // now find the highest votes + m_track.track = getHighestInHistogram(&tracks_histogram); + m_track.reversed = getHighestInHistogram(&reversed_histogram); + m_track.laps = getHighestInHistogram(&laps_histogram); +} + +//----------------------------------------------------------------------------- + +const TrackInfo* RaceConfig::getNextTrack() const +{ + return m_track.track; +} + +//----------------------------------------------------------------------------- + +bool RaceConfig::getReverse() const +{ + return m_track.track; +} + +//----------------------------------------------------------------------------- + +bool RaceConfig::getLapCount() const +{ + return m_track.track; +} + +//----------------------------------------------------------------------------- diff --git a/src/network/race_config.hpp b/src/network/race_config.hpp new file mode 100644 index 000000000..86182c4ba --- /dev/null +++ b/src/network/race_config.hpp @@ -0,0 +1,58 @@ +#ifndef RACE_CONFIG_HPP +#define RACE_CONFIG_HPP + +#include +#include + +class TrackInfo +{ + public: + std::string track; + bool reversed; + int laps; +}; +class TrackVote +{ + public: + TrackVote(); + + void voteTrack(std::string track); + void voteReversed(bool reversed); + void voteLaps(int laps); + + TrackInfo track_info; + + bool has_voted_track; + bool has_voted_reversed; + bool has_voted_laps; + + int minor_race_type; // corresponds to the enum in race_manager.hpp +}; + +class RaceConfig +{ + public: + RaceConfig(); + + void setPlayerCount(int count); + void setPlayerTrackVote(int player_id, std::string track_name); + void setPlayerReverseVote(int player_id, bool reversed); + void setPlayerLapVote(int player_id, int lap_count); + + void computeNextTrack(); + + const TrackInfo* getNextTrack() const; + bool getReverse() const; + bool getLapCount() const; + + protected: + TrackInfo m_track; + bool m_reverse; + int m_laps; + + std::vector m_votes; + int m_max_players; +}; + + +#endif // RACE_CONFIG_HPP