game_sync/worldsrv/playernotify.go

112 lines
2.5 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"
"mongo.games.com/goserver/core/logger"
"mongo.games.com/game/common"
"mongo.games.com/game/protocol/gamehall"
)
func init() {
common.ClockMgrSingleton.RegisterSinker(PlayerNotifySingle)
}
var PlayerNotifySingle = &PlayerNotify{
players: make(map[int32]map[int32]*PlayerNotifyInfo),
}
type PlayerNotifyInfo struct {
SnId int32 // 玩家id
Ts int64 // 失效时间戳
}
type PlayerNotify struct {
common.BaseClockSinker
players map[int32]map[int32]*PlayerNotifyInfo // 消息类型玩家id玩家信息
}
func (p *PlayerNotify) InterestClockEvent() int {
return 1 << common.ClockEventMinute
}
func (p *PlayerNotify) OnMiniTimer() {
now := time.Now()
for _, v := range p.players {
var ids []int32
for k, vv := range v {
if vv == nil || vv.Ts <= now.Unix() {
ids = append(ids, k)
}
}
for _, id := range ids {
delete(v, id)
}
}
}
// AddTime 延长某个类型消息的通知时间
// snid 玩家id
// tp 消息类型
// d 延长时间
func (p *PlayerNotify) AddTime(snid int32, tp common.NotifyType, d time.Duration) {
if _, ok := p.players[int32(tp)]; !ok {
p.players[int32(tp)] = make(map[int32]*PlayerNotifyInfo)
}
p.players[int32(tp)][snid] = &PlayerNotifyInfo{
SnId: snid,
Ts: time.Now().Add(d).Unix(),
}
}
// GetPlayers 获取某个类型消息的玩家id
// tp 消息类型
func (p *PlayerNotify) GetPlayers(tp common.NotifyType) []int32 {
now := time.Now()
var ret []int32
for k, v := range p.players[int32(tp)] {
if v == nil || v.Ts <= now.Unix() {
continue
}
ret = append(ret, k)
}
return ret
}
// SendToClient 发送消息给客户端
// tp 消息类型
func (p *PlayerNotify) SendToClient(tp common.NotifyType, packetId int, pack interface{}) {
switch tp {
case common.NotifyPrivateRoomList:
d := pack.(*gamehall.SCGetPrivateRoomList)
if len(d.GetDatas()) == 0 {
return
}
scene := SceneMgrSingleton.GetScene(int(d.GetDatas()[0].GetRoomId()), true)
if scene == nil {
return
}
roomConfigId := d.GetDatas()[0].GetRoomConfigId()
cfg := PlatformMgrSingleton.GetConfig(scene.limitPlatform.IdStr).RoomConfig[roomConfigId]
if cfg == nil {
return
}
var ids []int32
for _, v := range p.GetPlayers(tp) {
player := PlayerMgrSington.GetPlayerBySnId(v)
if player == nil {
continue
}
if slices.Contains(cfg.GetOnChannelName(), player.LastChannel) {
ids = append(ids, v)
}
}
PlayerMgrSington.BroadcastMessageToTarget(ids, packetId, pack)
logger.Logger.Tracef("PlayerNotify SendToClient tp:%v ids:%v", tp, ids)
}
}