public bool BuyIt(GameUserVM vm, Building building)
        {
            bool result = true;

            this.ApplyResourceConfigurations(building);
            foreach (var resource in building.NextCost)
            {
                var realResource = vm.PrincipalPlanet.Resources.FirstOrDefault(x => x.Name.Equals(resource.Name));
                if (resource.LastQuantity > realResource.LastQuantity)
                {
                    result = false;
                }
            }

            if (result)
            {
                using (var db = new ASPNetOgameLikeTPContext())
                {
                    foreach (var resource in building.NextCost)
                    {
                        var realResource = vm.PrincipalPlanet.Resources.FirstOrDefault(x => x.Name.Equals(resource.Name));
                        realResource.LastQuantity = realResource.LastQuantity - resource.LastQuantity;
                        this.SaveResource(db, realResource);
                    }
                }
            }

            return(result);
        }
        private void SaveResource(ASPNetOgameLikeTPContext db, Resource resource)
        {
            resource.LastUpdate = DateTime.Now;

            db.Resources.Attach(resource);
            db.Entry(resource).State = EntityState.Modified;

            db.SaveChanges();
        }
 private void ApplyResourceConfigurations(Building building)
 {
     // Apply configuration to resource generator
     using (var db = new ASPNetOgameLikeTPContext())
     {
         var resPlanetConf = db.Configurations.Find(ConfigurationKeys.GlobalPlanetConfiguration.GetName());
         var planetConf    = JsonConvert.DeserializeObject <GlobalPlanetConfiguration>(resPlanetConf.Data);
         ConfigurationsUtil.Instance.LoadBuildingConf(planetConf, building);
     }
 }
Beispiel #4
0
        public Universe BuildUniverse()
        {
            Universe result = new Universe()
            {
                Name = "universe 1"
            };

            List <SolarSystem> solarSystems = new List <SolarSystem>();

            using (var db = new ASPNetOgameLikeTPContext())
            {
                for (int i = 1; i < this.globalGameConfiguration.SolarSystemNb + 1; i++)
                {
                    SolarSystem solarSystem = new SolarSystem();
                    for (int j = 1; j < this.globalGameConfiguration.PlanetsNb + 1; j++)
                    {
                        Planet planet = new Planet();

                        foreach (var item in this.globalPlanetConfiguration.BuildingsIds)
                        {
                            ResourceGenerator buildingTemp = ConfigurationsUtil.Instance.PlanetResourceGenerators(globalPlanetConfiguration).FirstOrDefault(x => x.Id == item);
                            buildingTemp.Print = DEFAULT_DISPLAY_RESOURCE;
                            buildingTemp.Id    = null;
                            planet.Buildings.Add(ClassUtil.Copy(buildingTemp));
                        }

                        foreach (var item in this.globalPlanetConfiguration.ResourcesIds)
                        {
                            Resource resourceTemp = ConfigurationsUtil.Instance.PlanetResources(globalPlanetConfiguration).FirstOrDefault(x => x.Id == item);
                            resourceTemp.LastUpdate   = DateTime.Now;
                            resourceTemp.LastQuantity = 0;
                            resourceTemp.Print        = DEFAULT_DISPLAY_RESOURCE;
                            resourceTemp.Id           = null;
                            planet.Resources.Add(ClassUtil.Copy(resourceTemp));
                        }

                        planet.CaseNb = MathUtil.DrawRandom(20 * j % 300, 30 * j % 300);
                        planet.Name   = $"Planet{j}";
                        planet.Print  = DEFAULT_DISPLAY_RESOURCE;

                        solarSystem.Planets.Add(planet);
                    }
                    solarSystem.Name = "système solaire " + i;
                    solarSystems.Add(solarSystem);
                }
            }

            result.SolarSystems.AddRange(solarSystems);

            return(result);
        }
Beispiel #5
0
        public List <SolarSystem> BuildOnlySolarSystems()
        {
            List <SolarSystem> solarSystems = new List <SolarSystem>();

            using (var db = new ASPNetOgameLikeTPContext())
            {
                for (int i = 1; i < this.globalGameConfiguration.SolarSystemNb + 1; i++)
                {
                    SolarSystem solarSystem = new SolarSystem();
                    solarSystem.Name = "système solaire " + i;
                    solarSystems.Add(solarSystem);
                }
            }
            return(solarSystems);
        }
Beispiel #6
0
        public Planet Build()
        {
            Planet result = new Planet();

            result.Name   = this.name;
            result.CaseNb = this.caseNb == null ? 0 : this.caseNb;
            result.Print  = "Planet";

            using (var db = new ASPNetOgameLikeTPContext())
            {
                var resPlanetConf = db.Configurations.Find(ConfigurationKeys.GlobalPlanetConfiguration.GetName());
                var planetConf    = JsonConvert.DeserializeObject <GlobalPlanetConfiguration>(resPlanetConf.Data);
                result.Buildings = new List <Building>(ConfigurationsUtil.Instance.PlanetResourceGenerators(planetConf).ToList <Building>());
                result.Resources = new List <Resource>(ConfigurationsUtil.Instance.PlanetResources(planetConf));
            }

            return(result);
        }
 private static void InitFakeDatas(GameUserVM vm)
 {
     try
     {
         using (var db = new ASPNetOgameLikeTPContext())
         {
             Universe universe = db.Universes
                                 .Include(x => x.SolarSystems.Select(y => y.Planets.Select(p => p.Resources)))
                                 .Include(x => x.SolarSystems.Select(y => y.Planets.Select(p => p.Buildings)))
                                 .FirstOrDefault();
             vm.Universe        = universe;
             vm.PrincipalPlanet = universe.SolarSystems.SelectMany(y => y.Planets).FirstOrDefault();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.StackTrace);
     }
 }
        public void RefreshResources(GameUserVM vm)
        {
            List <Resource> resourcesGenerate = new List <Resource>();

            foreach (var building in vm.PrincipalPlanet.Buildings)
            {
                if (building is ResourceGenerator)
                {
                    var resourceGenerator = building as ResourceGenerator;
                    this.ApplyResourceConfigurations(resourceGenerator);

                    foreach (var resource in resourceGenerator.ResourceBySecond)
                    {
                        var      realResource = vm.PrincipalPlanet.Resources.FirstOrDefault(x => x.Name.Equals(resource.Name));
                        TimeSpan ts           = DateTime.Now - realResource.LastUpdate;
                        int      calc         = (int)((double)resource.LastQuantity.Value * ts.TotalSeconds);
                        if (resourcesGenerate.Any(x => x.Name.Equals(resource.Name)))
                        {
                            var resTmp = resourcesGenerate.FirstOrDefault(x => x.Name.Equals(resource.Name));
                            resTmp.LastQuantity = resTmp.LastQuantity + calc;
                        }
                        else
                        {
                            resourcesGenerate.Add(new Resource()
                            {
                                Name = resource.Name, LastQuantity = calc
                            });
                        }
                    }
                }
            }

            using (var db = new ASPNetOgameLikeTPContext())
            {
                foreach (var resource in vm.PrincipalPlanet.Resources)
                {
                    resource.LastQuantity = resource.LastQuantity + resourcesGenerate.FirstOrDefault(x => x.Name.Equals(resource.Name)).LastQuantity;
                    this.SaveResource(db, resource);
                }
            }
        }
        public ActionResult UpgradeBuilding(long?buildingId)
        {
            Building building = null;

            if (buildingId != null)
            {
                using (var db = new ASPNetOgameLikeTPContext())
                {
                    building = db.Buildings.Find(buildingId);
                    if (this.resourceService.BuyIt(GameUserController.vm, building))
                    {
                        building.Level          += 1;
                        db.Entry(building).State = EntityState.Modified;
                        db.SaveChanges();
                        vm.Universe.SolarSystems.SelectMany(x => x.Planets).SelectMany(x => x.Buildings).FirstOrDefault(x => x.Id == buildingId).Level++;
                    }
                }
            }

            return(PartialView("~/Views/GameUser/GameUserBuilding.cshtml", building));
        }