This repository was archived by the owner on Jun 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarket.cs
More file actions
151 lines (117 loc) · 5.4 KB
/
Copy pathMarket.cs
File metadata and controls
151 lines (117 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;
namespace AutoEliteOCR {
public class ParseException : Exception {
public ParseException(string msg) : base(msg) { }
}
public class ConfidenceException : Exception {
public ConfidenceException(string msg) : base(msg) { }
}
public class Market {
public const string CsvHeader = "System;Station;Commodity;Sell;Buy;Demand;;Supply;;Date;";
public const string BpcHeader = "userID;System;Station;Commodity;Sell;Buy;Demand;;Supply;;Date;";
private const string DateFormat = "yyyy-MM-ddTHH:mm:sszzz";
public const float MinConfidence = 0.8f;
public Exception Error;
public string SystemName;
public string StationName;
public DateTime Date;
public List<Commodity> Commodities = new List<Commodity>();
public List<Commodity> Ignored = new List<Commodity>();
public string ToCsv() {
var sb = new StringBuilder();
foreach (var str in Commodities.Select(c => String.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9};",
SystemName, StationName, c.Name, c.Sell, c.Buy,
c.Demand, c.DemandLevel, c.Supply, c.SupplyLevel,
Date.ToString(DateFormat)))
) {
sb.AppendLine(str);
}
return sb.ToString();
}
public string ToBpc() {
var sb = new StringBuilder();
foreach (var str in Commodities.Select(c => String.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9};{10};",
Settings.Default.UserId, SystemName, StationName,
c.Name, c.Sell, c.Buy,
c.Demand, c.DemandLevel, c.Supply, c.SupplyLevel,
Date.ToString(DateFormat)))
) {
sb.AppendLine(str);
}
return sb.ToString();
}
public static Market FromXml(XmlDocument doc) {
var market = new Market();
var res = doc.SelectNodes("/ocrresult");
if (res == null || res.Count < 1) throw new Exception("Invalid XML");
var xml_setup = res[0]["setup"];
var xml_loc = res[0]["location"];
var xml_market = res[0]["market"];
if(xml_setup == null || xml_loc == null || xml_market == null) throw new ParseException("Invalid XML");
if (xml_setup["filetimestamp"] == null || xml_setup["ocrtime"] == null)
throw new ParseException("No file and parse timestamps found");
var a = Convert.ToDateTime(xml_setup["filetimestamp"].InnerText);
var b = Convert.ToDateTime(xml_setup["ocrtime"].InnerText);
market.Date = a > b ? a : b;
if(xml_loc["system"] == null) throw new ParseException("No system name found, make sure eliteOCR works properly");
if (xml_loc["station"] == null) throw new ParseException("No station name found, invalid xml.");
market.SystemName = xml_loc["system"].InnerText;
market.StationName = xml_loc["station"].InnerText;
var conf = Convert.ToDouble(xml_loc["station"].Attributes["conf"].InnerText);
if(conf < Market.MinConfidence) throw new ConfidenceException("Confidence of system name is to low");
foreach (XmlElement entry in xml_market.ChildNodes) {
var c = Commodity.FromXmlEntry(entry);
if (c.Confidence >= Market.MinConfidence)
market.Commodities.Add(c);
else
market.Ignored.Add(c);
}
return market;
}
}
public class Commodity {
public float Confidence = 1.0f;
public string Name = null;
public int? Sell = null;
public int? Buy = null;
public int? Demand = null;
public string DemandLevel = null;
public int? Supply = null;
public string SupplyLevel = null;
public static readonly Dictionary<string, string> XmlLookup = new Dictionary<string, string> {
{ "Name", "commodity" },
{ "Sell", "sell" },
{ "Buy", "buy" },
{ "Demand", "demand" },
{ "DemandLevel", "demandlevel" },
{ "Supply", "supply" },
{ "SupplyLevel", "supplylevel" }
};
public static Commodity FromXmlEntry(XmlElement entry) {
var c = new Commodity();
float conf = 1.0f;
XmlElement el;
FieldInfo prop;
foreach (var kvp in XmlLookup) {
prop = c.GetType().GetField(kvp.Key);
el = entry[kvp.Value];
// null set continue
if (el == null || el.InnerText == String.Empty) continue;
// set confidence to minimum found
conf = Math.Min(conf, float.Parse(el.GetAttribute("conf")));
if (conf < Market.MinConfidence) continue;
// Convert the value type and set the value
Type t = Nullable.GetUnderlyingType(prop.FieldType) ?? prop.FieldType;
prop.SetValue(c, Convert.ChangeType(el.InnerText, t));
}
c.Confidence = conf;
return c;
}
}
}