-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameHubPages.xaml.cs
187 lines (154 loc) · 7.22 KB
/
GameHubPages.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Web.Http; //For POST method
using System.Threading.Tasks; //For POST method
using System.Collections.ObjectModel; //Used to notify listview values when objects are changed
using System.Diagnostics; //Debug
using System.Text.Json; //Used for (de)serizalisation and JSON manipulation
using System.Text.Json.Serialization; //Used as serialization library
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
namespace ReviewR
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class GameHubPages : Page
{
public GameHubPages()
{
this.InitializeComponent();
//Waits till page is fully loaded before running the event
this.Loaded += Page_Loaded;
}
public partial class GameListObject
{
[JsonPropertyName("id")]
public long GameID { get; set; }
[JsonPropertyName("name")]
public string GameName { get; set; }
[JsonPropertyName("release_dates")]
public ObservableCollection<ReleaseDate> ReleaseDates { get; set; }
[JsonPropertyName("url")]
public string WebsiteLink { get; set; }
[JsonPropertyName("genres")]
public ObservableCollection<GameGenre> GameGenres { get; set; }
[JsonPropertyName("summary")]
public string GameSummary { get; set; }
[JsonPropertyName("platforms")]
public ObservableCollection<GamePlatform> GamePlatforms { get; set; }
}
public partial class GameGenre
{
[JsonPropertyName("id")]
public long Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
}
public partial class GamePlatform
{
[JsonPropertyName("id")]
public long Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
}
public partial class ReleaseDate
{
[JsonPropertyName("id")]
public long Id { get; set; }
[JsonPropertyName("human")]
public string Human { get; set; }
}
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
username_text.Text = GameHubs.GameSpecificGameName + " [" + GameHubs.GameSpecificGameID + "]";
try
{
// Construct the HttpClient and Uri
HttpClient httpClient = new HttpClient();
Uri uri = new Uri("https://api.igdb.com/v4/games");
httpClient.DefaultRequestHeaders.Add("Client-ID", App.GlobalClientidIGDB);
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + App.GlobalAccessIGDB);
//Debug.WriteLine("Request Headers: ");
// Construct the JSON to post
HttpStringContent content = new HttpStringContent($"fields name,genres.name,summary,platforms.name,release_dates.human,external_games.category,url; where id = {GameHubs.GameSpecificGameID};");
Debug.WriteLine("Request Contents: " + content);
// Post the JSON and wait for a response
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(
uri,
content);
// Make sure the post succeeded, and write out the response
httpResponseMessage.EnsureSuccessStatusCode();
var httpResponseBody = await httpResponseMessage.Content.ReadAsStringAsync();
Debug.WriteLine("Request Response: " + httpResponseBody);
//Deserialise the return output into game id, game name and release date
List<GameListObject> gamelistobjects = JsonSerializer.Deserialize<List<GameListObject>>(httpResponseBody);
//Create ObservableCollection which uses the deserialized items
ObservableCollection<GameListObject> dataList = new ObservableCollection<GameListObject>(gamelistobjects);
ObservableCollection<GameListObject> GameList = new ObservableCollection<GameListObject>();
//For each item that is within the dataList
foreach (var item in dataList)
{
Debug.WriteLine($"id: {item.GameID}");
Debug.WriteLine($"name: {item.GameName}");
Debug.WriteLine($"summary: {item.GameSummary}");
Debug.WriteLine($"url: {item.WebsiteLink}");
WebsiteURI = item.WebsiteLink;
game_summary.Text = item.GameSummary;
//If there are no genres set, then don't run the statement
if (item.GameGenres != null)
{
//For each genre that is found within releasedates
foreach (var genre in item.GameGenres)
{
//FetchedGenre = Convert.ToString(genre.Id); //Select the genre ids rather than plain name
game_genre.Text = game_genre.Text + genre.Name + " [" + genre.Id + "], ";
Debug.WriteLine($"genres: {genre.Name}");
Debug.WriteLine($"genreid: {genre.Id}");
}
}
//If there are no platforms set, then don't run the statement
if (item.GamePlatforms != null)
{
//For each genre that is found within releasedates
foreach (var platform in item.GamePlatforms)
{
game_platform.Text = game_platform.Text + platform.Name + ", ";
Debug.WriteLine($"platforms: {platform.Name}");
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
public static string WebsiteURI;
private async void website_button_Click(object sender, RoutedEventArgs e)
{
string uriToLaunch = $"{WebsiteURI}";
var uri = new Uri(uriToLaunch);
// Set the option to show a warning
var options = new Windows.System.LauncherOptions();
options.TreatAsUntrusted = true;
// Launch the URI with a warning prompt
await Windows.System.Launcher.LaunchUriAsync(uri, options);
}
private void reviews_button_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(ReviewSystem), null); //Switch to the review landing page for search
}
}
}