上传背包接口补充
This commit is contained in:
parent
61faf1dd57
commit
83e65faf96
47
README.md
47
README.md
@ -46,6 +46,53 @@ utils #公用方法工具集
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### 启动服务
|
||||
- 调试模式下 工具VSCode 配置 .vscode\launch.json
|
||||
```shell
|
||||
{
|
||||
// 使用 IntelliSense 了解相关属性。
|
||||
// 悬停以查看现有属性的描述。
|
||||
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "gateway_1",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "debug",
|
||||
"program": "${workspaceFolder}/services/gateway", //配置Go项目启动文件路径,即main函数所在的.go文件的路径,${workspaceRoot}代表项目的根目录,也就是 /bin /pkg /src这三个文件夹所在的目录
|
||||
"args": ["-conf","./conf/gateway_1.yaml"], //指定服务启动的依赖配置文件 配置文件默认放在bin/conf 目录下
|
||||
"cwd": "${workspaceFolder}/bin", //设置工作目录
|
||||
"internalConsoleOptions": "openOnSessionStart",
|
||||
"output": "${workspaceFolder}/bin/vsdebug_gateway", //设置vscode调试时生成文件的路径
|
||||
"showGlobalVariables": true,
|
||||
"env": {}, //可以用来配置调试启动时所用的环境变量参数,比如gopath临时设置为某个参数就可以在这里指定,如果有多个gopath,用英文冒号:来连接多个gopath
|
||||
},
|
||||
{
|
||||
"name": "worker_1",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "debug",
|
||||
"program": "${workspaceFolder}/services/worker", //配置Go项目启动文件路径,即main函数所在的.go文件的路径,${workspaceRoot}代表项目的根目录,也就是 /bin /pkg /src这三个文件夹所在的目录
|
||||
"args": ["-conf","./conf/worker_1.yaml"], //指定服务启动的依赖配置文件
|
||||
"cwd": "${workspaceFolder}/bin", //设置工作目录
|
||||
"internalConsoleOptions": "openOnSessionStart",
|
||||
"output": "${workspaceFolder}/bin/vsdebug_worker", //设置vscode调试时生成文件的路径
|
||||
"showGlobalVariables": true,
|
||||
"env": {}, //可以用来配置调试启动时所用的环境变量参数,比如gopath临时设置为某个参数就可以在这里指定,如果有多个gopath,用英文冒号:来连接多个gopath
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
- 编译启动 执行 linux-build.bat 文件 生成执行文件 暂时只编译了linux版本的执行文件
|
||||
```
|
||||
cd ./bin
|
||||
./gateway -conf ./conf/gateway_1.yaml
|
||||
```
|
||||
|
||||
### 如何快速开发模块
|
||||
- 定义模块类型名称 在comm/core.go 中定义定义自己的业务模块名称
|
||||
```go
|
||||
|
@ -11,6 +11,7 @@ type (
|
||||
}
|
||||
//背包模块对外接口定义 提供给其他模块使用的
|
||||
IPack interface {
|
||||
//GetRewaredItems(uId string, itmes map[uint32]uint32)
|
||||
///添加多个物品到背包 (可以加物品和减物品)
|
||||
AddItemsToUserPack(uId string, items map[int32]int32) (err error)
|
||||
}
|
||||
)
|
||||
|
@ -85,7 +85,7 @@ func (this *UserSession) UnBuild(ServiceMethod string, msg proto.Message) (err e
|
||||
func (this *UserSession) SendMsg(mainType, subType string, code pb.ErrorCode, msg proto.Message) (err error) {
|
||||
reply := &pb.RPCMessageReply{}
|
||||
data, _ := proto.Marshal(msg)
|
||||
log.Debugf("SendMsg Data: %v", msg)
|
||||
log.Debugf("SendMsg to SessionId:[%s] UserId:[%s] Code:%d Data: %v", this.UserId, code, msg)
|
||||
if err := this.service.RpcCallById(this.GatewayServiceId, string(Rpc_GatewayAgentSendMsg), context.Background(), &pb.AgentSendMessageReq{
|
||||
UserSessionId: this.SessionId,
|
||||
MainType: mainType,
|
||||
|
@ -3,15 +3,8 @@ package mail
|
||||
import (
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/modules"
|
||||
"go_dreamfactory/pb"
|
||||
"time"
|
||||
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
"go_dreamfactory/sys/cache"
|
||||
"go_dreamfactory/sys/db"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -39,26 +32,3 @@ func (this *Mail) OnInstallComp() {
|
||||
this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp)
|
||||
this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp)
|
||||
}
|
||||
|
||||
func (this *Mail) CreateNewMail(uId string) {
|
||||
mail := &pb.DB_MailData{
|
||||
ObjId: primitive.NewObjectID().Hex(),
|
||||
UserId: uId,
|
||||
Title: "系统邮件",
|
||||
Contex: "恭喜获得专属礼包一份",
|
||||
CreateTime: uint64(time.Now().Unix()),
|
||||
DueTime: uint64(time.Now().Unix()) + 30*24*3600,
|
||||
Check: false,
|
||||
Reward: false,
|
||||
}
|
||||
err := db.Defsys.InsertUserMail(mail)
|
||||
if err != nil {
|
||||
log.Error("create mail failed")
|
||||
}
|
||||
// 通知玩家
|
||||
_cache := cache.Defsys.Get(uId)
|
||||
if _cache == nil {
|
||||
return
|
||||
}
|
||||
this.SendMsgToUser("mail", "getnewEmailresp", mail, _cache)
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ func (this *Api_Comp) QueryUserPackReq(ctx context.Context, session comm.IUserSe
|
||||
grids []*pb.DB_GridData
|
||||
)
|
||||
defer func() {
|
||||
session.SendMsg(string(this.module.GetType()), "queryuserpackresp", code, &pb.QueryUserPackResp{Grids: grids})
|
||||
session.SendMsg(string(this.module.GetType()), QueryUserPackResp, code, &pb.QueryUserPackResp{Grids: grids})
|
||||
}()
|
||||
if !session.IsLogin() {
|
||||
code = pb.ErrorCode_NoLogin
|
||||
|
11
modules/pack/core.go
Normal file
11
modules/pack/core.go
Normal file
@ -0,0 +1,11 @@
|
||||
package pack
|
||||
|
||||
import "go_dreamfactory/modules"
|
||||
|
||||
type (
|
||||
IPack interface {
|
||||
modules.IModule
|
||||
}
|
||||
IConfigure_Comp interface {
|
||||
}
|
||||
)
|
@ -3,6 +3,7 @@ package pack
|
||||
import (
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/modules"
|
||||
"go_dreamfactory/sys/cache"
|
||||
"go_dreamfactory/sys/db"
|
||||
|
||||
"go_dreamfactory/lego/core"
|
||||
@ -45,6 +46,14 @@ func (this *Pack) OnInstallComp() {
|
||||
this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp)
|
||||
}
|
||||
|
||||
///添加多个物品到背包 (可以加物品和减物品)
|
||||
func (this *Pack) AddItemsToUserPack(uId string, items map[int32]int32) (err error) {
|
||||
if err = cache.Defsys.Pack_AddItemsToUserPack(uId, items); err != nil {
|
||||
log.Errorf("AddItemsToUserPack err:%v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//Evens--------------------------------------------------------------------------------------------------------------------------------
|
||||
//接收玩家创建角色事件
|
||||
func (this *Pack) event_CreateUser(uid string) {
|
||||
|
226
sys/cache/pack.go
vendored
226
sys/cache/pack.go
vendored
@ -15,13 +15,15 @@ const ( //Redis
|
||||
)
|
||||
|
||||
const (
|
||||
GridCapMaxNum = 99 //单个格子的最大容量
|
||||
GridCapMaxNum = 99 //单个格子的最大容量
|
||||
GridMaxNUm = 200 //背包格子数量上限
|
||||
)
|
||||
|
||||
var (
|
||||
ItemNotEnoughError = errors.New("item not enough!") //物品不足
|
||||
NoFoundGirdError = errors.New("no found gvrid!") //未找到格子
|
||||
GirdAmountUpper = errors.New("grid amount upper!") //背包格子达到上限
|
||||
PackGridNumUpper = errors.New("grid amount upper!") //背包格子达到上限
|
||||
GirdAmountUpper = errors.New("grid amount upper!") //格子容量达到上限
|
||||
)
|
||||
|
||||
type IPack interface {
|
||||
@ -29,6 +31,8 @@ type IPack interface {
|
||||
Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error)
|
||||
///添加物品到背包 (可以加物品和减物品)
|
||||
Pack_AddItemToUserPack(uId string, itemId int32, addnum int32) (err error)
|
||||
///添加多个物品到背包 (可以加物品和减物品)
|
||||
Pack_AddItemsToUserPack(uId string, items map[int32]int32) (err error)
|
||||
//修改用户背包格子的标识
|
||||
Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (err error)
|
||||
}
|
||||
@ -56,8 +60,6 @@ func (this *Cache) Pack_AddItemToUserPack(uId string, itemId int32, addnum int32
|
||||
pack *pb.DB_UserPackData
|
||||
modifys []*pb.DB_GridData
|
||||
leftnum int64
|
||||
num int64
|
||||
isNew bool
|
||||
)
|
||||
if addnum == 0 {
|
||||
return
|
||||
@ -65,86 +67,54 @@ func (this *Cache) Pack_AddItemToUserPack(uId string, itemId int32, addnum int32
|
||||
if pack, err = this.Pack_QueryUserPack(uId); err != nil {
|
||||
return
|
||||
}
|
||||
isNew = true
|
||||
leftnum = int64(addnum)
|
||||
modifys = make([]*pb.DB_GridData, 0)
|
||||
for _, v := range pack.Pack {
|
||||
if !v.IsEmpty && v.ItemId == itemId {
|
||||
isNew = false
|
||||
num = int64(v.Amount) + int64(leftnum)
|
||||
if num < 0 {
|
||||
leftnum += int64(v.Amount)
|
||||
v.Amount = 0
|
||||
modifys = append(modifys, v)
|
||||
} else if num > 0 && num < int64(v.Amount) {
|
||||
leftnum = 0
|
||||
v.Amount = uint32(num)
|
||||
modifys = append(modifys, v)
|
||||
break
|
||||
} else if num > 0 && num > int64(v.Amount) {
|
||||
if num <= GridCapMaxNum {
|
||||
leftnum = 0
|
||||
v.Amount = uint32(num)
|
||||
modifys = append(modifys, v)
|
||||
break
|
||||
} else {
|
||||
if v.Amount < GridCapMaxNum {
|
||||
leftnum = int64(num - GridCapMaxNum)
|
||||
v.Amount = uint32(GridCapMaxNum)
|
||||
modifys = append(modifys, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if leftnum < 0 { //背包物品不够
|
||||
modifys, leftnum = this.pack_addItemToUserPack(pack, itemId, addnum)
|
||||
if leftnum < 0 {
|
||||
err = ItemNotEnoughError
|
||||
return
|
||||
} else if leftnum > 0 {
|
||||
err = PackGridNumUpper
|
||||
return
|
||||
}
|
||||
if leftnum > 0 { //还没有放完 寻找空的格子填充
|
||||
for _, v := range pack.Pack {
|
||||
if v.IsEmpty {
|
||||
if leftnum <= GridCapMaxNum {
|
||||
v.IsEmpty = false
|
||||
v.ItemId = itemId
|
||||
v.Amount = uint32(leftnum)
|
||||
leftnum = 0
|
||||
modifys = append(modifys, v)
|
||||
break
|
||||
} else {
|
||||
leftnum -= GridCapMaxNum
|
||||
v.IsEmpty = false
|
||||
v.ItemId = itemId
|
||||
v.Amount = uint32(GridCapMaxNum)
|
||||
modifys = append(modifys, v)
|
||||
if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, modifys...); err == nil {
|
||||
this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, -1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
///添加或则减少多个物品到用户背包
|
||||
func (this *Cache) Pack_AddItemsToUserPack(uId string, items map[int32]int32) (err error) {
|
||||
var (
|
||||
pack *pb.DB_UserPackData
|
||||
modifys []*pb.DB_GridData
|
||||
tempmodifys []*pb.DB_GridData
|
||||
leftnum int64
|
||||
iskeep bool
|
||||
)
|
||||
if pack, err = this.Pack_QueryUserPack(uId); err != nil {
|
||||
return
|
||||
}
|
||||
for k, v := range items {
|
||||
tempmodifys, leftnum = this.pack_addItemToUserPack(pack, k, v)
|
||||
if leftnum < 0 {
|
||||
err = ItemNotEnoughError
|
||||
return
|
||||
} else if leftnum > 0 {
|
||||
err = PackGridNumUpper
|
||||
return
|
||||
}
|
||||
for _, v1 := range tempmodifys {
|
||||
iskeep = false
|
||||
for _, v2 := range modifys {
|
||||
if v1.GridId == v2.GridId {
|
||||
iskeep = true
|
||||
}
|
||||
}
|
||||
}
|
||||
index := int32(len(pack.Pack))
|
||||
for leftnum > 0 { //需要补充格子
|
||||
if leftnum <= GridCapMaxNum {
|
||||
modifys = append(modifys, &pb.DB_GridData{
|
||||
GridId: index,
|
||||
IsEmpty: false,
|
||||
ItemId: itemId,
|
||||
Amount: uint32(leftnum),
|
||||
IsNewItem: isNew,
|
||||
})
|
||||
leftnum = 0
|
||||
break
|
||||
} else {
|
||||
leftnum -= GridCapMaxNum
|
||||
modifys = append(modifys, &pb.DB_GridData{
|
||||
GridId: index,
|
||||
IsEmpty: false,
|
||||
ItemId: itemId,
|
||||
Amount: uint32(GridCapMaxNum),
|
||||
IsNewItem: isNew,
|
||||
})
|
||||
index++
|
||||
if !iskeep {
|
||||
modifys = append(modifys, v1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, modifys...); err == nil {
|
||||
this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, -1)
|
||||
}
|
||||
@ -206,3 +176,107 @@ func (this *Cache) Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
///添加移除物品到用户背包
|
||||
func (this *Cache) pack_addItemToUserPack(pack *pb.DB_UserPackData, itemId int32, addnum int32) (modifys []*pb.DB_GridData, leftnum int64) {
|
||||
var (
|
||||
num int64
|
||||
isNew bool
|
||||
)
|
||||
if addnum == 0 {
|
||||
return
|
||||
}
|
||||
isNew = true
|
||||
leftnum = int64(addnum)
|
||||
modifys = make([]*pb.DB_GridData, 0)
|
||||
for _, v := range pack.Pack {
|
||||
if !v.IsEmpty && v.ItemId == itemId {
|
||||
isNew = false
|
||||
num = int64(v.Amount) + int64(leftnum)
|
||||
if num < 0 {
|
||||
leftnum += int64(v.Amount)
|
||||
v.Amount = 0
|
||||
v.IsEmpty = true
|
||||
modifys = append(modifys, v)
|
||||
} else if num > 0 && num < int64(v.Amount) {
|
||||
leftnum = 0
|
||||
v.Amount = uint32(num)
|
||||
modifys = append(modifys, v)
|
||||
break
|
||||
} else if num > 0 && num > int64(v.Amount) {
|
||||
if num <= GridCapMaxNum {
|
||||
leftnum = 0
|
||||
v.Amount = uint32(num)
|
||||
modifys = append(modifys, v)
|
||||
break
|
||||
} else {
|
||||
if v.Amount < GridCapMaxNum {
|
||||
leftnum = int64(num - GridCapMaxNum)
|
||||
v.Amount = uint32(GridCapMaxNum)
|
||||
modifys = append(modifys, v)
|
||||
}
|
||||
}
|
||||
} else if num == 0 {
|
||||
leftnum = 0
|
||||
v.Amount = 0
|
||||
v.IsEmpty = true
|
||||
modifys = append(modifys, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
if leftnum < 0 { //背包物品不够
|
||||
return
|
||||
}
|
||||
if leftnum > 0 { //还没有放完 寻找空的格子填充
|
||||
for _, v := range pack.Pack {
|
||||
if v.IsEmpty {
|
||||
if leftnum <= GridCapMaxNum {
|
||||
v.IsEmpty = false
|
||||
v.ItemId = itemId
|
||||
v.Amount = uint32(leftnum)
|
||||
leftnum = 0
|
||||
modifys = append(modifys, v)
|
||||
break
|
||||
} else {
|
||||
leftnum -= GridCapMaxNum
|
||||
v.IsEmpty = false
|
||||
v.ItemId = itemId
|
||||
v.Amount = uint32(GridCapMaxNum)
|
||||
modifys = append(modifys, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
index := int32(len(pack.Pack))
|
||||
for leftnum > 0 { //需要补充格子
|
||||
if leftnum <= GridCapMaxNum {
|
||||
grid := &pb.DB_GridData{
|
||||
GridId: index,
|
||||
IsEmpty: false,
|
||||
ItemId: itemId,
|
||||
Amount: uint32(leftnum),
|
||||
IsNewItem: isNew,
|
||||
}
|
||||
pack.Pack = append(pack.Pack, grid)
|
||||
modifys = append(modifys, grid)
|
||||
leftnum = 0
|
||||
break
|
||||
} else {
|
||||
leftnum -= GridCapMaxNum
|
||||
grid := &pb.DB_GridData{
|
||||
GridId: index,
|
||||
IsEmpty: false,
|
||||
ItemId: itemId,
|
||||
Amount: uint32(GridCapMaxNum),
|
||||
IsNewItem: isNew,
|
||||
}
|
||||
pack.Pack = append(pack.Pack, grid)
|
||||
modifys = append(modifys, grid)
|
||||
index++
|
||||
}
|
||||
if index > GridMaxNUm { //格子已达上限
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
6
sys/cache/pack_test.go
vendored
6
sys/cache/pack_test.go
vendored
@ -11,3 +11,9 @@ func Test_Pack_AddItemToUserPack(t *testing.T) {
|
||||
err := cache.Defsys.Pack_AddItemToUserPack("liwei1dao", 1001, 100)
|
||||
fmt.Printf("Pack_AddItemToUserPack err:%v\n", err)
|
||||
}
|
||||
|
||||
//测试用户背包 添加物品
|
||||
func Test_Pack_AddItemsToUserPack(t *testing.T) {
|
||||
err := cache.Defsys.Pack_AddItemsToUserPack("liwei1dao", map[int32]int32{1003: -100})
|
||||
fmt.Printf("Pack_AddItemToUserPack err:%v\n", err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user