示例#1
0
        public async Task Initialize()
        {
            context = await UtilityFactory.CreateContextAsync();

            countryService = new CountryService(UtilityFactory.CreateMapper(), context,
                                                new AsyncReaderWriterLock(), ModifierParserContainer.CreateDefault());
        }
示例#2
0
 public UnitService(UnderSeaDatabaseContext context,
                    ModifierParserContainer container, IMapper mapper)
 {
     Mapper  = mapper ?? throw new ArgumentNullException(nameof(mapper));
     Context = context ?? throw new ArgumentNullException(nameof(context));
     Parsers = container ?? throw new ArgumentNullException(nameof(container));
 }
示例#3
0
 public CountryService(IMapper mapper, UnderSeaDatabaseContext context, AsyncReaderWriterLock turnEndLock,
                       ModifierParserContainer parsers)
 {
     Mapper      = mapper ?? throw new ArgumentNullException(nameof(mapper));
     Context     = context ?? throw new ArgumentNullException(nameof(context));
     Parsers     = parsers ?? throw new ArgumentNullException(nameof(parsers));
     TurnEndLock = turnEndLock ?? throw new ArgumentNullException(nameof(turnEndLock));
 }
示例#4
0
        /// <summary>
        /// Parses all effects of a country into a <see cref="CountryModifierBuilder"/>.
        /// </summary>
        /// <param name="country">The country to parse effects for. Its buildings, researches, events and their effects must be included.</param>
        /// <param name="context">The database to use.</param>
        /// <param name="globals">The <see cref="GlobalValue"/> to use.</param>
        /// <param name="Parsers">The collection of parsers to use.</param>
        /// <param name="doApplyEvent">If random events should be applied to the modifier.</param>
        /// <param name="doApplyPermanent">If effects that have permanenet effects should be applied.</param>
        /// <returns>The builder containing the modifiers for the country</returns>
        public static CountryModifierBuilder ParseAllEffectForCountry(this Country country, UnderSeaDatabaseContext context,
                                                                      GlobalValue globals, ModifierParserContainer Parsers, bool doApplyEvent, bool doApplyPermanent)
        {
            if (country == null)
            {
                throw new ArgumentNullException(nameof(country));
            }

            // Set up builder
            var builder = new CountryModifierBuilder
            {
                BarrackSpace = globals.StartingBarrackSpace,
                Population   = globals.StartingPopulation
            };

            // First handle events
            if (doApplyEvent && country.CurrentEvent != null)
            {
                foreach (var e in country.CurrentEvent.Effects)
                {
                    if (!Parsers.TryParse(e.Effect, country, context, builder, doApplyPermanent))
                    {
                        Debug.WriteLine("Event effect with name {0} could not be handled by the provided parsers.", e.Effect.Name);
                    }
                }
            }

            // Then regular effects
            var effectparents = country.Buildings.Select(b => new
            {
                count   = b.Count,
                effects = b.Building.Effects.Select(e => e.Effect)
            }).Concat(country.Researches.Select(r => new
            {
                count   = r.Count,
                effects = r.Research.Effects.Select(e => e.Effect)
            })).ToList();

            foreach (var effectParent in effectparents)
            {
                for (int iii = 0; iii < effectParent.count; iii++)
                {
                    foreach (var e in effectParent.effects)
                    {
                        if (!Parsers.TryParse(e, country, context, builder, doApplyPermanent))
                        {
                            Debug.WriteLine("Effect with name {0} could not be handled by the provided parsers.", e.Name);
                        }
                    }
                }
            }

            return(builder);
        }
示例#5
0
 public CountryService(IMapper mapper, UnderSeaDatabaseContext context, ModifierParserContainer parsers)
 {
     Mapper  = mapper ?? throw new ArgumentNullException(nameof(mapper));
     Context = context ?? throw new ArgumentNullException(nameof(context));
     Parsers = parsers ?? throw new ArgumentNullException(nameof(parsers));
 }
示例#6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CountryTurnHandler"/>.
 /// </summary>
 /// <param name="parsers">The <see cref="ModifierParserContainer"/> containing effect parsers used by the handler.</param>
 public CountryTurnHandler(ModifierParserContainer parsers)
 {
     Parsers = parsers ?? throw new ArgumentNullException(nameof(parsers));
 }
示例#7
0
 public TurnHandlingService(ModifierParserContainer parsers)
 {
     Handler = new CountryTurnHandler(parsers ?? throw new ArgumentNullException(nameof(parsers)));
 }
        public async Task Initialize()
        {
            context = await UtilityFactory.CreateContextAsync();

            turnService = new TurnHandlingService(ModifierParserContainer.CreateDefault(), new AsyncReaderWriterLock());
        }
示例#9
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <PasswordHasherOptions>(options => options.IterationCount = 100000);
            services.Configure <IdentityOptions>(options =>
            {
                options.Password.RequireNonAlphanumeric = false;
            });

            services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection")));
            services.AddDbContext <UnderSeaDatabaseContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddDefaultIdentity <User>()
            .AddEntityFrameworkStores <UnderSeaDatabaseContext>();

            services.AddIdentityServer()
            .AddCorsPolicyService <IdentityServerCorsPolicyService>()
            .AddDeveloperSigningCredential()
            .AddInMemoryPersistedGrants()
            .AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources())
            .AddInMemoryApiResources(IdentityServerConfig.GetApiResources(Configuration))
            .AddInMemoryClients(IdentityServerConfig.GetClients(Configuration))
            .AddAspNetIdentity <User>();

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(options =>
            {
                options.Authority                 = Configuration["Authority"];
                options.Audience                  = Configuration["ApiName"];
                options.RequireHttpsMetadata      = false;
                options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
                {
                    NameClaimType = JwtClaimTypes.Name
                };
            });

            services.AddCors(options =>
            {
                options.AddDefaultPolicy(policy =>
                {
                    var allowedOrigins = Configuration.GetSection("CorsOrigins")
                                         .GetChildren()
                                         .Select(x => x.Value)
                                         .ToArray();

                    policy.WithOrigins(allowedOrigins)
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials();
                });
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddFluentValidation(options => options.RegisterValidatorsFromAssemblyContaining <PurchaseValidator>());

            services.AddSignalR();

            services.AddSwaggerDocument(options =>
            {
                options.Title       = "Undersea API";
                options.Version     = "1.0";
                options.Description = "API for the game called Under sea, which is a turn based online multiplayer strategy game.";

                options.PostProcess = document =>
                {
                    var settings = new TypeScriptClientGeneratorSettings
                    {
                        ClassName = "{controller}Client",
                        Template  = TypeScriptTemplate.Axios
                    };

                    var generator = new TypeScriptClientGenerator(document, settings);
                    var code      = generator.GenerateFile();

                    var path      = Directory.GetCurrentDirectory();
                    var directory = new DirectoryInfo(path + @"\TypeScript");
                    if (!directory.Exists)
                    {
                        directory.Create();
                    }

                    var filePath = path + @"\TypeScript\Client.ts";
                    File.WriteAllText(filePath, code);
                };
            });

            services.AddAutoMapper(typeof(KnownValues).Assembly);

            services.AddSingleton(ModifierParserContainer.CreateDefault());
            services.AddSingleton <IUserTracker, UserTracker>();
            services.AddSingleton <AsyncReaderWriterLock>();

            services.AddTransient <ITurnHandlingService, TurnHandlingService>();
            services.AddTransient <ICountryService, CountryService>();
            services.AddTransient <IResearchService, ResearchService>();
            services.AddTransient <IBuildingService, BuildingService>();
            services.AddTransient <IUnitService, UnitService>();
            services.AddTransient <ICommandService, CommandService>();
            services.AddTransient <IReportService, ReportService>();
            services.AddTransient <IDbLogger, DbLogger>();

            // User ID provider for SignalR Hub
            services.AddTransient <IUserIdProvider, UserIdProvider>();
        }
示例#10
0
 public TurnHandlingService(ModifierParserContainer parsers, AsyncReaderWriterLock turnEndLock)
 {
     Handler     = new CountryTurnHandler(parsers ?? throw new ArgumentNullException(nameof(parsers)));
     TurnEndLock = turnEndLock ?? throw new ArgumentNullException(nameof(turnEndLock));
 }