Пример #1
0
        public static void Load(bool initonly)
        {
            if (initonly)
            {
                Console.WriteLine("Initializing ResourcePool database...");
            }
            else
            {
                Console.WriteLine("Resource Pool Loading...");
            }

            if (!Directory.Exists("Saves/ResourcePool"))
            {
                Directory.CreateDirectory("Saves/ResourcePool");
            }

            bool             newconfig = false;
            FileStream       rpfs      = new FileStream("Saves/ResourcePool/ResourcePool.dat", FileMode.OpenOrCreate, FileAccess.Read);
            BinaryFileReader rpreader  = new BinaryFileReader(new BinaryReader(rpfs));

            if (!rpreader.End())
            {
                int rpversion = rpreader.ReadInt();
                switch (rpversion)
                {
                case 0:
                {
                    m_LastModified = rpreader.ReadDateTime();
                    FileInfo fi = new FileInfo("Data/ResourcePool/config.xml");
                    if (fi.LastWriteTime != m_LastModified)
                    {
                        m_LastModified = fi.LastWriteTime;
                        newconfig      = true;
                    }
                    else
                    {
                        newconfig = false;
                    }

                    break;
                }

                default:
                    throw new Exception("Error loading ResourcePool: Invalid ResourcePool.dat save version");
                }
            }
            else
            {
                m_LastModified = (new FileInfo("Data/ResourcePool/config.xml")).LastWriteTime;
                Console.WriteLine("Warning: Saves/ResourcePool/ResourcePool.dat not found.");
                newconfig = true;
            }

            if (newconfig)
            {
                Console.WriteLine("New configuration detected! Reading from Data/...");
            }
            XmlTextReader reader;

            reader = new XmlTextReader((newconfig ? "Data/ResourcePool/config.xml" : "Saves/ResourcePool/config.xml"));
            reader.WhitespaceHandling = WhitespaceHandling.None;

            FileStream       rcfs     = new FileStream("Saves/ResourcePool/Consignments.dat", FileMode.OpenOrCreate, FileAccess.Read);
            FileStream       dfs      = new FileStream("Saves/ResourcePool/Debts.dat", FileMode.OpenOrCreate, FileAccess.Read);
            BinaryFileReader rcreader = new BinaryFileReader(new BinaryReader(rcfs));
            BinaryFileReader dreader  = new BinaryFileReader(new BinaryReader(dfs));

            try { reader.MoveToContent(); }
            catch
            {
                Console.WriteLine("Save xml data not found or invalid, reverting to defaults");
                reader = new XmlTextReader("Data/ResourcePool/config.xml");
                reader.WhitespaceHandling = WhitespaceHandling.None;
                reader.MoveToContent();
            }

            int version = Int32.Parse(reader.GetAttribute("version"));

            reader.ReadStartElement("ResourcePool");

            switch (version)
            {
            case 0:
            {
                m_PaymentFactor     = Double.Parse(reader.ReadElementString("PaymentFactor"));
                m_FailsafePriceHike = Double.Parse(reader.ReadElementString("FailsafePriceHike"));

                m_Resources = new Hashtable();
                while (reader.LocalName == "ResourceData")
                {
                    ResourceData rd = new ResourceData(reader, (initonly ? null : rcreader));
                    if (rd != null)
                    {
                        m_Resources[rd.Type] = rd;
                    }
                }
                while (reader.LocalName == "RDRedirect")
                {
                    RDRedirect rd = new RDRedirect(reader, (initonly ? null : rcreader));
                    if (rd != null)
                    {
                        m_Resources[rd.Type] = rd;
                    }
                }

                m_Debts = new Hashtable();

                if (initonly)
                {
                    break;
                }

                while (!dreader.End())
                {
                    try
                    {
                        Mobile m    = dreader.ReadMobile();
                        double debt = dreader.ReadDouble();
                        if (m != null)
                        {
                            m_Debts[m] = debt;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception occured attempting to desrialize from Debts.dat : " + ex.Message);
                        break;
                    }
                }

                break;
            }

            case 1:
            {
                m_PaymentFactor     = Double.Parse(reader.ReadElementString("PaymentFactor"));
                m_FailsafePriceHike = Double.Parse(reader.ReadElementString("FailsafePriceHike"));

                m_Resources = new Hashtable();
                while (reader.LocalName == "ResourceData")
                {
                    ResourceData rd = new ResourceData(reader, (initonly ? null : rcreader));
                    if (rd != null)
                    {
                        m_Resources[rd.Type] = rd;
                    }
                }
                while (reader.LocalName == "RDRedirect")
                {
                    RDRedirect rd = new RDRedirect(reader, (initonly ? null : rcreader));
                    if (rd != null)
                    {
                        m_Resources[rd.Type] = rd;
                    }
                }

                m_Debts = new Hashtable();

                if (initonly)
                {
                    break;
                }

                // read count
                int count = dreader.ReadInt();

                for (int ix = 0; ix < count; ix++)
                {
                    Mobile m    = dreader.ReadMobile();
                    double debt = dreader.ReadDouble();
                    if (m != null)
                    {
                        m_Debts[m] = debt;
                    }
                }

                break;
            }

            default:
            {
                throw new Exception("Invalid ResourcePool save version.");
            }
            }

            reader.ReadEndElement();
            reader.Close();
            rcfs.Close();
            rpfs.Close();
            dfs.Close();
        }
Пример #2
0
        public static void ResourceTrack_OnCommand(CommandEventArgs e)
        {
            DateTime begin = DateTime.Now;

            if (!Directory.Exists(OUTPUT_DIRECTORY))
            {
                Directory.CreateDirectory(OUTPUT_DIRECTORY);
            }

            File.Copy(RESULTS_CSS, OUTPUT_DIRECTORY + "/results.css", true);

            StreamWriter sw = new StreamWriter(OUTPUT_DIRECTORY + "/" + OUTPUT_FILE);

            try
            {
                Console.Write("ResourceTracker running...");
                sw.WriteLine("<html>");
                sw.WriteLine("<head>");
                sw.WriteLine("<title>ResourceTracker Results</title>");
                sw.WriteLine("<link rel='stylesheet' type='text/css' href='results.css'/>");
                sw.WriteLine("</head>");
                sw.WriteLine("<body>");
                sw.WriteLine("<h1>Immediate ResourcePool State</h1>");
                sw.WriteLine("<h2>Resources</h2>");
                sw.WriteLine("<table width='90%'>");
                sw.WriteLine("<tr class='theader'><td>Resource Name</td><td>Count</td><td>Wholesale Price</td><td>Resale Price</td><td>Total Investment</td><td>Total Value</td></tr>");

                ArrayList resdatas = new ArrayList(ResourcePool.Resources.Values);
                resdatas.Sort();

                for (int i = 0, m = 0; i < resdatas.Count; i++)
                {
                    if (!(resdatas[i] is ResourceData) || resdatas[i] is RDRedirect)
                    {
                        continue;
                    }
                    ResourceData res = resdatas[i] as ResourceData;
                    sw.WriteLine("<tr class='tr{6}'><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr>",
                                 res.Name, res.TotalCount, res.WholesalePrice, res.ResalePrice, res.TotalInvested, res.TotalValue, m++ % 2 + 1);
                }

                sw.WriteLine("</table>");
                sw.WriteLine("<h2>ResourceData Redirects</h2>");
                sw.WriteLine("<table width='90%'>");
                sw.WriteLine("<tr class='theader'><td>RDRedirect Name</td><td>Amount Factor</td><td>Price Factor</td><td>Wholesale Price</td></tr>");

                ArrayList rddatas = new ArrayList(ResourcePool.Resources.Values);
                rddatas.Sort();

                for (int i = 0, m = 0; i < rddatas.Count; i++)
                {
                    if (!(rddatas[i] is RDRedirect))
                    {
                        continue;
                    }
                    RDRedirect rd = rddatas[i] as RDRedirect;
                    sw.WriteLine("<tr class='tr{4}'><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>",
                                 rd.Name, rd.AmountFactor, rd.PriceFactor, rd.WholesalePrice, m++ % 2 + 1);
                }

                sw.WriteLine("</table>");

                sw.WriteLine("<h2>ResourceData Detail</h2>");
                for (int i = 0; i < resdatas.Count; i++)
                {
                    ResourceData rd = resdatas[i] as ResourceData;
                    sw.WriteLine("<a name='{0}'/>", rd.Name);
                    sw.WriteLine("<h3>{0} Detail</h3>", rd.Name);
                    sw.WriteLine("<table width='90%'>");
                    sw.WriteLine("<tr class='theader'><td>Player Name</td><td>Amount Invested</td><td>Price Sold</td><td>Total Investment</td></tr>");

                    ArrayList rclist = new ArrayList(rd.ConsignmentList);
                    rclist.Sort(new ConsignmentComparer());
                    ArrayList histogram = new ArrayList();
                    for (int a = 0, m = 0; a < rclist.Count; a++)
                    {
                        ResourceConsignment rc = rclist[a] as ResourceConsignment;
                        while (histogram.Count <= rc.Amount)
                        {
                            histogram.Add((double)0);
                        }
                        histogram[rc.Amount] = (double)(histogram[rc.Amount]) + (double)1;

                        sw.WriteLine("<tr class='tr{4}'><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>",
                                     rc.Seller.Name, rc.Amount, rc.Price, rc.Amount * rc.Price, m++ % 2 + 1);
                    }
                    if (histogram.Count > 0)
                    {
                        Graph g = new Graph("Consignment Amount Distribution", "Amount of Consignment", "# of Consignments", (double[])histogram.ToArray(typeof(double)));
                        g.Create(OUTPUT_DIRECTORY + "/" + rd.Name + " dist.gif");
                        sw.WriteLine("<img src='{0} dist.gif'/>", rd.Name);
                    }

                    sw.WriteLine("</table>");
                }
                sw.WriteLine("</body>");
                sw.WriteLine("</html>");
            }
            catch (Exception ex)
            {
                LogHelper.LogException(ex);
                Console.WriteLine("Error!");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                return;
            }
            finally
            {
                sw.Flush();
                sw.Close();
            }

            TimeSpan ts = DateTime.Now - begin;

            Console.WriteLine("finished in {0} seconds.", (double)ts.Milliseconds / (double)1000);
        }
Пример #3
0
		public static void Load(bool initonly)
		{
			if (initonly)
				Console.WriteLine("Initializing ResourcePool database...");
			else
				Console.WriteLine("Resource Pool Loading...");
			
			if (!Directory.Exists("Saves/ResourcePool"))
				Directory.CreateDirectory("Saves/ResourcePool");
			
			bool newconfig = false;
			FileStream rpfs = new FileStream("Saves/ResourcePool/ResourcePool.dat", FileMode.OpenOrCreate, FileAccess.Read);
			BinaryFileReader rpreader = new BinaryFileReader(new BinaryReader(rpfs));
			if (!rpreader.End())
			{
				int rpversion = rpreader.ReadInt();
				switch (rpversion)
				{
					case 0:
					{
						m_LastModified = rpreader.ReadDateTime();
						FileInfo fi = new FileInfo("Data/ResourcePool/config.xml");
						if (fi.LastWriteTime != m_LastModified)
						{
							m_LastModified = fi.LastWriteTime;
							newconfig = true;
						}
						else
							newconfig = false;

						break;
					}
					default:
						throw new Exception("Error loading ResourcePool: Invalid ResourcePool.dat save version");
				}
			}
			else
			{
				m_LastModified = (new FileInfo("Data/ResourcePool/config.xml")).LastWriteTime;
				Console.WriteLine("Warning: Saves/ResourcePool/ResourcePool.dat not found.");
				newconfig = true;
			}

			if (newconfig)
				Console.WriteLine("New configuration detected! Reading from Data/...");
			XmlTextReader reader;
			reader = new XmlTextReader((newconfig ? "Data/ResourcePool/config.xml" : "Saves/ResourcePool/config.xml"));
			reader.WhitespaceHandling = WhitespaceHandling.None;

			FileStream rcfs = new FileStream("Saves/ResourcePool/Consignments.dat", FileMode.OpenOrCreate, FileAccess.Read);
			FileStream dfs = new FileStream("Saves/ResourcePool/Debts.dat", FileMode.OpenOrCreate, FileAccess.Read);
			BinaryFileReader rcreader = new BinaryFileReader(new BinaryReader(rcfs));
			BinaryFileReader dreader = new BinaryFileReader(new BinaryReader(dfs));

			try { reader.MoveToContent(); }
			catch
			{
				Console.WriteLine("Save xml data not found or invalid, reverting to defaults");
				reader = new XmlTextReader("Data/ResourcePool/config.xml");
				reader.WhitespaceHandling = WhitespaceHandling.None;
				reader.MoveToContent();
			}
		
			int version = Int32.Parse(reader.GetAttribute("version"));
			reader.ReadStartElement("ResourcePool");

			switch (version)
			{
				case 0:
				{
					m_PaymentFactor = Double.Parse(reader.ReadElementString("PaymentFactor"));
					m_FailsafePriceHike = Double.Parse(reader.ReadElementString("FailsafePriceHike"));

					m_Resources = new Hashtable();
					while (reader.LocalName == "ResourceData")
					{
						ResourceData rd = new ResourceData(reader, (initonly ? null : rcreader));
						if (rd != null)
							m_Resources[rd.Type] = rd;
					}
					while (reader.LocalName == "RDRedirect")
					{
						RDRedirect rd = new RDRedirect(reader, (initonly ? null : rcreader));
						if (rd != null)
							m_Resources[rd.Type] = rd;
					}

					m_Debts = new Hashtable();

					if (initonly)
						break;

					while (!dreader.End())
					{
						try
						{
							Mobile m = dreader.ReadMobile();
							double debt = dreader.ReadDouble();
							if (m != null)
								m_Debts[m] = debt;
						}
						catch (Exception ex)
						{
							Console.WriteLine("Exception occured attempting to desrialize from Debts.dat : " + ex.Message );
							break;
						}
						
					}

					break;
				}

				case 1:
				{
					m_PaymentFactor = Double.Parse(reader.ReadElementString("PaymentFactor"));
					m_FailsafePriceHike = Double.Parse(reader.ReadElementString("FailsafePriceHike"));

					m_Resources = new Hashtable();
					while (reader.LocalName == "ResourceData")
					{
						ResourceData rd = new ResourceData(reader, (initonly ? null : rcreader));
						if (rd != null)
							m_Resources[rd.Type] = rd;
					}
					while (reader.LocalName == "RDRedirect")
					{
						RDRedirect rd = new RDRedirect(reader, (initonly ? null : rcreader));
						if (rd != null)
							m_Resources[rd.Type] = rd;
					}

					m_Debts = new Hashtable();

					if (initonly)
						break;

					// read count
					int count = dreader.ReadInt();

					for (int ix = 0; ix < count; ix++)
					{
						Mobile m = dreader.ReadMobile();
						double debt = dreader.ReadDouble();
						if (m != null)
							m_Debts[m] = debt;
					}

					break;
				}

				default:
				{
					throw new Exception("Invalid ResourcePool save version.");
				}
			}

			reader.ReadEndElement();
			reader.Close();
			rcfs.Close();
			rpfs.Close();
			dfs.Close();
		}