func (g *Game) handleGameSnag(ctx context.Context, m message.Message, send messageSender) error {
	switch {
	case g.status != game.InProgress:
		return gameWarningNotInProgress
	case len(g.unusedTiles) == 0:
		return gameWarning("no tiles left to snag, use what you have to finish")
	}
	if _, err := g.checkPlayerBoard(m.PlayerName, g.Config.CheckOnSnag); err != nil {
		return err
	}
	snagPlayerMessages := make(map[player.Name]message.Message, len(g.players))
	snagPlayerNames := make([]player.Name, 1, len(g.players))
	snagPlayerNames[0] = m.PlayerName
	for n2 := range g.players {
		if m.PlayerName != n2 {
			snagPlayerNames = append(snagPlayerNames, n2)
		}
	}
	g.ShufflePlayersFunc(snagPlayerNames[1:])
	for _, n2 := range snagPlayerNames {
		m2 := message.Message{
			Type:       message.ChangeGameTiles,
			PlayerName: n2,
		}
		var tiles []tile.Tile
		switch {
		case n2 == m.PlayerName:
			tiles = g.unusedTiles[:1]
			m2.Info = "snagged a tile"
			if err := g.players[n2].Board.AddTile(g.unusedTiles[0]); err != nil {
				return err
			}
			g.unusedTiles = g.unusedTiles[1:]
		case len(g.unusedTiles) == 0:
			m2.Info = fmt.Sprintf("%v snagged a tile", m.PlayerName)
		default:
			m2.Info = fmt.Sprintf("%v snagged a tile, adding a tile to your pile", m.PlayerName)
			tiles = g.unusedTiles[:1]
			if err := g.players[n2].Board.AddTile(g.unusedTiles[0]); err != nil {
				return err
			}
			g.unusedTiles = g.unusedTiles[1:]
		}
		m2.Game = &game.Info{
			Board: board.New(tiles, nil),
		}
		snagPlayerMessages[n2] = m2
	}
	for _, m := range snagPlayerMessages {
		m.Game.TilesLeft = len(g.unusedTiles)
		send(m)
	}
	return nil
}
// handleGameSnag adds a tile to all the players.
// The order that the players receive their tiles is randomized, some players may not receive tiles if there are none left.
handleGameSnag adds a tile to all the players.
The order that the players receive their tiles is randomized, some players may not receive tiles if there are none left.
// handleGameSnag adds a tile to all the players . // The order that the players receive their tiles is randomized some players may not receive tiles if there are none left .
func (g *Game) handleGameSnag(ctx context.Context, m message.Message, send messageSender) error {
	switch {
	case g.status != game.InProgress:
		return gameWarningNotInProgress
	case len(g.unusedTiles) == 0:
		return gameWarning("no tiles left to snag, use what you have to finish")
	}
	if _, err := g.checkPlayerBoard(m.PlayerName, g.Config.CheckOnSnag); err != nil {
		return err
	}
	snagPlayerMessages := make(map[player.Name]message.Message, len(g.players))
	snagPlayerNames := make([]player.Name, 1, len(g.players))
	snagPlayerNames[0] = m.PlayerName
	for n2 := range g.players {
		if m.PlayerName != n2 {
			snagPlayerNames = append(snagPlayerNames, n2)
		}
	}
	g.ShufflePlayersFunc(snagPlayerNames[1:])
	for _, n2 := range snagPlayerNames {
		m2 := message.Message{
			Type:       message.ChangeGameTiles,
			PlayerName: n2,
		}
		var tiles []tile.Tile
		switch {
		case n2 == m.PlayerName:
			tiles = g.unusedTiles[:1]
			m2.Info = "snagged a tile"
			if err := g.players[n2].Board.AddTile(g.unusedTiles[0]); err != nil {
				return err
			}
			g.unusedTiles = g.unusedTiles[1:]
		case len(g.unusedTiles) == 0:
			m2.Info = fmt.Sprintf("%v snagged a tile", m.PlayerName)
		default:
			m2.Info = fmt.Sprintf("%v snagged a tile, adding a tile to your pile", m.PlayerName)
			tiles = g.unusedTiles[:1]
			if err := g.players[n2].Board.AddTile(g.unusedTiles[0]); err != nil {
				return err
			}
			g.unusedTiles = g.unusedTiles[1:]
		}
		m2.Game = &game.Info{
			Board: board.New(tiles, nil),
		}
		snagPlayerMessages[n2] = m2
	}
	for _, m := range snagPlayerMessages {
		m.Game.TilesLeft = len(g.unusedTiles)
		send(m)
	}
	return nil
}
func ( g * Game ) handleGameSnag ( ctx context . Context , m message . Message , send messageSender ) error { switch { case g . status != game . InProgress : return gameWarningNotInProgress case len ( g . unusedTiles ) == 0 : return gameWarning ( " " ) } if _ , err := g . checkPlayerBoard ( m . PlayerName , g . Config . CheckOnSnag ) ; err != nil { return err } snagPlayerMessages := make ( map [ player . Name ] message . Message , len ( g . players ) ) snagPlayerNames := make ( [ ] player . Name , 1 , len ( g . players ) ) snagPlayerNames [ 0 ] = m . PlayerName for n2 := range g . players { if m . PlayerName != n2 { snagPlayerNames = append ( snagPlayerNames , n2 ) } } g . ShufflePlayersFunc ( snagPlayerNames [ 1 : ] ) for _ , n2 := range snagPlayerNames { m2 := message . Message { Type : message . ChangeGameTiles , PlayerName : n2 , } var tiles [ ] tile . Tile switch { case n2 == m . PlayerName : tiles = g . unusedTiles [ : 1 ] m2 . Info = " " if err := g . players [ n2 ] . Board . AddTile ( g . unusedTiles [ 0 ] ) ; err != nil { return err } g . unusedTiles = g . unusedTiles [ 1 : ] case len ( g . unusedTiles ) == 0 : m2 . Info = fmt . Sprintf ( " " , m . PlayerName ) default : m2 . Info = fmt . Sprintf ( " " , m . PlayerName ) tiles = g . unusedTiles [ : 1 ] if err := g . players [ n2 ] . Board . AddTile ( g . unusedTiles [ 0 ] ) ; err != nil { return err } g . unusedTiles = g . unusedTiles [ 1 : ] } m2 . Game = & game . Info { Board : board . New ( tiles , nil ) , } snagPlayerMessages [ n2 ] = m2 } for _ , m := range snagPlayerMessages { m . Game . TilesLeft = len ( g . unusedTiles ) send ( m ) } return nil }
handleGameSnag adds a tile to all the players.
handleGameSnag adds a tile to all the players .