ecs/servers/game/logic/player_money.go

235 lines
6.7 KiB
Go
Raw Normal View History

2025-06-04 18:17:39 +08:00
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) {
switch moneyType {
case pb.MoneyType_Exp:
//this.addExp(value, logType)
case pb.MoneyType_Power:
this.addPower(value, logType)
default:
this.addMoney(moneyType, value, logType)
}
}
func (this *Player) CheckMoney(moneyType pb.MoneyType, value uint32) (bool, proto.TipError) {
2025-06-04 18:17:39 +08:00
if this.checkMoney(moneyType, value) {
return true, nil
2025-06-04 18:17:39 +08:00
}
switch moneyType {
case pb.MoneyType_Exp:
return false, proto.TipExpNotEnough
2025-06-04 18:17:39 +08:00
case pb.MoneyType_Power:
return false, proto.TipPowerNotEnough
2025-06-04 18:17:39 +08:00
default:
return false, proto.TipMoneyNotEnough
2025-06-04 18:17:39 +08:00
}
}
func (this *Player) ReduceMoney(moneyType pb.MoneyType, value uint32, logType LogType) {
switch moneyType {
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(uint16(pb.ModId_ModuleRole), uint16(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(uint16(pb.ModId_ModuleRole), uint16(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 money = this.GetMoney(proto.MoneyTypeExp)
// money.Value += value
//
// var curLevel = this.Role.Level
// //for {
// // var roleLevelTable = this.manager.tables.RoleLevelExtend.Get(int(this.Role.Career), int(this.Role.Level))
// // if roleLevelTable == nil {
// // return
// // }
// //
// // if money.Value < uint32(roleLevelTable.RequireExp) {
// // break
// // }
// //
// // curLevel += 1
// // money.Value -= uint32(roleLevelTable.RequireExp)
// //}
//
// this.SaveModel(money)
//
// _ = this.Send(proto.ModIdRole, proto.MsgIdRoleMoneyChange, &proto.MsgRoleMoneyListAck{List: []*proto.RoleMoney{money.BuildMsgMoney()}})
//
// if curLevel != this.Role.Level {
// var preLevel = this.Role.Level
// this.ChangeProperty(util.NewPair(proto.RolePropertyTypeLevel, uint64(curLevel)))
// if logType != LogTypeNone {
// this.manager.eventManager.PlayerLevelUpgrade(this, logType, preLevel, curLevel)
// }
// }
//
// 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(uint16(pb.ModId_ModuleRole), uint16(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(uint16(pb.ModId_ModuleRole), uint16(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}
}