Skip to content

Commit

Permalink
Merge pull request #637 from TheTrackerCouncil/ap-fixes
Browse files Browse the repository at this point in the history
AP Playtest Fixes
  • Loading branch information
MattEqualsCoder authored Jan 2, 2025
2 parents d3bcfa7 + cc1124b commit 04dc630
Show file tree
Hide file tree
Showing 28 changed files with 227 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,17 +301,18 @@ public bool IsJunk(Config? config)
/// configuration, assuming one is provided.
/// </summary>
/// <param name="config">The randomizer configuration.</param>
/// <param name="isLocalPlayerItem">If the item is for the local player</param>
/// <returns>
/// <c>true</c> if the item is considered progression; otherwise, <c>false</c>.
/// </returns>
/// <remarks>
/// This method only considers the item's value on its own. Call TrackerBase.IsWorth(ItemData) to include
/// items that this item might logically lead to.
/// </remarks>
public bool IsProgression(Config? config)
public bool IsProgression(Config? config, bool isLocalPlayerItem)
{
// Todo: We can add special logic like checking if it's one of the first two swords
return InternalItemType.IsPossibleProgression(config?.ZeldaKeysanity == true, config?.MetroidKeysanity == true);
return InternalItemType.IsPossibleProgression(config?.ZeldaKeysanity == true, config?.MetroidKeysanity == true, isLocalPlayerItem);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/TrackerCouncil.Smz3.Data/Options/Sprite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ private Sprite(string name, SpriteType spriteType, bool isDefault, bool isRandom
[YamlIgnore]
public string Author { get; set; }

public string FilePath { get; } = "";
public string FilePath { get; set; } = "";

public SpriteType SpriteType { get; }
public SpriteType SpriteType { get; set; }

[YamlIgnore]
public string PreviewPath { get; set; }
Expand Down
4 changes: 4 additions & 0 deletions src/TrackerCouncil.Smz3.Data/RandomizerDirectories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public static string SpritePath
}
}

public static bool DeleteSprites => SpritePath == Path.Combine(AppContext.BaseDirectory, "Sprites");

public static string UserSpritePath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"SMZ3CasRandomizer", "Sprites");

Expand Down Expand Up @@ -105,4 +107,6 @@ public static string TrackerSpritePath
#endif

public static string TrackerSpriteInitialJsonFilePath => Path.Combine(TrackerSpritePath, "tracker-sprites.json");

public static bool DeleteTrackerSprites => SpritePath == Path.Combine(AppContext.BaseDirectory, "Sprites");
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ public class GitHubFileDownloaderRequest
/// Timeout value for each individual request
/// </summary>
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(5);

/// <summary>
/// If any files that exist on the local computer but not on GitHub should be deleted
/// </summary>
public bool DeleteExtraFiles { get; set; }
}

public enum GitHubFileAction
{
Nothing,
Download,
Delete
}

/// <summary>
Expand All @@ -87,17 +99,12 @@ public class GitHubFileDetails
/// <summary>
/// The hash of the file on GitHub to track if the file was previously downloaded
/// </summary>
public required string RemoteHash { get; set; }
public required string Hash { get; set; }

/// <summary>
/// If the file is found on the local computer
/// What needs to happen for the file
/// </summary>
public required bool FileExistsLocally { get; set; }

/// <summary>
/// If there is a difference in the file between the local computer and Github
/// </summary>
public required bool FileMatchesLocally { get; set; }
public required GitHubFileAction Action { get; set; }

/// <summary>
/// The path to the file to save the GitHub hash to
Expand Down Expand Up @@ -132,6 +139,8 @@ public async Task<List<GitHubFileDetails>> GetGitHubFileDetailsAsync(GitHubFileD

var previousHashes = GetPreviousHashes(request);

_logger.LogInformation("{Count} previous file hashes found", previousHashes.Count);

var fileList = new ConcurrentBag<GitHubFileDetails>();

Parallel.ForEach(files, parallelOptions: new ParallelOptions { MaxDegreeOfParallelism = 4 },
Expand All @@ -140,15 +149,15 @@ public async Task<List<GitHubFileDetails>> GetGitHubFileDetailsAsync(GitHubFileD
var localPath = ConvertToLocalPath(request, fileData.Key);
var currentHash = fileData.Value;
previousHashes.TryGetValue(localPath, out var prevHash);
var needToDownload = !File.Exists(localPath) || prevHash != currentHash;

fileList.Add(new GitHubFileDetails()
{
Path = fileData.Key,
LocalPath = localPath,
DownloadUrl = $"https://raw.githubusercontent.com/{request.RepoOwner}/{request.RepoName}/main/{fileData.Key}",
RemoteHash = fileData.Value,
FileExistsLocally = File.Exists(localPath),
FileMatchesLocally = File.Exists(localPath) && prevHash == currentHash,
Hash = fileData.Value,
Action = needToDownload ? GitHubFileAction.Download : GitHubFileAction.Nothing,
HashPath = request.HashPath
});
});
Expand All @@ -159,21 +168,21 @@ public async Task<List<GitHubFileDetails>> GetGitHubFileDetailsAsync(GitHubFileD

foreach (var file in Directory.EnumerateFiles(request.DestinationFolder, "*", SearchOption.AllDirectories).Where(x => !foundLocalFiles.Contains(x) && IsValidPath(request, x)))
{
previousHashes.TryGetValue(file, out var previousHash);

fileList.Add(new GitHubFileDetails()
{
Path = file,
LocalPath = file,
DownloadUrl = "",
RemoteHash = "",
FileExistsLocally = true,
FileMatchesLocally = false,
Hash = previousHash ?? "",
Action = request.DeleteExtraFiles ? GitHubFileAction.Delete : GitHubFileAction.Nothing,
HashPath = request.HashPath
});
}
}


return fileList.Where(x => !x.FileMatchesLocally).ToList();
return fileList.ToList();
}

public async Task SyncGitHubFilesAsync(GitHubFileDownloaderRequest request)
Expand All @@ -190,7 +199,7 @@ public async Task SyncGitHubFilesAsync(List<GitHubFileDetails> fileDetails)
return;
}

var filesToProcess = fileDetails.Where(x => !x.FileMatchesLocally).ToList();
var filesToProcess = fileDetails.Where(x => x.Action != GitHubFileAction.Nothing).ToList();
var total = filesToProcess.Count;
var completed = 0;

Expand All @@ -216,9 +225,9 @@ public async Task SyncGitHubFilesAsync(List<GitHubFileDetails> fileDetails)

foreach (var hashPath in fileDetails.Select(x => x.HashPath).Distinct())
{
SaveFileHashYaml(hashPath,
fileDetails.Where(x => x.HashPath == hashPath && !string.IsNullOrEmpty(x.DownloadUrl))
.ToDictionary(x => x.LocalPath, x => x.RemoteHash));
var hashDetails = fileDetails.Where(x => x.HashPath == hashPath && !string.IsNullOrEmpty(x.Hash))
.ToDictionary(x => x.LocalPath, x => x.Hash);
SaveFileHashYaml(hashPath, hashDetails);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ private async Task UpdateSpritesAsync()
InitialJsonPath = RandomizerDirectories.SpriteInitialJsonFilePath,
ValidPathCheck = p => p.StartsWith("Sprites/") && p.Contains('.'),
ConvertGitHubPathToLocalPath = p => p.Replace("Sprites/", ""),
DeleteExtraFiles = RandomizerDirectories.DeleteSprites
};

var sprites = await gitHubFileSynchronizerService.GetGitHubFileDetailsAsync(spriteDownloadRequest);
Expand All @@ -228,11 +229,12 @@ private async Task UpdateSpritesAsync()
HashPath = RandomizerDirectories.TrackerSpritePath,
InitialJsonPath = RandomizerDirectories.TrackerSpritePath,
ValidPathCheck = p => p.EndsWith(".png", StringComparison.OrdinalIgnoreCase) || p.EndsWith(".gif", StringComparison.OrdinalIgnoreCase),
DeleteExtraFiles = RandomizerDirectories.DeleteTrackerSprites
};

sprites.AddRange(await gitHubFileSynchronizerService.GetGitHubFileDetailsAsync(spriteDownloadRequest));

if (sprites.Count > 0)
if (sprites.Any(x => x.Action != GitHubFileAction.Nothing))
{
SpriteDownloadStarted?.Invoke(this, EventArgs.Empty);
await gitHubFileSynchronizerService.SyncGitHubFilesAsync(sprites);
Expand Down
2 changes: 1 addition & 1 deletion src/TrackerCouncil.Smz3.Data/Services/SpriteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public Sprite GetSprite(SpriteType type)

if (sprite.IsRandomSprite)
{
var spriteType = sprite.SpriteType;
var spriteType = type;

var searchText = spriteType switch
{
Expand Down
2 changes: 1 addition & 1 deletion src/TrackerCouncil.Smz3.Data/WorldData/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public ItemType? MarkedItem
if (value != null)
{
State.MarkedUsefulness =
Item.Type.IsPossibleProgression(World.Config.ZeldaKeysanity, World.Config.MetroidKeysanity)
Item.Type.IsPossibleProgression(World.Config.ZeldaKeysanity, World.Config.MetroidKeysanity, Item.IsLocalPlayerItem)
? LocationUsefulness.NiceToHave
: LocationUsefulness.Useless;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public bool CanBeatBoss(Progression items)
private bool CanExit(Progression items)
{
return items.CardNorfairL2 /*Bubble Mountain*/ ||
(items.Gravity && items.Wave /* Volcano Room and Blue Gate */ && (items.Grapple || items.SpaceJump /*Spikey Acid Snakes and Croc Escape*/));
items is { Gravity: true, SpaceJump: true } /* Path back to Mire*/ ||
(items is { Gravity: true, Wave: true } /* Volcano Room and Blue Gate */ && (items.Grapple || items.SpaceJump /*Spikey Acid Snakes and Croc Escape*/));
}

public class SpringBallMazeRoom : Room
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ private PlayerHintTile GetBasicLocationHint(Location location)
Locations = [location.Id],
Usefulness =
location.ItemType.IsPossibleProgression(location.World.Config.ZeldaKeysanity,
location.World.Config.MetroidKeysanity)
location.World.Config.MetroidKeysanity, location.Item.IsLocalPlayerItem)
? LocationUsefulness.NiceToHave
: LocationUsefulness.Useless
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void FillItems(World world, List<World> worlds)
var generatedData = generatedLocationData.Single(x => x.Id == location.Id);
var itemType = generatedData.Item;
var itemWorld = worlds.Single(x => x.Id == generatedData.ItemWorldId);
location.Item = new Item(itemType, itemWorld, isProgression: itemType.IsPossibleProgression(itemWorld.Config.ZeldaKeysanity, itemWorld.Config.MetroidKeysanity));
location.Item = new Item(itemType, itemWorld, isProgression: itemType.IsPossibleProgression(itemWorld.Config.ZeldaKeysanity, itemWorld.Config.MetroidKeysanity, itemWorld == world));
_logger.LogDebug("Fast-filled {Item} at {Location}", generatedData.Item, location.Name);
}
EnsureLocationsHaveItems(world);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public List<World> LoadGeneratedRom(GeneratedRom rom)
location.Item = new Item(locationState.Item, itemWorld,
itemState.ItemName, itemMetadata, itemState,
locationState.Item.IsPossibleProgression(itemWorld.Config.ZeldaKeysanity,
itemWorld.Config.MetroidKeysanity),
itemWorld.Config.MetroidKeysanity, itemWorld == location.World),
locationState.ItemOwnerName, locationState.ItemName
);
}
Expand Down
Loading

0 comments on commit 04dc630

Please sign in to comment.