Skip to content

Commit

Permalink
Added new command to load mods.
Browse files Browse the repository at this point in the history
  • Loading branch information
ronoaldo committed Dec 17, 2016
1 parent 4669475 commit c207107
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 15 deletions.
34 changes: 25 additions & 9 deletions cmd/swgoh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,41 @@ import (
)

var (
profile string
starLevel int
profile string
starLevel int
showRoster bool
showMods bool
)

func init() {
flag.BoolVar(&showRoster, "roster", false, "Show user character collection")
flag.BoolVar(&showMods, "mods", false, "Show user mods collection")
flag.StringVar(&profile, "profile", "", "The user `profile` on https://swgoh.gg/")
flag.IntVar(&starLevel, "stars", 0, "The minimal `character stars` to filter")
}

func main() {
flag.Parse()

roster, err := swgohgg.NewClient().Roster(profile)
if err != nil {
log.Fatal(err)
swgg := swgohgg.NewClient(profile)
if showRoster {
roster, err := swgg.Roster()
if err != nil {
log.Fatal(err)
}
for _, char := range roster {
if char.Stars >= starLevel {
fmt.Println(char)
}
}
}
for _, char := range roster {
if char.Stars >= starLevel {
fmt.Println(char)

if showMods {
mods, err := swgg.Mods()
if err != nil {
log.Fatal(err)
}
for _, mod := range mods {
fmt.Println(mod)
}
}
}
13 changes: 10 additions & 3 deletions swgohgg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ import (
)

type Client struct {
hc *http.Client
hc *http.Client
profile string
}

func NewClient() *Client {
func NewClient(profile string) *Client {
return &Client{
hc: http.DefaultClient,
hc: http.DefaultClient,
profile: profile,
}
}

func (c *Client) UseHTTPClient(hc *http.Client) *Client {
c.hc = hc
return c
}

func (c *Client) Profile(profile string) *Client {
c.profile = profile
return c
}
153 changes: 153 additions & 0 deletions swgohgg/mods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package swgohgg

import (
"fmt"
"github.com/PuerkitoBio/goquery"
"log"
"strconv"
"strings"
)

type Mod struct {
ID string
Level int
Rarity int
Shape string
BonusSet string

PrimStat ModStat
SecStat []ModStat

UsingIn string
}

func (m *Mod) String() string {
if m == nil {
return "nil mod"
}
str := fmt.Sprintf("%s %-18s L%d %d* %v %v", m.ShapeIcon(), m.BonusSet, m.Level, m.Rarity, m.PrimStat, m.SecStat)
if m.UsingIn != "" {
str += " (" + m.UsingIn + ")"
}
return str
}

func (m *Mod) ShapeIcon() string {
switch m.Shape {
case "Transmitter":
return "◻"
case "Processor":
return "◇"
case "Holo-Array":
return "△"
case "Data-Bus":
return "○"
case "Receiver":
return "◹"
case "Multiplexer":
return "+"
default:
return m.Shape
}
}

func (m *Mod) ShapeName() string {
switch m.Shape {
case "Transmitter":
return "Quadrado "
case "Processor":
return "Losango "
case "Holo-Array":
return "Triangulo"
case "Data-Bus":
return "Circulo "
case "Receiver":
return "Seta "
case "Multiplexer":
return "Cruz "
default:
return m.Shape
}
}

type ModStat struct {
Type string
Value float64
ValuePercent bool
}

func (ms ModStat) String() string {
if ms.ValuePercent {
return fmt.Sprintf("%.02f%% %s", ms.Value, ms.Type)
}
return fmt.Sprintf("%.02f %s", ms.Value, ms.Type)
}

type ModCollection []*Mod

func (c *Client) Mods() (mods ModCollection, err error) {
url := fmt.Sprintf("https://swgoh.gg/u/%s/mods/", c.profile)
resp, err := c.hc.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, err
}
doc.Find(".collection-mod").Each(func(i int, s *goquery.Selection) {
mod := parseMod(s)
mods = append(mods, mod)
})
return mods, nil
}

func parseMod(s *goquery.Selection) *Mod {
var err error
mod := &Mod{}
mod.ID = s.AttrOr("data-id", "")
mod.Level, err = strconv.Atoi(s.Find(".statmod-level").Text())
if err != nil {
log.Println("Error: %v", err)
}
mod.Rarity = s.Find(".statmod-pip").Length()
shortname := strings.Fields(s.Find(".statmod-img").AttrOr("alt", "!Unkown!"))
switch len(shortname) {
case 4:
mod.BonusSet = shortname[2]
mod.Shape = shortname[3]
case 5:
mod.BonusSet = shortname[2] + " " + shortname[3]
mod.Shape = shortname[4]
default:
mod.BonusSet = "?"
mod.Shape = "?"
}

// Primary stat
mod.PrimStat = parseStat(s.Find(".statmod-stats-1 .statmod-stat"))
// Secondary stats
s.Find(".statmod-stats-2 .statmod-stat").Each(func(i int, stat *goquery.Selection) {
mod.SecStat = append(mod.SecStat, parseStat(stat))
})

mod.UsingIn = s.Find("img.char-portrait-img").AttrOr("alt", "")
return mod
}

func parseStat(s *goquery.Selection) (stat ModStat) {
stat.Type = s.Find(".statmod-stat-label").Text()

strvalue := s.Find(".statmod-stat-value").Text()
strvalue = strings.Replace(strvalue, "%", "", -1)
strvalue = strings.Replace(strvalue, "+", "", -1)

var err error
stat.Value, err = strconv.ParseFloat(strvalue, 64)
if err != nil {
log.Printf("parsestat: invalid value %s", s.Find(".statmod-stat-value").Text())
}
stat.ValuePercent = strings.Contains(s.Find(".statmod-stat-value").Text(), "%")
return stat
}
29 changes: 26 additions & 3 deletions swgohgg/roster.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strconv"
)

func (c *Client) Roster(profile string) (roster []*Char, err error) {
url := fmt.Sprintf("https://swgoh.gg/u/%s/collection/", profile)
func (c *Client) Roster() (roster Roster, err error) {
url := fmt.Sprintf("https://swgoh.gg/u/%s/collection/", c.profile)
resp, err := c.hc.Get(url)
if err != nil {
return nil, err
Expand All @@ -30,6 +30,26 @@ func (c *Client) Roster(profile string) (roster []*Char, err error) {
return roster, nil
}

type Roster []*Char

func (r Roster) Contains(char string) bool {
for i := range r {
if r[i].Name == char {
return true
}
}
return false
}

func (r Roster) ContainsAll(chars ...string) bool {
for _, char := range chars {
if !r.Contains(char) {
return false
}
}
return true
}

type Char struct {
Name string
Stars int
Expand All @@ -38,7 +58,10 @@ type Char struct {
}

func (c *Char) String() string {
return fmt.Sprintf("%s %d* G%d", c.Name, c.Stars, c.Gear)
if c == nil {
return "nil"
}
return fmt.Sprintf("%s %d* G%d Lvl%d", c.Name, c.Stars, c.Gear, c.Level)
}

func parseChar(s *goquery.Selection) *Char {
Expand Down
9 changes: 9 additions & 0 deletions swgohgg/vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package swgohgg

import (
"fmt"
)

var (
errNotImplemented = fmt.Errorf("swgohgg: not implemented")
)

0 comments on commit c207107

Please sign in to comment.