[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
+6 -6
View File
@@ -2,7 +2,7 @@ using System;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using YachtDice.Scoring;
using YachtDice.Categories;
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 previewZeroColor = new Color(1f, 0.88f, 0.88f, 1f);
private YachtCategory category;
private CategoryDefinitionSO category;
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;
categoryNameText.text = displayName;
categoryNameText.text = categoryDef.DisplayName;
previewText.text = "";
recordedScoreText.text = "-";
selectButton.onClick.AddListener(HandleClick);
+39 -36
View File
@@ -1,7 +1,8 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using VContainer;
using YachtDice.Categories;
using YachtDice.Dice;
using YachtDice.Game;
using YachtDice.Scoring;
using YachtDice.Economy;
@@ -34,33 +35,25 @@ namespace YachtDice.UI
[SerializeField] private int maxRollsPerTurn = 3;
[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 UpperBonusValue = 35;
private int totalCategoryCount;
private ModifierRegistry modifierRegistry;
private CategoryCatalogSO categoryCatalog;
private InventoryModel inventoryModel;
private ShopModel shopModel;
[Inject]
public void Construct(ModifierRegistry modifierRegistry)
public void Construct(ModifierRegistry modifierRegistry, CategoryCatalogSO categoryCatalog)
{
this.modifierRegistry = modifierRegistry;
this.categoryCatalog = categoryCatalog;
}
// ── Lifecycle ──────────────────────────────────────────────
private void Awake()
{
totalCategoryCount = Enum.GetValues(typeof(YachtCategory)).Length;
// Model → Controller
gameManager.OnTurnStarted += HandleTurnStarted;
gameManager.OnRollComplete += HandleRollComplete;
@@ -83,6 +76,9 @@ namespace YachtDice.UI
private void Start()
{
// Инициализируем скоркарту из каталога категорий
scoreCardView.Initialize(categoryCatalog);
InitializeModifierSystems();
}
@@ -197,6 +193,7 @@ namespace YachtDice.UI
private void HandleTurnStarted(int turn)
{
int totalCategoryCount = categoryCatalog.Count;
gameInfoView.SetTurnText(turn, totalCategoryCount);
dicePanelView.ResetForNewTurn();
dicePanelView.SetRollButtonState(true, 0, maxRollsPerTurn);
@@ -212,7 +209,7 @@ namespace YachtDice.UI
int[] values = diceManager.GetCurrentValues();
dicePanelView.SetAllDiceValues(values);
UpdatePreviewScores(values);
UpdatePreviewScores();
}
private void HandleDieSettled(int index, int value)
@@ -220,7 +217,7 @@ namespace YachtDice.UI
dicePanelView.SetDieValue(index, value);
}
private void HandleScored(YachtCategory category, int finalScore)
private void HandleScored(CategoryDefinitionSO category, int finalScore)
{
scoreCardView.SetCategoryScored(category, finalScore);
UpdateTotalDisplay();
@@ -262,7 +259,7 @@ namespace YachtDice.UI
dicePanelView.SetDieLocked(index, isLocked);
}
private void HandleCategorySelected(YachtCategory category)
private void HandleCategorySelected(CategoryDefinitionSO category)
{
if (!gameManager.CanScore) return;
if (scoringSystem.IsCategoryUsed(category)) return;
@@ -317,17 +314,19 @@ namespace YachtDice.UI
// ── Helpers ────────────────────────────────────────────────
private void UpdatePreviewScores(int[] diceValues)
private void UpdatePreviewScores()
{
var previews = new Dictionary<YachtCategory, int>();
var categories = (YachtCategory[])Enum.GetValues(typeof(YachtCategory));
var dice = diceManager.GetDice();
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]);
previews[categories[i]] = result.FinalScore;
ScoreResult result = scoringSystem.PreviewScore(dice, cat);
previews[cat] = result.FinalScore;
}
scoreCardView.UpdatePreviews(previews);
@@ -335,29 +334,33 @@ namespace YachtDice.UI
private void UpdateTotalDisplay()
{
int upperSum = 0;
for (int i = 0; i < UpperCategories.Length; i++)
{
int catScore = scoringSystem.GetCategoryScore(UpperCategories[i]);
if (catScore >= 0) upperSum += catScore;
}
int upperSum = CalculateUpperSum();
bool hasBonus = upperSum >= UpperBonusThreshold;
int displayTotal = CalculateDisplayTotal();
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()
{
int total = scoringSystem.TotalScore;
int upperSum = 0;
for (int i = 0; i < UpperCategories.Length; i++)
{
int catScore = scoringSystem.GetCategoryScore(UpperCategories[i]);
if (catScore >= 0) upperSum += catScore;
}
int upperSum = CalculateUpperSum();
if (upperSum >= UpperBonusThreshold)
total += UpperBonusValue;
+26 -34
View File
@@ -2,13 +2,13 @@ using System;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using YachtDice.Scoring;
using YachtDice.Categories;
namespace YachtDice.UI
{
public class ScoreCardView : MonoBehaviour
{
[Header("Category Rows (in YachtCategory enum order)")]
[Header("Category Rows (порядок соответствует каталогу)")]
[SerializeField] private List<CategoryRowView> categoryRows = new();
[Header("Summary")]
@@ -16,48 +16,41 @@ namespace YachtDice.UI
[SerializeField] private TMP_Text upperBonusText;
[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()
{
_allCategories = (YachtCategory[])Enum.GetValues(typeof(YachtCategory));
for (int i = 0; i < categoryRows.Count && i < _allCategories.Length; i++)
for (int i = 0; i < count; i++)
{
categoryRows[i].Initialize(_allCategories[i], CategoryNames[i]);
categoryRows[i].Initialize(all[i]);
categoryRows[i].OnCategorySelected += HandleCategorySelected;
categoryToRowIndex[all[i]] = i;
}
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[i].SetInteractable(true);
categoryRows[rowIndex].ShowPreview(kvp.Value);
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 (index >= 0 && index < categoryRows.Count)
if (categoryToRowIndex.TryGetValue(category, out int index))
categoryRows[index].SetRecordedScore(score);
}
@@ -99,7 +91,7 @@ namespace YachtDice.UI
UpdateTotalDisplay(0, 0, false);
}
private void HandleCategorySelected(YachtCategory category)
private void HandleCategorySelected(CategoryDefinitionSO category)
{
OnCategorySelected?.Invoke(category);
}