RoleAction.go
1.19 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
package actions
import (
"context"
"errors"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/types/known/emptypb"
"pro2d/conf"
"pro2d/models"
"pro2d/protos/pb"
"pro2d/utils"
)
func (s *GameServer) HeartBeatHandler(ctx context.Context, empty *emptypb.Empty) (*pb.HeartRsp, error) {
utils.Sugar.Debugf("HeartBeatHandler被调用!!!")
//获取元数据信息
_,ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil,errors.New("未传输token")
}
return &pb.HeartRsp{
Code: 0,
}, nil
}
func (s *GameServer) CreateRoleHandler(ctx context.Context, in *pb.LoginReq) (*pb.RoleRsp, error) {
uid := ctx.Value("uid").(string)
ok, role := models.RoleExistByUid(uid)
if !ok {
role = models.NewRole(conf.SnowFlack.NextVal())
role.Role.Device = in.Device
role.Role.Uid = uid
role.Create()
}
return &pb.RoleRsp{
Code: 0,
Role: role.Role,
}, nil
}
func (s *GameServer) LoginHandler(ctx context.Context, in *pb.LoginReq) (*pb.RoleRsp, error) {
uid := ctx.Value("uid").(string)
ok, role := models.RoleExistByUid(uid)
if !ok {
return &pb.RoleRsp{
Code: 1,
}, nil
}
return &pb.RoleRsp{
Code: 0,
Role: role.Role,
Hero: models.GetHeros(role.Heros),
}, nil
}