服务端(Go): - GM 塞背包/带属性装备发放:在线角色推送 op14 增量物品行,背包实时刷新,无需回角色选择界面 - GM 改金币/改等级:去掉在线拒绝,落库后推送 op14 钱包/op37 等级状态,在线实时刷新 - 等级端点统一切换到新版 GMSetCharacterLevel(自带在线刷新) - 无限/锁死疲劳模式 + 燃烧疲劳:每房间消耗 2 点、打怪与通关经验双倍 - 燃烧疲劳开关时给所有在线角色推送系统提示(op12 mode 0) - 深渊史诗掉率:GM 全局倍率 + 城镇增益药剂窗口倍率,真实接入掉落结算 启动器(Python/JS): - 标题栏自绘、窗口 1204x717、禁止拖拽改大小、仅最小化/关闭 - GM 工具:疲劳与燃烧、深渊史诗掉率、拍卖行上架(单一定价对齐) - 拍卖行上架前端对齐服务端单一价格语义,修复 400 报错
116 lines
2.8 KiB
Go
116 lines
2.8 KiB
Go
package replay
|
||
|
||
import (
|
||
"context"
|
||
"crypto/sha256"
|
||
"encoding/hex"
|
||
"errors"
|
||
"fmt"
|
||
"math"
|
||
"strings"
|
||
"time"
|
||
|
||
"longheng.io/server/internal/platform/db"
|
||
)
|
||
|
||
var (
|
||
// ErrReplayRedisExecutorRequired 保留旧 API 兼容;新代码统一用 ErrReplayStoreUnavailable 判断装配缺失。
|
||
ErrReplayRedisExecutorRequired = ErrReplayStoreUnavailable
|
||
// ErrReplayRedisOperationFailed 通过 errors.Is 暴露 Redis 写入失败,上层应拒绝请求并让调用方重试。
|
||
ErrReplayRedisOperationFailed = errors.New("replay redis operation failed")
|
||
// ErrReplayRedisUnexpectedReply 通过 errors.Is 暴露驱动返回值异常,避免未知结果被当成放行。
|
||
ErrReplayRedisUnexpectedReply = errors.New("replay redis returned unexpected reply")
|
||
)
|
||
|
||
// RedisOptions 是 Redis 重放保护 store 的装配参数。
|
||
type RedisOptions struct {
|
||
Executor db.RedisExecutor
|
||
KeyPrefix string
|
||
}
|
||
|
||
// RedisStore 使用 Redis SET NX EX 实现跨进程 nonce 去重。
|
||
type RedisStore struct {
|
||
executor db.RedisExecutor
|
||
keyPrefix string
|
||
}
|
||
|
||
const redisSeenScript = `
|
||
local ok = redis.call("SET", KEYS[1], "1", "NX", "EX", ARGV[1])
|
||
if ok then
|
||
return 1
|
||
end
|
||
return 0
|
||
`
|
||
|
||
// NewRedisStore 创建 Redis 重放保护 store;KeyPrefix 为空时使用框架默认前缀。
|
||
func NewRedisStore(opts RedisOptions) *RedisStore {
|
||
prefix := strings.Trim(strings.TrimSpace(opts.KeyPrefix), ":")
|
||
if prefix == "" {
|
||
prefix = "longheng:replay"
|
||
}
|
||
return &RedisStore{executor: opts.Executor, keyPrefix: prefix}
|
||
}
|
||
|
||
// Seen 记录 nonce 并返回它是否首次出现;Redis 不可用时不会放行请求。
|
||
func (s *RedisStore) Seen(ctx context.Context, key string, ttl time.Duration) (bool, error) {
|
||
ctx, key, err := normalizeInput(ctx, key, ttl)
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
if s == nil || s.executor == nil {
|
||
return false, ErrReplayStoreUnavailable
|
||
}
|
||
reply, err := s.executor.Do(ctx, "EVAL", redisSeenScript, 1, s.redisKey(key), ttlSeconds(ttl))
|
||
if err != nil {
|
||
return false, wrapRedisError(err)
|
||
}
|
||
return parseSeenReply(reply)
|
||
}
|
||
|
||
func (s *RedisStore) redisKey(key string) string {
|
||
sum := sha256.Sum256([]byte(key))
|
||
return s.keyPrefix + ":" + hex.EncodeToString(sum[:])
|
||
}
|
||
|
||
func ttlSeconds(ttl time.Duration) int64 {
|
||
seconds := int64(math.Ceil(ttl.Seconds()))
|
||
if seconds < 1 {
|
||
return 1
|
||
}
|
||
return seconds
|
||
}
|
||
|
||
func parseSeenReply(reply any) (bool, error) {
|
||
switch v := reply.(type) {
|
||
case int:
|
||
if v == 1 {
|
||
return true, nil
|
||
}
|
||
if v == 0 {
|
||
return false, nil
|
||
}
|
||
case int64:
|
||
if v == 1 {
|
||
return true, nil
|
||
}
|
||
if v == 0 {
|
||
return false, nil
|
||
}
|
||
case string:
|
||
switch strings.TrimSpace(v) {
|
||
case "1":
|
||
return true, nil
|
||
case "0":
|
||
return false, nil
|
||
}
|
||
case []byte:
|
||
switch strings.TrimSpace(string(v)) {
|
||
case "1":
|
||
return true, nil
|
||
case "0":
|
||
return false, nil
|
||
}
|
||
}
|
||
return false, fmt.Errorf("%w: %T", ErrReplayRedisUnexpectedReply, reply)
|
||
}
|