go_dreamfactory/modules/items/module.go

145 lines
4.7 KiB
Go

package items
import (
"go_dreamfactory/comm"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/lego/core"
)
/*
模块名:Pack
描述:背包系统模块
开发:李伟
*/
func NewModule() core.IModule {
m := new(Items)
return m
}
type Items struct {
modules.ModuleBase
api *apiComp
modelItems *ModelItemsComp
configure *ConfigureComp
}
//模块名称
func (this *Items) GetType() core.M_Modules {
return comm.ModuleItems
}
//模块初始化接口 注册用户创建角色事件
func (this *Items) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
return
}
//装备组件
func (this *Items) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelItems = this.RegisterComp(new(ModelItemsComp)).(*ModelItemsComp)
this.configure = this.RegisterComp(new(ConfigureComp)).(*ConfigureComp)
}
//IItems-------------------------------------------------------------------------------------------------------------------------------
///查询用户背包物品数量
func (this *Items) QueryItemAmount(source *comm.ModuleCallSource, uId string, itemid string) (amount uint32) {
defer this.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount)
amount = 0
if result := this.modelItems.QueryUserPackItemsAmount(uId, itemid); result != nil && len(result) > 0 {
return result[itemid]
}
return
}
///查询用户背包多个物品数量
func (this *Items) QueryItemsAmount(source *comm.ModuleCallSource, uId string, itemid ...string) (result map[string]uint32) {
result = this.modelItems.QueryUserPackItemsAmount(uId, itemid...)
return
}
func (this *Items) AddItemforGrid(source *comm.ModuleCallSource, session comm.IUserSession, gridid string, addnum int32, bPush bool) (code pb.ErrorCode) {
var (
err error
change []*pb.DB_UserItemData
)
defer this.Debugf("给用户添加物品 uId:%s gridid:%d addnum:%d issucc:%v", session.GetUserId(), gridid, addnum, err == nil)
if change, err = this.modelItems.AddItemToUserPackByGrid(session.GetUserId(), gridid, addnum); err != nil {
this.Errorf("给用户添加物品 uId:%s gridid:%d addnum:%d err:%v", session.GetUserId(), gridid, addnum, err)
if err == ItemNotEnoughError {
code = pb.ErrorCode_ItemsNoEnough
} else if err == PackGridNumUpper {
code = pb.ErrorCode_ItemsGridNumUpper
} else {
code = pb.ErrorCode_Unknown
}
return
}
if bPush {
this.itemsChangePush(session, change) //推送道具背包变化
}
return
}
///添加单个物品到背包 (可以加物品和减物品)
func (this *Items) AddItem(source *comm.ModuleCallSource, session comm.IUserSession, itemid string, addnum int32, bPush bool) (code pb.ErrorCode) {
var (
err error
change []*pb.DB_UserItemData
)
defer this.Debugf("给用户添加物品 uId:%s itemid:%d addnum:%d issucc:%v", session.GetUserId(), itemid, addnum, err == nil)
if change, err = this.modelItems.AddItemToUserPack(session.GetUserId(), itemid, addnum); err != nil {
this.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", session.GetUserId(), itemid, addnum, err)
if err == ItemNotEnoughError {
code = pb.ErrorCode_ItemsNoEnough
} else if err == PackGridNumUpper {
code = pb.ErrorCode_ItemsGridNumUpper
} else {
code = pb.ErrorCode_Unknown
}
return
}
if bPush {
this.itemsChangePush(session, change) //推送道具背包变化
}
return
}
///添加多个物品到背包 (可以加物品和减物品)
func (this *Items) AddItems(source *comm.ModuleCallSource, session comm.IUserSession, items map[string]int32, bPush bool) (code pb.ErrorCode) {
var (
err error
change []*pb.DB_UserItemData
)
defer this.Debugf("给用户添加物品 uId:%s items:%d items:%v", session.GetUserId(), items, err == nil)
if change, err = this.modelItems.AddItemsToUserPack(session.GetUserId(), items); err != nil {
this.Errorf("给用户添加物品 uId:%s items:%d err:%v", session.GetUserId(), items, err)
if err == ItemNotEnoughError {
code = pb.ErrorCode_ItemsNoEnough
} else if err == PackGridNumUpper {
code = pb.ErrorCode_ItemsGridNumUpper
} else {
code = pb.ErrorCode_Unknown
}
return
}
if len(change) > 0 && bPush {
this.itemsChangePush(session, change) //推送道具背包变化
}
return
}
//Evens--------------------------------------------------------------------------------------------------------------------------------
//推送道具变化消息
func (this *Items) itemsChangePush(session comm.IUserSession, items []*pb.DB_UserItemData) (err error) {
session.SendMsg(string(this.GetType()), "change", &pb.ItemsChangePush{Grids: items})
return
}