DNF90 服务端与启动器完整源码(含 GM 工具功能增强)

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

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

No files matched your search

@@ -0,0 +1,411 @@
// Package crystalcontract owns the durable rules for 晶之契约: the selected
// cube family and the atomic one-cube consumption reported by the current EXE.
package crystalcontract
import (
"context"
"encoding/binary"
"errors"
"fmt"
"strconv"
"strings"
"time"
"longheng.io/server/internal/modules/dnf/premium"
dnfrepo "longheng.io/server/internal/modules/dnf/repository"
)
const (
SelectionNone int8 = -1
SelectionCount = 6
selectionStatKey = "premium_crystal_selection"
)
var (
ErrRepositoriesUnavailable = errors.New("crystal contract repositories are unavailable")
ErrAccountRequired = errors.New("crystal contract account is required")
ErrCharacterRequired = errors.New("crystal contract character is required")
ErrAccountNotFound = errors.New("crystal contract account was not found")
ErrCharacterNotFound = errors.New("crystal contract character was not found")
ErrCharacterOwnerMismatch = errors.New("crystal contract character owner mismatch")
ErrContractInactive = errors.New("crystal contract is inactive")
ErrSelectionInvalid = errors.New("crystal contract selection is invalid")
ErrCubeUnavailable = errors.New("selected crystal contract cube is unavailable")
ErrCubeRequestMismatch = errors.New("crystal contract cube request does not match the selected cube")
)
// Catalog is the six current-client cube item IDs in native selection order:
// black, white, red, blue, clear, gold. The bridge builds it from the active
// runtime PVF rather than accepting item IDs from a request.
type Catalog struct {
cubeItemIDs [SelectionCount]int64
}
func NewCatalog(cubeItemIDs [SelectionCount]int64) (Catalog, error) {
seen := make(map[int64]struct{}, SelectionCount)
for index, itemID := range cubeItemIDs {
if itemID <= 0 {
return Catalog{}, fmt.Errorf("%w: index=%d item=%d", ErrSelectionInvalid, index, itemID)
}
if _, duplicate := seen[itemID]; duplicate {
return Catalog{}, fmt.Errorf("%w: duplicate item=%d", ErrSelectionInvalid, itemID)
}
seen[itemID] = struct{}{}
}
return Catalog{cubeItemIDs: cubeItemIDs}, nil
}
func (c Catalog) ItemID(selection int8) (int64, bool) {
if selection < 0 || int(selection) >= len(c.cubeItemIDs) {
return 0, false
}
itemID := c.cubeItemIDs[selection]
return itemID, itemID > 0
}
func (c Catalog) Selection(itemID int64) (int8, bool) {
for index, candidate := range c.cubeItemIDs {
if candidate == itemID && candidate > 0 {
return int8(index), true
}
}
return SelectionNone, false
}
// AccountSlot returns the current-client crystal warehouse slot for one
// selection. The client fixes the six cube families at list-0 slots 354..359
// in the same black, white, red, blue, clear, gold order as the PVF catalog.
func (c Catalog) AccountSlot(selection int8) (int16, bool) {
if _, valid := c.ItemID(selection); !valid {
return 0, false
}
return dnfrepo.CrystalWarehouseFirstSlot + int16(selection), true
}
type State struct {
Active bool
Selection int8
CubeItemID int64
}
type ConsumeResult struct {
ItemID int64
SlotIndex uint16
Consumed int64
Remaining int64
SelectionAfter int8
}
type Owner struct {
repositories dnfrepo.Group
catalog Catalog
}
func NewOwner(repositories dnfrepo.Group, catalog Catalog) (*Owner, error) {
if repositories.Account == nil ||
repositories.AccountInventory == nil ||
repositories.Character == nil ||
repositories.AccountAssets == nil {
return nil, ErrRepositoriesUnavailable
}
return &Owner{repositories: repositories, catalog: catalog}, nil
}
func (o *Owner) State(
ctx context.Context,
accountID string,
characterID string,
now time.Time,
) (State, error) {
accountID, characterID, err := normalizeOwnerIDs(accountID, characterID)
if err != nil {
return State{}, err
}
account, found, err := o.repositories.Account.Load(ctx, accountID)
if err != nil {
return State{}, err
}
if !found {
return State{}, ErrAccountNotFound
}
character, found, err := o.repositories.Character.Load(ctx, characterID)
if err != nil {
return State{}, err
}
if !found {
return State{}, ErrCharacterNotFound
}
if strings.TrimSpace(character.AccountID) != accountID {
return State{}, ErrCharacterOwnerMismatch
}
active := premium.Active(account, premium.TypeCrystal, now)
if !active {
return State{Active: false, Selection: SelectionNone}, nil
}
selection := storedSelection(character)
itemID, valid := o.catalog.ItemID(selection)
if !valid {
return State{Active: true, Selection: SelectionNone}, nil
}
accountInventory, found, err := o.repositories.AccountInventory.Load(ctx, accountID)
if err != nil {
return State{}, err
}
if !found || !accountInventoryHasSelectedCube(accountInventory, o.catalog, selection, itemID) {
return State{Active: true, Selection: SelectionNone}, nil
}
return State{Active: true, Selection: selection, CubeItemID: itemID}, nil
}
func (o *Owner) Select(
ctx context.Context,
accountID string,
characterID string,
selection int8,
now time.Time,
) (State, error) {
accountID, characterID, err := normalizeOwnerIDs(accountID, characterID)
if err != nil {
return State{}, err
}
if selection != SelectionNone {
if _, valid := o.catalog.ItemID(selection); !valid {
return State{}, ErrSelectionInvalid
}
}
account, found, err := o.repositories.Account.Load(ctx, accountID)
if err != nil {
return State{}, err
}
if !found {
return State{}, ErrAccountNotFound
}
active := premium.Active(account, premium.TypeCrystal, now)
if selection != SelectionNone && !active {
return State{}, ErrContractInactive
}
result := State{Active: active, Selection: selection}
err = o.repositories.AccountAssets.WithinAccountCharacterAssets(
ctx,
accountID,
characterID,
func(
accountInventories dnfrepo.AccountInventoryRepository,
characters dnfrepo.CharacterRepository,
_ dnfrepo.InventoryRepository,
_ dnfrepo.EquipmentRepository,
) error {
character, characterFound, loadErr := characters.Load(ctx, characterID)
if loadErr != nil {
return loadErr
}
if !characterFound {
return ErrCharacterNotFound
}
if strings.TrimSpace(character.AccountID) != accountID {
return ErrCharacterOwnerMismatch
}
if selection != SelectionNone {
itemID, _ := o.catalog.ItemID(selection)
accountInventory, inventoryFound, inventoryErr := accountInventories.Load(ctx, accountID)
if inventoryErr != nil {
return inventoryErr
}
if !inventoryFound || !accountInventoryHasSelectedCube(accountInventory, o.catalog, selection, itemID) {
return ErrCubeUnavailable
}
result.CubeItemID = itemID
}
storeSelection(&character, selection)
character.UpdatedAt = now
return dnfrepo.SaveCharacterFields(ctx, characters, character, dnfrepo.CharacterFieldStats)
},
)
if err != nil {
return State{}, err
}
return result, nil
}
func (o *Owner) Consume(
ctx context.Context,
accountID string,
characterID string,
slotIndex uint16,
itemID int64,
now time.Time,
) (ConsumeResult, error) {
return o.consume(ctx, accountID, characterID, &slotIndex, &itemID, now)
}
// ConsumeSelected atomically resolves the character's durable Crystal
// Contract selection and consumes one matching account-shared cube. The
// current client applies the contract effect locally but does not send a
// consumption command, so dungeon lifecycle ticks must not trust client item
// or slot fields.
func (o *Owner) ConsumeSelected(
ctx context.Context,
accountID string,
characterID string,
now time.Time,
) (ConsumeResult, error) {
return o.consume(ctx, accountID, characterID, nil, nil, now)
}
func (o *Owner) consume(
ctx context.Context,
accountID string,
characterID string,
expectedSlot *uint16,
expectedItemID *int64,
now time.Time,
) (ConsumeResult, error) {
accountID, characterID, err := normalizeOwnerIDs(accountID, characterID)
if err != nil {
return ConsumeResult{}, err
}
account, found, err := o.repositories.Account.Load(ctx, accountID)
if err != nil {
return ConsumeResult{}, err
}
if !found {
return ConsumeResult{}, ErrAccountNotFound
}
if !premium.Active(account, premium.TypeCrystal, now) {
return ConsumeResult{}, ErrContractInactive
}
result := ConsumeResult{Consumed: 1, SelectionAfter: SelectionNone}
err = o.repositories.AccountAssets.WithinAccountCharacterAssets(
ctx,
accountID,
characterID,
func(
accountInventories dnfrepo.AccountInventoryRepository,
characters dnfrepo.CharacterRepository,
_ dnfrepo.InventoryRepository,
_ dnfrepo.EquipmentRepository,
) error {
character, characterFound, loadErr := characters.Load(ctx, characterID)
if loadErr != nil {
return loadErr
}
if !characterFound {
return ErrCharacterNotFound
}
if strings.TrimSpace(character.AccountID) != accountID {
return ErrCharacterOwnerMismatch
}
selection := storedSelection(character)
selectedItemID, valid := o.catalog.ItemID(selection)
if !valid {
return ErrCubeRequestMismatch
}
selectedSlot, valid := o.catalog.AccountSlot(selection)
if !valid {
return ErrCubeRequestMismatch
}
if expectedItemID != nil && selectedItemID != *expectedItemID {
return ErrCubeRequestMismatch
}
if expectedSlot != nil && uint16(selectedSlot) != *expectedSlot {
return ErrCubeRequestMismatch
}
result.ItemID = selectedItemID
result.SlotIndex = uint16(selectedSlot)
accountInventory, inventoryFound, inventoryErr := accountInventories.Load(ctx, accountID)
if inventoryErr != nil {
return inventoryErr
}
if !inventoryFound {
return ErrCubeUnavailable
}
key := dnfrepo.AccountSharedInventorySlotKey(selectedSlot)
stack, stackFound := accountInventory.Slots[key]
if !stackFound || stack.ItemID != selectedItemID || stack.Count <= 0 {
return ErrCubeUnavailable
}
stack.Count--
result.Remaining = stack.Count
if stack.Count == 0 {
delete(accountInventory.Slots, key)
} else {
refreshStackCount(&stack)
accountInventory.Slots[key] = stack
}
accountInventory.UpdatedAt = now
if err := accountInventories.Save(ctx, accountInventory); err != nil {
return err
}
if stack.Count > 0 {
result.SelectionAfter = selection
return nil
}
storeSelection(&character, SelectionNone)
character.UpdatedAt = now
return dnfrepo.SaveCharacterFields(ctx, characters, character, dnfrepo.CharacterFieldStats)
},
)
if err != nil {
return ConsumeResult{}, err
}
return result, nil
}
func normalizeOwnerIDs(accountID string, characterID string) (string, string, error) {
accountID = strings.TrimSpace(accountID)
characterID = strings.TrimSpace(characterID)
if accountID == "" {
return "", "", ErrAccountRequired
}
if characterID == "" {
return "", "", ErrCharacterRequired
}
return accountID, characterID, nil
}
func storedSelection(character dnfrepo.CharacterRecord) int8 {
if character.Stats == nil {
return SelectionNone
}
value, found := character.Stats[selectionStatKey]
if !found || value < 0 || value >= SelectionCount {
return SelectionNone
}
return int8(value)
}
func storeSelection(character *dnfrepo.CharacterRecord, selection int8) {
if character.Stats == nil {
character.Stats = make(map[string]int64, 8)
}
character.Stats[selectionStatKey] = int64(selection)
}
func accountInventoryHasSelectedCube(
inventory dnfrepo.AccountInventoryRecord,
catalog Catalog,
selection int8,
itemID int64,
) bool {
slot, valid := catalog.AccountSlot(selection)
if !valid {
return false
}
stack, found := inventory.Slots[dnfrepo.AccountSharedInventorySlotKey(slot)]
return found && stack.ItemID == itemID && stack.Count > 0
}
func refreshStackCount(stack *dnfrepo.ItemStack) {
if stack == nil {
return
}
if len(stack.RawEntry) >= 10 {
binary.LittleEndian.PutUint32(stack.RawEntry[6:10], uint32(stack.Count))
}
if stack.Extra != nil {
stack.Extra["count"] = strconv.FormatInt(stack.Count, 10)
}
}
@@ -0,0 +1,218 @@
package crystalcontract
import (
"context"
"errors"
"testing"
"time"
"longheng.io/server/internal/modules/dnf/premium"
dnfrepo "longheng.io/server/internal/modules/dnf/repository"
dnfrepomemory "longheng.io/server/internal/modules/dnf/repository/memory"
)
func newOwnerTestRuntime(
t *testing.T,
active bool,
slots map[string]dnfrepo.ItemStack,
) (*Owner, dnfrepo.Group, time.Time) {
t.Helper()
repositories := dnfrepomemory.NewMemoryGroup()
now := time.Date(2026, time.July, 27, 12, 0, 0, 0, time.UTC)
account := dnfrepo.AccountRecord{
AccountID: "account-1",
Metadata: make(map[string]string),
}
if active {
premium.Upsert(&account, premium.TypeCrystal, int64(time.Hour/time.Second), 1, now)
}
ctx := context.Background()
if err := repositories.Account.Save(ctx, account); err != nil {
t.Fatal(err)
}
if err := repositories.Character.Save(ctx, dnfrepo.CharacterRecord{
CharacterID: "19",
AccountID: account.AccountID,
Stats: make(map[string]int64),
}); err != nil {
t.Fatal(err)
}
if err := repositories.AccountInventory.Save(ctx, dnfrepo.AccountInventoryRecord{
AccountID: account.AccountID,
Slots: slots,
}); err != nil {
t.Fatal(err)
}
if err := repositories.Inventory.Save(ctx, dnfrepo.InventoryRecord{
CharacterID: "19",
Slots: make(map[string]dnfrepo.ItemStack),
}); err != nil {
t.Fatal(err)
}
if err := repositories.Equipment.Save(ctx, dnfrepo.EquipmentRecord{
CharacterID: "19",
Entries: make(map[string]dnfrepo.EquipmentEntry),
}); err != nil {
t.Fatal(err)
}
catalog, err := NewCatalog([SelectionCount]int64{3033, 3034, 3035, 3036, 3037, 3262})
if err != nil {
t.Fatal(err)
}
owner, err := NewOwner(repositories, catalog)
if err != nil {
t.Fatal(err)
}
return owner, repositories, now
}
func testCrystalSlot(selection int8) string {
return dnfrepo.AccountSharedInventorySlotKey(dnfrepo.CrystalWarehouseFirstSlot + int16(selection))
}
func TestSelectRequiresActiveContractAndOwnedCube(t *testing.T) {
owner, _, now := newOwnerTestRuntime(t, false, map[string]dnfrepo.ItemStack{
testCrystalSlot(2): {ItemID: 3035, Count: 2},
})
if _, err := owner.Select(context.Background(), "account-1", "19", 2, now); !errors.Is(err, ErrContractInactive) {
t.Fatalf("inactive selection error = %v, want %v", err, ErrContractInactive)
}
owner, _, now = newOwnerTestRuntime(t, true, map[string]dnfrepo.ItemStack{})
if _, err := owner.Select(context.Background(), "account-1", "19", 2, now); !errors.Is(err, ErrCubeUnavailable) {
t.Fatalf("missing cube selection error = %v, want %v", err, ErrCubeUnavailable)
}
}
func TestSelectPersistsRuntimePVFSelectionAndState(t *testing.T) {
owner, repositories, now := newOwnerTestRuntime(t, true, map[string]dnfrepo.ItemStack{
testCrystalSlot(4): {ItemID: 3037, Count: 12},
})
state, err := owner.Select(context.Background(), "account-1", "19", 4, now)
if err != nil {
t.Fatal(err)
}
if !state.Active || state.Selection != 4 || state.CubeItemID != 3037 {
t.Fatalf("selected state = %+v", state)
}
loaded, err := owner.State(context.Background(), "account-1", "19", now)
if err != nil {
t.Fatal(err)
}
if loaded != state {
t.Fatalf("loaded state = %+v, want %+v", loaded, state)
}
character, found, err := repositories.Character.Load(context.Background(), "19")
if err != nil || !found {
t.Fatalf("load character found=%t err=%v", found, err)
}
if got := storedSelection(character); got != 4 {
t.Fatalf("stored selection = %d, want 4", got)
}
}
func TestConsumeAtomicallyDecrementsSelectedCubeAndClearsLastSelection(t *testing.T) {
owner, repositories, now := newOwnerTestRuntime(t, true, map[string]dnfrepo.ItemStack{
testCrystalSlot(0): {ItemID: 3033, Count: 1},
})
if _, err := owner.Select(context.Background(), "account-1", "19", 0, now); err != nil {
t.Fatal(err)
}
result, err := owner.Consume(context.Background(), "account-1", "19", 354, 3033, now)
if err != nil {
t.Fatal(err)
}
if result.ItemID != 3033 || result.Consumed != 1 || result.Remaining != 0 ||
result.SelectionAfter != SelectionNone {
t.Fatalf("consume result = %+v", result)
}
inventory, found, err := repositories.AccountInventory.Load(context.Background(), "account-1")
if err != nil || !found {
t.Fatalf("load inventory found=%t err=%v", found, err)
}
if _, exists := inventory.Slots[testCrystalSlot(0)]; exists {
t.Fatalf("last cube stack was not deleted: %+v", inventory.Slots)
}
character, found, err := repositories.Character.Load(context.Background(), "19")
if err != nil || !found {
t.Fatalf("load character found=%t err=%v", found, err)
}
if got := storedSelection(character); got != SelectionNone {
t.Fatalf("stored selection = %d, want none", got)
}
}
func TestConsumeKeepsSelectionWhileSelectedCubeStackRemains(t *testing.T) {
owner, repositories, now := newOwnerTestRuntime(t, true, map[string]dnfrepo.ItemStack{
testCrystalSlot(5): {ItemID: 3262, Count: 3, RawEntry: make([]byte, 10), Extra: map[string]string{"count": "3"}},
})
if _, err := owner.Select(context.Background(), "account-1", "19", 5, now); err != nil {
t.Fatal(err)
}
result, err := owner.Consume(context.Background(), "account-1", "19", 359, 3262, now)
if err != nil {
t.Fatal(err)
}
if result.SelectionAfter != 5 {
t.Fatalf("selection after = %d, want 5", result.SelectionAfter)
}
accountInventory, found, err := repositories.AccountInventory.Load(context.Background(), "account-1")
if err != nil || !found {
t.Fatalf("load account inventory found=%t err=%v", found, err)
}
stack := accountInventory.Slots[testCrystalSlot(5)]
if stack.Count != 2 || stack.Extra["count"] != "2" {
t.Fatalf("remaining stack = %+v, want count 2", stack)
}
}
func TestConsumeRejectsCubeThatDoesNotMatchPersistedSelection(t *testing.T) {
owner, repositories, now := newOwnerTestRuntime(t, true, map[string]dnfrepo.ItemStack{
testCrystalSlot(0): {ItemID: 3033, Count: 4},
testCrystalSlot(1): {ItemID: 3034, Count: 4},
})
if _, err := owner.Select(context.Background(), "account-1", "19", 0, now); err != nil {
t.Fatal(err)
}
if _, err := owner.Consume(context.Background(), "account-1", "19", 355, 3034, now); !errors.Is(err, ErrCubeRequestMismatch) {
t.Fatalf("mismatched consume error = %v, want %v", err, ErrCubeRequestMismatch)
}
inventory, found, err := repositories.AccountInventory.Load(context.Background(), "account-1")
if err != nil || !found {
t.Fatalf("load inventory found=%t err=%v", found, err)
}
if got := inventory.Slots[testCrystalSlot(1)].Count; got != 4 {
t.Fatalf("mismatched cube count = %d, want 4", got)
}
}
func TestExpiredContractStateSuppressesStoredSelection(t *testing.T) {
owner, _, now := newOwnerTestRuntime(t, true, map[string]dnfrepo.ItemStack{
testCrystalSlot(3): {ItemID: 3036, Count: 4},
})
if _, err := owner.Select(context.Background(), "account-1", "19", 3, now); err != nil {
t.Fatal(err)
}
state, err := owner.State(context.Background(), "account-1", "19", now.Add(2*time.Hour))
if err != nil {
t.Fatal(err)
}
if state.Active || state.Selection != SelectionNone || state.CubeItemID != 0 {
t.Fatalf("expired state = %+v", state)
}
}
func TestSelectDoesNotAcceptCubeFromCharacterMainBag(t *testing.T) {
owner, repositories, now := newOwnerTestRuntime(t, true, nil)
if err := repositories.Inventory.Save(context.Background(), dnfrepo.InventoryRecord{
CharacterID: "19",
Slots: map[string]dnfrepo.ItemStack{
"0:8": {ItemID: 3037, Count: 12},
},
}); err != nil {
t.Fatal(err)
}
if _, err := owner.Select(context.Background(), "account-1", "19", 4, now); !errors.Is(err, ErrCubeUnavailable) {
t.Fatalf("main-bag-only selection error = %v, want %v", err, ErrCubeUnavailable)
}
}