[Refactor] Replace hardcoded categories with data-driven SO system and abstract dice

- Add abstract dice system (IDie interface, DieDefinitionSO, StandardDieSO, DieInstance)
  to support future custom dice types while keeping backward compat via int[] DiceValues
- Replace YachtCategory enum and CategoryScorer switch with CategoryDefinitionSO hierarchy:
  SumOfValueCategorySO, NOfAKindCategorySO, FullHouseCategorySO, StraightCategorySO, SumAllCategorySO
- Add CategoryCatalogSO for ordered category collections and DiceCheckUtility for shared logic
- Refactor ScoringSystem, Views, GameManager, GameController to use SO references
- Update CategoryCondition modifier to use SO reference instead of enum
- Update all editor tests to use SO-based categories and DieInstance

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-01 11:46:50 +07:00
parent 6a48d68f75
commit 0f9b162061
31 changed files with 845 additions and 298 deletions
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using UnityEngine;
namespace YachtDice.Categories
{
/// <summary>
/// Каталог всех доступных категорий.
/// Порядок определяет порядок отображения в UI.
/// Аналог ModifierCatalogSO.
/// </summary>
[CreateAssetMenu(fileName = "CategoryCatalog", menuName = "YachtDice/Categories/Catalog")]
public class CategoryCatalogSO : ScriptableObject
{
[SerializeField] private List<CategoryDefinitionSO> categories = new();
public IReadOnlyList<CategoryDefinitionSO> All => categories;
public int Count => categories.Count;
public CategoryDefinitionSO FindById(string id)
{
for (int i = 0; i < categories.Count; i++)
{
if (categories[i] != null && categories[i].Id == id)
return categories[i];
}
return null;
}
public int IndexOf(CategoryDefinitionSO def)
{
for (int i = 0; i < categories.Count; i++)
{
if (categories[i] == def)
return i;
}
return -1;
}
#if UNITY_EDITOR
public static CategoryCatalogSO CreateForTest(List<CategoryDefinitionSO> defs)
{
var catalog = CreateInstance<CategoryCatalogSO>();
catalog.categories = defs ?? new List<CategoryDefinitionSO>();
return catalog;
}
#endif
}
}
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using UnityEngine;
using YachtDice.Dice;
namespace YachtDice.Categories
{
/// <summary>
/// Абстрактное определение категории для скоринга.
/// Каждая категория знает как вычислить очки по набору дайсов.
/// </summary>
public abstract class CategoryDefinitionSO : ScriptableObject
{
[Header("Identity")]
[SerializeField] private string id;
[SerializeField] private string displayName;
[SerializeField, TextArea] private string description;
[SerializeField] private Sprite icon;
[Header("Section")]
[SerializeField] private bool isUpperSection;
public string Id => id;
public string DisplayName => displayName;
public string Description => description;
public Sprite Icon => icon;
public bool IsUpperSection => isUpperSection;
/// <summary>
/// Вычисляет очки для данного набора дайсов.
/// </summary>
public abstract int Calculate(IReadOnlyList<IDie> dice);
#if UNITY_EDITOR
public void SetTestData(string testId, string testDisplayName, bool upperSection = false)
{
id = testId;
displayName = testDisplayName;
isUpperSection = upperSection;
}
#endif
}
}
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using UnityEngine;
using YachtDice.Dice;
namespace YachtDice.Categories
{
/// <summary>
/// Категория Фулл-хаус: 3 одинаковых + 2 одинаковых.
/// При совпадении возвращает фиксированное число очков.
/// </summary>
[CreateAssetMenu(fileName = "FullHouseCategory", menuName = "YachtDice/Categories/Full House")]
public class FullHouseCategorySO : CategoryDefinitionSO
{
[Header("Scoring")]
[Tooltip("Фиксированное число очков за фулл-хаус")]
[SerializeField] private int fixedScore = 25;
public override int Calculate(IReadOnlyList<IDie> dice)
{
int[] values = DiceCheckUtility.ExtractValues(dice);
return DiceCheckUtility.IsFullHouse(values) ? fixedScore : 0;
}
#if UNITY_EDITOR
public static FullHouseCategorySO CreateForTest(string id, string displayName, int score = 25)
{
var so = CreateInstance<FullHouseCategorySO>();
so.SetTestData(id, displayName);
so.fixedScore = score;
return so;
}
#endif
}
}
@@ -0,0 +1,47 @@
using System.Collections.Generic;
using UnityEngine;
using YachtDice.Dice;
namespace YachtDice.Categories
{
/// <summary>
/// Категория N-одинаковых: проверяет наличие N дайсов с одинаковым значением.
/// При успехе возвращает сумму всех дайсов или фиксированное число очков.
/// Используется для Тройки (3, сумма), Каре (4, сумма), Яхты (5, fixed=50).
/// </summary>
[CreateAssetMenu(fileName = "NOfAKindCategory", menuName = "YachtDice/Categories/N Of A Kind")]
public class NOfAKindCategorySO : CategoryDefinitionSO
{
[Header("Scoring")]
[Tooltip("Сколько одинаковых дайсов требуется")]
[SerializeField, Range(2, 6)] private int requiredCount = 3;
[Tooltip("Использовать фиксированное число очков вместо суммы")]
[SerializeField] private bool useFixedScore;
[Tooltip("Фиксированное число очков (если useFixedScore = true)")]
[SerializeField] private int fixedScore;
public override int Calculate(IReadOnlyList<IDie> dice)
{
int[] values = DiceCheckUtility.ExtractValues(dice);
if (!DiceCheckUtility.NOfAKind(values, requiredCount))
return 0;
return useFixedScore ? fixedScore : DiceCheckUtility.Sum(values);
}
#if UNITY_EDITOR
public static NOfAKindCategorySO CreateForTest(string id, string displayName, int count, bool fixedScoreMode = false, int score = 0)
{
var so = CreateInstance<NOfAKindCategorySO>();
so.SetTestData(id, displayName);
so.requiredCount = count;
so.useFixedScore = fixedScoreMode;
so.fixedScore = score;
return so;
}
#endif
}
}
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using UnityEngine;
using YachtDice.Dice;
namespace YachtDice.Categories
{
/// <summary>
/// Категория Стрит: последовательность заданной длины.
/// При совпадении возвращает фиксированное число очков.
/// Малый стрит: runLength=4, fixedScore=30. Большой стрит: runLength=5, fixedScore=40.
/// </summary>
[CreateAssetMenu(fileName = "StraightCategory", menuName = "YachtDice/Categories/Straight")]
public class StraightCategorySO : CategoryDefinitionSO
{
[Header("Scoring")]
[Tooltip("Требуемая длина последовательности")]
[SerializeField, Range(3, 6)] private int runLength = 4;
[Tooltip("Фиксированное число очков")]
[SerializeField] private int fixedScore = 30;
public override int Calculate(IReadOnlyList<IDie> dice)
{
int[] values = DiceCheckUtility.ExtractValues(dice);
return DiceCheckUtility.HasStraightRun(values, runLength) ? fixedScore : 0;
}
#if UNITY_EDITOR
public static StraightCategorySO CreateForTest(string id, string displayName, int run, int score)
{
var so = CreateInstance<StraightCategorySO>();
so.SetTestData(id, displayName);
so.runLength = run;
so.fixedScore = score;
return so;
}
#endif
}
}
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using UnityEngine;
using YachtDice.Dice;
namespace YachtDice.Categories
{
/// <summary>
/// Категория «Шанс»: суммирует все дайсы без условий.
/// </summary>
[CreateAssetMenu(fileName = "SumAllCategory", menuName = "YachtDice/Categories/Sum All (Chance)")]
public class SumAllCategorySO : CategoryDefinitionSO
{
public override int Calculate(IReadOnlyList<IDie> dice)
{
int sum = 0;
for (int i = 0; i < dice.Count; i++)
sum += dice[i].Value;
return sum;
}
#if UNITY_EDITOR
public static SumAllCategorySO CreateForTest(string id, string displayName)
{
var so = CreateInstance<SumAllCategorySO>();
so.SetTestData(id, displayName);
return so;
}
#endif
}
}
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using UnityEngine;
using YachtDice.Dice;
namespace YachtDice.Categories
{
/// <summary>
/// Категория верхней секции: суммирует все дайсы с заданным значением.
/// Используется для Единиц (1), Двоек (2), ... Шестёрок (6).
/// </summary>
[CreateAssetMenu(fileName = "SumOfValueCategory", menuName = "YachtDice/Categories/Sum Of Value")]
public class SumOfValueCategorySO : CategoryDefinitionSO
{
[Header("Scoring")]
[Tooltip("Значение грани для суммирования (1-6)")]
[SerializeField, Range(1, 6)] private int targetValue = 1;
public int TargetValue => targetValue;
public override int Calculate(IReadOnlyList<IDie> dice)
{
int sum = 0;
for (int i = 0; i < dice.Count; i++)
if (dice[i].Value == targetValue) sum += targetValue;
return sum;
}
#if UNITY_EDITOR
public static SumOfValueCategorySO CreateForTest(string id, string displayName, int target)
{
var so = CreateInstance<SumOfValueCategorySO>();
so.SetTestData(id, displayName, upperSection: true);
so.targetValue = target;
return so;
}
#endif
}
}
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using YachtDice.Dice;
namespace YachtDice.Categories
{
/// <summary>
/// Статические хелперы для проверки комбинаций дайсов.
/// Перенесены из CategoryScorer для переиспользования в конкретных SO-категориях.
/// </summary>
public static class DiceCheckUtility
{
/// <summary>Извлекает массив значений из абстрактных дайсов.</summary>
public static int[] ExtractValues(IReadOnlyList<IDie> dice)
{
int[] values = new int[dice.Count];
for (int i = 0; i < dice.Count; i++)
values[i] = dice[i].Value;
return values;
}
/// <summary>Сумма дайсов, показывающих конкретное значение.</summary>
public static int SumOfValue(int[] values, int target)
{
int sum = 0;
for (int i = 0; i < values.Length; i++)
if (values[i] == target) sum += target;
return sum;
}
/// <summary>Сумма всех дайсов.</summary>
public static int Sum(int[] values)
{
int sum = 0;
for (int i = 0; i < values.Length; i++) sum += values[i];
return sum;
}
/// <summary>Есть ли N или более одинаковых значений.</summary>
public static bool NOfAKind(int[] values, int n)
{
int[] counts = new int[7];
for (int i = 0; i < values.Length; i++) counts[values[i]]++;
for (int v = 1; v <= 6; v++)
if (counts[v] >= n) return true;
return false;
}
/// <summary>Проверяет фулл-хаус (3 + 2 одинаковых).</summary>
public static bool IsFullHouse(int[] values)
{
int[] counts = new int[7];
for (int i = 0; i < values.Length; i++) counts[values[i]]++;
bool hasTwo = false, hasThree = false;
for (int v = 1; v <= 6; v++)
{
if (counts[v] == 2) hasTwo = true;
if (counts[v] == 3) hasThree = true;
}
return hasTwo && hasThree;
}
/// <summary>Есть ли последовательность заданной длины.</summary>
public static bool HasStraightRun(int[] values, int runLength)
{
bool[] present = new bool[7];
for (int i = 0; i < values.Length; i++) present[values[i]] = true;
int consecutive = 0;
for (int v = 1; v <= 6; v++)
{
consecutive = present[v] ? consecutive + 1 : 0;
if (consecutive >= runLength) return true;
}
return false;
}
}
}
+5 -2
View File
@@ -1,6 +1,7 @@
using UnityEngine; using UnityEngine;
using VContainer; using VContainer;
using VContainer.Unity; using VContainer.Unity;
using YachtDice.Categories;
using YachtDice.Economy; using YachtDice.Economy;
using YachtDice.Events; using YachtDice.Events;
using YachtDice.Game; using YachtDice.Game;
@@ -14,6 +15,7 @@ namespace YachtDice.DI
public class GameLifetimeScope : LifetimeScope public class GameLifetimeScope : LifetimeScope
{ {
[SerializeField] private ModifierCatalogSO modifierCatalog; [SerializeField] private ModifierCatalogSO modifierCatalog;
[SerializeField] private CategoryCatalogSO categoryCatalog;
[Header("Scene References")] [Header("Scene References")]
[SerializeField] private ScoringSystem scoringSystem; [SerializeField] private ScoringSystem scoringSystem;
@@ -23,8 +25,9 @@ namespace YachtDice.DI
protected override void Configure(IContainerBuilder builder) protected override void Configure(IContainerBuilder builder)
{ {
// SO catalog // SO catalogs
builder.RegisterInstance(modifierCatalog); builder.RegisterInstance(modifierCatalog);
builder.RegisterInstance(categoryCatalog);
// Core modifier services // Core modifier services
builder.Register<ModifierRegistry>(Lifetime.Singleton); builder.Register<ModifierRegistry>(Lifetime.Singleton);
@@ -38,4 +41,4 @@ namespace YachtDice.DI
builder.RegisterComponent(diceManager); builder.RegisterComponent(diceManager);
} }
} }
} }
+4
View File
@@ -14,6 +14,10 @@ namespace YachtDice.Dice
[Header("References")] [Header("References")]
[SerializeField] private Dice dice; [SerializeField] private Dice dice;
[SerializeField] private Rigidbody rb; [SerializeField] private Rigidbody rb;
[SerializeField] private DieDefinitionSO definition;
/// <summary>Определение типа дайса (назначается в инспекторе).</summary>
public DieDefinitionSO Definition => definition;
[Header("Throw Settings")] [Header("Throw Settings")]
[Tooltip("Сила подброса вверх")] [Tooltip("Сила подброса вверх")]
+36
View File
@@ -0,0 +1,36 @@
using UnityEngine;
namespace YachtDice.Dice
{
/// <summary>
/// Абстрактное определение типа дайса.
/// Наследники описывают конкретные виды (стандартный d6, специальные и т.д.).
/// </summary>
public abstract class DieDefinitionSO : ScriptableObject
{
[Header("Identity")]
[SerializeField] private string id;
[SerializeField] private string displayName;
[SerializeField] private Sprite icon;
public string Id => id;
public string DisplayName => displayName;
public Sprite Icon => icon;
/// <summary>Количество граней.</summary>
public abstract int FaceCount { get; }
/// <summary>Возвращает массив всех возможных значений граней.</summary>
public abstract int[] GetFaceValues();
#if UNITY_EDITOR
public static T CreateForTest<T>(string id, string displayName = null) where T : DieDefinitionSO
{
var so = CreateInstance<T>();
so.id = id;
so.displayName = displayName ?? id;
return so;
}
#endif
}
}
+27
View File
@@ -0,0 +1,27 @@
namespace YachtDice.Dice
{
/// <summary>
/// Рантайм-состояние одного дайса.
/// Хранит текущее значение верхней грани и ссылку на определение типа.
/// </summary>
public class DieInstance : IDie
{
public DieDefinitionSO Definition { get; }
public int Value { get; set; }
public bool IsLocked { get; set; }
public DieInstance(DieDefinitionSO definition)
{
Definition = definition;
Value = 0;
IsLocked = false;
}
public DieInstance(DieDefinitionSO definition, int initialValue)
{
Definition = definition;
Value = initialValue;
IsLocked = false;
}
}
}
+15
View File
@@ -0,0 +1,15 @@
namespace YachtDice.Dice
{
/// <summary>
/// Минимальный контракт для любого дайса.
/// Каждый дайс всегда имеет текущее значение (верхняя грань) и определение типа.
/// </summary>
public interface IDie
{
/// <summary>Текущее значение верхней грани.</summary>
int Value { get; }
/// <summary>Определение типа дайса (ScriptableObject).</summary>
DieDefinitionSO Definition { get; }
}
}
+33
View File
@@ -0,0 +1,33 @@
using UnityEngine;
namespace YachtDice.Dice
{
/// <summary>
/// Стандартный дайс с настраиваемыми значениями граней.
/// По умолчанию — классический d6 (1-6).
/// </summary>
[CreateAssetMenu(fileName = "StandardDie", menuName = "YachtDice/Dice/Standard Die")]
public class StandardDieSO : DieDefinitionSO
{
[Header("Configuration")]
[SerializeField] private int[] faceValues = { 1, 2, 3, 4, 5, 6 };
public override int FaceCount => faceValues.Length;
public override int[] GetFaceValues()
{
int[] copy = new int[faceValues.Length];
System.Array.Copy(faceValues, copy, faceValues.Length);
return copy;
}
#if UNITY_EDITOR
public static StandardDieSO CreateStandardD6ForTest()
{
var so = CreateForTest<StandardDieSO>("standard_d6", "Стандартный d6");
so.faceValues = new[] { 1, 2, 3, 4, 5, 6 };
return so;
}
#endif
}
}
+25 -15
View File
@@ -14,32 +14,37 @@ namespace YachtDice.Game
public int DiceCount => diceRollers.Count; public int DiceCount => diceRollers.Count;
private bool[] locked; private DieInstance[] diceInstances;
private int[] currentValues;
private int pendingCount; private int pendingCount;
private void Awake() private void Awake()
{ {
int count = diceRollers.Count; int count = diceRollers.Count;
locked = new bool[count]; diceInstances = new DieInstance[count];
currentValues = new int[count];
for (int i = 0; i < count; i++)
{
var definition = diceRollers[i].Definition;
diceInstances[i] = new DieInstance(definition);
}
} }
public bool IsLocked(int index) => locked[index]; public bool IsLocked(int index) => diceInstances[index].IsLocked;
public void ToggleLock(int index) public void ToggleLock(int index)
{ {
locked[index] = !locked[index]; diceInstances[index].IsLocked = !diceInstances[index].IsLocked;
} }
public void SetLocked(int index, bool isLocked) public void SetLocked(int index, bool isLocked)
{ {
locked[index] = isLocked; diceInstances[index].IsLocked = isLocked;
} }
public void UnlockAll() public void UnlockAll()
{ {
for (int i = 0; i < locked.Length; i++) locked[i] = false; for (int i = 0; i < diceInstances.Length; i++)
diceInstances[i].IsLocked = false;
} }
public void RollUnlocked() public void RollUnlocked()
@@ -51,7 +56,7 @@ namespace YachtDice.Game
for (int i = 0; i < diceRollers.Count; i++) for (int i = 0; i < diceRollers.Count; i++)
{ {
if (locked[i]) continue; if (diceInstances[i].IsLocked) continue;
pendingCount++; pendingCount++;
int capturedIndex = i; int capturedIndex = i;
@@ -59,7 +64,7 @@ namespace YachtDice.Game
void Handler(int value) void Handler(int value)
{ {
diceRollers[capturedIndex].OnRollFinished -= Handler; diceRollers[capturedIndex].OnRollFinished -= Handler;
currentValues[capturedIndex] = value; diceInstances[capturedIndex].Value = value;
OnDieSettled?.Invoke(capturedIndex, value); OnDieSettled?.Invoke(capturedIndex, value);
pendingCount--; pendingCount--;
@@ -75,14 +80,19 @@ namespace YachtDice.Game
OnAllDiceSettled?.Invoke(); OnAllDiceSettled?.Invoke();
} }
/// <summary>Возвращает абстрактный список дайсов (основной API).</summary>
public IReadOnlyList<IDie> GetDice() => diceInstances;
/// <summary>Возвращает копию текущих значений (обратная совместимость).</summary>
public int[] GetCurrentValues() public int[] GetCurrentValues()
{ {
int[] copy = new int[currentValues.Length]; int[] values = new int[diceInstances.Length];
Array.Copy(currentValues, copy, currentValues.Length); for (int i = 0; i < diceInstances.Length; i++)
return copy; values[i] = diceInstances[i].Value;
return values;
} }
public int GetValue(int index) => currentValues[index]; public int GetValue(int index) => diceInstances[index].Value;
public bool IsAnyRolling public bool IsAnyRolling
{ {
@@ -100,7 +110,7 @@ namespace YachtDice.Game
{ {
var diceComponent = diceRollers[i].GetComponent<Dice.Dice>(); var diceComponent = diceRollers[i].GetComponent<Dice.Dice>();
if (diceComponent != null && diceComponent.TryGetTopValue(out int val)) if (diceComponent != null && diceComponent.TryGetTopValue(out int val))
currentValues[i] = val; diceInstances[i].Value = val;
} }
} }
} }
+6 -5
View File
@@ -1,5 +1,6 @@
using System; using System;
using UnityEngine; using UnityEngine;
using YachtDice.Categories;
using YachtDice.Scoring; using YachtDice.Scoring;
namespace YachtDice.Game namespace YachtDice.Game
@@ -22,7 +23,7 @@ namespace YachtDice.Game
public event Action<int> OnTurnStarted; public event Action<int> OnTurnStarted;
public event Action<int> OnRollComplete; public event Action<int> OnRollComplete;
public event Action<YachtCategory, int> OnScored; public event Action<CategoryDefinitionSO, int> OnScored;
public event Action<int> OnGameOver; public event Action<int> OnGameOver;
private void Start() private void Start()
@@ -75,15 +76,15 @@ namespace YachtDice.Game
Debug.Log($"Dice {index + 1} (value={diceManager.GetValue(index)}): {(isLocked ? "LOCKED" : "UNLOCKED")}"); Debug.Log($"Dice {index + 1} (value={diceManager.GetValue(index)}): {(isLocked ? "LOCKED" : "UNLOCKED")}");
} }
public void ScoreInCategory(YachtCategory category) public void ScoreInCategory(CategoryDefinitionSO category)
{ {
if (!CanScore) return; if (!CanScore) return;
if (scoringSystem.IsCategoryUsed(category)) return; if (scoringSystem.IsCategoryUsed(category)) return;
int[] values = diceManager.GetCurrentValues(); var dice = diceManager.GetDice();
ScoreResult result = scoringSystem.ScoreCategory(values, category); ScoreResult result = scoringSystem.ScoreCategory(dice, category);
Debug.Log($"Scored {category}: base={result.BaseScore}, " + Debug.Log($"Scored {category.DisplayName}: base={result.BaseScore}, " +
$"bonus=+{result.FlatBonus}, mult=x{result.Multiplier:F1}, " + $"bonus=+{result.FlatBonus}, mult=x{result.Multiplier:F1}, " +
$"FINAL={result.FinalScore} | Total={scoringSystem.TotalScore}"); $"FINAL={result.FinalScore} | Total={scoringSystem.TotalScore}");
@@ -1,4 +1,5 @@
using UnityEngine; using UnityEngine;
using YachtDice.Categories;
using YachtDice.Economy; using YachtDice.Economy;
using YachtDice.Modifiers.Runtime; using YachtDice.Modifiers.Runtime;
using YachtDice.Scoring; using YachtDice.Scoring;
@@ -73,7 +74,7 @@ namespace YachtDice.Inventory
RefreshView(); RefreshView();
} }
private void HandleCategoryConfirmed(YachtCategory category, ScoreResult result) private void HandleCategoryConfirmed(CategoryDefinitionSO category, ScoreResult result)
{ {
model.ConsumeUseOnActive(); model.ConsumeUseOnActive();
} }
@@ -1,15 +1,15 @@
using UnityEngine; using UnityEngine;
using YachtDice.Categories;
using YachtDice.Modifiers.Core; using YachtDice.Modifiers.Core;
using YachtDice.Modifiers.Definition; using YachtDice.Modifiers.Definition;
using YachtDice.Modifiers.Runtime; using YachtDice.Modifiers.Runtime;
using YachtDice.Scoring;
namespace YachtDice.Modifiers.Conditions namespace YachtDice.Modifiers.Conditions
{ {
[CreateAssetMenu(fileName = "CategoryCondition", menuName = "YachtDice/Modifiers/Conditions/Category")] [CreateAssetMenu(fileName = "CategoryCondition", menuName = "YachtDice/Modifiers/Conditions/Category")]
public class CategoryCondition : ConditionSO public class CategoryCondition : ConditionSO
{ {
[SerializeField] private YachtCategory requiredCategory; [SerializeField] private CategoryDefinitionSO requiredCategory;
public override bool Evaluate(ModifierContext context, ModifierInstance instance) public override bool Evaluate(ModifierContext context, ModifierInstance instance)
{ {
@@ -17,7 +17,7 @@ namespace YachtDice.Modifiers.Conditions
} }
#if UNITY_EDITOR #if UNITY_EDITOR
public static CategoryCondition CreateForTest(YachtCategory category) public static CategoryCondition CreateForTest(CategoryDefinitionSO category)
{ {
var so = CreateInstance<CategoryCondition>(); var so = CreateInstance<CategoryCondition>();
so.requiredCategory = category; so.requiredCategory = category;
@@ -1,5 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using YachtDice.Categories;
using YachtDice.Dice;
using YachtDice.Modifiers.Runtime; using YachtDice.Modifiers.Runtime;
using YachtDice.Scoring; using YachtDice.Scoring;
@@ -12,8 +14,15 @@ namespace YachtDice.Modifiers.Core
public int FlatBonus; public int FlatBonus;
public float Multiplier = 1f; public float Multiplier = 1f;
public float PostMultiplier = 1f; public float PostMultiplier = 1f;
/// <summary>Абстрактные дайсы (основной API).</summary>
public IReadOnlyList<IDie> Dice;
/// <summary>Значения дайсов (обратная совместимость с существующими модификаторами).</summary>
public int[] DiceValues; public int[] DiceValues;
public YachtCategory Category;
/// <summary>Категория, в которую записывается результат.</summary>
public CategoryDefinitionSO Category;
// Game state (read-only snapshot) // Game state (read-only snapshot)
public int CurrentRoll; public int CurrentRoll;
@@ -46,8 +55,8 @@ namespace YachtDice.Modifiers.Core
public static ModifierContext CreateForScoring( public static ModifierContext CreateForScoring(
int baseScore, int baseScore,
int[] diceValues, IReadOnlyList<IDie> dice,
YachtCategory category, CategoryDefinitionSO category,
int currentRoll, int currentRoll,
int currentTurn, int currentTurn,
int playerCurrency, int playerCurrency,
@@ -56,7 +65,8 @@ namespace YachtDice.Modifiers.Core
return new ModifierContext return new ModifierContext
{ {
BaseScore = baseScore, BaseScore = baseScore,
DiceValues = diceValues, Dice = dice,
DiceValues = DiceCheckUtility.ExtractValues(dice),
Category = category, Category = category,
CurrentRoll = currentRoll, CurrentRoll = currentRoll,
CurrentTurn = currentTurn, CurrentTurn = currentTurn,
-82
View File
@@ -1,82 +0,0 @@
using System;
namespace YachtDice.Scoring
{
public static class CategoryScorer
{
public static int Calculate(int[] dice, YachtCategory category)
{
if (dice == null || dice.Length != 5)
throw new ArgumentException("Exactly 5 dice values required.");
return category switch
{
YachtCategory.Ones => SumOfValue(dice, 1),
YachtCategory.Twos => SumOfValue(dice, 2),
YachtCategory.Threes => SumOfValue(dice, 3),
YachtCategory.Fours => SumOfValue(dice, 4),
YachtCategory.Fives => SumOfValue(dice, 5),
YachtCategory.Sixes => SumOfValue(dice, 6),
YachtCategory.ThreeOfAKind => NOfAKind(dice, 3) ? Sum(dice) : 0,
YachtCategory.FourOfAKind => NOfAKind(dice, 4) ? Sum(dice) : 0,
YachtCategory.FullHouse => IsFullHouse(dice) ? 25 : 0,
YachtCategory.SmallStraight => HasStraightRun(dice, 4) ? 30 : 0,
YachtCategory.LargeStraight => HasStraightRun(dice, 5) ? 40 : 0,
YachtCategory.Yacht => NOfAKind(dice, 5) ? 50 : 0,
YachtCategory.Chance => Sum(dice),
_ => 0
};
}
private static int SumOfValue(int[] dice, int target)
{
int sum = 0;
for (int i = 0; i < dice.Length; i++)
if (dice[i] == target) sum += target;
return sum;
}
private static int Sum(int[] dice)
{
int sum = 0;
for (int i = 0; i < dice.Length; i++) sum += dice[i];
return sum;
}
private static bool NOfAKind(int[] dice, int n)
{
int[] counts = new int[7];
for (int i = 0; i < dice.Length; i++) counts[dice[i]]++;
for (int v = 1; v <= 6; v++)
if (counts[v] >= n) return true;
return false;
}
private static bool IsFullHouse(int[] dice)
{
int[] counts = new int[7];
for (int i = 0; i < dice.Length; i++) counts[dice[i]]++;
bool hasTwo = false, hasThree = false;
for (int v = 1; v <= 6; v++)
{
if (counts[v] == 2) hasTwo = true;
if (counts[v] == 3) hasThree = true;
}
return hasTwo && hasThree;
}
private static bool HasStraightRun(int[] dice, int runLength)
{
bool[] present = new bool[7];
for (int i = 0; i < dice.Length; i++) present[dice[i]] = true;
int consecutive = 0;
for (int v = 1; v <= 6; v++)
{
consecutive = present[v] ? consecutive + 1 : 0;
if (consecutive >= runLength) return true;
}
return false;
}
}
}
@@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 2fa2cb346fa82b846a3cf52c47ca9cda
+6 -3
View File
@@ -1,5 +1,8 @@
using System; using System;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using YachtDice.Categories;
using YachtDice.Dice;
namespace YachtDice.Scoring namespace YachtDice.Scoring
{ {
@@ -10,18 +13,18 @@ namespace YachtDice.Scoring
public int FlatBonus; public int FlatBonus;
public float Multiplier; public float Multiplier;
public int[] DiceValues; public int[] DiceValues;
public YachtCategory Category; public CategoryDefinitionSO Category;
public int FinalScore => Mathf.FloorToInt((BaseScore + FlatBonus) * Multiplier); public int FinalScore => Mathf.FloorToInt((BaseScore + FlatBonus) * Multiplier);
public static ScoreResult Create(int baseScore, int[] diceValues, YachtCategory category) public static ScoreResult Create(int baseScore, IReadOnlyList<IDie> dice, CategoryDefinitionSO category)
{ {
return new ScoreResult return new ScoreResult
{ {
BaseScore = baseScore, BaseScore = baseScore,
FlatBonus = 0, FlatBonus = 0,
Multiplier = 1f, Multiplier = 1f,
DiceValues = diceValues, DiceValues = DiceCheckUtility.ExtractValues(dice),
Category = category Category = category
}; };
} }
+29 -22
View File
@@ -3,6 +3,8 @@ using System.Collections.Generic;
using Cysharp.Threading.Tasks; using Cysharp.Threading.Tasks;
using UnityEngine; using UnityEngine;
using VContainer; using VContainer;
using YachtDice.Categories;
using YachtDice.Dice;
using YachtDice.Events; using YachtDice.Events;
using YachtDice.Modifiers.Core; using YachtDice.Modifiers.Core;
using YachtDice.Modifiers.Runtime; using YachtDice.Modifiers.Runtime;
@@ -11,26 +13,30 @@ namespace YachtDice.Scoring
{ {
public class ScoringSystem : MonoBehaviour public class ScoringSystem : MonoBehaviour
{ {
public event Action<YachtCategory, int> OnCategoryScored; public event Action<CategoryDefinitionSO, int> OnCategoryScored;
public event Action<int> OnAllCategoriesScored; public event Action<int> OnAllCategoriesScored;
public event Action<YachtCategory, ScoreResult> OnCategoryConfirmed; public event Action<CategoryDefinitionSO, ScoreResult> OnCategoryConfirmed;
private readonly Dictionary<YachtCategory, int> scorecard = new(); private readonly Dictionary<CategoryDefinitionSO, int> scorecard = new();
private readonly HashSet<YachtCategory> usedCategories = new(); private readonly HashSet<CategoryDefinitionSO> usedCategories = new();
private GameEventBus eventBus; private GameEventBus eventBus;
private ModifierRegistry modifierRegistry; private ModifierRegistry modifierRegistry;
private CategoryCatalogSO catalog;
[Inject] [Inject]
public void Construct(GameEventBus eventBus, ModifierRegistry modifierRegistry) public void Construct(GameEventBus eventBus, ModifierRegistry modifierRegistry, CategoryCatalogSO catalog)
{ {
this.eventBus = eventBus; this.eventBus = eventBus;
this.modifierRegistry = modifierRegistry; this.modifierRegistry = modifierRegistry;
this.catalog = catalog;
} }
public bool IsCategoryUsed(YachtCategory category) => usedCategories.Contains(category); public CategoryCatalogSO Catalog => catalog;
public int GetCategoryScore(YachtCategory category) public bool IsCategoryUsed(CategoryDefinitionSO category) => usedCategories.Contains(category);
public int GetCategoryScore(CategoryDefinitionSO category)
{ {
return scorecard.TryGetValue(category, out int score) ? score : -1; return scorecard.TryGetValue(category, out int score) ? score : -1;
} }
@@ -47,20 +53,20 @@ namespace YachtDice.Scoring
public int CategoriesFilledCount => usedCategories.Count; public int CategoriesFilledCount => usedCategories.Count;
public int TotalCategoryCount => Enum.GetValues(typeof(YachtCategory)).Length; public int TotalCategoryCount => catalog != null ? catalog.Count : 0;
public bool IsComplete => CategoriesFilledCount >= TotalCategoryCount; public bool IsComplete => CategoriesFilledCount >= TotalCategoryCount;
public ScoreResult PreviewScore(int[] diceValues, YachtCategory category, public ScoreResult PreviewScore(IReadOnlyList<IDie> dice, CategoryDefinitionSO category,
int currentRoll = 0, int currentTurn = 0, int playerCurrency = 0) int currentRoll = 0, int currentTurn = 0, int playerCurrency = 0)
{ {
int baseScore = CategoryScorer.Calculate(diceValues, category); int baseScore = category.Calculate(dice);
if (eventBus == null || modifierRegistry == null) if (eventBus == null || modifierRegistry == null)
return ScoreResult.Create(baseScore, diceValues, category); return ScoreResult.Create(baseScore, dice, category);
var context = ModifierContext.CreateForScoring( var context = ModifierContext.CreateForScoring(
baseScore, diceValues, category, baseScore, dice, category,
currentRoll, currentTurn, playerCurrency, currentRoll, currentTurn, playerCurrency,
modifierRegistry.Active); modifierRegistry.Active);
@@ -69,19 +75,19 @@ namespace YachtDice.Scoring
return context.ToScoreResult(); return context.ToScoreResult();
} }
public async UniTask<ScoreResult> ScoreCategoryAsync(int[] diceValues, YachtCategory category, public async UniTask<ScoreResult> ScoreCategoryAsync(IReadOnlyList<IDie> dice, CategoryDefinitionSO category,
int currentRoll, int currentTurn, int playerCurrency) int currentRoll, int currentTurn, int playerCurrency)
{ {
if (usedCategories.Contains(category)) if (usedCategories.Contains(category))
throw new InvalidOperationException($"Category {category} has already been scored."); throw new InvalidOperationException($"Category {category.DisplayName} has already been scored.");
int baseScore = CategoryScorer.Calculate(diceValues, category); int baseScore = category.Calculate(dice);
ModifierContext context; ModifierContext context;
if (eventBus != null && modifierRegistry != null) if (eventBus != null && modifierRegistry != null)
{ {
context = ModifierContext.CreateForScoring( context = ModifierContext.CreateForScoring(
baseScore, diceValues, category, baseScore, dice, category,
currentRoll, currentTurn, playerCurrency, currentRoll, currentTurn, playerCurrency,
modifierRegistry.Active); modifierRegistry.Active);
@@ -92,7 +98,8 @@ namespace YachtDice.Scoring
context = new ModifierContext context = new ModifierContext
{ {
BaseScore = baseScore, BaseScore = baseScore,
DiceValues = diceValues, Dice = dice,
DiceValues = DiceCheckUtility.ExtractValues(dice),
Category = category, Category = category,
}; };
} }
@@ -111,18 +118,18 @@ namespace YachtDice.Scoring
return result; return result;
} }
public ScoreResult ScoreCategory(int[] diceValues, YachtCategory category) public ScoreResult ScoreCategory(IReadOnlyList<IDie> dice, CategoryDefinitionSO category)
{ {
if (usedCategories.Contains(category)) if (usedCategories.Contains(category))
throw new InvalidOperationException($"Category {category} has already been scored."); throw new InvalidOperationException($"Category {category.DisplayName} has already been scored.");
int baseScore = CategoryScorer.Calculate(diceValues, category); int baseScore = category.Calculate(dice);
ModifierContext context = null; ModifierContext context = null;
if (eventBus != null && modifierRegistry != null) if (eventBus != null && modifierRegistry != null)
{ {
context = ModifierContext.CreateForScoring( context = ModifierContext.CreateForScoring(
baseScore, diceValues, category, baseScore, dice, category,
0, 0, 0, 0, 0, 0,
modifierRegistry.Active); modifierRegistry.Active);
@@ -133,7 +140,7 @@ namespace YachtDice.Scoring
if (context != null) if (context != null)
result = context.ToScoreResult(); result = context.ToScoreResult();
else else
result = ScoreResult.Create(baseScore, diceValues, category); result = ScoreResult.Create(baseScore, dice, category);
int finalScore = result.FinalScore; int finalScore = result.FinalScore;
scorecard[category] = finalScore; scorecard[category] = finalScore;
-22
View File
@@ -1,22 +0,0 @@
namespace YachtDice.Scoring
{
public enum YachtCategory
{
// Upper Section
Ones,
Twos,
Threes,
Fours,
Fives,
Sixes,
// Lower Section
ThreeOfAKind,
FourOfAKind,
FullHouse,
SmallStraight,
LargeStraight,
Yacht,
Chance
}
}
@@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: b90683b5d8ef0d24c8c050d7fd0eb75d
@@ -1,5 +1,6 @@
using NUnit.Framework; using NUnit.Framework;
using UnityEngine; using UnityEngine;
using YachtDice.Categories;
using YachtDice.Modifiers.Core; using YachtDice.Modifiers.Core;
using YachtDice.Modifiers.Definition; using YachtDice.Modifiers.Definition;
using YachtDice.Modifiers.Effects; using YachtDice.Modifiers.Effects;
@@ -10,19 +11,33 @@ namespace YachtDice.Tests
{ {
public class ModifierEffectTests public class ModifierEffectTests
{ {
private CategoryDefinitionSO testCategory;
[SetUp]
public void SetUp()
{
testCategory = SumAllCategorySO.CreateForTest("chance", "Шанс");
}
[TearDown]
public void TearDown()
{
Object.DestroyImmediate(testCategory);
}
private ModifierInstance CreateInstance(string id = "test") private ModifierInstance CreateInstance(string id = "test")
{ {
var def = ModifierDefinitionSO.CreateForTest(id, null); var def = ModifierDefinitionSO.CreateForTest(id, null);
return new ModifierInstance(def); return new ModifierInstance(def);
} }
private ModifierContext CreateContext(int baseScore, int[] dice, YachtCategory category) private ModifierContext CreateContext(int baseScore, int[] dice)
{ {
return new ModifierContext return new ModifierContext
{ {
BaseScore = baseScore, BaseScore = baseScore,
DiceValues = dice, DiceValues = dice,
Category = category, Category = testCategory,
}; };
} }
@@ -32,7 +47,7 @@ namespace YachtDice.Tests
public void AddPerDieEffect_CountsMatchingDice() public void AddPerDieEffect_CountsMatchingDice()
{ {
var effect = AddPerDieEffect.CreateForTest(10, targetDieValue: 1); var effect = AddPerDieEffect.CreateForTest(10, targetDieValue: 1);
var ctx = CreateContext(5, new[] { 1, 1, 3, 4, 1 }, YachtCategory.Ones); var ctx = CreateContext(5, new[] { 1, 1, 3, 4, 1 });
var inst = CreateInstance(); var inst = CreateInstance();
effect.Apply(ctx, inst).GetAwaiter().GetResult(); effect.Apply(ctx, inst).GetAwaiter().GetResult();
@@ -44,7 +59,7 @@ namespace YachtDice.Tests
public void AddPerDieEffect_ZeroTarget_CountsAllDice() public void AddPerDieEffect_ZeroTarget_CountsAllDice()
{ {
var effect = AddPerDieEffect.CreateForTest(2, targetDieValue: 0); var effect = AddPerDieEffect.CreateForTest(2, targetDieValue: 0);
var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 });
var inst = CreateInstance(); var inst = CreateInstance();
effect.Apply(ctx, inst).GetAwaiter().GetResult(); effect.Apply(ctx, inst).GetAwaiter().GetResult();
@@ -56,7 +71,7 @@ namespace YachtDice.Tests
public void AddPerDieEffect_NoMatches_ZeroBonus() public void AddPerDieEffect_NoMatches_ZeroBonus()
{ {
var effect = AddPerDieEffect.CreateForTest(10, targetDieValue: 6); var effect = AddPerDieEffect.CreateForTest(10, targetDieValue: 6);
var ctx = CreateContext(5, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateContext(5, new[] { 1, 2, 3, 4, 5 });
var inst = CreateInstance(); var inst = CreateInstance();
effect.Apply(ctx, inst).GetAwaiter().GetResult(); effect.Apply(ctx, inst).GetAwaiter().GetResult();
@@ -68,7 +83,7 @@ namespace YachtDice.Tests
public void AddPerDieEffect_ScalesWithStacks() public void AddPerDieEffect_ScalesWithStacks()
{ {
var effect = AddPerDieEffect.CreateForTest(10, targetDieValue: 1); var effect = AddPerDieEffect.CreateForTest(10, targetDieValue: 1);
var ctx = CreateContext(5, new[] { 1, 1, 3, 4, 1 }, YachtCategory.Ones); var ctx = CreateContext(5, new[] { 1, 1, 3, 4, 1 });
var inst = CreateInstance(); var inst = CreateInstance();
inst.Stacks = 2; inst.Stacks = 2;
@@ -83,7 +98,7 @@ namespace YachtDice.Tests
public void AddFlatScoreEffect_AddsFlat() public void AddFlatScoreEffect_AddsFlat()
{ {
var effect = AddFlatScoreEffect.CreateForTest(15); var effect = AddFlatScoreEffect.CreateForTest(15);
var ctx = CreateContext(25, new[] { 3, 3, 2, 2, 2 }, YachtCategory.FullHouse); var ctx = CreateContext(25, new[] { 3, 3, 2, 2, 2 });
var inst = CreateInstance(); var inst = CreateInstance();
effect.Apply(ctx, inst).GetAwaiter().GetResult(); effect.Apply(ctx, inst).GetAwaiter().GetResult();
@@ -95,7 +110,7 @@ namespace YachtDice.Tests
public void AddFlatScoreEffect_ScalesWithStacks() public void AddFlatScoreEffect_ScalesWithStacks()
{ {
var effect = AddFlatScoreEffect.CreateForTest(15); var effect = AddFlatScoreEffect.CreateForTest(15);
var ctx = CreateContext(25, new[] { 3, 3, 2, 2, 2 }, YachtCategory.FullHouse); var ctx = CreateContext(25, new[] { 3, 3, 2, 2, 2 });
var inst = CreateInstance(); var inst = CreateInstance();
inst.Stacks = 3; inst.Stacks = 3;
@@ -110,7 +125,7 @@ namespace YachtDice.Tests
public void MultiplyPerDieEffect_MultipliesPerMatch() public void MultiplyPerDieEffect_MultipliesPerMatch()
{ {
var effect = MultiplyPerDieEffect.CreateForTest(2f, targetDieValue: 6); var effect = MultiplyPerDieEffect.CreateForTest(2f, targetDieValue: 6);
var ctx = CreateContext(18, new[] { 6, 6, 6, 1, 2 }, YachtCategory.Sixes); var ctx = CreateContext(18, new[] { 6, 6, 6, 1, 2 });
var inst = CreateInstance(); var inst = CreateInstance();
effect.Apply(ctx, inst).GetAwaiter().GetResult(); effect.Apply(ctx, inst).GetAwaiter().GetResult();
@@ -122,7 +137,7 @@ namespace YachtDice.Tests
public void MultiplyPerDieEffect_NoMatches_MultiplierUnchanged() public void MultiplyPerDieEffect_NoMatches_MultiplierUnchanged()
{ {
var effect = MultiplyPerDieEffect.CreateForTest(3f, targetDieValue: 6); var effect = MultiplyPerDieEffect.CreateForTest(3f, targetDieValue: 6);
var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 });
var inst = CreateInstance(); var inst = CreateInstance();
effect.Apply(ctx, inst).GetAwaiter().GetResult(); effect.Apply(ctx, inst).GetAwaiter().GetResult();
@@ -136,7 +151,7 @@ namespace YachtDice.Tests
public void MultiplyScoreEffect_MultipliesOnce() public void MultiplyScoreEffect_MultipliesOnce()
{ {
var effect = MultiplyScoreEffect.CreateForTest(1.5f); var effect = MultiplyScoreEffect.CreateForTest(1.5f);
var ctx = CreateContext(50, new[] { 6, 6, 6, 6, 6 }, YachtCategory.Yacht); var ctx = CreateContext(50, new[] { 6, 6, 6, 6, 6 });
var inst = CreateInstance(); var inst = CreateInstance();
effect.Apply(ctx, inst).GetAwaiter().GetResult(); effect.Apply(ctx, inst).GetAwaiter().GetResult();
@@ -148,7 +163,7 @@ namespace YachtDice.Tests
public void MultiplyScoreEffect_ScalesWithStacks() public void MultiplyScoreEffect_ScalesWithStacks()
{ {
var effect = MultiplyScoreEffect.CreateForTest(2f); var effect = MultiplyScoreEffect.CreateForTest(2f);
var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 });
var inst = CreateInstance(); var inst = CreateInstance();
inst.Stacks = 3; inst.Stacks = 3;
@@ -164,7 +179,7 @@ namespace YachtDice.Tests
public void PostMultiplyEffect_MultipliesPostMultiplier() public void PostMultiplyEffect_MultipliesPostMultiplier()
{ {
var effect = PostMultiplyEffect.CreateForTest(2f); var effect = PostMultiplyEffect.CreateForTest(2f);
var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 });
var inst = CreateInstance(); var inst = CreateInstance();
effect.Apply(ctx, inst).GetAwaiter().GetResult(); effect.Apply(ctx, inst).GetAwaiter().GetResult();
@@ -178,7 +193,7 @@ namespace YachtDice.Tests
public void AddCurrencyEffect_AddsToCurrencyDelta() public void AddCurrencyEffect_AddsToCurrencyDelta()
{ {
var effect = AddCurrencyEffect.CreateForTest(25); var effect = AddCurrencyEffect.CreateForTest(25);
var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 });
var inst = CreateInstance(); var inst = CreateInstance();
effect.Apply(ctx, inst).GetAwaiter().GetResult(); effect.Apply(ctx, inst).GetAwaiter().GetResult();
@@ -190,7 +205,7 @@ namespace YachtDice.Tests
public void AddCurrencyEffect_ScalesWithStacks() public void AddCurrencyEffect_ScalesWithStacks()
{ {
var effect = AddCurrencyEffect.CreateForTest(25); var effect = AddCurrencyEffect.CreateForTest(25);
var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 });
var inst = CreateInstance(); var inst = CreateInstance();
inst.Stacks = 2; inst.Stacks = 2;
@@ -205,7 +220,7 @@ namespace YachtDice.Tests
public void ConsumeChargeEffect_DecrementsRemainingUses() public void ConsumeChargeEffect_DecrementsRemainingUses()
{ {
var effect = ConsumeChargeEffect.CreateForTest(1); var effect = ConsumeChargeEffect.CreateForTest(1);
var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 });
var def = ModifierDefinitionSO.CreateForTest("limited", null, var def = ModifierDefinitionSO.CreateForTest("limited", null,
hasLimitedUses: true, maxUses: 3); hasLimitedUses: true, maxUses: 3);
var inst = new ModifierInstance(def); var inst = new ModifierInstance(def);
@@ -219,7 +234,7 @@ namespace YachtDice.Tests
public void ConsumeChargeEffect_IgnoresPermanent() public void ConsumeChargeEffect_IgnoresPermanent()
{ {
var effect = ConsumeChargeEffect.CreateForTest(1); var effect = ConsumeChargeEffect.CreateForTest(1);
var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 });
var inst = CreateInstance(); // permanent (no limited uses) var inst = CreateInstance(); // permanent (no limited uses)
effect.Apply(ctx, inst).GetAwaiter().GetResult(); effect.Apply(ctx, inst).GetAwaiter().GetResult();
@@ -232,7 +247,7 @@ namespace YachtDice.Tests
[Test] [Test]
public void FinalScore_CombinesBaseAndFlatAndMultiplier() public void FinalScore_CombinesBaseAndFlatAndMultiplier()
{ {
var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateContext(10, new[] { 1, 2, 3, 4, 5 });
ctx.FlatBonus = 5; ctx.FlatBonus = 5;
ctx.Multiplier = 2f; ctx.Multiplier = 2f;
ctx.PostMultiplier = 1.5f; ctx.PostMultiplier = 1.5f;
@@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using NUnit.Framework; using NUnit.Framework;
using UnityEngine; using UnityEngine;
using YachtDice.Categories;
using YachtDice.Modifiers.Conditions; using YachtDice.Modifiers.Conditions;
using YachtDice.Modifiers.Core; using YachtDice.Modifiers.Core;
using YachtDice.Modifiers.Definition; using YachtDice.Modifiers.Definition;
@@ -16,12 +17,38 @@ namespace YachtDice.Tests
private ModifierRegistry registry; private ModifierRegistry registry;
private ModifierPipeline pipeline; private ModifierPipeline pipeline;
// Тестовые категории
private CategoryDefinitionSO chanceCategory;
private CategoryDefinitionSO fullHouseCategory;
private CategoryDefinitionSO onesCategory;
private CategoryDefinitionSO threesCategory;
private CategoryDefinitionSO foursCategory;
private CategoryDefinitionSO sixesCategory;
[SetUp] [SetUp]
public void SetUp() public void SetUp()
{ {
registry = new ModifierRegistry(10); registry = new ModifierRegistry(10);
pipeline = new ModifierPipeline(registry); pipeline = new ModifierPipeline(registry);
pipeline.TracingEnabled = false; // disable debug logs during tests pipeline.TracingEnabled = false;
chanceCategory = SumAllCategorySO.CreateForTest("chance", "Шанс");
fullHouseCategory = FullHouseCategorySO.CreateForTest("full_house", "Фулл-хаус");
onesCategory = SumOfValueCategorySO.CreateForTest("ones", "Единицы", 1);
threesCategory = SumOfValueCategorySO.CreateForTest("threes", "Тройки", 3);
foursCategory = SumOfValueCategorySO.CreateForTest("fours", "Четвёрки", 4);
sixesCategory = SumOfValueCategorySO.CreateForTest("sixes", "Шестёрки", 6);
}
[TearDown]
public void TearDown()
{
Object.DestroyImmediate(chanceCategory);
Object.DestroyImmediate(fullHouseCategory);
Object.DestroyImmediate(onesCategory);
Object.DestroyImmediate(threesCategory);
Object.DestroyImmediate(foursCategory);
Object.DestroyImmediate(sixesCategory);
} }
private ModifierDefinitionSO CreateDef(string id, private ModifierDefinitionSO CreateDef(string id,
@@ -40,7 +67,7 @@ namespace YachtDice.Tests
registry.TryActivate(inst); registry.TryActivate(inst);
} }
private ModifierContext CreateScoringContext(int baseScore, int[] dice, YachtCategory category) private ModifierContext CreateScoringContext(int baseScore, int[] dice, CategoryDefinitionSO category)
{ {
return new ModifierContext return new ModifierContext
{ {
@@ -64,10 +91,10 @@ namespace YachtDice.Tests
var mulDef = CreateDef("mul", TriggerType.OnCategoryScored, null, var mulDef = CreateDef("mul", TriggerType.OnCategoryScored, null,
new List<EffectSO> { mulEffect }); new List<EffectSO> { mulEffect });
RegisterAndActivate(mulDef); // registered first, but multiplicative phase RegisterAndActivate(mulDef);
RegisterAndActivate(addDef); // registered second, but additive phase RegisterAndActivate(addDef);
var ctx = CreateScoringContext(20, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateScoringContext(20, new[] { 1, 2, 3, 4, 5 }, chanceCategory);
var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult(); var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult();
// (20 + 10) * 2 = 60 // (20 + 10) * 2 = 60
@@ -88,7 +115,7 @@ namespace YachtDice.Tests
RegisterAndActivate(postDef); RegisterAndActivate(postDef);
RegisterAndActivate(mulDef); RegisterAndActivate(mulDef);
var ctx = CreateScoringContext(10, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateScoringContext(10, new[] { 1, 2, 3, 4, 5 }, chanceCategory);
var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult(); var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult();
// (10 + 0) * 2 * 3 = 60 // (10 + 0) * 2 * 3 = 60
@@ -100,7 +127,7 @@ namespace YachtDice.Tests
[Test] [Test]
public void Execute_ConditionFails_SkipsEffect() public void Execute_ConditionFails_SkipsEffect()
{ {
var condition = CategoryCondition.CreateForTest(YachtCategory.FullHouse); var condition = CategoryCondition.CreateForTest(fullHouseCategory);
var effect = AddFlatScoreEffect.CreateForTest(100); var effect = AddFlatScoreEffect.CreateForTest(100);
var def = CreateDef("fh-bonus", TriggerType.OnCategoryScored, var def = CreateDef("fh-bonus", TriggerType.OnCategoryScored,
@@ -110,7 +137,7 @@ namespace YachtDice.Tests
RegisterAndActivate(def); RegisterAndActivate(def);
// Scoring Ones, not FullHouse — condition should fail // Scoring Ones, not FullHouse — condition should fail
var ctx = CreateScoringContext(5, new[] { 1, 1, 1, 1, 1 }, YachtCategory.Ones); var ctx = CreateScoringContext(5, new[] { 1, 1, 1, 1, 1 }, onesCategory);
var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult(); var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult();
Assert.AreEqual(0, result.FlatBonus); Assert.AreEqual(0, result.FlatBonus);
@@ -120,7 +147,7 @@ namespace YachtDice.Tests
[Test] [Test]
public void Execute_ConditionPasses_AppliesEffect() public void Execute_ConditionPasses_AppliesEffect()
{ {
var condition = CategoryCondition.CreateForTest(YachtCategory.FullHouse); var condition = CategoryCondition.CreateForTest(fullHouseCategory);
var effect = AddFlatScoreEffect.CreateForTest(15); var effect = AddFlatScoreEffect.CreateForTest(15);
var def = CreateDef("fh-bonus", TriggerType.OnCategoryScored, var def = CreateDef("fh-bonus", TriggerType.OnCategoryScored,
@@ -129,7 +156,7 @@ namespace YachtDice.Tests
RegisterAndActivate(def); RegisterAndActivate(def);
var ctx = CreateScoringContext(25, new[] { 3, 3, 3, 2, 2 }, YachtCategory.FullHouse); var ctx = CreateScoringContext(25, new[] { 3, 3, 3, 2, 2 }, fullHouseCategory);
var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult(); var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult();
Assert.AreEqual(15, result.FlatBonus); Assert.AreEqual(15, result.FlatBonus);
@@ -147,8 +174,7 @@ namespace YachtDice.Tests
RegisterAndActivate(def); RegisterAndActivate(def);
// Fire OnCategoryScored, not OnTurnStart var ctx = CreateScoringContext(10, new[] { 1, 2, 3, 4, 5 }, chanceCategory);
var ctx = CreateScoringContext(10, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance);
var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult(); var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult();
Assert.AreEqual(0, result.FlatBonus); Assert.AreEqual(0, result.FlatBonus);
@@ -180,12 +206,9 @@ namespace YachtDice.Tests
RegisterAndActivate(def1); RegisterAndActivate(def1);
// dice: [3, 3, 3, 1, 2] — 3 threes // dice: [3, 3, 3, 1, 2] — 3 threes
var ctx = CreateScoringContext(9, new[] { 3, 3, 3, 1, 2 }, YachtCategory.Threes); var ctx = CreateScoringContext(9, new[] { 3, 3, 3, 1, 2 }, threesCategory);
var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult(); var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult();
// Additive phase: perDieAdd (+2*3=6) + flatAdd (+10) → FlatBonus = 16
// Multiplicative phase: perDieMul (1.5^3=3.375) then finalMul (*2) → Multiplier = 6.75
// FinalScore = floor((9 + 16) * 6.75) = floor(168.75) = 168
Assert.AreEqual(16, result.FlatBonus); Assert.AreEqual(16, result.FlatBonus);
Assert.AreEqual(168, result.FinalScore); Assert.AreEqual(168, result.FinalScore);
} }
@@ -195,7 +218,7 @@ namespace YachtDice.Tests
[Test] [Test]
public void Execute_NoActiveModifiers_NoChange() public void Execute_NoActiveModifiers_NoChange()
{ {
var ctx = CreateScoringContext(10, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateScoringContext(10, new[] { 1, 2, 3, 4, 5 }, chanceCategory);
var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult(); var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult();
Assert.AreEqual(10, result.FinalScore); Assert.AreEqual(10, result.FinalScore);
@@ -210,10 +233,9 @@ namespace YachtDice.Tests
var def = CreateDef("inactive", TriggerType.OnCategoryScored, null, var def = CreateDef("inactive", TriggerType.OnCategoryScored, null,
new List<EffectSO> { effect }); new List<EffectSO> { effect });
// Add but don't activate
registry.Add(def); registry.Add(def);
var ctx = CreateScoringContext(10, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateScoringContext(10, new[] { 1, 2, 3, 4, 5 }, chanceCategory);
var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult(); var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult();
Assert.AreEqual(0, result.FlatBonus); Assert.AreEqual(0, result.FlatBonus);
@@ -233,7 +255,7 @@ namespace YachtDice.Tests
RegisterAndActivate(def); RegisterAndActivate(def);
var ctx = CreateScoringContext(10, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateScoringContext(10, new[] { 1, 2, 3, 4, 5 }, chanceCategory);
var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult(); var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult();
Assert.AreEqual(10, result.FlatBonus); Assert.AreEqual(10, result.FlatBonus);
@@ -254,7 +276,7 @@ namespace YachtDice.Tests
RegisterAndActivate(def); RegisterAndActivate(def);
var ctx = CreateScoringContext(10, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateScoringContext(10, new[] { 1, 2, 3, 4, 5 }, chanceCategory);
var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult(); var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult();
Assert.IsNotNull(result.DebugLog); Assert.IsNotNull(result.DebugLog);
@@ -276,13 +298,13 @@ namespace YachtDice.Tests
RegisterAndActivate(def); RegisterAndActivate(def);
// Only 2 sixes — condition requires 3 // Only 2 sixes — condition requires 3
var ctx = CreateScoringContext(12, new[] { 6, 6, 1, 2, 3 }, YachtCategory.Sixes); var ctx = CreateScoringContext(12, new[] { 6, 6, 1, 2, 3 }, sixesCategory);
var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult(); var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult();
Assert.AreEqual(0, result.FlatBonus); Assert.AreEqual(0, result.FlatBonus);
// 3 sixes — condition passes // 3 sixes — condition passes
var ctx2 = CreateScoringContext(18, new[] { 6, 6, 6, 1, 2 }, YachtCategory.Sixes); var ctx2 = CreateScoringContext(18, new[] { 6, 6, 6, 1, 2 }, sixesCategory);
var result2 = pipeline.Execute(TriggerType.OnCategoryScored, ctx2).GetAwaiter().GetResult(); var result2 = pipeline.Execute(TriggerType.OnCategoryScored, ctx2).GetAwaiter().GetResult();
Assert.AreEqual(100, result2.FlatBonus); Assert.AreEqual(100, result2.FlatBonus);
@@ -303,13 +325,13 @@ namespace YachtDice.Tests
RegisterAndActivate(def); RegisterAndActivate(def);
// Below threshold // Below threshold
var ctx = CreateScoringContext(15, new[] { 3, 3, 3, 3, 3 }, YachtCategory.Threes); var ctx = CreateScoringContext(15, new[] { 3, 3, 3, 3, 3 }, threesCategory);
var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult(); var result = pipeline.Execute(TriggerType.OnCategoryScored, ctx).GetAwaiter().GetResult();
Assert.AreEqual(1f, result.Multiplier); Assert.AreEqual(1f, result.Multiplier);
// At threshold // At threshold
var ctx2 = CreateScoringContext(20, new[] { 4, 4, 4, 4, 4 }, YachtCategory.Fours); var ctx2 = CreateScoringContext(20, new[] { 4, 4, 4, 4, 4 }, foursCategory);
var result2 = pipeline.Execute(TriggerType.OnCategoryScored, ctx2).GetAwaiter().GetResult(); var result2 = pipeline.Execute(TriggerType.OnCategoryScored, ctx2).GetAwaiter().GetResult();
Assert.AreEqual(2f, result2.Multiplier); Assert.AreEqual(2f, result2.Multiplier);
@@ -320,7 +342,7 @@ namespace YachtDice.Tests
[Test] [Test]
public void ToScoreResult_ConvertsCorrectly() public void ToScoreResult_ConvertsCorrectly()
{ {
var ctx = CreateScoringContext(10, new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var ctx = CreateScoringContext(10, new[] { 1, 2, 3, 4, 5 }, chanceCategory);
ctx.FlatBonus = 5; ctx.FlatBonus = 5;
ctx.Multiplier = 2f; ctx.Multiplier = 2f;
ctx.PostMultiplier = 1.5f; ctx.PostMultiplier = 1.5f;
@@ -330,7 +352,7 @@ namespace YachtDice.Tests
Assert.AreEqual(10, sr.BaseScore); Assert.AreEqual(10, sr.BaseScore);
Assert.AreEqual(5, sr.FlatBonus); Assert.AreEqual(5, sr.FlatBonus);
Assert.AreEqual(3f, sr.Multiplier, 0.001f); // 2 * 1.5 Assert.AreEqual(3f, sr.Multiplier, 0.001f); // 2 * 1.5
Assert.AreEqual(YachtCategory.Chance, sr.Category); Assert.AreEqual(chanceCategory, sr.Category);
} }
} }
} }
+131 -14
View File
@@ -1,15 +1,35 @@
using System.Collections.Generic;
using NUnit.Framework; using NUnit.Framework;
using UnityEngine; using UnityEngine;
using YachtDice.Categories;
using YachtDice.Dice;
using YachtDice.Scoring; using YachtDice.Scoring;
namespace YachtDice.Tests namespace YachtDice.Tests
{ {
public class ScoringSystemTests public class ScoringSystemTests
{ {
private ScoringSystem CreateScoringSystem() private CategoryDefinitionSO yachtCategory;
private CategoryDefinitionSO onesCategory;
private CategoryDefinitionSO twosCategory;
private CategoryDefinitionSO chanceCategory;
private CategoryCatalogSO catalog;
private DieDefinitionSO standardDie;
[SetUp]
public void SetUp()
{ {
var go = new GameObject("ScoringSystem"); standardDie = DieDefinitionSO.CreateForTest<StandardDieSO>("d6", "d6");
return go.AddComponent<ScoringSystem>();
yachtCategory = NOfAKindCategorySO.CreateForTest("yacht", "Яхта", 5, fixedScoreMode: true, score: 50);
onesCategory = SumOfValueCategorySO.CreateForTest("ones", "Единицы", 1);
twosCategory = SumOfValueCategorySO.CreateForTest("twos", "Двойки", 2);
chanceCategory = SumAllCategorySO.CreateForTest("chance", "Шанс");
catalog = CategoryCatalogSO.CreateForTest(new List<CategoryDefinitionSO>
{
onesCategory, twosCategory, yachtCategory, chanceCategory
});
} }
[TearDown] [TearDown]
@@ -17,13 +37,35 @@ namespace YachtDice.Tests
{ {
foreach (var go in Object.FindObjectsByType<ScoringSystem>(FindObjectsSortMode.None)) foreach (var go in Object.FindObjectsByType<ScoringSystem>(FindObjectsSortMode.None))
Object.DestroyImmediate(go.gameObject); Object.DestroyImmediate(go.gameObject);
Object.DestroyImmediate(yachtCategory);
Object.DestroyImmediate(onesCategory);
Object.DestroyImmediate(twosCategory);
Object.DestroyImmediate(chanceCategory);
Object.DestroyImmediate(catalog);
Object.DestroyImmediate(standardDie);
}
private ScoringSystem CreateScoringSystem()
{
var go = new GameObject("ScoringSystem");
return go.AddComponent<ScoringSystem>();
}
private IReadOnlyList<IDie> CreateDice(params int[] values)
{
var dice = new DieInstance[values.Length];
for (int i = 0; i < values.Length; i++)
dice[i] = new DieInstance(standardDie, values[i]);
return dice;
} }
[Test] [Test]
public void ScoreCategory_WithNoModifiers_CalculatesBaseOnly() public void ScoreCategory_WithNoModifiers_CalculatesBaseOnly()
{ {
var system = CreateScoringSystem(); var system = CreateScoringSystem();
var result = system.ScoreCategory(new[] { 6, 6, 6, 6, 6 }, YachtCategory.Yacht); var dice = CreateDice(6, 6, 6, 6, 6);
var result = system.ScoreCategory(dice, yachtCategory);
Assert.AreEqual(50, result.BaseScore); Assert.AreEqual(50, result.BaseScore);
Assert.AreEqual(0, result.FlatBonus); Assert.AreEqual(0, result.FlatBonus);
@@ -35,7 +77,7 @@ namespace YachtDice.Tests
public void ScoreCategory_FiresOnCategoryConfirmed() public void ScoreCategory_FiresOnCategoryConfirmed()
{ {
var system = CreateScoringSystem(); var system = CreateScoringSystem();
YachtCategory firedCategory = (YachtCategory)(-1); CategoryDefinitionSO firedCategory = null;
ScoreResult firedResult = default; ScoreResult firedResult = default;
system.OnCategoryConfirmed += (cat, res) => system.OnCategoryConfirmed += (cat, res) =>
@@ -44,9 +86,10 @@ namespace YachtDice.Tests
firedResult = res; firedResult = res;
}; };
system.ScoreCategory(new[] { 1, 1, 1, 1, 1 }, YachtCategory.Ones); var dice = CreateDice(1, 1, 1, 1, 1);
system.ScoreCategory(dice, onesCategory);
Assert.AreEqual(YachtCategory.Ones, firedCategory); Assert.AreEqual(onesCategory, firedCategory);
Assert.AreEqual(5, firedResult.BaseScore); Assert.AreEqual(5, firedResult.BaseScore);
} }
@@ -54,17 +97,19 @@ namespace YachtDice.Tests
public void ScoreCategory_PreventsDuplicateCategory() public void ScoreCategory_PreventsDuplicateCategory()
{ {
var system = CreateScoringSystem(); var system = CreateScoringSystem();
system.ScoreCategory(new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var dice = CreateDice(1, 2, 3, 4, 5);
system.ScoreCategory(dice, chanceCategory);
Assert.Throws<System.InvalidOperationException>(() => Assert.Throws<System.InvalidOperationException>(() =>
system.ScoreCategory(new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance)); system.ScoreCategory(dice, chanceCategory));
} }
[Test] [Test]
public void PreviewScore_WithNoModifiers_CalculatesBaseOnly() public void PreviewScore_WithNoModifiers_CalculatesBaseOnly()
{ {
var system = CreateScoringSystem(); var system = CreateScoringSystem();
var result = system.PreviewScore(new[] { 1, 2, 3, 4, 5 }, YachtCategory.Chance); var dice = CreateDice(1, 2, 3, 4, 5);
var result = system.PreviewScore(dice, chanceCategory);
Assert.AreEqual(15, result.BaseScore); Assert.AreEqual(15, result.BaseScore);
Assert.AreEqual(0, result.FlatBonus); Assert.AreEqual(0, result.FlatBonus);
@@ -75,8 +120,8 @@ namespace YachtDice.Tests
public void TotalScore_SumsAllScoredCategories() public void TotalScore_SumsAllScoredCategories()
{ {
var system = CreateScoringSystem(); var system = CreateScoringSystem();
system.ScoreCategory(new[] { 1, 1, 1, 1, 1 }, YachtCategory.Ones); system.ScoreCategory(CreateDice(1, 1, 1, 1, 1), onesCategory);
system.ScoreCategory(new[] { 2, 2, 2, 2, 2 }, YachtCategory.Twos); system.ScoreCategory(CreateDice(2, 2, 2, 2, 2), twosCategory);
Assert.AreEqual(15, system.TotalScore); // 5 + 10 Assert.AreEqual(15, system.TotalScore); // 5 + 10
} }
@@ -85,12 +130,84 @@ namespace YachtDice.Tests
public void ResetScorecard_ClearsAll() public void ResetScorecard_ClearsAll()
{ {
var system = CreateScoringSystem(); var system = CreateScoringSystem();
system.ScoreCategory(new[] { 1, 1, 1, 1, 1 }, YachtCategory.Ones); system.ScoreCategory(CreateDice(1, 1, 1, 1, 1), onesCategory);
system.ResetScorecard(); system.ResetScorecard();
Assert.AreEqual(0, system.TotalScore); Assert.AreEqual(0, system.TotalScore);
Assert.IsFalse(system.IsCategoryUsed(YachtCategory.Ones)); Assert.IsFalse(system.IsCategoryUsed(onesCategory));
}
// ── Category SO Unit Tests ──────────────────────────────────
[Test]
public void SumOfValueCategory_SumsCorrectly()
{
var dice = CreateDice(3, 3, 3, 1, 2);
var cat = SumOfValueCategorySO.CreateForTest("threes", "Тройки", 3);
Assert.AreEqual(9, cat.Calculate(dice));
Object.DestroyImmediate(cat);
}
[Test]
public void NOfAKindCategory_ThreeOfAKind_ReturnsSumOrZero()
{
var cat = NOfAKindCategorySO.CreateForTest("three_of_a_kind", "Тройка", 3);
Assert.AreEqual(17, cat.Calculate(CreateDice(4, 4, 4, 3, 2))); // sum = 17
Assert.AreEqual(0, cat.Calculate(CreateDice(1, 2, 3, 4, 5))); // no 3-of-a-kind
Object.DestroyImmediate(cat);
}
[Test]
public void NOfAKindCategory_Yacht_ReturnsFixedScore()
{
Assert.AreEqual(50, yachtCategory.Calculate(CreateDice(6, 6, 6, 6, 6)));
Assert.AreEqual(0, yachtCategory.Calculate(CreateDice(6, 6, 6, 6, 1)));
}
[Test]
public void FullHouseCategory_CalculatesCorrectly()
{
var cat = FullHouseCategorySO.CreateForTest("fh", "Фулл-хаус", 25);
Assert.AreEqual(25, cat.Calculate(CreateDice(3, 3, 3, 2, 2)));
Assert.AreEqual(0, cat.Calculate(CreateDice(3, 3, 3, 3, 2)));
Object.DestroyImmediate(cat);
}
[Test]
public void StraightCategory_SmallStraight()
{
var cat = StraightCategorySO.CreateForTest("ss", "Малый стрит", 4, 30);
Assert.AreEqual(30, cat.Calculate(CreateDice(1, 2, 3, 4, 6)));
Assert.AreEqual(0, cat.Calculate(CreateDice(1, 2, 3, 5, 6)));
Object.DestroyImmediate(cat);
}
[Test]
public void StraightCategory_LargeStraight()
{
var cat = StraightCategorySO.CreateForTest("ls", "Большой стрит", 5, 40);
Assert.AreEqual(40, cat.Calculate(CreateDice(1, 2, 3, 4, 5)));
Assert.AreEqual(40, cat.Calculate(CreateDice(2, 3, 4, 5, 6)));
Assert.AreEqual(0, cat.Calculate(CreateDice(1, 2, 3, 4, 6)));
Object.DestroyImmediate(cat);
}
[Test]
public void SumAllCategory_SumsEverything()
{
Assert.AreEqual(15, chanceCategory.Calculate(CreateDice(1, 2, 3, 4, 5)));
Assert.AreEqual(30, chanceCategory.Calculate(CreateDice(6, 6, 6, 6, 6)));
} }
} }
} }
+6 -6
View File
@@ -2,7 +2,7 @@ using System;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
using TMPro; using TMPro;
using YachtDice.Scoring; using YachtDice.Categories;
namespace YachtDice.UI namespace YachtDice.UI
{ {
@@ -21,16 +21,16 @@ namespace YachtDice.UI
[SerializeField] private Color previewPositiveColor = new Color(0.85f, 1f, 0.85f, 1f); [SerializeField] private Color previewPositiveColor = new Color(0.85f, 1f, 0.85f, 1f);
[SerializeField] private Color previewZeroColor = new Color(1f, 0.88f, 0.88f, 1f); [SerializeField] private Color previewZeroColor = new Color(1f, 0.88f, 0.88f, 1f);
private YachtCategory category; private CategoryDefinitionSO category;
private bool isUsed; private bool isUsed;
public event Action<YachtCategory> OnCategorySelected; public event Action<CategoryDefinitionSO> OnCategorySelected;
public void Initialize(YachtCategory cat, string displayName) public void Initialize(CategoryDefinitionSO categoryDef)
{ {
category = cat; category = categoryDef;
isUsed = false; isUsed = false;
categoryNameText.text = displayName; categoryNameText.text = categoryDef.DisplayName;
previewText.text = ""; previewText.text = "";
recordedScoreText.text = "-"; recordedScoreText.text = "-";
selectButton.onClick.AddListener(HandleClick); selectButton.onClick.AddListener(HandleClick);
+39 -36
View File
@@ -1,7 +1,8 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using VContainer; using VContainer;
using YachtDice.Categories;
using YachtDice.Dice;
using YachtDice.Game; using YachtDice.Game;
using YachtDice.Scoring; using YachtDice.Scoring;
using YachtDice.Economy; using YachtDice.Economy;
@@ -34,33 +35,25 @@ namespace YachtDice.UI
[SerializeField] private int maxRollsPerTurn = 3; [SerializeField] private int maxRollsPerTurn = 3;
[SerializeField] private int maxActiveModifierSlots = 5; [SerializeField] private int maxActiveModifierSlots = 5;
private static readonly YachtCategory[] UpperCategories =
{
YachtCategory.Ones, YachtCategory.Twos, YachtCategory.Threes,
YachtCategory.Fours, YachtCategory.Fives, YachtCategory.Sixes
};
private const int UpperBonusThreshold = 63; private const int UpperBonusThreshold = 63;
private const int UpperBonusValue = 35; private const int UpperBonusValue = 35;
private int totalCategoryCount;
private ModifierRegistry modifierRegistry; private ModifierRegistry modifierRegistry;
private CategoryCatalogSO categoryCatalog;
private InventoryModel inventoryModel; private InventoryModel inventoryModel;
private ShopModel shopModel; private ShopModel shopModel;
[Inject] [Inject]
public void Construct(ModifierRegistry modifierRegistry) public void Construct(ModifierRegistry modifierRegistry, CategoryCatalogSO categoryCatalog)
{ {
this.modifierRegistry = modifierRegistry; this.modifierRegistry = modifierRegistry;
this.categoryCatalog = categoryCatalog;
} }
// ── Lifecycle ────────────────────────────────────────────── // ── Lifecycle ──────────────────────────────────────────────
private void Awake() private void Awake()
{ {
totalCategoryCount = Enum.GetValues(typeof(YachtCategory)).Length;
// Model → Controller // Model → Controller
gameManager.OnTurnStarted += HandleTurnStarted; gameManager.OnTurnStarted += HandleTurnStarted;
gameManager.OnRollComplete += HandleRollComplete; gameManager.OnRollComplete += HandleRollComplete;
@@ -83,6 +76,9 @@ namespace YachtDice.UI
private void Start() private void Start()
{ {
// Инициализируем скоркарту из каталога категорий
scoreCardView.Initialize(categoryCatalog);
InitializeModifierSystems(); InitializeModifierSystems();
} }
@@ -197,6 +193,7 @@ namespace YachtDice.UI
private void HandleTurnStarted(int turn) private void HandleTurnStarted(int turn)
{ {
int totalCategoryCount = categoryCatalog.Count;
gameInfoView.SetTurnText(turn, totalCategoryCount); gameInfoView.SetTurnText(turn, totalCategoryCount);
dicePanelView.ResetForNewTurn(); dicePanelView.ResetForNewTurn();
dicePanelView.SetRollButtonState(true, 0, maxRollsPerTurn); dicePanelView.SetRollButtonState(true, 0, maxRollsPerTurn);
@@ -212,7 +209,7 @@ namespace YachtDice.UI
int[] values = diceManager.GetCurrentValues(); int[] values = diceManager.GetCurrentValues();
dicePanelView.SetAllDiceValues(values); dicePanelView.SetAllDiceValues(values);
UpdatePreviewScores(values); UpdatePreviewScores();
} }
private void HandleDieSettled(int index, int value) private void HandleDieSettled(int index, int value)
@@ -220,7 +217,7 @@ namespace YachtDice.UI
dicePanelView.SetDieValue(index, value); dicePanelView.SetDieValue(index, value);
} }
private void HandleScored(YachtCategory category, int finalScore) private void HandleScored(CategoryDefinitionSO category, int finalScore)
{ {
scoreCardView.SetCategoryScored(category, finalScore); scoreCardView.SetCategoryScored(category, finalScore);
UpdateTotalDisplay(); UpdateTotalDisplay();
@@ -262,7 +259,7 @@ namespace YachtDice.UI
dicePanelView.SetDieLocked(index, isLocked); dicePanelView.SetDieLocked(index, isLocked);
} }
private void HandleCategorySelected(YachtCategory category) private void HandleCategorySelected(CategoryDefinitionSO category)
{ {
if (!gameManager.CanScore) return; if (!gameManager.CanScore) return;
if (scoringSystem.IsCategoryUsed(category)) return; if (scoringSystem.IsCategoryUsed(category)) return;
@@ -317,17 +314,19 @@ namespace YachtDice.UI
// ── Helpers ──────────────────────────────────────────────── // ── Helpers ────────────────────────────────────────────────
private void UpdatePreviewScores(int[] diceValues) private void UpdatePreviewScores()
{ {
var previews = new Dictionary<YachtCategory, int>(); var dice = diceManager.GetDice();
var categories = (YachtCategory[])Enum.GetValues(typeof(YachtCategory)); var previews = new Dictionary<CategoryDefinitionSO, int>();
var allCategories = categoryCatalog.All;
for (int i = 0; i < categories.Length; i++) for (int i = 0; i < allCategories.Count; i++)
{ {
if (scoringSystem.IsCategoryUsed(categories[i])) continue; var cat = allCategories[i];
if (scoringSystem.IsCategoryUsed(cat)) continue;
ScoreResult result = scoringSystem.PreviewScore(diceValues, categories[i]); ScoreResult result = scoringSystem.PreviewScore(dice, cat);
previews[categories[i]] = result.FinalScore; previews[cat] = result.FinalScore;
} }
scoreCardView.UpdatePreviews(previews); scoreCardView.UpdatePreviews(previews);
@@ -335,29 +334,33 @@ namespace YachtDice.UI
private void UpdateTotalDisplay() private void UpdateTotalDisplay()
{ {
int upperSum = 0; int upperSum = CalculateUpperSum();
for (int i = 0; i < UpperCategories.Length; i++)
{
int catScore = scoringSystem.GetCategoryScore(UpperCategories[i]);
if (catScore >= 0) upperSum += catScore;
}
bool hasBonus = upperSum >= UpperBonusThreshold; bool hasBonus = upperSum >= UpperBonusThreshold;
int displayTotal = CalculateDisplayTotal(); int displayTotal = CalculateDisplayTotal();
scoreCardView.UpdateTotalDisplay(displayTotal, upperSum, hasBonus); scoreCardView.UpdateTotalDisplay(displayTotal, upperSum, hasBonus);
} }
private int CalculateUpperSum()
{
int upperSum = 0;
var allCategories = categoryCatalog.All;
for (int i = 0; i < allCategories.Count; i++)
{
if (!allCategories[i].IsUpperSection) continue;
int catScore = scoringSystem.GetCategoryScore(allCategories[i]);
if (catScore >= 0) upperSum += catScore;
}
return upperSum;
}
private int CalculateDisplayTotal() private int CalculateDisplayTotal()
{ {
int total = scoringSystem.TotalScore; int total = scoringSystem.TotalScore;
int upperSum = CalculateUpperSum();
int upperSum = 0;
for (int i = 0; i < UpperCategories.Length; i++)
{
int catScore = scoringSystem.GetCategoryScore(UpperCategories[i]);
if (catScore >= 0) upperSum += catScore;
}
if (upperSum >= UpperBonusThreshold) if (upperSum >= UpperBonusThreshold)
total += UpperBonusValue; total += UpperBonusValue;
+26 -34
View File
@@ -2,13 +2,13 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using TMPro; using TMPro;
using YachtDice.Scoring; using YachtDice.Categories;
namespace YachtDice.UI namespace YachtDice.UI
{ {
public class ScoreCardView : MonoBehaviour public class ScoreCardView : MonoBehaviour
{ {
[Header("Category Rows (in YachtCategory enum order)")] [Header("Category Rows (порядок соответствует каталогу)")]
[SerializeField] private List<CategoryRowView> categoryRows = new(); [SerializeField] private List<CategoryRowView> categoryRows = new();
[Header("Summary")] [Header("Summary")]
@@ -16,48 +16,41 @@ namespace YachtDice.UI
[SerializeField] private TMP_Text upperBonusText; [SerializeField] private TMP_Text upperBonusText;
[SerializeField] private TMP_Text totalScoreText; [SerializeField] private TMP_Text totalScoreText;
public event Action<YachtCategory> OnCategorySelected; public event Action<CategoryDefinitionSO> OnCategorySelected;
private static readonly string[] CategoryNames = private CategoryCatalogSO catalog;
private Dictionary<CategoryDefinitionSO, int> categoryToRowIndex;
/// <summary>
/// Инициализирует скоркарту из каталога категорий.
/// Вызывается из GameController после DI.
/// </summary>
public void Initialize(CategoryCatalogSO categoryCatalog)
{ {
"Единицы", catalog = categoryCatalog;
"Двойки", categoryToRowIndex = new Dictionary<CategoryDefinitionSO, int>();
"Тройки",
"Четвёрки",
"Пятёрки",
"Шестёрки",
"Тройка",
"Каре",
"Фулл-хаус",
"Малый стрит",
"Большой стрит",
"Яхта",
"Шанс"
};
private YachtCategory[] _allCategories; var all = catalog.All;
int count = Mathf.Min(categoryRows.Count, all.Count);
private void Awake() for (int i = 0; i < count; i++)
{
_allCategories = (YachtCategory[])Enum.GetValues(typeof(YachtCategory));
for (int i = 0; i < categoryRows.Count && i < _allCategories.Length; i++)
{ {
categoryRows[i].Initialize(_allCategories[i], CategoryNames[i]); categoryRows[i].Initialize(all[i]);
categoryRows[i].OnCategorySelected += HandleCategorySelected; categoryRows[i].OnCategorySelected += HandleCategorySelected;
categoryToRowIndex[all[i]] = i;
} }
UpdateTotalDisplay(0, 0, false); UpdateTotalDisplay(0, 0, false);
} }
public void UpdatePreviews(Dictionary<YachtCategory, int> previews) public void UpdatePreviews(Dictionary<CategoryDefinitionSO, int> previews)
{ {
for (int i = 0; i < categoryRows.Count && i < _allCategories.Length; i++) foreach (var kvp in previews)
{ {
if (previews.TryGetValue(_allCategories[i], out int preview)) if (categoryToRowIndex.TryGetValue(kvp.Key, out int rowIndex))
{ {
categoryRows[i].ShowPreview(preview); categoryRows[rowIndex].ShowPreview(kvp.Value);
categoryRows[i].SetInteractable(true); categoryRows[rowIndex].SetInteractable(true);
} }
} }
} }
@@ -71,10 +64,9 @@ namespace YachtDice.UI
} }
} }
public void SetCategoryScored(YachtCategory category, int score) public void SetCategoryScored(CategoryDefinitionSO category, int score)
{ {
int index = (int)category; if (categoryToRowIndex.TryGetValue(category, out int index))
if (index >= 0 && index < categoryRows.Count)
categoryRows[index].SetRecordedScore(score); categoryRows[index].SetRecordedScore(score);
} }
@@ -99,7 +91,7 @@ namespace YachtDice.UI
UpdateTotalDisplay(0, 0, false); UpdateTotalDisplay(0, 0, false);
} }
private void HandleCategorySelected(YachtCategory category) private void HandleCategorySelected(CategoryDefinitionSO category)
{ {
OnCategorySelected?.Invoke(category); OnCategorySelected?.Invoke(category);
} }