-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
189 lines (164 loc) · 6.87 KB
/
MainWindow.xaml.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Runtime.Remoting.Channels;
using System.Runtime.Serialization;
using System.Windows.Media.Imaging;
using Newtonsoft.Json.Linq;
using System.Threading;
using System.Windows.Documents;
using System.Diagnostics;
namespace PokemonWPF
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static MainWindow mainWindow;
private Logger logger = new Logger(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\.pokemon\logs\", "%date%.log");
private string requestURL = "https://localhost:44330/api/pokemon";
public JArray RequestJsonArray;
private bool upToDate = false;
public MainWindow()
{
mainWindow = this;
try
{
InitializeComponent();
logger.Log("---------------------------------------------------");
logger.Log("Initialize Componets...");
initData();
infoBanner.Text = "RequestURL: " + requestURL;
}
catch (Exception e)
{ logger.Error(e.Message + "\n" + e.StackTrace); }
}
public void initData()
{
int defaultPkmn = 1;
logger.Log("Getting json from \"" + requestURL + "\"");
RequestJsonArray = makeRequest(requestURL);
new Thread(() => DownloadImages(RequestJsonArray)).Start();
if(!upToDate)
{
foreach(JObject jsonItem in RequestJsonArray)
{
if (jsonItem.Value<int>("number") == defaultPkmn)
{
while (liveLogger.Content.ToString() == jsonItem["sprite"].ToString()) { Thread.Sleep(10); }
break;
}
}
}
SetPokemonData(defaultPkmn, RequestJsonArray);
}
public void DownloadImages(JArray jsonArray)
{
string savePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +"/.pokemon/images/pokemon/";
if (!Directory.Exists(savePath))
Directory.CreateDirectory(savePath);
if (!upToDate)
{
if (jsonArray == null)
return;
foreach (JObject json in jsonArray)
{
string link = json["sprite"].ToString();
string fileName = link.Split('/').Last<String>();
if (File.Exists(savePath + fileName))
{
logger.Log("File \"" + savePath + fileName + "\" already exists. Skipping download!");
continue;
}
logger.Log("Downloading image: \"" + json["sprite"] + "\"");
if (!WebDownloader.Download(link, savePath + fileName))
MessageBox.Show("ERROR:\n\nFileNotFound: " + link);
}
}
logger.Log("Done!");
}
public void SetPokemonData(int id, JArray requestJsonArray)
{
if (requestJsonArray == null)
return;
foreach (JObject json in requestJsonArray)
{
if(json["number"].ToString() == id.ToString())
{
logger.Log("Show pokemon " + json["number"].ToString() + " (" + json["pokemon"].ToString() + ")");
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/.pokemon/images/pokemon/";
string fileName = json["sprite"].ToString().Split('/').Last<String>();
pokemonImage.Source = new BitmapImage(new Uri(filePath + fileName, UriKind.Absolute));
pokemonImage.ToolTip = json["sprite"].ToString();
pokemonImageLink.NavigateUri = new Uri(json["sprite"].ToString(), UriKind.Absolute);
pokemonID.Content = json["number"];
pokemonName.Content = json["pokemon"];
if(json["type2"].ToString() == "none")
pokemonType.Content = json["type1"];
else
pokemonType.Content = json["type1"] + "; " + json["type2"];
pokemonLives.Content = json["hp"];
pokemonDamage.Content = json["attack"];
return;
}
}
}
private JArray makeRequest(string url)
{
string code = null;
string responseString = null;
WebRequest request = WebRequest.Create(url);
try
{
WebResponse response = request.GetResponse();
using (Stream st = response.GetResponseStream())
{
StreamReader reader = new StreamReader(st);
responseString = reader.ReadToEnd();
}
responseCode.Content = "Response Status: OK";
imageConnectionStatus.Source = new BitmapImage(new Uri("icons/connection/connected_16x16.png", UriKind.Relative));
code = null;
}
catch (WebException e)
{
code = e.Message.ToString();
responseCode.Content = "Response Status: ERROR";
imageConnectionStatus.Source = new BitmapImage(new Uri("icons/connection/disconnected_16x16.png", UriKind.Relative));
}
responseCode.ToolTip = code;
return responseString != null ? JArray.Parse(responseString) : null;
}
private void previousDataSet_Click(object sender, RoutedEventArgs e)
{
SetPokemonData(Int32.Parse(pokemonID.Content.ToString()) - 1, RequestJsonArray);
}
private void nextDataSet_Click(object sender, RoutedEventArgs e)
{
SetPokemonData(Int32.Parse(pokemonID.Content.ToString()) + 1, RequestJsonArray);
}
private void PokemonImageLink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
// Start Link in Browser
Hyperlink hl = (Hyperlink)sender;
string navigateUri = hl.NavigateUri.ToString();
Process.Start(new ProcessStartInfo(navigateUri));
e.Handled = true;
}
private void SearchDataSet_Click(object sender, RoutedEventArgs e)
{
Window w = new SearchWindow(this);
w.Show();
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
initData();
}
}
}