Example #1
0
        internal Daemon(FactoryPodConfiguration configuration, ILogger logger)
        {
            _configuration = configuration;
            _logger        = logger;

            _factorioProc = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName  = _configuration.Factorio.ServerPath,
                    Arguments = _configuration.Factorio.Arguments
                                .WithSaveFile(_configuration.Factorino.FactorySavePath)
                                .WithServerSettings(_configuration.Factorio.SettingsFilePath)
                                .WithFactorioConfig(_configuration.Factorio.ConfigFilePath)
                                .WithModsDirectory(_configuration.Factorio.ModsPath)
                                .WithRconSettings(_configuration.Factorio.Rcon)
                                .ToString(),
                    UseShellExecute        = _configuration.Factorio.UseShell,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                },
                EnableRaisingEvents = true,
            };

            _logger.Information($"Starting factorio using executable '{_factorioProc.StartInfo.FileName}', arguments: '{_factorioProc.StartInfo.Arguments}'");

            _factorioProc.OutputDataReceived += new DataReceivedEventHandler(FactorioOutputHandler);
            _factorioProc.ErrorDataReceived  += new DataReceivedEventHandler(FactorioErrorHandler);
            _factorioProc.Exited             += new EventHandler(FactorioProcessHandler);
        }
Example #2
0
        public Program()
        {
            var configuration = Configuration.GetConfiguration();

            _configuration = configuration.Bind <FactoryPodConfiguration>();
            _logger        = Logging.GetLogger(configuration);

            // Every pod needs a unique group id
            _configuration.Kafka.GroupId = $"{_configuration.Kafka.GroupId}-{_configuration.Factorino.FactoryId}";

            // Handle user exit (CTRL + C) gracefully
            Console.CancelKeyPress += new ConsoleCancelEventHandler((_, e) =>
            {
                // Prevent premature app termination
                e.Cancel = true;
                // Allow graceful exit
                _resetEvent.Set();
            });

            // Handle system exit gracefully
            AppDomain.CurrentDomain.ProcessExit += new EventHandler((_, e) => _resetEvent.Set());

            _daemon   = new Daemon(_configuration, _logger);
            _mediator = new Mediator(_configuration, _logger);
            _consumer = new KafkaConsumer(_configuration, _mediator, _logger);

            _daemon.OnRconReady  += (async(s, e) => await _mediator.Connect());
            _daemon.OnOutputData += (async(s, e) => await _mediator.LogOutput(e));
            _daemon.OnErrorData  += (async(s, e) => await _mediator.LogError(e));

            _mediator.OnDisconnect += (s, e) => _resetEvent.Set();
        }
Example #3
0
 internal Mediator(FactoryPodConfiguration configuration, ILogger logger)
 {
     _configuration = configuration;
     _logger        = logger;
     _pollTimer     = new Timer
     {
         Interval = POLL_INTERVAL_MS
     };
     _pollTimer.Elapsed += new ElapsedEventHandler(PollHandler);
     _producer           = new KafkaProducer(_configuration, _logger);
     _state              = new State();
 }
Example #4
0
        internal static IEnumerable <IEvent> TransformEvent(FactoryPodConfiguration config, PodEventDTO evnt)
        {
            var factoryId = config.Factorino.FactoryId;

            switch (evnt.Type)
            {
            case "on_built_entity":
                yield return(new FactoryBuiltEntityEvent(factoryId, evnt.Type, evnt.Tick)
                {
                    PlayerName = evnt.PlayerName,
                    Entity = evnt.Entity,
                });

                break;

            case "on_entity_died":
                yield return(new FactoryEntityDiedEvent(factoryId, evnt.Type, evnt.Tick)
                {
                    PlayerName = evnt.PlayerName,
                    Entity = evnt.Entity,
                });

                break;

            case "on_player_joined_game":
                yield return(new FactoryPlayerJoinedEvent(factoryId, evnt.Type, evnt.Tick)
                {
                    PlayerName = evnt.PlayerName,
                });

                break;

            case "on_player_mined_item":
                yield return(new FactoryPlayerMinedItemEvent(factoryId, evnt.Type, evnt.Tick)
                {
                    PlayerName = evnt.PlayerName,
                    ItemStack = evnt.ItemStack,
                });

                break;

            case "on_player_mined_entity":
                yield return(new FactoryPlayerMinedEntityEvent(factoryId, evnt.Type, evnt.Tick)
                {
                    PlayerName = evnt.PlayerName,
                    Entity = evnt.Entity,
                });

                break;

            case "on_player_died":
                yield return(new FactoryPlayerDiedEvent(factoryId, evnt.Type, evnt.Tick)
                {
                    PlayerName = evnt.PlayerName,
                });

                break;

            case "on_player_left_game":
                yield return(new FactoryPlayerLeftEvent(factoryId, evnt.Type, evnt.Tick)
                {
                    PlayerName = evnt.PlayerName,
                });

                break;

            case "on_console_chat":
                yield return(new FactoryChatEvent(factoryId, evnt.Type, evnt.Tick)
                {
                    PlayerName = evnt.PlayerName,
                    Message = evnt.Message,
                });

                break;

            case "on_research_started":
                yield return(new FactoryResearchStartedEvent(factoryId, evnt.Type, evnt.Tick)
                {
                    Technology = evnt.Technology,
                });

                break;

            case "on_research_finished":
                yield return(new FactoryResearchFinishedEvent(factoryId, evnt.Type, evnt.Tick)
                {
                    Technology = evnt.Technology,
                });

                break;

            case "on_rocket_launched":
                yield return(new FactoryRocketLaunchedEvent(factoryId, evnt.Type, evnt.Tick)
                {
                    Rocket = evnt.Entity,
                });

                break;

            case "factorino_outgoing_train":
                if (evnt.Inventory.Length > 0)
                {
                    yield return(new FactoryOutgoingTrainEvent(factoryId, evnt.Type, evnt.Tick)
                    {
                        TrainName = evnt.TrainName,
                        Inventory = evnt.Inventory.Select(i => (LuaItemStack)i).ToArray(),
                    });
                }
                if (Guid.TryParse(evnt.TrainName, out var shipmentId))
                {
                    yield return(new ShipmentCompletedEvent(shipmentId, config.Factorino.FactoryId, null)
                    {
                        ReturningCargo = evnt.Inventory.Select(i => (LuaItemStack)i).ToArray(),
                    });
                }
                break;

            default:
                throw new EventUnknownException($"Could not transform event with type {evnt.Type}!");
            }
        }
Example #5
0
 internal static IEvent[] TransformEvents(FactoryPodConfiguration config, params PodEventDTO[] events)
 {
     return(events.SelectMany(e => TransformEvent(config, e)).ToArray());
 }