package controller

import (
	"blazing/common/socket/errorcode"

	"blazing/logic/service/common"
	"blazing/logic/service/maphot"
	"blazing/logic/service/maps"
	"blazing/logic/service/player"
	"blazing/logic/service/space"

	"github.com/jinzhu/copier"
)

func (h *Controller) MapEnter(data *maps.InInfo, c *player.Player) (result *maps.OutInfo, err errorcode.ErrorCode) { //这个时候player应该是空的

	c.Info.MapID = data.MapId                          //登录地图
	space.GetSpace(c.Info.MapID).Set(c.Info.UserID, c) //添加玩家
	result = maps.NewOutInfo()
	c.Info.Pos = data.Point
	copier.Copy(result, c.Info)

	data.Broadcast(c.Info.MapID, *result) //同步广播

	return nil, -1
}
func (h Controller) MapHot(data *maphot.InInfo, c *player.Player) (result *maphot.OutInfo, err errorcode.ErrorCode) {

	result = &maphot.OutInfo{

		HotInfos: space.GetMapHot(),
	}

	return
}
func (h *Controller) MapLeave(data *maps.LeaveMapInboundInfo, c *player.Player) (result *space.LeaveMapOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
	//result = &maps.LeaveMapOutboundInfo{UserID: c.GetUserID()}
	c.Canmon = false                                                                //可以刷怪
	data.Broadcast(c.Info.MapID, space.LeaveMapOutboundInfo{UserID: c.Info.UserID}) //同步广播
	space.GetSpace(c.Info.MapID).Delete(c.Info.UserID)
	// 如果有正在运行的刷怪协程，发送停止信号

	c.Info.MapID = 0 // 重置当前地图
	return nil, -1
}
func (h *Controller) MapList(data *maps.ListMapPlayerInboundInfo, c *player.Player) (result *maps.ListMapPlayerOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的

	result = &maps.ListMapPlayerOutboundInfo{}
	result.Player = make([]maps.OutInfo, 0)

	space.GetSpace(c.Info.MapID).Range(func(userID uint32, player common.PlayerI) bool {
		result1 := maps.NewOutInfo()
		copier.Copy(result1, player.GetInfo())
		result.Player = append(result.Player, *result1)
		return true
	})
	c.Canmon = true //可以刷怪
	return
}
