roleaction.go 1.71 KB
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
}