Exemplo n.º 1
0
        void LoadEntities(string worldId)
        {
            IBiomeRepository    biomeRepository    = new BiomeRepository(Path.Combine(ApplicationPaths.WorldsDirectory, worldId, "biomes.xml"));
            IBorderRepository   borderRepository   = new BorderRepository(Path.Combine(ApplicationPaths.WorldsDirectory, worldId, "borders.xml"));
            ICultureRepository  cultureRepository  = new CultureRepository(Path.Combine(ApplicationPaths.WorldsDirectory, worldId, "cultures.xml"));
            IFactionRepository  factionRepository  = new FactionRepository(Path.Combine(ApplicationPaths.WorldsDirectory, worldId, "factions.xml"));
            IFlagRepository     flagRepository     = new FlagRepository(Path.Combine(ApplicationPaths.WorldsDirectory, worldId, "flags.xml"));
            IHoldingRepository  holdingRepository  = new HoldingRepository(Path.Combine(ApplicationPaths.WorldsDirectory, worldId, "holdings.xml"));
            IProvinceRepository provinceRepository = new ProvinceRepository(Path.Combine(ApplicationPaths.WorldsDirectory, worldId, "provinces.xml"));
            IResourceRepository resourceRepository = new ResourceRepository(Path.Combine(ApplicationPaths.WorldsDirectory, worldId, "resources.xml"));
            IUnitRepository     unitRepository     = new UnitRepository(Path.Combine(ApplicationPaths.WorldsDirectory, worldId, "units.xml"));
            IWorldRepository    worldRepository    = new WorldRepository(ApplicationPaths.WorldsDirectory);

            IEnumerable <Biome>    biomeList    = biomeRepository.GetAll().ToDomainModels();
            IEnumerable <Border>   borderList   = borderRepository.GetAll().ToDomainModels();
            IEnumerable <Culture>  cultureList  = cultureRepository.GetAll().ToDomainModels();
            IEnumerable <Faction>  factionList  = factionRepository.GetAll().ToDomainModels();
            IEnumerable <Flag>     flagList     = flagRepository.GetAll().ToDomainModels();
            IEnumerable <Province> provinceList = provinceRepository.GetAll().ToDomainModels();
            IEnumerable <Resource> resourceList = resourceRepository.GetAll().ToDomainModels();
            IEnumerable <Unit>     unitList     = unitRepository.GetAll().ToDomainModels();

            armies    = new ConcurrentDictionary <Tuple <string, string>, Army>();
            biomes    = new ConcurrentDictionary <string, Biome>(biomeList.ToDictionary(biome => biome.Id, biome => biome));
            borders   = new ConcurrentDictionary <Tuple <string, string>, Border>(borderList.ToDictionary(border => new Tuple <string, string>(border.SourceProvinceId, border.TargetProvinceId), border => border));
            cultures  = new ConcurrentDictionary <string, Culture>(cultureList.ToDictionary(culture => culture.Id, culture => culture));
            factions  = new ConcurrentDictionary <string, Faction>(factionList.ToDictionary(faction => faction.Id, faction => faction));
            flags     = new ConcurrentDictionary <string, Flag>(flagList.ToDictionary(flag => flag.Id, flag => flag));
            holdings  = new ConcurrentDictionary <string, Holding>();
            provinces = new ConcurrentDictionary <string, Province>(provinceList.ToDictionary(province => province.Id, province => province));
            relations = new ConcurrentDictionary <Tuple <string, string>, Relation>();
            resources = new ConcurrentDictionary <string, Resource>(resourceList.ToDictionary(resource => resource.Id, resource => resource));
            units     = new ConcurrentDictionary <string, Unit>(unitList.ToDictionary(unit => unit.Id, unit => unit));
            world     = worldRepository.Get(worldId).ToDomainModel();
        }
Exemplo n.º 2
0
        public void CanChangeReputation()
        {
            PlayerService.Create("UnitTest");

            const string FACTION_NAME = "Coalition of Systems";

            FactionRepository.Create(FACTION_NAME, 1);

            var operationResult = PlayerService.ChangeFactionStanding("UnitTest", FACTION_NAME, 5);

            Assert.IsTrue(operationResult.IsSuccessful, $"Could not change faction standing.  Reason: {operationResult.Message}");

            var existingPlayer = PlayerService.GetByName("UnitTest");

            Assert.IsTrue(existingPlayer.Reputations[0].Standing == 5);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds a certain amount of fame (or disrepute) for a player, to their relationship with a faction.
        /// </summary>
        /// <param name="playerName">Name of the player to change reputation for</param>
        /// <param name="factionName">Faction with which reputation will be changed</param>
        /// <param name="amount">Amount by which reputation will change</param>
        public static OperationResult ChangeFactionStanding(string playerName, string factionName, int amount)
        {
            var result = new OperationResult();

            var player = GetByName(playerName);

            if (player == null)
            {
                result.Message = $"Player {playerName} has no entry in the Penumbra database.";
                return(result);
            }

            var faction = FactionRepository.GetByName(factionName);

            if (faction == null)
            {
                result.Message = $"Faction {factionName} has no entry in the Penumbra database.";
                return(result);
            }

            var relevantReputation = player.Reputations.FirstOrDefault(r => r.Id == faction.Id);

            if (relevantReputation == null)
            {
                relevantReputation = new Reputation
                {
                    PlayerId  = player.Id,
                    FactionId = faction.Id,
                    Standing  = amount
                };

                ReputationRepository.Create(relevantReputation);
            }
            else
            {
                relevantReputation.Standing += amount;

                ReputationRepository.Update(relevantReputation);
            }

            return(result);
        }
Exemplo n.º 4
0
        public static void LoadSelf(FactionRepository dbRepository)
        {
            bool import;

            try
            {
                Language language = dbRepository.GetLanguage(Settings.LanguageName);
                Settings.LanguageId = language.Id;
                import = false;
            }
            catch
            {
                import = true;
            }

            if (import)
            {
                Language language = new Language();
                language.Name = Settings.LanguageName;
                dbRepository.Add(language);
                Settings.LanguageId = language.Id;
            }
        }
Exemplo n.º 5
0
        public static void Main(string[] args)
        {
            FactionSettings factionSettings  = Utility.GetConfiguration();
            string          connectionString = $"Host={factionSettings.POSTGRES_HOST};Database={factionSettings.POSTGRES_DATABASE};Username={factionSettings.POSTGRES_USERNAME};Password={factionSettings.POSTGRES_PASSWORD}";

            var host = new HostBuilder()
                       .ConfigureAppConfiguration((hostingContext, config) =>
            {
                // config.AddJsonFile("appsettings.json", optional: true);
            })
                       .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                logging.AddConsole();
                logging.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.Warning);
            })
                       .ConfigureServices((hostContext, services) =>
            {
                services.AddEntityFrameworkNpgsql().AddDbContext <FactionDbContext>(options =>
                                                                                    options.UseNpgsql(connectionString)
                                                                                    );

                // Open a connection to RabbitMQ and register it with DI
                services.AddSingleton <IRabbitMQPersistentConnection>(options =>
                {
                    var factory = new ConnectionFactory()
                    {
                        HostName = factionSettings.RABBIT_HOST,
                        UserName = factionSettings.RABBIT_USERNAME,
                        Password = factionSettings.RABBIT_PASSWORD
                    };
                    return(new DefaultRabbitMQPersistentConnection(factory));
                });

                services.AddSingleton <FactionRepository>();

                // Register the RabbitMQ EventBus with all the supporting Services (Event Handlers) with DI
                RegisterEventBus(services);

                // Configure the above registered EventBus with all the Event to EventHandler mappings
                ConfigureEventBus(services);

                // Ensure the DB is initalized and seeding data
                // SeedData(services);
                bool dbLoaded = false;
                Console.WriteLine("Checking if database is ready");
                using (var context = new FactionDbContext())
                {
                    while (!dbLoaded)
                    {
                        try {
                            var language = context.Language.CountAsync();
                            language.Wait();
                            dbLoaded = true;
                            Console.WriteLine("Database is ready");
                        }
                        catch {
                            Console.WriteLine("Database not ready, waiting for 5 seconds");
                            Task.Delay(5000).Wait();
                        }
                    }
                }

                var sp = services.BuildServiceProvider();
                FactionRepository dbRepository = sp.GetService <FactionRepository>();

                Loader.LoadSelf(dbRepository);
                Loader.LoadAgents(dbRepository);
                Loader.LoadModules(dbRepository);
            })
                       .Build();;

            host.Start();
        }
 public NewAgentCheckinEventHandler(IEventBus eventBus, FactionRepository taskRepository)
 {
     _eventBus       = eventBus; // Inject the EventBus into this Handler to Publish a message, insert AppDbContext here for DB Access
     _taskRepository = taskRepository;
 }
Exemplo n.º 7
0
        public static void LoadModules(FactionRepository dbRepository)
        {
            string[] files = Directory.GetFiles(Settings.ModulesPath, Settings.ModuleConfigName, SearchOption.AllDirectories);
            foreach (string file in files)
            {
                bool   import;
                string contents = File.ReadAllText(file);
                JsonSerializerSettings settings = new JsonSerializerSettings
                {
                    NullValueHandling     = NullValueHandling.Include,
                    MissingMemberHandling = MissingMemberHandling.Ignore
                };
                ModuleConfig moduleConfig = JsonConvert.DeserializeObject <ModuleConfig>(contents);
                try
                {
                    Module module = dbRepository.GetModule(moduleConfig.Name, Settings.LanguageName);
                    import = false;
                }
                catch
                {
                    import = true;
                }
                if (import)
                {
                    Module module = new Module();
                    module.Name          = moduleConfig.Name;
                    module.Description   = moduleConfig.Description;
                    module.Authors       = String.Join(", ", moduleConfig.Authors.ToArray());
                    module.BuildLocation = moduleConfig.BuildLocation;
                    module.BuildCommand  = moduleConfig.BuildCommand;
                    module.LanguageId    = Settings.LanguageId;
                    dbRepository.Add(module);

                    foreach (CommandConfig commandConfig in moduleConfig.Commands)
                    {
                        Command command = new Command();
                        command.Name           = commandConfig.Name;
                        command.Description    = commandConfig.Description;
                        command.Help           = commandConfig.Help;
                        command.MitreReference = commandConfig.MitreReference;
                        command.OpsecSafe      = commandConfig.OpsecSafe;
                        command.ModuleId       = module.Id;
                        if (commandConfig.Artifacts.Count > 0)
                        {
                            command.Artifacts = String.Join(",", commandConfig.Artifacts.ToArray());
                        }
                        dbRepository.Add(command);
                        foreach (CommandParameterConfig paramConfig in commandConfig.Parameters)
                        {
                            CommandParameter param = new CommandParameter();
                            param.Name      = paramConfig.Name;
                            param.CommandId = command.Id;
                            param.Help      = paramConfig.Help;
                            param.Required  = paramConfig.Required;
                            param.Position  = paramConfig.Position;
                            param.Values    = String.Join(",", paramConfig.Values.ToArray());
                            dbRepository.Add(param);
                        }
                    }
                }
            }
        }
Exemplo n.º 8
0
        public static void LoadAgents(FactionRepository dbRepository)
        {
            string[] files = Directory.GetFiles(Settings.AgentsPath, Settings.AgentConfigName, SearchOption.AllDirectories);
            foreach (string file in files)
            {
                string          contents        = File.ReadAllText(file);
                AgentTypeConfig agentTypeConfig = JsonConvert.DeserializeObject <AgentTypeConfig>(contents);
                AgentType       type            = dbRepository.GetAgentType(agentTypeConfig.Name);

                if (type == null)
                {
                    AgentType agentType = new AgentType();
                    agentType.Name          = agentTypeConfig.Name;
                    agentType.Guid          = agentTypeConfig.Guid;
                    agentType.Authors       = String.Join(", ", agentTypeConfig.Authors.ToArray());
                    agentType.LanguageId    = Settings.LanguageId;
                    agentType.BuildCommand  = agentTypeConfig.BuildCommand;
                    agentType.BuildLocation = agentTypeConfig.BuildLocation;
                    dbRepository.Add(agentType);

                    foreach (string value in agentTypeConfig.Architectures)
                    {
                        AgentTypeArchitecture agentTypeArchitecture = new AgentTypeArchitecture();
                        agentTypeArchitecture.Name        = value;
                        agentTypeArchitecture.AgentTypeId = agentType.Id;
                        dbRepository.Add(agentTypeArchitecture);
                    }

                    foreach (string value in agentTypeConfig.OperatingSystems)
                    {
                        AgentTypeOperatingSystem agentTypeOperatingSystem = new AgentTypeOperatingSystem();
                        agentTypeOperatingSystem.Name        = value;
                        agentTypeOperatingSystem.AgentTypeId = agentType.Id;
                        dbRepository.Add(agentTypeOperatingSystem);
                    }

                    foreach (string value in agentTypeConfig.Formats)
                    {
                        AgentTypeFormat agentTypeFormat = new AgentTypeFormat();
                        agentTypeFormat.Name        = value;
                        agentTypeFormat.AgentTypeId = agentType.Id;
                        dbRepository.Add(agentTypeFormat);
                    }

                    foreach (string value in agentTypeConfig.Versions)
                    {
                        AgentTypeVersion agentTypeVersion = new AgentTypeVersion();
                        agentTypeVersion.Name        = value;
                        agentTypeVersion.AgentTypeId = agentType.Id;
                        dbRepository.Add(agentTypeVersion);
                    }

                    foreach (string value in agentTypeConfig.Configurations)
                    {
                        AgentTypeConfiguration agentTypeConfiguration = new AgentTypeConfiguration();
                        agentTypeConfiguration.Name        = value;
                        agentTypeConfiguration.AgentTypeId = agentType.Id;
                        dbRepository.Add(agentTypeConfiguration);
                    }


                    foreach (AgentTransportConfig agentTransportConfig in agentTypeConfig.AgentTransportTypes)
                    {
                        AgentTransportType agentTransportType = new AgentTransportType();
                        agentTransportType.Name = agentTransportConfig.Name;
                        agentTransportType.TransportTypeGuid = agentTransportConfig.TransportTypeGuid;
                        agentTransportType.BuildCommand      = agentTransportConfig.BuildCommand;
                        agentTransportType.BuildLocation     = agentTransportConfig.BuildLocation;
                        agentTransportType.AgentTypeId       = agentType.Id;
                        dbRepository.Add(agentTransportType);
                    }

                    foreach (CommandConfig commandConfig in agentTypeConfig.Commands)
                    {
                        Command command = new Command();
                        command.Name           = commandConfig.Name;
                        command.Description    = commandConfig.Description;
                        command.Help           = commandConfig.Help;
                        command.MitreReference = commandConfig.MitreReference;
                        command.OpsecSafe      = commandConfig.OpsecSafe;
                        command.AgentTypeId    = agentType.Id;
                        if (commandConfig.Artifacts.Count > 0)
                        {
                            command.Artifacts = String.Join(",", commandConfig.Artifacts.ToArray());
                        }
                        dbRepository.Add(command);
                        foreach (CommandParameterConfig paramConfig in commandConfig.Parameters)
                        {
                            CommandParameter param = new CommandParameter();
                            param.Name      = paramConfig.Name;
                            param.CommandId = command.Id;
                            param.Help      = paramConfig.Help;
                            param.Required  = paramConfig.Required;
                            param.Position  = paramConfig.Position;
                            param.Values    = String.Join(",", paramConfig.Values.ToArray());
                            dbRepository.Add(param);
                        }
                    }
                }
            }
        }
Exemplo n.º 9
0
        private void PopulateToolsetViewModel()
        {
            ClearViewModelPopulation();

            using (GameModuleRepository repo = new GameModuleRepository())
            {
                ViewModel.ActiveModule = repo.GetModule();
            }

            using (ContentPackageResourceRepository repo = new ContentPackageResourceRepository())
            {
                ViewModel.TilesetSpriteSheetsList = repo.GetAllUIObjects(ContentPackageResourceTypeEnum.Tileset, false);
            }

            using (ItemRepository repo = new ItemRepository())
            {
                ViewModel.ItemList = repo.GetAllUIObjects();
            }

            using (ScriptRepository repo = new ScriptRepository())
            {
                ViewModel.ScriptList = repo.GetAllUIObjects();
            }

            using (GenderRepository repo = new GenderRepository())
            {
                ViewModel.GenderList = repo.GetAllUIObjects();
            }

            using (ConversationRepository repo = new ConversationRepository())
            {
                ViewModel.ConversationList = repo.GetAllUIObjects();
            }

            using (RaceRepository repo = new RaceRepository())
            {
                ViewModel.RaceList = repo.GetAllUIObjects();
            }

            using (FactionRepository repo = new FactionRepository())
            {
                ViewModel.FactionList = repo.GetAllUIObjects();
            }

            using (TilesetRepository repo = new TilesetRepository())
            {
                ViewModel.TilesetList = repo.GetAllUIObjects();
            }

            using (AbilityRepository repo = new AbilityRepository())
            {
                ViewModel.AbilityList = repo.GetAll();
            }

            using (SkillRepository repo = new SkillRepository())
            {
                ViewModel.SkillList = repo.GetAll();
            }

            using (LevelRequirementRepository repo = new LevelRequirementRepository())
            {
                ViewModel.LevelRequirementList = repo.GetAll();
            }
        }