示例#1
0
        internal static bool Load(XmlReader reader, EliteEnvironment container)
        {
            if (reader.NodeType != XmlNodeType.Element || reader.LocalName != "commodity")
            {
                return(false);
            }

            string cName     = null;
            string cCategory = null;

            while (reader.MoveToNextAttribute())
            {
                switch (reader.LocalName)
                {
                case "name":
                    cName = reader.Value;
                    break;

                case "category":
                    cCategory = reader.Value;
                    break;
                }
            }

            if (String.IsNullOrWhiteSpace(cName))
            {
                throw new EnvironmentLoadException("Invalid or missing commodity name", reader);
            }

            container.CreateCommodity(cName, cCategory);

            reader.Read();
            return(true);
        }
        /// <summary>
        /// Creates a new <see cref="Star"/> instance and adds it into this environment
        /// </summary>
        /// <param name="name">The name of the new star system</param>
        /// <param name="env">The environment that will contain the new star</param>
        /// <returns>The created <see cref="Star"/> instance.</returns>
        /// <exception cref="ArgumentNullException">The provided name is null</exception>
        /// <exception cref="ArgumentException">The provided name is already used by an existing star</exception>
        public static AstronomicalObject CreateStar(string name, EliteEnvironment env)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (env == null)
            {
                throw new ArgumentNullException("name");
            }

            AstronomicalObject result   = null;
            AstronomicalObject existing = env.FindObjectByName(name);

            if (existing != null)
            {
                existing.Star = existing;
                existing.Type = AstronomicalObjectType.Star;
                result        = existing;
            }
            else
            {
                result      = new AstronomicalObject(name, env, AstronomicalObjectType.Star, null);
                result.Star = result;
            }

            return(result);
        }
        // UNKNOWN
        // Star
        // Planet / Moon / Belt
        // Unidentified signals
        // Station

        internal AstronomicalObject(string name, EliteEnvironment environment, AstronomicalObjectType type, AstronomicalObject star)
        {
            Name        = name;
            Environment = environment;

            ObjectsInternal        = new ObservableCollection <AstronomicalObject>();
            Objects                = new ReadOnlyObservableCollection <AstronomicalObject>(ObjectsInternal);
            KnownObjectProximities = new StarProximityCollection();
            _trades                = new ObservableCollection <Trade>();
            Trades          = new ReadOnlyObservableCollection <Trade>(_trades);
            _commodityIndex = new Dictionary <Commodity, int>();

            if (environment.AutoDistanceEnabled)
            {
                float distance = 0;
                foreach (AstronomicalObject otherObject in environment.Objects)
                {
                    if (DistancesDB.TryGetDistance(this, otherObject, out distance))
                    {
                        RegisterDistanceFrom(otherObject, distance);
                    }
                }
            }


            Type = type;
            Star = star ?? this;

            Environment.ObjectsInternal.Add(this);
        }
示例#4
0
        internal static bool Load(XmlReader reader, EliteEnvironment container)
        {
            if (reader.NodeType != XmlNodeType.Element || reader.LocalName != "trade")
            {
                return(false);
            }

            Commodity          commodity = null;
            AstronomicalObject station   = null;
            float sellingPrice           = 0;
            float buyingPrice            = 0;
            int   stock = 0;

            while (reader.MoveToNextAttribute())
            {
                switch (reader.LocalName)
                {
                case "commodity":
                    commodity = container.FindCommodityByName(reader.Value);
                    if (commodity == null)
                    {
                        throw new EnvironmentLoadException(String.Format("Unknown commodity name '{0}'", reader.Value), reader);
                    }
                    break;

                case "station":
                    station = container.Stations.Where(s => s.Name == reader.Value).FirstOrDefault();
                    if (station == null)
                    {
                        throw new EnvironmentLoadException(String.Format("Unknown station name '{0}'", reader.Value), reader);
                    }
                    break;

                case "sellingPrice":
                    sellingPrice = reader.ReadFloat();
                    break;

                case "buyingPrice":
                    buyingPrice = reader.ReadFloat();
                    break;

                case "stock":
                    stock = reader.ReadInt();
                    break;
                }
            }

            if (commodity == null)
            {
                throw new EnvironmentLoadException("Missing commodity for a trade entry", reader);
            }

            if (station == null)
            {
                throw new EnvironmentLoadException("Missing station for a trade entry", reader);
            }

            station.CreateTrade(commodity, sellingPrice, buyingPrice, stock);

            reader.Read();
            return(true);
        }
        internal static bool Load(XmlReader reader, EliteEnvironment container)
        {
            if (!reader.IsStartElement())
            {
                return(false);
            }

            AstronomicalObjectType type = AstronomicalObjectType.Unspecified;

            if (Enum.TryParse <AstronomicalObjectType>(reader.LocalName, true, out type))
            {
                string             name = null;
                AstronomicalObject star = null;
                while (reader.MoveToNextAttribute())
                {
                    switch (reader.LocalName)
                    {
                    case "name":
                        name = reader.Value;
                        break;

                    case "star":
                        star = container.FindObjectByName(reader.Value, AstronomicalObjectType.Star);
                        if (star == null)
                        {
                            throw new EnvironmentLoadException(String.Format("The star {0} could not be found", reader.Value), reader);
                        }
                        break;
                    }
                }

                if (name == null)
                {
                    throw new EnvironmentLoadException("Missing name for astronomical object entry", reader);
                }


                AstronomicalObject result = new AstronomicalObject(name, container, type, star);

                // Read child elements
                int curDepth = reader.Depth;
                while (reader.Read() && reader.Depth >= curDepth)
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        switch (reader.LocalName)
                        {
                        case "notes":
                            result.UserNotes = reader.ReadElementContentAsString();
                            break;
                        }
                    }
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }