From 25cbe55e42e327dc92a2deae540e36e1407f0f1c Mon Sep 17 00:00:00 2001 From: milimoe Date: Tue, 10 Nov 2026 00:21:16 +0800 Subject: [PATCH] feat(S8-P1-core): add gameplay rules, transactional player store and API key identity binding --- OshimaBot.Tests/CommandFrameworkTests.cs | 2 +- OshimaBot.Tests/GameplayTests.cs | 308 +++++++++ OshimaBot/Commands/CommandModels.cs | 2 + OshimaBot/Gameplay/GameplayModels.cs | 34 + OshimaBot/Gameplay/GameplayRules.cs | 124 ++++ OshimaBot/Gameplay/PlayerService.cs | 774 ++++++++++++++++++++++ OshimaBot/Program.cs | 3 + OshimaBot/Security/ApiKeyAuthenticator.cs | 33 +- 8 files changed, 1275 insertions(+), 5 deletions(-) create mode 100644 OshimaBot.Tests/GameplayTests.cs create mode 100644 OshimaBot/Gameplay/GameplayModels.cs create mode 100644 OshimaBot/Gameplay/GameplayRules.cs create mode 100644 OshimaBot/Gameplay/PlayerService.cs diff --git a/OshimaBot.Tests/CommandFrameworkTests.cs b/OshimaBot.Tests/CommandFrameworkTests.cs index 6abbd2b..6d95262 100644 --- a/OshimaBot.Tests/CommandFrameworkTests.cs +++ b/OshimaBot.Tests/CommandFrameworkTests.cs @@ -229,7 +229,7 @@ public sealed class CommandFrameworkTests { sessions = new FakeSessions(); codes = new FakeBindCodes(); - return new CommandDependencies(null!, null!, codes, sessions, NullLogger.Instance); + return new CommandDependencies(null!, null!, codes, sessions, null!, NullLogger.Instance); } private static (CommandRouter Router, CommandRegistry Registry, FakeSessions Sessions, FakeBindCodes Codes) diff --git a/OshimaBot.Tests/GameplayTests.cs b/OshimaBot.Tests/GameplayTests.cs new file mode 100644 index 0000000..85e3e4d --- /dev/null +++ b/OshimaBot.Tests/GameplayTests.cs @@ -0,0 +1,308 @@ +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using OshimaBot.Data; +using OshimaBot.Data.Entities; +using OshimaBot.Game.Services; +using OshimaBot.Gameplay; +using OshimaBot.Models; +using OshimaBot.Security; +using Xunit; + +namespace OshimaBot.Tests; + +/// P1 玩法仓储:真实 SQLite 内存库,验证事务、ledger 对账与 @编号 生命周期。 +public sealed class GameplayTests +{ + private const long UserId = 42; + + [Fact] + public async Task Ensure_CreatesInitialStateOnlyOnce() + { + using GameplayDatabase db = new(); + IPlayerService players = db.Get(); + + PlayerSnapshot? first = await players.GetSnapshotAsync(UserId); + PlayerSnapshot? second = await players.GetSnapshotAsync(UserId); + + Assert.NotNull(first); + Assert.Equal(GameplayRules.StartingCredits, first.Credits); + Assert.Equal(GameplayRules.StartingDiamonds, first.Diamonds); + Assert.Equal(1, first.CharacterCount); + Assert.Equal(GameplayRules.StartingItems, first.ItemCount); + Assert.Equal(second!.Credits, first.Credits); + + using IServiceScope scope = db.Provider.CreateScope(); + BotDbContext context = scope.ServiceProvider.GetRequiredService(); + Assert.Equal(1, await context.PlayerStates.CountAsync()); + Assert.Equal(1, await context.LedgerEntries.CountAsync(x => x.Reason == "register")); + } + + [Fact] + public async Task SignIn_IsIdempotentAndWritesLedger() + { + using GameplayDatabase db = new(); + IPlayerService players = db.Get(); + + ActionResult first = await players.SignInAsync(UserId); + ActionResult second = await players.SignInAsync(UserId); + + Assert.True(first.Ok); + Assert.False(second.Ok); + Assert.Contains("已经签到", second.Title); + + using IServiceScope scope = db.Provider.CreateScope(); + BotDbContext context = scope.ServiceProvider.GetRequiredService(); + PlayerState state = await context.PlayerStates.FirstAsync(x => x.UserId == UserId); + LedgerEntry entry = await context.LedgerEntries.FirstAsync(x => x.Reason == "signin"); + Assert.Equal(state.Credits, entry.BalanceAfter); + Assert.Equal(GameplayRules.SignInCredits(0), entry.Delta); + Assert.Equal(1, await context.SignInRecords.CountAsync()); + Assert.Equal(1, await context.LedgerEntries.CountAsync(x => x.Reason == "signin")); + } + + [Fact] + public async Task Gacha_WithInsufficientDiamondsRollsBackEverything() + { + using GameplayDatabase db = new(); + IPlayerService players = db.Get(); + await players.GetSnapshotAsync(UserId); + await SetDiamondsAsync(db, 0); + + ActionResult result = await players.GachaAsync(UserId, 10); + + Assert.False(result.Ok); + using IServiceScope scope = db.Provider.CreateScope(); + BotDbContext context = scope.ServiceProvider.GetRequiredService(); + Assert.Equal(0, (await context.PlayerStates.FirstAsync(x => x.UserId == UserId)).Diamonds); + Assert.Empty(await context.LedgerEntries.Where(x => x.Reason == "gacha").ToListAsync()); + Assert.Equal(GameplayRules.StartingItems, await context.ItemInstances.CountAsync(x => x.UserId == UserId)); + } + + [Fact] + public async Task Gacha_TenPullDebitsDiscountAndKeepsLedgerBalanced() + { + using GameplayDatabase db = new(); + IPlayerService players = db.Get(); + await players.GetSnapshotAsync(UserId); + await SetDiamondsAsync(db, 100); + + ActionResult result = await players.GachaAsync(UserId, 10); + + Assert.True(result.Ok, result.Title); + using IServiceScope scope = db.Provider.CreateScope(); + BotDbContext context = scope.ServiceProvider.GetRequiredService(); + PlayerState state = await context.PlayerStates.FirstAsync(x => x.UserId == UserId); + Assert.Equal(100 - GameplayRules.GachaTenCost, state.Diamonds); + LedgerEntry entry = await context.LedgerEntries.FirstAsync(x => x.Reason == "gacha"); + Assert.Equal(-GameplayRules.GachaTenCost, entry.Delta); + Assert.Equal(state.Diamonds, entry.BalanceAfter); + Assert.True(await context.ItemInstances.CountAsync(x => x.UserId == UserId) > GameplayRules.StartingItems); + Assert.Contains(await context.ItemInstances.Where(x => x.CatalogId == "gacha").ToListAsync(), + item => item.Quality >= 4); // 十连保底 + } + + [Fact] + public async Task Bag_LockSalvageAndEquipLifecycle() + { + using GameplayDatabase db = new(); + IPlayerService players = db.Get(); + + IReadOnlyList bag = await players.GetBagAsync(UserId); + Assert.Equal(GameplayRules.StartingItems, bag.Count); + Assert.Equal(1, bag[0].Index); + + Assert.True((await players.SetLockAsync(UserId, 1, true)).Ok); + Assert.False((await players.SalvageAsync(UserId, 1, 1)).Ok); + Assert.True((await players.SetLockAsync(UserId, 1, false)).Ok); + + PlayerSnapshot before = (await players.GetSnapshotAsync(UserId))!; + ActionResult salvage = await players.SalvageAsync(UserId, 1, 1); + Assert.True(salvage.Ok, salvage.Title); + PlayerSnapshot after = (await players.GetSnapshotAsync(UserId))!; + Assert.Equal(before.Credits + GameplayRules.SalvageValue(1, 1), after.Credits); + + using IServiceScope scope = db.Provider.CreateScope(); + BotDbContext context = scope.ServiceProvider.GetRequiredService(); + LedgerEntry entry = await context.LedgerEntries.FirstAsync(x => x.Reason == "salvage"); + Assert.Equal(after.Credits, entry.BalanceAfter); + + Assert.True((await players.EquipAsync(UserId, 2)).Ok); + Assert.True((await players.GetBagAsync(UserId))[1].Equipped); + Assert.False((await players.EquipAsync(UserId, 2)).Ok); // 已绑定,不能重复装备 + Assert.False((await players.SalvageAsync(UserId, 2, 1)).Ok); // 绑定物品不可分解 + Assert.True((await players.UnequipAsync(UserId, "weapon")).Ok); + Assert.False((await players.GetBagAsync(UserId))[1].Equipped); + Assert.True((await players.SalvageAsync(UserId, 2, 1)).Ok); + } + + [Fact] + public async Task Train_RequiresExpiryThenGrantsExpForLevelUp() + { + using GameplayDatabase db = new(); + IPlayerService players = db.Get(); + + Assert.True((await players.TrainStartAsync(UserId, 1, 1)).Ok); + Assert.False((await players.TrainClaimAsync(UserId)).Ok); // 尚未结束 + Assert.False((await players.TrainStartAsync(UserId, 1, 1)).Ok); // 不能重复开练 + + using (IServiceScope scope = db.Provider.CreateScope()) + { + BotDbContext context = scope.ServiceProvider.GetRequiredService(); + TrainingSession session = await context.TrainingSessions.FirstAsync(x => x.UserId == UserId); + session.EndsAtUtc = DateTime.UtcNow.AddMinutes(-1); + await context.SaveChangesAsync(); + } + + Assert.True((await players.TrainClaimAsync(UserId)).Ok); + PlayerSnapshot snapshot = (await players.GetSnapshotAsync(UserId))!; + Assert.Equal(GameplayRules.TrainExpBase + GameplayRules.TrainExpPerLevel, snapshot.Exp); + + Assert.True((await players.LevelUpAsync(UserId, 1)).Ok); + IReadOnlyList characters = await players.GetCharactersAsync(UserId); + Assert.Equal(2, Assert.Single(characters).Level); + } + + [Fact] + public async Task Squad_MainAndQuestRewardsArePersistent() + { + using GameplayDatabase db = new(); + IPlayerService players = db.Get(); + + Assert.True((await players.SquadAsync(UserId, "", 0)).Ok); + Assert.True((await players.SquadAsync(UserId, "add", 1)).Ok); + Assert.False((await players.SquadAsync(UserId, "add", 1)).Ok); + Assert.True((await players.SquadAsync(UserId, "remove", 1)).Ok); + Assert.Equal(0, (await players.GetSnapshotAsync(UserId))!.SquadCount); + + Assert.True((await players.SignInAsync(UserId)).Ok); + IReadOnlyList quests = await players.GetQuestsAsync(UserId); + Assert.Equal(1, quests[0].Status); + Assert.Equal(0, quests[1].Status); + + PlayerSnapshot before = (await players.GetSnapshotAsync(UserId))!; + Assert.True((await players.ClaimQuestAsync(UserId, 1)).Ok); + Assert.False((await players.ClaimQuestAsync(UserId, 1)).Ok); // 不能重复领 + Assert.False((await players.ClaimQuestAsync(UserId, 2)).Ok); // 未完成 + PlayerSnapshot after = (await players.GetSnapshotAsync(UserId))!; + Assert.Equal(before.Credits + GameplayRules.QuestRewardCredits, after.Credits); + Assert.Equal(before.Diamonds + GameplayRules.QuestRewardDiamonds, after.Diamonds); + Assert.Equal(2, (await players.GetQuestsAsync(UserId))[0].Status); + } + + [Fact] + public async Task SimulationProgress_AdvancesQuest() + { + using GameplayDatabase db = new(); + IPlayerService players = db.Get(); + + Assert.True((await players.RecordSimulationAsync(UserId)).Ok); + Assert.Equal(1, (await players.GetQuestsAsync(UserId))[1].Status); + Assert.True((await players.ClaimQuestAsync(UserId, 2)).Ok); + } + + [Fact] + public async Task NoIdentity_NeverWritesPlayerState() + { + using GameplayDatabase db = new(); + IPlayerService players = db.Get(); + + Assert.False((await players.SignInAsync(0)).Ok); + Assert.False((await players.GachaAsync(0, 1)).Ok); + Assert.Null(await players.GetSnapshotAsync(0)); + Assert.Empty(await players.GetBagAsync(0)); + + using IServiceScope scope = db.Provider.CreateScope(); + BotDbContext context = scope.ServiceProvider.GetRequiredService(); + Assert.Equal(0, await context.PlayerStates.CountAsync()); + Assert.Equal(0, await context.LedgerEntries.CountAsync()); + } + + [Fact] + public async Task ApiKey_LazilyBindsIndependentInternalUsers() + { + using GameplayDatabase db = new(); + using (IServiceScope scope = db.Provider.CreateScope()) + { + BotDbContext context = scope.ServiceProvider.GetRequiredService(); + context.ApiKeys.AddRange( + new ApiKey { Name = "a", KeyHash = ApiKeyDefaults.Hash("key-a"), ScopesJson = "[]" }, + new ApiKey { Name = "b", KeyHash = ApiKeyDefaults.Hash("key-b"), ScopesJson = "[]" }); + await context.SaveChangesAsync(); + } + + ApiKeyAuthenticator authenticator = db.Get(); + ApiKeyIdentity? a1 = await authenticator.AuthenticateAsync("key-a"); + ApiKeyIdentity? b1 = await authenticator.AuthenticateAsync("key-b"); + ApiKeyIdentity? a2 = await authenticator.AuthenticateAsync("key-a"); + + Assert.NotNull(a1); + Assert.NotNull(b1); + Assert.NotNull(a2); + Assert.True(a1.UserId > 0); + Assert.True(b1.UserId > 0); + Assert.NotEqual(a1.UserId, b1.UserId); + Assert.Equal(a1.UserId, a2.UserId); + Assert.Equal(BotRole.Player, Assert.Single(a1.Roles)); + + using IServiceScope verifyScope = db.Provider.CreateScope(); + BotDbContext verifyContext = verifyScope.ServiceProvider.GetRequiredService(); + Assert.Equal(2, await verifyContext.Users.CountAsync()); + Assert.Equal(2, await verifyContext.UserIdentities.CountAsync(x => x.Provider == "apikey")); + Assert.Equal(0, await verifyContext.ApiKeys.CountAsync(x => x.UserId == null)); + } + + [Fact] + public void Rules_AreDeterministicAndBounded() + { + Assert.Equal(GameplayRules.Fortune(7, "2026-11-10"), GameplayRules.Fortune(7, "2026-11-10")); + Assert.Contains(GameplayRules.Fortune(8, "2026-11-10"), GameplayRules.FortuneTexts); + Assert.Equal(10, GameplayRules.LevelCap(0)); + Assert.Equal(20, GameplayRules.LevelCap(1)); + Assert.True(GameplayRules.ExpForLevel(10) > GameplayRules.ExpForLevel(1)); + Assert.InRange(GameplayRules.RollQuality(new Random(1)), 1, 5); + } + + private static async Task SetDiamondsAsync(GameplayDatabase db, int diamonds) + { + using IServiceScope scope = db.Provider.CreateScope(); + BotDbContext context = scope.ServiceProvider.GetRequiredService(); + PlayerState state = await context.PlayerStates.FirstAsync(x => x.UserId == UserId); + state.Diamonds = diamonds; + await context.SaveChangesAsync(); + } + + private sealed class GameplayDatabase : IDisposable + { + public SqliteConnection Connection { get; } + public ServiceProvider Provider { get; } + + public GameplayDatabase() + { + FunGameRuntime.EnsureInitialized(); + Connection = new SqliteConnection("Data Source=:memory:"); + Connection.Open(); + ServiceCollection services = new(); + services.AddLogging(); + services.AddDbContext(options => options.UseSqlite(Connection)); + services.AddSingleton(Options.Create(new ServerOptions())); + services.AddSingleton(Options.Create(new DatabaseOptions { ApplyMigrationsOnStartup = false })); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + Provider = services.BuildServiceProvider(); + using IServiceScope scope = Provider.CreateScope(); + scope.ServiceProvider.GetRequiredService().Database.EnsureCreated(); + } + + public T Get() where T : notnull => Provider.GetRequiredService(); + + public void Dispose() + { + Provider.Dispose(); + Connection.Dispose(); + } + } +} diff --git a/OshimaBot/Commands/CommandModels.cs b/OshimaBot/Commands/CommandModels.cs index a8b88eb..d444ae2 100644 --- a/OshimaBot/Commands/CommandModels.cs +++ b/OshimaBot/Commands/CommandModels.cs @@ -1,4 +1,5 @@ using System.Security.Claims; +using OshimaBot.Gameplay; using OshimaBot.Messaging; using OshimaBot.Models; using OshimaBot.Security; @@ -21,6 +22,7 @@ public sealed record CommandDependencies( FunGameSimulationService Simulations, IBindCodeService BindCodes, IUserSessionService Sessions, + IPlayerService Players, ILogger Logger); public interface ICommand diff --git a/OshimaBot/Gameplay/GameplayModels.cs b/OshimaBot/Gameplay/GameplayModels.cs new file mode 100644 index 0000000..40722b3 --- /dev/null +++ b/OshimaBot/Gameplay/GameplayModels.cs @@ -0,0 +1,34 @@ +namespace OshimaBot.Gameplay; + +/// 玩法层与指令层之间的纯数据契约,玩法层不依赖 Messaging/Models。 +public sealed record PlayerSnapshot( + long UserId, int Level, long Exp, long Credits, long Diamonds, + int SignInStreak, string SignedOn, string? MainCharacter, + int CharacterCount, int ItemCount, int SquadCount, int PendingTrainings); + +public sealed record BagEntry( + int Index, long Id, string Name, string CatalogId, int Quantity, + int Quality, bool Locked, bool Bound, bool Equipped); + +public sealed record CharacterEntry( + int Index, long Id, string Name, int Level, long Exp, + int BreakLevel, bool IsMain, int EquipmentCount); + +public sealed record QuestEntry(int Index, string Key, string Name, int Status, int Progress, int Target); + +/// +/// 统一的玩法结果。Ok=true 时按 4 段式渲染(Title / Status / Lines / Actions); +/// Ok=false 时按错误三段式渲染(发生了什么 / 没有发生什么 / 下一步)。 +/// +public sealed record ActionResult( + bool Ok, string Title, string Status, IReadOnlyList Lines, IReadOnlyList Actions) +{ + public static ActionResult Success( + string title, string status, + IEnumerable? lines = null, IEnumerable? actions = null) + => new(true, title, status, + lines?.ToArray() ?? [], actions?.ToArray() ?? ["/状态"]); + + public static ActionResult Fail(string what, string notHappened, string next, string? action = null) + => new(false, what, notHappened, [next], action is null ? ["/帮助"] : [action]); +} diff --git a/OshimaBot/Gameplay/GameplayRules.cs b/OshimaBot/Gameplay/GameplayRules.cs new file mode 100644 index 0000000..96e13ff --- /dev/null +++ b/OshimaBot/Gameplay/GameplayRules.cs @@ -0,0 +1,124 @@ +using System.Globalization; +using System.Security.Cryptography; +using System.Text; + +namespace OshimaBot.Gameplay; + +/// +/// P1 玩法数值与文案的默认规则。集中在一处便于调参,业务层不硬编码数字。 +/// 用户未指定数值时按本文件默认值实现(计划 §5.4)。 +/// +public static class GameplayRules +{ + public const long StartingCredits = 1200; + public const int StartingDiamonds = 30; + public const int StartingItems = 3; + + public const int GachaCost = 5; + public const int GachaTenCost = 45; + public const int MaxGachaPerCall = 10; + + public const int SignInBaseCredits = 100; + public const int SignInStreakBonus = 20; + public const int SignInStreakCapDays = 7; + public const int SignInDiamondEveryDays = 7; + public const int SignInDiamondAmount = 5; + + public const int LevelsPerBreak = 10; + public const int BreakCreditsBase = 500; + public const int SalvageValueBase = 20; + public const int UseExpBase = 30; + + public const int TrainCostCredits = 50; + public const int TrainDefaultMinutes = 10; + public const int TrainMaxMinutes = 120; + public const int TrainExpBase = 120; + public const int TrainExpPerLevel = 20; + + public const int QuestRewardCredits = 80; + public const int QuestRewardDiamonds = 2; + + /// 装备槽位,装备时按序占用第一个空槽。 + public static readonly IReadOnlyList EquipmentSlots = ["weapon", "armor", "trinket"]; + + public static readonly IReadOnlyList FortuneTexts = + [ + "大吉 · 今天适合孤身深入,意料之外的收获正在等你。", + "中吉 · 稳扎稳打,旧线索会连成一条路。", + "小吉 · 有人愿意搭把手,别拒绝。", + "平 · 雾很厚,但路还在脚下。", + "小凶 · 别急着做决定,先看看背包。", + "中凶 · 今天不宜逞强,休整也是一种前进。", + "大凶 · 收好贵重物品,等雾散了再出门。" + ]; + + public const string DefaultNotice = + "欢迎来到铎京。旧存档未迁移(MySQL 数据已丢失),所有旅人从今天重新开始;" + + "每日签到、抽卡、练级与背包已开放,商店与交易将在后续开放。"; + + /// 每日任务定义:key → 名称 / 目标次数。进度写在 daily_quests.progress_json。 + public static readonly IReadOnlyList<(string Key, string Name, int Target)> DailyQuests = + [ + ("signin", "今日签到", 1), + ("simulate", "完成一次战斗模拟", 1), + ("gacha", "进行一次抽卡", 1) + ]; + + private static TimeZoneInfo GameTimeZone + => TimeZoneInfo.TryFindSystemTimeZoneById("Asia/Shanghai", out TimeZoneInfo? iana) + ? iana + : TimeZoneInfo.FindSystemTimeZoneById("China Standard Time"); + + /// 游戏日(Asia/Shanghai),签到/任务/运势全部以它为准,避免 UTC 跨日。 + public static string Today() + => DateOnly.FromDateTime(TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, GameTimeZone)) + .ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); + + public static long ExpForLevel(int level) + => (long)Math.Ceiling(100 * Math.Pow(Math.Max(1, level), 1.5)); + + public static int LevelCap(int breakLevel) => LevelsPerBreak * (breakLevel + 1); + + public static long SalvageValue(int quality, int quantity) + => SalvageValueBase * (long)Math.Max(1, quality) * Math.Max(1, quality) * Math.Max(1, quantity); + + public static int UseExp(int quality, int quantity) + => UseExpBase * Math.Max(1, quality) * Math.Max(1, quantity); + + public static long BreakCost(int breakLevel) => BreakCreditsBase * (breakLevel + 1L); + + public static long SignInCredits(int streak) + => SignInBaseCredits + SignInStreakBonus * Math.Min(Math.Max(0, streak), SignInStreakCapDays); + + /// 品质权重:普通 60 / 精良 25 / 稀有 10 / 史诗 4 / 传说 1。 + public static int RollQuality(Random random) + { + int roll = random.Next(100); + return roll switch + { + < 60 => 1, + < 85 => 2, + < 95 => 3, + < 99 => 4, + _ => 5 + }; + } + + public static string QualityName(int quality) => quality switch + { + 1 => "普通", + 2 => "精良", + 3 => "稀有", + 4 => "史诗", + _ => "传说" + }; + + /// 同一用户同一天的运势稳定不变(服务端确定性哈希,不用 Random.Shared)。 + public static string Fortune(long userId, string date) + { + byte[] hash = SHA256.HashData(Encoding.UTF8.GetBytes($"{userId}:{date}")); + return FortuneTexts[hash[0] % FortuneTexts.Count]; + } + + public static string CatalogVersion => "core-v3"; +} diff --git a/OshimaBot/Gameplay/PlayerService.cs b/OshimaBot/Gameplay/PlayerService.cs new file mode 100644 index 0000000..250fca5 --- /dev/null +++ b/OshimaBot/Gameplay/PlayerService.cs @@ -0,0 +1,774 @@ +using System.Globalization; +using System.Text.Json; +using Microsoft.EntityFrameworkCore; +using OshimaBot.Data; +using OshimaBot.Data.Entities; +using OshimaBot.Game.Services; +using DataCharacter = OshimaBot.Data.Entities.Character; + +namespace OshimaBot.Gameplay; + +public interface IPlayerService +{ + Task GetSnapshotAsync(long userId, CancellationToken cancellationToken = default); + Task RegisterAsync(long userId, CancellationToken cancellationToken = default); + Task SignInAsync(long userId, CancellationToken cancellationToken = default); + Task GachaAsync(long userId, int count, CancellationToken cancellationToken = default); + Task> GetBagAsync(long userId, CancellationToken cancellationToken = default); + Task UseAsync(long userId, int index, int quantity, CancellationToken cancellationToken = default); + Task SalvageAsync(long userId, int index, int quantity, CancellationToken cancellationToken = default); + Task SetLockAsync(long userId, int index, bool locked, CancellationToken cancellationToken = default); + Task EquipAsync(long userId, int index, CancellationToken cancellationToken = default); + Task UnequipAsync(long userId, string slot, CancellationToken cancellationToken = default); + Task> GetCharactersAsync(long userId, CancellationToken cancellationToken = default); + Task SetMainAsync(long userId, int index, CancellationToken cancellationToken = default); + Task LevelUpAsync(long userId, int index, CancellationToken cancellationToken = default); + Task BreakAsync(long userId, int index, CancellationToken cancellationToken = default); + Task TrainStartAsync(long userId, int index, int minutes, CancellationToken cancellationToken = default); + Task TrainClaimAsync(long userId, CancellationToken cancellationToken = default); + Task SquadAsync(long userId, string operation, int index, CancellationToken cancellationToken = default); + Task> GetQuestsAsync(long userId, CancellationToken cancellationToken = default); + Task ClaimQuestAsync(long userId, int index, CancellationToken cancellationToken = default); + Task RecordSimulationAsync(long userId, CancellationToken cancellationToken = default); + Task GetNoticeAsync(CancellationToken cancellationToken = default); + Task GetFortuneAsync(long userId, CancellationToken cancellationToken = default); +} + +/// +/// P1 玩法仓储:所有「改货币 + 改物品 + 写 ledger」在一个 SQLite 事务内完成。 +/// singleton(与 CommandCatalog 生命周期一致),每次操作自己开 scope;userId<=0 一律拒绝。 +/// +public sealed class PlayerService(IServiceScopeFactory scopeFactory, ILogger logger) : IPlayerService +{ + private static ActionResult NoIdentity => ActionResult.Fail( + "身份尚未就绪", "没有读写任何存档", "请通过 QQ 消息触发注册,或稍后重试", "/状态"); + + // ---------------- 读 ---------------- + + private async Task ReadAsync( + Func> body, CancellationToken cancellationToken) + { + using IServiceScope scope = scopeFactory.CreateScope(); + BotDbContext db = scope.ServiceProvider.GetRequiredService(); + return await body(db, cancellationToken); + } + + public Task GetSnapshotAsync(long userId, CancellationToken cancellationToken = default) + => userId <= 0 ? Task.FromResult(null) : ReadAsync(async (db, ct) => + { + PlayerState state = await EnsureAsync(db, userId, ct); + List characters = await db.Characters.AsNoTracking() + .Where(x => x.UserId == userId).OrderBy(x => x.Id).ToListAsync(ct); + int itemCount = await db.ItemInstances.AsNoTracking().CountAsync(x => x.UserId == userId, ct); + List squadIds = await db.Squads.AsNoTracking() + .Where(x => x.UserId == userId).Select(x => x.Id).ToListAsync(ct); + int squadCount = squadIds.Count == 0 ? 0 : await db.SquadMembers.AsNoTracking() + .CountAsync(x => squadIds.Contains(x.SquadId), ct); + int pendingTrainings = await db.TrainingSessions.AsNoTracking() + .CountAsync(x => x.UserId == userId && x.SettledAtUtc == null, ct); + return new PlayerSnapshot(userId, state.Level, state.Exp, state.Credits, state.Diamonds, + state.SignInStreak, state.SignedOn, characters.FirstOrDefault(x => x.IsMain)?.Name, + characters.Count, itemCount, squadCount, pendingTrainings); + }, cancellationToken); + + public Task> GetBagAsync(long userId, CancellationToken cancellationToken = default) + => userId <= 0 ? Task.FromResult>([]) : ReadAsync(async (db, ct) => + { + await EnsureAsync(db, userId, ct); + List items = await db.ItemInstances.AsNoTracking() + .Where(x => x.UserId == userId).OrderBy(x => x.Id).ToListAsync(ct); + List ids = items.Select(x => x.Id).ToList(); + HashSet equipped = (await db.CharacterEquipment.AsNoTracking() + .Where(x => ids.Contains(x.ItemInstanceId)) + .Select(x => x.ItemInstanceId).ToListAsync(ct)).ToHashSet(); + return (IReadOnlyList)items.Select((item, i) => new BagEntry( + i + 1, item.Id, ReadName(item), item.CatalogId, item.Quantity, + item.Quality, item.Locked, item.Bound, equipped.Contains(item.Id))).ToArray(); + }, cancellationToken); + + public Task> GetCharactersAsync(long userId, CancellationToken cancellationToken = default) + => userId <= 0 ? Task.FromResult>([]) : ReadAsync(async (db, ct) => + { + await EnsureAsync(db, userId, ct); + List characters = await db.Characters.AsNoTracking() + .Where(x => x.UserId == userId).OrderBy(x => x.Id).ToListAsync(ct); + Dictionary equipment = await db.CharacterEquipment.AsNoTracking() + .GroupBy(x => x.CharacterId).Select(g => new { g.Key, Count = g.Count() }) + .ToDictionaryAsync(x => x.Key, x => x.Count, ct); + return (IReadOnlyList)characters.Select((c, i) => new CharacterEntry( + i + 1, c.Id, c.Name, c.Level, c.Exp, c.BreakLevel, c.IsMain, + equipment.GetValueOrDefault(c.Id))).ToArray(); + }, cancellationToken); + + public async Task> GetQuestsAsync(long userId, CancellationToken cancellationToken = default) + { + if (userId <= 0) return []; + string date = GameplayRules.Today(); + return await ReadAsync(async (db, ct) => + { + Dictionary rows = await db.DailyQuests.AsNoTracking() + .Where(x => x.UserId == userId && x.QuestDate == date) + .ToDictionaryAsync(x => x.QuestKey, ct); + List result = []; + for (int i = 0; i < GameplayRules.DailyQuests.Count; i++) + { + (string key, string name, int target) = GameplayRules.DailyQuests[i]; + rows.TryGetValue(key, out DailyQuest? row); + result.Add(new QuestEntry(i + 1, key, name, row?.Status ?? 0, + row is null ? 0 : ReadProgress(row), target)); + } + return (IReadOnlyList)result; + }, cancellationToken); + } + + public Task GetNoticeAsync(CancellationToken cancellationToken = default) + => ReadAsync(async (db, ct) => + { + AppConfig? config = await db.AppConfigs.AsNoTracking() + .FirstOrDefaultAsync(x => x.Key == "notice", ct); + if (config is null) return GameplayRules.DefaultNotice; + try + { + using JsonDocument document = JsonDocument.Parse(config.ValueJson); + return document.RootElement.TryGetProperty("text", out JsonElement text) + ? text.GetString() ?? GameplayRules.DefaultNotice + : GameplayRules.DefaultNotice; + } + catch (JsonException) { return GameplayRules.DefaultNotice; } + }, cancellationToken); + + public Task GetFortuneAsync(long userId, CancellationToken cancellationToken = default) + => Task.FromResult(userId <= 0 + ? "身份尚未就绪,无法占卜。" + : GameplayRules.Fortune(userId, GameplayRules.Today())); + + // ---------------- 写(事务) ---------------- + + private async Task MutateAsync( + long userId, + Func> body, + CancellationToken cancellationToken) + { + if (userId <= 0) return NoIdentity; + try + { + using IServiceScope scope = scopeFactory.CreateScope(); + BotDbContext db = scope.ServiceProvider.GetRequiredService(); + await using Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction transaction = + await db.Database.BeginTransactionAsync(cancellationToken); + + PlayerState state = await EnsureAsync(db, userId, cancellationToken); + ActionResult result = await body(db, state, cancellationToken); + if (!result.Ok) + { + await transaction.RollbackAsync(cancellationToken); + return result; + } + state.UpdatedAtUtc = DateTime.UtcNow; + await db.SaveChangesAsync(cancellationToken); + await transaction.CommitAsync(cancellationToken); + return result; + } + catch (DbUpdateConcurrencyException) + { + return ActionResult.Fail("存档正被另一条消息修改", "本次操作已回滚", "稍等一秒后重试", "/状态"); + } + catch (DbUpdateException exception) + { + logger.LogWarning(exception, "玩法写入冲突:UserId={UserId}", userId); + return ActionResult.Fail("存档写入冲突", "本次操作已回滚", "稍后重试", "/状态"); + } + catch (Exception exception) + { + logger.LogError(exception, "玩法操作失败:UserId={UserId}", userId); + return ActionResult.Fail("玩法操作出现异常", "本次操作已回滚", "稍后重试,或输入 /帮助", "/帮助"); + } + } + + public Task RegisterAsync(long userId, CancellationToken cancellationToken = default) + => userId <= 0 ? Task.FromResult(NoIdentity) : ReadAsync(async (db, ct) => + { + // 读路径自己判断是否为首次建档,避免 EnsureAsync 已建档后误报「存档已存在」。 + bool existed = await db.PlayerStates.AsNoTracking().AnyAsync(x => x.UserId == userId, ct); + PlayerState state = await EnsureAsync(db, userId, ct); + return ActionResult.Success("🎒 旅人登记", + existed ? "存档已存在,未重复发放初始资源" : "初始资源已发放", + [ + $"- 旅人等级:Lv.{state.Level} 经验:{state.Exp}", + $"- 金币:{state.Credits} 钻石:{state.Diamonds}", + $"- 角色:{await db.Characters.CountAsync(x => x.UserId == userId, ct)} 名", + $"- 物品:{await db.ItemInstances.CountAsync(x => x.UserId == userId, ct)} 格", + "- 旧版 JSON 存档未迁移,所有旅人从今天重新开始" + ], ["/状态", "/签到", "/抽卡", "/背包"]); + }, cancellationToken); + + public Task SignInAsync(long userId, CancellationToken cancellationToken = default) + => MutateAsync(userId, async (db, state, ct) => + { + string today = GameplayRules.Today(); + if (string.Equals(state.SignedOn, today, StringComparison.Ordinal)) + { + return ActionResult.Fail("今天已经签到过了", "没有重复发放奖励", + "明天再来,或先完成每日任务", "/每日任务"); + } + bool continuous = string.Equals(state.SignedOn, + DateOnly.ParseExact(today, "yyyy-MM-dd", CultureInfo.InvariantCulture) + .AddDays(-1).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture), StringComparison.Ordinal); + state.SignInStreak = continuous ? state.SignInStreak + 1 : 1; + state.SignedOn = today; + + long credits = GameplayRules.SignInCredits(state.SignInStreak - 1); + ApplyCurrency(db, state, credits, "credits", "signin", "signin", today); + int diamonds = state.SignInStreak % GameplayRules.SignInDiamondEveryDays == 0 + ? GameplayRules.SignInDiamondAmount : 0; + if (diamonds > 0) + { + ApplyCurrency(db, state, diamonds, "diamonds", "signin_streak", "signin", today); + } + db.SignInRecords.Add(new SignInRecord + { + UserId = userId, SignInDate = today, + RewardJson = JsonSerializer.Serialize(new { credits, diamonds }), + CreatedAtUtc = DateTime.UtcNow + }); + await AdvanceQuestAsync(db, userId, "signin", ct); + return ActionResult.Success("📅 签到成功", + $"连续 {state.SignInStreak} 天 · 金币 +{credits}{(diamonds > 0 ? $" · 钻石 +{diamonds}" : "")}", + [ + $"- 当前金币:{state.Credits} 钻石:{state.Diamonds}", + $"- 连续签到每满 {GameplayRules.SignInDiamondEveryDays} 天额外送 {GameplayRules.SignInDiamondAmount} 钻石", + "- 今日任务「今日签到」进度已更新" + ], ["/每日任务", "/状态", "/抽卡"]); + }, cancellationToken); + + public Task GachaAsync(long userId, int count, CancellationToken cancellationToken = default) + => MutateAsync(userId, async (db, state, ct) => + { + int pulls = Math.Clamp(count, 1, GameplayRules.MaxGachaPerCall); + int cost = pulls == 10 ? GameplayRules.GachaTenCost : GameplayRules.GachaCost * pulls; + if (state.Diamonds < cost) + { + return ActionResult.Fail("钻石不足", "没有扣除钻石,也没有生成物品", + $"需要 {cost} 钻石,当前 {state.Diamonds};可先签到或领取每日任务", "/签到"); + } + ApplyCurrency(db, state, -cost, "diamonds", "gacha", "gacha", GameplayRules.Today()); + + var catalog = FunGameService.AllItems; + List<(string Name, int Quality)> drawn = []; + for (int i = 0; i < pulls; i++) + { + int quality = GameplayRules.RollQuality(Random.Shared); + // 十连保底至少一件史诗及以上。 + if (pulls == 10 && i == pulls - 1 && drawn.All(x => x.Quality < 4)) quality = 4; + string name = catalog.Count == 0 + ? "无名的遗物" + : catalog[Random.Shared.Next(catalog.Count)].Name; + drawn.Add((name, quality)); + } + foreach ((string name, int quality) in drawn) + { + await AddItemAsync(db, userId, name, quality, 1, ct); + } + await AdvanceQuestAsync(db, userId, "gacha", ct); + + List lines = drawn.Select((item, i) => + $"- {i + 1}. {GameplayRules.QualityName(item.Quality)} · {item.Name}").ToList(); + lines.Add($"- 消耗钻石 {cost},剩余 {state.Diamonds}"); + lines.Add("- 用 /背包 查看物品编号后再 @编号 使用或分解"); + return ActionResult.Success(pulls == 10 ? "🎰 十连抽卡" : "🎲 抽卡", + $"{pulls} 次抽取 · 已全部放入背包", + lines, ["/背包", "/状态", "/抽卡"]); + }, cancellationToken); + + public Task UseAsync(long userId, int index, int quantity, CancellationToken cancellationToken = default) + => MutateAsync(userId, async (db, state, ct) => + { + ItemInstance? item = await ItemByIndexAsync(db, userId, index, ct); + if (item is null) return NoSuchItem(index); + if (await db.CharacterEquipment.AsNoTracking() + .AnyAsync(x => x.ItemInstanceId == item.Id, ct)) + { + return ActionResult.Fail("该物品已装备", "没有消耗物品", + "先 /卸下 <槽位> 再使用", "/背包"); + } + int amount = Math.Clamp(quantity, 1, item.Quantity); + int exp = GameplayRules.UseExp(item.Quality, amount); + state.Exp += exp; + Consume(db, item, amount); + return ActionResult.Success("🧪 使用物品", + $"{ReadName(item)} ×{amount} · 旅人经验 +{exp}", + [$"- 当前经验:{state.Exp} 等级:Lv.{state.Level}", "- 物品已扣除,未产生货币变动"], + ["/背包", "/状态"]); + }, cancellationToken); + + public Task SalvageAsync(long userId, int index, int quantity, CancellationToken cancellationToken = default) + => MutateAsync(userId, async (db, state, ct) => + { + ItemInstance? item = await ItemByIndexAsync(db, userId, index, ct); + if (item is null) return NoSuchItem(index); + if (item.Locked || item.Bound) + { + return ActionResult.Fail("该物品已锁定或绑定", "没有分解任何物品", + "先用 /解锁 @编号,绑定装备需先 /卸下", "/背包"); + } + int amount = Math.Clamp(quantity, 1, item.Quantity); + string name = ReadName(item); + long credits = GameplayRules.SalvageValue(item.Quality, amount); + ApplyCurrency(db, state, credits, "credits", "salvage", "item", item.Id.ToString(CultureInfo.InvariantCulture)); + Consume(db, item, amount); + return ActionResult.Success("♻️ 分解完成", + $"{name} ×{amount} · 金币 +{credits}", + [$"- 品质:{GameplayRules.QualityName(item.Quality)} 当前金币:{state.Credits}", "- 已写入账本 ledger,可对账"], + ["/背包", "/状态"]); + }, cancellationToken); + + public Task SetLockAsync(long userId, int index, bool locked, CancellationToken cancellationToken = default) + => MutateAsync(userId, async (db, _, ct) => + { + ItemInstance? item = await ItemByIndexAsync(db, userId, index, ct); + if (item is null) return NoSuchItem(index); + item.Locked = locked; + return ActionResult.Success(locked ? "🔒 物品已锁定" : "🔓 物品已解锁", + $"{ReadName(item)} 现在{(locked ? "不可分解/丢弃" : "可分解")}", + ["- 锁定状态已写入存档"], ["/背包"]); + }, cancellationToken); + + public Task EquipAsync(long userId, int index, CancellationToken cancellationToken = default) + => MutateAsync(userId, async (db, _, ct) => + { + ItemInstance? item = await ItemByIndexAsync(db, userId, index, ct); + if (item is null) return NoSuchItem(index); + if (item.Bound) return ActionResult.Fail("该物品已装备", "没有重复装备", "先 /卸下 <槽位>", "/背包"); + DataCharacter? main = await MainCharacterAsync(db, userId, ct); + if (main is null) return NoMainCharacter(); + List used = await db.CharacterEquipment.AsNoTracking() + .Where(x => x.CharacterId == main.Id).Select(x => x.Slot).ToListAsync(ct); + string? slot = GameplayRules.EquipmentSlots.FirstOrDefault(s => !used.Contains(s)); + if (slot is null) + { + return ActionResult.Fail("装备槽已满", "没有装备该物品", + "先 /卸下 weapon / armor / trinket 之一", "/角色"); + } + item.Bound = true; + db.CharacterEquipment.Add(new CharacterEquipment + { + CharacterId = main.Id, Slot = slot, ItemInstanceId = item.Id + }); + return ActionResult.Success("🛡️ 装备成功", + $"{ReadName(item)} → {main.Name} 的 {slot} 槽", + ["- 装备后自动绑定,需先卸下才能分解", "- 槽位:weapon / armor / trinket"], + ["/角色", "/背包", "/状态"]); + }, cancellationToken); + + public Task UnequipAsync(long userId, string slot, CancellationToken cancellationToken = default) + => MutateAsync(userId, async (db, _, ct) => + { + DataCharacter? main = await MainCharacterAsync(db, userId, ct); + if (main is null) return NoMainCharacter(); + CharacterEquipment? equipment = await db.CharacterEquipment + .FirstOrDefaultAsync(x => x.CharacterId == main.Id && x.Slot == slot, ct); + if (equipment is null) + { + return ActionResult.Fail("该槽位没有装备", "没有卸下任何物品", + "用 /角色 查看当前装备,槽位为 weapon / armor / trinket", "/角色"); + } + ItemInstance? item = await db.ItemInstances + .FirstOrDefaultAsync(x => x.Id == equipment.ItemInstanceId && x.UserId == userId, ct); + db.CharacterEquipment.Remove(equipment); + if (item is not null) item.Bound = false; + return ActionResult.Success("📦 已卸下", + $"{slot} 槽 · {(item is null ? "未知物品" : ReadName(item))}", + ["- 物品已回到背包并解除绑定"], ["/背包", "/角色"]); + }, cancellationToken); + + public Task SetMainAsync(long userId, int index, CancellationToken cancellationToken = default) + => MutateAsync(userId, async (db, _, ct) => + { + List characters = await db.Characters + .Where(x => x.UserId == userId).OrderBy(x => x.Id).ToListAsync(ct); + if (index <= 0 || index > characters.Count) return NoSuchCharacter(index); + foreach (DataCharacter character in characters) character.IsMain = false; + DataCharacter selected = characters[index - 1]; + selected.IsMain = true; + return ActionResult.Success("⭐ 主战角色已更新", selected.Name, + ["- 装备、练级、突破都作用于当前主战角色"], ["/角色", "/状态"]); + }, cancellationToken); + + public Task LevelUpAsync(long userId, int index, CancellationToken cancellationToken = default) + => MutateAsync(userId, async (db, state, ct) => + { + DataCharacter? character = await CharacterByIndexAsync(db, userId, index, ct); + if (character is null) return NoSuchCharacter(index); + int cap = GameplayRules.LevelCap(character.BreakLevel); + if (character.Level >= cap) + { + return ActionResult.Fail("已达当前等级上限", "没有消耗经验", + $"上限 Lv.{cap},先用 /突破 @编号 提升", "/角色"); + } + long cost = GameplayRules.ExpForLevel(character.Level); + if (state.Exp < cost) + { + return ActionResult.Fail("旅人经验不足", "没有消耗经验,等级未变", + $"升级需要 {cost} 经验,当前 {state.Exp};练级或使用物品可获得经验", "/练级 @1"); + } + state.Exp -= cost; + character.Level += 1; + character.Exp += cost; + return ActionResult.Success("⬆️ 角色升级", $"{character.Name} → Lv.{character.Level}", + [$"- 消耗旅人经验 {cost},剩余 {state.Exp}", $"- 当前上限 Lv.{cap}"], + ["/角色", "/状态"]); + }, cancellationToken); + + public Task BreakAsync(long userId, int index, CancellationToken cancellationToken = default) + => MutateAsync(userId, async (db, state, ct) => + { + DataCharacter? character = await CharacterByIndexAsync(db, userId, index, ct); + if (character is null) return NoSuchCharacter(index); + int cap = GameplayRules.LevelCap(character.BreakLevel); + if (character.Level < cap) + { + return ActionResult.Fail("尚未达到当前等级上限", "没有消耗金币", + $"需要 Lv.{cap},当前 Lv.{character.Level}", "/角色"); + } + long cost = GameplayRules.BreakCost(character.BreakLevel); + if (state.Credits < cost) + { + return ActionResult.Fail("金币不足", "没有消耗金币,突破等级未变", + $"需要 {cost} 金币,当前 {state.Credits}", "/签到"); + } + ApplyCurrency(db, state, -cost, "credits", "break", "character", character.Id.ToString(CultureInfo.InvariantCulture)); + character.BreakLevel += 1; + return ActionResult.Success("💥 突破成功", + $"{character.Name} · 突破 {character.BreakLevel} 阶 · 上限提升至 Lv.{GameplayRules.LevelCap(character.BreakLevel)}", + [$"- 消耗金币 {cost},剩余 {state.Credits}", "- 已写入账本 ledger"], + ["/角色", "/状态"]); + }, cancellationToken); + + public Task TrainStartAsync(long userId, int index, int minutes, CancellationToken cancellationToken = default) + => MutateAsync(userId, async (db, state, ct) => + { + DataCharacter? character = await CharacterByIndexAsync(db, userId, index, ct); + if (character is null) return NoSuchCharacter(index); + if (await db.TrainingSessions.AnyAsync(x => x.UserId == userId && x.SettledAtUtc == null, ct)) + { + return ActionResult.Fail("已有角色在练级", "没有重复扣除金币", + "等待结束后用 /收取练级 结算", "/收取练级"); + } + if (state.Credits < GameplayRules.TrainCostCredits) + { + return ActionResult.Fail("金币不足", "没有开始练级", + $"需要 {GameplayRules.TrainCostCredits} 金币,当前 {state.Credits}", "/签到"); + } + int duration = Math.Clamp(minutes, 1, GameplayRules.TrainMaxMinutes); + DateTime now = DateTime.UtcNow; + ApplyCurrency(db, state, -GameplayRules.TrainCostCredits, "credits", "train_start", "character", character.Id.ToString(CultureInfo.InvariantCulture)); + db.TrainingSessions.Add(new TrainingSession + { + UserId = userId, CharacterId = character.Id, + StartedAtUtc = now, EndsAtUtc = now.AddMinutes(duration) + }); + return ActionResult.Success("🏋️ 开始练级", + $"{character.Name} · {duration} 分钟后可用 /收取练级 结算", + [$"- 消耗金币 {GameplayRules.TrainCostCredits},剩余 {state.Credits}", + $"- 结算经验:{GameplayRules.TrainExpBase + GameplayRules.TrainExpPerLevel * character.Level}"], + ["/收取练级", "/状态"]); + }, cancellationToken); + + public Task TrainClaimAsync(long userId, CancellationToken cancellationToken = default) + => MutateAsync(userId, async (db, state, ct) => + { + TrainingSession? session = await db.TrainingSessions + .Where(x => x.UserId == userId && x.SettledAtUtc == null) + .OrderBy(x => x.EndsAtUtc).FirstOrDefaultAsync(ct); + if (session is null) + { + return ActionResult.Fail("当前没有练级中的角色", "没有结算任何经验", + "先用 /练级 @编号 [分钟] 开始", "/练级 @1 1"); + } + if (session.EndsAtUtc > DateTime.UtcNow) + { + int seconds = (int)Math.Ceiling((session.EndsAtUtc - DateTime.UtcNow).TotalSeconds); + return ActionResult.Fail("练级尚未结束", "没有结算任何经验", + $"还需约 {seconds} 秒,稍后再用 /收取练级", "/收取练级"); + } + DataCharacter? character = await db.Characters + .FirstOrDefaultAsync(x => x.Id == session.CharacterId && x.UserId == userId, ct); + long exp = GameplayRules.TrainExpBase + GameplayRules.TrainExpPerLevel * (character?.Level ?? 1); + state.Exp += exp; + if (character is not null) character.Exp += exp; + session.SettledAtUtc = DateTime.UtcNow; + return ActionResult.Success("🎓 练级结算", + $"{(character is null ? "角色" : character.Name)} · 旅人经验 +{exp}", + [$"- 当前经验:{state.Exp} 等级:Lv.{state.Level}", "- 经验可用于 /升级 @编号"], + ["/角色", "/升级 @1", "/状态"]); + }, cancellationToken); + + public Task SquadAsync(long userId, string operation, int index, CancellationToken cancellationToken = default) + => MutateAsync(userId, async (db, _, ct) => + { + Squad? squad = await db.Squads.FirstOrDefaultAsync(x => x.UserId == userId && x.IsActive, ct); + if (squad is null) + { + squad = new Squad { UserId = userId, Name = "主力小队", IsActive = true }; + db.Squads.Add(squad); + await db.SaveChangesAsync(ct); + } + List members = await db.SquadMembers + .Where(x => x.SquadId == squad.Id).OrderBy(x => x.Slot).ToListAsync(ct); + List characters = await db.Characters.AsNoTracking() + .Where(x => x.UserId == userId).OrderBy(x => x.Id).ToListAsync(ct); + + string op = operation.ToLowerInvariant(); + if (op is "add" or "加入") + { + if (index <= 0 || index > characters.Count) return NoSuchCharacter(index); + if (members.Any(x => x.CharacterId == characters[index - 1].Id)) + { + return ActionResult.Fail("该角色已在小队中", "没有重复加入", "用 /小队 查看编成", "/小队"); + } + if (members.Count >= 3) + { + return ActionResult.Fail("小队已满(3 人)", "没有加入新成员", + "先用 /小队 移除 @编号 腾出位置", "/小队"); + } + int slot = Enumerable.Range(1, 3).First(s => members.All(x => x.Slot != s)); + db.SquadMembers.Add(new SquadMember { SquadId = squad.Id, CharacterId = characters[index - 1].Id, Slot = slot }); + return ActionResult.Success("👥 小队编成", $"{characters[index - 1].Name} 加入 {slot} 号位", + ["- 小队上限 3 人"], ["/小队", "/角色"]); + } + if (op is "remove" or "移除" or "rm") + { + SquadMember? target = members.FirstOrDefault(x => x.Slot == index); + if (target is null) + { + return ActionResult.Fail("该位置没有成员", "没有移除任何角色", + "用 /小队 查看位置编号", "/小队"); + } + db.SquadMembers.Remove(target); + return ActionResult.Success("👥 小队编成", $"{index} 号位已移除", + ["- 其余成员位置保持不变"], ["/小队"]); + } + + List lines = []; + foreach (int slot in Enumerable.Range(1, 3)) + { + SquadMember? member = members.FirstOrDefault(x => x.Slot == slot); + DataCharacter? character = member is null ? null : characters.FirstOrDefault(x => x.Id == member.CharacterId); + lines.Add($"- @{slot} {(character is null ? "(空)" : character.Name)}"); + } + lines.Add("- /小队 加入 @编号 · /小队 移除 @位置"); + return ActionResult.Success("👥 主力小队", $"{members.Count}/3 人", + lines, ["/角色", "/状态"]); + }, cancellationToken); + + public Task ClaimQuestAsync(long userId, int index, CancellationToken cancellationToken = default) + => MutateAsync(userId, async (db, state, ct) => + { + if (index <= 0 || index > GameplayRules.DailyQuests.Count) + { + return ActionResult.Fail("任务编号无效", "没有领取任何奖励", + "用 /每日任务 查看编号", "/每日任务"); + } + (string key, string name, int target) = GameplayRules.DailyQuests[index - 1]; + string date = GameplayRules.Today(); + DailyQuest? quest = await db.DailyQuests + .FirstOrDefaultAsync(x => x.UserId == userId && x.QuestKey == key && x.QuestDate == date, ct); + if (quest is null || ReadProgress(quest) < target || quest.Status == 0) + { + return ActionResult.Fail($"「{name}」尚未完成", "没有发放奖励", + "完成对应行为后再领取", "/每日任务"); + } + if (quest.Status == 2) + { + return ActionResult.Fail("该任务奖励已领取", "没有重复发放", + "明天再来", "/每日任务"); + } + ApplyCurrency(db, state, GameplayRules.QuestRewardCredits, "credits", "quest", "quest", key); + ApplyCurrency(db, state, GameplayRules.QuestRewardDiamonds, "diamonds", "quest", "quest", key); + quest.Status = 2; + quest.ClaimedAtUtc = DateTime.UtcNow; + return ActionResult.Success("✅ 任务奖励已领取", name, + [ + $"- 金币 +{GameplayRules.QuestRewardCredits} 钻石 +{GameplayRules.QuestRewardDiamonds}", + $"- 当前金币:{state.Credits} 钻石:{state.Diamonds}", + "- 已写入账本 ledger" + ], ["/每日任务", "/状态"]); + }, cancellationToken); + + public Task RecordSimulationAsync(long userId, CancellationToken cancellationToken = default) + => MutateAsync(userId, async (db, _, ct) => + { + await AdvanceQuestAsync(db, userId, "simulate", ct); + return ActionResult.Success("模拟已计入每日任务", "「完成一次战斗模拟」进度 +1", + [], ["/每日任务"]); + }, cancellationToken); + + // ---------------- 内部工具 ---------------- + + /// 首次访问懒建档:player_state + 初始角色 + 初始物品 + 初始资金流水,全部一次保存。 + private static async Task EnsureAsync(BotDbContext db, long userId, CancellationToken ct) + { + PlayerState? state = await db.PlayerStates.FirstOrDefaultAsync(x => x.UserId == userId, ct); + if (state is not null) return state; + + state = new PlayerState + { + UserId = userId, Credits = GameplayRules.StartingCredits, + Diamonds = GameplayRules.StartingDiamonds, Level = 1, + LogonOn = GameplayRules.Today(), UpdatedAtUtc = DateTime.UtcNow + }; + db.PlayerStates.Add(state); + + var characterTemplates = FunGameService.Characters; + if (characterTemplates.Count > 0) + { + var template = characterTemplates[0]; + db.Characters.Add(new DataCharacter + { + UserId = userId, + CatalogId = template.Id.ToString(CultureInfo.InvariantCulture), + CatalogVersion = GameplayRules.CatalogVersion, + Name = template.GetName(), Level = 1, IsMain = true, + AttrsJson = "{}", CreatedAtUtc = DateTime.UtcNow + }); + } + var itemTemplates = FunGameService.AllItems; + for (int i = 0; i < Math.Min(GameplayRules.StartingItems, itemTemplates.Count); i++) + { + var template = itemTemplates[i]; + db.ItemInstances.Add(new ItemInstance + { + UserId = userId, CatalogId = template.Id.ToString(CultureInfo.InvariantCulture), + Quantity = 1, Quality = 1, PayloadJson = JsonSerializer.Serialize(new { name = template.Name }), + AcquiredAtUtc = DateTime.UtcNow + }); + } + db.LedgerEntries.Add(new LedgerEntry + { + UserId = userId, Currency = "credits", Delta = GameplayRules.StartingCredits, + BalanceAfter = GameplayRules.StartingCredits, Reason = "register", + RefType = "player", RefId = userId.ToString(CultureInfo.InvariantCulture), + CreatedAtUtc = DateTime.UtcNow + }); + await db.SaveChangesAsync(ct); + return state; + } + + /// 货币变动与 ledger 必须成对出现;Add 后由调用方的 SaveChanges 一次提交。 + private static void ApplyCurrency( + BotDbContext db, PlayerState state, long delta, string currency, + string reason, string refType, string refId) + { + if (delta == 0) return; + if (currency == "diamonds") state.Diamonds += delta; + else state.Credits += delta; + long balance = currency == "diamonds" ? state.Diamonds : state.Credits; + db.LedgerEntries.Add(new LedgerEntry + { + UserId = state.UserId, Currency = currency, Delta = delta, BalanceAfter = balance, + Reason = reason, RefType = refType, RefId = refId, CreatedAtUtc = DateTime.UtcNow + }); + } + + private static async Task AddItemAsync( + BotDbContext db, long userId, string name, int quality, int quantity, CancellationToken ct) + { + string payload = JsonSerializer.Serialize(new { name }); + ItemInstance? stack = await db.ItemInstances.FirstOrDefaultAsync( + x => x.UserId == userId && x.CatalogId == "gacha" && x.Quality == quality + && x.PayloadJson == payload && !x.Bound && !x.Locked, ct); + if (stack is null) + { + db.ItemInstances.Add(new ItemInstance + { + UserId = userId, CatalogId = "gacha", Quantity = quantity, Quality = quality, + PayloadJson = payload, AcquiredAtUtc = DateTime.UtcNow + }); + } + else + { + stack.Quantity += quantity; + } + } + + private static async Task ItemByIndexAsync( + BotDbContext db, long userId, int index, CancellationToken ct) + => index <= 0 ? null : await db.ItemInstances + .Where(x => x.UserId == userId).OrderBy(x => x.Id) + .Skip(index - 1).FirstOrDefaultAsync(ct); + + private static async Task CharacterByIndexAsync( + BotDbContext db, long userId, int index, CancellationToken ct) + => index <= 0 ? null : await db.Characters + .Where(x => x.UserId == userId).OrderBy(x => x.Id) + .Skip(index - 1).FirstOrDefaultAsync(ct); + + private static async Task MainCharacterAsync(BotDbContext db, long userId, CancellationToken ct) + => await db.Characters.Where(x => x.UserId == userId && x.IsMain) + .OrderBy(x => x.Id).FirstOrDefaultAsync(ct); + + private static void Consume(BotDbContext db, ItemInstance item, int amount) + { + if (item.Quantity <= amount) db.ItemInstances.Remove(item); + else item.Quantity -= amount; + } + + private static async Task AdvanceQuestAsync(BotDbContext db, long userId, string key, CancellationToken ct) + { + (string _, string _, int target) = GameplayRules.DailyQuests.First(x => x.Key == key); + string date = GameplayRules.Today(); + DailyQuest? quest = await db.DailyQuests.FirstOrDefaultAsync( + x => x.UserId == userId && x.QuestKey == key && x.QuestDate == date, ct); + if (quest is null) + { + quest = new DailyQuest + { + UserId = userId, QuestKey = key, QuestDate = date, + Status = target <= 1 ? 1 : 0, + ProgressJson = JsonSerializer.Serialize(new { count = 1 }) + }; + db.DailyQuests.Add(quest); + return; + } + if (quest.Status == 2) return; + int count = ReadProgress(quest) + 1; + quest.ProgressJson = JsonSerializer.Serialize(new { count }); + if (count >= target) quest.Status = 1; + } + + private static int ReadProgress(DailyQuest quest) + { + try + { + using JsonDocument document = JsonDocument.Parse(quest.ProgressJson); + return document.RootElement.TryGetProperty("count", out JsonElement count) + ? count.GetInt32() : 0; + } + catch (JsonException) { return 0; } + } + + private static string ReadName(ItemInstance item) + { + if (string.IsNullOrWhiteSpace(item.PayloadJson)) return $"物品 {item.CatalogId}"; + try + { + using JsonDocument document = JsonDocument.Parse(item.PayloadJson); + return document.RootElement.TryGetProperty("name", out JsonElement name) + ? name.GetString() ?? $"物品 {item.CatalogId}" + : $"物品 {item.CatalogId}"; + } + catch (JsonException) { return $"物品 {item.CatalogId}"; } + } + + private static ActionResult NoSuchItem(int index) + => ActionResult.Fail($"背包 @ {index} 不存在", "没有改动任何物品", + "用 /背包 查看当前编号,编号按背包顺序稳定排列", "/背包"); + + private static ActionResult NoSuchCharacter(int index) + => ActionResult.Fail($"角色 @ {index} 不存在", "没有改动任何角色", + "用 /角色 查看当前编号", "/角色"); + + private static ActionResult NoMainCharacter() + => ActionResult.Fail("没有主战角色", "没有改动任何装备", + "用 /角色 查看角色,再用 /设主战 @编号 指定", "/角色"); +} diff --git a/OshimaBot/Program.cs b/OshimaBot/Program.cs index dd0ce93..c49c070 100644 --- a/OshimaBot/Program.cs +++ b/OshimaBot/Program.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Options; using OshimaBot.Commands; using OshimaBot.Data; using OshimaBot.Game.Services; +using OshimaBot.Gameplay; using OshimaBot.Messaging; using OshimaBot.Models; using OshimaBot.Security; @@ -72,11 +73,13 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddScoped(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddSingleton(sp => new CommandDependencies( sp.GetRequiredService(), sp.GetRequiredService(), sp.GetRequiredService(), sp.GetRequiredService(), + sp.GetRequiredService(), sp.GetRequiredService().CreateLogger("OshimaBot.Commands"))); builder.Services.AddSingleton(sp => CommandCatalog.Build(sp.GetRequiredService())); builder.Services.AddSingleton(); diff --git a/OshimaBot/Security/ApiKeyAuthenticator.cs b/OshimaBot/Security/ApiKeyAuthenticator.cs index 94a5f6a..f1528fd 100644 --- a/OshimaBot/Security/ApiKeyAuthenticator.cs +++ b/OshimaBot/Security/ApiKeyAuthenticator.cs @@ -8,7 +8,10 @@ using System.Text.Encodings.Web; namespace OshimaBot.Security; /// 数据库侧 API Key 校验:只认 api_keys 表中的 SHA-256 摘要,支持吊销/过期/按 key 分权。 -public sealed class ApiKeyAuthenticator(IServiceScopeFactory scopeFactory) +public sealed class ApiKeyAuthenticator( + IServiceScopeFactory scopeFactory, + IUserDirectory directory, + ILogger logger) { public async Task AuthenticateAsync(string key, CancellationToken cancellationToken = default) { @@ -22,11 +25,33 @@ public sealed class ApiKeyAuthenticator(IServiceScopeFactory scopeFactory) if (row is null || row.RevokedAtUtc is not null) return null; if (row.ExpiresAtUtc is not null && row.ExpiresAtUtc <= DateTime.UtcNow) return null; - List roleValues = row.UserId is null + // S6 遗留边界:引导密钥的 user_id 为 NULL,第三方通道因此拿不到存档身份。 + // 每个 ApiKey 首次认证时懒建一个独立内部用户(provider=apikey),之后走纯读路径。 + long? userId = row.UserId; + if (userId is null) + { + try + { + ExternalIdentity identity = new("apikey", $"key-{row.Id}", "thirdparty"); + BotUserPrincipal principal = await directory.ResolveAsync(identity, cancellationToken); + await db.ApiKeys.Where(x => x.Id == row.Id && x.UserId == null) + .ExecuteUpdateAsync(setters => setters + .SetProperty(x => x.UserId, (long?)principal.UserId), cancellationToken); + userId = principal.UserId; + logger.LogInformation("API Key 已绑定内部用户:KeyId={KeyId}, UserId={UserId}", row.Id, userId); + } + catch (Exception exception) + { + // 身份写入失败不阻断只读查询;需身份的 P1 指令会提示「身份尚未就绪」。 + logger.LogWarning(exception, "API Key 身份懒注册失败:KeyId={KeyId}", row.Id); + } + } + + List roleValues = userId is null ? [] : await db.UserRoles.AsNoTracking() - .Where(x => x.UserId == row.UserId.Value).Select(x => x.Role).ToListAsync(cancellationToken); - return new ApiKeyIdentity(row.Id, row.UserId, row.Name, + .Where(x => x.UserId == userId.Value).Select(x => x.Role).ToListAsync(cancellationToken); + return new ApiKeyIdentity(row.Id, userId, row.Name, ApiKeyDefaults.ParseScopes(row.ScopesJson), ApiKeyDefaults.ParseRoles(roleValues)); } }