Example #1
0
func (players List) HandleDisconnect(identifier socket.Identifier) (*Player, error) {
	name := identifier.Name()
	player, exists := players[name]
	if !exists {
		return nil, errors.New("player was not connected")
	}

	if !player.CompareIdentifier(identifier) {
		return nil, errors.New("identifier does not match any connected player")
	}

	if err := player.ClearIdentifier(); err != nil {
		return nil, err
	}

	player.Ready = false

	return player, nil
}
Example #2
0
func (players List) HandleConnect(identifier socket.Identifier) (*Player, bool, error) {
	name := identifier.Name()
	if player, exists := players[name]; exists {
		if player.HasIdentifier() {
			return nil, false, errors.New("player already identified, rejecting")
		}

		player.Ready = false
		if err := player.SetIdentifier(identifier); err != nil {
			return nil, false, err
		}

		return player, false, nil
	}

	players[name] = NewPlayer(name)
	players[name].Ready = false

	if err := players[name].SetIdentifier(identifier); err != nil {
		return nil, false, err
	}

	return players[name], true, nil
}