设置初始金币和卡牌

This commit is contained in:
zhaocy 2022-06-30 15:24:55 +08:00
parent 541ffb0d42
commit 22457148d8
8 changed files with 68 additions and 30 deletions

View File

@ -13,6 +13,7 @@ var (
//hero //hero
heroBuilders = []*builder{ heroBuilders = []*builder{
{ {
desc: "英雄列表",
mainType: string(comm.ModuleHero), mainType: string(comm.ModuleHero),
subType: hero.HeroSubTypeList, subType: hero.HeroSubTypeList,
req: &pb.HeroListReq{}, req: &pb.HeroListReq{},
@ -23,7 +24,7 @@ var (
fmt.Printf("%d- %v\n", (i + 1), v) fmt.Printf("%d- %v\n", (i + 1), v)
} }
}, },
// enabled: true, enabled: true,
}, { }, {
mainType: string(comm.ModuleHero), mainType: string(comm.ModuleHero),
subType: hero.HeroSubTypeInfo, subType: hero.HeroSubTypeInfo,
@ -62,7 +63,7 @@ var (
Amount: 1, Amount: 1,
}, },
rsp: &pb.HeroStrengthenUplvResp{}, rsp: &pb.HeroStrengthenUplvResp{},
enabled: true, // enabled: true,
}, },
} }
) )

View File

@ -68,6 +68,7 @@ func (r *Robot) Run() {
} }
type builder struct { type builder struct {
desc string
mainType string mainType string
subType string subType string
req proto.Message req proto.Message
@ -90,6 +91,7 @@ func (r *Robot) addBuilders(builders []*builder) {
func (r *Robot) handleReq() { func (r *Robot) handleReq() {
for _, b := range r.builders { for _, b := range r.builders {
if b.req != nil && !b.requested { if b.req != nil && !b.requested {
time.Sleep(time.Second * 1)
b.requested = true b.requested = true
b.start = time.Now() b.start = time.Now()
head := &pb.UserMessage{MainType: b.mainType, SubType: b.subType} head := &pb.UserMessage{MainType: b.mainType, SubType: b.subType}
@ -113,7 +115,7 @@ func (r *Robot) handleRsp(msg *pb.UserMessage) {
if b.print == nil { if b.print == nil {
printReply(msg, b) printReply(msg, b)
} else { } else {
fmt.Printf("===== rsp [%s.%s] =====\n", msg.MainType, msg.SubType) fmt.Printf("===== %s [%s.%s] =====\n", b.desc, msg.MainType, msg.SubType)
b.print(b.rsp) b.print(b.rsp)
fmt.Println("==============================") fmt.Println("==============================")
} }
@ -159,15 +161,14 @@ func (r *Robot) onUserLoaded() {
r.RunNotify() r.RunNotify()
//user //user
r.RunUser() r.RunUser()
//hero
r.RunHero()
//friend //friend
// r.RunFriend() // r.RunFriend()
//pack //pack
// r.RunPack() // r.RunPack()
//hero
r.RunHero()
} }
//注册账号 //注册账号
@ -198,21 +199,21 @@ func (r *Robot) AccountRegister(account string, sid int32) {
fmt.Printf("account:%s 注册成功", regRsp.Account) fmt.Printf("account:%s 注册成功", regRsp.Account)
//登录 //登录
loginReg := &pb.UserLoginReq{ var user_builders = []*builder{
{
desc: "登录",
mainType: "user",
subType: "login",
req: &pb.UserLoginReq{
Account: account, Account: account,
Sid: sid, Sid: sid,
},
rsp: &pb.UserLoginResp{},
enabled: true,
},
} }
r.addBuilders(user_builders)
head := &pb.UserMessage{ r.handleReq()
MainType: "user",
SubType: "login",
Sec: r.BuildSecStr(),
}
err = r.SendToClient(head, loginReg)
if err != nil {
log.Fatal(err)
}
} }
} }
@ -225,9 +226,9 @@ func printReply(msg *pb.UserMessage, builder *builder) {
} else { } else {
tt = time.Since(builder.start) tt = time.Since(builder.start)
} }
log.Printf("rsp [%v] [%s.%s] [%v:%v]", tt, m.ReqMainType, m.ReqSubType, int32(m.Code), m.Data) log.Printf("rsp %s [%v] [%s.%s] [%v:%v]", builder.desc, tt, m.ReqMainType, m.ReqSubType, int32(m.Code), m.Data)
} else { } else {
log.Printf("rsp [%v] [%s.%s] [%v]", time.Since(builder.start), msg.MainType, msg.SubType, builder.rsp) log.Printf("rsp %s [%v] [%s.%s] [%v]", builder.desc, time.Since(builder.start), msg.MainType, msg.SubType, builder.rsp)
} }
} }

View File

@ -10,13 +10,14 @@ import (
var user_builders = []*builder{ var user_builders = []*builder{
{ {
//create //create
desc: "创角",
mainType: string(comm.ModuleUser), mainType: string(comm.ModuleUser),
subType: user.UserSubTypeCreate, subType: user.UserSubTypeCreate,
req: &pb.UserCreateReq{ //设置请求参数 req: &pb.UserCreateReq{ //设置请求参数
NickName: "乐谷6301", NickName: "乐谷6301",
}, },
rsp: &pb.UserCreateRsp{}, rsp: &pb.UserCreateRsp{},
// enabled: true, enabled: true,
}, },
} }

1
go.mod
View File

@ -21,6 +21,7 @@ require (
github.com/rs/xid v1.3.0 github.com/rs/xid v1.3.0
github.com/satori/go.uuid v1.2.0 github.com/satori/go.uuid v1.2.0
github.com/smallnest/rpcx v1.7.4 github.com/smallnest/rpcx v1.7.4
github.com/spf13/cast v1.3.1
github.com/spf13/cobra v1.2.1 github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5 github.com/spf13/pflag v1.0.5
github.com/tidwall/gjson v1.14.1 github.com/tidwall/gjson v1.14.1

1
go.sum
View File

@ -623,6 +623,7 @@ github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE
github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:UdhH50NIW0fCiwBSr0co2m7BnFLdv4fQTgdqdJTHFeE= github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:UdhH50NIW0fCiwBSr0co2m7BnFLdv4fQTgdqdJTHFeE=
github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA= github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I= github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/cobra v1.2.1 h1:+KmjbUw1hriSNMF55oPrkZcb27aECyrj8V2ytv7kWDw= github.com/spf13/cobra v1.2.1 h1:+KmjbUw1hriSNMF55oPrkZcb27aECyrj8V2ytv7kWDw=

View File

@ -41,6 +41,11 @@ func (this *apiComp) Create(session comm.IUserSession, result map[string]interfa
"name": req.NickName, "name": req.NickName,
} }
//设置初始金币
if val := this.module.configure.GetGlobalConf("init_gold"); val != "" {
update["gold"] = val
}
err := this.module.modelUser.Change(session.GetUserId(), update) err := this.module.modelUser.Change(session.GetUserId(), update)
if err != nil { if err != nil {
code = pb.ErrorCode_DBError code = pb.ErrorCode_DBError
@ -48,12 +53,14 @@ func (this *apiComp) Create(session comm.IUserSession, result map[string]interfa
} }
//初始化英雄卡 //初始化英雄卡
defaultHero := []int32{15001, 25001} //TODO 从配置中读取 if val := this.module.configure.GetGlobalConf("init_hero"); val != "" {
defaultHero := utils.TrInt32(val)
err = this.hero.CreateHero(session.GetUserId(), defaultHero...) err = this.hero.CreateHero(session.GetUserId(), defaultHero...)
if err != nil { if err != nil {
code = pb.ErrorCode_HeroInitCreat code = pb.ErrorCode_HeroInitCreat
return return
} }
}
return return
} }

View File

@ -18,6 +18,7 @@ type User struct {
api *apiComp api *apiComp
modelUser *ModelUser modelUser *ModelUser
modelSession *ModelSession modelSession *ModelSession
configure *configureComp
} }
func (this *User) GetType() core.M_Modules { func (this *User) GetType() core.M_Modules {
@ -35,6 +36,7 @@ func (this *User) OnInstallComp() {
this.api = this.RegisterComp(new(apiComp)).(*apiComp) this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelUser = this.RegisterComp(new(ModelUser)).(*ModelUser) this.modelUser = this.RegisterComp(new(ModelUser)).(*ModelUser)
this.modelSession = this.RegisterComp(new(ModelSession)).(*ModelSession) this.modelSession = this.RegisterComp(new(ModelSession)).(*ModelSession)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
} }
//获取英雄列表 //获取英雄列表

View File

@ -1,9 +1,12 @@
package utils package utils
import ( import (
"strconv"
"strings" "strings"
"go_dreamfactory/lego/sys/log" "go_dreamfactory/lego/sys/log"
"github.com/spf13/cast"
) )
func ParseP(p string) (string, string, bool) { func ParseP(p string) (string, string, bool) {
@ -34,3 +37,24 @@ func DeleteString(list []string, ele string) []string {
} }
return result return result
} }
//转换包含数字字符串数组
//eg. "3,4,5" to []int32{3,4,5}
func TrInt32(val string) (nums []int32) {
strArr := strings.Split(val, ",")
if len(strArr) == 0 {
return
}
for _, n := range strArr {
if IsNum(strings.TrimSpace(n)) {
nums = append(nums, cast.ToInt32(strings.TrimSpace(n)))
}
}
return
}
func IsNum(s string) bool {
_, err := strconv.ParseFloat(s, 64)
return err == nil
}