Merge branch 'dev' of http://git.legu.cc/liwei_3d/go_dreamfactory into meixiongfeng

This commit is contained in:
meixiongfeng 2022-06-07 17:31:00 +08:00
commit 1fda87e83d
29 changed files with 998 additions and 233 deletions

552
bin/json/tbhero.json Normal file
View File

@ -0,0 +1,552 @@
[
{
"id": "11001",
"name": "阿宝",
"star": 4,
"color": 3,
"chengwei": 3,
"zhongzu": 1,
"job": 1,
"type": 1,
"prefab": "Person/11001.prefab",
"icon": "11001",
"sound": "121323",
"tujing": "[1]",
"hpgrow": "57.4",
"atkgrow": "7.2",
"defgrow": "5.6",
"speedgrow": "2",
"hp": "576",
"atk": "73",
"def": "57",
"speed": "174",
"intr": "",
"events": "EventDataSet/Hero_11001.asset",
"cite": ""
},
{
"id": "11002",
"name": "原始人",
"star": 5,
"color": 4,
"chengwei": 3,
"zhongzu": 1,
"job": 1,
"type": 1,
"prefab": "Person/11002.prefab",
"icon": "11002",
"sound": "11002",
"tujing": "[1]",
"hpgrow": "82.9",
"atkgrow": "9.4",
"defgrow": "5.8",
"speedgrow": "2",
"hp": "830",
"atk": "95",
"def": "59",
"speed": "193",
"intr": "",
"events": "EventDataSet/Hero_11002.asset",
"cite": ""
},
{
"id": "11003",
"name": "波比",
"star": 5,
"color": 4,
"chengwei": 3,
"zhongzu": 1,
"job": 1,
"type": 1,
"prefab": "Person/11003.prefab",
"icon": "11003",
"sound": "11003",
"tujing": "[1]",
"hpgrow": "82.9",
"atkgrow": "9.4",
"defgrow": "5.8",
"speedgrow": "2",
"hp": "830",
"atk": "95",
"def": "59",
"speed": "193",
"intr": "",
"events": "EventDataSet/Hero_11003.asset",
"cite": ""
},
{
"id": "11004",
"name": "布兰",
"star": 5,
"color": 4,
"chengwei": 3,
"zhongzu": 1,
"job": 1,
"type": 1,
"prefab": "11004",
"icon": "11004",
"sound": "11004",
"tujing": "[1]",
"hpgrow": "82.9",
"atkgrow": "9.4",
"defgrow": "5.8",
"speedgrow": "2",
"hp": "830",
"atk": "95",
"def": "59",
"speed": "193",
"intr": "",
"events": "hero_2011",
"cite": ""
},
{
"id": "11005",
"name": "无牙仔",
"star": 4,
"color": 3,
"chengwei": 3,
"zhongzu": 1,
"job": 1,
"type": 1,
"prefab": "Person/11001.prefab",
"icon": "11005",
"sound": "121323",
"tujing": "[1]",
"hpgrow": "57.4",
"atkgrow": "7.2",
"defgrow": "5.6",
"speedgrow": "2",
"hp": "576",
"atk": "73",
"def": "57",
"speed": "174",
"intr": "",
"events": "EventDataSet/Hero_11001.asset",
"cite": ""
},
{
"id": "11006",
"name": "亚丝翠",
"star": 5,
"color": 4,
"chengwei": 3,
"zhongzu": 1,
"job": 1,
"type": 1,
"prefab": "11006",
"icon": "11006",
"sound": "11006",
"tujing": "[1]",
"hpgrow": "82.9",
"atkgrow": "9.4",
"defgrow": "5.8",
"speedgrow": "2",
"hp": "830",
"atk": "95",
"def": "59",
"speed": "193",
"intr": "",
"events": "hero_2011",
"cite": ""
},
{
"id": "11009",
"name": "希卡普",
"star": 4,
"color": 3,
"chengwei": 4,
"zhongzu": 2,
"job": 2,
"type": 1,
"prefab": "11009",
"icon": "11009",
"sound": "11009",
"tujing": "[1]",
"hpgrow": "38.6",
"atkgrow": "10.5",
"defgrow": "5.7",
"speedgrow": "2",
"hp": "389",
"atk": "106",
"def": "58",
"speed": "165",
"intr": "",
"events": "EventDataSet/Hero_11009.asset",
"cite": ""
},
{
"id": "22002",
"name": "沈王爷青春版",
"star": 5,
"color": 4,
"chengwei": 4,
"zhongzu": 2,
"job": 2,
"type": 1,
"prefab": "22002",
"icon": "22002",
"sound": "22002",
"tujing": "[1]",
"hpgrow": "55.6",
"atkgrow": "13.6",
"defgrow": "5.9",
"speedgrow": "2",
"hp": "561",
"atk": "137",
"def": "60",
"speed": "183",
"intr": "",
"events": "hero_2011",
"cite": ""
},
{
"id": "90001",
"name": "邪恶阿宝",
"star": 4,
"color": 3,
"chengwei": 3,
"zhongzu": 1,
"job": 1,
"type": 1,
"prefab": "Person/90001.prefab",
"icon": "90001",
"sound": "90001",
"tujing": "[1]",
"hpgrow": "57.4",
"atkgrow": "7.2",
"defgrow": "5.6",
"speedgrow": "2",
"hp": "576",
"atk": "73",
"def": "57",
"speed": "174",
"intr": "",
"events": "hero_90001",
"cite": "11001"
},
{
"id": "11011",
"name": "浣熊",
"star": 4,
"color": 3,
"chengwei": 4,
"zhongzu": 2,
"job": 2,
"type": 1,
"prefab": "11011",
"icon": "11011",
"sound": "11011",
"tujing": "[1]",
"hpgrow": "38.6",
"atkgrow": "10.5",
"defgrow": "5.7",
"speedgrow": "2",
"hp": "389",
"atk": "106",
"def": "58",
"speed": "165",
"intr": "",
"events": "EventDataSet/Hero_11011.asset",
"cite": ""
},
{
"id": "11008",
"name": "悍娇虎",
"star": 5,
"color": 4,
"chengwei": 4,
"zhongzu": 2,
"job": 2,
"type": 1,
"prefab": "11008",
"icon": "11008",
"sound": "11008",
"tujing": "[1]",
"hpgrow": "55.6",
"atkgrow": "13.6",
"defgrow": "5.9",
"speedgrow": "2",
"hp": "561",
"atk": "137",
"def": "60",
"speed": "183",
"intr": "",
"events": "hero_2011",
"cite": ""
},
{
"id": "11017",
"name": "冰霜杰克",
"star": 5,
"color": 4,
"chengwei": 4,
"zhongzu": 2,
"job": 2,
"type": 1,
"prefab": "11017",
"icon": "11017",
"sound": "11017",
"tujing": "[1]",
"hpgrow": "55.6",
"atkgrow": "13.6",
"defgrow": "5.9",
"speedgrow": "2",
"hp": "561",
"atk": "137",
"def": "60",
"speed": "183",
"intr": "",
"events": "EventDataSet/Hero_11017.asset",
"cite": ""
},
{
"id": "11018",
"name": "睡神",
"star": 5,
"color": 4,
"chengwei": 4,
"zhongzu": 2,
"job": 2,
"type": 1,
"prefab": "11018",
"icon": "11018",
"sound": "11018",
"tujing": "[1]",
"hpgrow": "55.6",
"atkgrow": "13.6",
"defgrow": "5.9",
"speedgrow": "2",
"hp": "561",
"atk": "137",
"def": "60",
"speed": "183",
"intr": "",
"events": "EventDataSet/Hero_11018.asset",
"cite": ""
},
{
"id": "11019",
"name": "莫甘娜",
"star": 5,
"color": 4,
"chengwei": 4,
"zhongzu": 2,
"job": 2,
"type": 1,
"prefab": "11019",
"icon": "11019",
"sound": "11019",
"tujing": "[1]",
"hpgrow": "55.6",
"atkgrow": "13.6",
"defgrow": "5.9",
"speedgrow": "2",
"hp": "561",
"atk": "137",
"def": "60",
"speed": "183",
"intr": "",
"events": "EventDataSet/Hero_11019.asset",
"cite": ""
},
{
"id": "11020",
"name": "莫甘娜二阶段",
"star": 5,
"color": 4,
"chengwei": 4,
"zhongzu": 2,
"job": 2,
"type": 1,
"prefab": "11020",
"icon": "11020",
"sound": "11020",
"tujing": "[1]",
"hpgrow": "55.6",
"atkgrow": "13.6",
"defgrow": "5.9",
"speedgrow": "2",
"hp": "561",
"atk": "137",
"def": "60",
"speed": "183",
"intr": "",
"events": "EventDataSet/Hero_11020.asset",
"cite": ""
},
{
"id": "11021",
"name": "莫甘娜巨怪",
"star": 5,
"color": 4,
"chengwei": 4,
"zhongzu": 2,
"job": 2,
"type": 1,
"prefab": "11021",
"icon": "11021",
"sound": "11021",
"tujing": "[1]",
"hpgrow": "55.6",
"atkgrow": "13.6",
"defgrow": "5.9",
"speedgrow": "2",
"hp": "561",
"atk": "137",
"def": "60",
"speed": "183",
"intr": "",
"events": "EventDataSet/Hero_11021.asset",
"cite": ""
},
{
"id": "11099",
"name": "触手",
"star": 5,
"color": 4,
"chengwei": 4,
"zhongzu": 2,
"job": 2,
"type": 1,
"prefab": "11099",
"icon": "11099",
"sound": "11099",
"tujing": "[1]",
"hpgrow": "55.6",
"atkgrow": "13.6",
"defgrow": "5.9",
"speedgrow": "2",
"hp": "561",
"atk": "137",
"def": "60",
"speed": "183",
"intr": "",
"events": "EventDataSet/Hero_11099.asset",
"cite": ""
},
{
"id": "12001",
"name": "黑化阿宝",
"star": 5,
"color": 4,
"chengwei": 4,
"zhongzu": 2,
"job": 2,
"type": 1,
"prefab": "Person/12001.prefab",
"icon": "12001",
"sound": "121323",
"tujing": "[1]",
"hpgrow": "57.4",
"atkgrow": "7.2",
"defgrow": "5.6",
"speedgrow": "2",
"hp": "576",
"atk": "73",
"def": "57",
"speed": "174",
"intr": "",
"events": "EventDataSet/Hero_12001.asset",
"cite": ""
},
{
"id": "12003",
"name": "黑化包比",
"star": 5,
"color": 4,
"chengwei": 4,
"zhongzu": 2,
"job": 2,
"type": 1,
"prefab": "Person/11003.prefab",
"icon": "12003",
"sound": "12003",
"tujing": "[1]",
"hpgrow": "82.9",
"atkgrow": "9.4",
"defgrow": "5.8",
"speedgrow": "2",
"hp": "830",
"atk": "95",
"def": "59",
"speed": "193",
"intr": "",
"events": "EventDataSet/Hero_11003.asset",
"cite": "11003"
},
{
"id": "12009",
"name": "黑化希卡普",
"star": 5,
"color": 4,
"chengwei": 4,
"zhongzu": 2,
"job": 2,
"type": 1,
"prefab": "12009",
"icon": "11009",
"sound": "11009",
"tujing": "[1]",
"hpgrow": "38.6",
"atkgrow": "10.5",
"defgrow": "5.7",
"speedgrow": "2",
"hp": "389",
"atk": "106",
"def": "58",
"speed": "165",
"intr": "",
"events": "EventDataSet/Hero_11009.asset",
"cite": ""
},
{
"id": "12017",
"name": "黑化杰克",
"star": 5,
"color": 4,
"chengwei": 4,
"zhongzu": 2,
"job": 2,
"type": 1,
"prefab": "12017",
"icon": "12017",
"sound": "12017",
"tujing": "[1]",
"hpgrow": "55.6",
"atkgrow": "13.6",
"defgrow": "5.9",
"speedgrow": "2",
"hp": "561",
"atk": "137",
"def": "60",
"speed": "183",
"intr": "",
"events": "EventDataSet/Hero_11017.asset",
"cite": ""
},
{
"id": "12018",
"name": "黑化睡神",
"star": 5,
"color": 4,
"chengwei": 4,
"zhongzu": 2,
"job": 2,
"type": 1,
"prefab": "12018",
"icon": "12018",
"sound": "12018",
"tujing": "[1]",
"hpgrow": "55.6",
"atkgrow": "13.6",
"defgrow": "5.9",
"speedgrow": "2",
"hp": "561",
"atk": "137",
"def": "60",
"speed": "183",
"intr": "",
"events": "EventDataSet/Hero_11018.asset",
"cite": "11018"
}
]

View File

@ -1,47 +1,55 @@
package robot
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"go_dreamfactory/utils"
"log"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/nacos-group/nacos-sdk-go/util"
"google.golang.org/protobuf/proto"
)
func (r *Robot) handleLogin(msg *pb.UserMessage) {
switch msg.SubType {
case "login":
handleLogin(r, msg)
default:
log.Fatal("methodName no exist")
}
type LoginParam struct {
Account string `json:"account"`
ServerId int `json:"serverId"`
TimeStamp int64 `json:"timestamp"`
}
//处理接口响应数据
func handleLogin(r *Robot, msg *pb.UserMessage) {
rsp := &pb.UserLoginResp{}
if !comm.ProtoDecode(msg, rsp) {
return
}
log.Printf("to client: %v", rsp.Data)
func (r *Robot) BuildSecStr() string {
jsonByte, _ := jsoniter.Marshal(&LoginParam{
Account: r.Opts.Account,
ServerId: r.Opts.ServerId,
TimeStamp: time.Now().Unix(),
})
jsonBase64 := utils.Base64Encode(jsonByte)
log.Printf("client base64:%s", jsonBase64)
clientMd5key := util.Md5(jsonBase64)
log.Printf("client md5:%s", clientMd5key)
return fmt.Sprintf("CE:%s%s", clientMd5key, jsonBase64)
}
//处理登录请求
func (r *Robot) AccountLogin() {
//登录
sec := r.BuildSecStr()
log.Printf("client secret key:%s", sec)
loginReg := &pb.UserLoginReq{
Name: r.Opts.Account,
Sec: sec,
}
head := &pb.UserMessage{
MainType: "login",
MainType: "user",
SubType: "login",
}
if comm.ProtoEncode(loginReg, head) {
data, _ := proto.Marshal(head)
err := r.SendToClient(data)
if err != nil {
log.Fatal(err)
log.Fatalf("send err:%v", err)
}
}

View File

@ -5,6 +5,8 @@ type Options struct {
RegUrl string //账号注册接口地址
Account string //玩家账号
Create bool
Secretkey string //秘钥串
ServerId int
}
func DefaultOpts() *Options {
@ -12,6 +14,7 @@ func DefaultOpts() *Options {
WsUrl: "ws://localhost:7891/gateway",
RegUrl: "http://localhost:8000/register",
Create: false,
ServerId: 1,
}
}

View File

@ -39,7 +39,6 @@ func (r *Robot) Run() {
if r.Opts.Create { //创建新用户
r.AccountRegister()
} else {
//login user
r.AccountLogin()
}
@ -62,10 +61,9 @@ func (r *Robot) Run() {
select {}
}
//模块处理
func (r *Robot) handleMsg(msg *pb.UserMessage) {
switch msg.MainType {
case "login":
r.handleLogin(msg)
case "user":
r.handleUserMsg(msg)
default:
@ -73,6 +71,7 @@ func (r *Robot) handleMsg(msg *pb.UserMessage) {
}
}
//在这里添加玩家成功登录以后的测试方法
func (r *Robot) onUserLoaded() {
}
@ -103,13 +102,14 @@ func (r *Robot) AccountRegister() {
err = jsoniter.Unmarshal(body, regRsp)
if regRsp.Code == pb.ErrorCode_Success { //注册成功
//登录
loginReg := &pb.UserLoginReq{
Name: regReq.Account,
Sec: r.BuildSecStr(),
}
head := &pb.UserMessage{
MainType: "login",
MainType: "user",
SubType: "login",
}
if comm.ProtoEncode(loginReg, head) {

View File

@ -3,37 +3,40 @@ package robot
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"github.com/liwei1dao/lego/sys/log"
"log"
)
func (r *Robot) handleUserMsg(msg *pb.UserMessage) {
switch msg.SubType {
//创建账号
case "login":
handleLogin(r, msg)
case "create":
case "load":
handleCreateUser(r, msg)
}
}
//加载玩家数据
if msg.SubType == "load" {
rsp := &pb.UserLoadRsp{}
//处理登录响应数据
func handleLogin(r *Robot, msg *pb.UserMessage) {
rsp := &pb.UserLoginResp{}
if !comm.ProtoDecode(msg, rsp) {
log.Fatal("load user err")
return
}
log.Printf("登录返回: %v", rsp)
if rsp.Data.UserData.UserId != "" {
//是否有登录数据返回
if rsp != nil {
r.onUserLoaded()
} else {
log.Debugf("%s不存在", r.Opts.Account)
//创建
}
r.AccountRegister() //请求Http接口模拟创建新账号
}
}
func handleCreateUser(r *Robot, msg *pb.UserMessage) {
}
//创角色
func (r *Robot) CreateUser() {
}

View File

@ -16,7 +16,7 @@ const (
const (
SM_GateModule core.M_Modules = "gateway" //gate模块 网关服务模块
SM_WebModule core.M_Modules = "web" //web模块
SM_LoginModule core.M_Modules = "user" //用户模块
SM_UserModule core.M_Modules = "user" //用户模块
SM_PackModule core.M_Modules = "pack" //背包模块
SM_MailModule core.M_Modules = "mail" //邮件模块
)
@ -31,6 +31,10 @@ const ( //Rpc
Rpc_GatewayAgentClose core.Rpc_Key = "Rpc_GatewayAgentClose" //代理关闭 关闭用户连接
)
const (
Event_UserLogin core.Event_Key = "Event_UserLogin" //登录事件
)
type ISC_GateRouteComp interface {
core.IServiceComp
RegisterRoute(methodName string, comp reflect.Value, msg reflect.Type, fn reflect.Method)

View File

@ -52,7 +52,7 @@ func (this *UserSession) Build(uid string) (err error) {
UserSessionId: this.SessionId,
UserId: uid,
}, reply); err != nil {
log.Errorf("UserSession:%s UserId:%d Build:%s err:%v", this.SessionId, this.UserId, err)
log.Errorf("UserSession:%s UserId:%s Build:%s err:%v", this.SessionId, this.UserId, err)
}
return
}
@ -62,7 +62,7 @@ func (this *UserSession) UnBuild(ServiceMethod string, msg proto.Message) (err e
if err := this.service.RpcCallById(this.GatewayServiceId, string(Rpc_GatewayAgentUnBuild), context.Background(), &pb.AgentUnBuildReq{
UserSessionId: this.SessionId,
}, reply); err != nil {
log.Errorf("UserSession:%s UserId:%d UnBuild err:%v", this.SessionId, this.UserId, err)
log.Errorf("UserSession:%s UserId:%s UnBuild err:%v", this.SessionId, this.UserId, err)
}
return
}
@ -78,7 +78,7 @@ func (this *UserSession) SendMsg(mainType, subType string, code pb.ErrorCode, ms
Code: code,
Data: data,
}, reply); err != nil {
log.Errorf("UserSession:%s UserId:%d SendMsg:%s err:%v", this.SessionId, this.UserId, mainType, err)
log.Errorf("UserSession:%s UserId:%s SendMsg:%s err:%v", this.SessionId, this.UserId, mainType, err)
}
return
}
@ -93,5 +93,5 @@ func (this *UserSession) Close() (err error) {
}
func (this *UserSession) ToString() string {
return fmt.Sprintf("SessionId:%s UserId:%d GatewayServiceId:%s", this.SessionId, this.UserId, this.GatewayServiceId)
return fmt.Sprintf("SessionId:%s UserId:%s GatewayServiceId:%s", this.SessionId, this.UserId, this.GatewayServiceId)
}

3
go.mod
View File

@ -10,6 +10,7 @@ require (
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.1
github.com/tidwall/gjson v1.14.1
go.mongodb.org/mongo-driver v1.5.1
google.golang.org/protobuf v1.28.0
)
@ -98,6 +99,8 @@ require (
github.com/soheilhy/cmux v0.1.5 // indirect
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 // indirect
github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tinylib/msgp v1.1.6 // indirect
github.com/tjfoc/gmsm v1.4.1 // indirect
github.com/toolkits/concurrent v0.0.0-20150624120057-a4371d70e3e3 // indirect

7
go.sum
View File

@ -783,8 +783,13 @@ github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 h1:89CEmDvlq/F7S
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161/go.mod h1:wM7WEvslTq+iOEAMDLSzhVuOt5BRZ05WirO+b09GHQU=
github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b h1:fj5tQ8acgNUr6O8LEplsxDhUIe2573iLkJc+PqnzZTI=
github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b/go.mod h1:5XA7W9S6mni3h5uvOC75dA3m9CCCaS83lltmc0ukdi4=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo=
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tinylib/msgp v1.1.6 h1:i+SbKraHhnrf9M5MYmvQhFnbLhAXSDWF8WWsuyRdocw=
github.com/tinylib/msgp v1.1.6/go.mod h1:75BAfg2hauQhs3qedfdDZmWAPcFMAvJE5b9rGOMufyw=
github.com/tjfoc/gmsm v1.4.1 h1:aMe1GlZb+0bLjn+cKTPEvvn9oUEBlJitaZiiBwsbgho=

View File

@ -52,19 +52,19 @@ func (this *Agent) readLoop() {
locp:
for {
if _, data, err = this.wsConn.ReadMessage(); err != nil {
log.Errorf("agent:%s uId:%d ReadMessage err:%v", this.sessionId, this.uId, err)
log.Errorf("agent:%s uId:%s ReadMessage err:%v", this.sessionId, this.uId, err)
go this.Close()
break locp
}
if err = proto.Unmarshal(data, msg); err != nil {
log.Errorf("agent:%s uId:%d Unmarshal err:%v", this.sessionId, this.uId, err)
log.Errorf("agent:%s uId:%s Unmarshal err:%v", this.sessionId, this.uId, err)
go this.Close()
break locp
} else {
this.messageDistribution(msg)
}
}
log.Debugf("agent:%s uId:%d readLoop end!", this.sessionId, this.uId)
log.Debugf("agent:%s uId:%s readLoop end!", this.sessionId, this.uId)
}
func (this *Agent) writeLoop() {
@ -90,7 +90,7 @@ locp:
}
}
}
log.Debugf("agent:%s uId:%d writeLoop end!", this.sessionId, this.uId)
log.Debugf("agent:%s uId:%s writeLoop end!", this.sessionId, this.uId)
}
func (this *Agent) SessionId() string {
@ -135,7 +135,7 @@ func (this *Agent) Close() {
//分发用户消息
func (this *Agent) messageDistribution(msg *pb.UserMessage) {
reply := &pb.RPCMessageReply{}
log.Debugf("agent:%s uId:%d MessageDistribution msg:%s.%s", this.sessionId, this.uId, msg.MainType, msg.SubType)
log.Debugf("agent:%s uId:%s MessageDistribution msg:%s.%s", this.sessionId, this.uId, msg.MainType, msg.SubType)
if err := this.gateway.Service().RpcCallByType("worker", string(comm.Rpc_GatewayRoute), context.Background(), &pb.AgentMessage{
Ip: this.IP(),
UserSessionId: this.sessionId,
@ -144,8 +144,8 @@ func (this *Agent) messageDistribution(msg *pb.UserMessage) {
Method: fmt.Sprintf("%s.%s", msg.MainType, msg.SubType),
Message: msg.Data,
}, reply); err != nil {
log.Errorf("agent:%s uId:%d MessageDistribution err:%v", this.sessionId, this.uId, err)
log.Errorf("agent:%s uId:%s MessageDistribution err:%v", this.sessionId, this.uId, err)
} else {
log.Debugf("agent:%s uId:%d MessageDistribution reply:%v", this.sessionId, this.uId, reply)
log.Debugf("agent:%s uId:%s MessageDistribution reply:%v", this.sessionId, this.uId, reply)
}
}

View File

@ -1,59 +0,0 @@
package gateway_test
import (
"fmt"
"go_dreamfactory/pb"
"os"
"os/signal"
"syscall"
"testing"
"github.com/gorilla/websocket"
"google.golang.org/protobuf/proto"
)
func Test_WebSocket(t *testing.T) {
url := "ws://localhost:7891/gateway" //服务器地址
ws, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
fmt.Printf("err:%v", err)
return
}
go func() {
var msg *pb.UserMessage = &pb.UserMessage{}
for {
_, data, err := ws.ReadMessage()
if err != nil {
fmt.Printf("err:%v\n", err)
}
if err = proto.Unmarshal(data, msg); err != nil {
fmt.Printf("err:%v\n", err)
} else {
fmt.Printf("ReadMessage msg:%v\n", msg)
}
}
}()
loginreq := &pb.UserLoginReq{
Name: "aaa",
}
logindata, _ := proto.Marshal(loginreq)
message := &pb.UserMessage{
MainType: "login",
SubType: "login",
Data: logindata,
}
data, _ := proto.Marshal(message)
err = ws.WriteMessage(websocket.BinaryMessage, data)
if err != nil {
fmt.Printf("err:%v\n", err)
}
sigterm := make(chan os.Signal, 1)
signal.Notify(sigterm, syscall.SIGINT, syscall.SIGTERM)
select {
case <-sigterm:
fmt.Printf("terminating: via signal\n")
}
}

View File

@ -64,6 +64,6 @@ func (this *Gateway) Connect(a IAgent) {
//有新的连接对象进入
func (this *Gateway) DisConnect(a IAgent) {
log.Debugf("[Module.Gateway] have disConnect:Ip[%s] SessionId:[%s] uid:[%d]", a.IP(), a.SessionId(), a.UserId())
log.Debugf("[Module.Gateway] have disConnect:Ip[%s] SessionId:[%s] uid:[%s]", a.IP(), a.SessionId(), a.UserId())
this.agentmgr_comp.DisConnect(a)
}

View File

@ -2,15 +2,18 @@ package user
import (
"context"
"encoding/base64"
"go_dreamfactory/comm"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
"go_dreamfactory/sys/db"
"go_dreamfactory/utils"
"time"
"github.com/liwei1dao/lego/sys/event"
"github.com/liwei1dao/lego/sys/log"
"github.com/tidwall/gjson"
"go.mongodb.org/mongo-driver/mongo"
)
@ -18,53 +21,76 @@ type LoginComp struct {
modules.MComp_GateComp
}
func DecodeUserData(base64Str string) *pb.DB_UserData {
//解码
dec, err := base64.StdEncoding.DecodeString(base64Str[35:])
if err != nil {
log.Errorf("base64 decode err %v", err)
return nil
}
now := time.Now().Unix()
jsonRet := gjson.Parse(string(dec))
serverId := jsonRet.Get("serverId").Int()
timestamp := jsonRet.Get("timestamp").Int()
if now-time.Unix(timestamp, 0).Unix() > 100 {
return nil
}
account := jsonRet.Get("account").String()
return &pb.DB_UserData{
ServerId: int32(serverId),
Account: account,
}
}
//登录
func (this *LoginComp) Login(ctx context.Context, session comm.IUserSession, req *pb.UserLoginReq) error {
log.Debugf("User - Login: session:%v rsp:%v", session.ToString(), req)
var code pb.ErrorCode = pb.ErrorCode_Success
if !utils.ValidSecretKey(req.Sec) {
session.SendMsg("user", "login", pb.ErrorCode_SecKeyInvalid, nil)
return nil
}
db_user, err := db.Defsys.User_FindUserByAccount(req.Name)
user := DecodeUserData(req.Sec)
db_user, err := db.Defsys.User_FindUserByAccount(user)
if err != nil {
if err != mongo.ErrNoDocuments {
return err
}
}
if db_user.UserId == "" {
db_user.Account = req.Name
err = db.Defsys.User_CreateUser(db_user)
//如果是新玩家,创建一条基础的数据,页面会引导进入创角页面
if db_user == nil {
err = db.Defsys.User_CreateUser(user)
if err != nil {
log.Errorf("User_CreateUser err %v", err)
return err
}
}
session.Build(db_user.UserId)
if data, ret := configure.GetConfigure("tbitem.json"); ret == nil {
_data := data.(*cfg.TbItem).GetDataMap()
for _, v := range _data {
if v.Id > 0 {
break
}
log.Debugf(v.Name)
}
}
session.Build(user.UserId)
cache_user := &pb.Cache_UserData{
SessionId: session.GetSessionId(),
GatewayServiceId: session.GetGatewayServiceId(),
UserData: db_user,
}
err = cache.Defsys.User_UpdateUser(cache_user)
err = cache.Defsys.Update(cache_user)
if err != nil {
log.Errorf("update cache err:%v", err)
return err
}
session.SendMsg("login", "login", pb.ErrorCode_Success, &pb.UserLoginResp{
err = session.SendMsg("user", "login", code, &pb.UserLoginResp{
Data: &pb.Cache_UserData{
UserData: &pb.DB_UserData{
UserId: db_user.UserId,
},
UserData: db_user,
},
})
if err != nil {
return err
}
event.TriggerEvent(comm.Event_UserLogin, db_user.UserId)
return nil
}

View File

@ -15,13 +15,21 @@ func NewModule() core.IModule {
type User struct {
modules.ModuleBase
login_comp *LoginComp
user_comp *UserComp
}
func (this *User) GetType() core.M_Modules {
return comm.SM_LoginModule
return comm.SM_UserModule
}
func (this *User) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
return
}
func (this *User) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.login_comp = this.RegisterComp(new(LoginComp)).(*LoginComp)
this.user_comp = this.RegisterComp(new(UserComp)).(*UserComp)
}

View File

@ -5,13 +5,23 @@ import (
"go_dreamfactory/comm"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"github.com/liwei1dao/lego/core"
)
type UserComp struct {
modules.MComp_GateComp
module *User
}
func (this *UserComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.MComp_GateComp.Init(service, module, comp, options)
this.module = module.(*User)
return
}
//创角
func (this *UserComp) CreateUser(ctx context.Context, session comm.IUserSession, req *pb.UserLoginReq) error {
func (this *UserComp) Create(ctx context.Context, session comm.IUserSession, req *pb.UserCreateReq) error {
return nil
}

View File

@ -33,6 +33,8 @@ const (
ErrorCode_InsufficientPermissions ErrorCode = 16 //权限不足
ErrorCode_NoLogin ErrorCode = 17 //未登录
ErrorCode_UserSessionNobeing ErrorCode = 18 //用户不存在
ErrorCode_SecKey ErrorCode = 19 //秘钥格式错误
ErrorCode_SecKeyInvalid ErrorCode = 20 //秘钥无效
)
// Enum value maps for ErrorCode.
@ -48,6 +50,8 @@ var (
16: "InsufficientPermissions",
17: "NoLogin",
18: "UserSessionNobeing",
19: "SecKey",
20: "SecKeyInvalid",
}
ErrorCode_value = map[string]int32{
"Success": 0,
@ -60,6 +64,8 @@ var (
"InsufficientPermissions": 16,
"NoLogin": 17,
"UserSessionNobeing": 18,
"SecKey": 19,
"SecKeyInvalid": 20,
}
)
@ -94,7 +100,7 @@ var File_errorcode_proto protoreflect.FileDescriptor
var file_errorcode_proto_rawDesc = []byte{
0x0a, 0x0f, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x63, 0x6f, 0x64, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x2a, 0xd9, 0x01, 0x0a, 0x09, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x43, 0x6f, 0x64, 0x65, 0x12,
0x6f, 0x2a, 0xf8, 0x01, 0x0a, 0x09, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x43, 0x6f, 0x64, 0x65, 0x12,
0x0b, 0x0a, 0x07, 0x53, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x10, 0x00, 0x12, 0x11, 0x0a, 0x0d,
0x4e, 0x6f, 0x46, 0x69, 0x6e, 0x64, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x10, 0x0a, 0x12,
0x19, 0x0a, 0x15, 0x52, 0x70, 0x63, 0x46, 0x75, 0x6e, 0x63, 0x45, 0x78, 0x65, 0x63, 0x75, 0x74,
@ -107,8 +113,10 @@ var file_errorcode_proto_rawDesc = []byte{
0x6e, 0x73, 0x75, 0x66, 0x66, 0x69, 0x63, 0x69, 0x65, 0x6e, 0x74, 0x50, 0x65, 0x72, 0x6d, 0x69,
0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x10, 0x10, 0x12, 0x0b, 0x0a, 0x07, 0x4e, 0x6f, 0x4c, 0x6f,
0x67, 0x69, 0x6e, 0x10, 0x11, 0x12, 0x16, 0x0a, 0x12, 0x55, 0x73, 0x65, 0x72, 0x53, 0x65, 0x73,
0x73, 0x69, 0x6f, 0x6e, 0x4e, 0x6f, 0x62, 0x65, 0x69, 0x6e, 0x67, 0x10, 0x12, 0x42, 0x06, 0x5a,
0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x73, 0x69, 0x6f, 0x6e, 0x4e, 0x6f, 0x62, 0x65, 0x69, 0x6e, 0x67, 0x10, 0x12, 0x12, 0x0a, 0x0a,
0x06, 0x53, 0x65, 0x63, 0x4b, 0x65, 0x79, 0x10, 0x13, 0x12, 0x11, 0x0a, 0x0d, 0x53, 0x65, 0x63,
0x4b, 0x65, 0x79, 0x49, 0x6e, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x10, 0x14, 0x42, 0x06, 0x5a, 0x04,
0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (

View File

@ -13,4 +13,6 @@ enum ErrorCode {
InsufficientPermissions = 16; //
NoLogin = 17; //
UserSessionNobeing = 18; //
SecKey = 19; //
SecKeyInvalid = 20; //
}

View File

@ -11,6 +11,5 @@ message DB_UserData {
string UserId = 1; //tags:{bson:"_id"}Id
string Account = 2;
string NiceName = 3;
string Email = 4;
string Password = 5;
int32 ServerId = 4;
}

View File

@ -3,13 +3,13 @@ option go_package = ".;pb";
import "errorcode.proto";
import "user_db.proto";
//
message UserLoginReq {
string Name = 1;
string sec= 1;//
}
message UserLoginResp {
ErrorCode Code = 1;
Cache_UserData data = 2;
Cache_UserData data = 1;
}
@ -25,11 +25,11 @@ message UserLoadRsp {
Cache_UserData data = 1;
}
//
message UserCreateReq{
string NickName = 1;//
int32 gender = 2; //
}
message UserCreateRsp{
string NickName = 1;
}

View File

@ -20,12 +20,13 @@ const (
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
//用户登录
type UserLoginReq struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Name string `protobuf:"bytes,1,opt,name=Name,proto3" json:"Name,omitempty"`
Sec string `protobuf:"bytes,1,opt,name=sec,proto3" json:"sec,omitempty"` //密文
}
func (x *UserLoginReq) Reset() {
@ -60,9 +61,9 @@ func (*UserLoginReq) Descriptor() ([]byte, []int) {
return file_user_msg_proto_rawDescGZIP(), []int{0}
}
func (x *UserLoginReq) GetName() string {
func (x *UserLoginReq) GetSec() string {
if x != nil {
return x.Name
return x.Sec
}
return ""
}
@ -72,8 +73,7 @@ type UserLoginResp struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Code ErrorCode `protobuf:"varint,1,opt,name=Code,proto3,enum=ErrorCode" json:"Code,omitempty"`
Data *Cache_UserData `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
Data *Cache_UserData `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"`
}
func (x *UserLoginResp) Reset() {
@ -108,13 +108,6 @@ func (*UserLoginResp) Descriptor() ([]byte, []int) {
return file_user_msg_proto_rawDescGZIP(), []int{1}
}
func (x *UserLoginResp) GetCode() ErrorCode {
if x != nil {
return x.Code
}
return ErrorCode_Success
}
func (x *UserLoginResp) GetData() *Cache_UserData {
if x != nil {
return x.Data
@ -263,13 +256,13 @@ func (x *UserLoadRsp) GetData() *Cache_UserData {
return nil
}
//创角
type UserCreateReq struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
NickName string `protobuf:"bytes,1,opt,name=NickName,proto3" json:"NickName,omitempty"` //昵称
Gender int32 `protobuf:"varint,2,opt,name=gender,proto3" json:"gender,omitempty"` //性别
}
func (x *UserCreateReq) Reset() {
@ -311,17 +304,12 @@ func (x *UserCreateReq) GetNickName() string {
return ""
}
func (x *UserCreateReq) GetGender() int32 {
if x != nil {
return x.Gender
}
return 0
}
type UserCreateRsp struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
NickName string `protobuf:"bytes,1,opt,name=NickName,proto3" json:"NickName,omitempty"`
}
func (x *UserCreateRsp) Reset() {
@ -356,36 +344,41 @@ func (*UserCreateRsp) Descriptor() ([]byte, []int) {
return file_user_msg_proto_rawDescGZIP(), []int{6}
}
func (x *UserCreateRsp) GetNickName() string {
if x != nil {
return x.NickName
}
return ""
}
var File_user_msg_proto protoreflect.FileDescriptor
var file_user_msg_proto_rawDesc = []byte{
0x0a, 0x0e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x73, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x1a, 0x0f, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x63, 0x6f, 0x64, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x1a, 0x0d, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x64, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x22, 0x22, 0x0a, 0x0c, 0x55, 0x73, 0x65, 0x72, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71,
0x12, 0x12, 0x0a, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04,
0x4e, 0x61, 0x6d, 0x65, 0x22, 0x54, 0x0a, 0x0d, 0x55, 0x73, 0x65, 0x72, 0x4c, 0x6f, 0x67, 0x69,
0x6e, 0x52, 0x65, 0x73, 0x70, 0x12, 0x1e, 0x0a, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20,
0x01, 0x28, 0x0e, 0x32, 0x0a, 0x2e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x43, 0x6f, 0x64, 0x65, 0x52,
0x04, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x23, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x02, 0x20,
0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x43, 0x61, 0x63, 0x68, 0x65, 0x5f, 0x55, 0x73, 0x65, 0x72,
0x44, 0x61, 0x74, 0x61, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x2b, 0x0a, 0x0f, 0x55, 0x73,
0x65, 0x72, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x12, 0x18, 0x0a,
0x07, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07,
0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x31, 0x0a, 0x0f, 0x55, 0x73, 0x65, 0x72, 0x52,
0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x73, 0x70, 0x12, 0x1e, 0x0a, 0x04, 0x43, 0x6f,
0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0a, 0x2e, 0x45, 0x72, 0x72, 0x6f, 0x72,
0x43, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x22, 0x32, 0x0a, 0x0b, 0x55, 0x73,
0x65, 0x72, 0x4c, 0x6f, 0x61, 0x64, 0x52, 0x73, 0x70, 0x12, 0x23, 0x0a, 0x04, 0x64, 0x61, 0x74,
0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x43, 0x61, 0x63, 0x68, 0x65, 0x5f,
0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x43,
0x0a, 0x0d, 0x55, 0x73, 0x65, 0x72, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x12,
0x1a, 0x0a, 0x08, 0x4e, 0x69, 0x63, 0x6b, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x08, 0x4e, 0x69, 0x63, 0x6b, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x67,
0x65, 0x6e, 0x64, 0x65, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x67, 0x65, 0x6e,
0x64, 0x65, 0x72, 0x22, 0x0f, 0x0a, 0x0d, 0x55, 0x73, 0x65, 0x72, 0x43, 0x72, 0x65, 0x61, 0x74,
0x65, 0x52, 0x73, 0x70, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x33,
0x22, 0x20, 0x0a, 0x0c, 0x55, 0x73, 0x65, 0x72, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71,
0x12, 0x10, 0x0a, 0x03, 0x73, 0x65, 0x63, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x73,
0x65, 0x63, 0x22, 0x34, 0x0a, 0x0d, 0x55, 0x73, 0x65, 0x72, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52,
0x65, 0x73, 0x70, 0x12, 0x23, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28,
0x0b, 0x32, 0x0f, 0x2e, 0x43, 0x61, 0x63, 0x68, 0x65, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61,
0x74, 0x61, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x2b, 0x0a, 0x0f, 0x55, 0x73, 0x65, 0x72,
0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x12, 0x18, 0x0a, 0x07, 0x61,
0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x61, 0x63,
0x63, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x31, 0x0a, 0x0f, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x67,
0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x73, 0x70, 0x12, 0x1e, 0x0a, 0x04, 0x43, 0x6f, 0x64, 0x65,
0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0a, 0x2e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x43, 0x6f,
0x64, 0x65, 0x52, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x22, 0x32, 0x0a, 0x0b, 0x55, 0x73, 0x65, 0x72,
0x4c, 0x6f, 0x61, 0x64, 0x52, 0x73, 0x70, 0x12, 0x23, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18,
0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x43, 0x61, 0x63, 0x68, 0x65, 0x5f, 0x55, 0x73,
0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x2b, 0x0a, 0x0d,
0x55, 0x73, 0x65, 0x72, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x12, 0x1a, 0x0a,
0x08, 0x4e, 0x69, 0x63, 0x6b, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
0x08, 0x4e, 0x69, 0x63, 0x6b, 0x4e, 0x61, 0x6d, 0x65, 0x22, 0x2b, 0x0a, 0x0d, 0x55, 0x73, 0x65,
0x72, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x73, 0x70, 0x12, 0x1a, 0x0a, 0x08, 0x4e, 0x69,
0x63, 0x6b, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x4e, 0x69,
0x63, 0x6b, 0x4e, 0x61, 0x6d, 0x65, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@ -409,19 +402,18 @@ var file_user_msg_proto_goTypes = []interface{}{
(*UserLoadRsp)(nil), // 4: UserLoadRsp
(*UserCreateReq)(nil), // 5: UserCreateReq
(*UserCreateRsp)(nil), // 6: UserCreateRsp
(ErrorCode)(0), // 7: ErrorCode
(*Cache_UserData)(nil), // 8: Cache_UserData
(*Cache_UserData)(nil), // 7: Cache_UserData
(ErrorCode)(0), // 8: ErrorCode
}
var file_user_msg_proto_depIdxs = []int32{
7, // 0: UserLoginResp.Code:type_name -> ErrorCode
8, // 1: UserLoginResp.data:type_name -> Cache_UserData
7, // 2: UserRegisterRsp.Code:type_name -> ErrorCode
8, // 3: UserLoadRsp.data:type_name -> Cache_UserData
4, // [4:4] is the sub-list for method output_type
4, // [4:4] is the sub-list for method input_type
4, // [4:4] is the sub-list for extension type_name
4, // [4:4] is the sub-list for extension extendee
0, // [0:4] is the sub-list for field type_name
7, // 0: UserLoginResp.data:type_name -> Cache_UserData
8, // 1: UserRegisterRsp.Code:type_name -> ErrorCode
7, // 2: UserLoadRsp.data:type_name -> Cache_UserData
3, // [3:3] is the sub-list for method output_type
3, // [3:3] is the sub-list for method input_type
3, // [3:3] is the sub-list for extension type_name
3, // [3:3] is the sub-list for extension extendee
0, // [0:3] is the sub-list for field type_name
}
func init() { file_user_msg_proto_init() }

17
sys/cache/user.go vendored
View File

@ -3,6 +3,8 @@ package cache
import (
"fmt"
"go_dreamfactory/pb"
"github.com/liwei1dao/lego/sys/log"
)
const ( //Redis
@ -10,10 +12,21 @@ const ( //Redis
)
type IUser interface {
User_UpdateUser(data *pb.Cache_UserData) (err error)
Update(data *pb.Cache_UserData) (err error)
Get(userId string) *pb.Cache_UserData
}
func (this *Cache) User_UpdateUser(data *pb.Cache_UserData) (err error) {
func (this *Cache) Update(data *pb.Cache_UserData) (err error) {
err = this.redis.Set(fmt.Sprintf(Redis_UserCache, data.UserData.UserId), data, -1)
return
}
func (this *Cache) Get(userId string) *pb.Cache_UserData {
var user *pb.Cache_UserData
err := this.redis.Get(fmt.Sprintf(Redis_UserCache, userId), &user)
if err != nil {
log.Errorf("get user cache err:%v", err)
return nil
}
return user
}

View File

@ -1,6 +1,7 @@
package cache
import (
"fmt"
"go_dreamfactory/pb"
"log"
"testing"
@ -33,6 +34,11 @@ func TestUpdateUser(t *testing.T) {
Account: "aaa",
},
}
err := cache.User_UpdateUser(user)
err := cache.Update(user)
require.Nil(t, err)
}
func TestGetUser(t *testing.T) {
c := cache.Get("62157")
fmt.Println(c)
}

View File

@ -0,0 +1,79 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
package cfg
import "errors"
type MyBeanHero struct {
Id string
Name string
Star int32
Color int32
Chengwei int32
Zhongzu int32
Job int32
Type int32
Prefab string
Icon string
Sound string
Tujing string
Hpgrow string
Atkgrow string
Defgrow string
Speedgrow string
Hp string
Atk string
Def string
Speed string
Intr string
Events string
Cite string
}
const TypeId_MyBeanHero = 1050121004
func (*MyBeanHero) GetTypeId() int32 {
return 1050121004
}
func (_v *MyBeanHero)Deserialize(_buf map[string]interface{}) (err error) {
{ var _ok_ bool; if _v.Id, _ok_ = _buf["id"].(string); !_ok_ { err = errors.New("id error"); return } }
{ var _ok_ bool; if _v.Name, _ok_ = _buf["name"].(string); !_ok_ { err = errors.New("name error"); return } }
{ var _ok_ bool; var _tempNum_ float64; if _tempNum_, _ok_ = _buf["star"].(float64); !_ok_ { err = errors.New("star error"); return }; _v.Star = int32(_tempNum_) }
{ var _ok_ bool; var _tempNum_ float64; if _tempNum_, _ok_ = _buf["color"].(float64); !_ok_ { err = errors.New("color error"); return }; _v.Color = int32(_tempNum_) }
{ var _ok_ bool; var _tempNum_ float64; if _tempNum_, _ok_ = _buf["chengwei"].(float64); !_ok_ { err = errors.New("chengwei error"); return }; _v.Chengwei = int32(_tempNum_) }
{ var _ok_ bool; var _tempNum_ float64; if _tempNum_, _ok_ = _buf["zhongzu"].(float64); !_ok_ { err = errors.New("zhongzu error"); return }; _v.Zhongzu = int32(_tempNum_) }
{ var _ok_ bool; var _tempNum_ float64; if _tempNum_, _ok_ = _buf["job"].(float64); !_ok_ { err = errors.New("job error"); return }; _v.Job = int32(_tempNum_) }
{ var _ok_ bool; var _tempNum_ float64; if _tempNum_, _ok_ = _buf["type"].(float64); !_ok_ { err = errors.New("type error"); return }; _v.Type = int32(_tempNum_) }
{ var _ok_ bool; if _v.Prefab, _ok_ = _buf["prefab"].(string); !_ok_ { err = errors.New("prefab error"); return } }
{ var _ok_ bool; if _v.Icon, _ok_ = _buf["icon"].(string); !_ok_ { err = errors.New("icon error"); return } }
{ var _ok_ bool; if _v.Sound, _ok_ = _buf["sound"].(string); !_ok_ { err = errors.New("sound error"); return } }
{ var _ok_ bool; if _v.Tujing, _ok_ = _buf["tujing"].(string); !_ok_ { err = errors.New("tujing error"); return } }
{ var _ok_ bool; if _v.Hpgrow, _ok_ = _buf["hpgrow"].(string); !_ok_ { err = errors.New("hpgrow error"); return } }
{ var _ok_ bool; if _v.Atkgrow, _ok_ = _buf["atkgrow"].(string); !_ok_ { err = errors.New("atkgrow error"); return } }
{ var _ok_ bool; if _v.Defgrow, _ok_ = _buf["defgrow"].(string); !_ok_ { err = errors.New("defgrow error"); return } }
{ var _ok_ bool; if _v.Speedgrow, _ok_ = _buf["speedgrow"].(string); !_ok_ { err = errors.New("speedgrow error"); return } }
{ var _ok_ bool; if _v.Hp, _ok_ = _buf["hp"].(string); !_ok_ { err = errors.New("hp error"); return } }
{ var _ok_ bool; if _v.Atk, _ok_ = _buf["atk"].(string); !_ok_ { err = errors.New("atk error"); return } }
{ var _ok_ bool; if _v.Def, _ok_ = _buf["def"].(string); !_ok_ { err = errors.New("def error"); return } }
{ var _ok_ bool; if _v.Speed, _ok_ = _buf["speed"].(string); !_ok_ { err = errors.New("speed error"); return } }
{ var _ok_ bool; if _v.Intr, _ok_ = _buf["intr"].(string); !_ok_ { err = errors.New("intr error"); return } }
{ var _ok_ bool; if _v.Events, _ok_ = _buf["events"].(string); !_ok_ { err = errors.New("events error"); return } }
{ var _ok_ bool; if _v.Cite, _ok_ = _buf["cite"].(string); !_ok_ { err = errors.New("cite error"); return } }
return
}
func DeserializeMyBeanHero(_buf map[string]interface{}) (*MyBeanHero, error) {
v := &MyBeanHero{}
if err := v.Deserialize(_buf); err == nil {
return v, nil
} else {
return nil, err
}
}

View File

@ -13,6 +13,7 @@ type JsonLoader func(string) ([]map[string]interface{}, error)
type Tables struct {
TbRewards *TbRewards
TbItem *TbItem
TbHero *TbHero
}
func NewTables(loader JsonLoader) (*Tables, error) {
@ -32,5 +33,11 @@ func NewTables(loader JsonLoader) (*Tables, error) {
if tables.TbItem, err = NewTbItem(buf) ; err != nil {
return nil, err
}
if buf, err = loader("tbhero") ; err != nil {
return nil, err
}
if tables.TbHero, err = NewTbHero(buf) ; err != nil {
return nil, err
}
return tables, nil
}

View File

@ -0,0 +1,42 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
package cfg
type TbHero struct {
_dataMap map[string]*MyBeanHero
_dataList []*MyBeanHero
}
func NewTbHero(_buf []map[string]interface{}) (*TbHero, error) {
_dataList := make([]*MyBeanHero, 0, len(_buf))
dataMap := make(map[string]*MyBeanHero)
for _, _ele_ := range _buf {
if _v, err2 := DeserializeMyBeanHero(_ele_); err2 != nil {
return nil, err2
} else {
_dataList = append(_dataList, _v)
dataMap[_v.Id] = _v
}
}
return &TbHero{_dataList:_dataList, _dataMap:dataMap}, nil
}
func (table *TbHero) GetDataMap() map[string]*MyBeanHero {
return table._dataMap
}
func (table *TbHero) GetDataList() []*MyBeanHero {
return table._dataList
}
func (table *TbHero) Get(key string) *MyBeanHero {
return table._dataMap[key]
}

View File

@ -14,20 +14,21 @@ const ( //Redis
)
type IUser interface {
User_FindUserByAccount(account string) (*pb.DB_UserData, error)
User_FindUserByAccount(user *pb.DB_UserData) (*pb.DB_UserData, error)
User_FindUserById(id string) (*pb.DB_UserData, error)
User_CreateUser(user *pb.DB_UserData) error
User_UpdateUser(data *pb.DB_UserData) (err error)
}
func (this *DB) User_FindUserByAccount(account string) (*pb.DB_UserData, error) {
func (this *DB) User_FindUserByAccount(user *pb.DB_UserData) (*pb.DB_UserData, error) {
filter := bson.D{
{"account", account},
{"serverid", user.ServerId},
{"account", user.Account},
}
sr := this.mgo.FindOne(DB_UserTable, filter)
user := &pb.DB_UserData{}
err := sr.Decode(user)
return user, err
var nd *pb.DB_UserData
err := sr.Decode(&nd)
return nd, err
}
func (this *DB) User_FindUserById(id string) (*pb.DB_UserData, error) {
@ -53,7 +54,6 @@ func (this *DB) User_UpdateUser(data *pb.DB_UserData) (err error) {
bson.M{"_id": data.UserId},
bson.M{"$set": bson.M{
"niceName": data.NiceName,
"email": data.Email,
}},
options.FindOneAndUpdate().SetUpsert(false).SetReturnDocument(options.After),
).Decode(data)

View File

@ -30,7 +30,7 @@ func TestCreate(t *testing.T) {
user := &pb.DB_UserData{
Account: "legu3",
NiceName: "乐谷3",
Email: "1111@legu.com",
ServerId: 1,
}
err := db.User_CreateUser(user)
@ -38,23 +38,23 @@ func TestCreate(t *testing.T) {
}
func TestFindOne(t *testing.T) {
user, err := db.User_FindUserById("")
user, err := db.User_FindUserById("629eb3f4132dc4bb26139659")
require.Nil(t, err)
assert.Equal(t, "legu1", user.Account)
assert.Equal(t, "legu3", user.Account)
user2, err := db.User_FindUserByAccount("legu1")
// user.ServerId = 2
user2, err := db.User_FindUserByAccount(user)
require.Nil(t, err)
assert.Equal(t, "legu1", user2.Account)
assert.Equal(t, "legu3", user2.Account)
assert.Equal(t, int32(1), user2.ServerId)
}
func TestUpdate(t *testing.T) {
user := &pb.DB_UserData{
UserId: primitive.NewObjectID().String(),
Email: "new@qq.com",
UserId: primitive.NewObjectID().Hex(),
}
err := db.User_UpdateUser(user)
require.Nil(t, err)
assert.Equal(t, "new@qq.com", user.Email)
assert.Equal(t, "NiceName", "")
}

37
utils/base64.go Normal file
View File

@ -0,0 +1,37 @@
package utils
import (
"encoding/base64"
"strings"
"github.com/liwei1dao/lego/sys/log"
)
func Base64Encode(data []byte) string {
return base64.StdEncoding.EncodeToString(data)
}
func Base64Decode(data string) string {
b, err := base64.StdEncoding.DecodeString(data)
if err != nil {
log.Errorf("base64 decode", err)
return ""
}
return string(b)
}
func ValidSecretKey(secStr string) bool {
if !strings.HasPrefix(secStr, "CE:") || len(secStr) < 35 {
return false
}
clientMd5Key := secStr[3:35]
rawmsg := secStr[35:]
log.Debugf("data base: %s", rawmsg)
serverMd5Key := MD5Str(rawmsg)
// s := fmt.Sprintf("%x", serverMd5Key)
if !strings.EqualFold(strings.ToLower(serverMd5Key), strings.ToLower(clientMd5Key)) {
return false
}
return true
}

17
utils/md5.go Normal file
View File

@ -0,0 +1,17 @@
package utils
import (
"crypto/md5"
"encoding/hex"
)
func MD5Str(s string) string {
return MD5Bytes([]byte(s))
}
func MD5Bytes(s []byte) string {
md5Ctx := md5.New()
md5Ctx.Write(s)
cipherStr := md5Ctx.Sum(nil)
return hex.EncodeToString(cipherStr)
}