-
Notifications
You must be signed in to change notification settings - Fork 5
/
HDTAHPluginHAPlugin.cs
238 lines (199 loc) · 8.5 KB
/
HDTAHPluginHAPlugin.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using AHPlugins;
using Hearthstone_Deck_Tracker;
using Hearthstone_Deck_Tracker.Hearthstone;
using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Threading.Tasks;
namespace HDTAHPluginHAPlugin
{
public class HDTAHPluginHAPlugin : AHPlugin
{
private Hashtable heartharenaheromapping = new Hashtable();
private Hashtable heartharenacardidmapping = new Hashtable();
private int heroid;
private List<int> pickedcards = new List<int>();
public override string Name
{
get { return "HDTAHPluginHAPlugin"; }
}
public override string Author
{
get { return "corlettb"; }
}
public override Version Version
{
get { return new Version("0.0.4"); }
}
public HDTAHPluginHAPlugin()
{
// Plugin constructor
Logger.WriteLine("HDTAHPluginHAPlugin constructor");
// Populate heartharena class mappings
heartharenaheromapping["druid"] = 1;
heartharenaheromapping["hunter"] = 2;
heartharenaheromapping["mage"] = 3;
heartharenaheromapping["paladin"] = 4;
heartharenaheromapping["priest"] = 5;
heartharenaheromapping["rogue"] = 6;
heartharenaheromapping["shaman"] = 7;
heartharenaheromapping["warlock"] = 8;
heartharenaheromapping["warrior"] = 9;
LoadHearthArenaCards();
Logger.WriteLine("HearthArena cards loaded");
}
private void LoadHearthArenaCards()
{
string assemblylocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string heartharenafile = Path.Combine(assemblylocation, "heartharena.json");
JObject json = JObject.Parse(File.ReadAllText(heartharenafile));
foreach (var item in json)
{
JToken value = item.Value;
heartharenacardidmapping[value["name"].ToString().ToLower()] = value["id"];
}
}
private string convertList(List<int> list)
{
string returnstr = "";
string seperator = "";
foreach (int id in list)
{
returnstr += seperator + id.ToString();
seperator = "-";
}
if (returnstr == "")
{
returnstr = "-";
}
return returnstr;
}
private int cardToHAID(Card card)
{
string cardname = card.Name.ToLower();
if (!heartharenacardidmapping.ContainsKey(cardname))
{
throw new Exception("Unable to find heartharena mapping for card with name " + cardname);
}
return Convert.ToInt32(heartharenacardidmapping[cardname].ToString());
}
private List<int> cardsToHAIDs(List<Card> cards)
{
List<int> returnlist = new List<int>();
foreach (Card card in cards)
{
returnlist.Add(cardToHAID(card));
}
return returnlist;
}
private string getHearthArenaUrl(List<Card> newcards)
{
return "http://draft.heartharena.com/arena/option-multi-score/" + heroid + "/" + convertList(pickedcards) + "/" + convertList(cardsToHAIDs(newcards));
}
private async Task<JObject> getHearthAranaData(string url)
{
string heartharenadatastr = "";
using (WebClient wc = new WebClient())
{
wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Logger.WriteLine("Making heartharena call to " + url + "...");
Stopwatch sw = new Stopwatch();
sw.Start();
heartharenadatastr = await wc.DownloadStringTaskAsync(url);
sw.Stop();
Logger.WriteLine("... Call took " + sw.ElapsedMilliseconds + "ms");
}
JObject heartharenadata = JObject.Parse(heartharenadatastr);
return heartharenadata;
}
public override async Task<List<string>> GetCardValues(ArenaHelper.Plugin.ArenaData arenadata, List<Card> newcards, List<string> defaultvalues)
{
List<string> values = new List<string>();
string url = getHearthArenaUrl(newcards);
JObject heartharenadata;
try
{
heartharenadata = await getHearthAranaData(url);
values.Add(heartharenadata["results"][0]["card"]["score"].ToString() + " (" + defaultvalues[0] + ")");
values.Add(heartharenadata["results"][1]["card"]["score"].ToString() + " (" + defaultvalues[1] + ")");
values.Add(heartharenadata["results"][2]["card"]["score"].ToString() + " (" + defaultvalues[2] + ")");
string heartharenamessage = heartharenadata["tip"]["text"].ToString();
Regex rgx = new Regex("<[^>]+/?>");
heartharenamessage = rgx.Replace(heartharenamessage, "*");
values.Add("HEARTHARENA: "+heartharenamessage+"\n== Default tier list score in brackets");
}
catch
{
for (int i = 0; i < 3; i++)
{
values.Add(defaultvalues[i]);
}
values.Add("Failed to get heartharena data. Returning default tier list");
}
return values;
}
public override async void NewArena(ArenaHelper.Plugin.ArenaData arenadata)
{
Logger.WriteLine("New Arena: " + arenadata.deckname);
pickedcards.Clear();
}
public override async void HeroesDetected(ArenaHelper.Plugin.ArenaData arenadata, string heroname0, string heroname1, string heroname2)
{
Logger.WriteLine("Heroes Detected: " + heroname0 + ", " + heroname1 + ", " + heroname2);
}
public override async void HeroPicked(ArenaHelper.Plugin.ArenaData arenadata, string heroname)
{
heroid = Convert.ToInt32(heartharenaheromapping[heroname.ToLower()]);
Logger.WriteLine("Hero Picked: " + heroname + ", id: " + heroid);
}
public override async void CardPicked(ArenaHelper.Plugin.ArenaData arenadata, int pickindex, Card card)
{
Logger.WriteLine("Card Picked: " + card.Name);
string cardname = card.Name.ToLower();
if (!heartharenacardidmapping.ContainsKey(cardname))
{
throw new Exception("Unable to find heartharena mapping for card with name " + cardname);
}
pickedcards.Add(Convert.ToInt32(heartharenacardidmapping[cardname].ToString()));
}
public override async void Done(ArenaHelper.Plugin.ArenaData arenadata)
{
Logger.WriteLine("Done");
}
public override async void ResumeArena(ArenaHelper.Plugin.ArenaData arenadata, ArenaHelper.Plugin.PluginState state)
{
Logger.WriteLine("Resuming Arena");
foreach (var heroname in arenadata.detectedheroes)
{
ArenaHelper.Plugin.HeroHashData hero = ArenaHelper.Plugin.GetHero(heroname);
Logger.WriteLine("Detected hero: " + hero.name);
}
if (arenadata.pickedhero != "")
{
ArenaHelper.Plugin.HeroHashData hero = ArenaHelper.Plugin.GetHero(arenadata.pickedhero);
heroid = Convert.ToInt32(heartharenaheromapping[hero.name.ToLower()]);
Logger.WriteLine("Picked hero: " + hero.name + ", id: " + heroid);
}
pickedcards.Clear();
foreach (var cardid in arenadata.pickedcards)
{
Card card = ArenaHelper.Plugin.GetCard(cardid);
pickedcards.Add(cardToHAID(card));
Logger.WriteLine(card.Name);
}
Logger.WriteLine("HearthArena picklist: " + convertList(pickedcards));
Logger.WriteLine("State: " + ArenaHelper.Plugin.GetState().ToString());
}
public override async void CloseArena(ArenaHelper.Plugin.ArenaData arenadata, ArenaHelper.Plugin.PluginState state)
{
Logger.WriteLine("Closing");
}
}
}