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,6 @@
|
||||
// Package module 定义可装配运行时模块的生命周期边界。
|
||||
//
|
||||
// Module 实现必须可重复 Stop,并且 Start/AfterStart 失败后要能接受回滚 Stop。
|
||||
// Preflight 只能验证配置和依赖可达性,不得改写业务状态;BeforeStop 不应无限等待外部流量自然耗尽。
|
||||
// 所有生命周期方法必须尊重传入 context,panic 应在模块内转换为 error。
|
||||
package module
|
||||
@@ -0,0 +1,186 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"longheng.io/server/internal/platform/health"
|
||||
"longheng.io/server/internal/platform/runtimeguard"
|
||||
)
|
||||
|
||||
type Module interface {
|
||||
Name() string
|
||||
Start(context.Context) error
|
||||
Stop(context.Context) error
|
||||
}
|
||||
|
||||
// AfterStarter 必须幂等;Manager 可能在失败回滚时调用 Stop 清理已完成 AfterStart 的模块。
|
||||
type AfterStarter interface {
|
||||
AfterStart(context.Context) error
|
||||
}
|
||||
|
||||
// BeforeStopper 不应阻塞等待外部流量自然耗尽;优雅排水应由模块自己的 Stop ctx 控制。
|
||||
type BeforeStopper interface {
|
||||
BeforeStop(context.Context) error
|
||||
}
|
||||
|
||||
// PreflightChecker 只校验装配和后端可达性,不应改变业务状态。
|
||||
type PreflightChecker interface {
|
||||
Preflight(context.Context) error
|
||||
}
|
||||
|
||||
type Manager struct {
|
||||
name string
|
||||
logger *slog.Logger
|
||||
health *health.Service
|
||||
modules []Module
|
||||
profile *runtimeguard.Profile
|
||||
}
|
||||
|
||||
func New(name string, logger *slog.Logger, healthSvc *health.Service) *Manager {
|
||||
return &Manager{
|
||||
name: name,
|
||||
logger: logger,
|
||||
health: healthSvc,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) Name() string {
|
||||
return m.name
|
||||
}
|
||||
|
||||
func (m *Manager) Add(mod Module) {
|
||||
m.modules = append(m.modules, mod)
|
||||
}
|
||||
|
||||
func (m *Manager) SetRuntimeGuardProfile(profile runtimeguard.Profile) {
|
||||
if m == nil {
|
||||
return
|
||||
}
|
||||
m.profile = &profile
|
||||
}
|
||||
|
||||
func (m *Manager) Preflight(ctx context.Context) error {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
for _, mod := range m.modules {
|
||||
if err := m.checkRuntimeBackend(mod); err != nil {
|
||||
return err
|
||||
}
|
||||
checker, ok := mod.(PreflightChecker)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if m.health != nil {
|
||||
m.health.SetComponent(mod.Name(), health.StateStarting, "preflight")
|
||||
}
|
||||
if err := checker.Preflight(ctx); err != nil {
|
||||
if m.health != nil {
|
||||
m.health.SetComponent(mod.Name(), health.StateDegraded, err.Error())
|
||||
}
|
||||
return fmt.Errorf("preflight module %s: %w", mod.Name(), err)
|
||||
}
|
||||
if m.health != nil {
|
||||
m.health.SetComponent(mod.Name(), health.StateStarting, "preflight ok")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) checkRuntimeBackend(mod Module) error {
|
||||
if m == nil || m.profile == nil || mod == nil {
|
||||
return nil
|
||||
}
|
||||
describer, ok := mod.(runtimeguard.BackendDescriber)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
if err := runtimeguard.CheckDescriber(mod.Name(), describer, *m.profile); err != nil {
|
||||
return fmt.Errorf("preflight module %s runtime backend: %w", mod.Name(), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) Start(ctx context.Context) error {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
for i, mod := range m.modules {
|
||||
if m.health != nil {
|
||||
m.health.SetComponent(mod.Name(), health.StateStarting, "")
|
||||
}
|
||||
if err := mod.Start(ctx); err != nil {
|
||||
if m.health != nil {
|
||||
m.health.SetComponent(mod.Name(), health.StateDegraded, err.Error())
|
||||
}
|
||||
_ = m.stopStarted(ctx, i+1)
|
||||
return fmt.Errorf("start module %s: %w", mod.Name(), err)
|
||||
}
|
||||
if m.health != nil {
|
||||
m.health.SetComponent(mod.Name(), health.StateReady, "running")
|
||||
}
|
||||
}
|
||||
for _, mod := range m.modules {
|
||||
hook, ok := mod.(AfterStarter)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if err := hook.AfterStart(ctx); err != nil {
|
||||
if m.health != nil {
|
||||
m.health.SetComponent(mod.Name(), health.StateDegraded, err.Error())
|
||||
}
|
||||
_ = m.stopStarted(ctx, len(m.modules))
|
||||
return fmt.Errorf("after start module %s: %w", mod.Name(), err)
|
||||
}
|
||||
}
|
||||
if m.logger != nil {
|
||||
m.logger.Info("module manager started", "name", m.name, "count", len(m.modules))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) Stop(ctx context.Context) error {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
return m.stopStarted(ctx, len(m.modules))
|
||||
}
|
||||
|
||||
func (m *Manager) stopStarted(ctx context.Context, count int) error {
|
||||
var firstErr error
|
||||
for i := count - 1; i >= 0; i-- {
|
||||
mod := m.modules[i]
|
||||
hook, ok := mod.(BeforeStopper)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if err := hook.BeforeStop(ctx); err != nil && firstErr == nil {
|
||||
firstErr = fmt.Errorf("before stop module %s: %w", mod.Name(), err)
|
||||
if m.health != nil {
|
||||
m.health.SetComponent(mod.Name(), health.StateDegraded, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
for i := count - 1; i >= 0; i-- {
|
||||
mod := m.modules[i]
|
||||
if m.health != nil {
|
||||
m.health.SetComponent(mod.Name(), health.StateStopping, "")
|
||||
}
|
||||
if err := mod.Stop(ctx); err != nil && firstErr == nil {
|
||||
firstErr = fmt.Errorf("stop module %s: %w", mod.Name(), err)
|
||||
if m.health != nil {
|
||||
m.health.SetComponent(mod.Name(), health.StateDegraded, err.Error())
|
||||
}
|
||||
continue
|
||||
}
|
||||
if m.health != nil {
|
||||
m.health.SetComponent(mod.Name(), health.StateStopped, "stopped")
|
||||
}
|
||||
}
|
||||
if m.logger != nil {
|
||||
m.logger.Info("module manager stopped", "name", m.name)
|
||||
}
|
||||
return firstErr
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrModuleNameRequired = errors.New("module name is required")
|
||||
ErrModuleFactoryRequired = errors.New("module factory is required")
|
||||
ErrModuleNotFound = errors.New("module not found")
|
||||
)
|
||||
|
||||
type Descriptor struct {
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version,omitempty"`
|
||||
Kind string `json:"kind,omitempty"`
|
||||
Critical bool `json:"critical,omitempty"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
type Factory func() (Module, error)
|
||||
|
||||
type Registry struct {
|
||||
mu sync.RWMutex
|
||||
entries map[string]registeredModule
|
||||
}
|
||||
|
||||
type registeredModule struct {
|
||||
descriptor Descriptor
|
||||
factory Factory
|
||||
}
|
||||
|
||||
func NewRegistry() *Registry {
|
||||
return &Registry{entries: make(map[string]registeredModule)}
|
||||
}
|
||||
|
||||
func (r *Registry) Register(descriptor Descriptor, factory Factory) error {
|
||||
name := strings.TrimSpace(descriptor.Name)
|
||||
if name == "" {
|
||||
return ErrModuleNameRequired
|
||||
}
|
||||
if factory == nil {
|
||||
return ErrModuleFactoryRequired
|
||||
}
|
||||
descriptor.Name = name
|
||||
descriptor.Metadata = cloneDescMeta(descriptor.Metadata)
|
||||
if r == nil {
|
||||
return ErrModuleFactoryRequired
|
||||
}
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if r.entries == nil {
|
||||
r.entries = make(map[string]registeredModule)
|
||||
}
|
||||
r.entries[name] = registeredModule{descriptor: descriptor, factory: factory}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Registry) Create(name string) (Module, Descriptor, error) {
|
||||
name = strings.TrimSpace(name)
|
||||
if name == "" {
|
||||
return nil, Descriptor{}, ErrModuleNameRequired
|
||||
}
|
||||
if r == nil {
|
||||
return nil, Descriptor{}, ErrModuleNotFound
|
||||
}
|
||||
r.mu.RLock()
|
||||
entry, ok := r.entries[name]
|
||||
r.mu.RUnlock()
|
||||
if !ok {
|
||||
return nil, Descriptor{}, ErrModuleNotFound
|
||||
}
|
||||
mod, err := entry.factory()
|
||||
if err != nil {
|
||||
return nil, Descriptor{}, err
|
||||
}
|
||||
if mod == nil {
|
||||
return nil, Descriptor{}, ErrModuleFactoryRequired
|
||||
}
|
||||
return mod, cloneDescriptor(entry.descriptor), nil
|
||||
}
|
||||
|
||||
func (r *Registry) List() []Descriptor {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
r.mu.RLock()
|
||||
out := make([]Descriptor, 0, len(r.entries))
|
||||
for _, entry := range r.entries {
|
||||
out = append(out, cloneDescriptor(entry.descriptor))
|
||||
}
|
||||
r.mu.RUnlock()
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneDescriptor(in Descriptor) Descriptor {
|
||||
in.Metadata = cloneDescMeta(in.Metadata)
|
||||
return in
|
||||
}
|
||||
|
||||
func cloneDescMeta(in map[string]string) map[string]string {
|
||||
if len(in) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]string, len(in))
|
||||
for key, value := range in {
|
||||
key = strings.TrimSpace(key)
|
||||
value = strings.TrimSpace(value)
|
||||
if key != "" && value != "" {
|
||||
out[key] = value
|
||||
}
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in new issue
Block a user