Skip to content

Commit

Permalink
Merge branch 'develop-v904-GetLfBuildings' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	pkg/extractor/v9/extractor_test.go
  • Loading branch information
alaingilbert committed Sep 24, 2022
2 parents a5552d3 + 4238ba3 commit 40d77f8
Show file tree
Hide file tree
Showing 19 changed files with 1,799 additions and 51 deletions.
13 changes: 10 additions & 3 deletions pkg/extractor/extractor.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package extractor

import (
"net/url"
"time"

"github.com/PuerkitoBio/goquery"
v6 "github.com/alaingilbert/ogame/pkg/extractor/v6"
v7 "github.com/alaingilbert/ogame/pkg/extractor/v7"
v9 "github.com/alaingilbert/ogame/pkg/extractor/v9"
"github.com/alaingilbert/ogame/pkg/ogame"
"net/url"
"time"
)

type FullPageExtractorBytes interface {
Expand Down Expand Up @@ -355,7 +356,7 @@ type TraderImportExportExtractorDoc interface {

// FetchTechsExtractorBytes ajax page fetchTechs
type FetchTechsExtractorBytes interface {
ExtractTechs(pageHTML []byte) (ogame.ResourcesBuildings, ogame.Facilities, ogame.ShipsInfos, ogame.DefensesInfos, ogame.Researches, error)
ExtractTechs(pageHTML []byte) (ogame.ResourcesBuildings, ogame.Facilities, ogame.ShipsInfos, ogame.DefensesInfos, ogame.Researches, ogame.LfBuildings, error)
}

type ResourcesSettingsExtractorBytes interface {
Expand Down Expand Up @@ -410,10 +411,16 @@ type MessagesMarketplaceExtractorBytes interface {

type LfBuildingsExtractorBytes interface {
ExtractUpgradeToken(pageHTML []byte) (string, error)
ExtractLfBuildings(pageHTML []byte) (ogame.LfBuildings, error)
}

type LfBuildingsExtractorDoc interface {
ExtractLfBuildingsFromDoc(doc *goquery.Document) (ogame.LfBuildings, error)
}

type LfBuildingsExtractorBytesDoc interface {
LfBuildingsExtractorBytes
LfBuildingsExtractorDoc
}

// ResourcesBuildingsExtractorBytes supplies page
Expand Down
15 changes: 13 additions & 2 deletions pkg/extractor/v6/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package v6
import (
"bytes"
"errors"
"github.com/alaingilbert/ogame/pkg/ogame"
"net/url"
"time"

"github.com/alaingilbert/ogame/pkg/ogame"

"github.com/PuerkitoBio/goquery"
"github.com/alaingilbert/clockwork"
)
Expand Down Expand Up @@ -57,7 +58,7 @@ func (e *Extractor) ExtractPremiumToken(pageHTML []byte, days int64) (string, er
}

// ExtractTechs ...
func (e *Extractor) ExtractTechs(pageHTML []byte) (ogame.ResourcesBuildings, ogame.Facilities, ogame.ShipsInfos, ogame.DefensesInfos, ogame.Researches, error) {
func (e *Extractor) ExtractTechs(pageHTML []byte) (ogame.ResourcesBuildings, ogame.Facilities, ogame.ShipsInfos, ogame.DefensesInfos, ogame.Researches, ogame.LfBuildings, error) {
panic("implement me")
}

Expand Down Expand Up @@ -882,3 +883,13 @@ func (e *Extractor) ExtractIsMobile(pageHTML []byte) bool {
func (e *Extractor) ExtractIsMobileFromDoc(doc *goquery.Document) bool {
panic("not implemented")
}

// ExtractLfBuildings ...
func (e *Extractor) ExtractLfBuildings(pageHTML []byte) (ogame.LfBuildings, error) {
panic("not implemented")
}

// ExtractLfBuildingsFromDoc ...
func (e *Extractor) ExtractLfBuildingsFromDoc(doc *goquery.Document) (ogame.LfBuildings, error) {
panic("not implemented")
}
5 changes: 3 additions & 2 deletions pkg/extractor/v71/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package v71

import (
"bytes"

"github.com/PuerkitoBio/goquery"
"github.com/alaingilbert/clockwork"
"github.com/alaingilbert/ogame/pkg/extractor/v7"
v7 "github.com/alaingilbert/ogame/pkg/extractor/v7"
"github.com/alaingilbert/ogame/pkg/ogame"
)

Expand Down Expand Up @@ -46,7 +47,7 @@ func (e *Extractor) ExtractResourcesDetails(pageHTML []byte) (out ogame.Resource
}

// ExtractTechs ...
func (e *Extractor) ExtractTechs(pageHTML []byte) (ogame.ResourcesBuildings, ogame.Facilities, ogame.ShipsInfos, ogame.DefensesInfos, ogame.Researches, error) {
func (e *Extractor) ExtractTechs(pageHTML []byte) (ogame.ResourcesBuildings, ogame.Facilities, ogame.ShipsInfos, ogame.DefensesInfos, ogame.Researches, ogame.LfBuildings, error) {
return extractTechs(pageHTML)
}

Expand Down
41 changes: 35 additions & 6 deletions pkg/extractor/v71/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/alaingilbert/ogame/pkg/extractor/v6"
"github.com/alaingilbert/ogame/pkg/extractor/v7"
"github.com/alaingilbert/ogame/pkg/ogame"
"github.com/alaingilbert/ogame/pkg/utils"
"math"
"regexp"
"strconv"
"strings"
"time"

v6 "github.com/alaingilbert/ogame/pkg/extractor/v6"
v7 "github.com/alaingilbert/ogame/pkg/extractor/v7"
"github.com/alaingilbert/ogame/pkg/ogame"
"github.com/alaingilbert/ogame/pkg/utils"

"github.com/PuerkitoBio/goquery"
"github.com/alaingilbert/clockwork"
"golang.org/x/net/html"
Expand Down Expand Up @@ -249,13 +250,27 @@ type planetTechsResp struct {
Num408 int64 `json:"408"`
Num502 int64 `json:"502"`
Num503 int64 `json:"503"`

// LFbuildings
Num11101 int64 `json:"11101"`
Num11102 int64 `json:"11102"`
Num11103 int64 `json:"11103"`
Num11104 int64 `json:"11104"`
Num11105 int64 `json:"11105"`
Num11106 int64 `json:"11106"`
Num11107 int64 `json:"11107"`
Num11108 int64 `json:"11108"`
Num11109 int64 `json:"11109"`
Num11110 int64 `json:"11110"`
Num11111 int64 `json:"11111"`
Num11112 int64 `json:"11112"`
}

func extractTechs(pageHTML []byte) (supplies ogame.ResourcesBuildings, facilities ogame.Facilities, shipsInfos ogame.ShipsInfos, defenses ogame.DefensesInfos, researches ogame.Researches, err error) {
func extractTechs(pageHTML []byte) (supplies ogame.ResourcesBuildings, facilities ogame.Facilities, shipsInfos ogame.ShipsInfos, defenses ogame.DefensesInfos, researches ogame.Researches, lfBuildings ogame.LfBuildings, err error) {
var res planetTechsResp
if err = json.Unmarshal(pageHTML, &res); err != nil {
if v6.IsLogged(pageHTML) {
return supplies, facilities, shipsInfos, defenses, researches, ogame.ErrInvalidPlanetID
return supplies, facilities, shipsInfos, defenses, researches, lfBuildings, ogame.ErrInvalidPlanetID
}
return
}
Expand Down Expand Up @@ -332,6 +347,20 @@ func extractTechs(pageHTML []byte) (supplies ogame.ResourcesBuildings, facilitie
ShieldingTechnology: res.Num110,
ArmourTechnology: res.Num111,
}
lfBuildings = ogame.LfBuildings{
ResidentialSector: res.Num11101,
BiosphereFarm: res.Num11102,
ResearchCentre: res.Num11103,
AcademyOfSciences: res.Num11104,
NeuroCalibrationCentre: res.Num11105,
HighEnergySmelting: res.Num11106,
FoodSilo: res.Num11107,
FusionPoweredProduction: res.Num11108,
Skyscraper: res.Num11109,
BiotechLab: res.Num11110,
Metropolis: res.Num11111,
PlanetaryShield: res.Num11112,
}
return
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/extractor/v9/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,24 @@ func (e *Extractor) ExtractConstructions(pageHTML []byte) (buildingID ogame.ID,
return ExtractConstructions(pageHTML, clockwork.NewRealClock())
}

// ExtractResourceSettings ...
func (e *Extractor) ExtractResourceSettings(pageHTML []byte) (ogame.ResourceSettings, string, error) {
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(pageHTML))
return e.ExtractResourceSettingsFromDoc(doc)
}

// ExtractResourceSettingsFromDoc ...
func (e *Extractor) ExtractResourceSettingsFromDoc(doc *goquery.Document) (ogame.ResourceSettings, string, error) {
return extractResourceSettingsFromDoc(doc)
}

// ExtractLfBuildings ...
func (e *Extractor) ExtractLfBuildings(pageHTML []byte) (ogame.LfBuildings, error) {
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(pageHTML))
return e.ExtractLfBuildingsFromDoc(doc)
}

// ExtractLfBuildingsFromDoc ...
func (e *Extractor) ExtractLfBuildingsFromDoc(doc *goquery.Document) (ogame.LfBuildings, error) {
return extractLfBuildingsFromDoc(doc)
}
17 changes: 17 additions & 0 deletions pkg/extractor/v9/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,20 @@ func TestExtractFleetResources(t *testing.T) {
assert.Equal(t, int64(2), fleets[0].Resources.Crystal)
assert.Equal(t, int64(3), fleets[0].Resources.Deuterium)
}

func TestExtractLfBuildings(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("../../../samples/v9.0.4/en/lfbuildings.html")
res, _ := NewExtractor().ExtractLfBuildings(pageHTMLBytes)
assert.Equal(t, int64(2), res.ResidentialSector)
assert.Equal(t, int64(1), res.BiosphereFarm)
assert.Equal(t, int64(0), res.ResearchCentre)
assert.Equal(t, int64(0), res.AcademyOfSciences)
assert.Equal(t, int64(0), res.NeuroCalibrationCentre)
assert.Equal(t, int64(0), res.HighEnergySmelting)
assert.Equal(t, int64(0), res.FoodSilo)
assert.Equal(t, int64(0), res.FusionPoweredProduction)
assert.Equal(t, int64(0), res.Skyscraper)
assert.Equal(t, int64(0), res.BiotechLab)
assert.Equal(t, int64(0), res.Metropolis)
assert.Equal(t, int64(0), res.PlanetaryShield)
}
34 changes: 34 additions & 0 deletions pkg/extractor/v9/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,37 @@ func extractResourceSettingsFromDoc(doc *goquery.Document) (ogame.ResourceSettin

return res, token, nil
}

func GetNbr(doc *goquery.Document, name string) int64 {
val := utils.DoParseI64(doc.Find("span."+name+" span.level").First().AttrOr("data-value", "0"))
return val
}

func extractLfBuildingsFromDoc(doc *goquery.Document) (ogame.LfBuildings, error) {
res := ogame.LfBuildings{}
// res.ResidentialSector = GetNbr(doc, "residentialSector")
// res.BiosphereFarm = GetNbr(doc, "biosphereFarm")
// res.ResearchCentre = GetNbr(doc, "researchCentre")
// res.AcademyOfSciences = GetNbr(doc, "academyOfSciences")
// res.NeuroCalibrationCentre = GetNbr(doc, "neuroCalibrationCentre")
// res.HighEnergySmelting = GetNbr(doc, "highEnergySmelting")
// res.FoodSilo = GetNbr(doc, "foodSilo")
// res.FusionPoweredProduction = GetNbr(doc, "fusionPoweredProduction")
// res.Skyscraper = GetNbr(doc, "skyscraper")
// res.BiotechLab = GetNbr(doc, "biotechLab")
// res.Metropolis = GetNbr(doc, "metropolis")
// res.PlanetaryShield = GetNbr(doc, "planetaryShield")
res.ResidentialSector = GetNbr(doc, "lifeformTech11101")
res.BiosphereFarm = GetNbr(doc, "lifeformTech11102")
res.ResearchCentre = GetNbr(doc, "lifeformTech11103")
res.AcademyOfSciences = GetNbr(doc, "lifeformTech11104")
res.NeuroCalibrationCentre = GetNbr(doc, "lifeformTech11105")
res.HighEnergySmelting = GetNbr(doc, "lifeformTech11106")
res.FoodSilo = GetNbr(doc, "lifeformTech11107")
res.FusionPoweredProduction = GetNbr(doc, "lifeformTech11108")
res.Skyscraper = GetNbr(doc, "lifeformTech11109")
res.BiotechLab = GetNbr(doc, "lifeformTech11110")
res.Metropolis = GetNbr(doc, "lifeformTech11111")
res.PlanetaryShield = GetNbr(doc, "lifeformTech11112")
return res, nil
}
2 changes: 1 addition & 1 deletion pkg/parser/fetchTechsAjaxPage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package parser

import "github.com/alaingilbert/ogame/pkg/ogame"

func (p FetchTechsAjaxPage) ExtractTechs() (ogame.ResourcesBuildings, ogame.Facilities, ogame.ShipsInfos, ogame.DefensesInfos, ogame.Researches, error) {
func (p FetchTechsAjaxPage) ExtractTechs() (ogame.ResourcesBuildings, ogame.Facilities, ogame.ShipsInfos, ogame.DefensesInfos, ogame.Researches, ogame.LfBuildings, error) {
return p.e.ExtractTechs(p.content)
}
7 changes: 6 additions & 1 deletion pkg/parser/fullPage.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package parser

import (
"github.com/alaingilbert/ogame/pkg/ogame"
"time"

"github.com/alaingilbert/ogame/pkg/ogame"
)

func (p FullPage) ExtractOGameSession() string {
Expand Down Expand Up @@ -45,6 +46,10 @@ func (p FullPage) ExtractLifeformEnabled() bool {
return p.e.ExtractLifeformEnabled(p.GetContent())
}

func (p FullPage) ExtractLfBuildings() (ogame.LfBuildings, error) {
return p.e.ExtractLfBuildings(p.GetContent())
}

func (p FullPage) ExtractServerTime() (time.Time, error) {
return p.e.ExtractServerTimeFromDoc(p.GetDoc())
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/parser/lfbuildingsPage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package parser

import "github.com/alaingilbert/ogame/pkg/ogame"

func (p SuppliesPage) ExtractLfBuildings() (ogame.LfBuildings, error) {
return p.e.ExtractLfBuildingsFromDoc(p.GetDoc())
}
7 changes: 6 additions & 1 deletion pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package parser
import (
"bytes"
"errors"
"time"

"github.com/PuerkitoBio/goquery"
"github.com/alaingilbert/ogame/pkg/extractor"
v6 "github.com/alaingilbert/ogame/pkg/extractor/v6"
"github.com/alaingilbert/ogame/pkg/ogame"
"time"
)

var ErrParsePageType = errors.New("failed to parse requested page type")
Expand Down Expand Up @@ -44,13 +45,15 @@ type FacilitiesPage struct{ FullPage }
type ShipyardPage struct{ FullPage }
type DefensesPage struct{ FullPage }
type MovementPage struct{ FullPage }
type LfBuildingsPage struct{ FullPage }

type FullPagePages interface {
OverviewPage |
PreferencesPage |
SuppliesPage |
ResourcesSettingsPage |
FacilitiesPage |
LfBuildingsPage |
//TraderOverviewPageContent |
//TraderResourcesPageContent |
ResearchPage |
Expand Down Expand Up @@ -127,6 +130,8 @@ func ParsePage[T FullPagePages](e extractor.Extractor, pageHTML []byte) (T, erro
return T(ResearchPage{fullPage}), nil
case FacilitiesPage:
return T(FacilitiesPage{fullPage}), nil
case LfBuildingsPage:
return T(LfBuildingsPage{fullPage}), nil
case SuppliesPage:
return T(SuppliesPage{fullPage}), nil
case ResourcesSettingsPage:
Expand Down
5 changes: 4 additions & 1 deletion pkg/wrapper/fetcher.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package wrapper

import (
"github.com/alaingilbert/ogame/pkg/parser"
"net/url"

"github.com/alaingilbert/ogame/pkg/parser"
)

// Page names
Expand Down Expand Up @@ -82,6 +83,8 @@ func getPage[T parser.FullPagePages](b *OGame, opts ...Option) (T, error) {
pageName = DefensesPageName
case parser.ResearchPage:
pageName = ResearchPageName
case parser.LfBuildingsPage:
pageName = LfbuildingsPageName
case parser.ShipyardPage:
pageName = ShipyardPageName
case parser.ResourcesSettingsPage:
Expand Down
20 changes: 11 additions & 9 deletions pkg/wrapper/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"encoding/base64"
"errors"
"fmt"
"github.com/alaingilbert/ogame/pkg/ogame"
"github.com/alaingilbert/ogame/pkg/utils"
echo "github.com/labstack/echo/v4"
"net/http"
"net/url"
"strings"

"github.com/alaingilbert/ogame/pkg/ogame"
"github.com/alaingilbert/ogame/pkg/utils"
echo "github.com/labstack/echo/v4"
)

// APIResp ...
Expand Down Expand Up @@ -1294,16 +1295,17 @@ func TechsHandler(c echo.Context) error {
if err != nil {
return c.JSON(http.StatusBadRequest, ErrorResp(400, "invalid celestial id"))
}
supplies, facilities, ships, defenses, researches, err := bot.GetTechs(ogame.CelestialID(celestialID))
supplies, facilities, ships, defenses, researches, lfbuildings, err := bot.GetTechs(ogame.CelestialID(celestialID))
if err != nil {
return c.JSON(http.StatusBadRequest, ErrorResp(400, err.Error()))
}
return c.JSON(http.StatusOK, SuccessResp(map[string]any{
"supplies": supplies,
"facilities": facilities,
"ships": ships,
"defenses": defenses,
"researches": researches,
"supplies": supplies,
"facilities": facilities,
"ships": ships,
"defenses": defenses,
"researches": researches,
"lfbuildings": lfbuildings,
}))
}

Expand Down
Loading

0 comments on commit 40d77f8

Please sign in to comment.