diff --git a/cmd/robot/robot.go b/cmd/robot/robot.go index 785194e23..a1977c903 100644 --- a/cmd/robot/robot.go +++ b/cmd/robot/robot.go @@ -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") } diff --git a/cmd/robot/user.go b/cmd/robot/user.go index b204d9268..0d9da8e52 100644 --- a/cmd/robot/user.go +++ b/cmd/robot/user.go @@ -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) + } + } } diff --git a/modules/user/login_comp.go b/modules/user/login_comp.go index 85106b8ba..c0d74080a 100644 --- a/modules/user/login_comp.go +++ b/modules/user/login_comp.go @@ -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(), diff --git a/modules/user/user_comp.go b/modules/user/user_comp.go index 8d5c3126c..90b9c9d2a 100644 --- a/modules/user/user_comp.go +++ b/modules/user/user_comp.go @@ -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 } diff --git a/pb/pack_db.pb.go b/pb/pack_db.pb.go index 0dc1074bd..6dd9577bf 100644 --- a/pb/pack_db.pb.go +++ b/pb/pack_db.pb.go @@ -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"` //背包列表 } diff --git a/pb/proto/user_db.proto b/pb/proto/user_db.proto index 46a2b4fb0..95ce91c15 100644 --- a/pb/proto/user_db.proto +++ b/pb/proto/user_db.proto @@ -12,4 +12,5 @@ message DB_UserData { string Account = 2; string NiceName = 3; int32 ServerId = 4; + int32 FriendPoint = 5;//友情点 } \ No newline at end of file diff --git a/pb/proto/user_msg.proto b/pb/proto/user_msg.proto index 88f64d141..3458babd1 100644 --- a/pb/proto/user_msg.proto +++ b/pb/proto/user_msg.proto @@ -31,5 +31,5 @@ message UserCreateReq{ } message UserCreateRsp{ - string NickName = 1; + } \ No newline at end of file diff --git a/pb/user_db.pb.go b/pb/user_db.pb.go index b4c44f448..44e8928d7 100644 --- a/pb/user_db.pb.go +++ b/pb/user_db.pb.go @@ -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 ( diff --git a/pb/user_msg.pb.go b/pb/user_msg.pb.go index 83f6141cc..58f347003 100644 --- a/pb/user_msg.pb.go +++ b/pb/user_msg.pb.go @@ -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 ( diff --git a/utils/trace.go b/utils/trace.go new file mode 100644 index 000000000..ccea0c2af --- /dev/null +++ b/utils/trace.go @@ -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 +}