mirror of
https://github.com/Ottermandias/Glamourer.git
synced 2025-12-12 10:17:23 +01:00
PreventRandom use WeakReference, reroll rand instead of changing list
This commit is contained in:
parent
67fd65d366
commit
5ca151b675
1 changed files with 8 additions and 7 deletions
|
|
@ -6,19 +6,20 @@ namespace Glamourer.Designs.Special;
|
|||
public class RandomDesignGenerator(DesignStorage designs, DesignFileSystem fileSystem, Configuration config) : IService
|
||||
{
|
||||
private readonly Random _rng = new();
|
||||
private Guid? _lastDesignID = null;
|
||||
private WeakReference<Design?> _lastDesign = new(null, false);
|
||||
|
||||
public Design? Design(IList<Design> localDesigns)
|
||||
public Design? Design(IReadOnlyList<Design> localDesigns)
|
||||
{
|
||||
if (localDesigns.Count == 0)
|
||||
return null;
|
||||
|
||||
if (config.PreventRandomRepeats && _lastDesignID != null && localDesigns.Count > 1 && localDesigns.FindFirst(d => d.Identifier == _lastDesignID, out var found))
|
||||
localDesigns.Remove(found);
|
||||
int idx;
|
||||
do
|
||||
idx = _rng.Next(0, localDesigns.Count);
|
||||
while (config.PreventRandomRepeats && localDesigns.Count > 1 && _lastDesign.TryGetTarget(out var lastDesign) && lastDesign == localDesigns[idx]);
|
||||
|
||||
var idx = _rng.Next(0, localDesigns.Count);
|
||||
Glamourer.Log.Verbose($"[Random Design] Chose design {idx + 1} out of {localDesigns.Count}: {localDesigns[idx].Incognito}.");
|
||||
_lastDesignID = localDesigns[idx].Identifier;
|
||||
_lastDesign.SetTarget(localDesigns[idx]);
|
||||
return localDesigns[idx];
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue