<commit_before>package main

import (
	"encoding/json"
	"log"
	"os"
	"strings"

	"github.com/davidnix/ffdraft/command"
	"github.com/davidnix/ffdraft/players"
	"github.com/pkg/errors"
	"github.com/urfave/cli/v2"
)

var teamCmd = &cli.Command{
	Name:  "team",
	Usage: "Manage a team",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:     "csv",
			Usage:    "(required) path to projections csv",
			Required: true,
		},
		&cli.StringFlag{
			Name:     "name",
			Aliases:  []string{"n"},
			Usage:    "(required) your team name",
			Required: true,
		},
	},
	Action: teamInteractive,
}

const teamUsage = `
--------------------------------------------------------------------------------------------------------------------
Commands:
    add [name]:             adds player to your team	
    ceil:                   print the highest ceiling value for available players for each position
    exit, ctl+D:            exits this program
    find, f [name]:         fuzzy finds players matching player name
    floor, fl:              sort by highest floor value for your team
    help, h:                print this help text
    rm, drop [name]:        removes player from your team
    team:                   print a team's depth chart
--------------------------------------------------------------------------------------------------------------------`

func teamInteractive(ctx *cli.Context) error {
	repo, err := buildRepo(ctx.String("csv"))
	if err != nil {
		return err
	}
	team, err := loadTeam(ctx.String("name"))
	if err != nil {
		return err
	}
	log.Println("Loaded", len(repo.Available), "players")
	repo.Sync(team.Players)
	log.Println(len(repo.Claimed), "players on your team")
	log.Println(len(repo.Available), "players remaining")

	defer mustSaveTeam(team, repo)

	log.Println(teamUsage)

	command.Lineup(repo, true)

	for {
		in, err := command.GetInput()
		if err != nil {
			return err
		}
		input := strings.Fields(in)
		var cmd string
		args := []string{}
		if len(input) > 0 {
			cmd = input[0]
			args = input[1:]
		}

		switch cmd {
		case "add":
			command.Pick(repo, args)
			command.Lineup(repo, true)

		case "rm", "drop":
			command.UnPick(repo, args)
			command.Lineup(repo, true)

		case "exit":
			return errors.New("user canceled")

		case "floor", "fl":
			command.Lineup(repo, true)

		case "ceil":
			command.Lineup(repo, false)

		case "team":
			command.Team(repo, args)

		case "find", "f":
			command.Find(repo, args)

		case "help", "h", "usage":
			log.Println(teamUsage)

		case "":
			continue

		default:
			log.Printf("Unrecognized command %q. Type help for usage.\n", cmd)
		}
	}
}

func loadTeam(name string) (*players.Team, error) {
	t := &players.Team{Name: name}
	fname := t.Name + ".json"
	existing, err := os.ReadFile(fname)
	if err != nil {
		log.Printf("team file %q: %v; creating new team", fname, err)
		return t, nil
	}
	err = json.Unmarshal(existing, t)
	return t, errors.Wrap(err, fname)
}

func mustSaveTeam(t *players.Team, repo *players.Repo) {
	t.Sync(repo.Claimed)
	fname := t.Name + ".json"
	log.Println("saving team to", fname)
	b, err := json.Marshal(t)
	if err != nil {
		panic(err)
	}
	err = os.WriteFile(fname, b, 0660)
	if err != nil {
		panic(err)
	}
}
<commit_msg>add waiver command<commit_after>package main

import (
	"encoding/json"
	"log"
	"os"
	"strings"

	"github.com/davidnix/ffdraft/command"
	"github.com/davidnix/ffdraft/players"
	"github.com/pkg/errors"
	"github.com/urfave/cli/v2"
)

var teamCmd = &cli.Command{
	Name:  "team",
	Usage: "Manage a team",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:     "csv",
			Usage:    "(required) path to projections csv",
			Required: true,
		},
		&cli.StringFlag{
			Name:     "name",
			Aliases:  []string{"n"},
			Usage:    "(required) your team name",
			Required: true,
		},
	},
	Action: teamInteractive,
}

const teamUsage = `
--------------------------------------------------------------------------------------------------------------------
Commands:
	add [name]:             adds player to your team
	ceil:                   print the highest ceiling value for available players for each position
	exit, ctl+D:            exits this program
	find, f [name]:         fuzzy finds players matching player name
	floor, fl:              sort by highest floor value for your team
	help, h:                print this help text
	rm, drop [name]:        removes player from your team
	team:                   print a team's depth chart
	waiver, w               show available sorted by highest floor value
--------------------------------------------------------------------------------------------------------------------`

func teamInteractive(ctx *cli.Context) error {
	repo, err := buildRepo(ctx.String("csv"))
	if err != nil {
		return err
	}
	team, err := loadTeam(ctx.String("name"))
	if err != nil {
		return err
	}
	log.Println("Loaded", len(repo.Available), "players")
	repo.Sync(team.Players)
	log.Println(len(repo.Claimed), "players on your team")
	log.Println(len(repo.Available), "players remaining")

	defer mustSaveTeam(team, repo)

	log.Println(teamUsage)

	command.Lineup(repo, true)

	for {
		in, err := command.GetInput()
		if err != nil {
			return err
		}
		input := strings.Fields(in)
		var cmd string
		args := []string{}
		if len(input) > 0 {
			cmd = input[0]
			args = input[1:]
		}

		switch cmd {
		case "add":
			command.Pick(repo, args)
			command.Lineup(repo, true)

		case "rm", "drop":
			command.UnPick(repo, args)
			command.Lineup(repo, true)

		case "exit":
			return errors.New("user canceled")

		case "floor", "fl":
			command.Lineup(repo, true)

		case "ceil":
			command.Lineup(repo, false)

		case "team":
			command.Team(repo, args)

		case "find", "f":
			command.Find(repo, args)

		case "help", "h", "usage":
			log.Println(teamUsage)

		case "waiver", "w":
			command.Floor(repo, args)

		case "":
			continue

		default:
			log.Printf("Unrecognized command %q. Type help for usage.\n", cmd)
		}
	}
}

func loadTeam(name string) (*players.Team, error) {
	t := &players.Team{Name: name}
	fname := t.Name + ".json"
	existing, err := os.ReadFile(fname)
	if err != nil {
		log.Printf("team file %q: %v; creating new team", fname, err)
		return t, nil
	}
	err = json.Unmarshal(existing, t)
	return t, errors.Wrap(err, fname)
}

func mustSaveTeam(t *players.Team, repo *players.Repo) {
	t.Sync(repo.Claimed)
	fname := t.Name + ".json"
	log.Println("saving team to", fname)
	b, err := json.Marshal(t)
	if err != nil {
		panic(err)
	}
	err = os.WriteFile(fname, b, 0660)
	if err != nil {
		panic(err)
	}
}

{'fraction_non_alphanumeric': 0.20593607305936074, 'fraction_numerical': 0.0024353120243531205, 'mean_word_length': 2.2920841683366735, 'pattern_counts': {'":': 19, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1472375', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2200, 'n_words': 600}