[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
-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.Collections.Generic;
using UnityEngine;
using YachtDice.Categories;
using YachtDice.Dice;
namespace YachtDice.Scoring
{
@@ -10,18 +13,18 @@ namespace YachtDice.Scoring
public int FlatBonus;
public float Multiplier;
public int[] DiceValues;
public YachtCategory Category;
public CategoryDefinitionSO Category;
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
{
BaseScore = baseScore,
FlatBonus = 0,
Multiplier = 1f,
DiceValues = diceValues,
DiceValues = DiceCheckUtility.ExtractValues(dice),
Category = category
};
}
+29 -22
View File
@@ -3,6 +3,8 @@ using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using UnityEngine;
using VContainer;
using YachtDice.Categories;
using YachtDice.Dice;
using YachtDice.Events;
using YachtDice.Modifiers.Core;
using YachtDice.Modifiers.Runtime;
@@ -11,26 +13,30 @@ namespace YachtDice.Scoring
{
public class ScoringSystem : MonoBehaviour
{
public event Action<YachtCategory, int> OnCategoryScored;
public event Action<CategoryDefinitionSO, int> OnCategoryScored;
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 HashSet<YachtCategory> usedCategories = new();
private readonly Dictionary<CategoryDefinitionSO, int> scorecard = new();
private readonly HashSet<CategoryDefinitionSO> usedCategories = new();
private GameEventBus eventBus;
private ModifierRegistry modifierRegistry;
private CategoryCatalogSO catalog;
[Inject]
public void Construct(GameEventBus eventBus, ModifierRegistry modifierRegistry)
public void Construct(GameEventBus eventBus, ModifierRegistry modifierRegistry, CategoryCatalogSO catalog)
{
this.eventBus = eventBus;
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;
}
@@ -47,20 +53,20 @@ namespace YachtDice.Scoring
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 ScoreResult PreviewScore(int[] diceValues, YachtCategory category,
public ScoreResult PreviewScore(IReadOnlyList<IDie> dice, CategoryDefinitionSO category,
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)
return ScoreResult.Create(baseScore, diceValues, category);
return ScoreResult.Create(baseScore, dice, category);
var context = ModifierContext.CreateForScoring(
baseScore, diceValues, category,
baseScore, dice, category,
currentRoll, currentTurn, playerCurrency,
modifierRegistry.Active);
@@ -69,19 +75,19 @@ namespace YachtDice.Scoring
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)
{
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;
if (eventBus != null && modifierRegistry != null)
{
context = ModifierContext.CreateForScoring(
baseScore, diceValues, category,
baseScore, dice, category,
currentRoll, currentTurn, playerCurrency,
modifierRegistry.Active);
@@ -92,7 +98,8 @@ namespace YachtDice.Scoring
context = new ModifierContext
{
BaseScore = baseScore,
DiceValues = diceValues,
Dice = dice,
DiceValues = DiceCheckUtility.ExtractValues(dice),
Category = category,
};
}
@@ -111,18 +118,18 @@ namespace YachtDice.Scoring
return result;
}
public ScoreResult ScoreCategory(int[] diceValues, YachtCategory category)
public ScoreResult ScoreCategory(IReadOnlyList<IDie> dice, CategoryDefinitionSO 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;
if (eventBus != null && modifierRegistry != null)
{
context = ModifierContext.CreateForScoring(
baseScore, diceValues, category,
baseScore, dice, category,
0, 0, 0,
modifierRegistry.Active);
@@ -133,7 +140,7 @@ namespace YachtDice.Scoring
if (context != null)
result = context.ToScoreResult();
else
result = ScoreResult.Create(baseScore, diceValues, category);
result = ScoreResult.Create(baseScore, dice, category);
int finalScore = result.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