package game

import (
	mergeCluster "server/cluster"
	"server/game/mod/msg"
	"server/pkg/github.com/name5566/leaf/log"
)

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

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

}

func clusterHandlerProcess(m *msg.Msg) {
	if fun, ok := clusterHandler[m.Type]; ok {
		fun(m)
	} else {
		log.Debug("clusterHandlerProcess not found handler %d", m.Type)
	}
}

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)
}
