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,181 @@
|
||||
package itemexpiration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
dnfrepo "longheng.io/server/internal/modules/dnf/repository"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrOwnerUnavailable = errors.New("item expiration owner is unavailable")
|
||||
ErrAccountRequired = errors.New("item expiration account id is required")
|
||||
ErrCharacterRequired = errors.New("item expiration character id is required")
|
||||
ErrProjectorRequired = errors.New("item expiration projector is required")
|
||||
ErrCharacterNotFound = errors.New("item expiration character is missing")
|
||||
ErrAccountMismatch = errors.New("item expiration character does not belong to account")
|
||||
)
|
||||
|
||||
type Summary struct {
|
||||
Inventory int
|
||||
Warehouse int
|
||||
Account int
|
||||
Equipment int
|
||||
}
|
||||
|
||||
func (summary Summary) Total() int {
|
||||
return summary.Inventory + summary.Warehouse + summary.Account + summary.Equipment
|
||||
}
|
||||
|
||||
type Assets struct {
|
||||
Inventory *dnfrepo.InventoryRecord
|
||||
AccountInventory *dnfrepo.AccountInventoryRecord
|
||||
Equipment *dnfrepo.EquipmentRecord
|
||||
}
|
||||
|
||||
type Projector func(*Assets) (Summary, error)
|
||||
|
||||
type Command struct {
|
||||
AccountID string
|
||||
CharacterID string
|
||||
UpdatedAt time.Time
|
||||
Project Projector
|
||||
}
|
||||
|
||||
// Owner owns the atomic reconciliation commit for every durable item
|
||||
// container affected by the shared PVF expiration projection. PVF lookup and
|
||||
// current-client row encoding 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) Reconcile(ctx context.Context, command Command) (Summary, error) {
|
||||
if o == nil || o.assets == nil {
|
||||
return Summary{}, ErrOwnerUnavailable
|
||||
}
|
||||
accountID := strings.TrimSpace(command.AccountID)
|
||||
if accountID == "" {
|
||||
return Summary{}, ErrAccountRequired
|
||||
}
|
||||
characterID := strings.TrimSpace(command.CharacterID)
|
||||
if characterID == "" {
|
||||
return Summary{}, ErrCharacterRequired
|
||||
}
|
||||
if command.Project == nil {
|
||||
return Summary{}, ErrProjectorRequired
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return Summary{}, err
|
||||
}
|
||||
now := command.UpdatedAt.UTC()
|
||||
if now.IsZero() {
|
||||
now = time.Now().UTC()
|
||||
}
|
||||
|
||||
var result Summary
|
||||
err := o.assets.WithinAccountCharacterAssets(ctx, accountID, characterID, func(
|
||||
accountInventories dnfrepo.AccountInventoryRepository,
|
||||
characters dnfrepo.CharacterRepository,
|
||||
inventories dnfrepo.InventoryRepository,
|
||||
equipmentRepo dnfrepo.EquipmentRepository,
|
||||
) error {
|
||||
assets, err := loadAssets(ctx, accountID, characterID, accountInventories, characters, inventories, equipmentRepo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result, err = command.Project(&assets)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if assets.Inventory != nil && (result.Inventory > 0 || result.Warehouse > 0) {
|
||||
fields := make([]dnfrepo.InventoryField, 0, 2)
|
||||
if result.Inventory > 0 {
|
||||
fields = append(fields, dnfrepo.InventoryFieldSlots)
|
||||
}
|
||||
if result.Warehouse > 0 {
|
||||
fields = append(fields, dnfrepo.InventoryFieldWarehouse)
|
||||
}
|
||||
assets.Inventory.UpdatedAt = now
|
||||
if err := dnfrepo.SaveInventoryFields(ctx, inventories, *assets.Inventory, fields...); err != nil {
|
||||
return fmt.Errorf("save item expiration inventory: %w", err)
|
||||
}
|
||||
}
|
||||
if assets.AccountInventory != nil && result.Account > 0 {
|
||||
assets.AccountInventory.UpdatedAt = now
|
||||
if err := accountInventories.Save(ctx, *assets.AccountInventory); err != nil {
|
||||
return fmt.Errorf("save item expiration account inventory: %w", err)
|
||||
}
|
||||
}
|
||||
if assets.Equipment != nil && result.Equipment > 0 {
|
||||
assets.Equipment.UpdatedAt = now
|
||||
if err := dnfrepo.SaveEquipmentFields(ctx, equipmentRepo, *assets.Equipment, dnfrepo.EquipmentFieldEntries); err != nil {
|
||||
return fmt.Errorf("save item expiration equipment: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return result, err
|
||||
}
|
||||
|
||||
func loadAssets(
|
||||
ctx context.Context,
|
||||
accountID string,
|
||||
characterID string,
|
||||
accountInventories dnfrepo.AccountInventoryRepository,
|
||||
characters dnfrepo.CharacterRepository,
|
||||
inventories dnfrepo.InventoryRepository,
|
||||
equipmentRepo dnfrepo.EquipmentRepository,
|
||||
) (Assets, error) {
|
||||
if accountInventories == nil || characters == nil || inventories == nil || equipmentRepo == nil {
|
||||
return Assets{}, ErrOwnerUnavailable
|
||||
}
|
||||
var assets Assets
|
||||
character, found, err := characters.Load(ctx, characterID)
|
||||
if err != nil {
|
||||
return Assets{}, fmt.Errorf("load item expiration character: %w", err)
|
||||
}
|
||||
if !found || strings.TrimSpace(character.CharacterID) != characterID {
|
||||
return Assets{}, fmt.Errorf("%w: character=%s", ErrCharacterNotFound, characterID)
|
||||
}
|
||||
if strings.TrimSpace(character.AccountID) != accountID {
|
||||
return Assets{}, fmt.Errorf("%w: account=%s character=%s", ErrAccountMismatch, accountID, characterID)
|
||||
}
|
||||
inventory, found, err := inventories.Load(ctx, characterID)
|
||||
if err != nil {
|
||||
return Assets{}, fmt.Errorf("load item expiration inventory: %w", err)
|
||||
}
|
||||
if found {
|
||||
inventory = dnfrepo.CloneInventory(inventory)
|
||||
assets.Inventory = &inventory
|
||||
}
|
||||
accountInventory, found, err := accountInventories.Load(ctx, accountID)
|
||||
if err != nil {
|
||||
return Assets{}, fmt.Errorf("load item expiration account inventory: %w", err)
|
||||
}
|
||||
if found {
|
||||
accountInventory = dnfrepo.CloneAccountInventory(accountInventory)
|
||||
assets.AccountInventory = &accountInventory
|
||||
}
|
||||
equipment, found, err := equipmentRepo.Load(ctx, characterID)
|
||||
if err != nil {
|
||||
return Assets{}, fmt.Errorf("load item expiration equipment: %w", err)
|
||||
}
|
||||
if found {
|
||||
equipment = dnfrepo.CloneEquipment(equipment)
|
||||
assets.Equipment = &equipment
|
||||
}
|
||||
return assets, nil
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package itemexpiration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
dnfrepo "longheng.io/server/internal/modules/dnf/repository"
|
||||
dnfrepomemory "longheng.io/server/internal/modules/dnf/repository/memory"
|
||||
)
|
||||
|
||||
func TestOwnerReconcileCommitsAllItemContainersAtomically(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repositories := seededRepositories(t, ctx)
|
||||
owner, err := NewOwner(repositories)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
updatedAt := time.Unix(2_000_000_000, 0).UTC()
|
||||
summary, err := owner.Reconcile(ctx, Command{
|
||||
AccountID: "account-1",
|
||||
CharacterID: "19",
|
||||
UpdatedAt: updatedAt,
|
||||
Project: func(assets *Assets) (Summary, error) {
|
||||
assets.Inventory.Slots["0:1"] = dnfrepo.ItemStack{ItemID: 101, Count: 1}
|
||||
assets.Inventory.Warehouse["2:1"] = dnfrepo.ItemStack{ItemID: 102, Count: 1}
|
||||
assets.AccountInventory.Slots["0:1"] = dnfrepo.ItemStack{ItemID: 103, Count: 1}
|
||||
assets.Equipment.Entries["0"] = dnfrepo.EquipmentEntry{SlotIndex: 0, ItemID: 104}
|
||||
return Summary{Inventory: 1, Warehouse: 1, Account: 1, Equipment: 1}, nil
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if summary.Total() != 4 {
|
||||
t.Fatalf("summary=%+v", summary)
|
||||
}
|
||||
|
||||
inventory, found, err := repositories.Inventory.Load(ctx, "19")
|
||||
if err != nil || !found {
|
||||
t.Fatalf("load inventory found=%t err=%v", found, err)
|
||||
}
|
||||
if inventory.Slots["0:1"].ItemID != 101 || inventory.Warehouse["2:1"].ItemID != 102 || !inventory.UpdatedAt.Equal(updatedAt) {
|
||||
t.Fatalf("inventory=%+v", inventory)
|
||||
}
|
||||
accountInventory, found, err := repositories.AccountInventory.Load(ctx, "account-1")
|
||||
if err != nil || !found {
|
||||
t.Fatalf("load account inventory found=%t err=%v", found, err)
|
||||
}
|
||||
if accountInventory.Slots["0:1"].ItemID != 103 || !accountInventory.UpdatedAt.Equal(updatedAt) {
|
||||
t.Fatalf("account inventory=%+v", accountInventory)
|
||||
}
|
||||
equipment, found, err := repositories.Equipment.Load(ctx, "19")
|
||||
if err != nil || !found {
|
||||
t.Fatalf("load equipment found=%t err=%v", found, err)
|
||||
}
|
||||
if equipment.Entries["0"].ItemID != 104 || !equipment.UpdatedAt.Equal(updatedAt) {
|
||||
t.Fatalf("equipment=%+v", equipment)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOwnerReconcileRollsBackProjectorFailure(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repositories := seededRepositories(t, ctx)
|
||||
owner, err := NewOwner(repositories)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
projectErr := errors.New("projection failed")
|
||||
summary, err := owner.Reconcile(ctx, Command{
|
||||
AccountID: "account-1",
|
||||
CharacterID: "19",
|
||||
Project: func(assets *Assets) (Summary, error) {
|
||||
assets.Inventory.Slots["0:1"] = dnfrepo.ItemStack{ItemID: 201, Count: 1}
|
||||
assets.AccountInventory.Slots["0:1"] = dnfrepo.ItemStack{ItemID: 202, Count: 1}
|
||||
assets.Equipment.Entries["0"] = dnfrepo.EquipmentEntry{SlotIndex: 0, ItemID: 203}
|
||||
return Summary{Inventory: 1, Account: 1, Equipment: 1}, projectErr
|
||||
},
|
||||
})
|
||||
if !errors.Is(err, projectErr) {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
if summary.Total() != 3 {
|
||||
t.Fatalf("summary=%+v", summary)
|
||||
}
|
||||
|
||||
inventory, _, _ := repositories.Inventory.Load(ctx, "19")
|
||||
accountInventory, _, _ := repositories.AccountInventory.Load(ctx, "account-1")
|
||||
equipment, _, _ := repositories.Equipment.Load(ctx, "19")
|
||||
if inventory.Slots["0:1"].ItemID != 1 || accountInventory.Slots["0:1"].ItemID != 2 || equipment.Entries["0"].ItemID != 3 {
|
||||
t.Fatalf("rollback failed inventory=%+v account=%+v equipment=%+v", inventory, accountInventory, equipment)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOwnerReconcileRejectsCrossAccountCharacter(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repositories := seededRepositories(t, ctx)
|
||||
owner, err := NewOwner(repositories)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
projected := false
|
||||
_, err = owner.Reconcile(ctx, Command{
|
||||
AccountID: "account-2",
|
||||
CharacterID: "19",
|
||||
Project: func(*Assets) (Summary, error) {
|
||||
projected = true
|
||||
return Summary{}, nil
|
||||
},
|
||||
})
|
||||
if !errors.Is(err, ErrAccountMismatch) {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
if projected {
|
||||
t.Fatal("cross-account request reached projector")
|
||||
}
|
||||
}
|
||||
|
||||
func seededRepositories(t *testing.T, ctx context.Context) dnfrepo.Group {
|
||||
t.Helper()
|
||||
repositories := dnfrepomemory.NewMemoryGroup()
|
||||
if err := repositories.Character.Save(ctx, dnfrepo.CharacterRecord{
|
||||
AccountID: "account-1",
|
||||
CharacterID: "19",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := repositories.Inventory.Save(ctx, dnfrepo.InventoryRecord{
|
||||
CharacterID: "19",
|
||||
Slots: map[string]dnfrepo.ItemStack{"0:1": {ItemID: 1, Count: 1}},
|
||||
Warehouse: map[string]dnfrepo.ItemStack{"2:1": {ItemID: 1, Count: 1}},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := repositories.AccountInventory.Save(ctx, dnfrepo.AccountInventoryRecord{
|
||||
AccountID: "account-1",
|
||||
Slots: map[string]dnfrepo.ItemStack{"0:1": {ItemID: 2, Count: 1}},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := repositories.Equipment.Save(ctx, dnfrepo.EquipmentRecord{
|
||||
CharacterID: "19",
|
||||
Entries: map[string]dnfrepo.EquipmentEntry{"0": {SlotIndex: 0, ItemID: 3}},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return repositories
|
||||
}
|
||||
Reference in new issue
Block a user