101 lines
3.2 KiB
Go
101 lines
3.2 KiB
Go
package items
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/log"
|
|
)
|
|
|
|
/*
|
|
模块名:Pack
|
|
描述:背包系统模块
|
|
开发:李伟
|
|
*/
|
|
func NewModule() core.IModule {
|
|
m := new(Items)
|
|
return m
|
|
}
|
|
|
|
type Items struct {
|
|
modules.ModuleBase
|
|
api_comp *Api_Comp
|
|
model_pack_comp *Model_Pack_Comp
|
|
// db_comp *DB_Comp
|
|
configure_comp *Configure_Comp
|
|
}
|
|
|
|
//模块名称
|
|
func (this *Items) GetType() core.M_Modules {
|
|
return comm.SM_ItemsModule
|
|
}
|
|
|
|
//模块初始化接口 注册用户创建角色事件
|
|
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_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp)
|
|
this.model_pack_comp = this.RegisterComp(new(Model_Pack_Comp)).(*Model_Pack_Comp)
|
|
this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp)
|
|
}
|
|
|
|
//IPack-------------------------------------------------------------------------------------------------------------------------------
|
|
///查询用户背包物品数量
|
|
func (this *Items) QueryUserItemAmount(uId string, itemid int32) (amount uint32) {
|
|
defer log.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount)
|
|
amount = 0
|
|
if result := this.model_pack_comp.Pack_QueryUserPackItemsAmount(uId, itemid); result != nil && len(result) > 0 {
|
|
return result[itemid]
|
|
}
|
|
return
|
|
}
|
|
|
|
///查询用户背包多个物品数量
|
|
func (this *Items) QueryUserItemsAmount(uId string, itemid ...int32) (result map[int32]uint32) {
|
|
result = this.model_pack_comp.Pack_QueryUserPackItemsAmount(uId, itemid...)
|
|
return
|
|
}
|
|
|
|
///添加单个物品到背包 (可以加物品和减物品)
|
|
func (this *Items) AddItem(uId string, itemid, addnum int32) (code pb.ErrorCode) {
|
|
var err error
|
|
defer log.Debugf("给用户添加物品 uId:%s itemid:%d addnum:%d issucc:%v", uId, itemid, addnum, err == nil)
|
|
if err = this.model_pack_comp.Pack_AddItemToUserPack(uId, itemid, addnum); err != nil {
|
|
log.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(uId string, items map[int32]int32) (code pb.ErrorCode) {
|
|
var err error
|
|
defer log.Debugf("给用户添加物品 uId:%s items:%d items:%v", uId, items, err == nil)
|
|
if err = this.model_pack_comp.Pack_AddItemsToUserPack(uId, items); err != nil {
|
|
log.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--------------------------------------------------------------------------------------------------------------------------------
|