go_dreamfactory/cmd/robot/user.go
2022-06-07 18:14:49 +08:00

65 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package robot
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"log"
"google.golang.org/protobuf/proto"
)
func (r *Robot) handleUserMsg(msg *pb.UserMessage) {
switch msg.SubType {
case "login":
handleLogin(r, msg)
case "create":
handleCreateUser(r, msg)
}
}
//处理登录响应数据
func handleLogin(r *Robot, msg *pb.UserMessage) {
rsp := &pb.UserLoginResp{}
if !comm.ProtoDecode(msg, rsp) {
return
}
log.Printf("登录返回: %v", rsp)
//是否有登录数据返回
if rsp != nil {
r.onUserLoaded()
} else {
r.AccountRegister() //请求Http接口模拟创建新账号
}
}
func handleCreateUser(r *Robot, msg *pb.UserMessage) {
rsp := &pb.UserCreateRsp{}
if !comm.ProtoDecode(msg, rsp) {
return
}
log.Printf("创角返回: %v", rsp)
}
//创角色
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)
}
}
}