印度包网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
3.3 KiB

1 year ago
package hall
import (
"fmt"
"math/rand"
"server/call"
"server/common"
"server/pb"
"time"
"github.com/gogo/protobuf/proto"
"github.com/liangdas/mqant/gate"
"github.com/liangdas/mqant/log"
timewheel "github.com/liangdas/mqant/module/modules/timer"
)
var (
broadcasts = map[int]*oneBroadcast{}
)
type oneBroadcast struct {
content string
id int
priority int
frequency int
interval int
targetID int
sub subBroadcast
}
type subBroadcast interface {
stop()
}
func startBroadcast() {
stopAll()
all := call.GetConfigBroadcast()
for _, v := range all {
if v.Open == 0 {
continue
}
one := &oneBroadcast{
id: v.ID,
priority: v.Priority,
frequency: v.LoopFrequency,
interval: v.Interval,
content: v.Content,
targetID: v.TargetID,
}
broadcasts[v.ID] = one
one.NewSub()
}
}
func (b *oneBroadcast) stopBroadcast() {
timewheel.GetTimeWheel().RemoveTimer(b.id)
}
func (b *oneBroadcast) NewSub() {
switch b.targetID {
case common.BrocastIDAll:
b.broadcast()
default:
return
}
}
func (b *oneBroadcast) broadcast() {
msg := &pb.BroadcastMsg{
Content: b.content,
Priority: uint32(b.priority),
Loop: int64(b.frequency),
Interval: int64(b.interval),
}
// log.Debug("broadcast:%+v", *msg)
send, _ := proto.Marshal(msg)
broadcastNatsImp(send)
// if b.frequency == -1 {
// timewheel.GetTimeWheel().AddTimerCustom(time.Duration(b.interval)*time.Second, b.id, nil, func(arge interface{}) {
// b.broadcast()
// })
// return
// }
// if b.frequency > 0 {
// b.frequency--
// timewheel.GetTimeWheel().AddTimerCustom(time.Duration(b.interval)*time.Second, b.id, nil, func(arge interface{}) {
// b.broadcast()
// })
// }
}
func stopAll() {
for _, v := range broadcasts {
v.stopBroadcast()
if v.sub != nil {
v.sub.stop()
}
}
broadcasts = map[int]*oneBroadcast{}
}
type redSub struct {
sid string
*oneBroadcast
}
func (s *redSub) stop() {
timewheel.GetTimeWheel().RemoveTimer(s.sid)
}
// 红包活动广播,9:00-16:00随机播放一次,16:00-24:00随机播放一次
func (s *redSub) broadcast() {
hour, _, _ := time.Now().Clock()
diff := 0
s.stop()
if hour < 16 {
diff = 16 - hour
timewheel.GetTimeWheel().AddTimerCustom(time.Duration(diff)*time.Hour, s.sid, nil, func(arge interface{}) {
s.broadcast()
})
} else {
diff = 24 - hour
timewheel.GetTimeWheel().AddTimerCustom(time.Duration(diff+9)*time.Hour, s.sid, nil, func(arge interface{}) {
s.broadcast()
})
}
rhour := rand.Intn(diff)
rmin := rand.Intn(60)
rsec := rand.Intn(60)
after := time.Duration(rhour)*time.Hour + time.Duration(rmin)*time.Minute + time.Duration(rsec)*time.Second
timewheel.GetTimeWheel().AddTimerCustom(after, s.id, nil, func(arge interface{}) {
s.oneBroadcast.broadcast()
})
}
// 广播协议
func broadcastMsg(pid int, msg proto.Message) {
var session gate.Session
sids := ""
playerIdMap.Range(func(key, value interface{}) bool {
p := value.(*player)
if session == nil {
session = p.session
}
sids += p.session.GetSessionID() + ","
return true
})
log.Debug("broadcast pid:%v,msg:%v,sids:%v", pid, msg, sids)
if sids == "" {
return
}
sids = sids[:len(sids)-1]
data, _ := proto.Marshal(msg)
session.SendBatch(sids, fmt.Sprintf("%v:%v", int(pb.ServerType_ServerTypeCommon), pid), data)
}