1
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful

This commit is contained in:
昔念
2026-02-20 23:33:24 +08:00
parent f6b583575a
commit 8e3ed21a3a
9 changed files with 105 additions and 28 deletions

View File

@@ -7,7 +7,10 @@ import (
"blazing/modules/player/model"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
@@ -175,6 +178,54 @@ func (s *InfoService) Kick(id uint32) error {
}
return nil
}
// saveToLocalFile 兜底保存将数据写入本地lose文件夹
func (s *InfoService) saveToLocalFile(player *model.Player, err error) {
// 1. 创建lose文件夹如果不存在
loseDir := "./lose"
if err := os.MkdirAll(loseDir, 0755); err != nil {
fmt.Printf("[ERROR] 创建lose文件夹失败: %v\n", err)
return
}
// 2. 构造保存的数据结构,包含错误信息和时间戳
type FallbackData struct {
PlayerData *model.Player `json:"player_data"`
ErrorMsg string `json:"error_msg"`
SaveTime string `json:"save_time"`
ServerInfo string `json:"server_info"`
}
fallbackData := FallbackData{
PlayerData: player,
ErrorMsg: err.Error(),
SaveTime: time.Now().Format("20060102150405.000"), // 精确到毫秒的时间戳
ServerInfo: fmt.Sprintf("server_vip:%d", cool.Config.ServerInfo.IsVip),
}
// 3. 生成唯一的文件名(避免覆盖)
playerID := fmt.Sprintf("%d", player.PlayerID) // 假设Player有PlayerID字段根据实际调整
filename := fmt.Sprintf("player_%s_%s.json", playerID, fallbackData.SaveTime)
filePath := filepath.Join(loseDir, filename)
// 4. 将数据序列化为JSON并写入文件
file, err := os.Create(filePath)
if err != nil {
fmt.Printf("[ERROR] 创建兜底文件失败 %s: %v\n", filePath, err)
return
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ") // 格式化JSON方便查看
if err := encoder.Encode(fallbackData); err != nil {
fmt.Printf("[ERROR] 写入兜底文件失败 %s: %v\n", filePath, err)
return
}
// 5. 记录日志,方便排查
fmt.Printf("[INFO] 数据库保存失败,已将玩家[%s]数据兜底保存到: %s\n", playerID, filePath)
}
func (s *InfoService) Save(data model.PlayerInfo) {
if cool.Config.ServerInfo.IsVip != 0 {
@@ -190,6 +241,7 @@ func (s *InfoService) Save(data model.PlayerInfo) {
_, err := m.Save(tt)
if err != nil {
//todo 待实现兜底保存,现在有可能出错
s.saveToLocalFile(tt, err)
panic(err)
}