Skip to content

Commit

Permalink
Merge pull request #4 from kevinblake/master
Browse files Browse the repository at this point in the history
Include option for users ship roster
  • Loading branch information
ronoaldo authored Oct 1, 2017
2 parents 090638b + 686c619 commit 414196c
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions cmd/swgoh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
maxStat string
shape string
showCollection bool
showShips bool
showMods bool
showStats bool
useCache bool
Expand All @@ -28,6 +29,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")
flag.BoolVar(&showStats, "stats", false, "Show a single character stats (requires -char)")

Expand Down Expand Up @@ -61,6 +63,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) {
Expand Down Expand Up @@ -115,6 +136,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 {
Expand Down
81 changes: 81 additions & 0 deletions swgohgg/ships.go
Original file line number Diff line number Diff line change
@@ -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
}


24 changes: 24 additions & 0 deletions swgohgg/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 414196c

Please sign in to comment.