[Fix] ScoringSystem

This commit is contained in:
2026-03-05 09:41:23 +07:00
parent dc901b9895
commit 72bbdc76af
3 changed files with 139 additions and 1 deletions
+14 -1
View File
@@ -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);
@@ -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<Object> _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<StandardDice>("d6", "d6");
_onesCategory = SumOfValueCategory.CreateForTest("ones", "Единицы", 1);
_catalog = CategoryCatalog.CreateForTest(new List<CategoryDefinition> { _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>();
_currencyBank.SetBalance(100);
}
[TearDown]
public void TearDown()
{
foreach (var scoring in Object.FindObjectsByType<ScoringSystem>(FindObjectsSortMode.None))
Object.DestroyImmediate(scoring.gameObject);
foreach (var bank in Object.FindObjectsByType<CurrencyBank>(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<ScoringSystem>();
system.Construct(_eventBus, _registry, _catalog, _currencyBank);
return system;
}
private IReadOnlyList<IDice> 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> { effect });
var definition = ModifierDefinition.CreateForTest(id, new List<ModifierBehavior> { behavior });
_createdAssets.Add(effect);
_createdAssets.Add(behavior);
_createdAssets.Add(definition);
var instance = _registry.Add(definition);
_registry.TryActivate(instance);
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b3d6ef3d50944f1da5dcf1276f8a277d