package ludo

import (
	"server/msg"
)

// 发送消息移动角色
func (room_info *RoomInfoWrapper) send_role_move(color msg.RoleType, role_id string) {
	room_info.CancelCountdownPlayerOpt()
	role_data := room_info.getRoleDataById(role_id)
	szNumber := room_info.getLastRoundByOpt(msg.OptType_ZHI_SHAI_ZI).SzNumber
	step_data := &msg.MoveStepData{
		MId: role_id,
	}
	message := &msg.NotifyPlayerMove{
		Color: color,
		Step:  step_data,
	}
	des_step := role_data.Step + szNumber
	if role_data.Step == 0 {
		des_step = role_data.Step + 1
	}
	road_key := room_info.getMapPosByColorAndStep(color, des_step)
	des_road_roles := room_info.getRoadRoles(road_key)
	if len(des_road_roles) > 0 { //如果路上还有其他人
		if room_info.isWuDiRoad(road_key) { //如果是无敌的路

		} else {
			enemys := room_info.getRolesByColorRoles(color, des_road_roles)
			if len(enemys) > 0 { //说明目标路上有其他敌人
				//将所有敌人踢回阵营，然后再加一轮筛子操作
				room_info.kickRolesBackHome(enemys)
				message.Kick = enemys
			}
		}
	}

	isFinish := false
	isFinishThree := false
	if room_info.moveTargetIsEnd(color, road_key) { //如果是终点，奖励一次筛子
		isFinishThree = len(room_info.getFinishRolesByColor(color)) == 3
		if isFinishThree { //这个玩家跑完
			room_info.addFinishColor(color)
			room_info.notify_player_status(color, msg.PlayerStatus_COLOR_FINISH, room_info.getFinishColors())
		} else {
			//奖励他一次筛子
			isFinish = true
		}
	}
	message.Step.Step = des_step
	message.Step.OldSetp = role_data.Step
	room_info.updateRoleStep(role_id, message.Step.Step)

	room_info.notify_update_room_info()
	room_info.notify_to_all_player(message)

	var kick_delay_time int32

	if len(message.Kick) > 0 {
		max_oldSetp := message.Kick[0].OldSetp
		for i := 0; i < len(message.Kick); i++ {
			element := message.Kick[i]
			if element.OldSetp > max_oldSetp {
				max_oldSetp = element.OldSetp
			}
		}
		kick_delay_time = kick_delay_time + max_oldSetp*100
	}

	delay_time := szNumber*300 + kick_delay_time

	isAddRound := len(message.Kick) > 0

	room_info.delayActionNextRound(delay_time, color, szNumber, isAddRound, isFinish)
}

// 发送掷骰子消息
func (room_info *RoomInfoWrapper) send_sz(color msg.RoleType) {
	room_info.CancelCountdownPlayerOpt()

	room_info.notify_player_status(color, msg.PlayerStatus_SZ_ANIMATION, nil)

	sz_number := randomSz()

	round_data := &msg.Round{
		MColor:   color,
		SzNumber: sz_number,
		Opt:      msg.OptType_ZHI_SHAI_ZI,
	}

	room_info.addRound(round_data)

	msg_body := &msg.NotifyPlayerSzNumber{
		Color:    color,
		SzNumber: sz_number,
	}

	room_info.notify_to_all_player(msg_body)
	room_info.delay_action_sz(1000, msg_body)
}
