服务端(Go): - GM 塞背包/带属性装备发放:在线角色推送 op14 增量物品行,背包实时刷新,无需回角色选择界面 - GM 改金币/改等级:去掉在线拒绝,落库后推送 op14 钱包/op37 等级状态,在线实时刷新 - 等级端点统一切换到新版 GMSetCharacterLevel(自带在线刷新) - 无限/锁死疲劳模式 + 燃烧疲劳:每房间消耗 2 点、打怪与通关经验双倍 - 燃烧疲劳开关时给所有在线角色推送系统提示(op12 mode 0) - 深渊史诗掉率:GM 全局倍率 + 城镇增益药剂窗口倍率,真实接入掉落结算 启动器(Python/JS): - 标题栏自绘、窗口 1204x717、禁止拖拽改大小、仅最小化/关闭 - GM 工具:疲劳与燃烧、深渊史诗掉率、拍卖行上架(单一定价对齐) - 拍卖行上架前端对齐服务端单一价格语义,修复 400 报错
170 lines
6.2 KiB
Go
170 lines
6.2 KiB
Go
// 本文件负责装配 DNF 仓储的 MySQL 实现。
|
|
package mysql
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"longheng.io/server/internal/modules/dnf/repository"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
ErrMySQLDBRequired = errors.New("dnf mysql db is required")
|
|
ErrMySQLConfigInvalid = errors.New("dnf mysql config is invalid")
|
|
)
|
|
|
|
// SQLRow 是 MySQL 查询单行结果的最小扫描接口。
|
|
type SQLRow interface {
|
|
Scan(...any) error
|
|
}
|
|
|
|
// SQLRows 是 MySQL 多行查询结果的最小接口。
|
|
type SQLRows interface {
|
|
Close() error
|
|
Next() bool
|
|
Scan(...any) error
|
|
Err() error
|
|
}
|
|
|
|
// SQLDB 是 DNF MySQL 仓储依赖的最小数据库接口。
|
|
type SQLDB interface {
|
|
ExecContext(context.Context, string, ...any) (sql.Result, error)
|
|
QueryContext(context.Context, string, ...any) (SQLRows, error)
|
|
QueryRowContext(context.Context, string, ...any) SQLRow
|
|
PingContext(context.Context) error
|
|
}
|
|
|
|
// SQLDBAdapter 把标准库 *sql.DB 适配成 DNF 仓储使用的 SQLDB。
|
|
type SQLDBAdapter struct {
|
|
DB *sql.DB
|
|
}
|
|
|
|
// ExecContext 执行 MySQL 写语句。
|
|
func (a SQLDBAdapter) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
|
|
if a.DB == nil {
|
|
return nil, ErrMySQLDBRequired
|
|
}
|
|
return a.DB.ExecContext(ctx, query, args...)
|
|
}
|
|
|
|
// QueryContext 执行 MySQL 多行查询。
|
|
func (a SQLDBAdapter) QueryContext(ctx context.Context, query string, args ...any) (SQLRows, error) {
|
|
if a.DB == nil {
|
|
return nil, ErrMySQLDBRequired
|
|
}
|
|
return a.DB.QueryContext(ctx, query, args...)
|
|
}
|
|
|
|
// QueryRowContext 执行 MySQL 单行查询。
|
|
func (a SQLDBAdapter) QueryRowContext(ctx context.Context, query string, args ...any) SQLRow {
|
|
if a.DB == nil {
|
|
return errorRow{err: ErrMySQLDBRequired}
|
|
}
|
|
return a.DB.QueryRowContext(ctx, query, args...)
|
|
}
|
|
|
|
// PingContext 检查 MySQL 连接是否可用。
|
|
func (a SQLDBAdapter) PingContext(ctx context.Context) error {
|
|
if a.DB == nil {
|
|
return ErrMySQLDBRequired
|
|
}
|
|
return a.DB.PingContext(ctx)
|
|
}
|
|
|
|
// BeginTx exposes a standard SQL transaction without widening the minimal
|
|
// SQLDB read/write interface used by repository test doubles.
|
|
func (a SQLDBAdapter) BeginTx(ctx context.Context, options *sql.TxOptions) (*sql.Tx, error) {
|
|
if a.DB == nil {
|
|
return nil, ErrMySQLDBRequired
|
|
}
|
|
return a.DB.BeginTx(ctx, options)
|
|
}
|
|
|
|
// MySQLGroupOptions 描述 DNF MySQL 仓储的区服数据库计划和表名前缀。
|
|
type MySQLGroupOptions struct {
|
|
DatabasePlan repository.DatabasePlan
|
|
TablePrefix string
|
|
Now func() time.Time
|
|
}
|
|
|
|
// NewMySQLGroup 使用注入的 SQLDB 创建 DNF MySQL 仓储聚合。
|
|
// 它只装配 Load/Save/SaveFields,不自动建库建表;schema 初始化由 SchemaComponent 负责。
|
|
func NewMySQLGroup(db SQLDB, options MySQLGroupOptions) (repository.Group, error) {
|
|
router, err := newMySQLRouter(db, options)
|
|
if err != nil {
|
|
return repository.Group{}, err
|
|
}
|
|
return newMySQLGroupFromRouter(router, true), nil
|
|
}
|
|
|
|
func newMySQLGroupFromRouter(router mysqlRouter, includeTransactions bool) repository.Group {
|
|
base := mysqlStoreBase{router: router}
|
|
group := repository.Group{
|
|
Account: &mysqlAccountStore{mysqlStoreBase: base},
|
|
AccountInventory: &mysqlAccountInventoryStore{mysqlStoreBase: base},
|
|
Character: &mysqlCharStore{mysqlStoreBase: base},
|
|
Inventory: &mysqlBagStore{mysqlStoreBase: base},
|
|
Equipment: &mysqlEquipmentStore{mysqlStoreBase: base},
|
|
Pet: &mysqlPetStore{mysqlStoreBase: base},
|
|
Quest: &mysqlQuestStore{mysqlStoreBase: base},
|
|
Skill: &mysqlSkillStore{mysqlStoreBase: base},
|
|
DungeonPermission: &mysqlDungeonPermissionStore{mysqlStoreBase: base},
|
|
HellPartyPermission: &mysqlHellPartyPermissionStore{mysqlStoreBase: base},
|
|
PacketTemplate: &mysqlPacketStore{mysqlStoreBase: base},
|
|
Settings: &mysqlSettingsStore{mysqlStoreBase: base},
|
|
Mailbox: &mysqlMailboxStore{mysqlStoreBase: base},
|
|
EpicBook: &mysqlEpicBookStore{mysqlStoreBase: base},
|
|
UnitedFriend: &mysqlUnitedFriendStore{mysqlStoreBase: base},
|
|
LegacyUserInfo: &mysqlLegacyUserInfoStore{mysqlStoreBase: base},
|
|
LegacyInventory: &mysqlLegacyInventoryStore{mysqlStoreBase: base},
|
|
}
|
|
if includeTransactions {
|
|
if _, ok := router.db.(sqlTxBeginner); ok {
|
|
group.CharacterCreate = &mysqlCharacterCreationUnitOfWork{router: router}
|
|
group.CharacterItems = &mysqlCharacterItemUnitOfWork{router: router}
|
|
group.CharacterTrade = &mysqlCharacterTradeUnitOfWork{router: router}
|
|
group.AccountCharacterTrade = &mysqlAccountCharacterTradeUnitOfWork{router: router}
|
|
group.AccountPairTrade = &mysqlAccountPairCharacterTradeUnitOfWork{router: router}
|
|
group.CharacterPets = &mysqlCharacterPetUnitOfWork{router: router}
|
|
group.AccountItems = &mysqlAccountCharacterItemUnitOfWork{router: router}
|
|
group.CharacterAssets = &mysqlCharacterAssetUnitOfWork{router: router}
|
|
group.MailboxAssets = &mysqlMailboxAssetUnitOfWork{router: router}
|
|
group.AccountAssets = &mysqlAccountCharacterAssetUnitOfWork{router: router}
|
|
group.BoosterAssets = &mysqlBoosterAssetUnitOfWork{router: router}
|
|
group.RentalAssets = &mysqlRentalAssetUnitOfWork{router: router}
|
|
group.CeraShopAssets = &mysqlCeraShopAssetUnitOfWork{router: router}
|
|
group.CharacterSkills = &mysqlCharacterSkillUnitOfWork{router: router}
|
|
group.CharacterProgression = &mysqlCharacterProgressionUnitOfWork{router: router}
|
|
group.CharacterSettlement = &mysqlCharacterSettlementUnitOfWork{router: router}
|
|
group.EpicBookCraft = &mysqlEpicBookCraftUnitOfWork{router: router}
|
|
group.RaidEntryCost = &mysqlRaidEntryCostUnitOfWork{router: router}
|
|
group.RaidRewards = &mysqlRaidRewardUnitOfWork{router: router}
|
|
}
|
|
}
|
|
return group
|
|
}
|
|
|
|
// NewMySQLGroupFromDB 使用标准库 *sql.DB 创建 DNF MySQL 仓储聚合。
|
|
// 调用方负责管理 DB 生命周期和连接池参数。
|
|
func NewMySQLGroupFromDB(db *sql.DB, options MySQLGroupOptions) (repository.Group, error) {
|
|
return NewMySQLGroup(SQLDBAdapter{DB: db}, options)
|
|
}
|
|
|
|
type errorRow struct {
|
|
err error
|
|
}
|
|
|
|
func (r errorRow) Scan(...any) error {
|
|
return r.err
|
|
}
|
|
|
|
func requireRecordKey[T any](keyFn func(T) string, record T, name string) (string, error) {
|
|
key, err := dbRecordKey(keyFn, record)
|
|
if err != nil {
|
|
return "", fmt.Errorf("%s: %w", name, err)
|
|
}
|
|
return key, nil
|
|
}
|