Expanded WeightedRandom() and GetWeightedWeatherTimePeriodMs() implementation
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
using Core.Annotations;
|
||||
using Core.Models.Spt.Helper;
|
||||
|
||||
namespace Core.Helpers;
|
||||
|
||||
[Injectable]
|
||||
public class WeightedRandomHelper
|
||||
{
|
||||
public WeightedRandomHelper()
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public WeightedRandomHelper(
|
||||
ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -33,10 +38,48 @@ public class WeightedRandomHelper
|
||||
/// <param name="items">List of items</param>
|
||||
/// <param name="weights">List of weights</param>
|
||||
/// <returns>Dictionary with item and index</returns>
|
||||
public object WeightedRandom<T>(List<T> items, List<double> weights)
|
||||
public WeightedRandomResult<T> WeightedRandom<T>(List<T> items, List<int> weights)
|
||||
{
|
||||
// TODO - create return type { item: any; index: number }
|
||||
throw new NotImplementedException();
|
||||
if (items.Count == 0)
|
||||
{
|
||||
_logger.LogError("Items must not be empty");
|
||||
}
|
||||
|
||||
if (weights.Count == 0)
|
||||
{
|
||||
_logger.LogError("Item weights must not be empty");
|
||||
}
|
||||
|
||||
if (items.Count != weights.Count)
|
||||
{
|
||||
_logger.LogError("Items and weight inputs must be of the same length");
|
||||
}
|
||||
|
||||
// Preparing the cumulative weights list.
|
||||
List<int> cumulativeWeights = [];
|
||||
for (var i = 0; i < weights.Count; i++)
|
||||
{
|
||||
cumulativeWeights.Add(weights[i] + (i > 0 ? cumulativeWeights[i - 1] : 0));
|
||||
}
|
||||
|
||||
// Getting the random number in a range of [0...sum(weights)]
|
||||
int maxCumulativeWeight = cumulativeWeights[cumulativeWeights.Count - 1];
|
||||
double randomNumber = maxCumulativeWeight * new Random().NextDouble();
|
||||
|
||||
// Picking the random item based on its weight.
|
||||
for (int itemIndex = 0; itemIndex < items.Count; itemIndex++)
|
||||
{
|
||||
if (cumulativeWeights[itemIndex] >= randomNumber)
|
||||
{
|
||||
return new WeightedRandomResult<T>()
|
||||
{
|
||||
Item = items[itemIndex],
|
||||
Index = itemIndex,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("No item was picked.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization;
|
||||
using Core.Models.Common;
|
||||
using Core.Models.Enums;
|
||||
using Core.Utils.Json.Converters;
|
||||
@@ -107,5 +107,5 @@ public class WeatherSettings<T>
|
||||
public List<T> Values { get; set; }
|
||||
|
||||
[JsonPropertyName("weights")]
|
||||
public List<double> Weights { get; set; }
|
||||
}
|
||||
public List<int> Weights { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Core.Models.Spt.Helper;
|
||||
|
||||
public class WeightedRandomResult<T>
|
||||
{
|
||||
public T Item { get; set; }
|
||||
public int Index { get; set; }
|
||||
}
|
||||
@@ -75,13 +75,11 @@ public class RaidWeatherService
|
||||
/// <returns>milliseconds</returns>
|
||||
protected long GetWeightedWeatherTimePeriodMs()
|
||||
{
|
||||
//var chosenTimePeriodMinutes = _weightedRandomHelper.WeightedRandom(
|
||||
// _weatherConfig.Weather.TimePeriod.Values,
|
||||
// _weatherConfig.Weather.TimePeriod.Weights).Item;
|
||||
var chosenTimePeriodMinutes = _weightedRandomHelper.WeightedRandom(
|
||||
_weatherConfig.Weather.TimePeriod.Values,
|
||||
_weatherConfig.Weather.TimePeriod.Weights).Item;
|
||||
|
||||
//return chosenTimePeriodMinutes * 60 * 1000; // Convert to milliseconds
|
||||
|
||||
throw new NotImplementedException();
|
||||
return chosenTimePeriodMinutes * 60 * 1000; // Convert to milliseconds
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user