go_dreamfactory/modules/items/module.go
2023-04-20 20:58:24 +08:00

197 lines
6.0 KiB
Go

package items
import (
"go_dreamfactory/comm"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/event"
)
/*
模块名:Pack
描述:背包系统模块
开发:李伟
*/
func NewModule() core.IModule {
m := new(Items)
return m
}
type Items struct {
modules.ModuleBase
service base.IRPCXService
privilege comm.IPrivilege
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)
this.service = service.(base.IRPCXService)
return
}
//模块启动
func (this *Items) Start() (err error) {
err = this.ModuleBase.Start()
var module core.IModule
if module, err = this.service.GetModule(comm.ModulePrivilege); err != nil {
return
}
this.privilege = module.(comm.IPrivilege)
event.RegisterGO(comm.EventUserOffline, this.EventUserOffline)
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)
}
//Event------------------------------------------------------------------------------------------------------------
func (this *Items) EventUserOffline(uid, sessionid string) {
this.modelItems.BatchDelLists(uid)
}
//IItems-------------------------------------------------------------------------------------------------------------------------------
///查询用户背包物品数量
func (this *Items) QueryItemAmount(uId string, itemid string) (amount uint32) {
defer this.Debugf("获取物品 uId:%s itemid:%s 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(uId string, itemid ...string) (result map[string]uint32) {
result = this.modelItems.QueryUserPackItemsAmount(uId, itemid...)
return
}
func (this *Items) AddItemforGrid(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(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(session comm.IUserSession, items map[string]int32, bPush bool) (change []*pb.DB_UserItemData, code pb.ErrorCode) {
var (
err error
)
defer this.Debugf("给用户添加物品 uId:%s items:%v items:%v", session.GetUserId(), items, err == nil)
if change, err = this.modelItems.AddItemsToUserPack(session.GetUserId(), items); err != nil {
this.Errorf("给用户添加物品 uId:%s items:%v err:%v", session.GetUserId(), items, err)
if err == ItemNotEnoughError {
code = pb.ErrorCode_ItemsNoEnough
} else if err == PackGridNumUpper {
code = pb.ErrorCode_ItemsGridNumUpper
} else if err == NoFoundItemConfig {
code = pb.ErrorCode_ConfigNoFound
} else {
code = pb.ErrorCode_Unknown
}
return
}
if len(change) > 0 && bPush {
this.itemsChangePush(session, change) //推送道具背包变化
}
return
}
//清理背包
func (this *Items) CleanItems(session comm.IUserSession) (code pb.ErrorCode) {
var (
err error
itmes []*pb.DB_UserItemData
)
if itmes, err = this.modelItems.QueryUserPack(session.GetUserId()); err != nil {
this.Errorf("err:%v", err)
return
}
for _, v := range itmes {
v.Amount = 0
}
this.modelItems.DeleteUserPack(session.GetUserId(), itmes...)
this.itemsChangePush(session, itmes) //推送道具背包变化
return
}
//购买门票道具
func (this *Items) BuyUnifiedTicket(session comm.IUserSession, buynum int32) (code pb.ErrorCode) {
_, code = this.modelItems.buyTicket(session, buynum)
return
}
//回复道具
func (this *Items) RecoverTicket(session comm.IUserSession) (code pb.ErrorCode) {
code = this.modelItems.recoverTicket(session)
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
}