package controller

import (
	"blazing/common/socket/errorcode"
	"blazing/cool"

	"blazing/logic/service/common"

	"blazing/logic/service/player"
	"blazing/logic/service/space"
	"blazing/logic/service/user"
	blservice "blazing/modules/blazing/service"
	"strings"
)

// 处理命令: 1001
func (h *Controller) CreatePlayer(data *user.CreatePlayerInboundInfo, c *player.Conn) (result *user.CreatePlayerOutInfo, err errorcode.ErrorCode) {

	blservice.NewUserService(data.Head.UserID).Reg(cool.Filter.Replace(data.Nickname, '*'), data.Color)
	return result, 0
}

func (h *Controller) ChangePlayerName(data *user.ChangePlayerNameInboundInfo, c *player.Player) (result *user.ChangePlayerNameOutboundInfo, err errorcode.ErrorCode) {
	newnice := cool.Filter.Replace(strings.Trim(data.Nickname, "\x00"), '*')

	c.Info.Nick = newnice
	result = &user.ChangePlayerNameOutboundInfo{
		Nickname: newnice,
		UserID:   c.Info.UserID,
	}
	space.GetSpace(c.Info.MapID).Range(func(playerID uint32, player common.PlayerI) bool {

		player.SendPack(data.Head.Pack(&result))
		return true
	})
	return result, 0
}
