game_sync/worldsrv/awardlogmgr.go

150 lines
4.1 KiB
Go

package main
import (
"time"
"mongo.games.com/game/common"
"mongo.games.com/game/model"
"mongo.games.com/game/srvdata"
"mongo.games.com/goserver/core/basic"
"mongo.games.com/goserver/core/logger"
"mongo.games.com/goserver/core/module"
"mongo.games.com/goserver/core/task"
)
type AwardLogManager struct {
BaseClockSinker
AnnouncerLog map[string]map[int32][]model.AnnouncerLog //key:1话费 2实物
}
var AwardLogMgr = &AwardLogManager{
AnnouncerLog: make(map[string]map[int32][]model.AnnouncerLog),
}
func (this *AwardLogManager) ModuleName() string {
return "AwardLogManager"
}
// GetAwardLog 获取总数量
// typeId 1 话费 2实物
func (this *AwardLogManager) GetAwardLog(plt string, typeId int32) map[int32]int64 {
d := PlatformMgrSingleton.GetConfig(plt).AwardItem
ret := make(map[int32]int64)
// 1 话费 2实物
switch typeId {
case 1:
case 2:
for _, v := range srvdata.GameItemMgr.GetArr(plt) {
if v.GetType() == common.ItemTypeObjective {
if d.AwardMap[v.GetId()] > 0 {
ret[v.GetId()] += d.AwardMap[v.GetId()]
}
}
}
}
return ret
}
func (this *AwardLogManager) GetAnnouncerLog(plt string, typeId int32) []model.AnnouncerLog {
data := this.AnnouncerLog[plt]
if data == nil {
data = make(map[int32][]model.AnnouncerLog)
this.AnnouncerLog[plt] = data
}
log := data[typeId]
if len(log) > 100 {
return log[len(log)-100:]
}
return log
}
// 已兑换数据
func (this *AwardLogManager) UpdateAwardLog(plt string, itemId int32, num int64) {
PlatformMgrSingleton.AddAwardItem(plt, itemId, num)
}
// 实时播报数据
func (this *AwardLogManager) UpdateAnnouncerLog(data model.AnnouncerLog) {
data.Ts = time.Now()
if this.AnnouncerLog == nil {
this.AnnouncerLog = make(map[string]map[int32][]model.AnnouncerLog)
}
if this.AnnouncerLog[data.Platform] == nil {
this.AnnouncerLog[data.Platform] = make(map[int32][]model.AnnouncerLog)
}
this.AnnouncerLog[data.Platform][data.TypeId] = append(this.AnnouncerLog[data.Platform][data.TypeId], data)
if len(this.AnnouncerLog[data.Platform][data.TypeId]) > 100 {
this.AnnouncerLog[data.Platform][data.TypeId] = this.AnnouncerLog[data.Platform][data.TypeId][len(this.AnnouncerLog[data.Platform][data.TypeId])-100:]
}
logger.Logger.Trace("更新实时播报数据 this.AnnouncerLog = ", this.AnnouncerLog)
task.New(nil, task.CallableWrapper(func(o *basic.Object) interface{} {
err := model.InsertAnnouncerLog(&data)
if err != nil {
logger.Logger.Error("UpdateAnnouncerLog InsertAnnouncerLog err :", err)
}
return nil
}), task.CompleteNotifyWrapper(func(i interface{}, t task.Task) {
}), "save_announcer").Start()
}
func (this *AwardLogManager) Init() {
for _, v := range PlatformMgrSingleton.platforms {
if v != nil {
// 获取道具获得总数
res, err := model.FetchAwardLog(v.IdStr)
if err != nil {
logger.Logger.Errorf("fetch award log error: %v", err)
} else {
PlatformMgrSingleton.GetConfig(v.IdStr).AwardItem = res
}
// 获取实时播报数据
arr, err := model.FetchAnnouncerLog(v.IdStr)
if err != nil {
logger.Logger.Errorf("fetch announcer log error: %v", err)
} else {
for _, v := range arr {
if this.AnnouncerLog[v.Platform] == nil {
this.AnnouncerLog[v.Platform] = make(map[int32][]model.AnnouncerLog)
}
if this.AnnouncerLog[v.Platform][v.TypeId] == nil {
this.AnnouncerLog[v.Platform][v.TypeId] = make([]model.AnnouncerLog, 0)
}
this.AnnouncerLog[v.Platform][v.TypeId] = append(this.AnnouncerLog[v.Platform][v.TypeId], v)
}
}
}
}
}
func (this *AwardLogManager) Update() {
}
func (this *AwardLogManager) Shutdown() {
this.Save()
}
func (this *AwardLogManager) OnHourTimer() {
task.New(nil, task.CallableWrapper(func(o *basic.Object) interface{} {
this.Save()
return nil
}), task.CompleteNotifyWrapper(func(i interface{}, t task.Task) {
}), "save_awarditem").Start()
}
func (this *AwardLogManager) Save() {
for _, v := range PlatformMgrSingleton.platforms {
if v != nil {
model.UpsertAwardLog(v.IdStr, &PlatformMgrSingleton.GetConfig(v.IdStr).AwardItem)
}
}
}
func init() {
module.RegisteModule(AwardLogMgr, time.Hour, 0)
ClockMgrSington.RegisteSinker(AwardLogMgr)
}