From 300fdbd95709db48a2901a8c06c3a5d774a9b462 Mon Sep 17 00:00:00 2001 From: lixiangwuxian Date: Sun, 9 Mar 2025 14:34:06 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96Steam=E6=B8=B8?= =?UTF-8?q?=E6=88=8F=E7=8A=B6=E6=80=81=E6=A3=80=E6=9F=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/steamplaying/service.go | 27 +++++++++++++++----- handler/steamplaying/steam_playing.go | 36 ++++++++++++++++++++------- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/handler/steamplaying/service.go b/handler/steamplaying/service.go index ee0fe6f..47ba2f3 100644 --- a/handler/steamplaying/service.go +++ b/handler/steamplaying/service.go @@ -315,15 +315,30 @@ func checkDiffSteamGameStatus(steamID []string, lastTimeStat map[string]string) return nil, nil, glbErr } gameStatusListStr := map[string]string{} + currentGameStatus := map[string]string{} // 用于记录当前所有玩家的游戏状态 + for _, userState := range Players { - if lastTimeStat[userState.SteamID] == userState.GameID { - continue + if userState.GameID != "" { + currentGameStatus[userState.SteamID] = userState.GameID + // 只有当玩家的游戏状态发生变化时才添加到通知列表 + if lastGameID, exists := lastTimeStat[userState.SteamID]; !exists || lastGameID != userState.GameID { + if userState.ToGameStatus() != "" { + gameStatusListStr[userState.SteamID] = userState.ToGameStatus() + } + } } - if 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 } diff --git a/handler/steamplaying/steam_playing.go b/handler/steamplaying/steam_playing.go index 156058e..d687fa6 100644 --- a/handler/steamplaying/steam_playing.go +++ b/handler/steamplaying/steam_playing.go @@ -211,10 +211,17 @@ func RoundCheckSteamPlaying() { } // 更新退出游戏信息 - for steamId, oldGameStatus := range playingMap { - if newGameStatus, exists := gamePlayingMap[steamId]; !exists { - // 玩家退出游戏,记录退出信息 - // 从API响应中获取玩家信息以获取GameID + for steamId := range playingMap { + var currentGameID string + for _, player := range players { + if player.SteamID == steamId { + currentGameID = player.GameID + break + } + } + + if currentGameID == "" { // 玩家退出游戏 + // 从API响应中获取玩家之前的游戏信息 for _, player := range players { if player.SteamID == steamId { exitInfoMap[steamId] = GameExitInfo{ @@ -224,9 +231,18 @@ func RoundCheckSteamPlaying() { break } } - } else if oldGameStatus != newGameStatus { - // 玩家切换了游戏,清除退出记录 - delete(exitInfoMap, steamId) + } else { // 玩家在玩游戏 + if oldStatus, exists := playingMap[steamId]; exists && oldStatus != currentGameID { + // 玩家切换了游戏,清除退出记录 + delete(exitInfoMap, steamId) + } + } + } + + // 处理新上线的玩家 + for steamId := range gamePlayingMap { + if _, exists := playingMap[steamId]; !exists { + delete(exitInfoMap, steamId) // 新上线的玩家,清除可能存在的退出记录 } } @@ -282,8 +298,10 @@ func RoundCheckSteamPlaying() { // 更新playingMap playingMap = make(map[string]string) - for k, v := range gamePlayingMap { - playingMap[k] = v + for _, player := range players { + if player.GameID != "" { + playingMap[player.SteamID] = player.GameID + } } once = false