Add current playing info to lobby

This commit is contained in:
Benau
2019-01-04 17:40:25 +08:00
parent 38043de27c
commit 4b22465836
10 changed files with 134 additions and 17 deletions

View File

@@ -34,6 +34,7 @@
#include "network/race_event_manager.hpp"
#include "race/race_manager.hpp"
#include "states_screens/state_manager.hpp"
#include "tracks/track_manager.hpp"
#include "utils/time.hpp"
std::weak_ptr<LobbyProtocol> LobbyProtocol::m_lobby;
@@ -44,6 +45,7 @@ LobbyProtocol::LobbyProtocol(CallbackObject* callback_object)
resetGameStartedProgress();
m_game_setup = new GameSetup();
m_end_voting_period.store(0);
m_current_track.store(-1);
} // LobbyProtocol
// ----------------------------------------------------------------------------
@@ -146,6 +148,7 @@ void LobbyProtocol::configRemoteKart(
*/
void LobbyProtocol::setup()
{
m_current_track.store(-1);
m_last_live_join_util_ticks = 0;
resetVotingTime();
m_peers_votes.clear();
@@ -228,3 +231,12 @@ bool LobbyProtocol::hasLiveJoiningRecently() const
w->getTicksSinceStart() - m_last_live_join_util_ticks > 0 &&
w->getTicksSinceStart() - m_last_live_join_util_ticks < 120;
} // hasLiveJoiningRecently
//-----------------------------------------------------------------------------
Track* LobbyProtocol::getPlayingTrack() const
{
int cur_idx = m_current_track.load();
if (cur_idx != -1)
return track_manager->getTrack(cur_idx);
return NULL;
} // getPlayingTrack

View File

@@ -25,6 +25,7 @@ class GameSetup;
class NetworkPlayerProfile;
class PeerVote;
class RemoteKartInfo;
class Track;
#include <atomic>
#include <cassert>
@@ -115,10 +116,13 @@ protected:
* uint32_t max if not available. */
std::atomic<uint32_t> m_estimated_progress;
// Save the last live join ticks, for physical objects to update current
// transformation in server, and reset smooth network body in client
/** Save the last live join ticks, for physical objects to update current
* transformation in server, and reset smooth network body in client. */
int m_last_live_join_util_ticks;
/** Store current playing track in id. */
std::atomic<int> m_current_track;
/** Stores data about the online game to play. */
GameSetup* m_game_setup;
@@ -211,6 +215,10 @@ public:
}
// ------------------------------------------------------------------------
bool hasLiveJoiningRecently() const;
// ------------------------------------------------------------------------
void storePlayingTrack(int track_id) { m_current_track.store(track_id); }
// ------------------------------------------------------------------------
Track* getPlayingTrack() const;
}; // class LobbyProtocol
#endif // LOBBY_PROTOCOL_HPP

View File

@@ -928,6 +928,15 @@ void ServerLobby::update(int ticks)
else
resetGameStartedProgress();
if (w && (w->getPhase() == World::RACE_PHASE ||
w->getPhase() == World::GOAL_PHASE))
{
storePlayingTrack(track_manager->getTrackIndexByIdent(
race_manager->getTrackName()));
}
else
storePlayingTrack(-1);
// Reset server to initial state if no more connected players
if (m_waiting_for_reset)
{
@@ -1461,6 +1470,9 @@ void ServerLobby::checkIncomingConnectionRequests()
STKHost::get()->getTotalPlayers());
request->addParameter("game-started",
m_state.load() == WAITING_FOR_START_GAME ? 0 : 1);
Track* current_track = getPlayingTrack();
if (current_track)
request->addParameter("current-track", current_track->getIdent());
request->queue();
} // checkIncomingConnectionRequests

View File

@@ -17,11 +17,12 @@
#include "network/server.hpp"
#include "config/player_manager.hpp"
#include "io/xml_node.hpp"
#include "online/online_player_profile.hpp"
#include "online/online_profile.hpp"
#include "online/profile_manager.hpp"
#include "network/network_config.hpp"
#include "io/xml_node.hpp"
#include "tracks/track_manager.hpp"
#include "utils/constants.hpp"
#include "utils/string_utils.hpp"
@@ -55,6 +56,7 @@ Server::Server(const XMLNode& server_info) : m_supports_encrytion(true)
xml.get("host_id", &m_server_owner);
xml.get("max_players", &m_max_players);
xml.get("current_players", &m_current_players);
xml.get("current_track", &m_current_track);
uint32_t ip;
xml.get("ip", &ip);
m_address.setIP(ip);
@@ -152,11 +154,12 @@ Server::Server(const XMLNode& server_info) : m_supports_encrytion(true)
* \param address IP and port of the server.
* \param password_protected True if can only be joined with a password.
* \param game_started True if there is already game begun in server.
* \param current_track If server is in game, store the track ident
*/
Server::Server(unsigned server_id, const core::stringw &name, int max_players,
int current_players, unsigned difficulty, unsigned server_mode,
const TransportAddress &address, bool password_protected,
bool game_started)
bool game_started, const std::string& current_track)
: m_supports_encrytion(false)
{
m_name = name;
@@ -174,4 +177,13 @@ Server::Server(unsigned server_id, const core::stringw &name, int max_players,
m_distance = 0.0f;
m_official = false;
m_game_started = game_started;
m_current_track = current_track;
} // server(server_id, ...)
// ----------------------------------------------------------------------------
Track* Server::getCurrentTrack() const
{
if (!m_current_track.empty())
return track_manager->getTrack(m_current_track);
return NULL;
}

View File

@@ -34,6 +34,7 @@
#include <string>
#include <tuple>
class Track;
class XMLNode;
/**
@@ -95,6 +96,7 @@ protected:
/*rank*/int, core::stringw, /*scores*/double, /*playing time*/float
> > m_players;
std::string m_current_track;
public:
/** Initialises the object from an XML node. */
@@ -102,7 +104,8 @@ public:
Server(unsigned server_id, const irr::core::stringw &name,
int max_players, int current_players, unsigned difficulty,
unsigned server_mode, const TransportAddress &address,
bool password_protected, bool game_started);
bool password_protected, bool game_started,
const std::string& current_track = "");
// ------------------------------------------------------------------------
/** Returns ip address and port of this server. */
const TransportAddress& getAddress() const { return m_address; }
@@ -169,5 +172,7 @@ public:
}
return server_name_found;
}
// ------------------------------------------------------------------------
Track* getCurrentTrack() const;
}; // Server
#endif // HEADER_SERVER_HPP

View File

@@ -194,10 +194,19 @@ Online::XMLRequest* ServersManager::getLANRefreshRequest() const
sender.setPort(port);
uint8_t password = s.getUInt8();
uint8_t game_started = s.getUInt8();
std::string current_track;
try
{
s.decodeString(&current_track);
}
catch (std::exception& e)
{
(void)e;
}
servers_now.insert(std::make_pair(name,
std::make_shared<Server>(cur_server_id++, name,
max_players, players, difficulty, mode, sender,
password == 1, game_started == 1)));
password == 1, game_started == 1, current_track)));
//all_servers.[name] = servers_now.back();
} // if received_data
} // while still waiting

View File

@@ -33,6 +33,8 @@
#include "network/protocol_manager.hpp"
#include "network/server_config.hpp"
#include "network/stk_peer.hpp"
#include "tracks/track.hpp"
#include "tracks/track_manager.hpp"
#include "utils/log.hpp"
#include "utils/separate_process.hpp"
#include "utils/time.hpp"
@@ -228,6 +230,7 @@ std::shared_ptr<LobbyProtocol> STKHost::create(SeparateProcess* p)
* 2. Host id with ping to each client currently connected
* 3. If game is currently started, 2 uint32_t which tell remaining time or
* progress in percent
* 4. If game is currently started, the track internal identity
*/
// ============================================================================
constexpr std::array<uint8_t, 5> g_ping_packet {{ 255, 'p', 'i', 'n', 'g' }};
@@ -810,11 +813,17 @@ void STKHost::mainLoop()
auto progress = sl->getGameStartedProgress();
ping_packet.addUInt32(progress.first)
.addUInt32(progress.second);
std::string current_track;
Track* t = sl->getPlayingTrack();
if (t)
current_track = t->getIdent();
ping_packet.encodeString(current_track);
}
else
{
ping_packet.addUInt32(std::numeric_limits<uint32_t>::max())
.addUInt32(std::numeric_limits<uint32_t>::max());
.addUInt32(std::numeric_limits<uint32_t>::max())
.addUInt8(0);
}
ping_packet.getBuffer().insert(
ping_packet.getBuffer().begin(), g_ping_packet.begin(),
@@ -974,10 +983,12 @@ void STKHost::mainLoop()
std::numeric_limits<uint32_t>::max();
uint32_t progress =
std::numeric_limits<uint32_t>::max();
std::string current_track;
try
{
remaining_time = ping_packet.getUInt32();
progress = ping_packet.getUInt32();
ping_packet.decodeString(&current_track);
}
catch (std::exception& e)
{
@@ -1000,6 +1011,9 @@ void STKHost::mainLoop()
{
lp->setGameStartedProgress(
std::make_pair(remaining_time, progress));
int idx = track_manager
->getTrackIndexByIdent(current_track);
lp->storePlayingTrack(idx);
}
}
}
@@ -1092,6 +1106,10 @@ void STKHost::handleDirectSocketRequest(Network* direct_socket,
s.addUInt8((uint8_t)
(sl->getCurrentState() == ServerLobby::WAITING_FOR_START_GAME ?
0 : 1));
std::string current_track;
if (Track* t = sl->getPlayingTrack())
current_track = t->getIdent();
s.encodeString(current_track);
direct_socket->sendRawPacket(s, sender);
} // if message is server-requested
else if (command == connection_cmd)
@@ -1303,7 +1321,7 @@ void STKHost::initClientNetwork(ENetEvent& event, Network* new_network)
auto pm = ProtocolManager::lock();
if (pm && !pm->isExiting())
pm->propagateEvent(new Event(&event, stk_peer));
} // replaceNetwork
} // initClientNetwork
// ----------------------------------------------------------------------------
std::pair<int, int> STKHost::getAllPlayersTeamInfo() const

View File

@@ -45,6 +45,7 @@
#include "states_screens/dialogs/network_user_dialog.hpp"
#include "states_screens/dialogs/server_configuration_dialog.hpp"
#include "states_screens/state_manager.hpp"
#include "tracks/track.hpp"
#include "utils/translation.hpp"
#include <utfwrapping.h>
@@ -266,20 +267,49 @@ void NetworkingLobby::onUpdate(float delta)
m_timeout_message->setVisible(true);
auto progress = cl->getGameStartedProgress();
core::stringw msg;
core::stringw current_track;
Track* t = cl->getPlayingTrack();
if (t)
current_track = t->getName();
if (progress.first != std::numeric_limits<uint32_t>::max())
{
//I18N: In the networking lobby, show when player is required to
//wait before the current game finish with remaining time
msg = _("Please wait for the current game's end, "
"estimated remaining time: %s.",
StringUtils::timeToString((float)progress.first).c_str());
if (!current_track.empty())
{
//I18N: In the networking lobby, show when player is required to
//wait before the current game finish with remaining time,
//showing the current track name inside bracket
msg = _("Please wait for the current game's (%s) end, "
"estimated remaining time: %s.", current_track,
StringUtils::timeToString((float)progress.first).c_str());
}
else
{
//I18N: In the networking lobby, show when player is required
//to wait before the current game finish with remaining time
msg = _("Please wait for the current game's end, "
"estimated remaining time: %s.",
StringUtils::timeToString((float)progress.first).c_str());
}
}
else if (progress.second != std::numeric_limits<uint32_t>::max())
{
//I18N: In the networking lobby, show when player is required to
//wait before the current game finish with progress in percent
msg = _("Please wait for the current game's end, "
"estimated progress: %d%.", progress.second);
if (!current_track.empty())
{
//I18N: In the networking lobby, show when player is required
//to wait before the current game finish with progress in
//percent, showing the current track name inside bracket
msg = _("Please wait for the current game's (%s) end, "
"estimated progress: %s%.", current_track,
progress.second);
}
else
{
//I18N: In the networking lobby, show when player is required
//to wait before the current game finish with progress in
//percent
msg = _("Please wait for the current game's end, "
"estimated progress: %d%.", progress.second);
}
}
else
{

View File

@@ -322,3 +322,12 @@ void TrackManager::updateGroups(const Track* track)
} // updateGroups
// ----------------------------------------------------------------------------
int TrackManager::getTrackIndexByIdent(const std::string& ident) const
{
for (unsigned i = 0; i < m_tracks.size(); i++)
{
if (m_tracks[i]->getIdent() == ident)
return i;
}
return -1;
} // getTrackIndexByIdent

View File

@@ -116,6 +116,8 @@ public:
* \param index The index number of the track. */
Track* getTrack(unsigned int index) const { return m_tracks[index];}
// ------------------------------------------------------------------------
int getTrackIndexByIdent(const std::string& ident) const;
// ------------------------------------------------------------------------
/** Checks if a certain track is available.
* \param n Index of the track to check. */
bool isAvailable(unsigned int n) const {return m_track_avail[n];}