go_dreamfactory/modules/items/module.go
2022-07-13 15:06:48 +08:00

99 lines
3.2 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 int32) (amount uint32) {
defer this.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount)
amount = 0
if result := this.modelItems.Pack_QueryUserPackItemsAmount(uId, itemid); result != nil && len(result) > 0 {
return result[itemid]
}
return
}
///查询用户背包多个物品数量
func (this *Items) QueryItemsAmount(source *comm.ModuleCallSource, uId string, itemid ...int32) (result map[int32]uint32) {
result = this.modelItems.Pack_QueryUserPackItemsAmount(uId, itemid...)
return
}
///添加单个物品到背包 (可以加物品和减物品)
func (this *Items) AddItem(source *comm.ModuleCallSource, uId string, itemid, addnum int32) (code pb.ErrorCode) {
var err error
defer this.Debugf("给用户添加物品 uId:%s itemid:%d addnum:%d issucc:%v", uId, itemid, addnum, err == nil)
if err = this.modelItems.Pack_AddItemToUserPack(uId, itemid, addnum); err != nil {
this.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", uId, itemid, addnum, err)
if err == ItemNotEnoughError {
code = pb.ErrorCode_ItemsNoEnough
} else if err == PackGridNumUpper {
code = pb.ErrorCode_ItemsGridNumUpper
} else {
code = pb.ErrorCode_Unknown
}
}
return
}
///添加多个物品到背包 (可以加物品和减物品)
func (this *Items) AddItems(source *comm.ModuleCallSource, uId string, items map[int32]int32) (code pb.ErrorCode) {
var err error
defer this.Debugf("给用户添加物品 uId:%s items:%d items:%v", uId, items, err == nil)
if err = this.modelItems.Pack_AddItemsToUserPack(uId, items); err != nil {
this.Errorf("给用户添加物品 uId:%s items:%d err:%v", uId, items, err)
if err == ItemNotEnoughError {
code = pb.ErrorCode_ItemsNoEnough
} else if err == PackGridNumUpper {
code = pb.ErrorCode_ItemsGridNumUpper
} else {
code = pb.ErrorCode_Unknown
}
}
return
}
//Evens--------------------------------------------------------------------------------------------------------------------------------