ZL-抽老婆插件 2.0 — 数据模型与存储设计

存储继续使用 JSON(数据量为群成员级别,无需数据库),重点解决:schema v2、原子写入、自动备份、旧数据迁移。

1. 文件布局(插件数据目录)

<数据目录>/
├── wife_config.json          # v2 配置(全局 + 分群覆盖)
├── wife_state.json           # v2 状态(升级后直接复用此文件名)
├── wife_state_v1_backup.json # 迁移时旧文件备份
└── backups/
    └── wife_state_2026-09-27.json   # 每日快照,保留最近 7 份

2. 配置模型 WifeConfig(v2)

在现有字段基础上新增/调整(旧字段全部保留,保证反序列化兼容):

public sealed class WifeConfig
{
    // ===== 现有字段沿用 =====
    public int DailyLimit { get; set; } = 1;
    public int ActiveUserDays { get; set; } = 30;
    public int MaxActiveRecords { get; set; } = 500;
    public int ForceMarryCooldownDays { get; set; } = 3;
    public int ProposeCooldownMinutes { get; set; } = 60;
    public int BreakupCooldownHours { get; set; } = 72;
    public int PickCandidateCount { get; set; } = 3;
    public bool EnableAllGroups { get; set; } = true;
    public bool AllowBotAsTarget { get; set; } = false;
    public bool AtPickedUser { get; set; } = true;
    public bool NotifyPickedUser { get; set; } = true;
    public List<long> EnabledGroups { get; set; } = new();
    public List<long> ExcludedUsers { get; set; } = new();
    public List<long> WhitelistGroups { get; set; } = new();
    public List<long> BlacklistGroups { get; set; } = new();

    // ===== 新增:品质 =====
    public QualityRates Quality { get; set; } = new();          // N/R/SR/SSR 概率(%)

    // ===== 新增:好感度 =====
    public int AffectionMax { get; set; } = 1000;
    public FlirtSettings Flirt { get; set; } = new();           // 次数3 / 2~5
    public FlirtSettings Date { get; set; } = new();            // 次数1 / 8~15
    public FlirtSettings Flower { get; set; } = new();          // 次数1 / 固定10
    public int IdleDaysThreshold { get; set; } = 3;
    public int IdleDailyPenalty { get; set; } = 20;

    // ===== 新增:抢夺 =====
    public int RaidBaseRate { get; set; } = 40;                 // %
    public int RaidAffectionStep { get; set; } = 5;             // 每100点好感减%
    public int RaidMinRate { get; set; } = 10;
    public int RaidDailyLimit { get; set; } = 2;
    public int RaidFailCooldownMinutes { get; set; } = 120;
    public int RaidProtectHours { get; set; } = 24;

    // ===== 新增:婚姻 =====
    public int MarryAffectionRequired { get; set; } = 550;
    public int MarryResponseHours { get; set; } = 24;
    public int DivorceCooldownDays { get; set; } = 7;
    public bool HaremMode { get; set; } = false;                // 海王模式

    // ===== 新增:红娘 =====
    public int MatchmakerDailyLimit { get; set; } = 1;
    public int MatchmakerResponseHours { get; set; } = 24;

    // ===== 新增:换老婆 =====
    public int RerollDailyLimit { get; set; } = 1;

    // ===== 新增:排行/播报 =====
    public bool WeeklyReportEnabled { get; set; } = true;
    public DayOfWeek WeeklyReportDay { get; set; } = DayOfWeek.Sunday;
    public string WeeklyReportTime { get; set; } = "21:00";

    // ===== 新增:播报模板 =====
    public BroadcastTemplates Templates { get; set; } = new();

    // ===== 新增:分群覆盖 =====
    public Dictionary<long, GroupOverride> GroupOverrides { get; set; } = new();
}

public sealed class GroupOverride
{
    public bool Enabled { get; set; } = true;                   // 本群总开关
    public Dictionary<string, bool> FeatureToggles { get; set; } = new(); // draw/affection/raid/marriage/rank
    public int? DailyLimit { get; set; }                        // null = 用全局
    public int? RaidDailyLimit { get; set; }
    public int? MatchmakerDailyLimit { get; set; }
    public List<long> ExcludedUsers { get; set; } = new();
}

3. 状态模型 WifeState(v2)

public sealed class WifeStateV2
{
    public Dictionary<long, GroupState> Groups { get; set; } = new(); // key = groupId
}

public sealed class GroupState
{
    public Dictionary<string, ActiveUserInfo> ActiveUsers { get; set; } = new(); // 沿用
    public List<BondRecord> Bonds { get; set; } = new();          // 存续关系
    public List<BondArchive> Archives { get; set; } = new();      // 已结束关系流水(含旧 RbqStats 迁移)
    public Dictionary<string, DailyCounters> Daily { get; set; } = new(); // key = yyyy-MM-dd
    public Dictionary<string, long> Cooldowns { get; set; } = new();      // key = "raid:userId" 等
    public List<PendingAction> Pending { get; set; } = new();     // 求婚/红娘/挑选,带过期
}

public sealed class BondRecord
{
    public long OwnerId { get; set; }            // 持有人
    public string OwnerName { get; set; } = "";
    public long WifeId { get; set; }
    public string WifeName { get; set; } = "";
    public string Quality { get; set; } = "N";   // N/R/SR/SSR
    public int Affection { get; set; }           // 0~1000
    public string Status { get; set; } = "dating"; // dating / married / cooling
    public string Source { get; set; } = "draw"; // draw/pick/force/propose/raid/match
    public DateTime StartedAt { get; set; }
    public DateTime LastInteractAt { get; set; }
    public bool IsForced { get; set; }
}

public sealed class DailyCounters
{
    public Dictionary<long, int> DrawCount { get; set; } = new();
    public Dictionary<long, int> RerollCount { get; set; } = new();
    public Dictionary<long, int> FlirtCount { get; set; } = new();   // 指向"当前老婆"的次数
    public Dictionary<long, int> DateCount { get; set; } = new();
    public Dictionary<long, int> FlowerCount { get; set; } = new();
    public Dictionary<long, int> RaidCount { get; set; } = new();
    public Dictionary<long, int> MatchmakerCount { get; set; } = new();
}

public sealed class PendingAction
{
    public string Kind { get; set; } = "";       // proposal / matchmaker / pick
    public long GroupId { get; set; }
    public long FromId { get; set; }             // 发起人(matchmaker 为 0)
    public long TargetId { get; set; }           // 目标(matchmaker 为 0,存 PairA/PairB)
    public long PairAId { get; set; }            // 仅 matchmaker 用
    public long PairBId { get; set; }
    public List<ActiveUserInfo> Candidates { get; set; } = new(); // 仅 pick 用
    public DateTime ExpiresAt { get; set; }
}

4. 存储机制

  • 写锁:SemaphoreSlim(1,1) 串行化所有状态写入,避免并发覆盖。

  • 原子写:先写 <file>.tmp,再 File.Replace / File.Move 覆盖。

  • 每日快照:写入路径统一走 WifeStore.SaveAsync();当日首次写入前复制一份到 backups/,清理 7 天前文件。

  • 惰性清理:Daily 计数器、过期 Pending、冷却 key 在每次加载/每日切换时清理。

5. 旧数据迁移(v1 → v2)

触发条件:状态文件反序列化为 WifeState(v1 结构)或文件缺少 v2 标记字段。

流程:

  1. 将原文件复制为 wife_state_v1_backup.json。

  2. 逐群迁移:

    • ActiveUsers 原样保留。

    • 每条 WifeRecord(Records)→ BondRecord:Affection=100、品质补 roll(按全局概率)、Source 沿用、Forced → IsForced、Status=dating、StartedAt=CreatedAt、LastInteractAt=CreatedAt。

    • RbqStats(用户 → 历史老婆 id 列表)→ Archives 流水(Quality=N,仅作展示)。

    • 三个冷却字典 key 前缀化并入 Cooldowns(force:/propose:/breakup:)。

  3. 写回为 v2 结构,后续按 v2 读写。

  4. 迁移结果写入运行日志(迁移群数 / 关系数)。

6. 控制台数据访问

控制台所有读写通过宿主提供的统一接口(WifeStore / WifeConfig),前端不直接碰文件,保证与群消息处理共用同一把写锁。

共 18 篇 ZL-抽老婆插件 / 数据模型与存储设计 0 字 0 行 zhangxiaolin
创建 2026-09-26T17:34:50.843527708Z 更新 2026-09-26T17:39:12.107557267Z