Ejemplo n.º 1
0
        public ErrorCode TryGetRandomGame(JoinRandomType joinType, ILobbyPeer peer, Hashtable gameProperties, string query, out GameState gameState, out string message)
        {
            message = null;

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

            LinkedListNode <GameState> startNode;

            switch (joinType)
            {
            default:
                startNode = this.gameDict.First;
                break;

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

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

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

            return(ErrorCode.Ok);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///   Determines whether the specified peer is blocked by any peer already joined the game.
        /// </summary>
        /// <param name = "peer">The peer to check.</param>
        /// <returns>
        ///   <c>true</c> if the specified peer is blocked; otherwise, <c>false</c>.
        /// </returns>
        public bool IsBlocked(ILobbyPeer peer)
        {
            if (!string.IsNullOrEmpty(peer.UserId))
            {
                if (this.peerDict.ContainsKey(peer.UserId))
                {
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Peer with userId {0} already joined game with id {1}.", peer.UserId, this.Id);
                    }

                    return(true);
                }
            }

            foreach (PeerState peerState in this.peerDict.Values)
            {
                if (peer.BlockedUsers != null && peer.BlockedUsers.Contains(peerState.UserId))
                {
                    return(true);
                }

                if (peer.UserId != null && peerState.BlockedUsers != null && peerState.BlockedUsers.Contains(peer.UserId))
                {
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 3
0
        public void AddPeer(ILobbyPeer peer)
        {
            var peerState = new PeerState(peer);

            this.joiningPeers.AddLast(peerState);

            if (string.IsNullOrEmpty(peerState.UserId) == false)
            {
                this.userIdList.AddLast(peer.UserId);
            }

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Added peer: gameId={0}, userId={1}, joiningPeers={2}", this.Id, peer.UserId, this.joiningPeers.Count);
            }

            // check if max player is reached and inform the game list
            if (this.MaxPlayer > 0 && this.PlayerCount >= this.MaxPlayer)
            {
                this.Lobby.GameList.OnMaxPlayerReached(this);
            }

            // update player state in the online players cache
            if (this.Lobby.Application.PlayerOnlineCache != null && string.IsNullOrEmpty(peer.UserId) == false)
            {
                this.Lobby.Application.PlayerOnlineCache.OnJoinedGamed(peer.UserId, this);
            }
        }
Ejemplo n.º 4
0
        public bool TryGetRandomGame(JoinRandomType joinType, ILobbyPeer peer, Hashtable gameProperties, out GameState gameState)
        {
            if (this.gameDict.Count == 0)
            {
                gameState = null;
                return(false);
            }

            LinkedListNode <GameState> startNode;

            switch (joinType)
            {
            default:
                startNode = this.gameDict.First;
                break;

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

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

            return(this.TryGetRandomGame(startNode, peer, gameProperties, out gameState));
        }
Ejemplo n.º 5
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;
        }
Ejemplo n.º 6
0
        public string GetServerAddress(ILobbyPeer peer)
        {
            string address;

            // savedgames-poc:
            if (this.gameServer == null)
            {
                IncomingGameServerPeer newGameServer;
                if (!this.Lobby.Application.LoadBalancer.TryGetServer(out newGameServer))
                {
                    throw new Exception("Failed to get server instance.");
                }
                this.gameServer = newGameServer;
                log.DebugFormat("GetServerAddress: game={0} got new host GS={1}", this.Id, this.gameServer.Key);
            }

            var useHostnames = peer.UseHostnames; // || config settimg ForceHostnames

            var useIPv4 = peer.LocalIPAddress.AddressFamily == AddressFamily.InterNetwork;

            switch (peer.NetworkProtocol)
            {
            case NetworkProtocolType.Udp:
                address = useHostnames ? this.GameServer.UdpHostname : (useIPv4 ? this.GameServer.UdpAddress : this.GameServer.UdpAddressIPv6);
                break;

            case NetworkProtocolType.Tcp:
                address = useHostnames ? this.GameServer.TcpHostname : (useIPv4 ? this.GameServer.TcpAddress : this.GameServer.TcpAddressIPv6);
                break;

            case NetworkProtocolType.WebSocket:
                address = useHostnames ? this.GameServer.WebSocketHostname : (useIPv4 ? this.GameServer.WebSocketAddress : this.GameServer.WebSocketAddressIPv6);
                break;

            case NetworkProtocolType.SecureWebSocket:
                address = this.GameServer.SecureWebSocketHostname;
                break;

            case NetworkProtocolType.Http:
                if (peer.LocalPort == 443)     // https:
                {
                    address = this.gameServer.SecureHttpHostname;
                }
                else     // http:
                {
                    address = useIPv4 ? this.gameServer.HttpAddress : this.gameServer.HttpAddressIPv6;
                }
                break;

            default:
                throw new NotSupportedException(string.Format("No GS address configured for Protocol {0} (Peer Type: {1})", peer.NetworkProtocol, ((PeerBase)peer).NetworkProtocol));
            }
            if (string.IsNullOrEmpty(address))
            {
                throw new NotSupportedException(
                          string.Format("No GS address configured for Protocol {0} (Peer Type: {1}, AddressFamily: {2})", peer.NetworkProtocol, peer.NetworkProtocol, peer.LocalIPAddress.AddressFamily));
            }
            return(address);
        }
Ejemplo n.º 7
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;
        }
Ejemplo n.º 8
0
        public void AddPeer(ILobbyPeer peer)
        {
            if (this.ContainsUser(peer.UserId))
            {
                return;
            }
            var peerState = new PeerState(peer);

            this.AddPeerState(peerState);
        }
Ejemplo n.º 9
0
        public ErrorCode TryGetRandomGame(JoinRandomType joinType, ILobbyPeer peer, Hashtable gameProperties, string query, out GameState gameState, out string message)
        {
            message = null;

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

            if (string.IsNullOrEmpty(query))
            {
                var node = this.gameDict.First;
                while (node != null)
                {
                    gameState = node.Value;
                    if (this.IsGameJoinable(gameState))
                    {
                        return(ErrorCode.Ok);
                    }

                    node = node.Next;
                }

                gameState = null;
                return(ErrorCode.NoMatchFound);
            }

            string id;

            try
            {
                id = this.gameDatabase.FindMatch(query);
            }
            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);
        }
Ejemplo n.º 10
0
        public void AddPeer(ILobbyPeer peer)
        {
            this.ExpectsReplication = false;

            if (this.ContainsUser(peer.UserId))
            {
                return;
            }

            var peerState = new PeerState(peer);

            this.AddPeerState(peerState);
        }
Ejemplo n.º 11
0
        public bool TryAddPeer(ILobbyPeer peer)
        {
            // for non anonymous peers check if the peer already
            // has been added to the game
            if (string.IsNullOrEmpty(peer.UserId) == false)
            {
                if (this.peerDict.ContainsKey(peer.UserId))
                {
                    return(false);
                }
            }

            this.AddPeer(peer);
            return(true);
        }
Ejemplo n.º 12
0
        public void AddPeer(ILobbyPeer peer)
        {
            var peerState = new PeerState(peer);

            this.joiningPeers.AddLast(peerState);

            if (string.IsNullOrEmpty(peerState.UserId) == false)
            {
                this.peerDict.Add(peer.UserId, peerState);
            }

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Added peer: gameId={0}, userId={1}, joiningPeers={2}", this.Id, peer.UserId, this.joiningPeers.Count);
            }
        }
Ejemplo n.º 13
0
        public string GetServerAddress(ILobbyPeer peer)
        {
            switch (peer.NetworkProtocol)
            {
            case NetworkProtocolType.Udp:
                return(this.GameServer.UdpAddress);

            case NetworkProtocolType.Tcp:
                return(this.GameServer.TcpAddress);

            case NetworkProtocolType.WebSocket:
                return(this.GameServer.WebSocketAddress);

            default:
                return(null);
            }
        }
Ejemplo n.º 14
0
        protected static bool IsGameJoinable(JoinRandomGameRequest joinRequest, ILobbyPeer peer, GameState gameState)
        {
            if (!gameState.IsJoinable)
            {
                return(false);
            }

            if (!gameState.SupportsProtocol(peer.NetworkProtocol))
            {
                return(false);
            }

            return(!gameState.CheckUserIdOnJoin ||
                   (!gameState.ContainsUser(peer.UserId) &&
                    !gameState.IsUserInExcludeList(peer.UserId) &&
                    gameState.CheckSlots(peer.UserId, joinRequest.AddUsers)));
        }
Ejemplo n.º 15
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;
        }
Ejemplo n.º 16
0
        public ErrorCode TryGetRandomGame(JoinRandomType joinType, ILobbyPeer peer, Hashtable gameProperties, string query, out GameState gameState, out string message)
        {
            message = null;

            foreach (GameState game in this.GameDict.Values)
            {
                if (game.IsOpen && game.IsVisible && game.IsCreatedOnGameServer && (game.MaxPlayer <= 0 || game.PlayerCount < game.MaxPlayer))
                {
                    if (gameProperties != null && game.MatchGameProperties(gameProperties) == false)
                    {
                        continue;
                    }

                    gameState = game;
                    return(ErrorCode.Ok);
                }
            }

            gameState = null;
            return(ErrorCode.NoMatchFound);
        }
Ejemplo n.º 17
0
        private ErrorCode GetFirstJoinableGame(JoinRandomGameRequest joinRequest, ILobbyPeer peer, out GameState gameState)
        {
            var node = this.gameDict.First;

            while (node != null)
            {
                gameState = node.Value;
                if (IsGameJoinable(joinRequest, peer, gameState))
                {
                    log.Debug($"First Joinable Game '{gameState}', CheckUserOnJoin:{gameState.CheckUserIdOnJoin}, " +
                              $"UserLists: {gameState.GetUserListsAsString()} Properties:{ValueToString.ToString(gameState.ToHashTable())}" +
                              $"is joinable for request: {ValueToString.OperationToString(joinRequest.OperationRequest)}, Joining User:{peer.UserId}");
                    return(ErrorCode.Ok);
                }

                node = node.Next;
            }

            gameState = null;
            return(ErrorCode.NoMatchFound);
        }
Ejemplo n.º 18
0
        private bool TryGetRandomGame(LinkedListNode <GameState> startNode, ILobbyPeer peer, Hashtable gameProperties, out GameState gameState)
        {
            var node = startNode;

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

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

                if (game.IsBlocked(peer))
                {
                    continue;
                }

                if (gameProperties != null && game.MatchGameProperties(gameProperties) == false)
                {
                    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);
        }
Ejemplo n.º 19
0
        public bool TryGetRandomGame(JoinRandomType joinType, ILobbyPeer peer, Hashtable gameProperties, out GameState gameState)
        {
            foreach (GameState game in this.GameDict.Values)
            {
                if (game.IsOpen && game.IsVisible && game.IsCreatedOnGameServer && (game.MaxPlayer <= 0 || game.PlayerCount < game.MaxPlayer))
                {
                    if (game.IsBlocked(peer))
                    {
                        continue;
                    }

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

                    gameState = game;
                    return(true);
                }
            }

            gameState = null;
            return(false);
        }
Ejemplo n.º 20
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);
        }
Ejemplo n.º 21
0
        public ErrorCode TryGetRandomGame(JoinRandomType joinType, ILobbyPeer peer, Hashtable gameProperties, string query, out GameState gameState, out string message)
        {
            message = null;

            foreach (GameState game in this.GameDict.Values)
            {
                if (game.IsOpen && game.IsVisible && game.IsCreatedOnGameServer && (game.MaxPlayer <= 0 || game.PlayerCount < game.MaxPlayer))
                {
                    if (gameProperties != null && game.MatchGameProperties(gameProperties) == false)
                    {
                        continue;
                    }

                    gameState = game;
                    return ErrorCode.Ok;
                }
            }

            gameState = null;
            return ErrorCode.NoMatchFound;
        }
Ejemplo n.º 22
0
        public void AddPeer(ILobbyPeer peer)
        {
            var peerState = new PeerState(peer);

            this.joiningPeers.AddLast(peerState);

            if (string.IsNullOrEmpty(peerState.UserId) == false)
            {
                this.peerDict.Add(peer.UserId, peerState);
            }

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Added peer: gameId={0}, userId={1}, joiningPeers={2}", this.Id, peer.UserId, this.joiningPeers.Count);
            }
        }
Ejemplo n.º 23
0
        public void AddPeer(ILobbyPeer peer)
        {
            if (this.ContainsUser(peer.UserId))
            {
                return;
            }
            var peerState = new PeerState(peer);

            this.AddPeerState(peerState);
        }
Ejemplo n.º 24
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;
        }
Ejemplo n.º 25
0
        public string GetServerAddress(ILobbyPeer peer)
        {
            string address;

            Func <GameServerContext, bool> filter = ctx =>
            {
                if (ctx.SupportedProtocols == null)
                {
                    return(true);
                }
                return(ctx.SupportedProtocols.Contains((byte)peer.NetworkProtocol));
            };


            // savedgames-poc:
            if (this.gameServerContext == null)
            {
                // try to get a game server instance from the load balancer

                GameServerContext newGameServerContext;
                if (!this.Lobby.Application.LoadBalancer.TryGetServer(out newGameServerContext, filter))
                {
                    throw new Exception("Failed to get server instance.");
                }
                this.gameServerContext = newGameServerContext;
                log.DebugFormat("GetServerAddress: game={0} got new host GS={1}", this.Id, this.gameServerContext.Key);
            }
            else
            {
                if (!filter(this.gameServerContext))
                {
                    log.WarnFormat("Client tries to join game with protocol {0}, but gs does not support it", peer.NetworkProtocol);
                    throw new NotSupportedException(
                              string.Format("Failed to get GS for game. It is on GS which does not support protcol {0} ",
                                            peer.NetworkProtocol));
                }
            }

            var useHostnames = peer.UseHostnames; // || config settimg ForceHostnames

            var useIPv4  = peer.LocalIPAddress.AddressFamily == AddressFamily.InterNetwork;
            var addrInfo = this.gameServerContext.AddressInfo;

            switch (peer.NetworkProtocol)
            {
            case NetworkProtocolType.Udp:
                address = useHostnames ? addrInfo.UdpHostname : (useIPv4 ? addrInfo.UdpAddress : addrInfo.UdpAddressIPv6);
                break;

            case NetworkProtocolType.Tcp:
                address = useHostnames ? addrInfo.TcpHostname : (useIPv4 ? addrInfo.TcpAddress : addrInfo.TcpAddressIPv6);
                break;

            case NetworkProtocolType.WebSocket:
                address = useHostnames ? addrInfo.WebSocketHostname : (useIPv4 ? addrInfo.WebSocketAddress : addrInfo.WebSocketAddressIPv6);
                break;

            case NetworkProtocolType.SecureWebSocket:
                address = addrInfo.SecureWebSocketHostname;
                break;

            case NetworkProtocolType.Http:
                if (peer.LocalPort == 443)     // https:
                {
                    address = addrInfo.SecureHttpHostname;
                }
                else     // http:
                {
                    address = useIPv4 ? addrInfo.HttpAddress : addrInfo.HttpAddressIPv6;
                }
                break;

            case NetworkProtocolType.WebRTC:
                address = addrInfo.WebRTCAddress;
                break;

            default:
                throw new NotSupportedException(string.Format("No GS address configured for Protocol {0} (Peer Type: {1})", peer.NetworkProtocol, ((PeerBase)peer).NetworkProtocol));
            }
            if (string.IsNullOrEmpty(address))
            {
                throw new NotSupportedException(
                          string.Format("No GS address configured for Protocol {0} (Peer Type: {1}, AddressFamily: {2})", peer.NetworkProtocol, peer.NetworkProtocol, peer.LocalIPAddress.AddressFamily));
            }
            return(address);
        }
Ejemplo n.º 26
0
 public PeerState(ILobbyPeer peer)
 {
     this.UserId = peer.UserId ?? string.Empty;
     this.UtcCreated = DateTime.UtcNow;
 }
Ejemplo n.º 27
0
        public ErrorCode TryGetRandomGame(JoinRandomType joinType, ILobbyPeer peer, Hashtable gameProperties, string query, out GameState gameState, out string message)
        {
            message = null;

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

            LinkedListNode<GameState> startNode;
            switch (joinType)
            {
                default:
                    startNode = this.gameDict.First;
                    break;

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

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

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

            return ErrorCode.Ok;
        }
Ejemplo n.º 28
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);
        }
Ejemplo n.º 29
0
        public bool TryAddPeer(ILobbyPeer peer)
        {
            // for non anonymous peers check if the peer already
            // has been added to the game
            if (string.IsNullOrEmpty(peer.UserId) == false)
            {
                if (this.peerDict.ContainsKey(peer.UserId))
                {
                    return false;
                }
            }

            this.AddPeer(peer);
            return true;
        }
Ejemplo n.º 30
0
        public void AddPeer(ILobbyPeer peer)
        {
            var peerState = new PeerState(peer);

            this.joiningPeers.AddLast(peerState);
            
            if (string.IsNullOrEmpty(peerState.UserId) == false)
            {
                this.userIdList.AddLast(peer.UserId);
            }

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Added peer: gameId={0}, userId={1}, joiningPeers={2}", this.Id, peer.UserId, this.joiningPeers.Count);
            }

            // check if max player is reached and inform the game list
            if (this.MaxPlayer > 0 && this.PlayerCount >= this.MaxPlayer)
            {
                this.Lobby.GameList.OnMaxPlayerReached(this);
            }

            // update player state in the online players cache
            if (this.Lobby.Application.PlayerOnlineCache != null && string.IsNullOrEmpty(peer.UserId) == false)
            {
                this.Lobby.Application.PlayerOnlineCache.OnJoinedGamed(peer.UserId, this);
            }
        }
Ejemplo n.º 31
0
        public bool TryGetRandomGame(ILobbyPeer peer, Hashtable gameProperties, out GameState gameState)
        {
            foreach (GameState game in this.gameDict)
            {
                if (game.IsOpen && game.IsVisible && game.IsCreatedOnGameServer && (game.MaxPlayer <= 0 || game.PlayerCount < game.MaxPlayer))
                {
                    if (game.IsBlocked(peer))
                    {
                        continue;
                    }

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

                    gameState = game;
                    return true;
                }
            }

            gameState = null;
            return false;
        }
Ejemplo n.º 32
0
 public bool IsBlocked(ILobbyPeer lobbyPeer)
 {
     return(this.BlockedUsers != null && this.BlockedUsers.Contains(lobbyPeer.UserId));
 }
Ejemplo n.º 33
0
        public string GetServerAddress(ILobbyPeer peer)
        {
            string address;

            // savedgames-poc:
            if (this.gameServer == null)
            {
                IncomingGameServerPeer newGameServer;
                if (!this.Lobby.Application.LoadBalancer.TryGetServer(out newGameServer))
                {
                    throw new Exception("Failed to get server instance.");
                }
                this.gameServer = newGameServer;
                log.DebugFormat("GetServerAddress: game={0} got new host GS={1}", this.Id, this.gameServer.Key);
            }

            var useHostnames = peer.UseHostnames; // || config settimg ForceHostnames

            var useIPv4 = peer.LocalIPAddress.AddressFamily == AddressFamily.InterNetwork;
            switch (peer.NetworkProtocol)
            {
                case NetworkProtocolType.Udp:
                    address = useHostnames ? this.GameServer.UdpHostname : (useIPv4 ? this.GameServer.UdpAddress : this.GameServer.UdpAddressIPv6);
                    break;
                case NetworkProtocolType.Tcp:
                    address = useHostnames ? this.GameServer.TcpHostname : (useIPv4 ? this.GameServer.TcpAddress : this.GameServer.TcpAddressIPv6);
                    break;
                case NetworkProtocolType.WebSocket:
                    address = useHostnames ? this.GameServer.WebSocketHostname : (useIPv4 ? this.GameServer.WebSocketAddress : this.GameServer.WebSocketAddressIPv6);
                    break;
                case NetworkProtocolType.SecureWebSocket:
                    address = this.GameServer.SecureWebSocketHostname;
                    break;
                case NetworkProtocolType.Http:
                    if (peer.LocalPort == 443) // https:
                    {
                        address = this.gameServer.SecureHttpHostname;
                    }
                    else // http:
                    {
                        address = useIPv4 ? this.gameServer.HttpAddress : this.gameServer.HttpAddressIPv6;
                    }
                    break;
                default:
                    throw new NotSupportedException(string.Format("No GS address configured for Protocol {0} (Peer Type: {1})", peer.NetworkProtocol, ((PeerBase)peer).NetworkProtocol));
            }
            if (string.IsNullOrEmpty(address))
            {
                throw new NotSupportedException(
                    string.Format("No GS address configured for Protocol {0} (Peer Type: {1}, AddressFamily: {2})", peer.NetworkProtocol, peer.NetworkProtocol, peer.LocalIPAddress.AddressFamily));
            }
            return address;
        }
Ejemplo n.º 34
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);
        }
Ejemplo n.º 35
0
        public ErrorCode TryGetRandomGame(JoinRandomType joinType, ILobbyPeer peer, Hashtable gameProperties, string query, out GameState gameState, out string message)
        {
            message = null;

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

            if (string.IsNullOrEmpty(query))
            {
                var node = this.gameDict.First;
                while (node != null)
                {
                    gameState = node.Value;
                    if (this.IsGameJoinable(gameState))
                    {
                        return ErrorCode.Ok;
                    }

                    node = node.Next;
                }

                gameState = null;
                return ErrorCode.NoMatchFound;
            }

            string id;
            try
            {
                id = this.gameDatabase.FindMatch(query);
            }
            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;
        }
Ejemplo n.º 36
0
 public abstract ErrorCode TryGetRandomGame(JoinRandomGameRequest joinRequest, ILobbyPeer peer, out GameState gameState, out string message);
        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);
        }
Ejemplo n.º 38
0
        private ErrorCode TryGetRandomGame(string queryData, JoinRandomGameRequest joinRequest, ILobbyPeer peer, out GameState gameState, out string message)
        {
            message = null;

            int skipCount = 0;

            while (true)
            {
                string id;
                try
                {
                    id = this.gameDatabase.FindMatch(queryData, skipCount++);
                }
                catch (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);
                }

                if (IsGameJoinable(joinRequest, peer, gameState))
                {
                    if (log.IsDebugEnabled)
                    {
                        log.Debug($"Random Game '{gameState}', CheckUserOnJoin:{gameState.CheckUserIdOnJoin}, " +
                                  $"UserLists: {gameState.GetUserListsAsString()} Properties:{ValueToString.ToString(gameState.ToHashTable())}" +
                                  $"is joinable for request: {ValueToString.OperationToString(joinRequest.OperationRequest)}, Joining User:{peer.UserId}");
                    }
                    return(ErrorCode.Ok);
                }
            }
        }
Ejemplo n.º 39
0
 public PeerState(ILobbyPeer peer)
 {
     this.UserId     = peer.UserId ?? string.Empty;
     this.UtcCreated = DateTime.UtcNow;
 }
Ejemplo n.º 40
0
        public override ErrorCode TryGetRandomGame(JoinRandomGameRequest joinRequest, ILobbyPeer peer, out GameState gameState, out string message)
        {
            message   = null;
            gameState = null;

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

            if (string.IsNullOrEmpty(joinRequest.QueryData))
            {
                return(GetFirstJoinableGame(joinRequest, peer, out gameState));

//                var node = this.gameDict.First;
//                while (node != null)
//                {
//                    gameState = node.Value;
//                    if (IsGameJoinable(joinRequest, peer, gameState))
//                    {
//                        return ErrorCode.Ok;
//                    }
//
//                    node = node.Next;
//                }
//
////                gameState = null;
//                return ErrorCode.NoMatchFound;
            }

            if (!useStoredProcedures)
            {
                //remove last semicolon, otherwise we have another (empty) query after split
                if (joinRequest.QueryData.EndsWith(";"))
                {
                    joinRequest.QueryData = joinRequest.QueryData.Remove(joinRequest.QueryData.Length - 1);
                }

                var splitQueries = joinRequest.QueryData.Split(';');
                //create a setting for max numbers of queries?
                if (splitQueries.Length > 3)
                {
                    //create entry in ErrorMessages?
                    message = "Max queries allowed: 3";
                    return(ErrorCode.OperationInvalid);
                }

                foreach (var query in splitQueries)
                {
                    if (string.IsNullOrEmpty(query))
                    {
                        //error or ignore?
                        continue;

//                        message = "Query was empty";
//                        return ErrorCode.OperationInvalid;
                    }

                    var result = TryGetRandomGame(query, joinRequest, peer, out gameState, out message);
                    if (result != ErrorCode.NoMatchFound)
                    {
                        //match or error
                        return(result);
                    }
                }

                //no match, check options
//                return joinRequest.JoinRandomType == (byte)JoinRandomType.JoinRandomOnSqlNoMatch
//                    ? GetFirstJoinableGame(joinRequest, peer, out gameState)
//                    : ErrorCode.NoMatchFound;

                return(ErrorCode.NoMatchFound);

//                return TryGetRandomGame(joinRequest.QueryData, joinRequest, peer, out gameState, out message);
            }

            //obsolete soon
            //TODO const
            //TODO decide stored procedure prefix
            var spPrefix = "$SP.";

            var spName = joinRequest.QueryData;

            //temp, until the prefix is removed
            if (joinRequest.QueryData.StartsWith(spPrefix, StringComparison.InvariantCultureIgnoreCase))
            {
                spName = joinRequest.QueryData.Substring(spPrefix.Length).ToLower();
            }

            if (!storedProcedures.ContainsKey(spName) || string.IsNullOrEmpty(storedProcedures[spName]))
            {
                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("Stored procedure '{0}' not found", spName);
                }

                message = "Stored procedure not found";
                return(ErrorCode.OperationInvalid);
            }

            var queryData = storedProcedures[spName];

            //TODO const
            //TODO decide value
//            var authCookiePlaceholderPrefix = "$ac.";
            const char placeholderStart = '[';
            const char placeholderEnd   = ']';

            //replace placeholder values from AuthCookie
            //just call string.Replace for all values in the AuthCookie?
            var masterClientPeer = (MasterClientPeer)peer;

            queryData = ReplacePlaceholderIgnoreCase(queryData, masterClientPeer.AuthCookie);

//            if (queryData.IndexOf(authCookiePlaceholderPrefix, StringComparison.InvariantCultureIgnoreCase) >= 0)
            if (queryData.IndexOf(placeholderStart) >= 0 || queryData.IndexOf(placeholderEnd) >= 0)
            {
                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("Stored procedure '{0}' contains placeholder entries not found in AuthCookie", spName);
                }

                message = "Stored procedure contains placeholder entries not found in AuthCookie";
                return(ErrorCode.OperationInvalid);
            }

            //strip last semicolon in case one was added to end of stored procedure > already done when reading file
//            if (queryData.EndsWith(";"))
//            {
//                queryData = queryData.Substring(0, queryData.Length - 1);
//            }

            //TODO decide delimiter. using semicolon means that funnels can only be used with stored procedures (if client sends semicolon a NotAllowedSemicolonInQuereyData is returned)
            var queries = queryData.Split(';');

            var errorCode = ErrorCode.NoMatchFound;

            //TODO const
            //TODO max number of queries
            var limit = 10;

            for (int i = 0; i < queries.Length; i++)
            {
                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("Searching match, index {0}, query '{1}'", i, queries[i]);
                }

                errorCode = TryGetRandomGame(queries[i], joinRequest, peer, out gameState, out message);

                if (errorCode != ErrorCode.NoMatchFound || i >= limit - 1)
                {
                    break;
                }
            }

            return(errorCode);
        }
Ejemplo n.º 41
0
        /// <summary>
        ///   Determines whether the specified peer is blocked by any peer already joined the game.
        /// </summary>
        /// <param name = "peer">The peer to check.</param>
        /// <returns>
        ///   <c>true</c> if the specified peer is blocked; otherwise, <c>false</c>.
        /// </returns>
        public bool IsBlocked(ILobbyPeer peer)
        {
            if (!string.IsNullOrEmpty(peer.UserId))
            {
                if (this.peerDict.ContainsKey(peer.UserId))
                {
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Peer with userId {0} already joined game with id {1}.", peer.UserId, this.Id);
                    }

                    return true;
                }
            }

            foreach (PeerState peerState in this.peerDict.Values)
            {
                if (peer.BlockedUsers != null && peer.BlockedUsers.Contains(peerState.UserId))
                {
                    return true;
                }

                if (peer.UserId != null && peerState.BlockedUsers != null && peerState.BlockedUsers.Contains(peer.UserId))
                {
                    return true;
                }
            }

            return false;
        }
Ejemplo n.º 42
0
 public abstract ErrorCode TryGetRandomGame(JoinRandomGameRequest joinRequest, ILobbyPeer peer, out GameState gameState, out string message);
Ejemplo n.º 43
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 (!IsGameJoinable(joinRequest, peer, game))
                {
                    continue;
                }

                if (joinRequest.GameProperties != null && game.MatchGameProperties(joinRequest.GameProperties) == false)
                {
                    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);
        }
Ejemplo n.º 44
0
        public string GetServerAddress(ILobbyPeer peer)
        {
            switch (peer.NetworkProtocol)
            {
                case NetworkProtocolType.Udp:
                    return this.GameServer.UdpAddress;

                case NetworkProtocolType.Tcp:
                    return this.GameServer.TcpAddress;

                case NetworkProtocolType.WebSocket:
                    return this.GameServer.WebSocketAddress;

                default:
                    return null;
            }
        }
Ejemplo n.º 45
0
 public bool IsBlocked(ILobbyPeer lobbyPeer)
 {
     return this.BlockedUsers != null && this.BlockedUsers.Contains(lobbyPeer.UserId);
 }