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,84 @@
|
||||
// 本文件定义角色链命令计划。
|
||||
// dnfbridge 已有登录、选角和 USERINFO 专门链路;这里仅为 aligned fallback 记录意图和 owner 缺口。
|
||||
package character
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"longheng.io/server/internal/modules/dnf/alignedcmd"
|
||||
)
|
||||
|
||||
// Command 是 character handler 交给角色 owner 或登录链路的最小命令计划。
|
||||
type Command struct {
|
||||
Operation string
|
||||
AccountID string
|
||||
SelectedCharacterID uint16
|
||||
SlotOrCharacterID uint16
|
||||
Slot uint16
|
||||
Job byte
|
||||
Name string
|
||||
RawLen int
|
||||
NeedsOwner string
|
||||
}
|
||||
|
||||
// NewSelectCommand 构造选角命令计划。
|
||||
func NewSelectCommand(req alignedcmd.Request, parsed SelectCharacterRequest) Command {
|
||||
cmd := newRawCommand(req, "select_character", "login/character owner + op4/op37/scene bootstrap order")
|
||||
cmd.SlotOrCharacterID = parsed.SlotOrCharacterID
|
||||
return cmd
|
||||
}
|
||||
|
||||
// NewCreateCommand 构造创角命令计划。
|
||||
func NewCreateCommand(req alignedcmd.Request, parsed CreateCharacterRequest) Command {
|
||||
cmd := newRawCommand(req, "create_character", "character owner + slot/name/job validation + roster op2 refresh order")
|
||||
cmd.Job = parsed.Job
|
||||
cmd.Name = parsed.Name
|
||||
return cmd
|
||||
}
|
||||
|
||||
// NewDeleteCommand 构造删角命令计划。
|
||||
func NewDeleteCommand(req alignedcmd.Request, parsed DeleteCharacterRequest) Command {
|
||||
cmd := newRawCommand(req, "delete_character", "character owner + delete guard + roster op2 refresh order")
|
||||
cmd.Slot = parsed.Slot
|
||||
cmd.Name = parsed.Name
|
||||
return cmd
|
||||
}
|
||||
|
||||
// NewCheckNameCommand 构造角色名查重命令计划。
|
||||
func NewCheckNameCommand(req alignedcmd.Request, parsed CheckNameRequest) Command {
|
||||
cmd := newRawCommand(req, "check_double_character_name", "character name index + current EXE response body evidence")
|
||||
cmd.Name = parsed.Name
|
||||
return cmd
|
||||
}
|
||||
|
||||
// NewRawCommand 构造无包体或由专门链路处理的角色命令计划。
|
||||
func NewRawCommand(req alignedcmd.Request, operation string, needs string) Command {
|
||||
return newRawCommand(req, operation, needs)
|
||||
}
|
||||
|
||||
func newRawCommand(req alignedcmd.Request, operation string, needs string) Command {
|
||||
return Command{
|
||||
Operation: operation,
|
||||
AccountID: strings.TrimSpace(req.AccountID),
|
||||
SelectedCharacterID: req.SelectedCharacterID,
|
||||
RawLen: len(req.Body),
|
||||
NeedsOwner: needs,
|
||||
}
|
||||
}
|
||||
|
||||
func (c Command) String() string {
|
||||
base := fmt.Sprintf("account=%q char=%d", c.AccountID, c.SelectedCharacterID)
|
||||
switch c.Operation {
|
||||
case "select_character":
|
||||
return fmt.Sprintf("%s slotOrChar=%d needs=%s", base, c.SlotOrCharacterID, c.NeedsOwner)
|
||||
case "create_character":
|
||||
return fmt.Sprintf("%s job=%d name=%q needs=%s", base, c.Job, c.Name, c.NeedsOwner)
|
||||
case "delete_character":
|
||||
return fmt.Sprintf("%s slot=%d name=%q needs=%s", base, c.Slot, c.Name, c.NeedsOwner)
|
||||
case "check_double_character_name":
|
||||
return fmt.Sprintf("%s name=%q needs=%s", base, c.Name, c.NeedsOwner)
|
||||
default:
|
||||
return fmt.Sprintf("%s rawLen=%d needs=%s", base, c.RawLen, c.NeedsOwner)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
// 本文件负责角色选择链命令的协议边界。
|
||||
// dnfbridge 已有专门登录、选角和 USERINFO 链路;这里只补齐 aligned registry 的 character 域解析入口。
|
||||
package character
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"longheng.io/server/internal/modules/dnf/alignedcmd"
|
||||
"longheng.io/server/internal/modules/dnf/dnfenum"
|
||||
)
|
||||
|
||||
type handler struct{}
|
||||
|
||||
// NewHandler 创建角色协议处理器。
|
||||
func NewHandler() alignedcmd.Handler {
|
||||
return handler{}
|
||||
}
|
||||
|
||||
// Domain 返回角色业务域。
|
||||
func (handler) Domain() dnfenum.AlignedDomain {
|
||||
return dnfenum.AlignedDomainCharacter
|
||||
}
|
||||
|
||||
// Handle 解析角色相关请求;现有专门链路优先处理,落到这里的命令不直接回成功包。
|
||||
func (handler) Handle(ctx context.Context, req alignedcmd.Request) (alignedcmd.Result, error) {
|
||||
switch dnfenum.CmdPacket(req.Opcode) {
|
||||
case dnfenum.CmdPacketSelectCharacter:
|
||||
parsed, err := DecodeSelectCharacterRequest(req.Body)
|
||||
cmd := NewSelectCommand(req, parsed)
|
||||
return applyOwner(ctx, req, cmd, err), nil
|
||||
case dnfenum.CmdPacketCreateCharacter:
|
||||
parsed, err := DecodeCreateCharacterRequest(req.Body)
|
||||
cmd := NewCreateCommand(req, parsed)
|
||||
return applyOwner(ctx, req, cmd, err), nil
|
||||
case dnfenum.CmdPacketDeleteCharacter:
|
||||
parsed, err := DecodeDeleteCharacterRequest(req.Body)
|
||||
cmd := NewDeleteCommand(req, parsed)
|
||||
return applyOwner(ctx, req, cmd, err), nil
|
||||
case dnfenum.CmdPacketReturnSelectCharacter:
|
||||
cmd := NewRawCommand(req, "return_select_character", "login chain owner + roster op2 refresh order")
|
||||
return applyOwner(ctx, req, cmd, nil), nil
|
||||
case dnfenum.CmdPacketGetUserinfo:
|
||||
cmd := NewRawCommand(req, "get_userinfo", "USERINFO builder + current EXE subtype order + scene bootstrap gate")
|
||||
return applyOwner(ctx, req, cmd, nil), nil
|
||||
case dnfenum.CmdPacketCheckDoubleCharacterName:
|
||||
parsed, err := DecodeCheckNameRequest(req.Body)
|
||||
cmd := NewCheckNameCommand(req, parsed)
|
||||
return applyOwner(ctx, req, cmd, err), nil
|
||||
default:
|
||||
return alignedcmd.Result{
|
||||
Handled: false,
|
||||
ResponseAllowed: false,
|
||||
Operation: "unregistered",
|
||||
Reason: fmt.Sprintf("character 模块未登记 opcode %d", req.Opcode),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func applyOwner(ctx context.Context, req alignedcmd.Request, cmd Command, parseErr error) alignedcmd.Result {
|
||||
if parseErr != nil {
|
||||
return blockedParsedResult(cmd.Operation, cmd.String(), parseErr)
|
||||
}
|
||||
owner, err := NewOwner(req.Repositories)
|
||||
if err != nil {
|
||||
return ownerBlockedResult(cmd, err)
|
||||
}
|
||||
result, err := owner.Plan(ctx, cmd)
|
||||
if err != nil {
|
||||
return ownerBlockedResult(cmd, planError(cmd.Operation, err))
|
||||
}
|
||||
return ownerPlannedResult(cmd, result)
|
||||
}
|
||||
|
||||
func blockedParsedResult(operation string, summary string, err error) alignedcmd.Result {
|
||||
if err != nil {
|
||||
return alignedcmd.Result{
|
||||
Handled: true,
|
||||
ResponseAllowed: false,
|
||||
Operation: operation,
|
||||
Reason: fmt.Sprintf("%s 请求体解析失败:%v;禁止回成功包", operation, err),
|
||||
}
|
||||
}
|
||||
return alignedcmd.Result{
|
||||
Handled: true,
|
||||
ResponseAllowed: false,
|
||||
Operation: operation,
|
||||
Reason: fmt.Sprintf("已解析 %s:%s;角色链现在由 dnfbridge 专门链路处理,aligned fallback 不直接回包", operation, summary),
|
||||
}
|
||||
}
|
||||
|
||||
func ownerBlockedResult(cmd Command, err error) alignedcmd.Result {
|
||||
return alignedcmd.Result{
|
||||
Handled: true,
|
||||
ResponseAllowed: false,
|
||||
Operation: cmd.Operation,
|
||||
Reason: fmt.Sprintf("已解析 %s:%s;character owner 预检未通过:%v;aligned fallback 禁止成功 ACK", cmd.Operation, cmd.String(), err),
|
||||
}
|
||||
}
|
||||
|
||||
func ownerPlannedResult(cmd Command, result PlanResult) alignedcmd.Result {
|
||||
return alignedcmd.Result{
|
||||
Handled: true,
|
||||
ResponseAllowed: false,
|
||||
Operation: cmd.Operation,
|
||||
Reason: fmt.Sprintf(
|
||||
"已解析 %s:%s;character owner verified account=%s char=%s known=%t name=%q slot=%d job=%q level=%d roster=%d requested=(slotOrChar=%d slot=%d job=%d name=%q) nameTaken=%t owner=%s;角色列表/USERINFO/进场专门链路未闭合,aligned fallback 禁止成功 ACK",
|
||||
cmd.Operation,
|
||||
cmd.String(),
|
||||
result.AccountID,
|
||||
result.CharacterID,
|
||||
result.CharacterKnown,
|
||||
result.Name,
|
||||
result.Slot,
|
||||
result.Job,
|
||||
result.Level,
|
||||
result.RosterCount,
|
||||
result.SelectedOrRequested,
|
||||
result.RequestedSlot,
|
||||
result.RequestedJob,
|
||||
result.RequestedName,
|
||||
result.NameTaken,
|
||||
result.NameOwnerID,
|
||||
),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package character
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"longheng.io/server/internal/modules/dnf/alignedcmd"
|
||||
"longheng.io/server/internal/modules/dnf/dnfenum"
|
||||
dnfrepo "longheng.io/server/internal/modules/dnf/repository"
|
||||
dnfrepomemory "longheng.io/server/internal/modules/dnf/repository/memory"
|
||||
)
|
||||
|
||||
func TestDecodeCreateCharacterRequest(t *testing.T) {
|
||||
got, err := DecodeCreateCharacterRequest([]byte{15, 3, 0, 0, 0, 'a', 'b', 'c', 0})
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeCreateCharacterRequest error = %v", err)
|
||||
}
|
||||
if got.Job != 15 || got.Name != "abc" {
|
||||
t.Fatalf("got = %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeDeleteCharacterRequest(t *testing.T) {
|
||||
got, err := DecodeDeleteCharacterRequest([]byte{2, 3, 0, 0, 0, 'a', 'b', 'c'})
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeDeleteCharacterRequest error = %v", err)
|
||||
}
|
||||
if got.Slot != 2 || got.Name != "abc" {
|
||||
t.Fatalf("got = %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeCurrentDeleteCharacterRequestUsesUint16Slot(t *testing.T) {
|
||||
got, err := DecodeDeleteCharacterRequest([]byte{2, 0, 3, 0, 0, 0, 'a', 'b', 'c'})
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeDeleteCharacterRequest error = %v", err)
|
||||
}
|
||||
if got.Slot != 2 || got.Name != "abc" {
|
||||
t.Fatalf("got = %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerBlocksCharacterFallbackAck(t *testing.T) {
|
||||
got, err := NewHandler().Handle(context.Background(), alignedcmd.Request{
|
||||
Opcode: uint16(dnfenum.CmdPacketReturnSelectCharacter),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Handle error = %v", err)
|
||||
}
|
||||
if !got.Handled || got.ResponseAllowed {
|
||||
t.Fatalf("result = %+v, want handled without response", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerUsesOwnerPreflightAndStillBlocksCharacterAck(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repos := dnfrepomemory.NewMemoryGroup()
|
||||
if err := repos.Character.Save(ctx, dnfrepo.CharacterRecord{
|
||||
CharacterID: "77",
|
||||
AccountID: "acc",
|
||||
Slot: 2,
|
||||
Name: "hero",
|
||||
Job: "15",
|
||||
Level: 86,
|
||||
}); err != nil {
|
||||
t.Fatalf("Character.Save error = %v", err)
|
||||
}
|
||||
|
||||
got, err := NewHandler().Handle(ctx, alignedcmd.Request{
|
||||
Opcode: uint16(dnfenum.CmdPacketCheckDoubleCharacterName),
|
||||
AccountID: "acc",
|
||||
Repositories: repos,
|
||||
Body: []byte{4, 0, 0, 0, 'h', 'e', 'r', 'o'},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Handle error = %v", err)
|
||||
}
|
||||
if !got.Handled || got.ResponseAllowed {
|
||||
t.Fatalf("result = %+v, want handled without response", got)
|
||||
}
|
||||
for _, want := range []string{"character owner verified", "roster=1", `requested=(slotOrChar=0 slot=0 job=0 name="hero")`, "nameTaken=true", "owner=77"} {
|
||||
if !strings.Contains(got.Reason, want) {
|
||||
t.Fatalf("reason %q missing %q", got.Reason, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateCommandRecordsCharacterOwnerGap(t *testing.T) {
|
||||
cmd := NewCreateCommand(alignedcmd.Request{
|
||||
AccountID: " acc ",
|
||||
SelectedCharacterID: 0,
|
||||
}, CreateCharacterRequest{Job: 15, Name: "abc"})
|
||||
summary := cmd.String()
|
||||
for _, want := range []string{`account="acc"`, "job=15", `name="abc"`, "character owner", "roster op2"} {
|
||||
if !strings.Contains(summary, want) {
|
||||
t.Fatalf("summary %q missing %q", summary, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
// 本文件提供角色链 fallback 的只读 owner 预检。
|
||||
// 登录、选角、USERINFO 和角色列表仍由 dnfbridge 专门链路闭合,这里不写角色也不开放成功 ACK。
|
||||
package character
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
dnfrepo "longheng.io/server/internal/modules/dnf/repository"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrOwnerUnavailable = errors.New("character owner unavailable")
|
||||
ErrCharacterRequired = errors.New("selected character id required")
|
||||
ErrCharacterNotFound = errors.New("character record not found")
|
||||
ErrAccountMismatch = errors.New("character account mismatch")
|
||||
)
|
||||
|
||||
// Owner 是角色链 fallback 的只读查询边界。
|
||||
type Owner struct {
|
||||
characters dnfrepo.CharacterRepository
|
||||
}
|
||||
|
||||
// PlanResult 描述角色请求当前能从角色仓储确认到的上下文。
|
||||
type PlanResult struct {
|
||||
AccountID string
|
||||
CharacterID string
|
||||
CharacterKnown bool
|
||||
Name string
|
||||
Slot int
|
||||
Job string
|
||||
Level int
|
||||
RosterCount int
|
||||
NameTaken bool
|
||||
NameOwnerID string
|
||||
RequestedName string
|
||||
RequestedJob byte
|
||||
RequestedSlot int
|
||||
SelectedOrRequested uint16
|
||||
}
|
||||
|
||||
// NewOwner 创建角色 owner;缺少角色仓储时拒绝预检。
|
||||
func NewOwner(repos dnfrepo.Group) (*Owner, error) {
|
||||
if repos.Character == nil {
|
||||
return nil, ErrOwnerUnavailable
|
||||
}
|
||||
return &Owner{characters: repos.Character}, nil
|
||||
}
|
||||
|
||||
// Plan 只读取角色名索引、账号角色列表或当前角色快照,不承担真实登录/选角回包。
|
||||
func (o *Owner) Plan(ctx context.Context, cmd Command) (PlanResult, error) {
|
||||
if o == nil || o.characters == nil {
|
||||
return PlanResult{}, ErrOwnerUnavailable
|
||||
}
|
||||
result := PlanResult{
|
||||
AccountID: strings.TrimSpace(cmd.AccountID),
|
||||
RequestedName: strings.TrimSpace(cmd.Name),
|
||||
RequestedJob: cmd.Job,
|
||||
RequestedSlot: int(cmd.Slot),
|
||||
SelectedOrRequested: cmd.SlotOrCharacterID,
|
||||
}
|
||||
if result.AccountID != "" {
|
||||
roster, err := o.characters.ListByAccount(ctx, result.AccountID, 32)
|
||||
if err != nil {
|
||||
return PlanResult{}, err
|
||||
}
|
||||
result.RosterCount = len(roster)
|
||||
}
|
||||
|
||||
switch cmd.Operation {
|
||||
case "select_character":
|
||||
record, ok, err := o.findSelected(ctx, cmd)
|
||||
if err != nil {
|
||||
return PlanResult{}, err
|
||||
}
|
||||
if !ok {
|
||||
return PlanResult{}, ErrCharacterNotFound
|
||||
}
|
||||
fillCharacterResult(&result, record)
|
||||
case "return_select_character":
|
||||
// 返回角色选择只需要账号角色列表上下文,真实 op2 刷新仍由专门链路生成。
|
||||
case "get_userinfo":
|
||||
record, ok, err := o.findSelected(ctx, cmd)
|
||||
if err != nil {
|
||||
return PlanResult{}, err
|
||||
}
|
||||
if !ok {
|
||||
return PlanResult{}, ErrCharacterNotFound
|
||||
}
|
||||
fillCharacterResult(&result, record)
|
||||
case "create_character", "check_double_character_name":
|
||||
if result.RequestedName != "" {
|
||||
id, taken, err := o.characters.FindIDByName(ctx, result.RequestedName)
|
||||
if err != nil {
|
||||
return PlanResult{}, err
|
||||
}
|
||||
result.NameTaken = taken
|
||||
result.NameOwnerID = id
|
||||
}
|
||||
case "delete_character":
|
||||
if result.RequestedName != "" {
|
||||
id, taken, err := o.characters.FindIDByName(ctx, result.RequestedName)
|
||||
if err != nil {
|
||||
return PlanResult{}, err
|
||||
}
|
||||
result.NameTaken = taken
|
||||
result.NameOwnerID = id
|
||||
if taken {
|
||||
record, ok, err := o.characters.Load(ctx, id)
|
||||
if err != nil {
|
||||
return PlanResult{}, err
|
||||
}
|
||||
if ok {
|
||||
fillCharacterResult(&result, record)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (o *Owner) findSelected(ctx context.Context, cmd Command) (dnfrepo.CharacterRecord, bool, error) {
|
||||
accountID := strings.TrimSpace(cmd.AccountID)
|
||||
if cmd.SelectedCharacterID != 0 {
|
||||
record, ok, err := o.loadByNumericID(ctx, cmd.SelectedCharacterID)
|
||||
if err != nil || !ok {
|
||||
return record, ok, err
|
||||
}
|
||||
if !matchesAccount(record, accountID) {
|
||||
return dnfrepo.CharacterRecord{}, false, ErrAccountMismatch
|
||||
}
|
||||
return record, true, nil
|
||||
}
|
||||
if cmd.SlotOrCharacterID != 0 {
|
||||
if record, ok, err := o.loadByNumericID(ctx, cmd.SlotOrCharacterID); err != nil || (ok && matchesAccount(record, accountID)) {
|
||||
return record, ok, err
|
||||
}
|
||||
}
|
||||
if accountID == "" {
|
||||
return dnfrepo.CharacterRecord{}, false, ErrCharacterRequired
|
||||
}
|
||||
roster, err := o.characters.ListByAccount(ctx, accountID, 32)
|
||||
if err != nil {
|
||||
return dnfrepo.CharacterRecord{}, false, err
|
||||
}
|
||||
requestedSlot := int(cmd.SlotOrCharacterID)
|
||||
for _, record := range roster {
|
||||
if record.Slot == requestedSlot {
|
||||
return dnfrepo.CloneCharacter(record), true, nil
|
||||
}
|
||||
}
|
||||
return dnfrepo.CharacterRecord{}, false, nil
|
||||
}
|
||||
|
||||
func (o *Owner) loadByNumericID(ctx context.Context, id uint16) (dnfrepo.CharacterRecord, bool, error) {
|
||||
key := strconv.FormatUint(uint64(id), 10)
|
||||
record, ok, err := o.characters.Load(ctx, key)
|
||||
if err != nil || !ok {
|
||||
return dnfrepo.CharacterRecord{}, ok, err
|
||||
}
|
||||
return dnfrepo.CloneCharacter(record), true, nil
|
||||
}
|
||||
|
||||
func matchesAccount(record dnfrepo.CharacterRecord, accountID string) bool {
|
||||
return accountID == "" || strings.TrimSpace(record.AccountID) == accountID
|
||||
}
|
||||
|
||||
func fillCharacterResult(result *PlanResult, record dnfrepo.CharacterRecord) {
|
||||
record = dnfrepo.CloneCharacter(record)
|
||||
result.CharacterID = record.CharacterID
|
||||
result.CharacterKnown = true
|
||||
result.AccountID = record.AccountID
|
||||
result.Name = record.Name
|
||||
result.Slot = record.Slot
|
||||
result.Job = record.Job
|
||||
result.Level = record.Level
|
||||
}
|
||||
|
||||
func planError(operation string, err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%s: %w", strings.TrimSpace(operation), err)
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package character
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
dnfrepo "longheng.io/server/internal/modules/dnf/repository"
|
||||
dnfrepomemory "longheng.io/server/internal/modules/dnf/repository/memory"
|
||||
)
|
||||
|
||||
func TestOwnerPlansSelectCharacterByAccountSlot(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repos := dnfrepomemory.NewMemoryGroup()
|
||||
if err := repos.Character.Save(ctx, dnfrepo.CharacterRecord{
|
||||
CharacterID: "77",
|
||||
AccountID: "acc",
|
||||
Slot: 2,
|
||||
Name: "hero",
|
||||
Job: "15",
|
||||
Level: 86,
|
||||
}); err != nil {
|
||||
t.Fatalf("Character.Save error = %v", err)
|
||||
}
|
||||
|
||||
owner, err := NewOwner(repos)
|
||||
if err != nil {
|
||||
t.Fatalf("NewOwner error = %v", err)
|
||||
}
|
||||
got, err := owner.Plan(ctx, Command{
|
||||
Operation: "select_character",
|
||||
AccountID: "acc",
|
||||
SlotOrCharacterID: 2,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Plan error = %v", err)
|
||||
}
|
||||
if !got.CharacterKnown || got.CharacterID != "77" || got.Name != "hero" || got.Slot != 2 || got.Level != 86 {
|
||||
t.Fatalf("result = %+v", got)
|
||||
}
|
||||
if got.RosterCount != 1 {
|
||||
t.Fatalf("roster count = %d", got.RosterCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOwnerSelectCharacterDoesNotCrossAccountOnSlotOrIDCollision(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repos := dnfrepomemory.NewMemoryGroup()
|
||||
if err := repos.Character.Save(ctx, dnfrepo.CharacterRecord{
|
||||
CharacterID: "2",
|
||||
AccountID: "other",
|
||||
Slot: 9,
|
||||
Name: "wrong",
|
||||
}); err != nil {
|
||||
t.Fatalf("Character.Save(other) error = %v", err)
|
||||
}
|
||||
if err := repos.Character.Save(ctx, dnfrepo.CharacterRecord{
|
||||
CharacterID: "77",
|
||||
AccountID: "acc",
|
||||
Slot: 2,
|
||||
Name: "hero",
|
||||
}); err != nil {
|
||||
t.Fatalf("Character.Save(acc) error = %v", err)
|
||||
}
|
||||
|
||||
owner, err := NewOwner(repos)
|
||||
if err != nil {
|
||||
t.Fatalf("NewOwner error = %v", err)
|
||||
}
|
||||
got, err := owner.Plan(ctx, Command{
|
||||
Operation: "select_character",
|
||||
AccountID: "acc",
|
||||
SlotOrCharacterID: 2,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Plan error = %v", err)
|
||||
}
|
||||
if got.CharacterID != "77" || got.Name != "hero" {
|
||||
t.Fatalf("result = %+v, want account slot match", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOwnerPlansNameTaken(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repos := dnfrepomemory.NewMemoryGroup()
|
||||
if err := repos.Character.Save(ctx, dnfrepo.CharacterRecord{
|
||||
CharacterID: "77",
|
||||
AccountID: "acc",
|
||||
Slot: 2,
|
||||
Name: "hero",
|
||||
}); err != nil {
|
||||
t.Fatalf("Character.Save error = %v", err)
|
||||
}
|
||||
|
||||
owner, err := NewOwner(repos)
|
||||
if err != nil {
|
||||
t.Fatalf("NewOwner error = %v", err)
|
||||
}
|
||||
got, err := owner.Plan(ctx, Command{
|
||||
Operation: "check_double_character_name",
|
||||
AccountID: "acc",
|
||||
Name: "hero",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Plan error = %v", err)
|
||||
}
|
||||
if !got.NameTaken || got.NameOwnerID != "77" || got.RosterCount != 1 {
|
||||
t.Fatalf("result = %+v", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
// 本文件解析角色选择链 C2S 请求体。
|
||||
// 字段顺序参考 C# CharacterSelectHandler;当前只用于 fallback 日志和后续 owner 化计划。
|
||||
package character
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// SelectCharacterRequest 描述选择角色请求。
|
||||
type SelectCharacterRequest struct {
|
||||
SlotOrCharacterID uint16
|
||||
}
|
||||
|
||||
// CreateCharacterRequest 描述创建角色请求。
|
||||
type CreateCharacterRequest struct {
|
||||
Job byte
|
||||
Name string
|
||||
}
|
||||
|
||||
// DeleteCharacterRequest 描述删除角色请求。
|
||||
type DeleteCharacterRequest struct {
|
||||
Slot uint16
|
||||
Name string
|
||||
}
|
||||
|
||||
// CheckNameRequest 描述角色名查重请求。
|
||||
type CheckNameRequest struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
// DecodeSelectCharacterRequest 解析 C# 选角请求的前 2 字节槽位。
|
||||
func DecodeSelectCharacterRequest(body []byte) (SelectCharacterRequest, error) {
|
||||
if len(body) < 2 {
|
||||
return SelectCharacterRequest{}, fmt.Errorf("body too short: got %d want >= 2", len(body))
|
||||
}
|
||||
return SelectCharacterRequest{SlotOrCharacterID: binary.LittleEndian.Uint16(body)}, nil
|
||||
}
|
||||
|
||||
// DecodeCreateCharacterRequest 解析创建角色请求:u8 job + i32 nameLen + name bytes。
|
||||
func DecodeCreateCharacterRequest(body []byte) (CreateCharacterRequest, error) {
|
||||
if len(body) < 6 {
|
||||
return CreateCharacterRequest{}, fmt.Errorf("body too short: got %d want >= 6", len(body))
|
||||
}
|
||||
nameLen := int(int32(binary.LittleEndian.Uint32(body[1:])))
|
||||
if nameLen < 0 || 5+nameLen+1 > len(body) {
|
||||
return CreateCharacterRequest{}, fmt.Errorf("name length %d invalid for body %d", nameLen, len(body))
|
||||
}
|
||||
return CreateCharacterRequest{
|
||||
Job: body[0],
|
||||
Name: string(body[5 : 5+nameLen]),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// DecodeDeleteCharacterRequest parses the current u16 slot + i32 nameLen +
|
||||
// ANSI name layout. The historical u8-slot variant remains accepted.
|
||||
func DecodeDeleteCharacterRequest(body []byte) (DeleteCharacterRequest, error) {
|
||||
if len(body) < 6 {
|
||||
return DeleteCharacterRequest{}, fmt.Errorf("body too short: got %d want >= 6", len(body))
|
||||
}
|
||||
nameLen := int(int32(binary.LittleEndian.Uint32(body[2:])))
|
||||
if nameLen > 0 && 6+nameLen <= len(body) {
|
||||
return DeleteCharacterRequest{
|
||||
Slot: binary.LittleEndian.Uint16(body[:2]),
|
||||
Name: string(body[6 : 6+nameLen]),
|
||||
}, nil
|
||||
}
|
||||
nameLen = int(int32(binary.LittleEndian.Uint32(body[1:])))
|
||||
if nameLen <= 0 || 5+nameLen > len(body) {
|
||||
return DeleteCharacterRequest{}, fmt.Errorf("name length %d invalid for body %d", nameLen, len(body))
|
||||
}
|
||||
return DeleteCharacterRequest{
|
||||
Slot: uint16(body[0]),
|
||||
Name: string(body[5 : 5+nameLen]),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// DecodeCheckNameRequest 解析角色名查重请求:i32 nameLen + name bytes。
|
||||
func DecodeCheckNameRequest(body []byte) (CheckNameRequest, error) {
|
||||
if len(body) < 5 {
|
||||
return CheckNameRequest{}, fmt.Errorf("body too short: got %d want >= 5", len(body))
|
||||
}
|
||||
nameLen := int(int32(binary.LittleEndian.Uint32(body)))
|
||||
if nameLen <= 0 || 4+nameLen > len(body) {
|
||||
return CheckNameRequest{}, fmt.Errorf("name length %d invalid for body %d", nameLen, len(body))
|
||||
}
|
||||
return CheckNameRequest{Name: string(body[4 : 4+nameLen])}, nil
|
||||
}
|
||||
|
||||
func (r SelectCharacterRequest) String() string {
|
||||
return fmt.Sprintf("slotOrChar=%d", r.SlotOrCharacterID)
|
||||
}
|
||||
|
||||
func (r CreateCharacterRequest) String() string {
|
||||
return fmt.Sprintf("job=%d name=%q", r.Job, r.Name)
|
||||
}
|
||||
|
||||
func (r DeleteCharacterRequest) String() string {
|
||||
return fmt.Sprintf("slot=%d name=%q", r.Slot, r.Name)
|
||||
}
|
||||
|
||||
func (r CheckNameRequest) String() string {
|
||||
return fmt.Sprintf("name=%q", r.Name)
|
||||
}
|
||||
Reference in new issue
Block a user