public Galaxy ToGalaxy()
        {
            var gal       = new Galaxy();
            int id        = 0;
            var planetIDs = PlanetDrawings.ToDictionary(
                d => d,
                d =>
            {
                var temp = id++;
                return(temp);
            });

            gal.Planets = PlanetDrawings.Select(
                d => new Planet(planetIDs[d],
                                (float)(Canvas.GetLeft(d) / imageSource.Width),
                                (float)(Canvas.GetTop(d) / imageSource.Height))).ToList();
            gal.Links    = LinkDrawings.Select(d => new Link(planetIDs[d.Planet1], planetIDs[d.Planet2])).ToList();
            gal.MapNames = ToLines(MapNames).ToList();
            return(gal);
        }
Beispiel #2
0
        public void SaveGalaxy(int galaxyNumber)
        {
            // using (var scope = new TransactionScope())
            {
                try
                {
                    var db  = new ZkDataContext();
                    var gal = db.Galaxies.SingleOrDefault(x => x.GalaxyID == galaxyNumber);
                    if (gal == null || galaxyNumber == 0)
                    {
                        gal = new Galaxy();
                        db.Galaxies.InsertOnSubmit(gal);
                    }
                    else if (gal.Started != null)
                    {
                        MessageBox.Show("This galaxy is running, cannot edit it!");
                        return;
                    }
                    else
                    {
                        db.Links.DeleteAllOnSubmit(gal.Links);
                        db.Planets.DeleteAllOnSubmit(gal.Planets);
                    }
                    gal.IsDirty = true;
                    db.SaveChanges();
                    galaxyNumber = gal.GalaxyID;

                    var maps = Maps.Shuffle();
                    var cnt  = 0;

                    foreach (var d in PlanetDrawings)
                    {
                        var memPlanet = d.Planet;

                        var p = new Planet();
                        db.Planets.Add(p);

                        p.Name           = memPlanet.Name;
                        p.GalaxyID       = galaxyNumber;
                        p.OwnerAccountID = memPlanet.OwnerAccountID;
                        p.X = (float)(Canvas.GetLeft(d) / imageSource.Width);
                        p.Y = (float)(Canvas.GetTop(d) / imageSource.Height);
                        if (memPlanet.MapResourceID == null)
                        {
                            p.MapResourceID = maps[cnt].ResourceID;
                            cnt++;
                            cnt = cnt % maps.Count;
                        }
                        else
                        {
                            p.MapResourceID = memPlanet.MapResourceID;
                        }

                        p.PlanetStructures.Clear();
                        p.PlanetStructures.AddRange(memPlanet.PlanetStructures.Select(x => new PlanetStructure()
                        {
                            StructureTypeID = x.StructureTypeID
                        }));
                    }
                    db.SaveChanges();

                    var linkList =
                        LinkDrawings.Select(
                            d =>
                            new Link()
                    {
                        GalaxyID  = galaxyNumber,
                        PlanetID1 = db.Planets.Single(x => x.GalaxyID == galaxyNumber && x.Name == d.Planet1.Planet.Name).PlanetID,
                        PlanetID2 = db.Planets.Single(x => x.GalaxyID == galaxyNumber && x.Name == d.Planet2.Planet.Name).PlanetID
                    });
                    db.Links.InsertAllOnSubmit(linkList);
                    db.SaveChanges();
                    // scope.Complete();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                MessageBox.Show("Exported!");
            }
        }