Let other players know the newly joined player

This commit is contained in:
Benau 2019-01-01 16:32:37 +08:00
parent 2f8da236e3
commit 81776cb4df
7 changed files with 109 additions and 2 deletions

View File

@ -536,6 +536,8 @@ public:
// ------------------------------------------------------------------------
virtual Stars* getStarsEffect() const = 0;
// ------------------------------------------------------------------------
int getLiveJoinUntilTicks() const { return m_live_join_util; }
// ------------------------------------------------------------------------
void setLiveJoinKart(int util_ticks) { m_live_join_util = util_ticks; }
}; // AbstractKart

View File

@ -28,6 +28,7 @@
#include "karts/max_speed.hpp"
#include "karts/skidding.hpp"
#include "modes/world.hpp"
#include "network/protocols/client_lobby.hpp"
#include "network/rewind_manager.hpp"
#include "network/network_string.hpp"
#include "physics/btKart.hpp"
@ -108,7 +109,7 @@ void KartRewinder::computeError()
if (!m_has_server_state && !isEliminated())
{
const int kartid = getWorldKartId();
Log::debug("KartRewinder", "Kart id %d disconnected", kartid);
Log::debug("KartRewinder", "Kart id %d disconnected.", kartid);
World::getWorld()->eliminateKart(kartid,
false/*notify_of_elimination*/);
setPosition(World::getWorld()->getCurrentNumKarts() + 1);
@ -119,6 +120,20 @@ void KartRewinder::computeError()
rki.makeReserved();
}
}
else if (m_has_server_state && isEliminated())
{
if (auto cl = LobbyProtocol::get<ClientLobby>())
{
Log::debug("KartRewinder", "Kart id %d connected.",
getWorldKartId());
cl->requestKartInfo((uint8_t)getWorldKartId());
// New live join kart, hide the node until new kart info is received
// see ClientLobby::handleKartInfo
World::getWorld()->addReservedKart(getWorldKartId());
reset();
getNode()->setVisible(false);
}
}
} // computeError
// ----------------------------------------------------------------------------

View File

@ -21,6 +21,7 @@
#include "audio/sfx_manager.hpp"
#include "config/user_config.hpp"
#include "config/player_manager.hpp"
#include "graphics/render_info.hpp"
#include "guiengine/modaldialog.hpp"
#include "guiengine/message_queue.hpp"
#include "guiengine/screen_keyboard.hpp"
@ -160,6 +161,7 @@ bool ClientLobby::notifyEvent(Event* event)
case LE_BAD_TEAM: handleBadTeam(); break;
case LE_BAD_CONNECTION: handleBadConnection(); break;
case LE_LIVE_JOIN_ACK: liveJoinAcknowledged(event); break;
case LE_KART_INFO: handleKartInfo(event); break;
default:
return false;
break;
@ -1005,3 +1007,56 @@ void ClientLobby::finishLiveJoin()
}
m_state.store(RACING);
} // finishLiveJoin
//-----------------------------------------------------------------------------
void ClientLobby::requestKartInfo(uint8_t kart_id)
{
NetworkString* ns = getNetworkString(1);
ns->setSynchronous(true);
ns->addUInt8(LE_KART_INFO).addUInt8(kart_id);
sendToServer(ns, true/*reliable*/);
delete ns;
} // requestKartInfo
//-----------------------------------------------------------------------------
void ClientLobby::handleKartInfo(Event* event)
{
World* w = World::getWorld();
if (!w)
return;
const NetworkString& data = event->data();
int live_join_util_ticks = data.getUInt32();
uint8_t kart_id = data.getUInt8();
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();
std::string kart_name;
data.decodeString(&kart_name);
RemoteKartInfo& rki = race_manager->getKartInfo(kart_id);
rki.setPlayerName(player_name);
rki.setHostId(host_id);
rki.setDefaultKartColor(kart_color);
rki.setOnlineId(online_id);
rki.setPerPlayerDifficulty(ppd);
rki.setLocalPlayerId(local_id);
rki.setKartName(kart_name);
AbstractKart* k = w->getKart(kart_id);
k->changeKart(rki.getKartName(), rki.getDifficulty(),
rki.getKartTeam() == KART_TEAM_RED ?
std::make_shared<RenderInfo>(1.0f) :
rki.getKartTeam() == KART_TEAM_BLUE ?
std::make_shared<RenderInfo>(0.66f) :
std::make_shared<RenderInfo>(rki.getDefaultKartColor()));
k->setLiveJoinKart(live_join_util_ticks);
// I18N: Show when player join the started game in network
core::stringw msg = _("%s joined the game.", player_name);
MessageQueue::add(MessageQueue::MT_FRIEND, msg);
} // handleKartInfo

View File

@ -100,6 +100,7 @@ private:
irr::core::stringw m_total_players;
void liveJoinAcknowledged(Event* event);
void handleKartInfo(Event* event);
void finishLiveJoin();
public:
ClientLobby(const TransportAddress& a, std::shared_ptr<Server> s);
@ -125,6 +126,7 @@ public:
bool isWaitingForGame() const { return m_waiting_for_game; }
bool isServerAutoGameTime() const { return m_server_auto_game_time; }
virtual bool isRacing() const OVERRIDE { return m_state.load() == RACING; }
void requestKartInfo(uint8_t kart_id);
};
#endif // CLIENT_LOBBY_HPP

View File

@ -69,7 +69,8 @@ public:
LE_CONFIG_SERVER,
LE_CHANGE_HANDICAP,
LE_LIVE_JOIN,
LE_LIVE_JOIN_ACK
LE_LIVE_JOIN_ACK,
LE_KART_INFO
};
enum RejectReason : uint8_t

View File

@ -300,6 +300,7 @@ bool ServerLobby::notifyEvent(Event* event)
case LE_RACE_FINISHED_ACK: playerFinishedResult(event); break;
case LE_LIVE_JOIN: liveJoinRequest(event); break;
case LE_CLIENT_LOADED_WORLD: finishedLoadingLiveJoinClient(event); break;
case LE_KART_INFO: handleKartInfo(event); break;
default: Log::error("ServerLobby", "Unknown message type %d - ignored.",
message_type);
break;
@ -3288,3 +3289,33 @@ void ServerLobby::setPlayerKarts(const NetworkString& ns, STKPeer* peer) const
}
}
} // setPlayerKarts
//-----------------------------------------------------------------------------
void ServerLobby::handleKartInfo(Event* event)
{
World* w = World::getWorld();
if (!w)
return;
STKPeer* peer = event->getPeer();
const NetworkString& data = event->data();
uint8_t kart_id = data.getUInt8();
if (kart_id > race_manager->getNumPlayers())
return;
AbstractKart* k = w->getKart(kart_id);
int live_join_util_ticks = k->getLiveJoinUntilTicks();
const RemoteKartInfo& rki = race_manager->getKartInfo(kart_id);
NetworkString* ns = getNetworkString(1);
ns->setSynchronous(true);
ns->addUInt8(LE_KART_INFO).addUInt32(live_join_util_ticks)
.addUInt8(kart_id) .encodeString(rki.getPlayerName())
.addUInt32(rki.getHostId()).addFloat(rki.getDefaultKartColor())
.addUInt32(rki.getOnlineId()).addUInt8(rki.getDifficulty())
.addUInt8((uint8_t)rki.getLocalPlayerId())
.encodeString(rki.getKartName());
peer->sendPacket(ns, true/*reliable*/);
delete ns;
} // handleKartInfo

View File

@ -281,6 +281,7 @@ private:
bool canLiveJoinNow() const;
int getReservedId(std::shared_ptr<NetworkPlayerProfile>& p,
unsigned local_id) const;
void handleKartInfo(Event* event);
public:
ServerLobby();
virtual ~ServerLobby();