-
Notifications
You must be signed in to change notification settings - Fork 0
/
Index.razor
206 lines (184 loc) · 5.95 KB
/
Index.razor
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
@page "/"
@inject IJSRuntime JSRuntime
<h3>Select a JSON file to convert to Markdown</h3>
<div class="file-input">
<InputFile OnChange="@HandleFileChange" accept="application/json" multiple="false" />
</div>
@if (errorMessage != null)
{
<div>
<p>Error: @errorMessage</p>
</div>
}
else if (markdownOutput != null)
{
<div class="file-output">
<button @onclick="SaveFile">Save Markdown</button>
</div>
}
else if (filename != null)
{
<div>
<p>Markdown saved!</p>
</div>
}
else if (isLoading)
{
<div>
<p>Loading...</p>
</div>
} else if (jsonData != null)
{
<div>
<button @onclick="ConvertData">Convert to Markdown</button>
</div>
}
@code {
private string? errorMessage;
private string markdownOutput;
private string filename;
private long maxFileSize = 1024 * 150; // 150 KB
private string jsonData;
private bool isLoading;
private async Task HandleFileChange(InputFileChangeEventArgs e)
{
isLoading = true;
var file = e.File;
if (file != null && file.ContentType == "application/json")
{
try
{
var buffer = new byte[file.Size];
await file.OpenReadStream(maxFileSize).ReadAsync(buffer);
jsonData = Encoding.UTF8.GetString(buffer);
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
}
isLoading = false;
}
private void ConvertData()
{
// Error handling if the JSON data is not in the expected format.
try
{
var steamData = JsonConvert.DeserializeObject<SteamData>(jsonData);
markdownOutput = ConvertToMarkdown(steamData);
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
}
static string ConvertToMarkdown(SteamData steamData)
{
StringBuilder markdown = new StringBuilder();
markdown.AppendLine("# Steam Wishlist");
markdown.AppendLine();
// Add general information section
markdown.AppendLine("## General Info");
markdown.AppendLine();
markdown.AppendLine($"- Number of games: {GetNumberOfGames(steamData)}");
markdown.AppendLine($"- Average price: {GetAveragePrice(steamData):F2} €"); // F2 formats to 2 decimal places
markdown.AppendLine($"- Total price: {GetTotalPrice(steamData):F2} €"); // F2 formats to 2 decimal places
markdown.AppendLine();
// Add game list section
markdown.AppendLine("## Game List");
markdown.AppendLine();
foreach (var entry in steamData.Data)
{
if (entry.Type != "Game" || string.IsNullOrEmpty(entry.Price) || entry.Price == "N/A")
continue;
string price = FormatPrice(entry.Price);
markdown.AppendLine($"### {entry.Title}");
markdown.AppendLine();
markdown.AppendLine($"- Price: {price} €");
markdown.AppendLine($"- GameID: {GetGameID(entry.GameId)}");
markdown.AppendLine($"- URL: {entry.Url}");
markdown.AppendLine();
}
return markdown.ToString();
}
static int GetNumberOfGames(SteamData steamData)
{
int count = 0;
foreach (var entry in steamData.Data)
{
if (entry.Type == "Game" && !string.IsNullOrEmpty(entry.Price) && entry.Price != "N/A")
count++;
}
return count;
}
static double GetAveragePrice(SteamData steamData)
{
double total = 0;
int count = 0;
foreach (var entry in steamData.Data)
{
if (entry.Type == "Game" && !string.IsNullOrEmpty(entry.Price) && entry.Price != "N/A")
{
total += Convert.ToDouble(entry.Price);
count++;
}
}
return count > 0 ? total / count / 100 : 0; // Convert total from cents to euros and calculate average
}
static double GetTotalPrice(SteamData steamData)
{
double total = 0;
foreach (var entry in steamData.Data)
{
if (entry.Type == "Game" && !string.IsNullOrEmpty(entry.Price) && entry.Price != "N/A")
{
total += Convert.ToDouble(entry.Price);
}
}
return total / 100; // Convert total from cents to euros
}
static string GetGameID(List<string> gameID)
{
if (gameID == null || gameID.Count < 2)
return "N/A";
return gameID[1].Replace("app/", "");
}
static string FormatPrice(string price)
{
if (string.IsNullOrEmpty(price))
return "N/A";
double priceValue = Convert.ToDouble(price);
string formattedPrice = $"{priceValue / 100:F2}"; // Convert cents to Euros with 2 decimal places
return formattedPrice;
}
private void SaveFile()
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.DefaultExt = "md";
saveFileDialog.Filter = "Markdown files (*.md)|*.md|All files (*.*)|*.*";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.Title = "Save Markdown File";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string filename = saveFileDialog.FileName;
File.WriteAllTextAsync(filename, markdownOutput);
}
}
class SteamData
{
public string Version { get; set; }
public List<SteamEntry> Data { get; set; }
}
class SteamEntry
{
public List<string> GameId { get; set; }
public string Title { get; set; }
public string Url { get; set; }
public string Type { get; set; }
public string? ReleaseDate { get; set; }
public string? Note { get; set; }
public string? Price { get; set; }
public int? Discount { get; set; }
}
}