game_sync/worldsrv/playeronlinegame.go

126 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"slices"
"time"
"golang.org/x/exp/maps"
"mongo.games.com/goserver/core/logger"
"mongo.games.com/goserver/core/module"
"mongo.games.com/game/common"
"mongo.games.com/game/model"
"mongo.games.com/game/mq"
"mongo.games.com/game/worldsrv/internal"
)
func init() {
module.RegisteModule(PlayerOnlineGameSingleton, 10*time.Second, 0)
internal.RegisterPlayerListenerFunc(&internal.PlayerListenerFunc[*Player, *Scene]{
OnPlayerEnterSceneAfterFunc: func(p *Player, s *Scene) {
if p == nil || p.IsRob {
return
}
PlayerOnlineGameSingleton.Check = true
},
OnPlayerLeaveSceneAfterFunc: func(p *Player, s *Scene) {
if p == nil || p.IsRob {
return
}
PlayerOnlineGameSingleton.Check = true
},
})
}
var PlayerOnlineGameSingleton = &PlayerOnlineGameEvent{
Online: make(map[string]map[string]map[string]map[int]map[int]int),
}
type PlayerOnlineGameEvent struct {
Online map[string]map[string]map[string]map[int]map[int]int // 平台推广渠道游戏组游戏id场次id在线人数
Check bool
}
func (p *PlayerOnlineGameEvent) ModuleName() string {
return "PlayerOnlineGameEvent"
}
func (p *PlayerOnlineGameEvent) Init() {
}
func (p *PlayerOnlineGameEvent) Update() {
if !p.Check {
return
}
p.Check = false
onlineCh := make(map[string]map[string]map[string]map[int]map[int]int)
for _, v := range SceneMgrSingleton.scenes {
if v == nil {
continue
}
plt := SceneMgrSingleton.GetPlatformBySceneId(v.sceneId)
// 平台
if _, ok := onlineCh[plt]; !ok {
onlineCh[plt] = map[string]map[string]map[int]map[int]int{}
}
for _, player := range v.players {
if player == nil || player.IsRob || player.Platform == common.Platform_Rob && player.Channel == common.Channel_Rob {
continue
}
// 渠道
if _, ok := onlineCh[plt][player.ChannelId]; !ok {
onlineCh[plt][player.ChannelId] = map[string]map[int]map[int]int{}
}
// 游戏组
if _, ok := onlineCh[plt][player.ChannelId][v.dbGameFree.GetGameDif()]; !ok {
onlineCh[plt][player.ChannelId][v.dbGameFree.GetGameDif()] = map[int]map[int]int{}
}
// 游戏id
if _, ok := onlineCh[plt][player.ChannelId][v.dbGameFree.GetGameDif()][int(v.dbGameFree.GetGameId())]; !ok {
onlineCh[plt][player.ChannelId][v.dbGameFree.GetGameDif()][int(v.dbGameFree.GetGameId())] = map[int]int{}
}
onlineCh[plt][player.ChannelId][v.dbGameFree.GetGameDif()][int(v.dbGameFree.GetGameId())][int(v.dbGameFree.GetId())] += 1
}
}
// 判断差异
if !slices.Equal(maps.Keys(p.Online), maps.Keys(onlineCh)) {
goto here
}
for k, v := range p.Online {
if !slices.Equal(maps.Keys(v), maps.Keys(onlineCh[k])) {
goto here
}
for k1, v1 := range v {
if !slices.Equal(maps.Keys(v1), maps.Keys(onlineCh[k][k1])) {
goto here
}
for k2, v2 := range v1 {
if !slices.Equal(maps.Keys(v2), maps.Keys(onlineCh[k][k1][k2])) {
goto here
}
for k3, v3 := range v2 {
if !slices.Equal(maps.Keys(v3), maps.Keys(onlineCh[k][k1][k2][k3])) {
goto here
}
}
}
}
}
here:
p.Online = onlineCh
mq.Write(model.GenerateOnlineGame(p.Online))
logger.Logger.Trace("PlayerOnlineGameEvent", p.Online)
}
func (p *PlayerOnlineGameEvent) Shutdown() {
module.UnregisteModule(p)
}