roleaction.go
1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package actions
import (
"context"
"errors"
"fmt"
"google.golang.org/grpc/metadata"
"pro2d/models"
"pro2d/protos/pb"
"pro2d/utils"
)
func (s *GameServer) HeartBeatHandler(ctx context.Context, in *pb.Token) (*pb.PubRsp, error) {
utils.Sugar.Debugf("HeartBeatHandler被调用!!!")
//获取元数据信息
md,ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil,errors.New("未传输token")
}
var (
appId string
appKey string
)
if val, ok := md["appId"]; ok {
appId = val[0]
}
if val, ok := md["appKey"]; ok {
appKey = val[0]
}
//进行校验的信息是否正确
if appId != "123" || appKey != "456" {
return nil, errors.New("token传输不正确")
}
return &pb.PubRsp{
Code: 0,
Msg: "heart beat successful",
}, nil
}
func (s *GameServer) CreateRoleHandler(ctx context.Context, in *pb.Token) (*pb.RoleRsp, error) {
account := utils.ParseToken(in.Token)
if account == nil {
return nil, fmt.Errorf("1")
}
ok, role := models.RoleExistByUid(account.Uid)
if !ok {
*role.Role = pb.Role{
Level: 0,
Device: account.Device,
Uid: account.Uid,
}
role.Create()
}
return &pb.RoleRsp{
Rsp: &pb.PubRsp{
Code: 0,
Msg: "successful",
},
Role: role.Role,
}, nil
}
func (s *GameServer) LoginHandler(ctx context.Context, in *pb.Token) (*pb.RoleRsp, error) {
account := utils.ParseToken(in.Token)
if account == nil {
return nil, fmt.Errorf("token is error")
}
ok, role := models.RoleExistByUid(account.Uid)
if !ok {
return &pb.RoleRsp{
Rsp: &pb.PubRsp{
Code: 1,
Msg: "role not exist",
},
}, nil
}
return &pb.RoleRsp{
Rsp: &pb.PubRsp{
Code: 0,
Msg: "successful",
},
Role: role.Role,
Hero: models.GetHeros(role.Heros),
}, nil
}