Example #1
0
        private bool TryCreateGame(Operation operation, string gameId, Hashtable properties, out GameState gameState, out OperationResponse errorResponse)
        {
            // try to get a game server instance from the load balancer
            IncomingGameServerPeer gameServer;

            if (!this.LoadBalancer.TryGetServer(out gameServer))
            {
                errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                {
                    ReturnCode   = (int)ErrorCode.ServerFull,
                    DebugMessage = "Failed to get server instance."
                };
                gameState = null;
                return(false);
            }

            // try to create create new game state
            gameState = new GameState(gameId, (byte)this.MaxPlayersDefault, gameServer);
            if (properties != null)
            {
                bool   changed;
                string debugMessage;

                if (!gameState.TrySetProperties(properties, out changed, out debugMessage))
                {
                    errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                    {
                        ReturnCode   = (int)ErrorCode.OperationInvalid,
                        DebugMessage = debugMessage
                    };
                    return(false);
                }
            }

            this.GameList.AddGameState(gameState);
            this.SchedulePublishGameChanges();

            errorResponse = null;
            return(true);
        }
Example #2
0
        protected virtual bool TryCreateGame(JoinGameRequest operation, NetworkProtocolType expectedProtocol, bool createIfNotExists,
                                             out bool gameCreated, out GameState gameState, out OperationResponse errorResponse, Dictionary <string, object> authCookie)
        {
            var gameId     = operation.GameId;
            var properties = operation.GameProperties;

            gameState   = null;
            gameCreated = false;

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

            // try to get a game server instance from the load balancer
            GameServerContext gameServerContext;

            if (!this.Application.LoadBalancer.TryGetServer(out gameServerContext, filter))
            {
                errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                {
                    ReturnCode   = (short)ErrorCode.ServerFull,
                    DebugMessage = LBErrorMessages.FailedToGetServerInstance,
                };

                return(false);
            }

            ErrorCode errorCode;
            string    errorMsg;

            // try to create or get game state
            if (createIfNotExists)
            {
                gameCreated = this.Application.GetOrCreateGame(gameId, this, (byte)this.MaxPlayersDefault, gameServerContext, out gameState, out errorCode, out errorMsg);
                if (errorCode != ErrorCode.Ok)
                {
                    errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                    {
                        ReturnCode   = (short)errorCode,
                        DebugMessage = errorMsg,
                    };

                    return(false);
                }
            }
            else
            {
                if (!this.Application.TryCreateGame(gameId, this, (byte)this.MaxPlayersDefault, gameServerContext, out gameState, out errorCode, out errorMsg))
                {
                    errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                    {
                        ReturnCode   = (short)errorCode,
                        DebugMessage = errorMsg,
                    };

                    return(false);
                }

                gameCreated = true;
            }

            if (gameCreated)
            {
                gameState.CreateRequest = operation;
            }

            if (properties != null)
            {
                bool   changed;
                string debugMessage;

                if (!gameState.TrySetProperties(properties, out changed, out debugMessage))
                {
                    if (gameCreated)
                    {
                        this.Application.RemoveGameByName(gameId);
                    }

                    errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                    {
                        ReturnCode   = (short)ErrorCode.OperationInvalid,
                        DebugMessage = debugMessage
                    };
                    return(false);
                }
            }

            try
            {
                this.GameList.AddGameState(gameState, authCookie);
            }
            catch (Exception)
            {
                log.ErrorFormat("New game state:{0}", gameState.ToString());
                log.ErrorFormat("Request Params for new state:{0}", Newtonsoft.Json.JsonConvert.SerializeObject(operation, serializeSettings));
                log.ErrorFormat("CreateIfNotExists: {0}, GameCreated: {1}, Game Properties:{2}",
                                createIfNotExists, gameCreated, Newtonsoft.Json.JsonConvert.SerializeObject(properties, serializeSettings));

                GameState gameInApp;
                if (this.Application.TryGetGame(gameId, out gameInApp))
                {
                    log.ErrorFormat("Game state in app:{0}", gameInApp.ToString());
                    log.ErrorFormat("Request Params for Game in App:{0}",
                                    Newtonsoft.Json.JsonConvert.SerializeObject(gameInApp.CreateRequest, serializeSettings));
                }

                this.Application.RemoveGameByName(gameState.Id);
                gameCreated = false;

                GameState gameStateInList;
                if (this.GameList.TryGetGame(gameState.Id, out gameStateInList))
                {
                    log.ErrorFormat("Game state in list:{0}", gameStateInList.ToString());
                    log.ErrorFormat("Request Params for Game in list:{0}",
                                    Newtonsoft.Json.JsonConvert.SerializeObject(gameStateInList.CreateRequest, serializeSettings));
                }
                else
                {
                    log.ErrorFormat("Game state {0} not found in list", gameState.Id);
                }
                throw;
            }

            this.SchedulePublishGameChanges();

            errorResponse = null;
            return(true);
        }
Example #3
0
        private bool TryCreateGame(Operation operation, string gameId, bool createIfNotExists, Hashtable properties, out bool gameCreated, out GameState gameState, out OperationResponse errorResponse)
        {
            gameState = null;
            gameCreated = false;

            // try to get a game server instance from the load balancer            
            IncomingGameServerPeer gameServer;
            if (!this.Application.LoadBalancer.TryGetServer(out gameServer))
            {
                errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                    {
                        ReturnCode = (int)ErrorCode.ServerFull,
                        DebugMessage = "Failed to get server instance."
                    };

                return false;
            }

            // try to create or get game state
            if (createIfNotExists)
            {
                gameCreated = this.Application.GetOrCreateGame(gameId, this, this.GameList, (byte)this.MaxPlayersDefault, gameServer, out gameState);
            }
            else
            {
                if (!this.Application.TryCreateGame(gameId, this, this.GameList, (byte)this.MaxPlayersDefault, gameServer, out gameState))
                {
                    errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                    {
                        ReturnCode = (int)ErrorCode.GameIdAlreadyExists,
                        DebugMessage = "A game with the specified id already exist."
                    };

                    return false;
                }

                gameCreated = true;
            }
          

            if (properties != null)
            {
                bool changed;
                string debugMessage;

                if (!gameState.TrySetProperties(properties, out changed, out debugMessage))
                {
                    errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                        {
                            ReturnCode = (int)ErrorCode.OperationInvalid,
                            DebugMessage = debugMessage
                        };
                    return false;
                }
            }

            this.GameList.AddGameState(gameState);
            this.SchedulePublishGameChanges();

            errorResponse = null;
            return true;
        }
Example #4
0
        private bool TryCreateGame(Operation operation, string gameId, bool createIfNotExists, Hashtable properties, out bool gameCreated, out GameState gameState, out OperationResponse errorResponse)
        {
            gameState   = null;
            gameCreated = false;

            // try to get a game server instance from the load balancer
            IncomingGameServerPeer gameServer;

            if (!this.Application.LoadBalancer.TryGetServer(out gameServer))
            {
                errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                {
                    ReturnCode   = (int)ErrorCode.ServerFull,
                    DebugMessage = "Failed to get server instance."
                };

                return(false);
            }

            // try to create or get game state
            if (createIfNotExists)
            {
                gameCreated = this.Application.GetOrCreateGame(gameId, this, this.GameList, (byte)this.MaxPlayersDefault, gameServer, out gameState);
            }
            else
            {
                if (!this.Application.TryCreateGame(gameId, this, this.GameList, (byte)this.MaxPlayersDefault, gameServer, out gameState))
                {
                    errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                    {
                        ReturnCode   = (int)ErrorCode.GameIdAlreadyExists,
                        DebugMessage = "A game with the specified id already exist."
                    };

                    return(false);
                }

                gameCreated = true;
            }


            if (properties != null)
            {
                bool   changed;
                string debugMessage;

                if (!gameState.TrySetProperties(properties, out changed, out debugMessage))
                {
                    errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                    {
                        ReturnCode   = (int)ErrorCode.OperationInvalid,
                        DebugMessage = debugMessage
                    };
                    return(false);
                }
            }

            this.GameList.AddGameState(gameState);
            this.SchedulePublishGameChanges();

            errorResponse = null;
            return(true);
        }
Example #5
0
        private bool TryCreateGame(Operation operation, string gameId, Hashtable properties, out GameState gameState, out OperationResponse errorResponse)
        {
            // try to get a game server instance from the load balancer
            IncomingGameServerPeer gameServer;
            if (!this.LoadBalancer.TryGetServer(out gameServer))
            {
                errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                    {
                        ReturnCode = (int)ErrorCode.ServerFull,
                        DebugMessage = "Failed to get server instance."
                    };
                gameState = null;
                return false;
            }

            // try to create create new game state
            gameState = new GameState(gameId, (byte)this.MaxPlayersDefault, gameServer);
            if (properties != null)
            {
                bool changed;
                string debugMessage;

                if (!gameState.TrySetProperties(properties, out changed, out debugMessage))
                {
                    errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                        {
                            ReturnCode = (int)ErrorCode.OperationInvalid,
                            DebugMessage = debugMessage
                        };
                    return false;
                }
            }

            this.GameList.AddGameState(gameState);

            if (this.schedule == null)
            {
                this.schedule = this.ExecutionFiber.Schedule(this.PublishGameChanges, 1000);
            }

            errorResponse = null;
            return true;
        }
Example #6
0
        private bool TryCreateGame(Operation operation, string gameId, bool createIfNotExists, Hashtable properties, out bool gameCreated, out GameState gameState, out OperationResponse errorResponse)
        {
            gameState   = null;
            gameCreated = false;

            // try to get a game server instance from the load balancer
            IncomingGameServerPeer gameServer;

            if (!this.Application.LoadBalancer.TryGetServer(out gameServer))
            {
                errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                {
                    ReturnCode   = (short)ErrorCode.ServerFull,
                    DebugMessage = LBErrorMessages.FailedToGetServerInstance,
                };

                return(false);
            }

            // try to create or get game state
            if (createIfNotExists)
            {
                gameCreated = this.Application.GetOrCreateGame(gameId, this, (byte)this.MaxPlayersDefault, gameServer, out gameState);
            }
            else
            {
                if (!this.Application.TryCreateGame(gameId, this, (byte)this.MaxPlayersDefault, gameServer, out gameState))
                {
                    errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                    {
                        ReturnCode   = (short)ErrorCode.GameIdAlreadyExists,
                        DebugMessage = LBErrorMessages.GameAlreadyExist,
                    };

                    return(false);
                }

                gameCreated = true;
            }


            if (properties != null)
            {
                bool   changed;
                string debugMessage;

                if (!gameState.TrySetProperties(properties, out changed, out debugMessage))
                {
                    errorResponse = new OperationResponse(operation.OperationRequest.OperationCode)
                    {
                        ReturnCode   = (short)ErrorCode.OperationInvalid,
                        DebugMessage = debugMessage
                    };
                    return(false);
                }
            }

            try
            {
                this.GameList.AddGameState(gameState);
            }
            catch (Exception)
            {
                log.ErrorFormat("New game state:{0}", gameState.ToString());

                this.Application.RemoveGame(gameState.Id);
                gameCreated = false;

                GameState gameStateInList;
                if (this.GameList.TryGetGame(gameState.Id, out gameStateInList))
                {
                    log.ErrorFormat("Game state in list:{0}", gameStateInList.ToString());
                }
                else
                {
                    log.ErrorFormat("Game state {0} not found in list", gameState.Id);
                }
                throw;
            }

            this.SchedulePublishGameChanges();

            errorResponse = null;
            return(true);
        }