コード例 #1
0
ファイル: Generation.cs プロジェクト: darkguhl/ph_contingency
        public static CelestialBody GenerateNebula(PhContext db, int distance, Star mainStar)
        {
            var position = new Position();
            // Check for existing Objects that collide
            var    collision = true;
            var    radius    = Utility.GetOrbitalRadius(distance);
            double angle     = Utility.RandomNumber(0, 360);

            while (collision)
            {
                position = Utility.GetOrbitalObjectPosition(radius, angle, new Position()
                {
                    X = 0, Y = 0
                }, 1);
                collision = CheckforSystemCollision(position, mainStar);
            }

            Nebula nebula = db.CelestialBodySet.Create <Nebula>();

            nebula.Star      = mainStar;
            nebula.Type      = GetNebulaType();
            nebula.Name      = GetSystemBodyName(distance, mainStar.Name);
            nebula.Dimension = GetBodyDimension();
            nebula.Angle     = angle;
            nebula.Radius    = radius;

            return(nebula);
        }
コード例 #2
0
ファイル: Generation.cs プロジェクト: darkguhl/ph_contingency
        public static CelestialBody GeneratePlanet(PhContext db, int distance, Star mainStar)
        {
            var position = new Position();
            // Check for existing Objects that collide
            var    collision = true;
            var    radius    = Utility.GetOrbitalRadius(distance);
            double angle     = Utility.RandomNumber(0, 360);

            while (collision)
            {
                position = Utility.GetOrbitalObjectPosition(radius, angle, new Position()
                {
                    X = 0, Y = 0
                }, 1);
                collision = CheckforSystemCollision(position, mainStar);
            }

            Planet planet = db.CelestialBodySet.Create <Planet>();

            planet.Star      = mainStar;
            planet.Type      = GetPlanetType(distance);
            planet.Name      = GetSystemBodyName(distance, mainStar.Name);
            planet.Dimension = GetBodyDimension();
            planet.Angle     = angle;
            planet.Radius    = radius;

            GenerateMoons(db, planet, 0, 3);

            return(planet);
        }
コード例 #3
0
        private void GenerateStars()
        {
            using (var db = PhContext.CreateContext())
            {
                this.ReportProgress(0, "Generating Stars");

                Cluster cluster = db.ClusterSet.FirstOrDefault(x => x.Id == m_clusterId);

                Position    pos   = new Position();
                List <Star> stars = new List <Star>();
                for (int i = 0; i < m_generationArguments.Amount; i++)
                {
                    string name           = string.Empty;
                    bool   starnameExists = true;
                    while (starnameExists)
                    {
                        name           = Utility.RandomString(3) + "-" + Utility.RandomNumber(100, 999);
                        starnameExists = stars.Any(x => x.Name == name);
                    }
                    bool collision = true;
                    var  tries     = 0;
                    while (collision)
                    {
                        pos.X     = Utility.RandomNumber(100, cluster.ExtentX - 100);
                        pos.Y     = Utility.RandomNumber(100, cluster.ExtentY - 100);
                        collision = stars.Any(s => s.X <pos.X + 200 && s.X> pos.X - 200 && s.Y <pos.Y + 150 && s.Y> pos.Y - 150);
                        tries++;
                        if (tries > 1000)
                        {
                            break;
                        }
                    }
                    if (tries > 1000)
                    {
                        Log.Warning("Exceeded 1000 tries while finding a position for Star " + i + ". Aborting star generation.");
                        break;
                    }


                    Star star = db.CelestialBodySet.Create <Star>();
                    star.Name     = name;
                    star.Type     = Generation.GetStarType();
                    star.CssClass = Generation.GetStarCssType(star.Type);

                    star.X = pos.X;
                    star.Y = pos.Y;

                    star.Cluster = cluster;
                    stars.Add(star);
                    int progress = 100 / m_generationArguments.Amount * (i + 1);
                    this.ReportProgress(progress, "Generating Stars");
                }
                db.CelestialBodySet.AddRange(stars);
                db.SaveChanges();
            }
        }
コード例 #4
0
 public static string GetVisibleStars(int top, int left, int bottom, int right, Position center)
 {
     using (PhContext db = PhContext.CreateContext())
     {
         var visibleStars = db.CelestialBodySet.OfType <Star>().Where(
             x => x.X > left && x.Y > top && x.X < right && x.Y < bottom
             ).Select(x => new { x.Name, X = x.X - center.X, Y = x.Y - center.Y, x.CssClass });
         var result = JsonConvert.SerializeObject(new { stars = visibleStars });
         return(result);
     }
 }
コード例 #5
0
 private void BackgroundGenerator_ProgressChanged(object sender, ProgressChangedEventArgs e)
 {
     using (var db = PhContext.CreateContext())
     {
         var job = db.JobStatusInfoSet.FirstOrDefault(x => x.Key == m_key);
         job.Progress = e.ProgressPercentage;
         job.Message  = e.UserState.ToString();
         job.Succeded = false;
         db.SaveChanges();
     }
 }
コード例 #6
0
ファイル: Generation.cs プロジェクト: darkguhl/ph_contingency
        public static void GenerateMoons(PhContext db, Planet p, int minAmount, int maxAmount)
        {
            //Determine Number of Moons
            int Moons = Utility.RandomNumber(minAmount, maxAmount);

            for (int i = 0; i < Moons; i++)
            {
                Position pos = new Position();

                var    dim        = GetBodyDimension();
                double size       = dim == Dimension.Large ? 25 : dim == Dimension.Small ? 15 : 20;
                double planetSize = p.Dimension == Dimension.Large ? 100 : p.Dimension == Dimension.Small ? 50 : 75;
                double radius     = planetSize / 2 + 15 + size / 2;
                double moonangle  = 0;
                int    count      = 0;
                // Check for existing Moons that collide
                bool collision   = true;
                bool noMoreSpace = false;

                while (collision)
                {
                    // Recalculate angle and position
                    moonangle = Utility.RandomNumber(45, 315);
                    pos       = Utility.GetMoonPosition(radius, moonangle, p.X, p.Y, 1);

                    collision = CheckforMoonCollision(pos, p);

                    // if after 100 tries, there is still a collision no moon will be added
                    count++;
                    if (count <= 100)
                    {
                        continue;
                    }
                    noMoreSpace = true;
                    collision   = false;
                }
                if (noMoreSpace)
                {
                    continue;
                }
                //new moon
                var moon = db.CelestialBodySet.Create <Moon>();
                moon.Planet    = p;
                moon.Angle     = moonangle;
                moon.Radius    = radius;
                moon.Dimension = GetBodyDimension();
                moon.Name      = p.Name + " Moon " + i + 1;
                moon.Type      = GetMoonType();

                db.CelestialBodySet.Add(moon);
            }
        }
コード例 #7
0
 private void BackgroundGenerator_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
 {
     //Save the Completion to the db, ajax call will query the completion
     using (var db = PhContext.CreateContext())
     {
         var     job     = db.JobStatusInfoSet.FirstOrDefault(x => x.Key == m_key);
         Cluster cluster = db.ClusterSet.FirstOrDefault(x => x.Id == m_clusterId);
         job.Progress = 100;
         job.Message  = "Cluster " + m_clusterId + " generated. " + cluster.Stars.Count + " systems created.";
         job.Succeded = true;
         db.SaveChanges();
     }
 }
コード例 #8
0
 private int CreateCluster()
 {
     using (var db = PhContext.CreateContext())
     {
         this.ReportProgress(0, "Create Cluster");
         var cluster = db.ClusterSet.Create();
         cluster.Name    = m_generationArguments.Name;
         cluster.ExtentX = m_generationArguments.ExtentX;
         cluster.ExtentY = m_generationArguments.ExtentY;
         db.ClusterSet.Add(cluster);
         db.SaveChanges();
         this.ReportProgress(100, "Create Cluster");
         return(cluster.Id);
     }
 }
コード例 #9
0
 public static string GetHomeSystem()
 {
     using (PhContext db = PhContext.CreateContext())
     {
         //todo get homesystem based on user corp in cluster
         var homeSystem = db.CelestialBodySet.OfType <Star>().FirstOrDefault(
             x => x.X > 1000 && x.Y > 1000 && x.X < 2000 && x.Y < 2000
             );
         //var result = JsonConvert.SerializeObject(homeSystem, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
         var result = JsonConvert.SerializeObject(new Star()
         {
             X = homeSystem.X, Y = homeSystem.Y, Name = homeSystem.Name
         });
         return(result);
     }
 }
コード例 #10
0
        private void GenerateSystems()
        {
            using (var db = PhContext.CreateContext())
            {
                this.ReportProgress(0, "Generating Systems");
                var cluster       = db.ClusterSet.FirstOrDefault(x => x.Id == m_clusterId);
                int progressCount = 0;
                foreach (Star star in cluster.Stars)
                {
                    //Determine Number of orbital Celestial Objects
                    int count = Utility.RandomNumber(0, 5);

                    if (count == 0)
                    {
                        return;
                    }

                    //Generate Orbital celestial Objects
                    for (int i = 0; i < count; i++)
                    {
                        CelestialBody body = null;
                        //Determin Type: Planet 75%, Asteroid 20% or Nebula 5%
                        var systemObjectType = Generation.GetSystemObjectType();

                        if (systemObjectType == typeof(Planet))
                        {
                            body = Generation.GeneratePlanet(db, i, star);
                        }
                        if (systemObjectType == typeof(Asteroid))
                        {
                            body = Generation.GenerateAsteroid(db, i, star);
                        }
                        if (systemObjectType == typeof(Nebula))
                        {
                            body = Generation.GenerateNebula(db, i, star);
                        }

                        db.CelestialBodySet.Add(body);
                    }
                    progressCount++;
                    int progress = 100 / cluster.Stars.Count * progressCount;
                    this.ReportProgress(progress, "Generating Systems");
                }
            }
        }
コード例 #11
0
        public static void CreateUser(User user, RoleType roleType)
        {
            using (var db = PhContext.CreateContext())
            {
                var crypto   = new SimpleCrypto.PBKDF2();
                var encrPass = crypto.Compute(user.Password);
                var sysUser  = db.UserSet.Create();

                sysUser.Username     = user.Username;
                sysUser.Email        = user.Email;
                sysUser.Password     = encrPass;
                sysUser.PasswordSalt = crypto.Salt;
                sysUser.RoleType     = roleType;

                db.UserSet.Add(sysUser);
                db.SaveChanges();
            }
        }
コード例 #12
0
ファイル: Utility.cs プロジェクト: darkguhl/ph_contingency
        public static bool IsUserValid(string name, string password, bool requireAdmin = false)
        {
            var crypto  = new SimpleCrypto.PBKDF2();
            var isValid = false;

            using (var db = PhContext.CreateContext())
            {
                var user = db.UserSet.FirstOrDefault(u => (u.Email == name || u.Username == name) && requireAdmin ? u.RoleType == RoleType.Admin : true);
                if (user != null)
                {
                    if (user.Password == crypto.Compute(password, user.PasswordSalt))
                    {
                        isValid = true;
                    }
                }
            }

            return(isValid);
        }
コード例 #13
0
        private bool GenerateJob()
        {
            using (var db = PhContext.CreateContext())
            {
                //check if a generation is in progress. if yes return
                bool unfinishedJobsExist = db.JobStatusInfoSet.Any(x => x.Progress != 100);
                if (unfinishedJobsExist)
                {
                    return(false);
                }

                //create the new job
                var job = db.JobStatusInfoSet.Create();
                job.Progress = 0;
                job.Key      = m_key;
                job.Message  = "Cluster Generating";
                job.Succeded = false;
                db.JobStatusInfoSet.Add(job);
                db.SaveChanges();
            }
            return(true);
        }