-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Port] Discounts In Uplink / Скидки В Аплинке (#11)
* add: uplink discount * test * tweak
- Loading branch information
Showing
13 changed files
with
196 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
Content.Server/_White/StoreDiscount/StoreDiscountSystem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Linq; | ||
using Content.Shared.FixedPoint; | ||
using Content.Shared.Store; | ||
using Robust.Shared.Prototypes; | ||
using Robust.Shared.Random; | ||
|
||
namespace Content.Server._White.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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
namespace Content.Shared._White.StoreDiscount; | ||
|
||
[DataDefinition] | ||
public sealed partial class SalesSpecifier | ||
{ | ||
[DataField] | ||
public bool Enabled { get; private set; } | ||
|
||
[DataField] | ||
public float MinMultiplier { get; private set; } | ||
|
||
[DataField] | ||
public float MaxMultiplier { get; private set; } | ||
|
||
[DataField] | ||
public int MinItems { get; private set; } | ||
|
||
[DataField] | ||
public int MaxItems { get; private set; } | ||
|
||
[DataField] | ||
public string SalesCategory { get; private set; } = string.Empty; | ||
|
||
public SalesSpecifier() | ||
{ | ||
} | ||
|
||
public SalesSpecifier(bool enabled, float minMultiplier, float maxMultiplier, int minItems, int maxItems, | ||
string salesCategory) | ||
{ | ||
Enabled = enabled; | ||
MinMultiplier = minMultiplier; | ||
MaxMultiplier = maxMultiplier; | ||
MinItems = minItems; | ||
MaxItems = maxItems; | ||
SalesCategory = salesCategory; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
store-sales-amount = [DISCOUNT] { $amount }%! | ||
store-sales-over = [The sale is over] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
store-sales-amount = [СКИДКА] { $amount }%! | ||
store-sales-over = [Скидка закончилась] |
Oops, something went wrong.