83 lines
1.8 KiB
Go
83 lines
1.8 KiB
Go
package main
|
||
|
||
import (
|
||
"time"
|
||
|
||
"mongo.games.com/goserver/core/logger"
|
||
|
||
"mongo.games.com/game/common"
|
||
)
|
||
|
||
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{}) {
|
||
ids := p.GetPlayers(tp)
|
||
PlayerMgrSington.BroadcastMessageToTarget(ids, packetId, pack)
|
||
logger.Logger.Tracef("PlayerNotify SendToClient tp:%v ids:%v", tp, ids)
|
||
}
|