Skip to content

Commit

Permalink
add players flags
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusfm committed Dec 16, 2021
1 parent 2453fd4 commit be00a8b
Show file tree
Hide file tree
Showing 8 changed files with 485 additions and 95 deletions.
75 changes: 71 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ go install github.com/matheusfm/futbin@latest
# Usage

```
futbin command line tool
Usage:
futbin [command]
Expand All @@ -37,13 +35,82 @@ Available Commands:
totw current TOTW players
Flags:
--config string config file (default is $HOME/.futbin.yaml)
--config string Config file (default is $HOME/.futbin.yaml)
-h, --help help for futbin
--platform string platform (PS, XB or PC) (default "PS")
--platform string Platform (PS, XB or PC) (default "PS")
Use "futbin [command] --help" for more information about a command.
```

### `players` command examples:

```
Usage:
futbin players [flags]
Examples:
1. # Brazilian players in LaLiga:
futbin players --nation 54 --league 53
2. # OTW players (see options in 'futbin cardversions' command):
futbin players --version otw
3. # Brazilian players with more than 90 of passing:
futbin players --nation 54 --passing 90-
4. # Icons with a maximum price of 300K:
futbin players --league 2118 --price -300000
5. # Players with 5 weak foot and 5 skills:
futbin players --wf 5 --skills 5
Flags:
--acceleration string Acceleration
--aggression string Aggression
--agility string Agility
--balance string Balance
--ball-control string Ball Control
--club int Club ID
--composure string Composure
--crossing string Crossing
--curve string Curve
--defending string Defending
--dribbling string Dribbling
--finishing string Finishing
--free-kick-accuracy string Free Kick Accuracy
--heading-accuracy string Heading Accuracy
-h, --help help for players
--interceptions string Interceptions
--jumping string Jumping
--league int League ID
--long-passing string Long Passing
--long-shots string Long Shots
--marking string Marking
--nation int Nation ID
--ovr string Rating
--pace string Pace
--page int Page (default 1)
--passing string Passing
--penalties string Penalties
--physicality string Physicality
--positioning string Positioning
--price string Price
--reactions string Reactions
--shooting string Shooting
--short-passing string Short Passing
--shot-power string Shot Power
--skills string Skills
--sliding-tackle string Sliding Tackle
--sprint-speed string Sprint Speed
--stamina string Stamina
--standing-tackle string Standing Tackle
--strength string Strength
--version string Card version
--vision string Vision
--volleys string Volleys
--wf string Weak Foot
Global Flags:
--config string Config file (default is $HOME/.futbin.yaml)
--platform string Platform (PS, XB or PC) (default "PS")
```

## Usage (Library)

```go
Expand Down
6 changes: 2 additions & 4 deletions cmd/clubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ var clubsCmd = &cobra.Command{
cobra.CheckErr(err)
data := make([][]string, 1)
for _, league := range leagues {
// 0 -> percorre e adiciona
//
if fLeagueID != 0 && fLeagueID != league.ID {
if flagLeagueID != 0 && flagLeagueID != league.ID {
continue
}
for _, club := range league.Clubs {
Expand All @@ -41,5 +39,5 @@ var clubsCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(clubsCmd)
clubsCmd.PersistentFlags().IntVar(&fLeagueID, "league", 0, "league ID")
clubsCmd.PersistentFlags().IntVar(&flagLeagueID, "league", 0, "League ID")
}
189 changes: 169 additions & 20 deletions cmd/players.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"strconv"
"strings"

"github.com/dustin/go-humanize"
"github.com/fatih/color"
Expand All @@ -12,12 +13,50 @@ import (
)

var (
playersClient = players.DefaultClient()
fPage int
fVersion string
fClubID int
fNationID int
fLeagueID int
playersClient = players.DefaultClient()
flagPage int
flagVersion string
flagClubID int
flagNationID int
flagLeagueID int
flagPrice string
flagWeakFoot string
flagSkills string
flagRating string
flagPace string
flagAcceleration string
flagSprintSpeed string
flagShooting string
flagPositioning string
flagFinishing string
flagShotPower string
flagLongShots string
flagVolleys string
flagPenalties string
flagDribbling string
flagAgility string
flagBalance string
flagReactions string
flagBallControl string
flagComposure string
flagDefending string
flagInterceptions string
flagHeadingAccuracy string
flagMarking string
flagStandingTackle string
flagSlidingTackle string
flagPassing string
flagVision string
flagCrossing string
flagFreeKickAccuracy string
flagShortPassing string
flagLongPassing string
flagCurve string
flagPhysicality string
flagJumping string
flagStamina string
flagStrength string
flagAggression string
)

// playersCmd represents the players command
Expand All @@ -27,15 +66,59 @@ var playersCmd = &cobra.Command{
Example: `1. # Brazilian players in LaLiga:
futbin players --nation 54 --league 53
2. # OTW players (see options in 'futbin cardversions' command):
futbin players --version otw`,
futbin players --version otw
3. # Brazilian players with more than 90 of passing:
futbin players --nation 54 --passing 90-
4. # Icons with a maximum price of 300K:
futbin players --league 2118 --price -300000
5. # Players with 5 weak foot and 5 skills:
futbin players --wf 5 --skills 5`,
Run: func(cmd *cobra.Command, args []string) {
p, err := playersClient.Get(&players.Options{
Platform: platform,
Page: fPage,
Version: fVersion,
ClubID: fClubID,
NationID: fNationID,
LeagueID: fLeagueID,
Platform: platform,
Page: flagPage,
Version: flagVersion,
ClubID: flagClubID,
NationID: flagNationID,
LeagueID: flagLeagueID,
Price: flagToRange(flagPrice),
WeakFoot: flagToRange(flagWeakFoot),
Skills: flagToRange(flagSkills),
Rating: flagToRange(flagRating),
Pace: flagToRange(flagPace),
Acceleration: flagToRange(flagAcceleration),
SprintSpeed: flagToRange(flagSprintSpeed),
Shooting: flagToRange(flagShooting),
Positioning: flagToRange(flagPositioning),
Finishing: flagToRange(flagFinishing),
ShotPower: flagToRange(flagShotPower),
LongShots: flagToRange(flagLongShots),
Volleys: flagToRange(flagVolleys),
Penalties: flagToRange(flagPenalties),
Dribbling: flagToRange(flagDribbling),
Agility: flagToRange(flagAgility),
Balance: flagToRange(flagBalance),
Reactions: flagToRange(flagReactions),
BallControl: flagToRange(flagBallControl),
Composure: flagToRange(flagComposure),
Defending: flagToRange(flagDefending),
Interceptions: flagToRange(flagInterceptions),
HeadingAccuracy: flagToRange(flagHeadingAccuracy),
Marking: flagToRange(flagMarking),
StandingTackle: flagToRange(flagStandingTackle),
SlidingTackle: flagToRange(flagSlidingTackle),
Passing: flagToRange(flagPassing),
Vision: flagToRange(flagVision),
Crossing: flagToRange(flagCrossing),
FreeKickAccuracy: flagToRange(flagFreeKickAccuracy),
ShortPassing: flagToRange(flagShortPassing),
LongPassing: flagToRange(flagLongPassing),
Curve: flagToRange(flagCurve),
Physicality: flagToRange(flagPhysicality),
Jumping: flagToRange(flagJumping),
Stamina: flagToRange(flagStamina),
Strength: flagToRange(flagStrength),
Aggression: flagToRange(flagAggression),
})
cobra.CheckErr(err)
printPlayers(p)
Expand All @@ -44,12 +127,49 @@ var playersCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(playersCmd)

playersCmd.PersistentFlags().IntVar(&fPage, "page", 1, "page")
playersCmd.PersistentFlags().StringVar(&fVersion, "version", "", "card version")
playersCmd.PersistentFlags().IntVar(&fClubID, "club", 0, "club ID")
playersCmd.PersistentFlags().IntVar(&fNationID, "nation", 0, "nation ID")
playersCmd.PersistentFlags().IntVar(&fLeagueID, "league", 0, "league ID")
playersCmd.PersistentFlags().IntVar(&flagPage, "page", 1, "Page")
playersCmd.PersistentFlags().StringVar(&flagVersion, "version", "", "Card version")
playersCmd.PersistentFlags().IntVar(&flagClubID, "club", 0, "Club ID")
playersCmd.PersistentFlags().IntVar(&flagNationID, "nation", 0, "Nation ID")
playersCmd.PersistentFlags().IntVar(&flagLeagueID, "league", 0, "League ID")
playersCmd.PersistentFlags().StringVar(&flagWeakFoot, "wf", "", "Weak Foot")
playersCmd.PersistentFlags().StringVar(&flagSkills, "skills", "", "Skills")
playersCmd.PersistentFlags().StringVar(&flagRating, "ovr", "", "Rating")
playersCmd.PersistentFlags().StringVar(&flagPrice, "price", "", "Price")
playersCmd.PersistentFlags().StringVar(&flagPace, "pace", "", "Pace")
playersCmd.PersistentFlags().StringVar(&flagAcceleration, "acceleration", "", "Acceleration")
playersCmd.PersistentFlags().StringVar(&flagSprintSpeed, "sprint-speed", "", "Sprint Speed")
playersCmd.PersistentFlags().StringVar(&flagShooting, "shooting", "", "Shooting")
playersCmd.PersistentFlags().StringVar(&flagPositioning, "positioning", "", "Positioning")
playersCmd.PersistentFlags().StringVar(&flagFinishing, "finishing", "", "Finishing")
playersCmd.PersistentFlags().StringVar(&flagShotPower, "shot-power", "", "Shot Power")
playersCmd.PersistentFlags().StringVar(&flagLongShots, "long-shots", "", "Long Shots")
playersCmd.PersistentFlags().StringVar(&flagVolleys, "volleys", "", "Volleys")
playersCmd.PersistentFlags().StringVar(&flagPenalties, "penalties", "", "Penalties")
playersCmd.PersistentFlags().StringVar(&flagDribbling, "dribbling", "", "Dribbling")
playersCmd.PersistentFlags().StringVar(&flagAgility, "agility", "", "Agility")
playersCmd.PersistentFlags().StringVar(&flagBalance, "balance", "", "Balance")
playersCmd.PersistentFlags().StringVar(&flagReactions, "reactions", "", "Reactions")
playersCmd.PersistentFlags().StringVar(&flagBallControl, "ball-control", "", "Ball Control")
playersCmd.PersistentFlags().StringVar(&flagComposure, "composure", "", "Composure")
playersCmd.PersistentFlags().StringVar(&flagDefending, "defending", "", "Defending")
playersCmd.PersistentFlags().StringVar(&flagInterceptions, "interceptions", "", "Interceptions")
playersCmd.PersistentFlags().StringVar(&flagHeadingAccuracy, "heading-accuracy", "", "Heading Accuracy")
playersCmd.PersistentFlags().StringVar(&flagMarking, "marking", "", "Marking")
playersCmd.PersistentFlags().StringVar(&flagStandingTackle, "standing-tackle", "", "Standing Tackle")
playersCmd.PersistentFlags().StringVar(&flagSlidingTackle, "sliding-tackle", "", "Sliding Tackle")
playersCmd.PersistentFlags().StringVar(&flagPassing, "passing", "", "Passing")
playersCmd.PersistentFlags().StringVar(&flagVision, "vision", "", "Vision")
playersCmd.PersistentFlags().StringVar(&flagCrossing, "crossing", "", "Crossing")
playersCmd.PersistentFlags().StringVar(&flagFreeKickAccuracy, "free-kick-accuracy", "", "Free Kick Accuracy")
playersCmd.PersistentFlags().StringVar(&flagShortPassing, "short-passing", "", "Short Passing")
playersCmd.PersistentFlags().StringVar(&flagLongPassing, "long-passing", "", "Long Passing")
playersCmd.PersistentFlags().StringVar(&flagCurve, "curve", "", "Curve")
playersCmd.PersistentFlags().StringVar(&flagPhysicality, "physicality", "", "Physicality")
playersCmd.PersistentFlags().StringVar(&flagJumping, "jumping", "", "Jumping")
playersCmd.PersistentFlags().StringVar(&flagStamina, "stamina", "", "Stamina")
playersCmd.PersistentFlags().StringVar(&flagStrength, "strength", "", "Strength")
playersCmd.PersistentFlags().StringVar(&flagAggression, "aggression", "", "Aggression")
}

func printPlayers(p []players.Player) {
Expand Down Expand Up @@ -79,7 +199,7 @@ func printPlayers(p []players.Player) {
fmt.Sprintf("%s (%d)", nations.Nations[player.NationID], player.NationID),
fmt.Sprintf("%s (%d)", player.League, player.LeagueID),
player.Position,
humanize.Comma(int64(player.Price(platform))),
color.HiYellowString(humanize.Comma(int64(player.Price(platform)))),
colorStat(player.Pace),
colorStat(player.Shooting),
colorStat(player.Passing),
Expand All @@ -100,3 +220,32 @@ func colorStat(stat int) string {
}
return color.GreenString("%d", stat)
}

func flagToRange(s string) players.Range {
values := strings.SplitN(s, "-", 2)
minS := strings.ReplaceAll(values[0], ",", "") // to support comma separated prices
min, err := strconv.Atoi(minS)
minOK := err == nil
if !minOK && minS != "" {
return players.Range{} //invalid character
}
max := 0
maxOK := false
if len(values) > 1 {
maxS := strings.ReplaceAll(values[1], ",", "")
max, err = strconv.Atoi(maxS)
maxOK = err == nil
}
r := players.Range{}
if minOK {
r.Min = min
}
if maxOK {
r.Max = max
} else if !strings.Contains(s, "-") {
r.Max = min
} else if values[1] != "" {
return players.Range{} //invalid character
}
return r
}
35 changes: 35 additions & 0 deletions cmd/players_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"reflect"
"testing"

"github.com/matheusfm/futbin/players"
)

func TestFlagToRange(t *testing.T) {
tests := []struct {
name string
arg string
want players.Range
}{
{name: "exactly", arg: "90", want: players.Range{Min: 90, Max: 90}},
{name: "range", arg: "80-90", want: players.Range{Min: 80, Max: 90}},
{name: "invalid", arg: "foo", want: players.Range{}},
{name: "min invalid", arg: "foo-90", want: players.Range{}},
{name: "max invalid", arg: "90-bar", want: players.Range{}},
{name: "min", arg: "90-", want: players.Range{Min: 90}},
{name: "max", arg: "-99", want: players.Range{Max: 99}},
{name: "long numbers", arg: "1000000-10000000", want: players.Range{Min: 1000000, Max: 10000000}},
{name: "comma", arg: "10,000-100,000", want: players.Range{Min: 10000, Max: 100000}},
{name: "comma min", arg: "10,000-", want: players.Range{Min: 10000}},
{name: "comma max", arg: "-100,000", want: players.Range{Max: 100000}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := flagToRange(tt.arg); !reflect.DeepEqual(got, tt.want) {
t.Errorf("flagToRange() = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func Execute() {

func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.futbin.yaml)")
rootCmd.PersistentFlags().StringVar(&platform, "platform", "PS", "platform (PS, XB or PC)")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "Config file (default is $HOME/.futbin.yaml)")
rootCmd.PersistentFlags().StringVar(&platform, "platform", "PS", "Platform (PS, XB or PC)")
viper.BindPFlag("platform", rootCmd.PersistentFlags().Lookup("platform"))
}

Expand Down
3 changes: 2 additions & 1 deletion players/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type client struct {

func (c client) Get(opt *Options) ([]Player, error) {
u, _ := url.Parse("https://www.futbin.org/futbin/api/getFilteredPlayers")
q, err := query.Values(opt)
pq := newPlayerQuery(opt)
q, err := query.Values(pq)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit be00a8b

Please sign in to comment.