package logic import ( "ecs/proto/pb" "fmt" "github.com/oylshe1314/framework/util" ) type PlayerCounter struct { Key string `bson:"key" key:"1"` Type uint32 `bson:"type"` Id uint64 `bson:"Id"` 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), Id: this.Id, Time: this.Time, Daily: this.Daily, Total: this.Total} } func (this *Player) CounterKey(counterType pb.CounterType, key uint64) string { return fmt.Sprint(counterType, ":", key) } func (this *Player) getCounter(counterType pb.CounterType, id uint64) *PlayerCounter { var counter = this.Counter[this.CounterKey(counterType, id)] if counter == nil { counter = &PlayerCounter{Key: this.CounterKey(counterType, id), Type: uint32(counterType), Id: id} this.Counter[counter.Key] = counter } return counter } func (this *Player) AddCounter(counterType pb.CounterType, key uint64, value uint32) { var counter = this.getCounter(counterType, key) counter.Time = util.NowUnix() 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(counterType pb.CounterType, id uint64, value uint32, onlyDaily bool) { var counter = this.getCounter(counterType, id) counter.Time = util.NowUnix() 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(counterType pb.CounterType, id uint64) bool { var counter = this.Counter[this.CounterKey(counterType, id)] if counter == nil || counter.Daily == 0 { return false } counter.Time = util.NowUnix() 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(counterType pb.CounterType, id uint64) (int64, uint32, uint32) { var counter = this.getCounter(counterType, id) if counter == nil { return 0, 0, 0 } else { return counter.Time, counter.Daily, counter.Total } } func (this *Player) GetCounterTime(counterType pb.CounterType, id uint64) (time int64) { time, _, _ = this.GetCounter(counterType, id) return } func (this *Player) GetCounterDaily(counterType pb.CounterType, id uint64) (daily uint32) { _, daily, _ = this.GetCounter(counterType, id) return } func (this *Player) GetCounterTotal(counterType pb.CounterType, id uint64) (total uint32) { _, _, total = this.GetCounter(counterType, id) return } func (this *Player) BuildMsgCounterListAck() *pb.CounterListAck { var counterList []*pb.Counter for _, counter := range this.Counter { counterList = append(counterList, counter.BuildMsgCounter()) } return &pb.CounterListAck{CounterList: counterList} }