package logic import ( "ecs/proto/pb" "github.com/oylshe1314/framework/util" "sort" ) type PlayerRewardActive struct { MoneyType uint32 `bson:"money_type" key:"1"` RewardStatus map[uint32]uint32 `bson:"reward_status"` } func (this *PlayerRewardActive) BuildMsgRewardActive() *pb.RewardActive { var msg = &pb.RewardActive{MoneyType: pb.MoneyType(this.MoneyType)} for id, status := range this.RewardStatus { msg.StatusList = append(msg.StatusList, &pb.RewardStatus{Id: id, Status: pb.AbleStatus(status)}) } sort.Slice(msg.StatusList, func(i, j int) bool { return msg.StatusList[i].Id < msg.StatusList[j].Id }) return msg } func (this *Player) checkRewardActive(money *PlayerMoney) bool { var changed = false var rewardActive = this.RewardActive[money.Type] if rewardActive == nil { rewardActive = this.newRewardActive(money) if rewardActive == nil { return false } this.RewardActive[rewardActive.MoneyType] = rewardActive changed = true } else { var rewardTables = this.manager.tables.RewardActive.Find2(int(money.Type)) if len(rewardTables) == 0 { return false } for _, rewardTable := range rewardTables { if money.Value < uint32(rewardTable.ActiveValue) { break } if status := rewardActive.RewardStatus[uint32(rewardTable.Id)]; pb.AbleStatus(status) == pb.AbleStatus_Unable { rewardActive.RewardStatus[uint32(rewardTable.Id)] = uint32(pb.AbleStatus_Able) changed = true } } } if changed { this.SaveModel(rewardActive) _ = this.Send(pb.ModId_ModuleReward, pb.MsgId_ModRewardActiveChange, &pb.RewardActiveChangeAck{RewardActive: rewardActive.BuildMsgRewardActive()}) } return changed } func (this *Player) newRewardActive(money *PlayerMoney) *PlayerRewardActive { var rewardTables = this.manager.tables.RewardActive.Find2(int(money.Type)) if len(rewardTables) == 0 { return nil } var rewardActive = &PlayerRewardActive{MoneyType: money.Type, RewardStatus: map[uint32]uint32{}} for _, rewardTable := range rewardTables { rewardActive.RewardStatus[uint32(rewardTable.Id)] = util.If(money.Value >= uint32(rewardTable.ActiveValue), uint32(pb.AbleStatus_Able), uint32(pb.AbleStatus_Unable)) } return rewardActive } func (this *Player) BuildMsgRewardActiveListAck() *pb.RewardActiveListAck { var activeList []*pb.RewardActive for _, rewardStatus := range this.RewardActive { activeList = append(activeList, rewardStatus.BuildMsgRewardActive()) } return &pb.RewardActiveListAck{ActiveList: activeList} }