Skip to content

Commit

Permalink
Fix fleets resources (lifeform)
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Sep 24, 2022
1 parent 5537c50 commit a5552d3
Show file tree
Hide file tree
Showing 4 changed files with 1,789 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/extractor/v6/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ func (e *Extractor) ExtractFleetsFromDoc(doc *goquery.Document) (res []ogame.Fle
}

func (e *Extractor) extractFleetsFromDoc(doc *goquery.Document, location *time.Location) (res []ogame.Fleet) {
return extractFleetsFromDoc(doc, location)
return extractFleetsFromDoc(doc, location, e.lifeformEnabled)
}

// ExtractSlotsFromDoc extract fleet slots from page "fleet1"
Expand Down
16 changes: 12 additions & 4 deletions pkg/extractor/v6/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ func extractIPMFromDoc(doc *goquery.Document) (duration, max int64, token string
return
}

func extractFleetsFromDoc(doc *goquery.Document, location *time.Location) (res []ogame.Fleet) {
func extractFleetsFromDoc(doc *goquery.Document, location *time.Location, lifeformEnabled bool) (res []ogame.Fleet) {
res = make([]ogame.Fleet, 0)
script := doc.Find("body script").Text()
doc.Find("div.fleetDetails").Each(func(i int, s *goquery.Selection) {
Expand Down Expand Up @@ -1134,9 +1134,17 @@ func extractFleetsFromDoc(doc *goquery.Document, location *time.Location) (res [

trs := s.Find("table.fleetinfo tr")
shipment := ogame.Resources{}
shipment.Metal = utils.ParseInt(trs.Eq(trs.Size() - 3).Find("td").Eq(1).Text())
shipment.Crystal = utils.ParseInt(trs.Eq(trs.Size() - 2).Find("td").Eq(1).Text())
shipment.Deuterium = utils.ParseInt(trs.Eq(trs.Size() - 1).Find("td").Eq(1).Text())
metalTrOffset := 3
crystalTrOffset := 2
DeuteriumTrOffset := 1
if lifeformEnabled {
metalTrOffset = 4
crystalTrOffset = 3
DeuteriumTrOffset = 2
}
shipment.Metal = utils.ParseInt(trs.Eq(trs.Size() - metalTrOffset).Find("td").Eq(1).Text())
shipment.Crystal = utils.ParseInt(trs.Eq(trs.Size() - crystalTrOffset).Find("td").Eq(1).Text())
shipment.Deuterium = utils.ParseInt(trs.Eq(trs.Size() - DeuteriumTrOffset).Find("td").Eq(1).Text())

fedAttackHref := s.Find("span.fedAttack a").AttrOr("href", "")
fedAttackURL, _ := url.Parse(fedAttackHref)
Expand Down
11 changes: 11 additions & 0 deletions pkg/extractor/v9/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,14 @@ func TestExtractUserInfos(t *testing.T) {
assert.Equal(t, int64(1102), info.Rank)
assert.Equal(t, int64(2931), info.Total)
}

func TestExtractFleetResources(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("../../../samples/v9.0.4/en/lifeform/movement.html")
e := NewExtractor()
e.SetLocation(time.FixedZone("OGT", 3600))
e.SetLifeformEnabled(true)
fleets := e.ExtractFleets(pageHTMLBytes)
assert.Equal(t, int64(1), fleets[0].Resources.Metal)
assert.Equal(t, int64(2), fleets[0].Resources.Crystal)
assert.Equal(t, int64(3), fleets[0].Resources.Deuterium)
}
Loading

0 comments on commit a5552d3

Please sign in to comment.