默认弹药改为JSON文件导入 + DatabaseManager种子数据 + 测试手动插入, 129测试通过

This commit is contained in:
tian 2026-06-12 11:17:59 +08:00
parent c9c2a75328
commit 2d14c17d9c
6 changed files with 75 additions and 19 deletions

56
data/default_ammo.json Normal file
View File

@ -0,0 +1,56 @@
[
{
"Id": "default-inert",
"AerosolType": 0,
"Name": "惰性气体弹(发烟罐型)",
"InitialRadius": 3.8,
"InitialVolume": 14000.0,
"CoreDensity": 1.5,
"EdgeDensity": 0.1,
"InitialTemperature": 1800.0,
"BuoyancyFactor": 0.3,
"EffectiveConcentration": 0.0001,
"MaxRadius": 100.0,
"MaxDuration": 120.0,
"SourceStrength": 10.0,
"BurstChargeKg": 1.5,
"TurbulentExpansionK": 3.0,
"ParticlesJson": "{}"
},
{
"Id": "default-active",
"AerosolType": 1,
"Name": "活性材料弹(爆炸分散型)",
"InitialRadius": 5.0,
"InitialVolume": 30000.0,
"CoreDensity": 2.0,
"EdgeDensity": 0.2,
"InitialTemperature": 2400.0,
"BuoyancyFactor": 0.6,
"EffectiveConcentration": 0.0002,
"MaxRadius": 80.0,
"MaxDuration": 90.0,
"SourceStrength": 12.0,
"BurstChargeKg": 4.0,
"TurbulentExpansionK": 4.0,
"ParticlesJson": "{}"
},
{
"Id": "default-fuel",
"AerosolType": 2,
"Name": "活性燃料弹(抛射分散型)",
"InitialRadius": 3.8,
"InitialVolume": 14000.0,
"CoreDensity": 1.8,
"EdgeDensity": 0.15,
"InitialTemperature": 1900.0,
"BuoyancyFactor": 0.4,
"EffectiveConcentration": 0.0001,
"MaxRadius": 90.0,
"MaxDuration": 100.0,
"SourceStrength": 10.0,
"BurstChargeKg": 1.5,
"TurbulentExpansionK": 3.0,
"ParticlesJson": "{}"
}
]

View File

@ -1,4 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using CounterDrone.Core.Models;
using SQLite;
@ -14,12 +17,13 @@ namespace CounterDrone.Core
_paths = paths;
}
/// <summary>初始化主数据库(建表</summary>
/// <summary>初始化主数据库(建表 + 种子数据</summary>
public SQLiteConnection OpenMainDb()
{
var dbPath = _paths.GetMainDbPath();
var db = new SQLiteConnection(dbPath);
CreateMainTables(db);
SeedDefaultData(db);
return db;
}
@ -41,6 +45,18 @@ namespace CounterDrone.Core
File.Delete(dbPath);
}
private void SeedDefaultData(SQLiteConnection db)
{
if (db.Table<AmmunitionSpec>().Count() > 0) return;
var jsonPath = Path.Combine(_paths.GetDataRoot(), "default_ammo.json");
if (!File.Exists(jsonPath)) return;
var specs = JsonSerializer.Deserialize<List<AmmunitionSpec>>(File.ReadAllText(jsonPath));
if (specs != null)
foreach (var s in specs) db.Insert(s);
}
private void CreateMainTables(SQLiteConnection db)
{
db.CreateTable<ModelInfo>();

View File

@ -41,7 +41,7 @@ namespace CounterDrone.Core.Simulation
private List<ControlZoneEntity> _zones = new();
private CombatScene _scene = new();
private float _disperseHeight = 300f;
private float _disperseHeight;
private AmmunitionSpec _ammoSpec = new();
private int _tickRate = 20;

View File

@ -46,12 +46,6 @@ namespace CounterDrone.Unity
Debug.Log("3. Scenario configured");
var db = new DatabaseManager(paths).OpenMainDb();
db.Insert(new AmmunitionSpec
{
AerosolType = (int)AerosolType.InertGas, InitialRadius = 50,
CoreDensity = 1.0, SourceStrength = 50.0, DispersionRateBase = 3.0, MaxRadius = 500,
MaxDuration = 120, EffectiveConcentration = 0.05,
});
var ammoCatalog = db.Table<AmmunitionSpec>().ToList();
var detail = scenarioMgr.GetDetail(taskId);

View File

@ -32,14 +32,7 @@ namespace CounterDrone.Unity
var dbm = new DatabaseManager(paths);
var db = dbm.OpenMainDb();
// 弹药规格
db.Insert(new AmmunitionSpec
{
AerosolType = (int)AerosolType.InertGas, Name = "惰性气体弹",
InitialRadius = 50, CoreDensity = 1.0, SourceStrength = 50.0, DispersionRateBase = 3.0,
MaxRadius = 500, MaxDuration = 120, EffectiveConcentration = 0.05,
});
foreach (var a in DefaultAmmunition.GetAll()) db.Insert(a);
// 创建想定
var scenario = new ScenarioService(

View File

@ -29,10 +29,7 @@ namespace CounterDrone.Core.Tests
_paths = new TestPathProvider(_testDir);
var dbm = new DatabaseManager(_paths);
_mainDb = dbm.OpenMainDb();
// 使用默认弹药规格
foreach (var a in DefaultAmmunition.GetAll()) _mainDb.Insert(a);
_ammoCatalog = new List<AmmunitionSpec>(DefaultAmmunition.GetAll());
_scenario = new ScenarioService(