More minor issue fixes

* Move Joining state change sooner in the code to prevent an issue where
failing to connect multiple times in a row doesn't change the state (as
it goes from CouldNotConnect -> CouldNotConnect which doesn't trigger a
state changed callback)
* Prevent double clicking too fast on a room in the lobby from causing
issues
* Lobby no longer closes when joining a room
This commit is contained in:
James Rowe 2018-04-19 10:23:39 -06:00
parent 62257e0d79
commit d35693bbbc
5 changed files with 35 additions and 57 deletions

View File

@ -26,7 +26,7 @@ Lobby::Lobby(QWidget* parent, QStandardItemModel* list,
// setup the watcher for background connections // setup the watcher for background connections
watcher = new QFutureWatcher<void>; watcher = new QFutureWatcher<void>;
connect(watcher, &QFutureWatcher<void>::finished, this, &Lobby::OnConnection); connect(watcher, &QFutureWatcher<void>::finished, [&] { joining = false; });
model = new QStandardItemModel(ui->room_list); model = new QStandardItemModel(ui->room_list);
proxy = new LobbyFilterProxyModel(this, game_list); proxy = new LobbyFilterProxyModel(this, game_list);
@ -62,8 +62,6 @@ Lobby::Lobby(QWidget* parent, QStandardItemModel* list,
// Actions // Actions
connect(this, &Lobby::LobbyRefreshed, this, &Lobby::OnRefreshLobby); connect(this, &Lobby::LobbyRefreshed, this, &Lobby::OnRefreshLobby);
// TODO(jroweboy): change this slot to OnConnected?
connect(this, &Lobby::Connected, p, &MultiplayerState::OnOpenNetworkRoom);
// manually start a refresh when the window is opening // manually start a refresh when the window is opening
// TODO(jroweboy): if this refresh is slow for people with bad internet, then don't do it as // TODO(jroweboy): if this refresh is slow for people with bad internet, then don't do it as
@ -86,6 +84,10 @@ void Lobby::OnExpandRoom(const QModelIndex& index) {
} }
void Lobby::OnJoinRoom(const QModelIndex& source) { void Lobby::OnJoinRoom(const QModelIndex& source) {
if (joining) {
return;
}
joining = true;
QModelIndex index = source; QModelIndex index = source;
// If the user double clicks on a child row (aka the player list) then use the parent instead // If the user double clicks on a child row (aka the player list) then use the parent instead
if (source.parent() != QModelIndex()) { if (source.parent() != QModelIndex()) {
@ -301,24 +303,3 @@ void LobbyFilterProxyModel::SetFilterSearch(const QString& filter) {
filter_search = filter; filter_search = filter;
invalidate(); invalidate();
} }
void Lobby::OnConnection() {
if (auto room_member = Network::GetRoomMember().lock()) {
switch (room_member->GetState()) {
case Network::RoomMember::State::CouldNotConnect:
ShowError(NetworkMessage::UNABLE_TO_CONNECT);
break;
case Network::RoomMember::State::NameCollision:
ShowError(NetworkMessage::USERNAME_IN_USE);
break;
case Network::RoomMember::State::Error:
ShowError(NetworkMessage::UNABLE_TO_CONNECT);
break;
case Network::RoomMember::State::Joining:
auto parent = static_cast<MultiplayerState*>(parentWidget());
parent->OnOpenNetworkRoom();
close();
break;
}
}
}

View File

@ -60,29 +60,12 @@ private slots:
*/ */
void OnJoinRoom(const QModelIndex&); void OnJoinRoom(const QModelIndex&);
/**
* Handler for connection status changes. Launches the client room window if successful or
* displays an error
*/
void OnConnection();
signals: signals:
/** /**
* Signalled when the latest lobby data is retrieved. * Signalled when the latest lobby data is retrieved.
*/ */
void LobbyRefreshed(); void LobbyRefreshed();
/**
* Signalled when the status for room connection changes.
*/
void Connected();
/**
* Signalled by this widget when it is closing itself and destroying any state such as
* connections that it might have.
*/
void Closed();
void StateChanged(const Network::RoomMember::State&); void StateChanged(const Network::RoomMember::State&);
private: private:
@ -106,6 +89,7 @@ private:
std::unique_ptr<Ui::Lobby> ui; std::unique_ptr<Ui::Lobby> ui;
QFutureWatcher<void>* watcher; QFutureWatcher<void>* watcher;
Validation validation; Validation validation;
bool joining = false;
}; };
/** /**

View File

@ -66,45 +66,31 @@ void MultiplayerState::Close() {
} }
void MultiplayerState::OnNetworkStateChanged(const Network::RoomMember::State& state) { void MultiplayerState::OnNetworkStateChanged(const Network::RoomMember::State& state) {
NGLOG_DEBUG(Frontend, "Network state change"); NGLOG_DEBUG(Frontend, "Network State: {}", Network::GetStateStr(state));
bool is_connected = false; bool is_connected = false;
switch (state) { switch (state) {
case Network::RoomMember::State::Idle:
NGLOG_DEBUG(Network, "State: Idle");
break;
case Network::RoomMember::State::Joining:
NGLOG_DEBUG(Network, "State: Joining");
break;
case Network::RoomMember::State::LostConnection: case Network::RoomMember::State::LostConnection:
NetworkMessage::ShowError(NetworkMessage::LOST_CONNECTION); NetworkMessage::ShowError(NetworkMessage::LOST_CONNECTION);
NGLOG_DEBUG(Network, "State: LostConnection");
break; break;
case Network::RoomMember::State::CouldNotConnect: case Network::RoomMember::State::CouldNotConnect:
NetworkMessage::ShowError(NetworkMessage::UNABLE_TO_CONNECT); NetworkMessage::ShowError(NetworkMessage::UNABLE_TO_CONNECT);
NGLOG_DEBUG(Network, "State: CouldNotConnect");
break; break;
case Network::RoomMember::State::NameCollision: case Network::RoomMember::State::NameCollision:
NetworkMessage::ShowError(NetworkMessage::USERNAME_IN_USE); NetworkMessage::ShowError(NetworkMessage::USERNAME_IN_USE);
NGLOG_DEBUG(Network, "State: NameCollision");
break; break;
case Network::RoomMember::State::MacCollision: case Network::RoomMember::State::MacCollision:
NetworkMessage::ShowError(NetworkMessage::MAC_COLLISION); NetworkMessage::ShowError(NetworkMessage::MAC_COLLISION);
NGLOG_DEBUG(Network, "State: MacCollision");
break; break;
case Network::RoomMember::State::WrongPassword: case Network::RoomMember::State::WrongPassword:
NetworkMessage::ShowError(NetworkMessage::WRONG_PASSWORD); NetworkMessage::ShowError(NetworkMessage::WRONG_PASSWORD);
NGLOG_DEBUG(Network, "State: WrongPassword");
break; break;
case Network::RoomMember::State::WrongVersion: case Network::RoomMember::State::WrongVersion:
NetworkMessage::ShowError(NetworkMessage::WRONG_VERSION); NetworkMessage::ShowError(NetworkMessage::WRONG_VERSION);
NGLOG_DEBUG(Network, "State: WrongVersion");
break; break;
case Network::RoomMember::State::Error: case Network::RoomMember::State::Error:
NetworkMessage::ShowError(NetworkMessage::UNABLE_TO_CONNECT); NetworkMessage::ShowError(NetworkMessage::UNABLE_TO_CONNECT);
NGLOG_DEBUG(Network, "State: GenericError");
break; break;
case Network::RoomMember::State::Joined: case Network::RoomMember::State::Joined:
NGLOG_DEBUG(Network, "State: Joined");
is_connected = true; is_connected = true;
OnOpenNetworkRoom(); OnOpenNetworkRoom();
break; break;

View File

@ -406,6 +406,8 @@ void RoomMember::Join(const std::string& nick, const char* server_addr, u16 serv
ASSERT_MSG(room_member_impl->client != nullptr, "Could not create client"); ASSERT_MSG(room_member_impl->client != nullptr, "Could not create client");
} }
room_member_impl->SetState(State::Joining);
ENetAddress address{}; ENetAddress address{};
enet_address_set_host(&address, server_addr); enet_address_set_host(&address, server_addr);
address.port = server_port; address.port = server_port;
@ -421,7 +423,6 @@ void RoomMember::Join(const std::string& nick, const char* server_addr, u16 serv
int net = enet_host_service(room_member_impl->client, &event, ConnectionTimeoutMs); int net = enet_host_service(room_member_impl->client, &event, ConnectionTimeoutMs);
if (net > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { if (net > 0 && event.type == ENET_EVENT_TYPE_CONNECT) {
room_member_impl->nickname = nick; room_member_impl->nickname = nick;
room_member_impl->SetState(State::Joining);
room_member_impl->StartLoop(); room_member_impl->StartLoop();
room_member_impl->SendJoinRequest(nick, preferred_mac, password); room_member_impl->SendJoinRequest(nick, preferred_mac, password);
SendGameInfo(room_member_impl->current_game_info); SendGameInfo(room_member_impl->current_game_info);

View File

@ -187,4 +187,30 @@ private:
std::unique_ptr<RoomMemberImpl> room_member_impl; std::unique_ptr<RoomMemberImpl> room_member_impl;
}; };
static const char* GetStateStr(const RoomMember::State& s) {
switch (s) {
case RoomMember::State::Idle:
return "Idle";
case RoomMember::State::Error:
return "Error";
case RoomMember::State::Joining:
return "Joining";
case RoomMember::State::Joined:
return "Joined";
case RoomMember::State::LostConnection:
return "LostConnection";
case RoomMember::State::NameCollision:
return "NameCollision";
case RoomMember::State::MacCollision:
return "MacCollision";
case RoomMember::State::WrongVersion:
return "WrongVersion";
case RoomMember::State::WrongPassword:
return "WrongPassword";
case RoomMember::State::CouldNotConnect:
return "CouldNotConnect";
}
return "Unknown";
}
} // namespace Network } // namespace Network