Split decode and encode players list for later usage

This commit is contained in:
Benau 2019-04-16 10:29:46 +08:00
parent 78eb83b989
commit 54375ab682
4 changed files with 95 additions and 53 deletions

View File

@ -260,31 +260,9 @@ void ClientLobby::addAllPlayers(Event* event)
peer->cleanPlayerProfiles();
m_server_send_live_load_world = data.getUInt8() == 1;
std::vector<std::shared_ptr<NetworkPlayerProfile> > players;
unsigned player_count = data.getUInt8();
bool is_specator = true;
for (unsigned i = 0; i < player_count; i++)
{
core::stringw player_name;
data.decodeStringW(&player_name);
uint32_t host_id = data.getUInt32();
float kart_color = data.getFloat();
uint32_t online_id = data.getUInt32();
PerPlayerDifficulty ppd = (PerPlayerDifficulty)data.getUInt8();
uint8_t local_id = data.getUInt8();
KartTeam team = (KartTeam)data.getUInt8();
std::string country_id;
data.decodeString(&country_id);
if (host_id == STKHost::get()->getMyHostId())
is_specator = false;
auto player = std::make_shared<NetworkPlayerProfile>(peer, player_name,
host_id, kart_color, online_id, ppd, local_id, team, country_id);
std::string kart_name;
data.decodeString(&kart_name);
player->setKartName(kart_name);
players.push_back(player);
}
std::vector<std::shared_ptr<NetworkPlayerProfile> > players =
decodePlayers(data, peer, &is_specator);
setSpectator(is_specator);
uint32_t random_seed = data.getUInt32();
@ -331,6 +309,39 @@ void ClientLobby::addAllPlayers(Event* event)
input_manager->getDeviceManager()->setAssignMode(ASSIGN);
} // addAllPlayers
//-----------------------------------------------------------------------------
/* Get list of players from server and see if we are spectating it. */
std::vector<std::shared_ptr<NetworkPlayerProfile> >
ClientLobby::decodePlayers(const BareNetworkString& data,
std::shared_ptr<STKPeer> peer,
bool* is_specator) const
{
std::vector<std::shared_ptr<NetworkPlayerProfile> > players;
unsigned player_count = data.getUInt8();
for (unsigned i = 0; i < player_count; i++)
{
core::stringw player_name;
data.decodeStringW(&player_name);
uint32_t host_id = data.getUInt32();
float kart_color = data.getFloat();
uint32_t online_id = data.getUInt32();
PerPlayerDifficulty ppd = (PerPlayerDifficulty)data.getUInt8();
uint8_t local_id = data.getUInt8();
KartTeam team = (KartTeam)data.getUInt8();
std::string country_id;
data.decodeString(&country_id);
if (is_specator && host_id == STKHost::get()->getMyHostId())
*is_specator = false;
auto player = std::make_shared<NetworkPlayerProfile>(peer, player_name,
host_id, kart_color, online_id, ppd, local_id, team, country_id);
std::string kart_name;
data.decodeString(&kart_name);
player->setKartName(kart_name);
players.push_back(player);
}
return players;
} // decodePlayers
//-----------------------------------------------------------------------------
void ClientLobby::update(int ticks)
{

View File

@ -130,6 +130,10 @@ private:
void liveJoinAcknowledged(Event* event);
void handleKartInfo(Event* event);
void finishLiveJoin();
std::vector<std::shared_ptr<NetworkPlayerProfile> >
decodePlayers(const BareNetworkString& data,
std::shared_ptr<STKPeer> peer = nullptr,
bool* is_specator = NULL) const;
public:
ClientLobby(const TransportAddress& a, std::shared_ptr<Server> s);
virtual ~ClientLobby();

View File

@ -636,6 +636,19 @@ void ServerLobby::asynchronousUpdate()
// Add placeholder players for live join
addLiveJoinPlaceholder(players);
// If player chose random / hasn't chose any kart
for (unsigned i = 0; i < players.size(); i++)
{
if (players[i]->getKartName().empty())
{
RandomGenerator rg;
std::set<std::string>::iterator it =
m_available_kts.first.begin();
std::advance(it,
rg.get((int)m_available_kts.first.size()));
players[i]->setKartName(*it);
}
}
NetworkString* load_world_message = getLoadWorldMessage(players,
false/*live_join*/);
@ -663,6 +676,27 @@ void ServerLobby::asynchronousUpdate()
} // asynchronousUpdate
//-----------------------------------------------------------------------------
void ServerLobby::encodePlayers(BareNetworkString* bns,
std::vector<std::shared_ptr<NetworkPlayerProfile> >& players) const
{
bns->addUInt8((uint8_t)players.size());
for (unsigned i = 0; i < players.size(); i++)
{
std::shared_ptr<NetworkPlayerProfile>& player = players[i];
bns->encodeString(player->getName())
.addUInt32(player->getHostId())
.addFloat(player->getDefaultKartColor())
.addUInt32(player->getOnlineId())
.addUInt8(player->getPerPlayerDifficulty())
.addUInt8(player->getLocalPlayerId())
.addUInt8(
race_manager->teamEnabled() ? player->getTeam() : KART_TEAM_NONE)
.encodeString(player->getCountryId());
bns->encodeString(player->getKartName());
}
} // encodePlayers
//-----------------------------------------------------------------------------
NetworkString* ServerLobby::getLoadWorldMessage(
std::vector<std::shared_ptr<NetworkPlayerProfile> >& players,
@ -673,29 +707,8 @@ NetworkString* ServerLobby::getLoadWorldMessage(
load_world_message->addUInt8(LE_LOAD_WORLD);
load_world_message->addUInt32(m_winner_peer_id);
m_default_vote->encode(load_world_message);
load_world_message->addUInt8(live_join ? 1 : 0).
addUInt8((uint8_t)players.size());
for (unsigned i = 0; i < players.size(); i++)
{
std::shared_ptr<NetworkPlayerProfile>& player = players[i];
load_world_message->encodeString(player->getName())
.addUInt32(player->getHostId())
.addFloat(player->getDefaultKartColor())
.addUInt32(player->getOnlineId())
.addUInt8(player->getPerPlayerDifficulty())
.addUInt8(player->getLocalPlayerId())
.addUInt8(
race_manager->teamEnabled() ? player->getTeam() : KART_TEAM_NONE)
.encodeString(player->getCountryId());
if (player->getKartName().empty())
{
RandomGenerator rg;
std::set<std::string>::iterator it = m_available_kts.first.begin();
std::advance(it, rg.get((int)m_available_kts.first.size()));
player->setKartName(*it);
}
load_world_message->encodeString(player->getKartName());
}
load_world_message->addUInt8(live_join ? 1 : 0);
encodePlayers(load_world_message, players);
load_world_message->addUInt32(m_item_seed);
if (race_manager->isBattleMode())
{
@ -840,6 +853,21 @@ void ServerLobby::liveJoinRequest(Event* event)
peer->getAddress().toString().c_str());
}
std::vector<std::shared_ptr<NetworkPlayerProfile> > players =
getLivePlayers();
NetworkString* load_world_message = getLoadWorldMessage(players,
true/*live_join*/);
peer->sendPacket(load_world_message, true/*reliable*/);
delete load_world_message;
peer->updateLastActivity();
} // liveJoinRequest
//-----------------------------------------------------------------------------
/** Get a list of current ingame players for live join or spectate.
*/
std::vector<std::shared_ptr<NetworkPlayerProfile> >
ServerLobby::getLivePlayers() const
{
std::vector<std::shared_ptr<NetworkPlayerProfile> > players;
for (unsigned i = 0; i < race_manager->getNumPlayers(); i++)
{
@ -869,12 +897,8 @@ void ServerLobby::liveJoinRequest(Event* event)
}
players.push_back(player);
}
NetworkString* load_world_message = getLoadWorldMessage(players,
true/*live_join*/);
peer->sendPacket(load_world_message, true/*reliable*/);
delete load_world_message;
peer->updateLastActivity();
} // liveJoinRequest
return players;
} // getLivePlayers
//-----------------------------------------------------------------------------
/** Decide where to put the live join player depends on his team and game mode.

View File

@ -283,6 +283,9 @@ private:
NetworkString* getLoadWorldMessage(
std::vector<std::shared_ptr<NetworkPlayerProfile> >& players,
bool live_join) const;
void encodePlayers(BareNetworkString* bns,
std::vector<std::shared_ptr<NetworkPlayerProfile> >& players) const;
std::vector<std::shared_ptr<NetworkPlayerProfile> > getLivePlayers() const;
void setPlayerKarts(const NetworkString& ns, STKPeer* peer) const;
void liveJoinRequest(Event* event);
void rejectLiveJoin(STKPeer* peer, BackLobbyReason blr);