Compare commits
2 Commits
1fda87e83d
...
1a4aa01d1e
Author | SHA1 | Date | |
---|---|---|---|
![]() |
1a4aa01d1e | ||
![]() |
8048801620 |
@ -63,6 +63,7 @@ func (r *Robot) Run() {
|
||||
|
||||
//模块处理
|
||||
func (r *Robot) handleMsg(msg *pb.UserMessage) {
|
||||
log.Printf("route: %s.%s", msg.MainType, msg.SubType)
|
||||
switch msg.MainType {
|
||||
case "user":
|
||||
r.handleUserMsg(msg)
|
||||
@ -73,6 +74,8 @@ func (r *Robot) handleMsg(msg *pb.UserMessage) {
|
||||
|
||||
//在这里添加玩家成功登录以后的测试方法
|
||||
func (r *Robot) onUserLoaded() {
|
||||
//user
|
||||
r.CreateUser("user671")
|
||||
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,8 @@ import (
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/pb"
|
||||
"log"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
func (r *Robot) handleUserMsg(msg *pb.UserMessage) {
|
||||
@ -33,10 +35,30 @@ func handleLogin(r *Robot, msg *pb.UserMessage) {
|
||||
}
|
||||
|
||||
func handleCreateUser(r *Robot, msg *pb.UserMessage) {
|
||||
|
||||
rsp := &pb.UserCreateRsp{}
|
||||
if !comm.ProtoDecode(msg, rsp) {
|
||||
return
|
||||
}
|
||||
log.Printf("创角返回: %v", rsp)
|
||||
}
|
||||
|
||||
//创角色
|
||||
func (r *Robot) CreateUser() {
|
||||
func (r *Robot) CreateUser(NickName string) {
|
||||
req := &pb.UserCreateReq{
|
||||
NickName: NickName,
|
||||
}
|
||||
|
||||
head := &pb.UserMessage{
|
||||
MainType: "user",
|
||||
SubType: "create",
|
||||
}
|
||||
|
||||
if comm.ProtoEncode(req, head) {
|
||||
data, _ := proto.Marshal(head)
|
||||
err := r.SendToClient(data)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -67,10 +67,11 @@ func (this *LoginComp) Login(ctx context.Context, session comm.IUserSession, req
|
||||
log.Errorf("User_CreateUser err %v", err)
|
||||
return err
|
||||
}
|
||||
session.Build(user.UserId)
|
||||
} else {
|
||||
session.Build(db_user.UserId)
|
||||
}
|
||||
|
||||
session.Build(user.UserId)
|
||||
|
||||
cache_user := &pb.Cache_UserData{
|
||||
SessionId: session.GetSessionId(),
|
||||
GatewayServiceId: session.GetGatewayServiceId(),
|
||||
|
@ -5,8 +5,11 @@ import (
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/modules"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/cache"
|
||||
"go_dreamfactory/utils"
|
||||
|
||||
"github.com/liwei1dao/lego/core"
|
||||
"github.com/liwei1dao/lego/sys/log"
|
||||
)
|
||||
|
||||
type UserComp struct {
|
||||
@ -22,6 +25,19 @@ func (this *UserComp) Init(service core.IService, module core.IModule, comp core
|
||||
|
||||
//创角
|
||||
func (this *UserComp) Create(ctx context.Context, session comm.IUserSession, req *pb.UserCreateReq) error {
|
||||
defer utils.TraceFunc(session.GetUserId(), "user", "create", req, nil)
|
||||
user := cache.Defsys.Get(session.GetUserId())
|
||||
if user == nil {
|
||||
log.Errorf("user no exist")
|
||||
session.SendMsg("user", "create", pb.ErrorCode_UserSessionNobeing, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
user.UserData.NiceName = req.NickName
|
||||
err := cache.Defsys.Update(user)
|
||||
if err != nil {
|
||||
log.Errorf("cache update err")
|
||||
}
|
||||
session.SendMsg("user", "create", pb.ErrorCode_Success, &pb.UserCreateRsp{})
|
||||
return nil
|
||||
}
|
||||
|
@ -139,7 +139,7 @@ type DB_UserPackData struct {
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
UserId string `bson:"_id" json:"UserId,omitempty" protobuf:"bytes,1,opt,name=UserId,proto3"` //tags:{bson:"_id"}用户Id
|
||||
UserId string `protobuf:"bytes,1,opt,name=UserId,proto3" json:"UserId,omitempty" bson:"_id"` //tags:{bson:"_id"}用户Id
|
||||
Pack []*GridData `protobuf:"bytes,2,rep,name=Pack,proto3" json:"Pack,omitempty"` //背包列表
|
||||
}
|
||||
|
||||
|
@ -12,4 +12,5 @@ message DB_UserData {
|
||||
string Account = 2;
|
||||
string NiceName = 3;
|
||||
int32 ServerId = 4;
|
||||
int32 FriendPoint = 5;//友情点
|
||||
}
|
@ -31,5 +31,5 @@ message UserCreateReq{
|
||||
}
|
||||
|
||||
message UserCreateRsp{
|
||||
string NickName = 1;
|
||||
|
||||
}
|
@ -88,11 +88,11 @@ type DB_UserData struct {
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
UserId string `bson:"_id" json:"UserId,omitempty" protobuf:"bytes,1,opt,name=UserId,proto3"` //tags:{bson:"_id"}动态Id
|
||||
Account string `protobuf:"bytes,2,opt,name=Account,proto3" json:"Account,omitempty"`
|
||||
NiceName string `protobuf:"bytes,3,opt,name=NiceName,proto3" json:"NiceName,omitempty"`
|
||||
Email string `protobuf:"bytes,4,opt,name=Email,proto3" json:"Email,omitempty"`
|
||||
Password string `protobuf:"bytes,5,opt,name=Password,proto3" json:"Password,omitempty"`
|
||||
UserId string `protobuf:"bytes,1,opt,name=UserId,proto3" json:"UserId,omitempty" bson:"_id"` //tags:{bson:"_id"}动态Id
|
||||
Account string `protobuf:"bytes,2,opt,name=Account,proto3" json:"Account,omitempty"`
|
||||
NiceName string `protobuf:"bytes,3,opt,name=NiceName,proto3" json:"NiceName,omitempty"`
|
||||
ServerId int32 `protobuf:"varint,4,opt,name=ServerId,proto3" json:"ServerId,omitempty"`
|
||||
FriendPoint int32 `protobuf:"varint,5,opt,name=FriendPoint,proto3" json:"FriendPoint,omitempty"` //友情点
|
||||
}
|
||||
|
||||
func (x *DB_UserData) Reset() {
|
||||
@ -148,18 +148,18 @@ func (x *DB_UserData) GetNiceName() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *DB_UserData) GetEmail() string {
|
||||
func (x *DB_UserData) GetServerId() int32 {
|
||||
if x != nil {
|
||||
return x.Email
|
||||
return x.ServerId
|
||||
}
|
||||
return ""
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *DB_UserData) GetPassword() string {
|
||||
func (x *DB_UserData) GetFriendPoint() int32 {
|
||||
if x != nil {
|
||||
return x.Password
|
||||
return x.FriendPoint
|
||||
}
|
||||
return ""
|
||||
return 0
|
||||
}
|
||||
|
||||
var File_user_db_proto protoreflect.FileDescriptor
|
||||
@ -174,17 +174,18 @@ var file_user_db_proto_rawDesc = []byte{
|
||||
0x77, 0x61, 0x79, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x49, 0x64, 0x12, 0x28, 0x0a, 0x08,
|
||||
0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c,
|
||||
0x2e, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x52, 0x08, 0x55, 0x73,
|
||||
0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x22, 0x8d, 0x01, 0x0a, 0x0b, 0x44, 0x42, 0x5f, 0x55, 0x73,
|
||||
0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x22, 0x99, 0x01, 0x0a, 0x0b, 0x44, 0x42, 0x5f, 0x55, 0x73,
|
||||
0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64,
|
||||
0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x18,
|
||||
0x0a, 0x07, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,
|
||||
0x07, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x4e, 0x69, 0x63, 0x65,
|
||||
0x4e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x4e, 0x69, 0x63, 0x65,
|
||||
0x4e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x18, 0x04, 0x20,
|
||||
0x01, 0x28, 0x09, 0x52, 0x05, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x12, 0x1a, 0x0a, 0x08, 0x50, 0x61,
|
||||
0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x50, 0x61,
|
||||
0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06,
|
||||
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
0x4e, 0x61, 0x6d, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x49, 0x64,
|
||||
0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x49, 0x64,
|
||||
0x12, 0x20, 0x0a, 0x0b, 0x46, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x50, 0x6f, 0x69, 0x6e, 0x74, 0x18,
|
||||
0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0b, 0x46, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x50, 0x6f, 0x69,
|
||||
0x6e, 0x74, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74,
|
||||
0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -308,8 +308,6 @@ 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() {
|
||||
@ -344,13 +342,6 @@ 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{
|
||||
@ -374,11 +365,9 @@ var file_user_msg_proto_rawDesc = []byte{
|
||||
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,
|
||||
0x08, 0x4e, 0x69, 0x63, 0x6b, 0x4e, 0x61, 0x6d, 0x65, 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,
|
||||
}
|
||||
|
||||
var (
|
||||
|
47
utils/trace.go
Normal file
47
utils/trace.go
Normal file
@ -0,0 +1,47 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/liwei1dao/lego/sys/log"
|
||||
)
|
||||
|
||||
const (
|
||||
UID = "uid"
|
||||
Module = "module"
|
||||
FuncName = "funcName"
|
||||
FuncArgs = "funcArgs"
|
||||
FuncRsp = "response"
|
||||
TimeCost = "timeCost"
|
||||
)
|
||||
|
||||
//打印函数处理信息
|
||||
func TraceFunc(uid string, module string, funcName string, funcArgs interface{}, rsp interface{}) {
|
||||
log.Debugf("traceFunc uid:%s module:%s funcName:%s funcArgs:%v", uid, module, funcName, funcArgs)
|
||||
}
|
||||
|
||||
//打印函数处理时间
|
||||
func TraceTimeCost(funcName string, invocation time.Time) {
|
||||
elapsed := time.Since(invocation)
|
||||
cost := elapsed.Milliseconds()
|
||||
lg := fmt.Sprintf("funcName:%s timeCost:%v", funcName, cost)
|
||||
|
||||
if cost < 20 {
|
||||
log.Debugf(lg)
|
||||
return
|
||||
}
|
||||
log.Warnf(lg)
|
||||
}
|
||||
|
||||
//打印异常
|
||||
func TraceError(err error) (b bool) {
|
||||
if err != nil {
|
||||
pc, fn, line, _ := runtime.Caller(1)
|
||||
|
||||
log.Errorf("[error] in %s[%s:%d] %v", runtime.FuncForPC(pc).Name(), fn, line, err)
|
||||
b = true
|
||||
}
|
||||
return
|
||||
}
|
Loading…
Reference in New Issue
Block a user