Ejemplo n.º 1
0
        /// <summary>
        /// Invoked whenever matchmaker finds an opponent.
        /// </summary>
        private void OnMatchmakerMatched(IMatchmakerMatched matched)
        {
            _connection.BattleConnection = new BattleConnection(matched);
            _connection.Socket.ReceivedMatchmakerMatched -= OnMatchmakerMatched;

            SceneManager.LoadScene(GameConfigurationManager.Instance.GameConfiguration.SceneNameBattle);
        }
Ejemplo n.º 2
0
    private async void MatchJoin(IMatchmakerMatched matched)
    {
        var remote = matched.Users.FirstOrDefault(_1 => !_1.Presence.UserId.Equals(Composition.DataAccount.IdUser));

        Remote.Seed      = (float)remote.NumericProperties[KEY_SEED];
        Remote.NameUser  = remote.Presence.Username;
        Remote.BallColor = new Color(
            (float)remote.NumericProperties[KEY_BALL_COLOR_R],
            (float)remote.NumericProperties[KEY_BALL_COLOR_G],
            (float)remote.NumericProperties[KEY_BALL_COLOR_B]);
        Remote.BallRadius = (float)remote.NumericProperties[KEY_BALL_RADIUS];
        Remote.BallSpeedX = (float)remote.NumericProperties[KEY_BALL_SPEED_X];
        Remote.BallSpeedY = (float)remote.NumericProperties[KEY_BALL_SPEED_Y];

        if (Local.Seed == Remote.Seed)
        {
            throw new Exception("seeds are equals");
        }

        Debug.Log($">> match found for: {string.Join(", ", matched.Users.Select(_ => _.Presence.Username))}");

        _netMatch = await _netSocket.JoinMatchAsync(matched);

        lock (_users)
        {
            _users.AddRange(_netMatch.Presences);
        }

        Debug.Log($">> match state (joining): {string.Join(", ", _netMatch.Presences.Select(_ => _.Username))}");
    }
        /// <summary>
        /// Invoked whenever matchmaker finds an opponent.
        /// </summary>
        private void OnMatchmakerMatched(object sender, IMatchmakerMatched e)
        {
            UnityMainThreadDispatcher.Instance().Enqueue(() =>
            {
                ISocket socket              = NakamaSessionManager.Instance.Socket;
                socket.OnMatchmakerMatched -= OnMatchmakerMatched;

                StartCoroutine(LoadBattle(e));
            });
        }
        void MatchmakerMatched(IMatchmakerMatched matched)
        {
            UnityMainThreadDispatcher.Instance().Enqueue(() =>
            {
                ISocket socket = ServerSessionManager.Instance.Socket;
                socket.ReceivedMatchmakerMatched -= MatchmakerMatched;

                //StartCoroutine(LoadStadium(matched));
            });
        }
    void OnMatchmakerMatchFound(IMatchmakerMatched matched)
    {
        MatchMaker.Instance.UnsubscribeFromMatchFoundEvent(OnMatchmakerMatchFound);
        MatchMaker.Instance.SubscribeToPlayerJoinedEvent(OnPlayerJoinedMatch);
        MatchMaker.Instance.SubscribeToMatchStartEvent(OnMatchStart);

        ReadyPlayersCount.UpdateTotalPlayers(matched.Users.Count());
        AcceptMatchPanel.SetActive(true);
        LoadingPanel.SetActive(false);
    }
Ejemplo n.º 6
0
    void OnMatchMakerMatched(IMatchmakerMatched matched)
    {
        ServerSessionManager.Instance.Socket.ReceivedMatchmakerMatched -= OnMatchMakerMatched;
        _pendingMatch = matched;

        ChooseHost();

        UnityMainThreadDispatcher.Instance().Enqueue(() => {
            OnMatchFound?.Invoke(matched);
        });
    }
        IEnumerator LoadStadium(IMatchmakerMatched matched)
        {
            AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("Stadium1", LoadSceneMode.Additive);

            while (!asyncLoad.isDone)
            {
                yield return(null);
            }

            SceneManager.UnloadSceneAsync("FakeMatchmaker");
            //MatchCommunicationManager.Instance.JoinMatchAsync(matched);
        }
        /// <summary>
        /// Starts the game scene and joins the match
        /// </summary>
        ///

        private IEnumerator LoadBattle(IMatchmakerMatched matched)
        {
            AsyncOperation asyncLoad = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync("PlayingField", UnityEngine.SceneManagement.LoadSceneMode.Additive);

            while (!asyncLoad.isDone)
            {
                yield return(null);
            }

            UnityEngine.SceneManagement.SceneManager.UnloadSceneAsync("MainMenu");
            MatchCommunicationManager.Instance.JoinMatchAsync(matched);
        }
    // +++ event handler ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    async void OnMatchmakerMatched(object sender, IMatchmakerMatched e)
    {
        Debug.Log("Match found");

        _match = await _socket.JoinMatchAsync(e);

        Debug.Log("match joined");

        // persisting own presence
        _self    = _match.Self;
        _matchId = _match.Id;

        _connectedUsers.AddRange(_match.Presences);
    }
Ejemplo n.º 10
0
        /// <summary>
        /// Joins given match found by matchmaker
        /// </summary>
        /// <param name="matched"></param>
        public async void JoinMatchAsync(IMatchmakerMatched matched)
        {
            //Choosing host in deterministic way, with no need to exchange data between players
            ChooseHost(matched);

            //Filling list of match participants
            Players = new List <IUserPresence>();

            try
            {
                // Listen to incomming match messages and user connection changes
                _socket.OnMatchPresence += OnMatchPresence;
                _socket.OnMatchState    += ReceiveMatchStateMessage;

                // Join the match
                IMatch match = await _socket.JoinMatchAsync(matched);

                // Set current match id
                // It will be used to leave the match later
                MatchId = match.Id;
                Debug.Log("Joined match with id: " + match.Id + "; presences count: " + match.Presences.Count());

                // Add all players already connected to the match
                // If both players uses the same account, exit the game
                bool noDuplicateUsers = AddConnectedPlayers(match);
                if (noDuplicateUsers == true)
                {
                    // Match joined successfully
                    // Setting gameplay
                    _matchJoined = true;
                    StartGame();
                }
                else
                {
                    Debug.Log("Duplicate USERS! Leaving.");
                    LeaveGame();
                }
            }
            catch (Exception e)
            {
                Debug.LogError("Couldn't join match: " + e.Message);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Joins given match found by matchmaker
        /// </summary>
        /// <param name="matched"></param>
        public async void JoinMatchAsync(IMatchmakerMatched matched)
        {
            //Filling list of match participants
            Players = new List <IUserPresence>();

            try
            {
                // Listen to incomming match messages and user connection changes
                _socket.ReceivedMatchPresence += OnMatchPresence;
                _socket.ReceivedMatchState    += ReceiveMatchStateMessage;

                // Join the match
                IMatch match = await _socket.JoinMatchAsync(matched);

                // Set current match id
                // It will be used to leave the match later
                MatchId = match.Id;
                Debug.Log("Joined match with id: " + match.Id + "; presences count: " + match.Presences.Count());
                //string json = JsonUtility.ToJson(match, true);
                //string json = MatchMessage<T>.ToJson(message);
                //Debug.Log(json);

                // Add all players already connected to the match
                // If both players uses the same account, exit the game
                bool noDuplicateUsers = AddConnectedPlayers(match);
                if (noDuplicateUsers == true)
                {
                    // Match joined successfully
                    // Setting gameplay
                    _matchJoined = true;
                    StartGame();
                }
                else
                {
                    LeaveGame();
                }
            }
            catch (Exception e)
            {
                Debug.LogError("Couldn't join match: " + e.Message);
            }
        }
        /// <summary>
        /// Chooses host in deterministic way
        /// </summary>
        private void ChooseHost(IMatchmakerMatched matched)
        {
            // Add the session id of all users connected to the match
            List <string> userSessionIds = new List <string>();

            foreach (IMatchmakerUser user in matched.Users)
            {
                userSessionIds.Add(user.Presence.SessionId);
            }

            // Perform a lexicographical sort on list of user session ids
            userSessionIds.Sort();

            // First user from the sorted list will be the host of current match
            string hostSessionId = userSessionIds.First();

            // Get the user id from session id
            IMatchmakerUser hostUser = matched.Users.First(x => x.Presence.SessionId == hostSessionId);

            HostId = hostUser.Presence.UserId;
        }
Ejemplo n.º 13
0
        /// <inheritdoc cref="JoinMatchAsync(Nakama.IMatchmakerMatched)"/>
        public async Task <IMatch> JoinMatchAsync(IMatchmakerMatched matched)
        {
            var message = new MatchJoinMessage();

            if (matched.Token != null)
            {
                message.Token = matched.Token;
            }
            else
            {
                message.MatchId = matched.MatchId;
            }

            var envelope = new WebSocketMessageEnvelope
            {
                Cid       = $"{_cid++}",
                MatchJoin = message
            };
            var response = await SendAsync(envelope);

            return(response.Match);
        }
        // -------------------------------------------

        /*
         * OnReceivedMatchmakerMatched
         */
        private async void OnReceivedMatchmakerMatched(IMatchmakerMatched matched)
        {
            // Cache a reference to the local user.
            m_localUser = matched.Self.Presence;

            // Debug.LogError("MatchId=" + matched.MatchId);
            // Debug.LogError("Token=" + matched.Token);
            // Debug.LogError("Ticket=" + matched.Ticket);

            // Join the match.
            var match = await NakamaConnection.Socket.JoinMatchAsync(matched);

            // Spawn a player instance for each connected user.
            foreach (var user in match.Presences)
            {
                RegisterPlayer(match.Id, user);
            }

            m_currentMatch = match;

            await NakamaConnection.SendMainChatMessage(REMOVE_ROOMS_MESSAGE, m_roomName);
        }
Ejemplo n.º 15
0
        /// <inheritdoc />
        public async Task <IMatch> JoinMatchAsync(IMatchmakerMatched matched)
        {
            var message = new MatchJoinMessage();

            if (matched.Token != null)
            {
                message.Token = matched.Token;
            }
            else
            {
                message.MatchId = matched.MatchId;
            }

            var envelope = new WebSocketMessageEnvelope
            {
                Cid       = Guid.NewGuid().ToString(),
                MatchJoin = message
            };
            var response = await SendAsync(envelope).ConfigureAwait(false);

            return(response.Match);
        }
 private void ActionReceivedMatchmakerMatched(IMatchmakerMatched m)
 {
     m_mainThread.Enqueue(() => OnReceivedMatchmakerMatched(m));
 }
Ejemplo n.º 17
0
 private void _socket_OnMatchmakerMatched(object sender, IMatchmakerMatched e)
 {
     Debug.Log("Call to socket_OnMatchmakerMatched");
 }
Ejemplo n.º 18
0
 private void OnMatchmakerMatched(object sender, IMatchmakerMatched matched)
 {
     _context.CreateEntity().ReplaceDebugMessage("On Matchmaker Matched");
     _matchmakerMatched     = matched;
     _context.isGameMatched = true;
 }
Ejemplo n.º 19
0
    // +++ nakama event handler +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    private void OnMatchmakerMatched(object s, IMatchmakerMatched matched)
    {
        _matchMakerMatch = matched;
        nvpEventManager.INSTANCE.InvokeEvent(GameEvents.OnNakama_MatchFound, this, _matchMakerMatch);
    }
 public BattleConnection(IMatchmakerMatched matched)
 {
     Matched = matched;
 }