535 lines
15 KiB
Go
535 lines
15 KiB
Go
package equipment
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/event"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"math"
|
|
"math/rand"
|
|
"time"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
)
|
|
|
|
const moduleName = "装备"
|
|
|
|
/*
|
|
模块名:装备
|
|
描述:用户装备管理以及装备升级强化相关
|
|
开发:李伟
|
|
*/
|
|
func NewModule() core.IModule {
|
|
m := new(Equipment)
|
|
return m
|
|
}
|
|
|
|
type Equipment struct {
|
|
modules.ModuleBase
|
|
service core.IService
|
|
chat comm.IChat
|
|
api *apiComp
|
|
configure *configureComp
|
|
modelEquipment *modelEquipmentComp
|
|
}
|
|
|
|
//模块名
|
|
func (this *Equipment) GetType() core.M_Modules {
|
|
return comm.ModuleEquipment
|
|
}
|
|
|
|
//模块初始化接口 注册用户创建角色事件
|
|
func (this *Equipment) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
|
|
err = this.ModuleBase.Init(service, module, options)
|
|
this.service = service
|
|
return
|
|
}
|
|
|
|
//模块启动接口
|
|
//模块启动
|
|
func (this *Equipment) Start() (err error) {
|
|
err = this.ModuleBase.Start()
|
|
var module core.IModule
|
|
if module, err = this.service.GetModule(comm.ModuleChat); err != nil {
|
|
return
|
|
}
|
|
this.chat = module.(comm.IChat)
|
|
event.RegisterGO(comm.EventUserOffline, this.EventUserOffline)
|
|
return
|
|
}
|
|
|
|
//装备组件
|
|
func (this *Equipment) OnInstallComp() {
|
|
this.ModuleBase.OnInstallComp()
|
|
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
|
|
this.modelEquipment = this.RegisterComp(new(modelEquipmentComp)).(*modelEquipmentComp)
|
|
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
|
|
}
|
|
|
|
//Event------------------------------------------------------------------------------------------------------------
|
|
func (this *Equipment) EventUserOffline(uid, sessionid string) {
|
|
this.modelEquipment.BatchDelLists(uid)
|
|
}
|
|
|
|
//IEquipment-------------------------------------------------------------------------------------------------------------------------------
|
|
//查询武器信息
|
|
func (this *Equipment) QueryEquipment(uid string, id string) (equipment *pb.DB_Equipment, errdata *pb.ErrorData) {
|
|
var err error
|
|
if uid == "" || id == "" {
|
|
this.Errorf("请求参数错误 uid:%s Id:%s", uid, id)
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
}
|
|
return
|
|
}
|
|
if equipment, err = this.modelEquipment.QueryUserEquipmentsById(uid, id); err != nil {
|
|
if err == redis.Nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_EquipmentOnFoundEquipment,
|
|
Title: pb.ErrorCode_EquipmentOnFoundEquipment.ToString(),
|
|
Message: fmt.Sprintf("未找到装备 uid:%s id:%s", uid, id),
|
|
}
|
|
} else {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_SystemError,
|
|
Title: pb.ErrorCode_SystemError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//查询服务资源数量
|
|
func (this *Equipment) QueryEquipments(uid string) (equipment []*pb.DB_Equipment, errdata *pb.ErrorData) {
|
|
var err error
|
|
if uid == "" {
|
|
this.Errorf("请求参数错误 uid:%s ", uid)
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
}
|
|
return
|
|
}
|
|
if equipment, err = this.modelEquipment.QueryUserEquipments(uid); err != nil {
|
|
if err != redis.Nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_SystemError,
|
|
Title: pb.ErrorCode_SystemError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//查询卡片数量
|
|
func (this *Equipment) QueryEquipmentAmount(uid string, equipmentId string) (amount uint32) {
|
|
amount = this.modelEquipment.QueryEquipmentAmount(uid, equipmentId)
|
|
return
|
|
}
|
|
|
|
//添加武器
|
|
func (this *Equipment) AddNewEquipments(session comm.IUserSession, cIds map[string]uint32, bPush bool) (change []*pb.DB_Equipment, errdata *pb.ErrorData) {
|
|
var (
|
|
err error
|
|
)
|
|
if change, err = this.modelEquipment.AddEquipments(session, cIds); err != nil {
|
|
this.Errorf("err%v", err)
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_SystemError,
|
|
Title: pb.ErrorCode_SystemError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
if len(change) > 0 && bPush {
|
|
this.equipmentsChangePush(session, change)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *Equipment) AddAllEquipments(session comm.IUserSession) (errdata *pb.ErrorData) {
|
|
var (
|
|
configure *cfg.GameEquip
|
|
cIds map[string]uint32
|
|
err error
|
|
)
|
|
if configure, err = this.configure.GetEquipmentConfigure(); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
cIds = make(map[string]uint32)
|
|
for _, v := range configure.GetDataList() {
|
|
cIds[v.Id] = 1
|
|
}
|
|
_, errdata = this.AddNewEquipments(session, cIds, true)
|
|
return
|
|
}
|
|
|
|
//删除武器
|
|
func (this *Equipment) DelEquipments(session comm.IUserSession, equipIds []string, bPush bool) (errdata *pb.ErrorData) {
|
|
var (
|
|
err error
|
|
change []*pb.DB_Equipment
|
|
)
|
|
if change, err = this.modelEquipment.DelEquipments(session.GetUserId(), equipIds); err != nil {
|
|
this.Errorf("err%v", err)
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_SystemError,
|
|
Title: pb.ErrorCode_SystemError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
if len(change) > 0 && bPush {
|
|
this.equipmentsChangePush(session, change)
|
|
}
|
|
return
|
|
}
|
|
|
|
//创建新的装备
|
|
func (this *Equipment) NewEquipment(uid, cid string) (errdata *pb.ErrorData, equip *pb.DB_Equipment) {
|
|
var (
|
|
conf *cfg.GameEquipData
|
|
err error
|
|
)
|
|
if conf, err = this.configure.GetEquipmentConfigureById(cid); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
if equip, err = this.modelEquipment.newEquipment(uid, conf, nil, false); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
//创建新的装备
|
|
func (this *Equipment) AddEquipment(session comm.IUserSession, equip *pb.DB_Equipment) (errdata *pb.ErrorData) {
|
|
var (
|
|
err error
|
|
configure *cfg.GameEquipData
|
|
)
|
|
if err = this.modelEquipment.addEquipment(equip); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
if configure, err = this.configure.GetEquipmentConfigureById(equip.CId); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
tasks := make([]*pb.BuriedParam, 0)
|
|
//随机任务
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype50, 1, configure.InitLv))
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype97, 1, configure.Suittype, configure.InitLv))
|
|
unm := int32(1)
|
|
if equip.AdverbEntry != nil {
|
|
unm += int32(len(equip.AdverbEntry))
|
|
}
|
|
if equip.Adverbskill != nil {
|
|
unm += int32(len(equip.Adverbskill))
|
|
}
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype99, 1, unm, configure.InitLv))
|
|
if configure.Pos == 7 {
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype103, 1, configure.InitLv))
|
|
} else if configure.Pos == 6 {
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype101, 1, configure.InitLv))
|
|
}
|
|
go this.ModuleBuried.TriggerBuried(session.GetUserId(), tasks...)
|
|
this.equipmentsChangePush(session, []*pb.DB_Equipment{equip})
|
|
return
|
|
}
|
|
|
|
///出售装备
|
|
func (this *Equipment) SellEquipments(session comm.IUserSession, equs []string) (errdata *pb.ErrorData, atno []*pb.UserAtno) {
|
|
var (
|
|
err error
|
|
equipments []*pb.DB_Equipment
|
|
confs []*cfg.GameEquipData
|
|
sale [][]*cfg.Gameatn
|
|
)
|
|
if equipments, err = this.modelEquipment.QueryUserEquipmentsByIds(session.GetUserId(), equs); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
}
|
|
return
|
|
}
|
|
confs = make([]*cfg.GameEquipData, len(equipments))
|
|
sale = make([][]*cfg.Gameatn, len(equipments))
|
|
for i, v := range equipments {
|
|
if v.HeroId != "" || v.Islock {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_EquipmentNoCanSell,
|
|
Title: pb.ErrorCode_EquipmentNoCanSell.ToString(),
|
|
Message: fmt.Sprintf("装备锁定或者穿戴状态下 不可售卖!"),
|
|
}
|
|
this.Errorf("NoCanSell %v", v)
|
|
return
|
|
}
|
|
if confs[i], err = this.configure.GetEquipmentConfigureById(v.CId); err != nil {
|
|
this.Errorln(err)
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
if confs[i].Sale == nil || len(confs[i].Sale) == 0 {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_EquipmentNoCanSell,
|
|
Title: pb.ErrorCode_EquipmentNoCanSell.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
sale[i] = make([]*cfg.Gameatn, len(confs[i].Sale))
|
|
for n, s := range confs[i].Sale {
|
|
_s := &cfg.Gameatn{
|
|
A: s.A,
|
|
T: s.T,
|
|
N: s.N + int32(math.Floor(float64(s.N*(v.Lv-1))*float64(confs[i].Salecoef))),
|
|
}
|
|
sale[i][n] = _s
|
|
}
|
|
}
|
|
|
|
sales := make([]*cfg.Gameatn, 0)
|
|
for _, v := range sale {
|
|
sales = append(sales, v...)
|
|
}
|
|
if errdata, atno = this.DispenseAtno(session, sales, true); errdata != nil {
|
|
return
|
|
}
|
|
if errdata = this.DelEquipments(session, equs, true); errdata != nil {
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
//回收装备接口
|
|
func (this *Equipment) RecycleEquipments(session comm.IUserSession, equs []string, discount int32) (errdata *pb.ErrorData, atno []*pb.UserAtno) {
|
|
var (
|
|
err error
|
|
equipments []*pb.DB_Equipment
|
|
sellconf *cfg.GameSellCoefficientData
|
|
confs []*cfg.GameEquipData
|
|
sale [][]*cfg.Gameatn
|
|
)
|
|
if equipments, err = this.modelEquipment.QueryUserEquipmentsByIds(session.GetUserId(), equs); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
}
|
|
return
|
|
}
|
|
confs = make([]*cfg.GameEquipData, len(equipments))
|
|
sale = make([][]*cfg.Gameatn, len(equipments))
|
|
for i, v := range equipments {
|
|
if v.HeroId != "" || v.Islock {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
}
|
|
this.Errorf("NoCanSell %v", v)
|
|
return
|
|
}
|
|
if confs[i], err = this.configure.GetEquipmentConfigureById(v.CId); err != nil {
|
|
this.Errorln(err)
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
}
|
|
return
|
|
}
|
|
if confs[i].SmithySale == nil || len(confs[i].SmithySale) == 0 {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_EquipmentNoCanSell,
|
|
Title: pb.ErrorCode_EquipmentNoCanSell.ToString(),
|
|
}
|
|
return
|
|
}
|
|
if sellconf, err = this.configure.getSellcoefficient(int32(len(v.AdverbEntry) + 1)); err != nil {
|
|
this.Errorln(err)
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
sale[i] = make([]*cfg.Gameatn, len(confs[i].Sale))
|
|
for n, s := range confs[i].SmithySale {
|
|
_s := &cfg.Gameatn{
|
|
A: s.A,
|
|
T: s.T,
|
|
N: int32(math.Floor(float64(s.N) * (float64(discount+1000) / float64(1000)) * (float64(sellconf.Coefficient) / float64(1000)))),
|
|
}
|
|
sale[i][n] = _s
|
|
}
|
|
}
|
|
|
|
sales := make([]*cfg.Gameatn, 0)
|
|
for _, v := range sale {
|
|
sales = append(sales, v...)
|
|
}
|
|
if errdata, atno = this.DispenseAtno(session, sales, true); errdata != nil {
|
|
return
|
|
}
|
|
if errdata = this.DelEquipments(session, equs, true); errdata != nil {
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
//获得可操作用户装备列表
|
|
func (this *Equipment) GetActionableEquipments(uid string) (errdata *pb.ErrorData, eruips []*pb.DB_Equipment) {
|
|
var (
|
|
err error
|
|
equipments []*pb.DB_Equipment
|
|
)
|
|
eruips = make([]*pb.DB_Equipment, 0)
|
|
if equipments, err = this.modelEquipment.QueryUserEquipments(uid); err != nil {
|
|
return
|
|
}
|
|
for _, v := range equipments {
|
|
if v.HeroId == "" && !v.Islock {
|
|
eruips = append(eruips, v)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//获取可用套装 (铁匠铺使用)
|
|
func (this *Equipment) GetActionableSuit(uid string) (errdata *pb.ErrorData, Suit []int32) {
|
|
var (
|
|
err error
|
|
equipments []*pb.DB_Equipment
|
|
suit map[int32]struct{}
|
|
)
|
|
suit = make(map[int32]struct{})
|
|
if equipments, err = this.modelEquipment.QueryUserEquipments(uid); err != nil {
|
|
return
|
|
}
|
|
for _, v := range equipments {
|
|
if v.HeroId == "" && !v.Islock {
|
|
if conf, err := this.configure.GetEquipmentConfigureById(v.CId); err == nil {
|
|
suit[conf.Suittype] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
for i, _ := range suit {
|
|
Suit = append(Suit, i)
|
|
}
|
|
return
|
|
}
|
|
|
|
// 随机获得一件N级的装备装备
|
|
/*
|
|
suiteId: 套装id
|
|
pos: 位置(-1 表示随机位置 大于0 表示获得指定位置 )
|
|
lv: 装备等级
|
|
dyweight: 动态权重
|
|
isepic:是否史诗 与 动态权重互斥
|
|
*/
|
|
func (this *Equipment) GetForgeEquip(session comm.IUserSession, suiteId int32, pos int32, lv int32, dyweight []int32, isepic bool) (eruip *pb.DB_Equipment, errdata *pb.ErrorData) {
|
|
var (
|
|
configures []*cfg.GameEquipData
|
|
lvs []*cfg.GameEquipData
|
|
err error
|
|
)
|
|
if configures, err = this.configure.GetSuitEquipmentConfigure(suiteId); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_EquipmentSuiteNotFound,
|
|
Title: pb.ErrorCode_EquipmentSuiteNotFound.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
if pos != -1 {
|
|
for _, v := range configures {
|
|
if v.Pos == pos && v.InitLv == lv {
|
|
if eruip, err = this.modelEquipment.newEquipment(session.GetUserId(), v, dyweight, isepic); err == nil {
|
|
errdata = this.AddEquipment(session, eruip)
|
|
// this.equipmentsChangePush(session, []*pb.DB_Equipment{eruip})
|
|
return
|
|
}
|
|
this.Errorf("err%v", err)
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_SystemError,
|
|
Title: pb.ErrorCode_SystemError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
}
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
Message: fmt.Sprintf("no fond pos:%d intlv:%d", pos, lv),
|
|
}
|
|
return
|
|
} else {
|
|
lvs = make([]*cfg.GameEquipData, 0)
|
|
for _, v := range configures {
|
|
if v.InitLv == lv {
|
|
lvs = append(lvs, v)
|
|
}
|
|
}
|
|
if len(lvs) == 0 {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
}
|
|
return
|
|
}
|
|
r := rand.New(rand.NewSource(time.Now().Unix()))
|
|
index := r.Perm(len(lvs))[0]
|
|
if eruip, err = this.modelEquipment.newEquipment(session.GetUserId(), lvs[index], dyweight, isepic); err == nil {
|
|
errdata = this.AddEquipment(session, eruip)
|
|
// this.equipmentsChangePush(session, []*pb.DB_Equipment{eruip})
|
|
return
|
|
}
|
|
this.Errorf("err%v", err)
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_SystemError,
|
|
Title: pb.ErrorCode_SystemError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
}
|
|
|
|
//Evens--------------------------------------------------------------------------------------------------------------------------------
|
|
//推送道具变化消息
|
|
func (this *Equipment) equipmentsChangePush(session comm.IUserSession, items []*pb.DB_Equipment) (err error) {
|
|
session.SendMsg(string(this.GetType()), "change", &pb.EquipmentChangePush{Equipments: items})
|
|
return
|
|
}
|