package main

import (
	"WorldTester/conf"
	"common/logger"
	"math/rand"
	"time"
)

func StartObserver(interval int) {
	//按照节奏，启动旁观者
	i := 0
	counter := 0
	for uid, _ := range conf.Config.ObserverMap {
		if counter >= obeserver_count {
			return
		}
		counter++

		conn := getTcpClient(uint32(uid))
		var player PlayerInfo
		player.uid = uint32(uid)
		player.rid = 0
		player.conn = conn
		player.isObserver = true
		m_map_players.Set(uint32(uid), &player)

		SendLogin(&player)
		StartPlayer(&player)
		i++
		if i > 5 {
			i = 0
			time.Sleep(time.Duration(interval))
		}
	}
}
func HandleObserverLogin(player *PlayerInfo) {
	//login response
}
func HandleObserverJoinRoom(player *PlayerInfo) {
	//join room response
}
func HandleObserverLeaveRoom(player *PlayerInfo) {
	//leave room response
}
func checkObserver(player *PlayerInfo) {
	//	now := time.Now().Unix()
	//	if now < player.lastCheckTime + 50{
	//		return
	//	}
	//	player.lastCheckTime = now
	//	RandomGet(player)

	//	switch player.state {
	//	case STATE_PLAYER_INIT:
	//		return
	//	case STATE_PLAYER_LOGINED:
	//		ActionWhenObserverLogined(player)
	//		return
	//	case STATE_PLAYER_ENTERED:
	//		ActionWhenObserverEntered(player)
	//		return
	//	default:
	//		return
	//	}
}
func GetRoomIDRandomly() uint32 {
	items := m_map_rooms.Items()
	i := rand.Intn(int(m_map_rooms.Len()) + 1)
	idx := 0
	for rid, _ := range items {
		if i == idx {
			return rid
		}
		idx++
	}
	return 0
}
func ActionWhenObserverLogined(player *PlayerInfo) {
	//登录状态的时候
	//随机进入或者不进入房间
	//随机挑选一个房间进入
	r := rand.Intn(1000)
	if r < 500 {
		//do nothing
	} else {
		player.rid = GetRoomIDRandomly()
		if player.rid == 0 {
			return
		}
		//SendEnterRoom(player)
		logger.Info("obervser uid:%v do enter rid:%v", player.uid, player.rid)
	}
}

func ActionWhenObserverEntered(player *PlayerInfo) {
	//在房间里面的时候
	//随机离开房间，发表情，发言
	r := rand.Intn(1000)
	if r < 100 {
		//leave room
		//SendLeaveRoomRequest(player)
		logger.Info("observer uid:%v do leave room:%v", player.uid, player.rid)
	} else if r < 250 {
		//send message
		//SendChat(player)
		logger.Info("observer uid:%v send chat room:%v", player.uid, player.rid)
	} else if r < 400 {
		logger.Info("observer uid:%v interactive expressiong room:%v", player.uid, player.rid)
		//DoInteractiveExpression(player)
	} else {
		//do nothing
	}
}
