Example #1
0
        public IActionResult OnGet(Guid?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Statuses = new List <SelectListItem>
            {
                new SelectListItem {
                    Text = "Active", Value = "0"
                },
                new SelectListItem {
                    Text = "Draft", Value = "2"
                },
                new SelectListItem {
                    Text = "Disabled", Value = "2"
                }
            };

            try
            {
                TimedMessage = _repository.Single(DataItemPolicy <Core.Data.TimedMessage> .ById(id.Value));
            }
            catch (Exception ex)
            {
                Log.Error($"TimedMessage.Details.OnGet(): {ex.Message} - {ex.StackTrace}");
            }

            if (TimedMessage == null)
            {
                return(NotFound());
            }
            return(Page());
        }
        public GameServer Get(int id)
        {
            _logger.LogInformation("Attempting for get server with ID {id}", id);
            var result = _repository.Single(DataItemPolicy <GameServer> .ById(id));

            return(result);
        }
Example #3
0
        public Task <CommandResponseGameServer> Handle(StartServerCommand request, CancellationToken cancellationToken)
        {
            _logger.LogDebug($"Running Handler RestartServerCommandHandler");
            var response   = new CommandResponseGameServer();
            var gameServer = _repository.Single(DataItemPolicy <GameServer> .ById(request.gameServerId));

            _repository.Single(DataItemPolicy <GameServerCurrentStats> .ById(request.gameServerId));
            if (gameServer == null)
            {
                response.status  = CommandResponseStatusEnum.Error;
                response.message = $"Unable located game server with ID {request.gameServerId}";
                return(Task.FromResult(response));
            }

            response.payload = gameServer;
            try
            {
                var proc = _procManager.StartServer(gameServer);
                response.status = CommandResponseStatusEnum.Success;
                gameServer.GameServerCurrentStats.Pid    = proc.Id;
                gameServer.GameServerCurrentStats.Status = ServerStatusStates.Running;
                _repository.Update(gameServer);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                response.status  = CommandResponseStatusEnum.Error;
                response.message = e.ToString();
                gameServer.GameServerCurrentStats.Status = ServerStatusStates.Error;
                _repository.Update(gameServer);
            }

            return(Task.FromResult(response));
        }
        public void Delete(int id)
        {
            var gameServer = _repository.Single(DataItemPolicy <GameServer> .ById(id));

            if (gameServer != null)
            {
                _serverManager.RemoveGameServer(gameServer);
            }
        }
        public IQueryProtocol GetQueryProtocol(GameServer gameServer)
        {
            var queryProto = _repository.Single(DataItemPolicy <GameProtocol> .ById(gameServer.Game.GameProtocolId));
            var queryType  = Type.GetType(queryProto.FullTypeName);

            if (queryType == null)
            {
                return(null);
            }

            return(Activator.CreateInstance(queryType,
                                            new IPEndPoint(IPAddress.Parse(gameServer.IpAddress), gameServer.QueryPort), _logger) as IQueryProtocol);
        }
Example #6
0
        public List <GameServer> GetGameServers()
        {
            _logger.LogDebug("Returning all game servers from GameServerProvider");
            var result = _repository.List(DataItemPolicy <GameServer> .All());

            // Force stats nav prop to be loaded
            foreach (var gameServer in result)
            {
                _repository.Single(DataItemPolicy <GameServerCurrentStats> .ById(gameServer.Id));
            }

            return(result);
        }
        public Task <CommandResponseGameServer> Handle(ProcessCrashedServerCommand request, CancellationToken cancellationToken)
        {
            _logger.LogDebug($"Running Handler ProcessCrashedServerCommandHandler");
            var response   = new CommandResponseGameServer();
            var gameServer = _repository.Single(DataItemPolicy <GameServer> .ById(request.gameServerId));

            _repository.Single(DataItemPolicy <GameServerCurrentStats> .ById(request.gameServerId));
            if (gameServer == null)
            {
                response.status  = CommandResponseStatusEnum.Error;
                response.message = $"Unable located game server with ID {request.gameServerId}";
                return(Task.FromResult(response));
            }

            response.payload = gameServer;

            if (gameServer.GameServerCurrentStats.Status != ServerStatusStates.Crashed)
            {
                _logger.LogDebug("Game server {id} has a PID set but is not running.  Marking as crashed", gameServer.Id);
                gameServer.GameServerCurrentStats.Status = ServerStatusStates.Crashed;
                _repository.Update(gameServer);
            }

            if (gameServer.GameServerCurrentStats.RestartAttempts < 3) // TODO: Move max restarts to config
            {
                gameServer.GameServerCurrentStats.RestartAttempts++;
                _logger.LogDebug("Attempt #{attempt} to restart server {id}", gameServer.GameServerCurrentStats.RestartAttempts, gameServer.Id);
                _mediator.Send(new RestartServerCommand(request.gameServerId));
                _mediator.Publish(new ServerCrashNotification(
                                      $"Server {gameServer.Id} has crashed.  Attempt {gameServer.GameServerCurrentStats.RestartAttempts} to restart the server"));
                _repository.Update(gameServer);
            }
            else
            {
                _logger.LogDebug("Server {id} has hit the max restart attempts.  Stopping server", gameServer.Id);
                gameServer.GameServerCurrentStats.Status = ServerStatusStates.Stopped;
                gameServer.GameServerCurrentStats.Pid    = null;
                _mediator.Send(new StopServerCommand(request.gameServerId));
                _repository.Update(gameServer);
                _mediator.Publish(new ServerCrashNotification(
                                      $"Server {gameServer.Id} has reached the max amount of restart attemps and will be stopped"));
            }

            return(Task.FromResult(response));
        }
        public Task <CommandResponseBase> Handle(UpdateServerStatusCommand request, CancellationToken cancellationToken)
        {
            var response   = new CommandResponseBase();
            var gameServer = _repository.Single(DataItemPolicy <GameServer> .ById(request.gameServerId));

            if (gameServer == null)
            {
                response.status  = CommandResponseStatusEnum.Error;
                response.message = $"Unable to locate game server with ID {request.gameServerId}";
                return(Task.FromResult(response));
            }

            gameServer.GameServerCurrentStats.Status = request.newState;
            _repository.Update(gameServer);

            response.status  = CommandResponseStatusEnum.Success;
            response.message = "Game server status updated";

            return(Task.FromResult(response));
        }
        public async Task <CommandResponseBase> Get(int id, string command)
        {
            _logger.LogInformation("Running GameServer action with ID {id} and action {action}", id, command);

            var gameServer = _repository.Single(DataItemPolicy <GameServer> .ById(id));

            if (gameServer != null)
            {
                switch (command.ToLower())
                {
                case "start":
                    return(await _mediator.Send(new StartServerCommand(id)));

                case "stop":
                    return(await _mediator.Send(new StopServerCommand(id)));

                case "restart":
                    return(await _mediator.Send(new RestartServerCommand(id)));

                case "update":
                    return(await _mediator.Send(new UpdateServerCommand(id)));

                default:
                    var result = new CommandResponseBase()
                    {
                        status  = CommandResponseStatusEnum.Error,
                        message = $"Unknown command {command}"
                    };
                    return(result);
                }
            }

            var errorResult = new CommandResponseBase()
            {
                status  = CommandResponseStatusEnum.Error,
                message = $"Unable to locate server with ID {id}"
            };

            return(errorResult);
        }
        public Task <CommandResponseGameServer> Handle(UpdateServerCommand request, CancellationToken cancellationToken)
        {
            var response   = new CommandResponseGameServer();
            var gameServer = _repository.Single(DataItemPolicy <GameServer> .ById(request.gameServerId));
            var game       = _repository.Single(DataItemPolicy <Game> .ById(gameServer.GameId));

            _repository.Single(DataItemPolicy <GameServerCurrentStats> .ById(request.gameServerId));
            if (gameServer == null)
            {
                response.status  = CommandResponseStatusEnum.Error;
                response.message = $"Unable located game server with ID {request.gameServerId}";
                return(Task.FromResult(response));
            }

            _serverManager.InstallGameServer(gameServer);
            gameServer.GameServerCurrentStats.Status = ServerStatusStates.Updating;
            _repository.Update(gameServer);
            response.status  = CommandResponseStatusEnum.Success;
            response.message = "Server is now updating";

            return(Task.FromResult(response));
        }
Example #11
0
        public IActionResult OnGet(Guid?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            try
            {
                TimedMessage = _repository.Single <Core.Data.TimedMessage>(DataItemPolicy <Core.Data.TimedMessage> .ById(id.Value));
            }
            catch (Exception ex)
            {
                Log.Error($"TimedMessage.Delete.OnGet(): {ex.Message} - {ex.StackTrace}");
            }

            if (TimedMessage == null)
            {
                return(NotFound());
            }
            return(Page());
        }
Example #12
0
        public IActionResult OnGet(Guid?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            try
            {
                ProjectText = _repository.Single <ProjectText>(DataItemPolicy <ProjectText> .ById(id.Value));
            }
            catch (Exception ex)
            {
                Log.Error($"Project.Edit.OnGet(): {ex.Message} - {ex.StackTrace}");
            }

            if (ProjectText == null)
            {
                return(NotFound());
            }

            return(Page());
        }
        public async Task <ServerStatsWrapper> Handle(QueryServerCommand request, CancellationToken cancellationToken)
        {
            _logger.LogDebug($"Running Handler QueryGameServerHandler");
            var statsWrapper = new ServerStatsWrapper()
            {
                gameServerId = request.gameServerId
            };
            var gameServer = _repository.Single(DataItemPolicy <GameServer> .ById(request.gameServerId));

            if (gameServer == null)
            {
                _logger.LogError($"Unable to located game server with ID {request.gameServerId}");
                return(statsWrapper);
            }

            var query = _gameQueryFactory.GetQueryProtocol(gameServer);

            statsWrapper.players = await query.GetServerPlayersAsync();

            statsWrapper.serverInfo = await query.GetServerInfoAsync();

            return(statsWrapper);
        }
Example #14
0
        public IActionResult OnPost(Guid?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            try
            {
                TimedMessage = _repository.Single <Core.Data.TimedMessage>(DataItemPolicy <Core.Data.TimedMessage> .ById(id.Value));

                if (TimedMessage != null)
                {
                    _repository.Remove(TimedMessage);
                }
            }
            catch (Exception ex)
            {
                Log.Error($"TimedMessage.Delete.OnPost(): {ex.Message} - {ex.StackTrace}");
            }

            return(RedirectToPage("/Index"));
        }
        public async Task Invoke(int gameServerId)
        {
            var gameServer = _repository.Single(DataItemPolicy <GameServer> .ById(gameServerId));

            _repository.List(GameServerConfigFilePolicy.ByServerId(gameServer.Id));
            foreach (var gameServerGameConfigFile in gameServer.GameConfigFiles)
            {
                _logger.LogDebug($"Processing config {gameServerGameConfigFile.FilePath} for game server {gameServer.Id}");
                var variables = ConfigFileUtils.GetVariablesFromGameServer(gameServer);
                if (File.Exists(Path.Combine(gameServer.HomeDirectory, gameServerGameConfigFile.FilePath)))
                {
                    _logger.LogDebug($"Found existing config ({gameServerGameConfigFile.FilePath})");
                    FileStream fileStream = new FileStream(Path.Combine(gameServer.HomeDirectory, gameServerGameConfigFile.FilePath), FileMode.Open);
                    using (StreamReader reader = new StreamReader(fileStream))
                    {
                        gameServerGameConfigFile.FileContent = reader.ReadToEnd();
                    }

                    _repository.Update(gameServerGameConfigFile);
                }

                var config = ConfigFileUtils.InterpolateConfigFromDict(variables, gameServerGameConfigFile.FileContent);
                try
                {
                    using (StreamWriter file =
                               new StreamWriter(Path.Combine(gameServer.HomeDirectory, gameServerGameConfigFile.FilePath)))
                    {
                        file.Write(config);
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError($"Failed to write {gameServerGameConfigFile.FilePath} for server {gameServer.Id}");
                }
            }
        }
        public Task <CommandResponseGameServer> Handle(CreateServerCommand request, CancellationToken cancellationToken)
        {
            _logger.LogDebug($"Running Handler CreateServerCommandHandler");
            var response   = new CommandResponseGameServer();
            var gameServer = request.gameServer;
            var game       = _repository.Single(DataItemPolicy <Game> .ById(gameServer.GameId));

            if (game == null)
            {
                response.status  = CommandResponseStatusEnum.Error;
                response.message = $"Unable to locate game with ID {gameServer.GameId}";
                _mediator.Publish(new ServerInstallStatusNotification("error", $"Unable to locate game with ID {gameServer.GameId}"));
                return(Task.FromResult(response));
            }

            var configFiles = _repository.List(GameDefaultConfigPolicy.ByGameId(game.Id));

            gameServer.GameConfigFiles = new List <GameServerConfigFile>();
            var variables = ConfigFileUtils.GetVariablesFromGameServer(gameServer);

            foreach (var configFile in configFiles)
            {
                gameServer.GameConfigFiles.Add(new GameServerConfigFile()
                {
                    Description           = configFile.Description,
                    FileContent           = configFile.Template,
                    FilePath              = configFile.FilePath,
                    GameServer            = gameServer,
                    GameDefaultConfigFile = configFile
                });
            }


            gameServer.GamePort               = _portProvider.GetNextAvailablePort(game.GamePort, gameServer.IpAddress, game.PortIncrement);
            gameServer.QueryPort              = _portProvider.GetNextAvailablePort(game.QueryPort, gameServer.IpAddress, game.PortIncrement);
            gameServer.HomeDirectory          = Path.Combine(_dirProvider.GetBaseInstallDirectory(), gameServer.Guid.ToString());
            gameServer.GameServerCurrentStats = new GameServerCurrentStats();

            gameServer.GameServerCurrentStats.Status = ServerStatusStates.Installing;
            try
            {
                _repository.Create(gameServer);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                response.status  = CommandResponseStatusEnum.Success;
                response.message = e.ToString();
                _mediator.Publish(new ServerInstallStatusNotification("error", e.ToString()));
                return(Task.FromResult(response));
            }

            try
            {
                _serverManager.InstallGameServer(gameServer);
                _mediator.Publish(new ServerInstallStatusNotification("installing", $"Game server install started for server {gameServer.Guid}"));
                response.status  = CommandResponseStatusEnum.Success;
                response.message = "Game server now installing";
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                response.status  = CommandResponseStatusEnum.Error;
                response.message = e.ToString();
                _mediator.Publish(new ServerInstallStatusNotification("error", e.ToString()));
            }



            return(Task.FromResult(response));
        }
Example #17
0
 private bool ProjectTextExists(Guid id)
 {
     return(_repository.List <ProjectText>(DataItemPolicy <Core.Data.ProjectText> .ById(id)).Any());
 }
Example #18
0
        public Game Get(int id)
        {
            Game result = _repository.Single(DataItemPolicy <Game> .ById(id));

            return(result);
        }
Example #19
0
 private bool TimedMessageExists(Guid id)
 {
     return(_repository.List <Core.Data.TimedMessage>(DataItemPolicy <Core.Data.TimedMessage> .ById(id)).Any());
 }