Files
wwdpublic/Content.Server/StoreDiscount/StoreDiscountSystem.cs
VMSolidus 99125235e4 [Port] Uplink Discounts From White Dream (#930)
# Description

This is a port of
https://github.com/WWhiteDreamProject/wwdpublic/pull/11 from White
Dream. This feature selects random items in the traitor uplink each
round to be discounted and moved to the Discount tab, which are the same
for every traitor. This in theory helps encourage players to be
spontaneous, and use items that they otherwise might not normally
consider using, which helps mix things up from round to round.

<details><summary><h1>Media</h1></summary>
<p>

> # Описание PR
> Порт скидок в аплинке.
> 
> # Изменения
> 🆑 Spatison
> 
> * add: Added discounts in uplink / Добавлены скидки в аплинк

</p>
</details>

# Changelog

🆑 Spatison
add: Added discounts in uplink / Добавлены скидки в аплинк

---------

Signed-off-by: VMSolidus <evilexecutive@gmail.com>
Co-authored-by: Spatison <137375981+Spatison@users.noreply.github.com>
Co-authored-by: DEATHB4DEFEAT <77995199+DEATHB4DEFEAT@users.noreply.github.com>
2024-10-19 13:16:16 +07:00

56 lines
1.9 KiB
C#

using System.Linq;
using Content.Shared.FixedPoint;
using Content.Shared.Store;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Server.StoreDiscount;
public sealed class StoreDiscountSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
public void ApplyDiscounts(IEnumerable<ListingData> listings, StorePresetPrototype store)
{
if (!store.Sales.Enabled)
return;
var count = _random.Next(store.Sales.MinItems, store.Sales.MaxItems + 1);
listings = listings
.Where(l =>
!l.SaleBlacklist
&& l.Cost.Any(x => x.Value > 1)
&& store.Categories.Overlaps(ChangedFormatCategories(l.Categories)))
.OrderBy(_ => _random.Next()).Take(count).ToList();
foreach (var listing in listings)
{
var sale = GetDiscount(store.Sales.MinMultiplier, store.Sales.MaxMultiplier);
var newCost = listing.Cost.ToDictionary(x => x.Key,
x => FixedPoint2.New(Math.Max(1, (int) MathF.Round(x.Value.Float() * sale))));
if (listing.Cost.All(x => x.Value.Int() == newCost[x.Key].Int()))
continue;
var key = listing.Cost.First(x => x.Value > 0).Key;
listing.OldCost = listing.Cost;
listing.DiscountValue = 100 - (newCost[key] / listing.Cost[key] * 100).Int();
listing.Cost = newCost;
listing.Categories = new() {store.Sales.SalesCategory};
}
}
private IEnumerable<string> ChangedFormatCategories(List<ProtoId<StoreCategoryPrototype>> categories)
{
var modified = from p in categories select p.Id;
return modified;
}
private float GetDiscount(float minMultiplier, float maxMultiplier)
{
return _random.NextFloat() * (maxMultiplier - minMultiplier) + minMultiplier;
}
}