Skip to content

Commit

Permalink
generate realistic data
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Miller committed Dec 4, 2024
1 parent f41c6ed commit d29693e
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions Maui.DataGrid.Sample/Utils/DummyDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,64 @@ namespace Maui.DataGrid.Sample.Utils;

internal static class DummyDataProvider
{
private static List<Team>? _realTeams;
private static readonly List<Team> RealTeams = LoadTeamsFromResource();

public static List<Team> GetTeams(int numberOfCopies = 1)
{
if (_realTeams == null)
if (numberOfCopies == 1)
{
var assembly = typeof(DummyDataProvider).GetTypeInfo().Assembly;
return RealTeams;
}

using var stream = assembly.GetManifestResourceStream("Maui.DataGrid.Sample.teams.json")
?? throw new FileNotFoundException("Could not load teams.json");
return GenerateRandomTeams(numberOfCopies);
}

using var reader = new StreamReader(stream);
var json = reader.ReadToEnd();
private static List<Team> LoadTeamsFromResource()
{
var assembly = typeof(DummyDataProvider).GetTypeInfo().Assembly;

_realTeams = JsonSerializer.Deserialize<List<Team>>(json)
?? throw new InvalidOperationException("Could not deserialize teams.json");
}
using var stream = assembly.GetManifestResourceStream("Maui.DataGrid.Sample.teams.json")
?? throw new FileNotFoundException("Could not load teams.json");

if (numberOfCopies == 1)
{
return _realTeams;
}
using var reader = new StreamReader(stream);
var json = reader.ReadToEnd();

return JsonSerializer.Deserialize<List<Team>>(json)
?? throw new InvalidOperationException("Could not deserialize teams.json");
}

var teams = new List<Team>(_realTeams);
private static List<Team> GenerateRandomTeams(int numberOfCopies)
{
var teams = new List<Team>(RealTeams);

for (var i = 0; i < numberOfCopies; i++)
{
foreach (var realTeam in _realTeams)
foreach (var realTeam in RealTeams)
{
var totalGames = RandomNumberGenerator.GetInt32(72, 83); // Total games in a season
var won = RandomNumberGenerator.GetInt32(20, Math.Min(60, totalGames)); // Random wins between 20 and the lesser of 60 or total games
var lost = totalGames - won; // Losses are the remaining games
var homeWins = RandomNumberGenerator.GetInt32(10, Math.Min(won, totalGames / 2)); // Home wins between 10 and half of total games or total wins
var roadWins = won - homeWins; // Road wins are the remaining wins
var last10Wins = RandomNumberGenerator.GetInt32(0, Math.Min(10, won)); // Wins in the last 10 games, but not more than total wins
var streakResult = (GameResult)RandomNumberGenerator.GetInt32(0, 2); // Random streak result (Win or Loss)
var streakLength = Math.Min(10, streakResult == GameResult.Won ? won : lost); // Streak length between 0 and 10, but not more than total wins or losses

var randomTeam = new Team
{
Name = $"{realTeam.Name} {i}",
Won = RandomNumberGenerator.GetInt32(0, 50),
Lost = RandomNumberGenerator.GetInt32(0, 50),
Percentage = Math.Round(RandomDouble() * 100) / 100,
Won = won,
Lost = lost,
Percentage = (double)won / totalGames,
Conf = $"{realTeam.Conf} {RandomNumberGenerator.GetInt32(1, 10)}",
Div = $"{realTeam.Div} {RandomNumberGenerator.GetInt32(1, 10)}",
Home = $"{RandomNumberGenerator.GetInt32(1, 10)}",
Road = $"{RandomNumberGenerator.GetInt32(1, 10)}",
Last10 = $"{RandomNumberGenerator.GetInt32(1, 10)}",
Home = $"{homeWins}-{(totalGames / 2) - homeWins}", // Home record
Road = $"{roadWins}-{(totalGames / 2) - roadWins}", // Road record
Last10 = $"{last10Wins}-{10 - last10Wins}", // Last 10 games record
Streak = new Streak
{
Result = (GameResult)RandomNumberGenerator.GetInt32(0, 2),
NumStreak = RandomNumberGenerator.GetInt32(0, 10),
Result = streakResult,
NumStreak = streakLength,
},
Logo = realTeam.Logo,
};
Expand All @@ -61,6 +75,4 @@ public static List<Team> GetTeams(int numberOfCopies = 1)

return teams;
}

private static double RandomDouble() => (double)RandomNumberGenerator.GetInt32(int.MaxValue) / int.MaxValue;
}

0 comments on commit d29693e

Please sign in to comment.