-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data.cs
58 lines (53 loc) · 2.38 KB
/
Data.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
using System.Globalization;
using System.Text.Json;
namespace Predictions
{
internal static class Data
{
public static ValueTask<Dictionary<string, Country>> ReadRankings(CancellationToken cancellationToken)
{
var fileList = Directory.GetFiles(Path.Combine(Environment.CurrentDirectory, "rankings"));
var dates = new List<string>(fileList.Length);
var database = new Dictionary<string, Country>();
var minimumPoints = double.MaxValue;
foreach (var fileName in fileList)
{
if (cancellationToken.IsCancellationRequested)
break;
var date = fileName.Split("\\").Last().Replace(".csv", "");
dates.Add(date);
using var file = File.OpenRead(fileName);
using var reader = new StreamReader(file);
var line = reader.ReadLine();
while (line != null)
{
if (cancellationToken.IsCancellationRequested)
break;
var splited = line.Split(',');
var countryName = splited[0];
var points = double.Parse(splited[1], CultureInfo.InvariantCulture);
if (minimumPoints > points)
minimumPoints = points;
if (!database.ContainsKey(countryName))
database.Add(countryName, new Country(countryName));
database[countryName].AddPoints(date, points);
line = reader.ReadLine();
}
}
minimumPoints -= 50;
foreach (var country in database.Values) {
foreach (var date in dates) {
if (!country.ContainsPoints(date))
country.AddPoints(date, minimumPoints);
}
country.CalculateStrength();
}
return ValueTask.FromResult(database);
}
public static ValueTask<IEnumerable<IEnumerable<string>>> ReadGroups(CancellationToken cancellationToken)
{
using var groupsFile = File.OpenRead(Path.Combine(Environment.CurrentDirectory, "groups.json"));
return JsonSerializer.DeserializeAsync<IEnumerable<IEnumerable<string>>>(groupsFile, cancellationToken: cancellationToken);
}
}
}