From 72bbdc76af355070457eec7f6794e0975ae73323 Mon Sep 17 00:00:00 2001 From: Konstantin Dyachenko Date: Thu, 5 Mar 2026 09:41:23 +0700 Subject: [PATCH] [Fix] ScoringSystem --- Assets/Scripts/Scoring/ScoringSystem.cs | 15 ++- .../Editor/ScoringCurrencyIntegrationTests.cs | 123 ++++++++++++++++++ .../ScoringCurrencyIntegrationTests.cs.meta | 2 + 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 Assets/Scripts/Tests/Editor/ScoringCurrencyIntegrationTests.cs create mode 100644 Assets/Scripts/Tests/Editor/ScoringCurrencyIntegrationTests.cs.meta diff --git a/Assets/Scripts/Scoring/ScoringSystem.cs b/Assets/Scripts/Scoring/ScoringSystem.cs index 2289267..f976326 100644 --- a/Assets/Scripts/Scoring/ScoringSystem.cs +++ b/Assets/Scripts/Scoring/ScoringSystem.cs @@ -5,6 +5,7 @@ using UnityEngine; using VContainer; using YachtDice.Categories; using YachtDice.Dice; +using YachtDice.Economy; using YachtDice.Events; using YachtDice.Modifiers.Core; using YachtDice.Modifiers.Runtime; @@ -23,13 +24,19 @@ namespace YachtDice.Scoring private GameEventBus _eventBus; private ModifierRegistry _modifierRegistry; private CategoryCatalog _catalog; + private CurrencyBank _currencyBank; [Inject] - public void Construct(GameEventBus eventBus, ModifierRegistry modifierRegistry, CategoryCatalog catalog) + public void Construct( + GameEventBus eventBus, + ModifierRegistry modifierRegistry, + CategoryCatalog catalog, + CurrencyBank currencyBank) { this._eventBus = eventBus; this._modifierRegistry = modifierRegistry; this._catalog = catalog; + this._currencyBank = currencyBank; } public CategoryCatalog Catalog => _catalog; @@ -109,6 +116,9 @@ namespace YachtDice.Scoring _scorecard[category] = finalScore; _usedCategories.Add(category); + if (context.CurrencyDelta != 0 && _currencyBank != null) + _currencyBank.Add(context.CurrencyDelta); + OnCategoryScored?.Invoke(category, finalScore); OnCategoryConfirmed?.Invoke(category, result); @@ -147,6 +157,9 @@ namespace YachtDice.Scoring _scorecard[category] = finalScore; _usedCategories.Add(category); + if (context != null && context.CurrencyDelta != 0 && _currencyBank != null) + _currencyBank.Add(context.CurrencyDelta); + OnCategoryScored?.Invoke(category, finalScore); OnCategoryConfirmed?.Invoke(category, result); diff --git a/Assets/Scripts/Tests/Editor/ScoringCurrencyIntegrationTests.cs b/Assets/Scripts/Tests/Editor/ScoringCurrencyIntegrationTests.cs new file mode 100644 index 0000000..51c6506 --- /dev/null +++ b/Assets/Scripts/Tests/Editor/ScoringCurrencyIntegrationTests.cs @@ -0,0 +1,123 @@ +using System.Collections.Generic; +using NUnit.Framework; +using UnityEngine; +using YachtDice.Categories; +using YachtDice.Dice; +using YachtDice.Economy; +using YachtDice.Events; +using YachtDice.Modifiers.Core; +using YachtDice.Modifiers.Definition; +using YachtDice.Modifiers.Effects; +using YachtDice.Modifiers.Pipeline; +using YachtDice.Modifiers.Runtime; +using YachtDice.Scoring; + +namespace YachtDice.Tests +{ + public class ScoringCurrencyIntegrationTests + { + private readonly List _createdAssets = new(); + + private CategoryDefinition _onesCategory; + private CategoryCatalog _catalog; + private DiceDefinition _standardDice; + private ModifierRegistry _registry; + private GameEventBus _eventBus; + private CurrencyBank _currencyBank; + + [SetUp] + public void SetUp() + { + _standardDice = DiceDefinition.CreateForTest("d6", "d6"); + _onesCategory = SumOfValueCategory.CreateForTest("ones", "Единицы", 1); + _catalog = CategoryCatalog.CreateForTest(new List { _onesCategory }); + + _createdAssets.Add(_standardDice); + _createdAssets.Add(_onesCategory); + _createdAssets.Add(_catalog); + + _registry = new ModifierRegistry(5); + var pipeline = new ModifierPipeline(_registry) + { + TracingEnabled = false, + }; + _eventBus = new GameEventBus(pipeline); + + var bankGo = new GameObject("CurrencyBank"); + _currencyBank = bankGo.AddComponent(); + _currencyBank.SetBalance(100); + } + + [TearDown] + public void TearDown() + { + foreach (var scoring in Object.FindObjectsByType(FindObjectsSortMode.None)) + Object.DestroyImmediate(scoring.gameObject); + + foreach (var bank in Object.FindObjectsByType(FindObjectsSortMode.None)) + Object.DestroyImmediate(bank.gameObject); + + for (var i = 0; i < _createdAssets.Count; i++) + { + if (_createdAssets[i] != null) + Object.DestroyImmediate(_createdAssets[i]); + } + + _createdAssets.Clear(); + } + + [Test] + public void ScoreCategory_WithCurrencyModifier_AddsCurrencyAndFiresBalanceChanged() + { + var system = CreateScoringSystem(); + RegisterCurrencyModifier("bonus-money", 25); + + var balanceChanged = false; + var newBalance = 0; + _currencyBank.OnBalanceChanged += value => + { + balanceChanged = true; + newBalance = value; + }; + + system.ScoreCategory(CreateDice(1, 1, 2, 3, 4), _onesCategory); + + Assert.AreEqual(125, _currencyBank.Balance); + Assert.IsTrue(balanceChanged); + Assert.AreEqual(125, newBalance); + } + + private ScoringSystem CreateScoringSystem() + { + var go = new GameObject("ScoringSystem"); + var system = go.AddComponent(); + system.Construct(_eventBus, _registry, _catalog, _currencyBank); + return system; + } + + private IReadOnlyList CreateDice(params int[] values) + { + var dice = new DiceInstance[values.Length]; + for (var i = 0; i < values.Length; i++) + dice[i] = new DiceInstance(_standardDice, values[i]); + return dice; + } + + private void RegisterCurrencyModifier(string id, int amount) + { + var effect = AddCurrencyEffect.CreateForTest(amount, ModifierPhase.SideEffect); + var behavior = ModifierBehavior.CreateForTest( + TriggerType.OnCategoryScored, + null, + new List { effect }); + var definition = ModifierDefinition.CreateForTest(id, new List { behavior }); + + _createdAssets.Add(effect); + _createdAssets.Add(behavior); + _createdAssets.Add(definition); + + var instance = _registry.Add(definition); + _registry.TryActivate(instance); + } + } +} diff --git a/Assets/Scripts/Tests/Editor/ScoringCurrencyIntegrationTests.cs.meta b/Assets/Scripts/Tests/Editor/ScoringCurrencyIntegrationTests.cs.meta new file mode 100644 index 0000000..eac29fe --- /dev/null +++ b/Assets/Scripts/Tests/Editor/ScoringCurrencyIntegrationTests.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: b3d6ef3d50944f1da5dcf1276f8a277d