Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spanish support fix #2

Merged
merged 1 commit into from
Aug 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions ogame.go
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ func (b *OGame) getAttacks() []AttackEvent {
return extractAttacks(pageHTML)
}

func extractGalaxyInfos(pageHTML string) ([]PlanetInfos, error) {
func extractGalaxyInfos(pageHTML, lang string) ([]PlanetInfos, error) {
var tmp struct {
Galaxy string
}
Expand All @@ -1399,12 +1399,24 @@ func extractGalaxyInfos(pageHTML string) ([]PlanetInfos, error) {
planetImg, _ := planetTooltip.Find("img").Attr("src")
coordsRaw := planetTooltip.Find("span#pos-planet").Text()

var metalRgx *regexp.Regexp
var crystalRgx *regexp.Regexp
var recyclersRgx *regexp.Regexp

switch lang {
case "en":
metalRgx = regexp.MustCompile(`Metal: ([\d.]+)`)
crystalRgx = regexp.MustCompile(`Crystal: ([\d.]+)`)
recyclersRgx = regexp.MustCompile(`Recyclers needed: ([\d.]+)`)
case "es":
metalRgx = regexp.MustCompile(`Metal: ([\d.]+)`)
crystalRgx = regexp.MustCompile(`Cristal: ([\d.]+)`)
recyclersRgx = regexp.MustCompile(`Se necesitan recicladores: ([\d.]+)`)
}

metalTxt := s.Find("div#debris" + position + " ul.ListLinks li").First().Text()
metalRgx := regexp.MustCompile(`Metal: ([\d.]+)`)
crystalTxt := s.Find("div#debris" + position + " ul.ListLinks li").Eq(1).Text()
crystalRgx := regexp.MustCompile(`Crystal: ([\d.]+)`)
recyclersTxt := s.Find("div#debris" + position + " ul.ListLinks li").Eq(2).Text()
recyclersRgx := regexp.MustCompile(`Recyclers needed: ([\d.]+)`)

planetInfos := PlanetInfos{}

Expand Down Expand Up @@ -1478,7 +1490,7 @@ func (b *OGame) galaxyInfos(galaxy, system int) ([]PlanetInfos, error) {
defer resp.Body.Close()
by, _ := ioutil.ReadAll(resp.Body)
pageHTML := string(by)
return extractGalaxyInfos(pageHTML)
return extractGalaxyInfos(pageHTML, b.language)
}

func (b *OGame) getResourceSettings(planetID PlanetID) (ResourceSettings, error) {
Expand Down
23 changes: 16 additions & 7 deletions ogame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestExtractAttacks1(t *testing.T) {

func TestExtractGalaxyInfos(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("samples/galaxy_ajax.html")
infos, _ := extractGalaxyInfos(string(pageHTMLBytes))
infos, _ := extractGalaxyInfos(string(pageHTMLBytes), "en")
assert.Equal(t, 5, len(infos))
}

Expand Down Expand Up @@ -137,39 +137,48 @@ func TestExtractPlanets_fr(t *testing.T) {

func TestExtractGalaxyInfos_honorableTarget(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("samples/galaxy_debris.html")
infos, _ := extractGalaxyInfos(string(pageHTMLBytes))
infos, _ := extractGalaxyInfos(string(pageHTMLBytes), "en")
assert.False(t, infos[0].HonorableTarget)
assert.True(t, infos[1].HonorableTarget)
assert.False(t, infos[2].HonorableTarget)
}

func TestExtractGalaxyInfos_inactive(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("samples/galaxy_debris.html")
infos, _ := extractGalaxyInfos(string(pageHTMLBytes))
infos, _ := extractGalaxyInfos(string(pageHTMLBytes), "en")
assert.True(t, infos[0].Inactive)
assert.False(t, infos[1].Inactive)
assert.False(t, infos[2].Inactive)
}

func TestExtractGalaxyInfos_strongPlayer(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("samples/galaxy_debris.html")
infos, _ := extractGalaxyInfos(string(pageHTMLBytes))
infos, _ := extractGalaxyInfos(string(pageHTMLBytes), "en")
assert.False(t, infos[0].StrongPlayer)
assert.True(t, infos[1].StrongPlayer)
assert.False(t, infos[2].StrongPlayer)
}

func TestExtractGalaxyInfos_debris(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("samples/galaxy_debris.html")
infos, _ := extractGalaxyInfos(string(pageHTMLBytes))
infos, _ := extractGalaxyInfos(string(pageHTMLBytes), "en")
assert.Equal(t, 0, infos[0].Debris.Metal)
assert.Equal(t, 700, infos[0].Debris.Crystal)
assert.Equal(t, 1, infos[0].Debris.RecyclersNeeded)
}


func TestExtractGalaxyInfos_debris_es(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("samples/galaxy_debris_es.html")
infos, _ := extractGalaxyInfos(string(pageHTMLBytes), "es")
assert.Equal(t, 0, infos[5].Debris.Metal)
assert.Equal(t, 128000, infos[5].Debris.Crystal)
assert.Equal(t, 7, infos[5].Debris.RecyclersNeeded)
}

func TestExtractGalaxyInfos_vacation(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("samples/galaxy_ajax.html")
infos, _ := extractGalaxyInfos(string(pageHTMLBytes))
infos, _ := extractGalaxyInfos(string(pageHTMLBytes), "en")
assert.Equal(t, 5, len(infos))
assert.False(t, infos[0].Vacation)
assert.True(t, infos[1].Vacation)
Expand All @@ -180,7 +189,7 @@ func TestExtractGalaxyInfos_vacation(t *testing.T) {

func TestExtractGalaxyInfos_alliance(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("samples/galaxy_ajax.html")
infos, _ := extractGalaxyInfos(string(pageHTMLBytes))
infos, _ := extractGalaxyInfos(string(pageHTMLBytes), "en")
assert.Equal(t, 303, infos[3].Alliance.ID)
assert.Equal(t, "Qrvix", infos[3].Alliance.Name)
assert.Equal(t, 27, infos[3].Alliance.Rank)
Expand Down
1 change: 1 addition & 0 deletions samples/galaxy_debris_es.html

Large diffs are not rendered by default.