package service

import (
	"blazing/cool"
	"blazing/modules/blazing/model"
	"context"
	"strings"
	"time"

	"github.com/gogf/gf/v2/os/glog"
)

// 是否注册,如果注册过,那么就会产生用户player信息
func (s *UserService) IsReg() bool {

	m := cool.DBM(s.info.Model).Where("player_id", s.userid)

	record, err := m.One()
	if err != nil {
		return false
	}
	if record != nil {
		return true
	}
	return false
}

// 实现注册,id+昵称+颜色
func (s *UserService) Reg(nick string, color uint32) {
	if s.IsReg() {
		return
	}

	nick = strings.Trim(nick, "\x00")

	t := model.NewPlayer()
	t.PlayerID = uint64(s.userid)
	//设置用户信息
	t.Data = model.NewPlayerInfo()
	t.Data.Nick = nick

	t.Data.Color = color
	t.Data.RegisterTime = uint32(time.Now().Unix()) //写入注册时间

	_, err := cool.DBM(s.info.Model).Data(t).FieldsEx("id").Insert()
	if err != nil {
		glog.Error(context.Background(), err)
		return
	}
	//go s.InitTask()
}

func (s *UserService) Person(userid uint32) *model.PlayerInfo {

	m := cool.DBM(s.info.Model).Where("player_id", userid)
	var tt model.PlayerEX
	err := m.Scan(&tt)
	if err != nil {
		panic(err)
	}
	ret := tt.Data
	return ret

}
func (s *UserService) Save(data *model.PlayerInfo) {

	m := cool.DBM(s.info.Model).Where("player_id", data.UserID)
	var tt model.PlayerEX
	m.Scan(&tt)
	tt.Data = data
	_, err := m.Update(tt)
	if err != nil {
		panic(err)
	}

}
