Files
90CNServer/server/internal/modules/dnf/equip/pet_sync.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

283 lines
9.4 KiB
Go

package equip
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"time"
dnfrepo "longheng.io/server/internal/modules/dnf/repository"
)
// syncPetEquipmentMove runs after the list-7/slot-26 mutation but before its
// CharacterPetUnitOfWork callback returns. It updates only location/equipped
// ownership fields in PetRecord; name, level, experience, satiety, evolution
// mode, and all opaque persisted metadata remain untouched.
func (o *Owner) syncPetEquipmentMove(ctx context.Context, characterID string, result MoveResult) error {
if o == nil || o.pets == nil {
return ErrPetRepositoryRequired
}
petSlot, ok := petInventorySlotFromMoveResult(result)
if !ok {
return fmt.Errorf("%w: result endpoints=(%d,%d)->(%d,%d)",
ErrPetOwnershipMismatch,
result.SourceListType,
result.SourceSlotIndex,
result.DestinationListType,
result.DestinationSlotIndex,
)
}
inventory, equipment, err := o.loadMoveRecords(ctx, characterID)
if err != nil {
return err
}
record, found, err := o.pets.Load(ctx, characterID)
if err != nil {
return err
}
if !found {
return fmt.Errorf("%w: character=%s", ErrPetRecordNotFound, characterID)
}
record = dnfrepo.ClonePet(record)
record.CharacterID = characterID
if len(record.Entries) == 0 {
return fmt.Errorf("%w: character=%s", ErrPetEntryNotFound, characterID)
}
petStack, petStackFound := inventory.Slots[inventoryKey(wireListPet, petSlot)]
equipped, equippedFound := equipment.Entries[entryKey(petCreatureWornSlot)]
if equippedFound && equipped.SlotIndex != petCreatureWornSlot {
return fmt.Errorf("%w: equipped key=%d entry=%d", ErrMoveSlotMismatch, petCreatureWornSlot, equipped.SlotIndex)
}
var inventoryKey string
if petStackFound {
serial := petStackSerial(petStack)
inventoryKey, err = findPetEntryKey(record, serial, petStack.ItemID)
if err != nil {
return err
}
}
var equippedKey string
if equippedFound {
serial := petEquipmentSerial(equipped)
equippedKey, err = findPetEntryKey(record, serial, equipped.ItemID)
if err != nil && errors.Is(err, ErrPetEntryNotFound) {
// A direct creature grant (for example a GM-mail creature item) can
// be equipped before the login-time pet reconciliation has created
// its PetRecord entry. Register it here from the equipped instance so
// the first equip succeeds instead of surfacing as "inventory full".
entry, registerErr := newDirectPetEntry(equipped, serial)
if registerErr != nil {
return err
}
if occupied, exists := record.Entries[entry.PetKey]; exists && occupied.ItemID != entry.ItemID {
return fmt.Errorf("%w: pet key=%s item=%d equipped_item=%d",
ErrPetOwnershipMismatch, entry.PetKey, occupied.ItemID, entry.ItemID)
}
record.Entries[entry.PetKey] = entry
equippedKey = entry.PetKey
} else if err != nil {
return err
}
}
if inventoryKey != "" && inventoryKey == equippedKey {
return fmt.Errorf("%w: pet=%s occupies list7 and slot%d", ErrPetOwnershipMismatch, inventoryKey, petCreatureWornSlot)
}
if err := validatePetMoveProjection(
result,
record.EquippedKey,
inventoryKey,
equippedKey,
petStackFound,
equippedFound,
petStack.ItemID,
equipped.ItemID,
); err != nil {
return err
}
if petStackFound {
entry := record.Entries[inventoryKey]
entry.SourceListType = wireListPet
entry.SourceSlotIndex = petSlot
record.Entries[inventoryKey] = entry
}
if equippedFound {
entry := record.Entries[equippedKey]
entry.SourceListType = wireListEquipment
entry.SourceSlotIndex = petCreatureWornSlot
record.Entries[equippedKey] = entry
record.EquippedKey = equippedKey
} else {
record.EquippedKey = ""
}
record.UpdatedAt = time.Now()
return dnfrepo.SavePetFields(ctx, o.pets, record, dnfrepo.PetFieldEntries, dnfrepo.PetFieldEquipped)
}
func petInventorySlotFromMoveResult(result MoveResult) (int16, bool) {
if result.SourceListType == wireListPet && result.DestinationListType != wireListPet {
return result.SourceSlotIndex, result.SourceSlotIndex >= 0
}
if result.DestinationListType == wireListPet && result.SourceListType != wireListPet {
return result.DestinationSlotIndex, result.DestinationSlotIndex >= 0
}
return 0, false
}
func validatePetMoveProjection(
result MoveResult,
previousEquippedKey string,
inventoryKey string,
equippedKey string,
inventoryFound bool,
equippedFound bool,
inventoryItemID int64,
equippedItemID int64,
) error {
previousEquippedKey = strings.TrimSpace(previousEquippedKey)
switch result.Mode {
case "equip":
if inventoryFound || !equippedFound || result.ItemID <= 0 || result.SwappedItemID != 0 || equippedItemID != result.ItemID {
return fmt.Errorf("%w: invalid equip projection", ErrPetOwnershipMismatch)
}
if previousEquippedKey != "" {
return fmt.Errorf("%w: persisted equipped=%s but slot%d was empty", ErrPetOwnershipMismatch, previousEquippedKey, petCreatureWornSlot)
}
case "equip_swap":
if !inventoryFound || !equippedFound || inventoryKey == "" || equippedKey == "" || result.SwappedItemID <= 0 ||
inventoryItemID != result.SwappedItemID || equippedItemID != result.ItemID {
return fmt.Errorf("%w: invalid equip swap projection", ErrPetOwnershipMismatch)
}
if previousEquippedKey != "" && previousEquippedKey != inventoryKey {
return fmt.Errorf("%w: persisted equipped=%s swapped=%s", ErrPetOwnershipMismatch, previousEquippedKey, inventoryKey)
}
case "unequip":
if !inventoryFound || equippedFound || inventoryKey == "" || result.ItemID <= 0 || result.SwappedItemID != 0 || inventoryItemID != result.ItemID {
return fmt.Errorf("%w: invalid unequip projection", ErrPetOwnershipMismatch)
}
if previousEquippedKey != "" && previousEquippedKey != inventoryKey {
return fmt.Errorf("%w: persisted equipped=%s removed=%s", ErrPetOwnershipMismatch, previousEquippedKey, inventoryKey)
}
case "unequip_swap":
if !inventoryFound || !equippedFound || inventoryKey == "" || equippedKey == "" || result.SwappedItemID <= 0 ||
inventoryItemID != result.ItemID || equippedItemID != result.SwappedItemID {
return fmt.Errorf("%w: invalid unequip swap projection", ErrPetOwnershipMismatch)
}
if previousEquippedKey != "" && previousEquippedKey != inventoryKey {
return fmt.Errorf("%w: persisted equipped=%s removed=%s", ErrPetOwnershipMismatch, previousEquippedKey, inventoryKey)
}
default:
return fmt.Errorf("%w: unsupported pet move mode=%q", ErrPetOwnershipMismatch, result.Mode)
}
return nil
}
func newDirectPetEntry(equipment dnfrepo.EquipmentEntry, serial int64) (dnfrepo.PetEntry, error) {
if serial <= 0 {
serial = firstExtraInt(equipment.Extra, 0, "item_serial", "item_uid")
}
if serial <= 0 || serial > int64(^uint32(0)) || equipment.ItemID <= 0 {
return dnfrepo.PetEntry{}, fmt.Errorf("%w: serial=%d item=%d", ErrPetOwnershipMismatch, serial, equipment.ItemID)
}
key := strconv.FormatInt(serial, 10)
return dnfrepo.PetEntry{
PetKey: key,
CreatureKey: uint32(serial),
ItemID: equipment.ItemID,
SourceListType: wireListEquipment,
SourceSlotIndex: petCreatureWornSlot,
Satiety: 100,
SatietyMicros: 100000000,
Level: 1,
Extra: cloneExtra(equipment.Extra),
}, nil
}
func petStackSerial(stack dnfrepo.ItemStack) int64 {
return firstExtraInt(stack.Extra, 0,
"creature_serial_or_handle",
"creature_serial",
"pet_serial",
"serial",
"handle",
"instance_value",
"item_uid",
"item_serial",
)
}
func petEquipmentSerial(entry dnfrepo.EquipmentEntry) int64 {
if serial := petSerialFromEquippedRaw(entry.RawEntry); serial > 0 {
return serial
}
return firstExtraInt(entry.Extra, 0,
"creature_serial_or_handle",
"creature_serial",
"pet_serial",
"serial",
"handle",
"instance_value",
"item_uid",
"item_serial",
)
}
func findPetEntryKey(record dnfrepo.PetRecord, serial int64, itemID int64) (string, error) {
if serial <= 0 || serial > int64(^uint32(0)) || itemID <= 0 {
return "", fmt.Errorf("%w: serial=%d item=%d", ErrPetOwnershipMismatch, serial, itemID)
}
directKey := strconv.FormatInt(serial, 10)
if entry, ok := record.Entries[directKey]; ok {
entrySerial, valid := petEntrySerial(entry)
if entry.ItemID != itemID || (valid && entrySerial != serial) {
return "", fmt.Errorf("%w: key=%s serial=%d item=%d", ErrPetOwnershipMismatch, directKey, serial, itemID)
}
}
matchedKey := ""
for key, entry := range record.Entries {
entrySerial, valid := petEntrySerial(entry)
if key == directKey && !valid {
entrySerial, valid = serial, true
}
if !valid || entrySerial != serial {
continue
}
if entry.ItemID != itemID {
return "", fmt.Errorf("%w: serial=%d inventory_item=%d pet_item=%d", ErrPetOwnershipMismatch, serial, itemID, entry.ItemID)
}
if matchedKey != "" {
return "", fmt.Errorf("%w: duplicate serial=%d keys=%s,%s", ErrPetOwnershipMismatch, serial, matchedKey, key)
}
matchedKey = key
}
if matchedKey == "" {
return "", fmt.Errorf("%w: serial=%d item=%d", ErrPetEntryNotFound, serial, itemID)
}
return matchedKey, nil
}
func petEntrySerial(entry dnfrepo.PetEntry) (int64, bool) {
if entry.CreatureKey != 0 {
return int64(entry.CreatureKey), true
}
if parsed, err := strconv.ParseUint(strings.TrimSpace(entry.PetKey), 0, 32); err == nil && parsed != 0 {
return int64(parsed), true
}
value := firstExtraInt(entry.Extra, 0,
"creature_serial_or_handle",
"creature_serial",
"pet_serial",
"serial",
"handle",
"instance_value",
"item_uid",
)
return value, value > 0 && value <= int64(^uint32(0))
}