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

+368
View File
@@ -0,0 +1,368 @@
package cache
import (
"context"
"errors"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
)
var (
ErrKeyRequired = errors.New("cache key is required")
ErrNilMemoryStore = errors.New("cache memory store is required")
)
type Store interface {
Get(context.Context, string) ([]byte, bool, error)
Set(context.Context, string, []byte, time.Duration) error
Delete(context.Context, string) error
Snapshot() Snapshot
Close() error
}
type TakeStore interface {
Take(context.Context, string) ([]byte, bool, error)
}
type Snapshot struct {
Name string `json:"name"`
Kind string `json:"kind"`
Keys int `json:"keys,omitempty"`
MaxEntries int `json:"max_entries,omitempty"`
KeyPrefix string `json:"key_prefix,omitempty"`
Closed bool `json:"closed,omitempty"`
}
type MemoryOptions struct {
Name string
DefaultTTL time.Duration
MaxEntries int
Now func() time.Time
}
type MemoryStore struct {
mu sync.RWMutex
initOnce sync.Once
name string
defaultTTL time.Duration
maxEntries int
now func() time.Time
items map[string]memoryItem
closed bool
metrics *Metrics
}
type memoryItem struct {
value []byte
expiresAt time.Time
lastAccess *atomic.Int64
}
func NewMemory(options MemoryOptions) *MemoryStore {
name := strings.TrimSpace(options.Name)
if name == "" {
name = "memory-cache"
}
now := options.Now
if now == nil {
now = time.Now
}
return &MemoryStore{
name: name,
defaultTTL: options.DefaultTTL,
maxEntries: options.MaxEntries,
now: now,
items: make(map[string]memoryItem),
}
}
func (s *MemoryStore) SetMetrics(m *Metrics) {
if s == nil {
return
}
s.mu.Lock()
s.metrics = m
s.mu.Unlock()
}
func (s *MemoryStore) Get(ctx context.Context, key string) ([]byte, bool, error) {
if err := ctxErr(ctx); err != nil {
return nil, false, err
}
key = normalizeKey(key)
if key == "" {
return nil, false, ErrKeyRequired
}
if err := s.ready(); err != nil {
return nil, false, err
}
now := s.now().UTC()
s.mu.RLock()
item, ok := s.items[key]
closed := s.closed
metricsRef := s.metrics
if closed {
s.mu.RUnlock()
metricsRef.recordMiss()
return nil, false, nil
}
if !ok {
s.mu.RUnlock()
metricsRef.recordMiss()
return nil, false, nil
}
if expired(item, now) {
s.mu.RUnlock()
s.deleteExpired(key, now)
metricsRef.recordEviction()
metricsRef.recordMiss()
return nil, false, nil
}
value := cloneBytes(item.value)
lastAccess := item.lastAccess
s.mu.RUnlock()
storeLastAccess(lastAccess, now)
metricsRef.recordHit()
return value, true, nil
}
func (s *MemoryStore) Take(ctx context.Context, key string) ([]byte, bool, error) {
if err := ctxErr(ctx); err != nil {
return nil, false, err
}
key = normalizeKey(key)
if key == "" {
return nil, false, ErrKeyRequired
}
if err := s.ready(); err != nil {
return nil, false, err
}
now := s.now().UTC()
s.mu.Lock()
item, ok := s.items[key]
closed := s.closed
metricsRef := s.metrics
if closed {
s.mu.Unlock()
metricsRef.recordMiss()
return nil, false, nil
}
if !ok {
s.mu.Unlock()
metricsRef.recordMiss()
return nil, false, nil
}
if expired(item, now) {
delete(s.items, key)
s.mu.Unlock()
metricsRef.recordEviction()
metricsRef.recordMiss()
return nil, false, nil
}
delete(s.items, key)
value := cloneBytes(item.value)
s.mu.Unlock()
metricsRef.recordHit()
return value, true, nil
}
func (s *MemoryStore) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error {
if err := ctxErr(ctx); err != nil {
return err
}
key = normalizeKey(key)
if key == "" {
return ErrKeyRequired
}
if err := s.ready(); err != nil {
return err
}
if ttl == 0 {
ttl = s.defaultTTL
}
now := s.now().UTC()
item := memoryItem{value: cloneBytes(value), lastAccess: newAccessClock(now)}
if ttl > 0 {
item.expiresAt = now.Add(ttl)
}
s.mu.Lock()
if s.closed {
s.mu.Unlock()
return nil
}
s.items[key] = item
evicted := s.evictOverflowLocked()
metricsRef := s.metrics
s.mu.Unlock()
for idx := 0; idx < evicted; idx++ {
metricsRef.recordEviction()
}
return nil
}
func (s *MemoryStore) Delete(ctx context.Context, key string) error {
if err := ctxErr(ctx); err != nil {
return err
}
key = normalizeKey(key)
if key == "" {
return ErrKeyRequired
}
if err := s.ready(); err != nil {
return err
}
s.mu.Lock()
delete(s.items, key)
s.mu.Unlock()
return nil
}
func (s *MemoryStore) Snapshot() Snapshot {
if err := s.ready(); err != nil {
return Snapshot{Kind: "memory"}
}
now := s.now().UTC()
s.mu.Lock()
metricsRef := s.metrics
evicted := 0
for key, item := range s.items {
if expired(item, now) {
delete(s.items, key)
evicted++
}
}
snapshot := Snapshot{
Name: s.name,
Kind: "memory",
Keys: len(s.items),
MaxEntries: s.maxEntries,
Closed: s.closed,
}
s.mu.Unlock()
for idx := 0; idx < evicted; idx++ {
metricsRef.recordEviction()
}
return snapshot
}
func (s *MemoryStore) Close() error {
if s == nil {
return nil
}
if err := s.ready(); err != nil {
return err
}
s.mu.Lock()
s.closed = true
s.items = make(map[string]memoryItem)
s.mu.Unlock()
return nil
}
func (s *MemoryStore) ready() error {
if s == nil {
return ErrNilMemoryStore
}
s.initOnce.Do(func() {
s.mu.Lock()
defer s.mu.Unlock()
s.name = strings.TrimSpace(s.name)
if s.name == "" {
s.name = "memory-cache"
}
if s.now == nil {
s.now = time.Now
}
if s.items == nil {
s.items = make(map[string]memoryItem)
}
})
return nil
}
func normalizeKey(key string) string {
return strings.TrimSpace(key)
}
func expired(item memoryItem, now time.Time) bool {
return !item.expiresAt.IsZero() && !item.expiresAt.After(now)
}
func (s *MemoryStore) evictOverflowLocked() int {
if s.maxEntries <= 0 {
return 0
}
overflow := len(s.items) - s.maxEntries
if overflow <= 0 {
return 0
}
keys := make([]string, 0, len(s.items))
for key := range s.items {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool {
left := itemAccessTime(s.items[keys[i]])
right := itemAccessTime(s.items[keys[j]])
if left.Equal(right) {
return keys[i] < keys[j]
}
return left.Before(right)
})
if overflow > len(keys) {
overflow = len(keys)
}
for idx := 0; idx < overflow; idx++ {
delete(s.items, keys[idx])
}
return overflow
}
func newAccessClock(now time.Time) *atomic.Int64 {
clock := &atomic.Int64{}
storeLastAccess(clock, now)
return clock
}
func storeLastAccess(clock *atomic.Int64, now time.Time) {
if clock != nil {
clock.Store(now.UnixNano())
}
}
func itemAccessTime(item memoryItem) time.Time {
if item.lastAccess != nil {
if unixNano := item.lastAccess.Load(); unixNano > 0 {
return time.Unix(0, unixNano).UTC()
}
}
return item.expiresAt
}
func (s *MemoryStore) deleteExpired(key string, now time.Time) {
s.mu.Lock()
item, ok := s.items[key]
if ok && expired(item, now) {
delete(s.items, key)
}
s.mu.Unlock()
}
func cloneBytes(value []byte) []byte {
if value == nil {
return nil
}
return append([]byte(nil), value...)
}
func ctxErr(ctx context.Context) error {
if ctx == nil {
return nil
}
return ctx.Err()
}
+51
View File
@@ -0,0 +1,51 @@
package cache
import (
"strings"
"longheng.io/server/internal/platform/metrics"
)
const (
MetricHitsTotal = "cache_hits_total"
MetricMissesTotal = "cache_misses_total"
MetricEvictionsTotal = "cache_evictions_total"
)
type Metrics struct {
reg *metrics.Registry
kind string
}
func NewMetrics(reg *metrics.Registry, kind string) *Metrics {
kind = strings.TrimSpace(kind)
if reg == nil || kind == "" {
return nil
}
return &Metrics{reg: reg, kind: kind}
}
func (m *Metrics) recordHit() {
if m == nil {
return
}
m.reg.Inc(MetricHitsTotal, m.labels())
}
func (m *Metrics) recordMiss() {
if m == nil {
return
}
m.reg.Inc(MetricMissesTotal, m.labels())
}
func (m *Metrics) recordEviction() {
if m == nil {
return
}
m.reg.Inc(MetricEvictionsTotal, m.labels())
}
func (m *Metrics) labels() map[string]string {
return map[string]string{"kind": m.kind}
}
+249
View File
@@ -0,0 +1,249 @@
package cache
import (
"context"
"errors"
"strings"
"sync"
"time"
"github.com/gomodule/redigo/redis"
"longheng.io/server/internal/platform/db"
)
var ErrNilRedisExecutor = errors.New("cache redis executor is required")
type RedisOptions struct {
Name string
Executor db.RedisExecutor
KeyPrefix string
Address string
Password string
DB int
PoolSize int
Timeout time.Duration
ConnectTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
DefaultTTL time.Duration
}
type RedisStore struct {
name string
keyPrefix string
defaultTTL time.Duration
executor db.RedisExecutor
closer interface{ Close() error }
metricsMu sync.RWMutex
metrics *Metrics
}
const redisTakeScript = `local v = redis.call("GET", KEYS[1]); if v then redis.call("DEL", KEYS[1]); end; return v`
func NewRedis(options RedisOptions) *RedisStore {
options = normRedisOpts(options)
executor := options.Executor
var closer interface{ Close() error }
if executor == nil {
redigo := db.NewRedigoExecutor(db.RedigoOptions{
Address: options.Address,
Password: options.Password,
DB: options.DB,
PoolSize: options.PoolSize,
Timeout: options.Timeout,
ConnectTimeout: options.ConnectTimeout,
ReadTimeout: options.ReadTimeout,
WriteTimeout: options.WriteTimeout,
})
executor = redigo
closer = redigo
} else if candidate, ok := executor.(interface{ Close() error }); ok {
closer = candidate
}
return &RedisStore{
name: options.Name,
keyPrefix: options.KeyPrefix,
defaultTTL: options.DefaultTTL,
executor: executor,
closer: closer,
}
}
func (s *RedisStore) SetMetrics(m *Metrics) {
if s == nil {
return
}
s.metricsMu.Lock()
defer s.metricsMu.Unlock()
s.metrics = m
}
func (s *RedisStore) Get(ctx context.Context, key string) ([]byte, bool, error) {
if err := ctxErr(ctx); err != nil {
return nil, false, err
}
key = normalizeKey(key)
if key == "" {
return nil, false, ErrKeyRequired
}
if err := s.ready(); err != nil {
return nil, false, err
}
value, err := redis.Bytes(s.do(ctx, "GET", s.cacheKey(key)))
if errors.Is(err, redis.ErrNil) {
s.cacheMetrics().recordMiss()
return nil, false, nil
}
if err != nil {
return nil, false, err
}
s.cacheMetrics().recordHit()
return value, true, nil
}
func (s *RedisStore) Take(ctx context.Context, key string) ([]byte, bool, error) {
if err := ctxErr(ctx); err != nil {
return nil, false, err
}
key = normalizeKey(key)
if key == "" {
return nil, false, ErrKeyRequired
}
if err := s.ready(); err != nil {
return nil, false, err
}
value, err := redis.Bytes(s.do(ctx, "EVAL", redisTakeScript, 1, s.cacheKey(key)))
if errors.Is(err, redis.ErrNil) {
s.cacheMetrics().recordMiss()
return nil, false, nil
}
if err != nil {
return nil, false, err
}
s.cacheMetrics().recordHit()
return value, true, nil
}
func (s *RedisStore) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error {
if err := ctxErr(ctx); err != nil {
return err
}
key = normalizeKey(key)
if key == "" {
return ErrKeyRequired
}
if err := s.ready(); err != nil {
return err
}
if ttl == 0 {
ttl = s.defaultTTL
}
if ttl > 0 {
_, err := s.do(ctx, "PSETEX", s.cacheKey(key), ttlMillis(ttl), cloneBytes(value))
return err
}
_, err := s.do(ctx, "SET", s.cacheKey(key), cloneBytes(value))
return err
}
func (s *RedisStore) Delete(ctx context.Context, key string) error {
if err := ctxErr(ctx); err != nil {
return err
}
key = normalizeKey(key)
if key == "" {
return ErrKeyRequired
}
if err := s.ready(); err != nil {
return err
}
_, err := s.do(ctx, "DEL", s.cacheKey(key))
return err
}
func (s *RedisStore) Snapshot() Snapshot {
if s == nil {
return Snapshot{Kind: "redis"}
}
return Snapshot{
Name: s.name,
Kind: "redis",
KeyPrefix: s.keyPrefix,
}
}
func (s *RedisStore) Close() error {
if s == nil || s.closer == nil {
return nil
}
return s.closer.Close()
}
func (s *RedisStore) ready() error {
if s == nil || s.executor == nil {
return ErrNilRedisExecutor
}
return nil
}
func (s *RedisStore) do(ctx context.Context, command string, args ...any) (any, error) {
if err := s.ready(); err != nil {
return nil, err
}
return s.executor.Do(ctx, command, args...)
}
func (s *RedisStore) cacheMetrics() *Metrics {
if s == nil {
return nil
}
s.metricsMu.RLock()
defer s.metricsMu.RUnlock()
return s.metrics
}
func (s *RedisStore) cacheKey(key string) string {
if s == nil || s.keyPrefix == "" {
return key
}
return s.keyPrefix + ":" + key
}
func normRedisOpts(options RedisOptions) RedisOptions {
options.Name = strings.TrimSpace(options.Name)
if options.Name == "" {
options.Name = "redis-cache"
}
options.KeyPrefix = strings.TrimSpace(options.KeyPrefix)
if options.KeyPrefix == "" {
options.KeyPrefix = "cache"
}
options.Address = strings.TrimSpace(options.Address)
if options.Address == "" {
options.Address = "127.0.0.1:6379"
}
if options.PoolSize <= 0 {
options.PoolSize = 8
}
if options.Timeout <= 0 {
options.Timeout = 2 * time.Second
}
if options.ConnectTimeout <= 0 {
options.ConnectTimeout = options.Timeout
}
if options.ReadTimeout <= 0 {
options.ReadTimeout = options.Timeout
}
if options.WriteTimeout <= 0 {
options.WriteTimeout = options.Timeout
}
return options
}
func ttlMillis(ttl time.Duration) int64 {
ms := int64(ttl / time.Millisecond)
if ms <= 0 {
return 1
}
return ms
}