Exemple #1
0
        public ActionResult SaveMilestone(int id, TradeMilestone milestone)
        {
            using (var session = DB.Instance.GetSession())
            {
                session.Advanced.UseOptimisticConcurrency = true;
                var route = session.Load <TradeRoute>(id);
                if (route != null)
                {
                    if (!String.IsNullOrEmpty(milestone.Guid))
                    {
                        route.Milestones.Remove(route.Milestones.First(x => x.Guid == milestone.Guid));
                    }
                    else
                    {
                        milestone.Guid = Guid.NewGuid().ToString();
                    }

                    //Refs
                    var systems = session.Load <SolarSystem>(new int[] {
                        milestone.From.SolarSystem.Id,
                        milestone.To.SolarSystem.Id
                    }.Cast <ValueType>());
                    milestone.From.SolarSystem = SolarSystemRef.FromSolarSystem(systems.First(x => x.Id == milestone.From.SolarSystem.Id));
                    milestone.To.SolarSystem   = SolarSystemRef.FromSolarSystem(systems.First(x => x.Id == milestone.To.SolarSystem.Id));

                    route.Milestones.Add(milestone);
                }
                session.SaveChanges();
            }
            return(new JsonResult {
                Data = new { status = "OK" }
            });
        }
Exemple #2
0
        public ActionResult Edit(int?id, TaskEditView input)
        {
            var task = input.Task;

            using (var session = DB.Instance.GetSession())
            {
                //Create solarsystem ref
                if (task.SolarSystem != null && task.SolarSystem.Id != 0)
                {
                    task.SolarSystem = SolarSystemRef.FromSolarSystem(session.Load <SolarSystem>(task.SolarSystem.Id));
                }
                else
                {
                    task.SolarSystem = null;
                }

                if (id.HasValue)
                {
                    var org = session.Load <Task>(id);
                    org.Description = task.Description;
                    org.Heading     = task.Heading;
                    org.Priority    = task.Priority;
                    org.Status      = task.Status;
                    org.Type        = task.Type;
                    org.SolarSystem = task.SolarSystem;

                    org.Owner = new CommanderRef
                    {
                        Id   = CommanderId,
                        Name = User.Identity.Name
                    };
                    org.Date = DateTime.UtcNow;
                }
                else
                {
                    task.Owner = new CommanderRef {
                        Id   = CommanderId,
                        Name = User.Identity.Name
                    };
                    task.Date = DateTime.UtcNow;
                    session.Store(task);
                }
                session.SaveChanges();
            }

            return(RedirectToAction("View", new { id = id ?? task.Id }));
        }
        public ActionResult Edit(int?id, FactionEditView input)
        {
            var faction = input.Faction;
            List <SolarSystem> postedSystems = new List <SolarSystem>();
            List <SolarSystem> oldSystems    = null;

            using (var session = DB.Instance.GetSession())
            {
                //Check if faction exists?
                if (!id.HasValue)
                {
                    var existing = session.Query <Faction>().Where(x => x.Name == input.Faction.Name).ToList().FirstOrDefault(x => x.Name.Equals(input.Faction.Name, StringComparison.CurrentCultureIgnoreCase));
                    if (existing != null)
                    {
                        return(RedirectToAction("Edit", new { status = "Faction already exists" }));
                    }
                }

                if (id.HasValue)
                {
                    faction            = session.Load <Faction>(id.Value);
                    oldSystems         = session.Load <SolarSystem>(faction.SolarSystems.Select(x => x.Id).Cast <ValueType>()).ToList();
                    faction.Name       = input.Faction.Name;
                    faction.Government = input.Faction.Government;
                    faction.Attitude   = input.Faction.Attitude;
                    faction.Allegiance = input.Faction.Allegiance;
                    faction.EDDB_Id    = input.Faction.EDDB_Id;
                }

                //Solar systems
                SolarSystem homeSystem = null;
                if (input.PostedSystems != null && input.PostedSystems.Count > 0)
                {
                    var systemIds = input.PostedSystems.Select(x => int.Parse(x)).Cast <System.ValueType>();
                    postedSystems        = session.Load <SolarSystem>(systemIds).ToList();
                    faction.SolarSystems = postedSystems.Select(x => SolarSystemRef.FromSolarSystem(x)).ToList();
                    //Try to set homesystem by checking posted systems
                    homeSystem = postedSystems.FirstOrDefault(x => x.Id == input.Faction.HomeSolarSystem.Id);
                }
                else
                {
                    faction.SolarSystems.Clear();
                }

                //If homesystem was not among posted systems
                if (homeSystem == null)
                {
                    homeSystem = session.Load <SolarSystem>(input.Faction.HomeSolarSystem.Id);
                    postedSystems.Add(homeSystem);
                    //Make sure it's added to posted systems
                    if (!faction.SolarSystems.Any(x => x.Id == homeSystem.Id))
                    {
                        faction.SolarSystems.Add(SolarSystemRef.FromSolarSystem(homeSystem));
                    }
                }
                //Create ref
                faction.HomeSolarSystem = SolarSystemRef.FromSolarSystem(homeSystem);

                //Store faction, attaches id
                if (!id.HasValue)
                {
                    session.Store(faction);
                }

                //Remove faction from removed systems
                if (oldSystems != null)
                {
                    foreach (var system in oldSystems.Except(postedSystems))
                    {
                        system.Factions.RemoveAll(x => x.Id == faction.Id);
                    }
                }
                //Add allied factions to current systems
                foreach (var system in postedSystems)
                {
                    if (!system.Factions.Any(x => x.Id == faction.Id))
                    {
                        system.Factions.Add(FactionRef.FromFaction(faction));
                    }
                }

                session.SaveChanges();
            }
            return(RedirectToAction("Edit", new { id = faction.Id, status = "saved" }));
        }
Exemple #4
0
        public ActionResult Import(HttpPostedFileBase file)
        {
            if (!User.IsInRole("administrator"))
            {
                return(new HttpUnauthorizedResult());
            }

            if (file != null)
            {
                List <Faction>     factions = new List <Faction>();
                List <SolarSystem> systems  = new List <SolarSystem>();
                using (StreamReader reader = new StreamReader(file.InputStream))
                {
                    do
                    {
                        var line = reader.ReadLine();
                        var cols = line.Split(',');
                        if (cols.Length < 14)
                        {
                            continue;
                        }

                        var  systemName    = cols[0];
                        var  factionName   = cols[1];
                        var  strAllegiance = cols[2];
                        var  strEconomy    = cols[3];
                        long population    = 0;
                        try
                        {
                            population = !String.IsNullOrEmpty(cols[5]) ? long.Parse(cols[5].Replace(".", "").Replace(",", "")) : 0;
                        }
                        catch { }
                        float coordX = !String.IsNullOrEmpty(cols[11]) ? float.Parse(cols[11], NumberFormatInfo.InvariantInfo) : 0f;
                        float coordY = !String.IsNullOrEmpty(cols[12]) ? float.Parse(cols[12], NumberFormatInfo.InvariantInfo) : 0f;
                        float coordZ = !String.IsNullOrEmpty(cols[13]) ? float.Parse(cols[13], NumberFormatInfo.InvariantInfo) : 0f;

                        if (String.IsNullOrEmpty(systemName))
                        {
                            continue;
                        }
                        systemName = systemName.Trim();

                        using (var session = DB.Instance.GetSession())
                        {
                            SolarSystem system = systems.FirstOrDefault(x => x.Name.Equals(systemName, StringComparison.CurrentCultureIgnoreCase));
                            if (system == null)
                            {
                                //System
                                system = new SolarSystem
                                {
                                    Name        = Capitalize(systemName),
                                    Population  = population,
                                    Coordinates = new Coordinate
                                    {
                                        X = coordX,
                                        Y = coordY,
                                        Z = coordZ
                                    }
                                };
                                session.Store(system);
                                //Save system to get id
                                session.SaveChanges();
                                systems.Add(system);
                            }

                            //Faction
                            if (!String.IsNullOrEmpty(factionName))
                            {
                                var faction = factions.FirstOrDefault(x => x.Name.Equals(factionName, StringComparison.CurrentCultureIgnoreCase));
                                if (faction == null)
                                {
                                    factionName = factionName.Trim();
                                    faction     = new Faction
                                    {
                                        Name       = factionName,
                                        Attitude   = FactionAttitude.Ally,
                                        Government = FactionGovernment.Communism,
                                        Allegiance = Power.Independent
                                    };
                                    if (!String.IsNullOrEmpty(strAllegiance))
                                    {
                                        Power power = Power.Independent;
                                        if (Enum.TryParse <Power>(strAllegiance, out power))
                                        {
                                            faction.Allegiance = power;
                                        }
                                    }
                                    faction.HomeSolarSystem = new SolarSystemRef
                                    {
                                        Id   = system.Id,
                                        Name = system.Name
                                    };
                                    faction.SolarSystems.Add(faction.HomeSolarSystem);
                                    session.Store(faction);
                                    session.SaveChanges();
                                    factions.Add(faction);
                                }
                                else
                                {
                                    faction = session.Load <Faction>(faction.Id);
                                    faction.SolarSystems.Add(SolarSystemRef.FromSolarSystem(system));
                                    session.SaveChanges();
                                }
                            }
                        }
                    }while (reader.Peek() != -1);
                }
            }
            return(View());
        }