package main

import (
	"net/http"
	redismgr "server/db/redis"
	"server/msg"
	"server/user"

	"github.com/gin-gonic/gin"
)

func ReqLogin(c *gin.Context) {
	req := c.MustGet("protobuf_data").(*msg.ReqLogin)
	UserId, err := redismgr.GetUserIDFromRedisByAP(req.Account, req.Password)
	if err != nil {

		c.ProtoBuf(http.StatusOK, &msg.ResLogin{
			NikeName: "",
			UserId:   "",
			ErrMsg: &msg.MsgError{
				ErrorCode: 101,
				ErrorMsg:  "Password or Account is error！",
			},
		})
		return
	}
	// 获取用户信息 ,如果新用户则创建一个
	userData := user.GetUserInfoById(UserId)
	c.ProtoBuf(http.StatusOK, &msg.ResLogin{
		UserInfo: &msg.UserInfo{
			UserId: userData.UserId,
			MHead:  userData.MHead,
			MCoin:  userData.MCoin,
			Name:   userData.Name,
			RoomId: userData.RoomId,
		},
		ErrMsg: nil,
	})
}
