示例#1
0
        public override ErrorCode TryGetRandomGame(JoinRandomGameRequest joinRequest, ILobbyPeer peer, out GameState gameState, out string message)
        {
            message = null;

            if (this.gameDict.Count == 0)
            {
                gameState = null;
                return ErrorCode.NoMatchFound;
            }

            LinkedListNode<GameState> startNode;
            switch ((JoinRandomType)joinRequest.JoinRandomType)
            {
                default:
                case JoinRandomType.FillRoom:
                    startNode = this.gameDict.First;
                    break;

                case JoinRandomType.SerialMatching:
                    startNode = this.nextJoinRandomStartNode ?? this.gameDict.First;
                    break;

                case JoinRandomType.RandomMatching:
                    var startNodeIndex = this.rnd.Next(this.gameDict.Count);
                    startNode = this.gameDict.GetAtIndex(startNodeIndex);
                    break;
            }

            if (!TryGetRandomGame(startNode, joinRequest, peer, out gameState))
            {
                return ErrorCode.NoMatchFound;
            }

            return ErrorCode.Ok;
        }
示例#2
0
        public override ErrorCode TryGetRandomGame(JoinRandomGameRequest joinRequest, ILobbyPeer peer, out GameState gameState, out string message)
        {
            message = null;

            if (this.gameDict.Count == 0)
            {
                gameState = null;
                return ErrorCode.NoMatchFound;
            }

            if (string.IsNullOrEmpty(joinRequest.QueryData))
            {
                var node = this.gameDict.First;
                while (node != null)
                {
                    gameState = node.Value;
                    if (gameState.IsJoinable)
                    {
                        if (!gameState.CheckUserIdOnJoin
                            || (!gameState.ContainsUser(peer.UserId)
                                && !gameState.IsUserInExcludeList(peer.UserId)
                                && gameState.CheckSlots(peer.UserId, joinRequest.AddUsers)))
                        {
                            return ErrorCode.Ok;
                        }
                    }

                    node = node.Next;
                }

                gameState = null;
                return ErrorCode.NoMatchFound;
            }

            string id;
            try
            {
                id = this.gameDatabase.FindMatch(joinRequest.QueryData);
            }
            catch (System.Data.Common.DbException sqlException)
            {
                gameState = null;
                message = sqlException.Message;
                return ErrorCode.OperationInvalid;
            }

            if (string.IsNullOrEmpty(id))
            {
                gameState = null;
                return ErrorCode.NoMatchFound;
            }

            if (!this.gameDict.TryGet(id, out gameState))
            {
                return ErrorCode.NoMatchFound;
            }

            return ErrorCode.Ok;
        }
示例#3
0
        private bool TryGetRandomGame(LinkedListNode<GameState> startNode, JoinRandomGameRequest joinRequest, ILobbyPeer peer, out GameState gameState)
        {
            var node = startNode;

            do
            {
                var game = node.Value;
                node = node.Next ?? this.gameDict.First;

                if (!game.IsOpen || !game.IsVisible || !game.HasBeenCreatedOnGameServer || (game.MaxPlayer > 0 && game.PlayerCount >= game.MaxPlayer))
                {
                    continue;
                }

                if (joinRequest.GameProperties != null && game.MatchGameProperties(joinRequest.GameProperties) == false)
                {
                    continue;
                }

                if (game.CheckUserIdOnJoin
                    && (game.ContainsUser(peer.UserId)
                    || game.IsUserInExcludeList(peer.UserId)
                    || !game.CheckSlots(peer.UserId, joinRequest.AddUsers)))
                {
                    continue;
                }

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("Found match. Next start node gameid={0}", node.Value.Id);
                }

                this.nextJoinRandomStartNode = node;
                gameState = game;
                return true;
            }
            while (node != startNode);

            gameState = null;
            return false;
        }
示例#4
0
        private void JoinRandom(Hashtable properties)
        {
            if (log.IsDebugEnabled)
            {
                log.Debug("MASTER: Joining random game ...");
            }

            var operation = new JoinRandomGameRequest { GameProperties = properties };
            var request = new OperationRequest((byte)Operations.OperationCode.JoinRandomGame, operation);

            this.masterClient.SendOperationRequest(request, new SendParameters());
        }
示例#5
0
        public override ErrorCode TryGetRandomGame(JoinRandomGameRequest joinRequest, ILobbyPeer peer, out GameState gameState, out string message)
        {
            message = null;

            foreach (GameState game in this.gameDict)
            {
                if (!game.IsOpen || !game.IsVisible || !game.HasBeenCreatedOnGameServer || (game.MaxPlayer > 0 && game.PlayerCount >= game.MaxPlayer))
                {
                    continue;
                }

                if (joinRequest.GameProperties != null && game.MatchGameProperties(joinRequest.GameProperties) == false)
                {
                    continue;
                }

                if (game.CheckUserIdOnJoin
                    && (game.ContainsUser(peer.UserId)
                    || game.IsUserInExcludeList(peer.UserId)
                    || !game.CheckSlots(peer.UserId, joinRequest.AddUsers)))
                {
                    continue;
                }

                gameState = game;
                return ErrorCode.Ok;
            }

            gameState = null;
            return ErrorCode.NoMatchFound;
        }
示例#6
0
 public abstract ErrorCode TryGetRandomGame(JoinRandomGameRequest joinRequest, ILobbyPeer peer, out GameState gameState, out string message);
示例#7
0
        protected virtual OperationResponse HandleJoinRandomGame(MasterClientPeer peer, OperationRequest operationRequest)
        {
            // validate the operation request
            var operation = new JoinRandomGameRequest(peer.Protocol, operationRequest);
            OperationResponse response;
            if (OperationHelper.ValidateOperation(operation, log, out response) == false)
            {
                return response;
            }

            // special handling for game properties send by AS3/Flash (Amf3 protocol) clients
            if (peer.Protocol.ProtocolType == ProtocolType.Amf3V152 || peer.Protocol.ProtocolType == ProtocolType.Json)
            {
                Utilities.ConvertAs3WellKnownPropertyKeys(operation.GameProperties, null);   
            }

            // try to find a match
            GameState game;
            string errorMessage;
            var result = this.GameList.TryGetRandomGame((JoinRandomType)operation.JoinRandomType, peer, operation.GameProperties, operation.QueryData, out game, out errorMessage);
            if (result != ErrorCode.Ok)
            {
                if (string.IsNullOrEmpty(errorMessage))
                {
                    errorMessage = "No match found";
                }

                response = new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (short)result, DebugMessage = errorMessage};
                return response;
            }

            // match found, add peer to game and notify the peer
            game.AddPeer(peer);

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Found match: connectionId={0}, userId={1}, gameId={2}", peer.ConnectionId, peer.UserId, game.Id);
            }

            this.ScheduleCheckJoinTimeOuts();

            object joinResponse = this.GetJoinRandomGameResponse(peer, game);
            return new OperationResponse(operationRequest.OperationCode, joinResponse);
        }
        public OperationResponse HandleJoinRandomGame(OperationRequest operationRequest, SendParameters sendParameters)
        {
            var joinRandomGameRequest = new JoinRandomGameRequest(this.Protocol, operationRequest);

            OperationResponse response;
            if (OperationHelper.ValidateOperation(joinRandomGameRequest, log, out response) == false)
            {
                return response;
            }

            AppLobby lobby;
            response = this.TryGetLobby(joinRandomGameRequest.LobbyName,
                joinRandomGameRequest.LobbyType, operationRequest.OperationCode, out lobby);
            if (response != null)
            {
                return response;
            }

            lobby.EnqueueOperation(this, operationRequest, sendParameters);
            return null;
        }
        public OperationResponse HandleJoinRandomGame(OperationRequest operationRequest, SendParameters sendParameters)
        {
            var joinRandomGameRequest = new JoinRandomGameRequest(this.Protocol, operationRequest);

            OperationResponse response;
            if (OperationHelper.ValidateOperation(joinRandomGameRequest, log, out response) == false)
            {
                return response;
            }

            if (string.IsNullOrEmpty(joinRandomGameRequest.LobbyName) && this.AppLobby != null)
            {
                this.AppLobby.EnqueueOperation(this, operationRequest, sendParameters);
                return null;
            }

            AppLobby lobby;
            if (!this.Application.LobbyFactory.GetOrCreateAppLobby(joinRandomGameRequest.LobbyName, (AppLobbyType)joinRandomGameRequest.LobbyType, out lobby))
            {
                return new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (int)ErrorCode.OperationDenied, DebugMessage = "Lobby does not exist" };
            }

            lobby.EnqueueOperation(this, operationRequest, sendParameters);
            return null;
        }
示例#10
0
        protected virtual OperationResponse HandleJoinRandomGame(MasterClientPeer peer, OperationRequest operationRequest)
        {
            // validate the operation request
            var operation = new JoinRandomGameRequest(peer.Protocol, operationRequest);
            OperationResponse response;
            if (OperationHelper.ValidateOperation(operation, log, out response) == false)
            {
                return response;
            }

            // Check if peer is a flash (amf3) client because flash clients does not support byte keys in a hastable.
            // If a flash client likes to match a game with a specific 'MaxPlayer' value 'MaxPlayer' will be sent
            // with the string key "255" and the max player value as int.
            if (peer.Protocol.ProtocolType == ProtocolType.Amf3V151 || peer.Protocol.ProtocolType == ProtocolType.Amf3V152)
            {
                if (operation.GameProperties != null && operation.GameProperties.Count > 0)
                {
                    if (operation.GameProperties.ContainsKey("255"))
                    {
                        var maxPlayer = (byte)(int)operation.GameProperties["255"];
                        operation.GameProperties.Remove("255");
                        operation.GameProperties.Add((byte)GameParameter.MaxPlayer, maxPlayer);
                    }
                }
            }

            // try to find a match
            GameState game;
            if (this.GameList.TryGetRandomGame(peer, operation.GameProperties, out game) == false)
            {
                response = new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (int)ErrorCode.NoMatchFound, DebugMessage = "No match found" };
                return response;
            }

            // match found, add peer to game and notify the peer
            game.AddPeer(peer);

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Found match: connectionId={0}, userId={1}, gameId={2}", peer.ConnectionId, peer.UserId, game.Id);
            }

            object joinResponse = this.GetJoinRandomGameResponse(peer, game);
            return new OperationResponse(operationRequest.OperationCode, joinResponse);
        }