235 lines
6.3 KiB
Go
235 lines
6.3 KiB
Go
package logic
|
|
|
|
import (
|
|
"ecs/proto"
|
|
"ecs/proto/pb"
|
|
"github.com/oylshe1314/framework/util"
|
|
)
|
|
|
|
type PlayerMoney struct {
|
|
Type uint32 `bson:"type" key:"1"`
|
|
Value uint32 `bson:"value"`
|
|
}
|
|
|
|
func (this *PlayerMoney) BuildMsgMoney() *pb.Money {
|
|
return &pb.Money{Type: pb.MoneyType(this.Type), Value: this.Value}
|
|
}
|
|
|
|
func (this *Player) GetMoney(moneyType pb.MoneyType) *PlayerMoney {
|
|
var money = this.Money[uint32(moneyType)]
|
|
if money == nil {
|
|
money = &PlayerMoney{Type: uint32(moneyType)}
|
|
this.Money[money.Type] = money
|
|
}
|
|
return money
|
|
}
|
|
|
|
func (this *Player) GetMoneyValue(moneyType pb.MoneyType) uint32 {
|
|
var money = this.Money[uint32(moneyType)]
|
|
if money == nil {
|
|
return 0
|
|
}
|
|
return money.Value
|
|
}
|
|
|
|
func (this *Player) AddMoney(moneyType pb.MoneyType, value uint32, logType LogType) bool {
|
|
switch moneyType {
|
|
case pb.MoneyType_Exp:
|
|
this.addExp(value, logType)
|
|
case pb.MoneyType_Power:
|
|
this.addPower(value, logType)
|
|
case pb.MoneyType_DailyActive:
|
|
this.addActiveValue(moneyType, value, logType)
|
|
default:
|
|
this.addMoney(moneyType, value, logType)
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (this *Player) CheckMoney(moneyType pb.MoneyType, value uint32) (bool, proto.TipError) {
|
|
if this.checkMoney(moneyType, value) {
|
|
return true, nil
|
|
}
|
|
switch moneyType {
|
|
case pb.MoneyType_Exp:
|
|
return false, proto.TipExpNotEnough
|
|
case pb.MoneyType_Power:
|
|
return false, proto.TipPowerNotEnough
|
|
default:
|
|
return false, proto.TipMoneyNotEnough
|
|
}
|
|
}
|
|
|
|
func (this *Player) ReduceMoney(moneyType pb.MoneyType, value uint32, logType LogType) {
|
|
switch moneyType {
|
|
case pb.MoneyType_Exp:
|
|
return
|
|
case pb.MoneyType_Power:
|
|
this.reducePower(value, logType)
|
|
default:
|
|
this.reduceMoney(moneyType, value, logType)
|
|
}
|
|
}
|
|
|
|
func (this *Player) addMoney(moneyType pb.MoneyType, value uint32, logType LogType) {
|
|
if value == 0 {
|
|
return
|
|
}
|
|
|
|
var money = this.GetMoney(moneyType)
|
|
money.Value += value
|
|
this.SaveModel(money)
|
|
|
|
_ = this.Send(pb.ModId_ModuleRole, pb.MsgId_ModRoleMoneyChange, &pb.MoneyListAck{MoneyList: []*pb.Money{money.BuildMsgMoney()}})
|
|
|
|
//if logType != LogTypeNone {
|
|
// this.manager.eventManager.PlayerMoneyObtain(this, logType, uint32(moneyType), value, money.Value)
|
|
//}
|
|
}
|
|
|
|
func (this *Player) checkMoney(moneyType pb.MoneyType, value uint32) bool {
|
|
if value == 0 {
|
|
return true
|
|
}
|
|
return this.GetMoney(moneyType).Value >= value
|
|
}
|
|
|
|
func (this *Player) reduceMoney(moneyType pb.MoneyType, value uint32, logType LogType) {
|
|
if value == 0 {
|
|
return
|
|
}
|
|
|
|
var money = this.GetMoney(moneyType)
|
|
if value > money.Value {
|
|
this.manager.logger.Error("Reduce money out of range, did not check before reducing?")
|
|
value = money.Value
|
|
}
|
|
|
|
money.Value -= value
|
|
this.SaveModel(money)
|
|
|
|
_ = this.Send(pb.ModId_ModuleRole, pb.MsgId_ModRoleMoneyChange, &pb.MoneyListAck{MoneyList: []*pb.Money{money.BuildMsgMoney()}})
|
|
|
|
//if logType != LogTypeNone {
|
|
// this.manager.eventManager.PlayerMoneyConsume(this, logType, uint32(moneyType), value, money.Value)
|
|
//}
|
|
}
|
|
|
|
func (this *Player) addExp(value uint32, logType LogType) {
|
|
if value == 0 {
|
|
return
|
|
}
|
|
|
|
var roleHero = this.RoleHero()
|
|
if roleHero == nil {
|
|
return
|
|
}
|
|
|
|
var curExp = roleHero.Exp + uint64(value)
|
|
var curLevel = roleHero.Level
|
|
for {
|
|
var heroLevelTable = this.manager.tables.HeroLevel.Find3(int(roleHero.Id), int(curLevel))
|
|
if heroLevelTable == nil {
|
|
return
|
|
}
|
|
|
|
if curExp < uint64(heroLevelTable.NeedExp) {
|
|
break
|
|
}
|
|
|
|
curLevel += 1
|
|
curExp -= uint64(heroLevelTable.NeedExp)
|
|
}
|
|
|
|
roleHero.Exp = curExp
|
|
roleHero.Level = curLevel
|
|
this.SaveModel(roleHero)
|
|
|
|
this.RoleExp = roleHero.Exp
|
|
this.RoleLevel = roleHero.Level
|
|
this.SaveField("role_exp", this.RoleExp)
|
|
this.SaveField("role_level", this.RoleLevel)
|
|
|
|
_ = this.Send(pb.ModId_ModuleHero, pb.MsgId_ModHeroChange, &pb.HeroChangeListAck{
|
|
ChangeList: []*pb.HeroChange{{ChangeType: pb.ChangeType_Changed, Hero: roleHero.BuildMsgHero()}},
|
|
})
|
|
|
|
//if logType != LogTypeNone {
|
|
// this.manager.eventManager.PlayerMoneyObtain(this, logType, uint32(proto.MoneyTypeExp), value, money.Value)
|
|
//}
|
|
}
|
|
|
|
func (this *Player) addPower(value uint32, logType LogType) {
|
|
var money = this.GetMoney(pb.MoneyType_Power)
|
|
money.Value += value
|
|
this.SaveModel(money)
|
|
|
|
_ = this.Send(pb.ModId_ModuleRole, pb.MsgId_ModRoleMoneyChange, &pb.MoneyListAck{MoneyList: []*pb.Money{money.BuildMsgMoney()}})
|
|
|
|
if money.Value >= uint32(RolePowerMax) && this.PowerNextTime > 0 {
|
|
this.ChangeProperty(util.NewPair(pb.RolePropertyType_PowerNextTime, int64(0)))
|
|
}
|
|
|
|
//if logType != LogTypeNone {
|
|
// this.manager.eventManager.PlayerMoneyObtain(this, logType, uint32(proto.MoneyTypeExp), value, money.Value)
|
|
//}
|
|
}
|
|
|
|
func (this *Player) reducePower(value uint32, logType LogType) {
|
|
var money = this.GetMoney(pb.MoneyType_Power)
|
|
|
|
if value > money.Value {
|
|
this.manager.logger.Error("Reduce money out of range, did not check before reducing?")
|
|
value = money.Value
|
|
}
|
|
|
|
money.Value -= value
|
|
this.SaveModel(money)
|
|
|
|
if money.Value < uint32(RolePowerMax) && this.PowerNextTime == 0 {
|
|
this.ChangeProperty(util.NewPair(pb.RolePropertyType_PowerNextTime, util.Unix()+int64(RolePowerCycle)))
|
|
}
|
|
|
|
_ = this.Send(pb.ModId_ModuleRole, pb.MsgId_ModRoleMoneyChange, &pb.MoneyListAck{MoneyList: []*pb.Money{money.BuildMsgMoney()}})
|
|
|
|
//if logType != LogTypeNone {
|
|
// this.manager.eventManager.PlayerMoneyConsume(this, logType, uint32(proto.MoneyTypePower), value, money.Value)
|
|
//}
|
|
}
|
|
|
|
func (this *Player) addActiveValue(moneyType pb.MoneyType, value uint32, logType LogType) {
|
|
var money = this.GetMoney(pb.MoneyType_Power)
|
|
money.Value += value
|
|
this.SaveModel(money)
|
|
|
|
_ = this.Send(pb.ModId_ModuleRole, pb.MsgId_ModRoleMoneyChange, &pb.MoneyListAck{MoneyList: []*pb.Money{money.BuildMsgMoney()}})
|
|
|
|
this.checkRewardActive(money)
|
|
}
|
|
|
|
func (this *Player) MoneyActiveValueReset(moneyType pb.MoneyType) bool {
|
|
var money = this.GetMoney(moneyType)
|
|
if money.Value == 0 {
|
|
return false
|
|
}
|
|
|
|
money.Value = 0
|
|
this.SaveModel(money)
|
|
|
|
_ = this.Send(pb.ModId_ModuleRole, pb.MsgId_ModRoleMoneyChange, &pb.MoneyListAck{MoneyList: []*pb.Money{money.BuildMsgMoney()}})
|
|
|
|
var rewardActive = this.newRewardActive(money)
|
|
|
|
_ = this.Send(pb.ModId_ModuleReward, pb.MsgId_ModRewardActiveChange, &pb.RewardActiveChangeAck{RewardActive: rewardActive.BuildMsgRewardActive()})
|
|
|
|
return true
|
|
}
|
|
|
|
func (this *Player) BuildMsgMoneyListAck() *pb.MoneyListAck {
|
|
var list []*pb.Money
|
|
for _, money := range this.Money {
|
|
list = append(list, money.BuildMsgMoney())
|
|
}
|
|
return &pb.MoneyListAck{MoneyList: list}
|
|
}
|