Allow going back to lobby in selecting karts or tracks phase

It allows player to be spectator later
This commit is contained in:
Benau 2019-01-16 13:43:26 +08:00
parent 0355922116
commit 4e2b843f71
4 changed files with 64 additions and 11 deletions

View File

@ -303,7 +303,7 @@ bool ServerLobby::notifyEvent(Event* event)
case LE_LIVE_JOIN: liveJoinRequest(event); break;
case LE_CLIENT_LOADED_WORLD: finishedLoadingLiveJoinClient(event); break;
case LE_KART_INFO: handleKartInfo(event); break;
case LE_CLIENT_BACK_LOBBY: clientWantsToBackLobby(event); break;
case LE_CLIENT_BACK_LOBBY: clientInGameWantsToBackLobby(event); break;
default: Log::error("ServerLobby", "Unknown message type %d - ignored.",
message_type);
break;
@ -409,6 +409,8 @@ bool ServerLobby::notifyEventAsynchronous(Event* event)
case LE_CHAT: handleChat(event); break;
case LE_CONFIG_SERVER: handleServerConfiguration(event); break;
case LE_CHANGE_HANDICAP: changeHandicap(event); break;
case LE_CLIENT_BACK_LOBBY:
clientSelectingAssetsWantsToBackLobby(event); break;
default: break;
} // switch
} // if (event->getType() == EVENT_TYPE_MESSAGE)
@ -3466,7 +3468,7 @@ void ServerLobby::handleKartInfo(Event* event)
/** Client if currently in-game (including spectator) wants to go back to
* lobby.
*/
void ServerLobby::clientWantsToBackLobby(Event* event)
void ServerLobby::clientInGameWantsToBackLobby(Event* event)
{
World* w = World::getWorld();
std::shared_ptr<STKPeer> peer = event->getPeerSP();
@ -3513,4 +3515,36 @@ void ServerLobby::clientWantsToBackLobby(Event* event)
m_game_setup->addServerInfo(server_info);
peer->sendPacket(server_info, /*reliable*/true);
delete server_info;
} // clientWantsToBackLobby
} // clientInGameWantsToBackLobby
//-----------------------------------------------------------------------------
/** Client if currently select assets wants to go back to lobby.
*/
void ServerLobby::clientSelectingAssetsWantsToBackLobby(Event* event)
{
std::shared_ptr<STKPeer> peer = event->getPeerSP();
if (m_state.load() != SELECTING || peer->isWaitingForGame())
{
Log::warn("ServerLobby",
"%s try to leave selecting assets at wrong time.",
peer->getAddress().toString().c_str());
return;
}
m_peers_ready.erase(peer);
peer->setWaitingForGame(true);
NetworkString* reset = getNetworkString(2);
reset->setSynchronous(true);
reset->addUInt8(LE_BACK_LOBBY).addUInt8(BLR_NONE);
peer->sendPacket(reset, /*reliable*/true);
delete reset;
updatePlayerList();
NetworkString* server_info = getNetworkString();
server_info->setSynchronous(true);
server_info->addUInt8(LE_SERVER_INFO);
m_game_setup->addServerInfo(server_info);
peer->sendPacket(server_info, /*reliable*/true);
delete server_info;
} // clientSelectingAssetsWantsToBackLobby

View File

@ -286,7 +286,8 @@ private:
int getReservedId(std::shared_ptr<NetworkPlayerProfile>& p,
unsigned local_id) const;
void handleKartInfo(Event* event);
void clientWantsToBackLobby(Event* event);
void clientInGameWantsToBackLobby(Event* event);
void clientSelectingAssetsWantsToBackLobby(Event* event);
public:
ServerLobby();
virtual ~ServerLobby();

View File

@ -45,10 +45,6 @@ void NetworkKartSelectionScreen::init()
updateProgressBarText();
}
// change the back button image (because it makes the game quit)
IconButtonWidget* back_button = getWidget<IconButtonWidget>("back");
back_button->setImage("gui/icons/main_quit.png");
DynamicRibbonWidget* w = getWidget<DynamicRibbonWidget>("karts");
assert(w != NULL);
for (auto& p : NetworkConfig::get()->getNetworkPlayers())
@ -66,6 +62,7 @@ void NetworkKartSelectionScreen::init()
w->setSelection(0, 0, true);
}
}
m_exit_timeout = std::numeric_limits<uint64_t>::max();
} // init
// ----------------------------------------------------------------------------
@ -74,6 +71,16 @@ void NetworkKartSelectionScreen::init()
*/
void NetworkKartSelectionScreen::onUpdate(float dt)
{
if (StkTime::getRealTimeMs() > m_exit_timeout)
{
// Reset the screen to networking menu if failed to back to lobby
STKHost::get()->shutdown();
StateManager::get()->resetAndSetStack(
NetworkConfig::get()->getResetScreens().data());
NetworkConfig::get()->unsetNetworking();
return;
}
KartSelectionScreen::onUpdate(dt);
updateProgressBarText();
} // onUpdate
@ -131,9 +138,19 @@ void NetworkKartSelectionScreen::allPlayersDone()
// ----------------------------------------------------------------------------
bool NetworkKartSelectionScreen::onEscapePressed()
{
// then remove the lobby screen (you left the server)
StateManager::get()->popMenu();
STKHost::get()->shutdown();
if (!m_live_join)
{
if (m_exit_timeout == std::numeric_limits<uint64_t>::max())
{
// Send go back lobby event to server with an exit timeout, so if
// server doesn't react in time we exit the server
m_exit_timeout = StkTime::getRealTimeMs() + 5000;
NetworkString back(PROTOCOL_LOBBY_ROOM);
back.addUInt8(LobbyProtocol::LE_CLIENT_BACK_LOBBY);
STKHost::get()->sendToServer(&back, true);
}
return false;
}
return true; // remove the screen
} // onEscapePressed

View File

@ -40,6 +40,7 @@ private:
bool m_live_join;
uint64_t m_exit_timeout;
protected:
// ------------------------------------------------------------------------
NetworkKartSelectionScreen()