/// <summary>
        /// Joins matchmaker queue.
        /// </summary>
        /// <remarks>
        /// Nakama allows for joining multiple matchmakers, however for the purposes of this demo,
        /// we will allow only for joining a single matchmaking queue.
        /// </remarks>
        private async Task <bool> StartMatchmakerAsync()
        {
            if (ticket != null)
            {
                Debug.Log("Matchmaker already started");
                return(false);
            }

            ISocket socket = NakamaSessionManager.Instance.Socket;

            // Create params object with default values
            MatchmakingParams param = new MatchmakingParams();

            socket.OnMatchmakerMatched += OnMatchmakerMatched;
            // Join the matchmaker
            ticket = await MatchmakingManager.EnterQueueAsync(socket, param);

            if (ticket == null)
            {
                Debug.Log("Couldn't start matchmaker" + Environment.NewLine + "Try again later");
                socket.OnMatchmakerMatched -= OnMatchmakerMatched;
                return(false);
            }
            else
            {
                // Matchmaker queue joined
                return(true);
            }
        }
示例#2
0
        /// <summary>
        /// Adds user to matchmaking queue with given params.
        /// Returns matchmaker ticket on succes or null on failure.
        /// </summary>
        public static async Task <IMatchmakerTicket> EnterQueueAsync(ISocket socket, MatchmakingParams matchmakingParams)
        {
            if (matchmakingParams == null)
            {
                Debug.LogError("Matchmaking params cannot be null");
                return(null);
            }

            try
            {
                // Acquires matchmaking ticket used to join a match
                IMatchmakerTicket ticket = await socket.AddMatchmakerAsync(
                    matchmakingParams.query,
                    matchmakingParams.minUserCount,
                    matchmakingParams.maxUserCount,
                    matchmakingParams.stringProperties,
                    matchmakingParams.numericProperties);

                return(ticket);
            }
            catch (Exception e)
            {
                Debug.LogWarning("An error has occured while joining the matchmaker: " + e);
                return(null);
            }
        }
示例#3
0
        /// <summary>
        /// Removes user from a queue.
        /// Returns matchmaker true on succes.
        /// </summary>
        public static async Task <bool> LeaveQueueAsync(ISocket socket, IMatchmakerTicket ticket)
        {
            try
            {
                await socket.RemoveMatchmakerAsync(ticket);

                return(true);
            }
            catch (Exception e)
            {
                Debug.Log("An exception has occured while leaving matchamaker: " + e);
                return(false);
            }
        }
        /// <summary>
        /// Leaves matchmaker queue.
        /// </summary>
        private async Task <bool> StopMatchmakerAsync(IMatchmakerTicket ticket)
        {
            if (ticket == null)
            {
                Debug.Log("Couldn't stop matchmaker; matchmaking hasn't been started yet");
                return(false);
            }
            ISocket socket = NakamaSessionManager.Instance.Socket;
            bool    good   = await MatchmakingManager.LeaveQueueAsync(socket, ticket);

            this.ticket = null;
            socket.OnMatchmakerMatched -= OnMatchmakerMatched;
            return(good);
        }
示例#5
0
        /// <summary>
        /// Leaves matchmaker queue and hides this panel.
        /// </summary>
        public async override void Hide(bool isMuteSoundManager = false)
        {
            try
            {
                await _connection.Socket.RemoveMatchmakerAsync(_ticket);
            }
            catch (Exception e)
            {
                Debug.LogWarning("An error has occured while removing from matchmaker: " + e);
            }

            _connection.Socket.ReceivedMatchmakerMatched -= OnMatchmakerMatched;
            _ticket = null;
            base.Hide(isMuteSoundManager);
        }
示例#6
0
    public async Task <bool> StartMatchMaker(int matchType, int playerCount)
    {
        var minCount          = playerCount;
        var maxCount          = playerCount;
        var numericProperties = new Dictionary <string, double>()
        {
            { "matchType", matchType }
        };
        string query;

        switch (matchType)
        {
        case 0:
            query = "*";
            break;

        case 1:
            query = "-properties.matchType:2 -properties.matchType:3";
            break;

        case 2:
            query = "-properties.matchType:1 -properties.matchType:3";
            break;

        case 3:
            query = "-properties.matchType:1 -properties.matchType:2";
            break;

        default:
            query = "*";
            break;
        }

        try
        {
            _matchmakerTicket = await _socket.AddMatchmakerAsync(query, minCount, maxCount, null, numericProperties);
        }
        catch (Exception)
        {
            return(false);
        }

        return(true);
    }
示例#7
0
        public async void Search(string query, int minCount, int maxCount)
        {
            ServerConnection.Instance.Socket.ReceivedMatchmakerMatched += async matched => {
                var match = await ServerConnection.Instance.Socket.JoinMatchAsync(matched);

                Match = match;
#if UNITY_EDITOR
                Debug.LogFormat("Received: {0}", matched);
                Debug.LogFormat("Self: {0}", match.Self);
                Debug.LogFormat("Presences: {0}", match.Presences.ToJson());
#endif
                foreach (var u in matched.Users)
                {
                    if (u.Presence.UserId != GlobalModel.Me.User.Id)
                    {
                        GlobalModel.Opponent = u.Presence;
                    }
                }

                ServerConnection.Instance.Socket.ReceivedMatchState += GameController.RecieveState;

                if (string.Compare(GlobalModel.Me?.User.Id, GlobalModel.Opponent?.UserId) > 0)
                {
                    int myChar = new System.Random().Next(2);
                    GlobalModel.SetMyCharacter(myChar);
                    GameController.SendState(GameController.ACTION_PICKED_CHARACTER, myChar.ToJson());
                }
                IsMatchReady = true;
            };

            ServerConnection.Instance.Socket.ReceivedMatchPresence += presenceEvent => {
                if (presenceEvent.Leaves != null && (presenceEvent.Leaves as IList).Count != 0)
                {
                    Debug.Log("Opponent Left the game...");
                    Instance.Socket.LeaveMatchAsync(Match);
                    CancelSearch();
                    GlobalModel.ResetGameFlags();
                }
            };

            matchticket = await ServerConnection.Instance.Socket.AddMatchmakerAsync(query, minCount, maxCount);
        }
    // +++ unity callbacks ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    async void Start()
    {
        Init();

        // create client
        _client   = new Client(_key, _host, 7350, false);
        _playerId = _userManager.GetPlayerId();

        // create session
        _session = await _client.AuthenticateDeviceAsync(_playerId);

        // Init List of connected users
        _connectedUsers = new List <IUserPresence>(0);

        // create socket
        _socket = _client.CreateWebSocket();

        // subscribe to socket events
        _socket.OnMatchmakerMatched += OnMatchmakerMatched;
        _socket.OnConnect           += OnConnect;
        _socket.OnDisconnect        += OnDisconnect;
        _socket.OnMatchPresence     += OnMatchPresence;
        _socket.OnMatchState        += OnMatchState;

        // wait for socket connection
        await _socket.ConnectAsync(_session);

        Debug.Log("Socket connected");

        // wait for match maker ticket
        _matchMakerTicket = await _socket.AddMatchmakerAsync(
            "*",
            NUMPLAYERS,
            NUMPLAYERS);

        Debug.Log("Matchmaker ticket received");

        // wait for 2 players to connect
        StartCoroutine(WaitForNumberOfPlayers(NUMPLAYERS));
    }
示例#9
0
        /// <summary>
        /// Joins matchmaker queue and shows this panel.
        /// </summary>
        public async override void Show(bool isMuteButtonClick = false)
        {
            _connection.Socket.ReceivedMatchmakerMatched += OnMatchmakerMatched;

            // Join the matchmaker
            try
            {
                // Acquires matchmaking ticket used to join a match
                _ticket = await _connection.Socket.AddMatchmakerAsync(
                    query : "*",
                    minCount : 2,
                    maxCount : 2,
                    stringProperties : null,
                    numericProperties : null);
            }
            catch (Exception e)
            {
                Debug.LogWarning("An error has occured while joining the matchmaker: " + e);
            }

            base.Show(isMuteButtonClick);
        }
示例#10
0
    public async Task CreateMatchMakerMatchAsync()
    {
        // Init List of connected users
        _connectedUsers = new List <IUserPresence>(0);

        // create socket
        _socket = _client.CreateWebSocket();

        _socket.OnMatchmakerMatched += OnMatchmakerMatched;
        _socket.OnConnect           += OnConnect;
        _socket.OnDisconnect        += OnDisconnect;
        _socket.OnMatchPresence     += OnMatchPresence;
        _socket.OnMatchState        += OnMatchState;

        // wait for socket connection
        await _socket.ConnectAsync(_session);

        // MatchMaker Parameters
        var query = "*";

        _matchMakerTicket = await _socket.AddMatchmakerAsync(query, _minPlayers, _maxPlayers);
    }
示例#11
0
 /// <inheritdoc cref="RemoveMatchmakerAsync(Nakama.IMatchmakerTicket)"/>
 public Task RemoveMatchmakerAsync(IMatchmakerTicket ticket) => RemoveMatchmakerAsync(ticket.Ticket);
示例#12
0
 /// <inheritdoc />
 public async Task RemoveMatchmakerAsync(IMatchmakerTicket matchmakerTicket) =>
 await RemoveMatchmakerAsync(matchmakerTicket.Ticket);