package ludo

import (
	"fmt"
	"server/game"
	"server/msg"
	"time"

	"github.com/name5566/leaf/gate"
)

// 接受玩家移动消息
func RecvPlayerRoleMove(args []interface{}) {
	m := args[0].(*msg.SendRoleMove)
	a := args[1].(gate.Agent)
	fmt.Printf("接受玩家移动消息 %v\n", m)
	ud := a.UserData()
	room_id := ud.(*msg.UserInfo).RoomId
	// color := m.Color
	if room_id == 0 {
		fmt.Printf("I房间不存在 %d\n", room_id)
	} else {
		room_info := GetBattleRoomById(room_id)
		if room_info != nil {
			room_info.CancelCountdownPlayerOpt()
			room_info.send_role_move(m.Color, m.RoleId)
		} else {
			fmt.Printf("IV房间不存在 %d\n", room_id)
		}

	}
}

// 接受玩家摇筛子消息
func RecvPlayerSzNumber(args []interface{}) {
	m := args[0].(*msg.SendColorSz)
	a := args[1].(gate.Agent)
	fmt.Printf("接受玩家摇筛子消息：%v\n", m)
	ud := a.UserData()
	room_id := ud.(*msg.UserInfo).RoomId
	fmt.Printf("玩家房间id%d\n", room_id)
	color := m.Color
	if room_id == 0 {
		a.WriteMsg(&msg.NotifyPlayerSzNumber{
			Color: color,
			ErrMsg: &msg.MsgError{
				ErrorCode: 101,
				ErrorMsg:  "玩家不在房间！",
			},
		})
	} else {
		room_info := GetBattleRoomById(room_id)

		if room_info != nil {
			room_info.send_sz(color)
		}

	}

}

// 接受玩家离开消息
func RecvPlayerLeave(args []interface{}) {
	m := args[0].(*msg.SendQuitRoom)
	a := args[1].(gate.Agent)
	fmt.Printf("接受玩家离开消息:%v\n", m)
	ud := a.UserData()
	room_id := ud.(*msg.UserInfo).RoomId
	fmt.Printf("玩家房间id%d\n", room_id)
	if room_id == 0 {

	} else {
		room_info := GetBattleRoomById(room_id)

		if room_info != nil {
			color_data := room_info.getColorDataByColor(m.Color)
			color_data.IsKick = true
			room_info.kickColor(m.Color)
			room_info.kick_color_of_room(m.Color)
			if room_info.IsOverGame() { //宣布游戏结束
				game.Module.AfterFunc(time.Second*time.Duration(1), func() {
					room_info.notify_settlement()
				})
			}
		}

	}

}
