public PeerWithinLobbyResponse ReceiveWithinLobbyPeerRequest()
        {
            // same spiel as above goes for here as well
            if (!TryGetMessageOrStateChange(out PeerWithinLobbyRequest request))
            {
                return(null);
            }

            switch (request.MessageCase)
            {
            case PeerWithinLobbyRequest.MessageOneofCase.LeaveLobbyRequest:
            {
                LeaveLobby();
                state = Tcp_State.WithoutLobby;
                var response = new LeaveLobbyResponse();
                response.Success = true;

                return(new PeerWithinLobbyResponse
                    {
                        LeaveLobbyResponse = response
                    });
            }

            default:
                Log($"Unexpected within lobby request message.");
                return(null);
            }
        }
Beispiel #2
0
    public async void onLeaveLobby()
    {
        Api api = new Api();
        LeaveLobbyResponse leaveResponse = await api.LeaveLobby(ApplicationState.currentGameRoom.RoomId);

        // Reload the scene to update lobby.
        ApplicationState.currentGameRoom = null;
        SceneManager.LoadScene("GameSelect");
    }
        public HostWithinLobbyResponse ReceiveWithinLobbyHostRequest()
        {
            // same spiel as above goes for here as well
            if (!TryGetMessageOrStateChange(out HostWithinLobbyRequest request))
            {
                return(null);
            }

            var outerResponse = new HostWithinLobbyResponse();

            switch (request.MessageCase)
            {
            case HostWithinLobbyRequest.MessageOneofCase.LeaveLobbyRequest:
            {
                LeaveLobby();

                var response = new LeaveLobbyResponse();
                response.Success = true;
                state            = Tcp_State.WithoutLobby;

                return(new HostWithinLobbyResponse
                    {
                        LeaveLobbyResponse = response
                    });
            }

            case MakeHostRequest:
            {
                var response = new MakeHostResponse();
                int peer_id  = request.MakeHostRequest.PeerId;

                if (id != peer_id &&      // since we are host
                    joined_lobby.peers.ContainsKey(peer_id))
                {
                    response.NewHostId = peer_id;
                    MakeHost(joined_lobby.peers[peer_id]);
                }

                return(new HostWithinLobbyResponse
                    {
                        MakeHostResponse = response
                    });
            }

            case GoRequest:
            {
                var host_response        = new GoResponse();
                var host_address_message = CreateAddressMessage();

                var peer_notification = new PeerWithinLobbyResponse
                {
                    HostAddressInfo = host_address_message
                };

                // TODO: run concurrently (although sending is pretty fast, I suppose)
                foreach (var peer_id in joined_lobby.GetNonHostPeerIds())
                {
                    var peer = joined_lobby.peers[peer_id];
                    try
                    {
                        peer.joined_lobby = null;
                        peer.TransitionState(Tcp_State.Closing);
                        peer_notification.WriteDelimitedTo(peer.stream);
                        host_response.PeerAddressInfo.Add(peer.CreateAddressMessage());
                    }
                    catch
                    {
                        peer.Log($"An error has been catched while trying to send PeerAddressInfo");
                    }
                }
                server.lobbies.Remove(joined_lobby.id);
                joined_lobby = null;
                state        = Tcp_State.Closing;
                return(new HostWithinLobbyResponse
                    {
                        GoResponse = host_response
                    });
            }

            default:
                Log($"Unexpected within lobby request message.");
                return(null);
            }
        }