ecs/servers/game/logic/player_counter.go

112 lines
3.1 KiB
Go
Raw Normal View History

2025-06-04 18:17:39 +08:00
package logic
import (
"ecs/proto/pb"
2025-07-16 10:05:22 +08:00
"fmt"
2025-06-04 18:17:39 +08:00
"github.com/oylshe1314/framework/util"
)
type PlayerCounter struct {
2025-07-16 10:05:22 +08:00
Key string `bson:"key" key:"1"`
Type uint32 `bson:"type"`
Id uint64 `bson:"Id"`
2025-06-04 18:17:39 +08:00
Time int64 `bson:"time"`
Daily uint32 `bson:"daily"`
Total uint32 `bson:"total"`
}
func (this *PlayerCounter) BuildMsgCounter() *pb.Counter {
2025-07-16 10:05:22 +08:00
return &pb.Counter{Type: pb.CounterType(this.Type), Id: this.Id, Time: this.Time, Daily: this.Daily, Total: this.Total}
2025-06-04 18:17:39 +08:00
}
2025-07-16 10:05:22 +08:00
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)]
2025-06-04 18:17:39 +08:00
if counter == nil {
2025-07-16 10:05:22 +08:00
counter = &PlayerCounter{Key: this.CounterKey(counterType, id), Type: uint32(counterType), Id: id}
this.Counter[counter.Key] = counter
2025-06-04 18:17:39 +08:00
}
return counter
}
2025-07-16 10:05:22 +08:00
func (this *Player) AddCounter(counterType pb.CounterType, key uint64, value uint32) {
var counter = this.getCounter(counterType, key)
counter.Time = util.NowUnix()
2025-06-04 18:17:39 +08:00
counter.Daily += value
counter.Total += value
this.SaveModel(counter)
2025-06-20 15:34:46 +08:00
_ = this.Send(pb.ModId_ModuleCounter, pb.MsgId_ModCounterChange, &pb.CounterListAck{
CounterList: []*pb.Counter{counter.BuildMsgCounter()},
})
2025-06-04 18:17:39 +08:00
}
2025-07-16 10:05:22 +08:00
func (this *Player) SetCounter(counterType pb.CounterType, id uint64, value uint32, onlyDaily bool) {
var counter = this.getCounter(counterType, id)
2025-06-04 18:17:39 +08:00
2025-07-16 10:05:22 +08:00
counter.Time = util.NowUnix()
2025-06-04 18:17:39 +08:00
counter.Daily = value
if !onlyDaily {
counter.Total = value
}
this.SaveModel(counter)
2025-06-20 15:34:46 +08:00
_ = this.Send(pb.ModId_ModuleCounter, pb.MsgId_ModCounterChange, &pb.CounterListAck{
CounterList: []*pb.Counter{counter.BuildMsgCounter()},
})
2025-06-04 18:17:39 +08:00
}
2025-07-16 10:05:22 +08:00
func (this *Player) ResetCounterDaily(counterType pb.CounterType, id uint64) bool {
var counter = this.Counter[this.CounterKey(counterType, id)]
2025-06-04 18:17:39 +08:00
if counter == nil || counter.Daily == 0 {
return false
}
2025-07-16 10:05:22 +08:00
counter.Time = util.NowUnix()
2025-06-04 18:17:39 +08:00
counter.Daily = 0
this.SaveModel(counter)
2025-06-20 15:34:46 +08:00
_ = this.Send(pb.ModId_ModuleCounter, pb.MsgId_ModCounterChange, &pb.CounterListAck{
CounterList: []*pb.Counter{counter.BuildMsgCounter()},
})
2025-06-04 18:17:39 +08:00
return true
}
2025-07-16 10:05:22 +08:00
func (this *Player) GetCounter(counterType pb.CounterType, id uint64) (int64, uint32, uint32) {
var counter = this.getCounter(counterType, id)
2025-06-04 18:17:39 +08:00
if counter == nil {
return 0, 0, 0
} else {
return counter.Time, counter.Daily, counter.Total
}
}
2025-07-16 10:05:22 +08:00
func (this *Player) GetCounterTime(counterType pb.CounterType, id uint64) (time int64) {
time, _, _ = this.GetCounter(counterType, id)
2025-06-04 18:17:39 +08:00
return
}
2025-07-16 10:05:22 +08:00
func (this *Player) GetCounterDaily(counterType pb.CounterType, id uint64) (daily uint32) {
_, daily, _ = this.GetCounter(counterType, id)
2025-06-04 18:17:39 +08:00
return
}
2025-07-16 10:05:22 +08:00
func (this *Player) GetCounterTotal(counterType pb.CounterType, id uint64) (total uint32) {
_, _, total = this.GetCounter(counterType, id)
2025-06-04 18:17:39 +08:00
return
}
func (this *Player) BuildMsgCounterListAck() *pb.CounterListAck {
var counterList []*pb.Counter
2025-07-16 10:05:22 +08:00
for _, counter := range this.Counter {
counterList = append(counterList, counter.BuildMsgCounter())
2025-06-04 18:17:39 +08:00
}
return &pb.CounterListAck{CounterList: counterList}
}