242 lines
6.6 KiB
Go
242 lines
6.6 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) 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)
|
|
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) updateActiveReward(moneyActivation *PlayerMoney) {
|
|
// for _, active := range this.TaskActive {
|
|
// if proto.RewardStatus(active.Status) == proto.RewardStatusUnable {
|
|
// var activeTable = this.manager.tables.Active.Get(int(active.Id))
|
|
// if activeTable == nil {
|
|
// this.WipeModel(active)
|
|
// delete(this.TaskActive, active.Id)
|
|
// } else {
|
|
// if moneyActivation.Value >= uint32(activeTable.ActiveValue) {
|
|
// active.Status = uint32(proto.RewardStatusCanGet)
|
|
// this.SaveModel(active)
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
//}
|
|
//
|
|
//func (this *Player) resetActiveReward() {
|
|
// var activeTables = this.manager.tables.Active.List()
|
|
//
|
|
// this.TaskActive = map[uint32]*PlayerTaskActive{}
|
|
// for _, activeTable := range activeTables {
|
|
// var active = &PlayerTaskActive{Id: uint32(activeTable.Id), Status: uint32(proto.RewardStatusUnable)}
|
|
// this.TaskActive[active.Id] = active
|
|
// }
|
|
//}
|
|
//
|
|
//func (this *Player) ActivationReset() bool {
|
|
// var money = this.GetMoney(proto.MoneyTypeActivation)
|
|
// if money.Value == 0 {
|
|
// return false
|
|
// }
|
|
//
|
|
// money.Value = 0
|
|
// this.SaveModel(money)
|
|
//
|
|
// _ = this.Send(proto.ModIdRole, proto.MsgIdRoleMoneyChange, &proto.MsgRoleMoneyListAck{List: []*proto.RoleMoney{money.BuildMsgMoney()}})
|
|
//
|
|
// this.resetActiveReward()
|
|
//
|
|
// _ = this.Send(proto.ModIdTask, proto.MsgIdActiveInfo, this.BuildMsgActiveInfoAck())
|
|
//
|
|
// 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}
|
|
}
|