From ed04e5c6eb6d391f13d2541ce26c5cd18811b912 Mon Sep 17 00:00:00 2001 From: Kevin Blake Date: Sat, 30 Sep 2017 10:48:24 +0100 Subject: [PATCH 1/3] Include option for users ship roster --- swgohgg/ships.go | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ swgohgg/sort.go | 24 ++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 swgohgg/ships.go diff --git a/swgohgg/ships.go b/swgohgg/ships.go new file mode 100644 index 0000000..bb5fbc0 --- /dev/null +++ b/swgohgg/ships.go @@ -0,0 +1,81 @@ +package swgohgg + +import ( + "fmt" + "sort" + "strconv" + + "github.com/PuerkitoBio/goquery" +) + +func (c *Client) Ships() (ships Ships, err error) { + url := fmt.Sprintf("https://swgoh.gg/u/%s/ships/", c.profile) + doc, err := c.Get(url) + if err != nil { + return nil, err + } + doc.Find(".collection-char-list .collection-ship").Each(func(i int, s *goquery.Selection) { + ship := parseShip(s) + if !ships.Contains(ship.Name) { + ships = append(ships, ship) + } + }) + sort.Sort(ByShipStars(ships, false)) + return ships, nil +} + +type Ships []*Ship + +func (r Ships) Contains(ship string) bool { + for i := range r { + if r[i].Name == ship { + return true + } + } + return false +} + +func (r Ships) ContainsAll(ships ...string) bool { + for _, ship := range ships { + if !r.Contains(ship) { + return false + } + } + return true +} + + +type Ship struct { + Name string + Stars int + Level int +} + +func (c *Ship) String() string { + if c == nil { + return "nil" + } + return fmt.Sprintf("%s %d* Lvl%d", c.Name, c.Stars, c.Level) +} + +func parseShip(s *goquery.Selection) *Ship { + var ship Ship + ship.Name = s.Find(".collection-ship-name-link").Text() + + ship.Level, _ = strconv.Atoi(s.Find(".ship-portrait-full-frame-level").Text()) + ship.Stars = shipStars(s) + return &ship +} + +func shipStars(s *goquery.Selection) int { + level := 0 + s.Find(".ship-portrait-full-star").Each(func(i int, star *goquery.Selection) { + if star.HasClass("ship-portrait-full-star-inactive") { + return + } + level++ + }) + return level +} + + diff --git a/swgohgg/sort.go b/swgohgg/sort.go index 035bd2b..7c1679c 100644 --- a/swgohgg/sort.go +++ b/swgohgg/sort.go @@ -23,6 +23,30 @@ func (bs sortByStars) Less(i, j int) bool { return bs.chars[i].Stars > bs.chars[j].Stars } +type sortByShipStars struct { + ships []*Ship + asc bool +} + +func ByShipStars(ships []*Ship, ascending bool) sortByShipStars { + return sortByShipStars{ + ships: ships, + asc: ascending, + } +} + +func (bs sortByShipStars) Len() int { return len(bs.ships) } + +func (bs sortByShipStars) Swap(i, j int) { bs.ships[i], bs.ships[j] = bs.ships[j], bs.ships[i] } + +func (bs sortByShipStars) Less(i, j int) bool { + if bs.asc { + return bs.ships[i].Stars < bs.ships[j].Stars + } + return bs.ships[i].Stars > bs.ships[j].Stars +} + + type sortByShape struct { mods []*Mod asc bool From f2504777255e162d4dc00b1dc8ff2f3818506c8c Mon Sep 17 00:00:00 2001 From: Kevin Blake Date: Sat, 30 Sep 2017 10:51:38 +0100 Subject: [PATCH 2/3] Add ships to the cmd client --- cmd/swgoh/main.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/cmd/swgoh/main.go b/cmd/swgoh/main.go index 1896285..afdc95f 100644 --- a/cmd/swgoh/main.go +++ b/cmd/swgoh/main.go @@ -16,6 +16,7 @@ var ( maxStat string shape string showCollection bool + showShips bool showMods bool useCache bool ) @@ -25,6 +26,7 @@ func init() { // Operation flags flag.BoolVar(&showCollection, "collection", false, "Show user character collection") + flag.BoolVar(&showShips, "ships", false, "Show user ships collection") flag.BoolVar(&showMods, "mods", false, "Show user mods collection") // Cache flags @@ -57,6 +59,25 @@ func fetchCollection(swgg *swgohgg.Client) (collection swgohgg.Collection, err e return collection, nil } +func fetchShips(swgg *swgohgg.Client) (ships swgohgg.Ships, err error) { + log.Printf("Fetching ships ...") + ships = make(swgohgg.Ships, 0) + err = loadCache("ships", &ships) + if err != nil { + log.Printf("Data not cached, loading from website (%v)", err) + ships, err = swgg.Ships() + if err != nil { + log.Fatal(err) + } + if useCache { + if err = saveCache("ships", &ships); err != nil { + log.Printf("Can't save to cache: %v", err) + } + } + } + return ships, nil +} + var modFilterAll = swgohgg.ModFilter{} func fetchMods(swgg *swgohgg.Client) (mods swgohgg.ModCollection, err error) { @@ -93,6 +114,18 @@ func main() { } } + if showShips { + ships, err := fetchShips(swgg) + if err != nil { + log.Fatal(err) + } + for _, ship := range ships { + if ship.Stars >= starLevel { + fmt.Println(ship) + } + } + } + if showMods { mods, err := fetchMods(swgg) if err != nil { From 686c619a19801987293fe50e5f0e91185da9cbc8 Mon Sep 17 00:00:00 2001 From: Kevin Blake Date: Sat, 30 Sep 2017 10:53:55 +0100 Subject: [PATCH 3/3] Include ships details in README.md --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 3d2cdd0..5a7cbaa 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,16 @@ To see the full list of available options, run: swgoh --help +### Ships list + +To list your ships, you can use the following +command: + + swgoh -profile ronoaldo -ships + +The result is a CSV list of your ships, their +level, and stars. + ### Character list To list your characters, you can use the following