Skip to content

Commit

Permalink
tear down extracts methods
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Aug 22, 2022
1 parent 696c4bf commit 779ee4b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
16 changes: 16 additions & 0 deletions pkg/extractor/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ type ResearchExtractorBytesDoc interface {

type FacilitiesExtractorBytes interface {
ExtractFacilities(pageHTML []byte) (ogame.Facilities, error)
ExtractTearDownToken(pageHTML []byte) (string, error)
}

type FacilitiesExtractorDoc interface {
Expand Down Expand Up @@ -407,6 +408,7 @@ type MessagesMarketplaceExtractorBytes interface {
// ResourcesBuildingsExtractorBytes supplies page
type ResourcesBuildingsExtractorBytes interface {
ExtractResourcesBuildings(pageHTML []byte) (ogame.ResourcesBuildings, error)
ExtractTearDownToken(pageHTML []byte) (string, error)
}

type ResourcesBuildingsExtractorDoc interface {
Expand All @@ -427,6 +429,19 @@ type PlanetLayerExtractorDoc interface {
ExtractAbandonInformation(doc *goquery.Document) (abandonToken string, token string)
}

type TechnologyDetailsExtractorBytes interface {
ExtractTearDownButtonEnabled(pageHTML []byte) bool
}

type TechnologyDetailsExtractorDoc interface {
ExtractTearDownButtonEnabledFromDoc(doc *goquery.Document) bool
}

type TechnologyDetailsExtractorBytesDoc interface {
TechnologyDetailsExtractorBytes
TechnologyDetailsExtractorDoc
}

// Extractor ...
type Extractor interface {
GetLanguage() string
Expand Down Expand Up @@ -454,6 +469,7 @@ type Extractor interface {
ResourcesBuildingsExtractorBytesDoc
ResourcesSettingsExtractorBytesDoc
ShipyardExtractorBytesDoc
TechnologyDetailsExtractorBytesDoc

BuffActivationExtractorBytes
DestroyRocketsExtractorBytes
Expand Down
16 changes: 16 additions & 0 deletions pkg/extractor/v6/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func (e *Extractor) ExtractExpeditionMessagesFromDoc(doc *goquery.Document) ([]o
panic("implement me")
}

// ExtractTearDownButtonEnabled ...
func (e *Extractor) ExtractTearDownButtonEnabled(pageHTML []byte) bool {
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(pageHTML))
return e.ExtractTearDownButtonEnabledFromDoc(doc)
}

// ExtractLifeformEnabled ...
func (e *Extractor) ExtractLifeformEnabled(pageHTML []byte) bool {
return bytes.Contains(pageHTML, []byte(`lifeformEnabled":true`))
Expand Down Expand Up @@ -231,6 +237,11 @@ func (e *Extractor) ExtractFacilities(pageHTML []byte) (ogame.Facilities, error)
return e.ExtractFacilitiesFromDoc(doc)
}

// ExtractTearDownToken ...
func (e *Extractor) ExtractTearDownToken(pageHTML []byte) (string, error) {
return extractTearDownToken(pageHTML)
}

// ExtractResearch ...
func (e *Extractor) ExtractResearch(pageHTML []byte) ogame.Researches {
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(pageHTML))
Expand Down Expand Up @@ -370,6 +381,11 @@ func (e *Extractor) ExtractIsInVacationFromDoc(doc *goquery.Document) bool {
return extractIsInVacationFromDoc(doc)
}

// ExtractTearDownButtonEnabledFromDoc ...
func (e *Extractor) ExtractTearDownButtonEnabledFromDoc(doc *goquery.Document) bool {
return extractTearDownButtonEnabledFromDoc(doc)
}

// ExtractPlanetsFromDoc ...
func (e *Extractor) ExtractPlanetsFromDoc(doc *goquery.Document) []ogame.Planet {
return extractPlanetsFromDoc(doc)
Expand Down
12 changes: 12 additions & 0 deletions pkg/extractor/v6/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
"golang.org/x/net/html"
)

func extractTearDownButtonEnabledFromDoc(doc *goquery.Document) bool {
return !doc.Find("a.demolish_link div").HasClass("demolish_img_disabled")
}

func extractIsInVacationFromDoc(doc *goquery.Document) bool {
href := doc.Find("div#advice-bar a").AttrOr("href", "")
if href == "" {
Expand Down Expand Up @@ -1398,6 +1402,14 @@ func extractPlanetCoordinate(pageHTML []byte) (ogame.Coordinate, error) {
return ogame.Coordinate{galaxy, system, position, planetType}, nil
}

func extractTearDownToken(pageHTML []byte) (string, error) {
m := regexp.MustCompile(`modus=3&token=([^&]+)&`).FindSubmatch(pageHTML)
if len(m) != 2 {
return "", errors.New("unable to find tear down token")
}
return string(m[1]), nil
}

func extractPlanetID(pageHTML []byte) (ogame.CelestialID, error) {
m := regexp.MustCompile(`<meta name="ogame-planet-id" content="(\d+)"/>`).FindSubmatch(pageHTML)
if len(m) == 0 {
Expand Down
21 changes: 4 additions & 17 deletions pkg/wrapper/ogame.go
Original file line number Diff line number Diff line change
Expand Up @@ -2887,15 +2887,6 @@ func getToken(b *OGame, page string, celestialID ogame.CelestialID) (string, err
return string(m[1]), nil
}

func getDemolishToken(b *OGame, page string, celestialID ogame.CelestialID) (string, error) {
pageHTML, _ := b.getPage(page, ChangePlanet(celestialID))
m := regexp.MustCompile(`modus=3&token=([^&]+)&`).FindSubmatch(pageHTML)
if len(m) != 2 {
return "", errors.New("unable to find form token")
}
return string(m[1]), nil
}

func (b *OGame) tearDown(celestialID ogame.CelestialID, id ogame.ID) error {
var page string
if id.IsResourceBuilding() {
Expand All @@ -2906,12 +2897,13 @@ func (b *OGame) tearDown(celestialID ogame.CelestialID, id ogame.ID) error {
return errors.New("invalid id " + id.String())
}

token, err := getDemolishToken(b, page, celestialID)
pageHTML, _ := b.getPage(page, ChangePlanet(celestialID))
token, err := b.extractor.ExtractTearDownToken(pageHTML)
if err != nil {
return err
}

pageHTML, _ := b.getPageContent(url.Values{
pageHTML, _ = b.getPageContent(url.Values{
"page": {"ingame"},
"component": {"technologydetails"},
"ajax": {"1"},
Expand All @@ -2920,12 +2912,7 @@ func (b *OGame) tearDown(celestialID ogame.CelestialID, id ogame.ID) error {
"cp": {utils.FI64(celestialID)},
})

doc, err := goquery.NewDocumentFromReader(bytes.NewReader(pageHTML))
if err != nil {
return err
}
imgDisabled := doc.Find("a.demolish_link div").HasClass("demolish_img_disabled")
if imgDisabled {
if !b.extractor.ExtractTearDownButtonEnabled(pageHTML) {
return errors.New("tear down button is disabled")
}

Expand Down

0 comments on commit 779ee4b

Please sign in to comment.