DNF90 服务端与启动器完整源码(含 GM 工具功能增强)
服务端(Go): - GM 塞背包/带属性装备发放:在线角色推送 op14 增量物品行,背包实时刷新,无需回角色选择界面 - GM 改金币/改等级:去掉在线拒绝,落库后推送 op14 钱包/op37 等级状态,在线实时刷新 - 等级端点统一切换到新版 GMSetCharacterLevel(自带在线刷新) - 无限/锁死疲劳模式 + 燃烧疲劳:每房间消耗 2 点、打怪与通关经验双倍 - 燃烧疲劳开关时给所有在线角色推送系统提示(op12 mode 0) - 深渊史诗掉率:GM 全局倍率 + 城镇增益药剂窗口倍率,真实接入掉落结算 启动器(Python/JS): - 标题栏自绘、窗口 1204x717、禁止拖拽改大小、仅最小化/关闭 - GM 工具:疲劳与燃烧、深渊史诗掉率、拍卖行上架(单一定价对齐) - 拍卖行上架前端对齐服务端单一价格语义,修复 400 报错
This commit is contained in:
commit
645b64fe21
4388 files changed
+1405457
No files matched your search
@@ -0,0 +1,189 @@
|
||||
package npcshop
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
dnfrepo "longheng.io/server/internal/modules/dnf/repository"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrOwnerUnavailable = errors.New("NPC shop owner is unavailable")
|
||||
ErrAccountRequired = errors.New("NPC shop account id is required")
|
||||
ErrAccountNotFound = errors.New("NPC shop account is not found")
|
||||
ErrCharacterRequired = errors.New("NPC shop character id is required")
|
||||
ErrCharacterNotFound = errors.New("NPC shop character is not found")
|
||||
ErrAccountMismatch = errors.New("NPC shop character account mismatch")
|
||||
ErrInventoryNotFound = errors.New("NPC shop inventory is not found")
|
||||
ErrProjectorRequired = errors.New("NPC shop mutation projector is required")
|
||||
// ErrShopPurchaseTransactionUnavailable 表示仓储没有实现限购所需的
|
||||
// ShopPurchaseAssetUnitOfWork 边界;此时不能安全地落账号维度计数。
|
||||
ErrShopPurchaseTransactionUnavailable = errors.New("NPC shop purchase transaction is unavailable")
|
||||
)
|
||||
|
||||
type Assets struct {
|
||||
// Account 是账号记录本身,限购计数放在它的 Metadata 上(PVF
|
||||
// `[purchase limit] `account`` 的已购次数是账号维度状态)。
|
||||
Account *dnfrepo.AccountRecord
|
||||
AccountInventory *dnfrepo.AccountInventoryRecord
|
||||
Character *dnfrepo.CharacterRecord
|
||||
Inventory *dnfrepo.InventoryRecord
|
||||
}
|
||||
|
||||
type Changes struct {
|
||||
Account bool
|
||||
AccountInventory bool
|
||||
Character bool
|
||||
Inventory bool
|
||||
}
|
||||
|
||||
type Projector func(*Assets) (Changes, error)
|
||||
|
||||
type Command struct {
|
||||
AccountID string
|
||||
CharacterID string
|
||||
UpdatedAt time.Time
|
||||
Project Projector
|
||||
}
|
||||
|
||||
// Owner owns NPC-shop authorization and the atomic account warehouse,
|
||||
// character wallet, and character inventory commit. Current-client packet
|
||||
// rows and PVF projections remain in the bridge.
|
||||
type Owner struct {
|
||||
assets dnfrepo.AccountCharacterAssetUnitOfWork
|
||||
}
|
||||
|
||||
func NewOwner(repositories dnfrepo.Group) (*Owner, error) {
|
||||
if repositories.AccountAssets == nil {
|
||||
return nil, ErrOwnerUnavailable
|
||||
}
|
||||
return &Owner{assets: repositories.AccountAssets}, nil
|
||||
}
|
||||
|
||||
func (o *Owner) Mutate(ctx context.Context, command Command) error {
|
||||
if o == nil || o.assets == nil {
|
||||
return ErrOwnerUnavailable
|
||||
}
|
||||
accountID := strings.TrimSpace(command.AccountID)
|
||||
characterID := strings.TrimSpace(command.CharacterID)
|
||||
if accountID == "" {
|
||||
return ErrAccountRequired
|
||||
}
|
||||
if characterID == "" {
|
||||
return ErrCharacterRequired
|
||||
}
|
||||
if command.Project == nil {
|
||||
return ErrProjectorRequired
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
now := command.UpdatedAt.UTC()
|
||||
if now.IsZero() {
|
||||
now = time.Now().UTC()
|
||||
}
|
||||
|
||||
// 限购计数分两个作用域:角色维度落在角色记录 Stats,账号维度落在账号记录
|
||||
// Metadata。两者都必须与钱包/背包变更同事务提交,所以这里要求仓储实现
|
||||
// ShopPurchaseAssetUnitOfWork(AccountCharacterAssetUnitOfWork 的扩展)。
|
||||
shopAssets, ok := o.assets.(dnfrepo.ShopPurchaseAssetUnitOfWork)
|
||||
if !ok {
|
||||
return ErrShopPurchaseTransactionUnavailable
|
||||
}
|
||||
return shopAssets.WithinShopPurchaseAssets(ctx, accountID, characterID, func(
|
||||
account dnfrepo.AccountRepository,
|
||||
accountInventories dnfrepo.AccountInventoryRepository,
|
||||
characters dnfrepo.CharacterRepository,
|
||||
inventories dnfrepo.InventoryRepository,
|
||||
) error {
|
||||
if account == nil || accountInventories == nil || characters == nil || inventories == nil {
|
||||
return ErrOwnerUnavailable
|
||||
}
|
||||
// 先校验角色归属(账号不匹配要在读账号记录之前就拒绝),再读账号记录。
|
||||
character, found, err := characters.Load(ctx, characterID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !found || strings.TrimSpace(character.CharacterID) != characterID {
|
||||
return fmt.Errorf("%w: character=%s", ErrCharacterNotFound, characterID)
|
||||
}
|
||||
if strings.TrimSpace(character.AccountID) != accountID {
|
||||
return fmt.Errorf("%w: account=%s character=%s", ErrAccountMismatch, accountID, characterID)
|
||||
}
|
||||
accountRecord, found, err := account.Load(ctx, accountID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !found {
|
||||
return fmt.Errorf("%w: account=%s", ErrAccountNotFound, accountID)
|
||||
}
|
||||
inventory, found, err := inventories.Load(ctx, characterID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !found || strings.TrimSpace(inventory.CharacterID) != characterID {
|
||||
return fmt.Errorf("%w: character=%s", ErrInventoryNotFound, characterID)
|
||||
}
|
||||
accountInventory, _, err := accountInventories.Load(ctx, accountID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
accountRecord = dnfrepo.CloneAccount(accountRecord)
|
||||
accountRecord.AccountID = accountID
|
||||
if accountRecord.Metadata == nil {
|
||||
accountRecord.Metadata = make(map[string]string)
|
||||
}
|
||||
character = dnfrepo.CloneCharacter(character)
|
||||
inventory = dnfrepo.CloneInventory(inventory)
|
||||
accountInventory = dnfrepo.CloneAccountInventory(accountInventory)
|
||||
accountInventory.AccountID = accountID
|
||||
if inventory.Slots == nil {
|
||||
inventory.Slots = make(map[string]dnfrepo.ItemStack)
|
||||
}
|
||||
if accountInventory.Slots == nil {
|
||||
accountInventory.Slots = make(map[string]dnfrepo.ItemStack)
|
||||
}
|
||||
|
||||
changes, err := command.Project(&Assets{
|
||||
Account: &accountRecord,
|
||||
AccountInventory: &accountInventory,
|
||||
Character: &character,
|
||||
Inventory: &inventory,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if changes.Account {
|
||||
accountRecord.UpdatedAt = now
|
||||
if err := account.Save(ctx, accountRecord); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if changes.Character {
|
||||
character.UpdatedAt = now
|
||||
if err := dnfrepo.SaveCharacterFields(ctx, characters, character, dnfrepo.CharacterFieldStats); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if changes.Inventory {
|
||||
inventory.UpdatedAt = now
|
||||
if err := dnfrepo.SaveInventoryFields(ctx, inventories, inventory, dnfrepo.InventoryFieldSlots); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if changes.AccountInventory {
|
||||
accountInventory.UpdatedAt = now
|
||||
if err := accountInventories.Save(ctx, accountInventory); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package npcshop
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
dnfrepo "longheng.io/server/internal/modules/dnf/repository"
|
||||
dnfrepomemory "longheng.io/server/internal/modules/dnf/repository/memory"
|
||||
)
|
||||
|
||||
func TestOwnerMutateCommitsSelectedNPCShopAssets(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repositories := dnfrepomemory.NewMemoryGroup()
|
||||
seedNPCShopOwner(t, ctx, repositories)
|
||||
owner, err := NewOwner(repositories)
|
||||
if err != nil {
|
||||
t.Fatalf("NewOwner error = %v", err)
|
||||
}
|
||||
|
||||
err = owner.Mutate(ctx, Command{
|
||||
AccountID: "dnf:1",
|
||||
CharacterID: "77",
|
||||
Project: func(assets *Assets) (Changes, error) {
|
||||
assets.Character.Stats["gold"] = 700
|
||||
assets.Inventory.Slots["0:10"] = dnfrepo.ItemStack{ItemID: 600, Count: 3}
|
||||
assets.AccountInventory.Slots[dnfrepo.AccountSharedInventorySlotKey(3)] = dnfrepo.ItemStack{ItemID: 3033, Count: 2}
|
||||
return Changes{AccountInventory: true, Character: true, Inventory: true}, nil
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Mutate error = %v", err)
|
||||
}
|
||||
|
||||
character, found, err := repositories.Character.Load(ctx, "77")
|
||||
if err != nil || !found || character.Stats["gold"] != 700 {
|
||||
t.Fatalf("character = %+v found=%t error=%v", character, found, err)
|
||||
}
|
||||
inventory, found, err := repositories.Inventory.Load(ctx, "77")
|
||||
if err != nil || !found || inventory.Slots["0:10"].Count != 3 {
|
||||
t.Fatalf("inventory = %+v found=%t error=%v", inventory, found, err)
|
||||
}
|
||||
account, found, err := repositories.AccountInventory.Load(ctx, "dnf:1")
|
||||
if err != nil || !found || account.Slots[dnfrepo.AccountSharedInventorySlotKey(3)].Count != 2 {
|
||||
t.Fatalf("account inventory = %+v found=%t error=%v", account, found, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOwnerMutateRejectsAccountMismatchBeforeProjection(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repositories := dnfrepomemory.NewMemoryGroup()
|
||||
seedNPCShopOwner(t, ctx, repositories)
|
||||
owner, err := NewOwner(repositories)
|
||||
if err != nil {
|
||||
t.Fatalf("NewOwner error = %v", err)
|
||||
}
|
||||
called := false
|
||||
err = owner.Mutate(ctx, Command{
|
||||
AccountID: "dnf:2",
|
||||
CharacterID: "77",
|
||||
Project: func(*Assets) (Changes, error) {
|
||||
called = true
|
||||
return Changes{}, nil
|
||||
},
|
||||
})
|
||||
if !errors.Is(err, ErrAccountMismatch) || called {
|
||||
t.Fatalf("Mutate error = %v called=%t", err, called)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOwnerMutateRollsBackAllAssetsWhenProjectionFails(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repositories := dnfrepomemory.NewMemoryGroup()
|
||||
seedNPCShopOwner(t, ctx, repositories)
|
||||
owner, err := NewOwner(repositories)
|
||||
if err != nil {
|
||||
t.Fatalf("NewOwner error = %v", err)
|
||||
}
|
||||
wantErr := errors.New("reject shop projection")
|
||||
err = owner.Mutate(ctx, Command{
|
||||
AccountID: "dnf:1",
|
||||
CharacterID: "77",
|
||||
Project: func(assets *Assets) (Changes, error) {
|
||||
assets.Character.Stats["gold"] = 1
|
||||
assets.Inventory.Slots["0:10"] = dnfrepo.ItemStack{ItemID: 600, Count: 3}
|
||||
return Changes{Character: true, Inventory: true}, wantErr
|
||||
},
|
||||
})
|
||||
if !errors.Is(err, wantErr) {
|
||||
t.Fatalf("Mutate error = %v, want %v", err, wantErr)
|
||||
}
|
||||
character, _, _ := repositories.Character.Load(ctx, "77")
|
||||
inventory, _, _ := repositories.Inventory.Load(ctx, "77")
|
||||
if character.Stats["gold"] != 1000 || len(inventory.Slots) != 0 {
|
||||
t.Fatalf("rollback character=%+v inventory=%+v", character, inventory)
|
||||
}
|
||||
}
|
||||
|
||||
func seedNPCShopOwner(t *testing.T, ctx context.Context, repositories dnfrepo.Group) {
|
||||
t.Helper()
|
||||
// 账号记录是限购计数的账号维度落点(`[purchase limit] `account``),
|
||||
// 商店事务现在要求它存在;线上账号在登录时必然已建行。
|
||||
if err := repositories.Account.Save(ctx, dnfrepo.AccountRecord{
|
||||
AccountID: "dnf:1",
|
||||
Metadata: map[string]string{},
|
||||
}); err != nil {
|
||||
t.Fatalf("save account: %v", err)
|
||||
}
|
||||
if err := repositories.Character.Save(ctx, dnfrepo.CharacterRecord{
|
||||
CharacterID: "77",
|
||||
AccountID: "dnf:1",
|
||||
Stats: map[string]int64{"gold": 1000, "sp": 30},
|
||||
}); err != nil {
|
||||
t.Fatalf("save character: %v", err)
|
||||
}
|
||||
if err := repositories.Inventory.Save(ctx, dnfrepo.InventoryRecord{
|
||||
CharacterID: "77",
|
||||
Slots: map[string]dnfrepo.ItemStack{},
|
||||
}); err != nil {
|
||||
t.Fatalf("save inventory: %v", err)
|
||||
}
|
||||
if err := repositories.AccountInventory.Save(ctx, dnfrepo.AccountInventoryRecord{
|
||||
AccountID: "dnf:1",
|
||||
Slots: map[string]dnfrepo.ItemStack{},
|
||||
}); err != nil {
|
||||
t.Fatalf("save account inventory: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user