package game

import (
	mergeCluster "server/cluster"
	"server/game/mod/msg"
)

var clusterHandler map[int]func(*msg.Msg) error

func ClusterMgrInit() {
	go func() {
		for {
			m := <-mergeCluster.MsgChan
			MessageHandle(m)
		}
	}()

}

func RegisterClusterHandler(t int, fun func(*msg.Msg) error) {
	clusterHandler[t] = fun
}

func init() {
	clusterHandler = make(map[int]func(*msg.Msg) error)
	RegisterClusterHandler(msg.HANDLE_TYPE_DEL, FriendMgrSend)
	RegisterClusterHandler(msg.HANDLE_TYPE_APPLY, FriendMgrSend)
	RegisterClusterHandler(msg.HADNLE_TYPE_AGREE, FriendMgrSend)
	RegisterClusterHandler(msg.HANDLE_TYPE_REQ_CARD, FriendMgrSend)
	RegisterClusterHandler(msg.HANDLE_TYPE_AGREE_CARD, FriendMgrSend)
	RegisterClusterHandler(msg.CLUSTER_FRIEND_SYNC, ClusterFriendSync)
	RegisterClusterHandler(msg.HANDLE_TYPE_CHAMPSHIP_INRANK, champshipInrankHandler)
	RegisterClusterHandler(msg.HANDLE_TYPE_CHAMPSHIP_RANK_INFO, champshipRankInfoHandler)
	RegisterClusterHandler(msg.HANDLE_TYPE_CHAMPSHIP_MY_RANK, champshipMyRankHandler)
}

func champshipInrankHandler(m *msg.Msg) error {
	G_GameLogicPtr.ChampshipMgrSend(m)
	return nil
}

func champshipRankInfoHandler(m *msg.Msg) error {
	data := G_GameLogicPtr.ChampshipMgrCall(m)
	m.To = m.From
	m.From = 0
	m.Extra = data
	FriendMgrSend(m)
	return nil
}

func champshipMyRankHandler(m *msg.Msg) error {
	MyRank := G_GameLogicPtr.ChampshipMgr.getMyRank(m.From)
	m.To = m.From
	m.From = 0
	m.Extra = MyRank
	FriendMgrSend(m)
	return nil
}
