Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Datra/
│ ├── Models/ # DataModelInfo, PropertyInfo 등
│ └── DataContextSourceGenerator.cs # 메인 진입점 (ISourceGenerator)
│
├── Datra.Lab/ # 밸런스 실험실 코어 (UI 없음): 손잡이 스키마, 갈래 컨텍스트, 시뮬레이터 계약, 봇 실행기
├── Datra.Lab.Sample/ # 실험실용 장난감 게임 데이터 + 시뮬레이터 (웹 샘플·테스트 공용)
├── Datra.Lab.Tests/ # 실험실 코어 테스트
├── Datra.Analyzers/ # Roslyn Analyzer
├── Datra.SampleData/ # 테스트용 샘플 데이터 모델
├── Datra.SampleData2/ # 멀티 컨텍스트 테스트용
Expand Down
31 changes: 31 additions & 0 deletions Datra.Lab.Sample/Datra.Lab.Sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<RootNamespace>Datra.Lab.Sample</RootNamespace>
</PropertyGroup>

<!-- Make project directory available to source generators -->
<ItemGroup>
<CompilerVisibleProperty Include="ProjectDir" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Datra.Generators\Datra.Generators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
<ProjectReference Include="..\Datra.Analyzers\Datra.Analyzers.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
<ProjectReference Include="..\Datra\Datra.csproj" />
<ProjectReference Include="..\Datra.Lab\Datra.Lab.csproj" />
</ItemGroup>

<!-- The data ships inside the assembly so the web sample and the tests can stage it anywhere. -->
<ItemGroup>
<EmbeddedResource Include="Resources\**\*" LogicalName="Descent/%(Filename)%(Extension)" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions Datra.Lab.Sample/DatraConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Datra.Attributes;

[assembly: DatraConfiguration("Descent",
Namespace = "Datra.Lab.Sample.Generated",
EnableLocalization = false,
EmitPhysicalFiles = false
)]
71 changes: 71 additions & 0 deletions Datra.Lab.Sample/DescentData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Datra.Lab.Sample.Generated;
using Datra.Lab.Sample.Models;

namespace Datra.Lab.Sample
{
/// <summary>
/// The toy game's data files (embedded in this assembly) and the few helpers that read
/// the loaded tables the way a game's own code would.
/// </summary>
public static class DescentData
{
private const string Prefix = "Descent/";

/// <summary>File name → YAML text, as shipped.</summary>
public static IReadOnlyDictionary<string, string> Files()
{
var assembly = typeof(DescentData).GetTypeInfo().Assembly;
var files = new Dictionary<string, string>(StringComparer.Ordinal);
foreach (var name in assembly.GetManifestResourceNames().Where(n => n.StartsWith(Prefix, StringComparison.Ordinal)))
{
using var stream = assembly.GetManifestResourceStream(name)!;
using var reader = new StreamReader(stream);
files[name.Substring(Prefix.Length)] = reader.ReadToEnd();
}
return files;
}

/// <summary>Write the data files into <paramref name="directory"/> so a file provider can serve them.</summary>
public static string WriteTo(string directory)
{
Directory.CreateDirectory(directory);
foreach (var file in Files())
File.WriteAllText(Path.Combine(directory, file.Key), file.Value);
return directory;
}

/// <summary>Floors top to bottom, in file order.</summary>
public static IReadOnlyList<FloorData> FloorsInOrder(DescentContext data) =>
data.Floor.LoadedItems.Values.ToList();

/// <summary>The factor each floor's value multiplier grows by, on average.</summary>
public static double ValueGrowth(DescentContext data)
{
var floors = FloorsInOrder(data);
if (floors.Count < 2 || floors[0].ValueMultiplier <= 0) return 1;
return Math.Pow(floors[floors.Count - 1].ValueMultiplier / floors[0].ValueMultiplier, 1.0 / (floors.Count - 1));
}

/// <summary>
/// Re-slope the value multipliers to grow by <paramref name="growth"/> per floor. Each
/// floor keeps its hand-tuned distance from the curve: only the slope changes.
/// </summary>
public static void SetValueGrowth(DescentContext data, double growth)
{
var current = ValueGrowth(data);
if (current <= 0 || growth <= 0) return;
var floors = FloorsInOrder(data);
for (var i = 0; i < floors.Count; i++)
{
var value = (float)(floors[i].ValueMultiplier * Math.Pow(growth / current, i));
ForkedData.Set(floors[i], f => f.ValueMultiplier, value);
}
}
}
}
37 changes: 37 additions & 0 deletions Datra.Lab.Sample/DescentLab.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#nullable enable
using Datra.Lab.Sample.Generated;

namespace Datra.Lab.Sample
{
/// <summary>The toy game's lab setup, shared by the web sample and the tests.</summary>
public static class DescentLab
{
/// <summary>
/// Policies, the policy parameter, the curve knob, the checks: everything the game adds
/// on top of its <c>[Knob]</c> attributes and its simulator.
/// </summary>
public static void Configure(LabOptions<DescentContext> lab)
{
lab.Title = "Descent · balance lab";
lab.Bots = 40;

lab.Policy(DescentSim.Efficient, "Efficient", "Buys what gets it down soonest, saves otherwise");
lab.Policy(DescentSim.Cheap, "Cheapest first", "Buys whatever it can afford");
lab.Policy(DescentSim.RandomPick, "Random", "Picks among the upgrades that pay back");
lab.PolicyParam("shopSeconds", "Time between runs", @default: 8, min: 2, max: 30, step: 1, unit: "s");

// One knob over a whole column: an attribute cannot say "re-slope these five rows".
lab.Knobs(knobs => knobs.Add(
"Depth value growth",
read: DescentData.ValueGrowth,
write: DescentData.SetValueGrowth,
min: 1.4, max: 3, step: 0.05,
group: "Floors", unit: "×/floor",
hint: "How much more each floor pays than the one above",
id: "Floors.ValueGrowth"));

lab.Check("First purchase", bot => bot.FirstPurchaseSeconds, "<= 60", unit: "s");
lab.Check("Longest run", bot => bot.LongestTickSeconds("run"), "<= 90", unit: "s");
}
}
}
102 changes: 102 additions & 0 deletions Datra.Lab.Sample/DescentPhysics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using Datra.Lab.Sample.Generated;
using Datra.Lab.Sample.Models;

namespace Datra.Lab.Sample
{
/// <summary>What happened to one bottle on the stairs: the unit the physics cache stores.</summary>
public sealed class BottleOutcome
{
/// <summary>Steps the bottle survived.</summary>
public int Steps { get; set; }

/// <summary>It reached the bottom in one piece.</summary>
public bool Intact { get; set; }

/// <summary>It came to rest on the landing.</summary>
public bool StoppedOnLanding { get; set; }

/// <summary>How long the bottle was in play.</summary>
public double Seconds { get; set; }
}

/// <summary>
/// Stands in for a game's headless physics batch. A real game runs its engine overnight and
/// stores the outcomes; this fills the same kind of pools from a few lines of arithmetic so
/// the sample has something to draw from.
/// </summary>
public static class DescentPhysics
{
public const string Version = "toy-1";
public const int SamplesPerKey = 64;

/// <summary>The physics state a bottle is rolled in. Only inputs that change the outcome belong here.</summary>
public static PhysicsKey Key(string floor, int push, int wrap) =>
PhysicsKey.Of(("floor", floor), ("push", push), ("wrap", wrap));

/// <summary>Roll every state a bot can pass through and keep the outcomes.</summary>
public static InMemoryOutcomes Bake(DescentContext data)
{
var outcomes = new InMemoryOutcomes(Version);
var economy = data.Economy.Current ?? new EconomyData();
var upgrades = data.Upgrade.LoadedItems;
var push = upgrades["push"];
var wrap = upgrades["wrap"];
var floors = DescentData.FloorsInOrder(data);

for (var f = 0; f < floors.Count; f++)
{
for (var p = 0; p <= push.MaxLevel; p++)
{
for (var w = 0; w <= wrap.MaxLevel; w++)
{
// One fixed seed per state: baking twice gives the same pools.
var random = new SimRandom(1_000_003L * (f + 1) + 1_009L * p + w);
var pool = Enumerable.Range(0, SamplesPerKey)
.Select(_ => Roll(floors[f], p * push.Amount, w * wrap.Amount, economy.GlassToughness, random))
.ToList();
outcomes.Add(Key(floors[f].Id, p, w), pool);
}
}
}
return outcomes;
}

private static BottleOutcome Roll(FloorData floor, double reach, double padding, double toughness, SimRandom random)
{
// Each step is a chance to break. Pushing harder carries the bottle further before
// it slows down; wrap and tougher glass make every step kinder.
var breakChance = 0.20 / (toughness * (1 + padding));
var carry = 3.0 + reach * 4.0;
var steps = 0;
var stopped = false;

while (steps < floor.Steps)
{
if (random.Chance(breakChance)) break;
steps++;
if (steps == floor.LandingStep && random.Chance(0.55))
{
stopped = true;
break;
}
if (steps > carry && random.Chance(0.28))
{
stopped = true;
break;
}
}

return new BottleOutcome
{
Steps = steps,
Intact = steps >= floor.Steps,
StoppedOnLanding = stopped && steps == floor.LandingStep && floor.LandingStep > 0,
Seconds = 3.0 + steps * 0.8,
};
}
}
}
Loading
Loading