package baseroom

import (
	"game/common/proto/pb"
)

type PlayerMgr struct {
	players      map[int64]IPlayer
	createPlayer ICreatePlayer
}

func NewPlayerMgr(create ICreatePlayer) *PlayerMgr {
	return &PlayerMgr{
		players:      make(map[int64]IPlayer),
		createPlayer: create,
	}
}

func (s *PlayerMgr) Count() int {
	return len(s.players)
}

func (s *PlayerMgr) Init() {
}

func (s *PlayerMgr) Add(player IPlayer) {
	s.players[player.Id()] = player
}

func (s *PlayerMgr) Del(uid int64) {
	delete(s.players, uid)
}

func (s *PlayerMgr) Find(uid int64) IPlayer {
	return s.players[uid]
}

func (s *PlayerMgr) Filter(predicate func(player IPlayer) bool) []IPlayer {
	players := make([]IPlayer, 0)
	for _, player := range s.players {
		if predicate(player) {
			players = append(players, player)
		}
	}
	return players
}

func (s *PlayerMgr) CreatePlayer(uid int64) (player IPlayer, code pb.ErrCode) {
	player, code = s.createPlayer.CreatePlayer(uid)
	return
}
