Пример #1
0
func Reg(userInfo *model.UserInfo, reqData []byte) (ackData []byte, err error) {
	_req := &RegisterReq{}
	_ack := &RegisterAck{}
	err = decode(reqData, _req)
	if err != nil {
		log.Println("数据包格式不对", err)
		return nil, err
	}
	reCode := message.SUCCESS
	roleInfo := model.NewRoleInfo()
	err = userInfo.AddUser(_req.Username, _req.Password)
	if err != nil {
		reCode = message.ERR_REGISTER
		err = nil
	} else {
		//根据用户uid得到角色信息
		err := roleInfo.AddRole(userInfo.Uid, 1, "summer", 1, 1)
		if err != nil {
			log.Println("增加默认角色出错", err)
			return nil, err
		}
	}

	log.Println("登陆返回的uid为:", userInfo.Uid)
	_ack.UserInfo = userInfo
	_ack.RoleInfo = roleInfo
	ackData = encode(userInfo.Uid, message.MSG_REGISTER, reCode, _ack)
	return
}
Пример #2
0
func SelectRole(userInfo *model.UserInfo, reqData []byte) (ackData []byte, err error) {
	_req := &RoleReq{}
	_ack := &RoleAck{}
	err = decode(reqData, _req)
	if err != nil {
		log.Println("数据包格式不对", err)
		return nil, err
	}
	reCode := message.SUCCESS
	roleInfo := model.NewRoleInfo()
	err = roleInfo.GetRoleInfo(userInfo.Uid, _req.Role_id)
	if err != nil {
		log.Println(userInfo.Uid, "查询角色信息出错", err)
		return nil, err
	}

	if roleInfo.Id == 0 {
		reCode = message.ERR_SELECTROLE
	}
	err = userInfo.ChangeRole(userInfo.Uid, _req.Role_id)
	if err != nil {
		log.Println(userInfo.Uid, "选择角色信息出错", err)
		return nil, err
	}
	ackData = encode(userInfo.Uid, message.MSG_SELECTROLE, reCode, _ack)
	return
}
Пример #3
0
func Login(userInfo *model.UserInfo, reqData []byte) (ackData []byte, err error) {
	log.Println("登录数据包是:", reqData)
	_req := &LoginReq{}
	_ack := &LoginAck{}
	err = decode(reqData, _req)
	if err != nil {
		log.Println("数据包格式不对", err)
		return nil, err
	}
	reCode := message.SUCCESS
	roleInfo := model.NewRoleInfo()

	err = userInfo.CheckLogin(_req.Username, _req.Password)
	if err != nil {
		reCode = message.ERR_LOGIN
		err = nil
	} else {
		//根据用户uid得到角色信息
		err = roleInfo.GetRoleInfo(userInfo.Uid, userInfo.Role_Id)
		log.Println(err, roleInfo)
		if err != nil {
			log.Println("获取角色信息出错")
			return nil, err
		}
	}

	//建立当前会话信息

	log.Println("登陆返回的uid为:", userInfo.Uid)
	_ack.UserInfo = userInfo
	_ack.RoleInfo = roleInfo
	ackData = encode(userInfo.Uid, message.MSG_LOGIN, reCode, _ack)
	return
}
Пример #4
0
func RoleBuy(userInfo *model.UserInfo, reqData []byte) (ackData []byte, err error) {
	_req := &RoleBuyReq{}
	_ack := &RoleBuyAck{}
	err = decode(reqData, _req)
	if err != nil {
		log.Println("数据包格式不对", err)
		return nil, err
	}
	reCode := message.SUCCESS
	roleInfo := model.NewRoleInfo()
	err = roleInfo.GetRoleInfo(userInfo.Uid, _req.Role_id)
	if err != nil {
		log.Println(userInfo.Uid, "查询角色信息出错", err)
		reCode = message.ERR_BUYROLE
	}

	//说明还不拥有此角色
	if roleInfo.Id == 0 {
		err = userInfo.GetUserInfo(userInfo.Uid)
		//获取角色的信息(数值相关)
		cash := gamedata.GetInt("角色ID", strconv.Itoa(int(_req.Role_id)), "cash")
		diamond := gamedata.GetInt("角色ID", strconv.Itoa(int(_req.Role_id)), "diamond")
		log.Println("角色ID:", _req.Role_id, " cash:", cash)
		if userInfo.Cash > int32(cash) && userInfo.Diamond > int32(diamond) {
			err = userInfo.ChangeCash(userInfo.Uid, -cash)
			if err != nil {
				log.Println(userInfo.Uid, "购买角色信息出错", err)
				reCode = message.ERR_BUYROLE
			}
			err = userInfo.ChangeDiamond(userInfo.Uid, -diamond)
			if err != nil {
				log.Println(userInfo.Uid, "购买角色信息出错", err)
				reCode = message.ERR_BUYROLE
			}
			name := gamedata.GetString("角色ID", strconv.Itoa(int(_req.Role_id)), "角色名称")
			log.Println("角色ID:", _req.Role_id, " 角色名称:", name)
			err = roleInfo.AddRole(userInfo.Uid, _req.Role_id, name, 1, 1)
			if err != nil {
				log.Println(userInfo.Uid, "增加角色信息出错", err)
				reCode = message.ERR_BUYROLE
			}
		}
	}

	err = userInfo.ChangeRole(userInfo.Uid, _req.Role_id)
	if err != nil {
		log.Println(userInfo.Uid, "选择角色信息出错", err)
		reCode = message.ERR_BUYROLE
	}

	ackData = encode(userInfo.Uid, message.MSG_BUYROLE, reCode, _ack)
	return
}