Files
90CNServer/server/internal/services/dnfbridge/gm_auction.go
T
hanfuit 645b64fe21 DNF90 服务端与启动器完整源码(含 GM 工具功能增强)
服务端(Go):
- GM 塞背包/带属性装备发放:在线角色推送 op14 增量物品行,背包实时刷新,无需回角色选择界面
- GM 改金币/改等级:去掉在线拒绝,落库后推送 op14 钱包/op37 等级状态,在线实时刷新
- 等级端点统一切换到新版 GMSetCharacterLevel(自带在线刷新)
- 无限/锁死疲劳模式 + 燃烧疲劳:每房间消耗 2 点、打怪与通关经验双倍
- 燃烧疲劳开关时给所有在线角色推送系统提示(op12 mode 0)
- 深渊史诗掉率:GM 全局倍率 + 城镇增益药剂窗口倍率,真实接入掉落结算

启动器(Python/JS):
- 标题栏自绘、窗口 1204x717、禁止拖拽改大小、仅最小化/关闭
- GM 工具:疲劳与燃烧、深渊史诗掉率、拍卖行上架(单一定价对齐)
- 拍卖行上架前端对齐服务端单一价格语义,修复 400 报错
2026-11-04 14:31:55 +08:00

508 lines
17 KiB
Go

package dnfbridge
import (
"context"
"errors"
"fmt"
"sort"
"strconv"
"strings"
"time"
dnfauction "longheng.io/server/internal/modules/dnf/auction"
dnfrepo "longheng.io/server/internal/modules/dnf/repository"
)
// GM 拍卖行接口。
//
// 服务端的拍卖行没有面向工具的 GM 能力,但 dnfauction.Owner 已经提供
// 不依赖玩家 session 的纯数据层方法(Register / Cancel / Search /
// LoadListing)。本文件把它们包成 GM 工具可直接调用的接口,供「线上
// 上架/下架」使用 —— 与 GM 工具 SQLite 直读的路径互补:服务端在线时走
// 这里(由服务端事务保证一致),停服时走 SQLite 直读。
// 高位段挂单 ID:服务端玩家路径的 ID 来自进程内计数器(重启后从当前
// Unix 时间戳重新开始),GM 工具若占用小 ID,重启后的服务端连续撞号会
// 上架失败。因此 GM 挂单统一落在 9e18 起的高位段,永不与玩家路径冲突。
const gmAuctionListingIDBase uint64 = 9000000000000000000
// gmAuctionListingPrefix 与 owner.go 的 listingPrefix 保持一致(该常量未导出)。
const gmAuctionListingPrefix = "auction:listing:"
// GMAuctionListing 是给 GM 工具看的挂单视图。
type GMAuctionListing struct {
ListingID uint64 `json:"listing_id"`
ItemID int64 `json:"item_id"`
Count int64 `json:"count"`
SellerID string `json:"seller_id"`
SellerName string `json:"seller_name"`
ListType byte `json:"list_type"`
SourceSlot int16 `json:"source_slot"`
UnitPrice int64 `json:"unit_price"`
StartPrice int64 `json:"start_price"`
BuyoutPrice int64 `json:"buyout_price"`
Deposit int64 `json:"deposit"`
FeePercent int64 `json:"fee_percent"`
BidTotal int64 `json:"bid_total"`
BidderID string `json:"bidder_id"`
CreatedAt int64 `json:"created_at"`
ExpiresAt int64 `json:"expires_at"`
Expired bool `json:"expired"`
}
// GMAuctionListingsResult 是列出挂单的返回。
type GMAuctionListingsResult struct {
Listings []GMAuctionListing `json:"listings"`
EscrowGold int64 `json:"escrow_gold"`
}
// GMAuctionListRequest 是 GM 上架请求。
type GMAuctionListRequest struct {
SellerID string `json:"seller_id"`
SellerName string `json:"seller_name"`
ItemID int64 `json:"item_id"`
Count int64 `json:"count"`
UnitPrice int64 `json:"unit_price"`
StartPrice int64 `json:"start_price"` // 每件起拍价,-1 表示不设竞拍
BuyoutPrice int64 `json:"buyout_price"` // 每件一口价,-1 表示不设一口价
DurationHours int64 `json:"duration_hours"` // 24 或 48
GrantIfMissing bool `json:"grant_if_missing,omitempty"` // 背包不足时自动发放差额(发放式上架)
}
// GMAuctionListResult 是 GM 上架结果。
type GMAuctionListResult struct {
ListingID uint64 `json:"listing_id"`
ItemID int64 `json:"item_id"`
Count int64 `json:"count"`
UnitPrice int64 `json:"unit_price"`
StartPrice int64 `json:"start_price"`
BuyoutPrice int64 `json:"buyout_price"`
Deposit int64 `json:"deposit"`
FeePercent int64 `json:"fee_percent"`
ExpiresAt int64 `json:"expires_at"`
SellerGoldAfter int64 `json:"seller_gold_after"`
}
// GMAuctionCancelRequest 是 GM 下架请求。
type GMAuctionCancelRequest struct {
ListingID uint64 `json:"listing_id"`
}
// GMAuctionCancelResult 是 GM 下架结果。
type GMAuctionCancelResult struct {
ListingID uint64 `json:"listing_id"`
ItemID int64 `json:"item_id"`
Count int64 `json:"count"`
SellerID string `json:"seller_id"`
DepositForfeited int64 `json:"deposit_forfeited"`
RefundGold int64 `json:"refund_gold"` // 有竞价时退给竞价者的金额
RefundBidder string `json:"refund_bidder"`
}
// gmAuctionOwner 构造一个拍卖行 Owner 并返回 repositories。
func (s *Service) gmAuctionOwner() (*dnfauction.Owner, dnfrepo.Group, error) {
if s == nil {
return nil, dnfrepo.Group{}, dnfauction.ErrUnavailable
}
repositories, ok := s.repositoryGroup()
if !ok {
return nil, dnfrepo.Group{}, dnfauction.ErrUnavailable
}
owner, err := dnfauction.NewOwner(repositories)
if err != nil {
return nil, dnfrepo.Group{}, err
}
return owner, repositories, nil
}
// gmAuctionNextID 返回一个高位段挂单 ID。它从 9e18 起单调递增,且跳过
// escrow 里已经占用的 ID(含已产生投递/退款的历史 ID,避免复用)。
func gmAuctionNextID(escrow dnfrepo.InventoryRecord) uint64 {
best := gmAuctionListingIDBase - 1
for key := range escrow.Slots {
if !strings.HasPrefix(key, gmAuctionListingPrefix) {
continue
}
raw := strings.TrimPrefix(key, gmAuctionListingPrefix)
if id, err := strconv.ParseUint(raw, 10, 64); err == nil && id >= gmAuctionListingIDBase && id > best {
best = id
}
}
return best + 1
}
func gmAuctionListingView(l dnfauction.Listing) GMAuctionListing {
now := time.Now()
return GMAuctionListing{
ListingID: l.ID,
ItemID: l.Item.ItemID,
Count: l.Item.Count,
SellerID: l.SellerID,
SellerName: l.SellerName,
ListType: l.ListType,
SourceSlot: l.SourceSlot,
UnitPrice: l.UnitPrice,
StartPrice: l.StartPrice,
BuyoutPrice: l.BuyoutPrice,
Deposit: l.Deposit,
FeePercent: l.FeePercent,
BidTotal: l.BidTotal,
BidderID: l.BidderID,
CreatedAt: l.CreatedAt.Unix(),
ExpiresAt: l.ExpiresAt.Unix(),
Expired: !l.ExpiresAt.IsZero() && l.ExpiresAt.Before(now),
}
}
// GMAuctionListings 列出所有挂单(含已过期),供 GM 工具展示与下架。
func (s *Service) GMAuctionListings(ctx context.Context) (GMAuctionListingsResult, error) {
result := GMAuctionListingsResult{Listings: make([]GMAuctionListing, 0)}
owner, repositories, err := s.gmAuctionOwner()
if err != nil {
return result, err
}
if err := owner.EnsureEscrow(ctx); err != nil {
return result, err
}
escrow, found, err := repositories.Inventory.Load(ctx, dnfauction.EscrowCharacterID)
if err != nil {
return result, err
}
if !found {
return result, nil
}
// 托管角色金币
if escrowChar, charFound, charErr := repositories.Character.Load(ctx, dnfauction.EscrowCharacterID); charErr == nil && charFound {
result.EscrowGold = escrowChar.Stats["gold"]
}
for key := range escrow.Slots {
if !strings.HasPrefix(key, gmAuctionListingPrefix) {
continue
}
id, parseErr := strconv.ParseUint(strings.TrimPrefix(key, gmAuctionListingPrefix), 10, 64)
if parseErr != nil {
continue
}
listing, loadErr := owner.LoadListing(ctx, id)
if loadErr != nil {
continue
}
result.Listings = append(result.Listings, gmAuctionListingView(listing))
}
sort.Slice(result.Listings, func(i, j int) bool {
return result.Listings[i].ListingID > result.Listings[j].ListingID
})
return result, nil
}
// GMAuctionList 上架一件物品。行为对齐玩家路径,但不依赖 session:
// 押金 10000、标准手续费 5%、时长 24/48 小时、主背包(list 0)为来源。
func (s *Service) GMAuctionList(ctx context.Context, request GMAuctionListRequest) (GMAuctionListResult, error) {
var result GMAuctionListResult
owner, repositories, err := s.gmAuctionOwner()
if err != nil {
return result, err
}
if err := owner.EnsureEscrow(ctx); err != nil {
return result, err
}
// 解析卖家
sellerID := strings.TrimSpace(request.SellerID)
if sellerID == "" {
return result, fmt.Errorf("%w: seller is required", ErrGMRequestInvalid)
}
seller, err := s.gmCharacter(ctx, repositories, sellerID, request.SellerName)
if err != nil {
return result, err
}
if seller.CharacterID == dnfauction.EscrowCharacterID {
return result, fmt.Errorf("%w: cannot list from escrow", ErrGMRequestInvalid)
}
// 参数校验
if request.ItemID <= 0 || request.Count <= 0 || request.UnitPrice <= 0 {
return result, fmt.Errorf("%w: item_id/count/unit_price must be positive", ErrGMRequestInvalid)
}
if request.StartPrice < -1 || request.StartPrice > request.UnitPrice {
return result, fmt.Errorf("%w: start_price must be -1 or in [0, unit_price]", ErrGMRequestInvalid)
}
buyoutPrice := request.BuyoutPrice
if buyoutPrice == 0 {
buyoutPrice = request.UnitPrice
}
if buyoutPrice == -1 {
if request.StartPrice != request.UnitPrice {
return result, fmt.Errorf("%w: buyout-only listing requires start_price == unit_price", ErrGMRequestInvalid)
}
} else if buyoutPrice != request.UnitPrice {
return result, fmt.Errorf("%w: buyout_price must equal unit_price", ErrGMRequestInvalid)
}
durationHours := request.DurationHours
if durationHours != 24 && durationHours != 48 {
return result, fmt.Errorf("%w: duration_hours must be 24 or 48", ErrGMRequestInvalid)
}
// 从卖家背包找满足条件的物品槽(主背包 list 0)
inventory, found, err := repositories.Inventory.Load(ctx, seller.CharacterID)
if err != nil {
return result, err
}
if !found {
return result, ErrGMInventoryNotFound
}
sourceSlot, stack, ok := gmFindStackableSourceSlot(inventory.Slots, request.ItemID, request.Count)
if !ok && request.GrantIfMissing {
// 发放式上架:背包不足时,先凭空发放差额(复用 GMGrantItem 的
// 副本拾取发放路径,物品名/绑定/容器格位均由 PVF 目录正确推导),
// 再重新读背包找槽。
have := int64(0)
for key, st := range inventory.Slots {
parts := strings.SplitN(key, ":", 2)
if len(parts) == 2 && parts[0] == "0" && st.ItemID == request.ItemID {
have += st.Count
}
}
shortage := request.Count - have
if shortage > 0 {
if _, err := s.GMGrantItem(ctx, GMItemGrantRequest{
CharacterID: seller.CharacterID,
ItemID: uint32(request.ItemID),
Count: uint32(shortage),
}); err != nil {
return result, fmt.Errorf("%w: 自动补足发放失败: %v", ErrGMItemRowNotFound, err)
}
inventory, found, err = repositories.Inventory.Load(ctx, seller.CharacterID)
if err != nil {
return result, err
}
if !found {
return result, ErrGMInventoryNotFound
}
sourceSlot, stack, ok = gmFindStackableSourceSlot(inventory.Slots, request.ItemID, request.Count)
}
}
if !ok {
return result, fmt.Errorf("%w: item %d not found in seller bag with enough count", ErrGMItemRowNotFound, request.ItemID)
}
// 分配高位段挂单 ID,撞号时刷新 escrow 重试
escrow, _, err := repositories.Inventory.Load(ctx, dnfauction.EscrowCharacterID)
if err != nil {
return result, err
}
var listingID uint64
var registerResult dnfauction.Result
for attempts := 0; attempts < 8; attempts++ {
listingID = gmAuctionNextID(escrow)
registerResult, err = owner.Register(ctx, dnfauction.RegisterCommand{
ListingID: listingID, SellerID: seller.CharacterID,
AccountID: strings.TrimSpace(seller.AccountID), SellerName: seller.Name,
ListType: 0, SourceSlot: sourceSlot, ExpectedID: request.ItemID,
Count: request.Count, UnitPrice: request.UnitPrice,
StartPrice: request.StartPrice, BuyoutPrice: buyoutPrice,
Deposit: 0, FeePercent: dnfauction.SaleFeePercentFor(false),
Duration: time.Duration(durationHours) * time.Hour, ListingLimit: 5,
ExpectedBind: stack.Bind, AllowSealed: false,
})
if !errors.Is(err, dnfauction.ErrListingIDExists) {
break
}
escrow, _, err = repositories.Inventory.Load(ctx, dnfauction.EscrowCharacterID)
if err != nil {
return result, err
}
}
if err != nil {
return result, err
}
result = GMAuctionListResult{
ListingID: registerResult.Listing.ID,
ItemID: registerResult.Listing.Item.ItemID,
Count: registerResult.Listing.Item.Count,
UnitPrice: registerResult.Listing.UnitPrice,
StartPrice: registerResult.Listing.StartPrice,
BuyoutPrice: registerResult.Listing.BuyoutPrice,
Deposit: registerResult.Listing.Deposit,
FeePercent: registerResult.Listing.FeePercent,
ExpiresAt: registerResult.Listing.ExpiresAt.Unix(),
SellerGoldAfter: registerResult.GoldAfter,
}
return result, nil
}
// gmFindStackableSourceSlot 在主背包(list 0)里找一个 item_id 匹配且数量足够
// 的槽位,返回槽号与物品。
func gmFindStackableSourceSlot(slots map[string]dnfrepo.ItemStack, itemID, count int64) (int16, dnfrepo.ItemStack, bool) {
for key, stack := range slots {
parts := strings.SplitN(key, ":", 2)
if len(parts) != 2 || parts[0] != "0" {
continue
}
slot, err := strconv.ParseInt(parts[1], 10, 16)
if err != nil {
continue
}
if stack.ItemID == itemID && stack.Count >= count {
return int16(slot), stack, true
}
}
return 0, dnfrepo.ItemStack{}, false
}
// GMAuctionCancel 下架一件挂单。行为对齐 Owner.Cancel:物品退回卖家(游戏内
// 走邮件),押金不退;若有竞价则退款给竞价者。
func (s *Service) GMAuctionCancel(ctx context.Context, request GMAuctionCancelRequest) (GMAuctionCancelResult, error) {
var result GMAuctionCancelResult
if request.ListingID == 0 {
return result, fmt.Errorf("%w: listing_id is required", ErrGMRequestInvalid)
}
owner, repositories, err := s.gmAuctionOwner()
if err != nil {
return result, err
}
if err := owner.EnsureEscrow(ctx); err != nil {
return result, err
}
// 先读出挂单,拿到卖家与物品信息
listing, err := owner.LoadListing(ctx, request.ListingID)
if err != nil {
return result, err
}
// 卖家账号(Cancel 需要 AccountID 校验归属)
seller, sellerFound, err := repositories.Character.Load(ctx, listing.SellerID)
if err != nil {
return result, err
}
if !sellerFound {
return result, ErrGMCharacterNotFound
}
cancelResult, err := owner.Cancel(ctx, dnfauction.CancelCommand{
SellerID: listing.SellerID,
AccountID: strings.TrimSpace(seller.AccountID),
ListingID: request.ListingID,
})
if err != nil {
return result, err
}
result = GMAuctionCancelResult{
ListingID: request.ListingID,
ItemID: listing.Item.ItemID,
Count: listing.Item.Count,
SellerID: listing.SellerID,
DepositForfeited: listing.Deposit,
}
if cancelResult.Refund.ID != "" {
result.RefundGold = cancelResult.Refund.Gold
result.RefundBidder = cancelResult.Refund.SellerID
}
return result, nil
}
// GMInventoryRow 是 GM 工具「背包管理」里的单行条目(背包行或穿戴行)。
type GMInventoryRow struct {
Collection string `json:"collection"` // "slots"(背包)或 "wear"(穿戴)
EntryKey string `json:"entry_key"` // "list_type:slot" 或 "slot"
ItemID int64 `json:"item_id"`
Count int64 `json:"count"`
Bind bool `json:"bind"`
ItemName string `json:"item_name,omitempty"`
}
// GMInventoryListResult 是列背包的返回。
type GMInventoryListResult struct {
CharacterID string `json:"character_id"`
Rows []GMInventoryRow `json:"rows"`
}
// GMInventoryList 列出角色背包所有行 + 穿戴装备行,供 GM 工具展示与删除。
// 与 GMDeleteItemV2(按 entry_key 删除)配合使用。
func (s *Service) GMInventoryList(ctx context.Context, characterID string) (GMInventoryListResult, error) {
result := GMInventoryListResult{}
if s == nil {
return result, ErrGMItemGrantUnavailable
}
if ctx == nil {
ctx = context.Background()
}
characterID = strings.TrimSpace(characterID)
if characterID == "" {
return result, fmt.Errorf("%w: character_id is required", ErrGMRequestInvalid)
}
result.CharacterID = characterID
repositories, available := s.repositoryGroup()
if !available || repositories.Inventory == nil {
return result, ErrGMRepositoryUnavailable
}
// 物品名解析(失败时留空,前端兜底显示 item_id)
var catalog *pvfDungeonDropCatalog
if c, err := s.currentPVFItemCatalog(); err == nil {
catalog = c
}
itemName := func(itemID int64) string {
if catalog == nil {
return ""
}
if def, err := catalog.ResolveItem(uint32(itemID)); err == nil {
return def.Name
}
return ""
}
// 背包行
inventory, found, err := repositories.Inventory.Load(ctx, characterID)
if err != nil {
return result, err
}
if found {
keys := make([]string, 0, len(inventory.Slots))
for key := range inventory.Slots {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
stack := inventory.Slots[key]
result.Rows = append(result.Rows, GMInventoryRow{
Collection: "slots",
EntryKey: key,
ItemID: stack.ItemID,
Count: stack.Count,
Bind: stack.Bind,
ItemName: itemName(stack.ItemID),
})
}
}
// 穿戴装备行
if repositories.Equipment != nil {
equip, found, err := repositories.Equipment.Load(ctx, characterID)
if err != nil {
return result, err
}
if found {
keys := make([]string, 0, len(equip.Entries))
for key := range equip.Entries {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
entry := equip.Entries[key]
result.Rows = append(result.Rows, GMInventoryRow{
Collection: "wear",
EntryKey: key,
ItemID: entry.ItemID,
Count: 1,
Bind: entry.Bind,
ItemName: itemName(entry.ItemID),
})
}
}
}
return result, nil
}