refactor: 优化Steam游戏状态检查逻辑

This commit is contained in:
lixiangwuxian 2025-03-09 14:34:06 +08:00
parent db054c6a70
commit 300fdbd957
2 changed files with 48 additions and 15 deletions

View File

@ -315,15 +315,30 @@ func checkDiffSteamGameStatus(steamID []string, lastTimeStat map[string]string)
return nil, nil, glbErr return nil, nil, glbErr
} }
gameStatusListStr := map[string]string{} gameStatusListStr := map[string]string{}
currentGameStatus := map[string]string{} // 用于记录当前所有玩家的游戏状态
for _, userState := range Players { for _, userState := range Players {
if lastTimeStat[userState.SteamID] == userState.GameID { if userState.GameID != "" {
continue currentGameStatus[userState.SteamID] = userState.GameID
} // 只有当玩家的游戏状态发生变化时才添加到通知列表
if lastGameID, exists := lastTimeStat[userState.SteamID]; !exists || lastGameID != userState.GameID {
if userState.ToGameStatus() != "" { if userState.ToGameStatus() != "" {
gameStatusListStr[userState.SteamID] = userState.ToGameStatus() gameStatusListStr[userState.SteamID] = userState.ToGameStatus()
} }
lastTimeStat[userState.SteamID] = userState.GameID
} }
}
}
// 检查是否有玩家退出游戏
for steamID, lastGameID := range lastTimeStat {
if lastGameID != "" {
if currentGameID, exists := currentGameStatus[steamID]; !exists || currentGameID == "" {
// 玩家已退出游戏,不需要添加到通知列表,但需要更新状态
currentGameStatus[steamID] = ""
}
}
}
return gameStatusListStr, Players, nil return gameStatusListStr, Players, nil
} }

View File

@ -211,10 +211,17 @@ func RoundCheckSteamPlaying() {
} }
// 更新退出游戏信息 // 更新退出游戏信息
for steamId, oldGameStatus := range playingMap { for steamId := range playingMap {
if newGameStatus, exists := gamePlayingMap[steamId]; !exists { var currentGameID string
// 玩家退出游戏,记录退出信息 for _, player := range players {
// 从API响应中获取玩家信息以获取GameID if player.SteamID == steamId {
currentGameID = player.GameID
break
}
}
if currentGameID == "" { // 玩家退出游戏
// 从API响应中获取玩家之前的游戏信息
for _, player := range players { for _, player := range players {
if player.SteamID == steamId { if player.SteamID == steamId {
exitInfoMap[steamId] = GameExitInfo{ exitInfoMap[steamId] = GameExitInfo{
@ -224,11 +231,20 @@ func RoundCheckSteamPlaying() {
break break
} }
} }
} else if oldGameStatus != newGameStatus { } else { // 玩家在玩游戏
if oldStatus, exists := playingMap[steamId]; exists && oldStatus != currentGameID {
// 玩家切换了游戏,清除退出记录 // 玩家切换了游戏,清除退出记录
delete(exitInfoMap, steamId) delete(exitInfoMap, steamId)
} }
} }
}
// 处理新上线的玩家
for steamId := range gamePlayingMap {
if _, exists := playingMap[steamId]; !exists {
delete(exitInfoMap, steamId) // 新上线的玩家,清除可能存在的退出记录
}
}
for _, group := range groups { for _, group := range groups {
time.Sleep(15 * time.Second) time.Sleep(15 * time.Second)
@ -282,8 +298,10 @@ func RoundCheckSteamPlaying() {
// 更新playingMap // 更新playingMap
playingMap = make(map[string]string) playingMap = make(map[string]string)
for k, v := range gamePlayingMap { for _, player := range players {
playingMap[k] = v if player.GameID != "" {
playingMap[player.SteamID] = player.GameID
}
} }
once = false once = false