forked from Da-Teach/Questor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmMain.cs
137 lines (114 loc) · 4.42 KB
/
frmMain.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace UpdateInvTypes
{
using System.IO;
using System.Xml.Linq;
using Questor;
public partial class frmMain : Form
{
private bool _doUpdate;
private bool _updating;
private List<InvType> _invTypes;
public string InvTypesPath
{
get
{
return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\InvTypes.xml";
}
}
public frmMain()
{
InitializeComponent();
_invTypes = new List<InvType>();
var invTypes = XDocument.Load(InvTypesPath);
foreach (var element in invTypes.Root.Elements("invtype"))
_invTypes.Add(new InvType(element));
Progress.Step = 50;
Progress.Value = 0;
Progress.Minimum = 0;
Progress.Maximum = _invTypes.Count;
}
private void Update_Click(object sender, EventArgs e)
{
_doUpdate ^= true;
UpdateButton.Text = _doUpdate ? "Stop" : "Update";
if (!_doUpdate)
{
var xdoc = new XDocument(new XElement("invtypes"));
foreach (var type in _invTypes)
xdoc.Root.Add(type.Save());
xdoc.Save(InvTypesPath);
}
}
private void tUpdate_Tick(object sender, EventArgs e)
{
// This is what you get if your too bored to setup an actual thread and do UI-invoke shit
if (!_doUpdate)
return;
if (_updating)
return;
_updating = true;
try
{
var types = _invTypes.Skip(Progress.Value).Take(Progress.Step);
try
{
var needUpdating = types.Where(type => !type.LastUpdate.HasValue || DateTime.Now.Subtract(type.LastUpdate.Value).TotalDays > 7);
if (needUpdating.Count() == 0)
return;
var queryString = string.Join("&", types.Select(type => "typeid=" + type.Id).ToArray());
queryString += "®ionlimit=10000002";
var url = "http://api.eve-central.com/api/marketstat?" + queryString;
try
{
var prices = XDocument.Load(url);
if ((string)prices.Root.Attribute("method") != "marketstat_xml")
throw new Exception("Invalid XML method");
foreach (var type in prices.Root.Element("marketstat").Elements("type"))
{
var id = (int)type.Attribute("id");
var invType = types.Single(t => t.Id == id);
var all = type.Element("all");
if (all != null)
invType.MedianAll = (double?)all.Element("median");
var buy = type.Element("buy");
if (buy != null)
invType.MedianBuy = (double?)buy.Element("median");
var sell = type.Element("sell");
if (sell != null)
invType.MedianSell = (double?)sell.Element("median");
invType.LastUpdate = DateTime.Now;
}
}
catch (Exception ex)
{
}
}
finally
{
Progress.Value += types.Count();
if (Progress.Value >= _invTypes.Count - 1)
{
_doUpdate = false;
var xdoc = new XDocument(new XElement("invtypes"));
foreach (var type in _invTypes)
xdoc.Root.Add(type.Save());
xdoc.Save(InvTypesPath);
UpdateButton.Text = _doUpdate ? "Stop" : "Update";
}
}
}
finally
{
_updating = false;
}
}
}
}