BotGen fixes

This commit is contained in:
Chomp
2025-01-20 23:13:17 +00:00
parent 80cc38f80f
commit 0b412f4a18
7 changed files with 68 additions and 28 deletions
@@ -729,7 +729,7 @@ public class RagfairController
}
else
{
requirementsPriceInRub += _ragfairPriceService.GetDynamicPriceForItem(requestedItemTpl).Value * item.Count.Value;
requirementsPriceInRub += _itemHelper.GetDynamicItemPrice(requestedItemTpl).Value * item.Count.Value;
}
}
@@ -793,10 +793,9 @@ public class BotLootGenerator(
public void RandomiseMoneyStackSize(string botRole, TemplateItem itemTemplate, Item moneyItem)
{
// Get all currency weights for this bot type
var currencyWeights = _botConfig.CurrencyStackSize[botRole];
if (currencyWeights is null)
if (!_botConfig.CurrencyStackSize.TryGetValue(botRole, out var currencyWeights))
{
currencyWeights = new();
currencyWeights = _botConfig.CurrencyStackSize["default"];
}
var currencyWeight = currencyWeights[moneyItem.Template];
@@ -59,6 +59,7 @@ public class PMCLootGenerator
// Hydrate loot dictionary if empty
if (_pocketLootPool is null)
{
_pocketLootPool = new Dictionary<string, double>();
var items = _databaseService.GetItems();
var pmcPriceOverrides =
_databaseService.GetBots().Types[botRole.ToLower() == "pmcbear" ? "bear" : "usec"].BotInventory.Items.Pockets;
@@ -138,6 +139,7 @@ public class PMCLootGenerator
// Hydrate loot dictionary if empty
if (_vestLootPool is null)
{
_vestLootPool = new Dictionary<string, double>();
var items = _databaseService.GetItems();
var pmcPriceOverrides =
_databaseService.GetBots().Types[botRole.ToLower() == "pmcbear" ? "bear" : "usec"].BotInventory.Items.TacticalVest;
@@ -219,6 +221,7 @@ public class PMCLootGenerator
// Hydrate loot dictionary if empty
if (_backpackLootPool is null)
{
_backpackLootPool = new Dictionary<string, double>();
var items = _databaseService.GetItems();
var pmcPriceOverrides =
_databaseService.GetBots().Types[botRole.ToLower() == "pmcbear" ? "bear" : "usec"].BotInventory.Items.Backpack;
+8 -3
View File
@@ -406,7 +406,7 @@ public class ItemHelper(
var staticPrice = GetStaticItemPrice(tpl);
var dynamicPrice = GetDynamicItemPrice(tpl);
return Math.Max(staticPrice ?? 0, dynamicPrice);
return Math.Max(staticPrice ?? 0d, dynamicPrice ?? 0d);
}
/// <summary>
@@ -430,9 +430,14 @@ public class ItemHelper(
/// </summary>
/// <param name="tpl">Items tpl id to look up price</param>
/// <returns>Price in roubles (undefined if not found)</returns>
public double GetDynamicItemPrice(string tpl)
public double? GetDynamicItemPrice(string tpl)
{
return _databaseService.GetPrices()[tpl];
if (_databaseService.GetPrices().TryGetValue(tpl, out var price))
{
return price;
}
return null;
}
/// <summary>
+46 -3
View File
@@ -89,16 +89,59 @@ public class WeightedRandomHelper(
/// <param name="weightedDict">Values to reduce</param>
public void ReduceWeightValues(Dictionary<string, double> weightedDict)
{
throw new NotImplementedException();
// No values, nothing to reduce
if (weightedDict.Count == 0)
{
return;
}
// Only one value, set to 1 and exit
if (weightedDict.Count == 1)
{
var kvp = weightedDict.FirstOrDefault();
weightedDict[kvp.Key] = 1;
return;
}
var weights = weightedDict.Values.ToList();
var commonDivisor = CommonDivisor(weights);
// No point in dividing by 1
if (commonDivisor == 1)
{
return;
}
foreach (var kvp in weightedDict) {
weightedDict[kvp.Key] /= commonDivisor;
}
}
/**
* Get the common divisor between all values in the passed in list and returns it
*/
protected double CommonDivisor(List<double> numbers)
{
throw new NotImplementedException();
var result = numbers[0];
for (var i = 1; i < numbers.Count; i++)
{
result = Gcd(result, numbers[i]);
}
return result;
}
protected double Gcd(double a, double b)
{
throw new NotImplementedException();
var x = a;
var y = b;
while (y != 0)
{
var temp = y;
y = x % y;
x = temp;
}
return x;
}
}
+1 -11
View File
@@ -70,7 +70,7 @@ public class RagfairPriceService(
public double GetFleaPriceForItem(string tplId)
{
// Get dynamic price (templates/prices), if that doesnt exist get price from static array (templates/handbook)
var itemPrice = GetDynamicPriceForItem(tplId) ?? GetStaticPriceForItem(tplId);
var itemPrice = _itemHelper.GetDynamicItemPrice(tplId) ?? GetStaticPriceForItem(tplId);
if (itemPrice is null)
{
var itemFromDb = _itemHelper.GetItem(tplId);
@@ -98,16 +98,6 @@ public class RagfairPriceService(
throw new NotImplementedException();
}
/// <summary>
/// get the dynamic (flea) price for an item
/// </summary>
/// <param name="itemTpl">item template id to look up</param>
/// <returns>price in roubles</returns>
public double? GetDynamicPriceForItem(string itemTpl)
{
return _databaseService.GetPrices()[itemTpl]; ;
}
/// <summary>
/// Grab the static (handbook) for an item by its tplId
/// </summary>
+7 -7
View File
@@ -418,21 +418,21 @@ public class RandomUtil
/// <returns>A secure random number between 0 (inclusive) and 1 (exclusive).</returns>
private static double GetSecureRandomNumber()
{
byte[] buffer = new byte[6]; // 48 bits
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
var buffer = new byte[6]; // 48 bits
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(buffer);
}
// Convert the byte array to unsigned long
ulong integer = 0;
for (int i = 0; i < buffer.Length; i++)
// Convert byte array to unsigned long
ulong value = 0;
for (var i = 0; i < buffer.Length; i++)
{
integer |= (ulong)buffer[i] << (8 * (buffer.Length - 1 - i));
value |= (ulong)buffer[i] << (8 * (buffer.Length - 1 - i));
}
const ulong maxInteger = 281474976710656; // 2^48
return (double)integer / maxInteger;
return (double)value / maxInteger;
}
/// <summary>