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 { common.BaseClockSinker AwardMap map[string]map[int32]map[int32]int64 //key1:plt key2:1话费 2实物 key3 itemId value:数量 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 { ret := make(map[int32]int64) if this.AwardMap[plt] != nil && this.AwardMap[plt][typeId] != nil { ret = this.AwardMap[plt][typeId] } 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) { if srvdata.GameItemMgr.Get(plt, itemId).Type == common.ItemTypeObjective || srvdata.GameItemMgr.Get(plt, itemId).Type == common.ItemTypeChange { typeId := int32(1) if srvdata.GameItemMgr.Get(plt, itemId).Type == common.ItemTypeObjective { typeId = 2 } if this.AwardMap[plt] == nil { this.AwardMap[plt] = make(map[int32]map[int32]int64) } if this.AwardMap[plt][typeId] == nil { this.AwardMap[plt][typeId] = make(map[int32]int64) } this.AwardMap[plt][typeId][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() { this.AwardMap = make(map[string]map[int32]map[int32]int64) for _, v := range PlatformMgrSingleton.platforms { if v != nil && v.IdStr != common.Platform_Sys { // 获取道具获得总数 res, err := model.FetchAwardLog(v.IdStr) if err != nil { logger.Logger.Errorf("fetch award log error: %v", err) } else { this.AwardMap[v.IdStr] = res.AwardMap } // 获取实时播报数据 arr, err := model.FetchAnnouncerLog(v.IdStr) if err != nil { logger.Logger.Errorf("fetch announcer log error: %v", err) } else { for _, data := range arr { if this.AnnouncerLog[data.Platform] == nil { this.AnnouncerLog[data.Platform] = make(map[int32][]model.AnnouncerLog) } if this.AnnouncerLog[data.Platform][data.TypeId] == nil { this.AnnouncerLog[data.Platform][data.TypeId] = make([]model.AnnouncerLog, 0) } this.AnnouncerLog[data.Platform][data.TypeId] = append(this.AnnouncerLog[data.Platform][data.TypeId], data) } } } } } func (this *AwardLogManager) Update() { } func (this *AwardLogManager) Shutdown() { this.Save() module.UnregisteModule(this) } 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 plt, v := range this.AwardMap { if v != nil { data := &model.AwardLog{ Ts: time.Now().Unix(), AwardMap: v, } model.UpsertAwardLog(plt, data) } } } func init() { module.RegisteModule(AwardLogMgr, time.Hour, 0) common.ClockMgrSingleton.RegisterSinker(AwardLogMgr) }