package logic import ( "ecs/proto/pb" "github.com/oylshe1314/framework/util" ) type PlayerCounter struct { Type uint32 `bson:"type" key:"1"` Key uint64 `bson:"key" key:"2"` Time int64 `bson:"time"` Daily uint32 `bson:"daily"` Total uint32 `bson:"total"` } func (this *PlayerCounter) BuildMsgCounter() *pb.Counter { return &pb.Counter{Type: pb.CounterType(this.Type), Key: this.Key, Time: this.Time, Daily: this.Daily, Total: this.Total} } func (this *Player) getCounter(tipe pb.CounterType, key uint64) *PlayerCounter { var counters = this.Counter[uint32(tipe)] if counters == nil { counters = map[uint64]*PlayerCounter{} this.Counter[uint32(tipe)] = counters } var counter = counters[key] if counter == nil { counter = &PlayerCounter{Type: uint32(tipe), Key: key} counters[key] = counter } return counter } func (this *Player) AddCounter(tipe pb.CounterType, key uint64, value uint32) { var counter = this.getCounter(tipe, key) counter.Time = util.Unix() counter.Daily += value counter.Total += value this.SaveModel(counter) _ = this.Send(pb.ModId_ModuleCounter, pb.MsgId_ModCounterChange, &pb.CounterListAck{ CounterList: []*pb.Counter{counter.BuildMsgCounter()}, }) } func (this *Player) SetCounter(tipe pb.CounterType, key uint64, value uint32, onlyDaily bool) { var counter = this.getCounter(tipe, key) counter.Time = util.Unix() counter.Daily = value if !onlyDaily { counter.Total = value } this.SaveModel(counter) _ = this.Send(pb.ModId_ModuleCounter, pb.MsgId_ModCounterChange, &pb.CounterListAck{ CounterList: []*pb.Counter{counter.BuildMsgCounter()}, }) } func (this *Player) ResetCounterDaily(tipe pb.CounterType, key uint64) bool { var counters = this.Counter[uint32(tipe)] if counters == nil { return false } var counter = counters[key] if counter == nil || counter.Daily == 0 { return false } counter.Time = util.Unix() counter.Daily = 0 this.SaveModel(counter) _ = this.Send(pb.ModId_ModuleCounter, pb.MsgId_ModCounterChange, &pb.CounterListAck{ CounterList: []*pb.Counter{counter.BuildMsgCounter()}, }) return true } func (this *Player) GetCounter(tipe pb.CounterType, key uint64) (int64, uint32, uint32) { var counter = this.getCounter(tipe, key) if counter == nil { return 0, 0, 0 } else { return counter.Time, counter.Daily, counter.Total } } func (this *Player) GetCounterTime(tipe pb.CounterType, key uint64) (time int64) { time, _, _ = this.GetCounter(tipe, key) return } func (this *Player) GetCounterDaily(tipe pb.CounterType, key uint64) (daily uint32) { _, daily, _ = this.GetCounter(tipe, key) return } func (this *Player) GetCounterTotal(tipe pb.CounterType, key uint64) (total uint32) { _, _, total = this.GetCounter(tipe, key) return } func (this *Player) BuildMsgCounterListAck() *pb.CounterListAck { var counterList []*pb.Counter for _, counters := range this.Counter { for _, counter := range counters { counterList = append(counterList, counter.BuildMsgCounter()) } } return &pb.CounterListAck{CounterList: counterList} }