Skip to content

Commit

Permalink
Fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Heufneutje committed Dec 1, 2024
1 parent f1aab72 commit 3cb271f
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 67 deletions.
4 changes: 2 additions & 2 deletions SpeedrunTracker/Models/RunDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ public class RunDetails
{
public int Place { get; set; }
public required Speedrun Run { get; set; }
public required Category Category { get; set; }
public Category? Category { get; set; }
public Level? Level { get; set; }
public required GamePlatform Platform { get; set; }
public GamePlatform? Platform { get; set; }
public required List<RunVariable> Variables { get; set; }
public GameAssets? GameAssets { get; set; }
public Ruleset? Ruleset { get; set; }
Expand Down
30 changes: 17 additions & 13 deletions SpeedrunTracker/Models/SpeedrunDotCom/LeaderboardEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,47 @@ public record LeaderboardEntry
{
public int Place { get; set; }
public required Speedrun Run { get; set; }
public BaseData<BaseGame> Game { get; set; }
public BaseData<BaseGame>? Game { get; set; }

// For some ungodly reason it's a single object when there is a level and an empty list if there's not.
[JsonPropertyName("level")]
public BaseData<object> LevelJson { get; set; }
public BaseData<object>? LevelJson { get; set; }

public BaseData<Category> Category { get; set; }
public BaseData<Category>? Category { get; set; }

// For some ungodly reason it's a single object when there is a platform and an empty list if there's not.
[JsonPropertyName("platform")]
public BaseData<object> PlatformJson { get; set; }
public BaseData<object>? PlatformJson { get; set; }

private Level _level;
private Level? _level;

[JsonIgnore]
public Level Level
public Level? Level
{
get
{
_level ??= Run.LevelId == null ? null : JsonSerializer.Deserialize<Level>(LevelJson.Data.ToString());
_level ??= Run.LevelId == null || LevelJson?.Data == null
? null
: JsonSerializer.Deserialize<Level>(LevelJson.Data.ToString()!);
return _level;
}
}

private GamePlatform _platform;
private GamePlatform? _platform;

[JsonIgnore]
public GamePlatform Platform
public GamePlatform? Platform
{
get
{
_platform ??= Run?.System?.PlatformId == null ? null : JsonSerializer.Deserialize<GamePlatform>(PlatformJson.Data.ToString());
_platform ??= Run?.System?.PlatformId == null || PlatformJson?.Data == null
? null
: JsonSerializer.Deserialize<GamePlatform>(PlatformJson.Data.ToString()!);
return _platform;
}
}

private string _ordinalPlace;
private string? _ordinalPlace;

[JsonIgnore]
public string OrdinalPlace
Expand All @@ -55,10 +59,10 @@ public string OrdinalPlace
}
}

private Asset _trophyAsset;
private Asset? _trophyAsset;

[JsonIgnore]
public Asset TrophyAsset
public Asset? TrophyAsset
{
get
{
Expand Down
11 changes: 10 additions & 1 deletion SpeedrunTracker/Models/SpeedrunDotCom/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ public record User : BaseSpeedrunObject
public UserAssets? Assets { get; set; }

[JsonIgnore]
public string? DisplayName => PlayerType == PlayerType.Guest ? Name : string.IsNullOrEmpty(Names?.International) ? Names?.Japanese : Names?.International;
public string? DisplayName
{
get
{
if (PlayerType == PlayerType.Guest)
return Name;

return string.IsNullOrEmpty(Names?.International) ? Names?.Japanese : Names.International;
}
}

public static User GetUserNotFoundPlaceholder()
{
Expand Down
4 changes: 2 additions & 2 deletions SpeedrunTracker/Services/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public Task ShowAlertAsync(string title, string message, string cancel = "OK")
public async Task<bool> ShowConfirmationAsync(string title, string message, string accept = "Yes", string cancel = "No")
{
Page? mainPage = GetMainPage();
return mainPage != null ? await mainPage.DisplayAlert(title, message, accept, cancel) : false;
return mainPage != null && await mainPage.DisplayAlert(title, message, accept, cancel);
}

private Page? GetMainPage() => Application.Current?.Windows[0].Page;
private static Page? GetMainPage() => Application.Current?.Windows[0].Page;
}
2 changes: 1 addition & 1 deletion SpeedrunTracker/Services/EmbedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private EmbeddableUrl HandleYouTube(string url)
{
Uri uri = new(url);
NameValueCollection query = HttpUtility.ParseQueryString(uri.Query);
string? videoId = query.AllKeys.Contains("v") == true ? query["v"] : uri.Segments[^1];
string? videoId = query.AllKeys.Contains("v") ? query["v"] : uri.Segments[^1];
return new EmbeddableUrl()
{
Url = url,
Expand Down
94 changes: 55 additions & 39 deletions SpeedrunTracker/ViewModels/GameDetailViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class GameDetailViewModel : BaseFollowViewModel<Game>
private readonly ILeaderboardService _leaderboardService;
private readonly IUserService _userService;
private readonly ILocalSettingsService _settingsService;
private IEnumerable<Category> _fullGameCategories;
private IEnumerable<Category> _levelCategories;
private IEnumerable<Variable> _allVariables;
private IEnumerable<Category>? _fullGameCategories;
private IEnumerable<Category>? _levelCategories;
private IEnumerable<Variable>? _allVariables;
private int _leaderboardEntriesVisible;
private const int _leaderboardEntriesStepSize = 10;

Expand All @@ -27,7 +27,7 @@ public GameDetailViewModel(IGameService gameService, ILeaderboardService leaderb
LeaderboardEntries = new RangedObservableCollection<LeaderboardEntry>();
}

public Game Game
public Game? Game
{
get => _followEntity;
set
Expand All @@ -38,16 +38,16 @@ public Game Game
}
}

private ObservableCollection<Category> _categories;
private ObservableCollection<Category>? _categories;

public ObservableCollection<Category> Categories
{
get => _categories;
get => _categories ?? [];
set
{
if (_categories.SequenceEqualOrNull(value))
if (_categories != null && _categories.SequenceEqualOrNull(value))
{
if (_allVariables.Any(x => x.Scope.Type == VariableScopeType.SingleLevel))
if (_allVariables != null && _allVariables.Any(x => x.Scope.Type == VariableScopeType.SingleLevel))
UpdateVariables();
return;
}
Expand All @@ -58,9 +58,9 @@ public ObservableCollection<Category> Categories
}
}

private Category _selectedCategory;
private Category? _selectedCategory;

public Category SelectedCategory
public Category? SelectedCategory
{
get => _selectedCategory;
set
Expand All @@ -75,14 +75,14 @@ public Category SelectedCategory
}
}

private ObservableCollection<Level> _levels;
private ObservableCollection<Level>? _levels;

public ObservableCollection<Level> Levels
{
get => _levels;
get => _levels ?? [];
set
{
if (_levels.SequenceEqualOrNull(value))
if (_levels == null || _levels.SequenceEqualOrNull(value))
return;

_levels = value;
Expand All @@ -92,9 +92,9 @@ public ObservableCollection<Level> Levels
}
}

private Level _selectedLevel;
private Level? _selectedLevel;

public Level SelectedLevel
public Level? SelectedLevel
{
get => _selectedLevel;
set
Expand All @@ -104,26 +104,26 @@ public Level SelectedLevel

_selectedLevel = value;
NotifyPropertyChanged();
Categories = string.IsNullOrEmpty(value.Id) ? _fullGameCategories.AsObservableCollection() : _levelCategories.AsObservableCollection();
Categories = string.IsNullOrEmpty(value?.Id) ? (_fullGameCategories ?? []).AsObservableCollection() : (_levelCategories ?? []).AsObservableCollection();
}
}

private ObservableCollection<VariableViewModel> _variables;
private ObservableCollection<VariableViewModel>? _variables;

public ObservableCollection<VariableViewModel> Variables
{
get => _variables;
get => _variables ?? [];
set
{
if (_variables.SequenceEqualOrNull(value))
if (_variables == null || _variables.SequenceEqualOrNull(value))
return;

_variables = value;
NotifyPropertyChanged();
}
}

private Leaderboard _leaderboard;
private Leaderboard? _leaderboard;
public RangedObservableCollection<LeaderboardEntry> LeaderboardEntries { get; set; }

private bool _isLoadingLeaderboard;
Expand All @@ -143,9 +143,9 @@ public bool IsLoadingLeaderboard

public bool HasIndividualLevels => _levels != null && _levels.Count > 1;

private LeaderboardEntry _selectedLeaderboardEntry;
private LeaderboardEntry? _selectedLeaderboardEntry;

public LeaderboardEntry SelectedLeaderboardEntry
public LeaderboardEntry? SelectedLeaderboardEntry
{
get => _selectedLeaderboardEntry;
set
Expand Down Expand Up @@ -179,7 +179,10 @@ public string Platforms

public async Task<bool> LoadCategoriesAsync()
{
List<Category> categories = await ExecuteNetworkTask(_gameService.GetGameCategoriesAsync(Game.Id));
if (Game == null)
return false;

List<Category>? categories = await ExecuteNetworkTask(_gameService.GetGameCategoriesAsync(Game.Id));
if (categories == null)
return false;

Expand All @@ -190,8 +193,11 @@ public async Task<bool> LoadCategoriesAsync()

public async Task<bool> LoadLevelsAsync()
{
if (Game == null)
return false;

List<Level> allLevels = new() { new() { Name = "Full Game" } };
List<Level> gameLevels = await ExecuteNetworkTask(_gameService.GetGameLevelsAsync(Game.Id));
List<Level>? gameLevels = await ExecuteNetworkTask(_gameService.GetGameLevelsAsync(Game.Id));
if (gameLevels == null)
return false;

Expand All @@ -202,12 +208,18 @@ public async Task<bool> LoadLevelsAsync()

public async Task<bool> LoadVariablesAsync()
{
if (Game == null)
return false;

_allVariables = await ExecuteNetworkTask(_gameService.GetGameVariablesAsync(Game.Id));
return _allVariables != null;
}

public async Task LoadLeaderboardAsync()
{
if (Game == null)
return;

IsLoadingLeaderboard = true;
_leaderboardEntriesVisible = 0;
_leaderboard = null;
Expand All @@ -219,7 +231,7 @@ public async Task LoadLeaderboardAsync()
if (Variables != null)
{
foreach (VariableViewModel vm in Variables)
variableValues.Add($"var-{vm.VariableId}={vm.SelectedValue.Id}");
variableValues.Add($"var-{vm.VariableId}={vm.SelectedValue?.Id}");
variables = string.Join('&', variableValues);
}

Expand All @@ -228,7 +240,7 @@ public async Task LoadLeaderboardAsync()
if (!string.IsNullOrEmpty(variables))
variables = $"&{variables}";

_leaderboard = string.IsNullOrEmpty(SelectedLevel.Id) ?
_leaderboard = string.IsNullOrEmpty(SelectedLevel?.Id) ?
await ExecuteNetworkTask(_leaderboardService.GetFullGameLeaderboardAsync(Game.Id, SelectedCategory.Id, variables, _settingsService.UserSettings.MaxLeaderboardResults)) :
await ExecuteNetworkTask(_leaderboardService.GetLevelLeaderboardAsync(Game.Id, SelectedLevel.Id, SelectedCategory.Id, variables, _settingsService.UserSettings.MaxLeaderboardResults));

Expand All @@ -250,8 +262,9 @@ private void DisplayLeaderboardEntries()
for (int i = 0; i < entry.Run.Players.Count; i++)
{
if (entry.Run.Players[i].PlayerType == PlayerType.User)
entry.Run.Players[i] = _leaderboard.Players.Data.Find(x => x.Id == entry.Run.Players[i].Id);
entry.TrophyAsset = Game.Assets.GetTrophyAsset(entry.Place);
entry.Run.Players[i] = _leaderboard.Players.Data.First(x => x.Id == entry.Run.Players[i].Id);

entry.TrophyAsset = Game?.Assets.GetTrophyAsset(entry.Place);
}
}

Expand All @@ -261,22 +274,25 @@ private void DisplayLeaderboardEntries()

private async Task NavigateToRunAsync()
{
if (_selectedLeaderboardEntry == null)
if (_selectedLeaderboardEntry == null || Game == null)
return;

Category? category = _categories?.FirstOrDefault(x => x.Id == _selectedLeaderboardEntry.Run.CategoryId);
if (category == null)
return;

Category category = _categories.FirstOrDefault(x => x.Id == _selectedLeaderboardEntry.Run.CategoryId);
Level level = _levels.FirstOrDefault(x => x.Id == _selectedLeaderboardEntry.Run.LevelId);
GamePlatform platform = Game.Platforms.Data.Find(x => x.Id == _selectedLeaderboardEntry.Run.System.PlatformId);
Level? level = _levels?.FirstOrDefault(x => x.Id == _selectedLeaderboardEntry.Run.LevelId);
GamePlatform? platform = Game.Platforms.Data.Find(x => x.Id == _selectedLeaderboardEntry.Run.System?.PlatformId);

User examiner = null;
string examinerId = _selectedLeaderboardEntry.Run.Status.ExaminerId;
User? examiner = null;
string? examinerId = _selectedLeaderboardEntry.Run.Status?.ExaminerId;
if (examinerId != null)
examiner = Game.Moderators.Data.Find(x => x.Id == examinerId) ?? await ExecuteNetworkTask(_userService.GetUserAsync(examinerId)) ?? User.GetUserNotFoundPlaceholder();

if (!_selectedLeaderboardEntry.Run.Variables.Any())
foreach (KeyValuePair<string, string> valuePair in _selectedLeaderboardEntry.Run.Values)
{
Variable variable = _allVariables.FirstOrDefault(x => x.Id == valuePair.Key);
Variable? variable = _allVariables?.FirstOrDefault(x => x.Id == valuePair.Key);
if (variable == null)
continue;

Expand Down Expand Up @@ -307,13 +323,13 @@ private async Task NavigateToRunAsync()
private void UpdateVariables()
{
List<VariableViewModel> variablesVMs = new();
IEnumerable<Variable> variables = _allVariables.Where(x => x.IsSubcategory);
if (string.IsNullOrEmpty(SelectedLevel.Id))
IEnumerable<Variable> variables = (_allVariables ?? []).Where(x => x.IsSubcategory);
if (string.IsNullOrEmpty(_selectedLevel?.Id))
variables = variables.Where(x => x.Scope.Type == VariableScopeType.Global || x.Scope.Type == VariableScopeType.FullGame);
else
variables = variables.Where(x => x.Scope.Type == VariableScopeType.Global || x.Scope.Type == VariableScopeType.AllLevels || x.Scope.Type == VariableScopeType.SingleLevel && x.Scope.Level == SelectedLevel.Id);
variables = variables.Where(x => x.Scope.Type == VariableScopeType.Global || x.Scope.Type == VariableScopeType.AllLevels || x.Scope.Type == VariableScopeType.SingleLevel && x.Scope.Level == _selectedLevel?.Id);

foreach (Variable variable in variables.Where(x => x.Category == null || x.Category == SelectedCategory.Id))
foreach (Variable variable in variables.Where(x => x.Category == null || x.Category == _selectedCategory?.Id))
{
VariableViewModel vm = new()
{
Expand Down
4 changes: 2 additions & 2 deletions SpeedrunTracker/ViewModels/RunDetailsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public EmbeddableUrl? SelectedVideo

public ICommand OpenLinkCommand => new AsyncRelayCommand<string>(OpenLinkAsync);

public string? Title => RunDetails == null ? "RunDetails" : $"{_runDetails?.Category.Name} in {_runDetails?.Run?.Times?.PrimaryTimeSpan} by {_runDetails?.Run.Players[0].DisplayName}";
public string? Title => RunDetails == null ? "RunDetails" : $"{_runDetails?.Category?.Name} in {_runDetails?.Run?.Times?.PrimaryTimeSpan} by {_runDetails?.Run.Players[0].DisplayName}";

public string? FormattedDate => RunDetails?.Run.Date?.ToString(_settingsService.UserSettings.DateFormat);

Expand Down Expand Up @@ -151,7 +151,7 @@ public RunDetailsViewModel(IBrowserService browserService, IUserService userServ
private async Task ShowVideo()
{
if (!string.IsNullOrEmpty(_selectedVideo?.Url))
await _browserService.OpenAsync(_selectedVideo.Url);
await _browserService.OpenAsync(_selectedVideo.Url);
}


Expand Down
Loading

0 comments on commit 3cb271f

Please sign in to comment.