game_sync/worldsrv/permitmgr.go

77 lines
2.0 KiB
Go

package main
import (
"encoding/json"
"time"
"mongo.games.com/goserver/core/basic"
"mongo.games.com/goserver/core/logger"
"mongo.games.com/goserver/core/task"
"mongo.games.com/game/common"
"mongo.games.com/game/model"
)
var PermitMgrInst = new(PermitMgr)
type PermitMgr struct {
BaseClockSinker
}
func (r *PermitMgr) InterestClockEvent() int {
return 1 << CLOCK_EVENT_DAY
}
func (r *PermitMgr) OnDayTimer() {
logger.Logger.Info("(this *PermitMgr) OnDayTimer")
now := time.Now()
for _, v := range PlatformMgrSingleton.GetPlatforms() {
pl := PlatformMgrSingleton.GetConfig(v.IdStr).ActPermitConfig
if pl == nil {
continue
}
b := model.GetStrKVGameData(common.PermitStartTsKey + v.IdStr)
if b == "" {
logger.Logger.Errorf("(this *PermitMgr) OnDayTimer GetStrKVGameData not found")
continue
}
se := new(model.PermitStartTs)
if err := json.Unmarshal([]byte(b), se); err != nil {
logger.Logger.Errorf("(this *PermitMgr) OnDayTimer json.Unmarshal err:%v", err)
continue
}
if se.StartTs <= now.Unix() && now.Unix() < se.EndTs {
// 在活动内
continue
}
if now.Unix() < se.StartTs {
// 活动未开始
continue
}
if now.Unix() >= se.EndTs {
// 活动已结束, 新开始
se.StartTs = se.EndTs
se.EndTs = se.EndTs + int64(pl.Days*24*3600)
if now.Unix() < se.StartTs || now.Unix() >= se.EndTs {
se.StartTs = common.GetDayStartTs(now.Unix())
se.EndTs = se.StartTs + int64(pl.Days*24*3600)
}
PlatformMgrSingleton.GetConfig(v.IdStr).PermitStartTs = se.StartTs
PlatformMgrSingleton.GetConfig(v.IdStr).PermitEndTs = se.EndTs
b, err := json.Marshal(se)
if err != nil {
logger.Logger.Errorf("(this *PermitMgr) OnDayTimer json.Marshal err:%v", err)
continue
}
task.New(nil, task.CallableWrapper(func(o *basic.Object) interface{} {
model.UptStrKVGameData(common.PermitStartTsKey+v.IdStr, string(b))
return nil
}), nil).StartByExecutor("permit_start_ts")
}
}
}
func init() {
ClockMgrSington.RegisteSinker(PermitMgrInst)
}