feat: 为Steam游戏状态检查添加重复登录游戏的防重复通知机制

优化Steam游戏状态检查逻辑,增加了以下特性:
1. 记录玩家游戏退出信息
2. 实现30分钟内重复登录同一游戏时抑制通知
3. 修改checkDiffSteamGameStatus函数返回值,增加玩家信息返回
4. 更新playingMap的方式更加安全和清晰
This commit is contained in:
lixiangwuxian 2025-03-09 01:04:26 +08:00
parent 2eb9716d24
commit 3a26404755
2 changed files with 58 additions and 5 deletions

View File

@ -248,9 +248,9 @@ func checkSteamGameStatus(steamID []string) (string, error) {
return gameStatusList, nil
}
func checkDiffSteamGameStatus(steamID []string, lastTimeStat map[string]string) (map[string]string, error) {
func checkDiffSteamGameStatus(steamID []string, lastTimeStat map[string]string) (map[string]string, []PlayerSummary, error) {
if len(steamID) == 0 {
return nil, nil
return nil, nil, nil
}
var glbErr error
var writeMutex sync.Mutex
@ -312,7 +312,7 @@ func checkDiffSteamGameStatus(steamID []string, lastTimeStat map[string]string)
}
wg.Wait()
if glbErr != nil {
return nil, glbErr
return nil, nil, glbErr
}
gameStatusListStr := map[string]string{}
for _, userState := range Players {
@ -324,7 +324,7 @@ func checkDiffSteamGameStatus(steamID []string, lastTimeStat map[string]string)
}
lastTimeStat[userState.SteamID] = userState.GameID
}
return gameStatusListStr, nil
return gameStatusListStr, Players, nil
}
func checkSteamIDValid(steamID string) (bool, error) {

View File

@ -13,6 +13,12 @@ import (
"git.lxtend.com/qqbot/util"
)
// 用于记录玩家退出游戏的信息
type GameExitInfo struct {
GameID string // 游戏ID
ExitTime time.Time // 退出时间
}
func init() {
// Register the handler with the server
handler.RegisterHandler("绑steam", bindSteam, constants.LEVEL_USER)
@ -177,6 +183,9 @@ func checkSteamPlaying(msg model.Message) model.Reply {
func RoundCheckSteamPlaying() {
once := true
playingMap := map[string]string{}
// 记录玩家退出游戏的信息
exitInfoMap := map[string]GameExitInfo{}
util.AddCycleTask("checkSteamPlaying", 15*time.Second, 15*time.Second, func() {
allSteamIDs, err := getAllSteamID()
if err != nil {
@ -188,12 +197,32 @@ func RoundCheckSteamPlaying() {
fmt.Println("获取群列表失败: ", err)
return
}
gamePlayingMap, err := checkDiffSteamGameStatus(allSteamIDs, playingMap)
gamePlayingMap, players, err := checkDiffSteamGameStatus(allSteamIDs, playingMap)
if err != nil {
fmt.Println("获取游戏列表失败: ", err)
return
}
// 更新退出游戏信息
for steamId, oldGameStatus := range playingMap {
if newGameStatus, exists := gamePlayingMap[steamId]; !exists {
// 玩家退出游戏,记录退出信息
// 从API响应中获取玩家信息以获取GameID
for _, player := range players {
if player.SteamID == steamId {
exitInfoMap[steamId] = GameExitInfo{
GameID: player.GameID,
ExitTime: time.Now(),
}
break
}
}
} else if oldGameStatus != newGameStatus {
// 玩家切换了游戏,清除退出记录
delete(exitInfoMap, steamId)
}
}
for _, group := range groups {
time.Sleep(15 * time.Second)
users, err := getSteamUsersInGroup(group)
@ -209,6 +238,23 @@ func RoundCheckSteamPlaying() {
var gameList []string
for _, steamId := range steamIds {
if gameStatus, ok := gamePlayingMap[steamId]; ok {
// 检查是否是30分钟内重新登录同一游戏
if exitInfo, exists := exitInfoMap[steamId]; exists {
// 从API响应中获取当前游戏ID
var currentGameID string
for _, player := range players {
if player.SteamID == steamId {
currentGameID = player.GameID
break
}
}
if exitInfo.GameID == currentGameID && time.Since(exitInfo.ExitTime) <= 30*time.Minute {
// 30分钟内重新登录同一游戏不发送通知
continue
}
// 清除退出记录
delete(exitInfoMap, steamId)
}
gameList = append(gameList, gameStatus)
}
}
@ -222,6 +268,13 @@ func RoundCheckSteamPlaying() {
action.ActionManager.SendMsg(msg)
}
}
// 更新playingMap
playingMap = make(map[string]string)
for k, v := range gamePlayingMap {
playingMap[k] = v
}
once = false
})
}