PlayerList in game

This commit is contained in:
Tnze
2022-06-23 13:13:38 +08:00
parent eecfa6d8a8
commit d829c47731
7 changed files with 119 additions and 56 deletions

View File

@ -30,7 +30,6 @@ func NewPlayerList(maxPlayers int) *PlayerList {
}
}
// ClientJoin implement Component for PlayerList
func (p *PlayerList) ClientJoin(client PlayerListClient, player PlayerSample) {
p.playersLock.Lock()
defer p.playersLock.Unlock()
@ -49,6 +48,7 @@ func (p *PlayerList) ClientLeft(client PlayerListClient) {
delete(p.players, client)
}
// CheckPlayer implements LoginChecker for PlayerList
func (p *PlayerList) CheckPlayer(string, uuid.UUID, int32) (ok bool, reason chat.Message) {
p.playersLock.Lock()
defer p.playersLock.Unlock()
@ -87,3 +87,17 @@ func (p *PlayerList) PlayerSamples() (sample []PlayerSample) {
}
return
}
func (p *PlayerList) Len() int {
p.playersLock.Lock()
defer p.playersLock.Unlock()
return len(p.players)
}
func (p *PlayerList) Range(f func(PlayerListClient, PlayerSample)) {
p.playersLock.Lock()
defer p.playersLock.Unlock()
for client, player := range p.players {
f(client, player)
}
}