Skip to content

Commit

Permalink
Merge branch 'release/49.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Sep 24, 2022
2 parents 61da082 + ea420e0 commit c841c2f
Show file tree
Hide file tree
Showing 38 changed files with 8,099 additions and 182 deletions.
6 changes: 3 additions & 3 deletions cmd/c/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ func BuildShips(planetID, shipID, nbr C.int) (errorMsg *C.char) {
// TODO: GetProduction(PlanetID) ([]Quantifiable, error)

//export ConstructionsBeingBuilt
func ConstructionsBeingBuilt(planetID C.int) (buildingID, buildingCountdown, researchID, researchCountdown C.int) {
a, b, c, d := bot.ConstructionsBeingBuilt(ogame2.CelestialID(planetID))
return C.int(a), C.int(b), C.int(c), C.int(d)
func ConstructionsBeingBuilt(planetID C.int) (buildingID, buildingCountdown, researchID, researchCountdown, lfBuildingID, lfBuildingCountdown C.int) {
a, b, c, d, e, f := bot.ConstructionsBeingBuilt(ogame2.CelestialID(planetID))
return C.int(a), C.int(b), C.int(c), C.int(d), C.int(e), C.int(f)
}

//export CancelBuilding
Expand Down
2 changes: 2 additions & 0 deletions cmd/ogamed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ func start(c *cli.Context) error {
e.GET("/bot/get-research", wrapper.GetResearchHandler)
e.GET("/bot/buy-offer-of-the-day", wrapper.BuyOfferOfTheDayHandler)
e.GET("/bot/price/:ogameID/:nbr", wrapper.GetPriceHandler)
e.GET("/bot/requirements/:ogameID", wrapper.GetRequirementsHandler)
e.GET("/bot/moons", wrapper.GetMoonsHandler)
e.GET("/bot/moons/:moonID", wrapper.GetMoonHandler)
e.GET("/bot/moons/:galaxy/:system/:position", wrapper.GetMoonByCoordHandler)
Expand All @@ -300,6 +301,7 @@ func start(c *cli.Context) error {
e.GET("/bot/planets/:planetID/resource-settings", wrapper.GetResourceSettingsHandler)
e.POST("/bot/planets/:planetID/resource-settings", wrapper.SetResourceSettingsHandler)
e.GET("/bot/planets/:planetID/resources-buildings", wrapper.GetResourcesBuildingsHandler)
e.GET("/bot/planets/:planetID/lifeform-buildings", wrapper.GetLfBuildingsHandler)
e.GET("/bot/planets/:planetID/defence", wrapper.GetDefenseHandler)
e.GET("/bot/planets/:planetID/ships", wrapper.GetShipsHandler)
e.GET("/bot/planets/:planetID/facilities", wrapper.GetFacilitiesHandler)
Expand Down
15 changes: 11 additions & 4 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 @@ -74,7 +75,7 @@ type OverviewExtractorBytes interface {
ExtractCancelLfBuildingInfos(pageHTML []byte) (token string, id, listID int64, err error)
ExtractCancelResearchInfos(pageHTML []byte) (token string, techID, listID int64, err error)
ExtractCharacterClass(pageHTML []byte) (ogame.CharacterClass, error)
ExtractConstructions(pageHTML []byte) (buildingID ogame.ID, buildingCountdown int64, researchID ogame.ID, researchCountdown int64)
ExtractConstructions(pageHTML []byte) (buildingID ogame.ID, buildingCountdown int64, researchID ogame.ID, researchCountdown int64, lfBuildingID ogame.ID, lfBuildingCountdown int64)
ExtractDMCosts(pageHTML []byte) (ogame.DMCosts, error)
ExtractFleetDeutSaveFactor(pageHTML []byte) float64
ExtractOverviewProduction(pageHTML []byte) ([]ogame.Quantifiable, int64, error)
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
19 changes: 15 additions & 4 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 @@ -546,7 +547,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 Expand Up @@ -808,7 +809,7 @@ func (e *Extractor) ExtractFederation(pageHTML []byte) url.Values {
}

// ExtractConstructions ...
func (e *Extractor) ExtractConstructions(pageHTML []byte) (buildingID ogame.ID, buildingCountdown int64, researchID ogame.ID, researchCountdown int64) {
func (e *Extractor) ExtractConstructions(pageHTML []byte) (buildingID ogame.ID, buildingCountdown int64, researchID ogame.ID, researchCountdown int64, lfBuildingID ogame.ID, lfBuildingCountdown int64) {
return extractConstructions(pageHTML)
}

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")
}
2 changes: 1 addition & 1 deletion pkg/extractor/v6/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,7 @@ func TestCancelResearch(t *testing.T) {

func TestGetConstructions(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("../../../samples/unversioned/overview_active.html")
buildingID, buildingCountdown, researchID, researchCountdown := NewExtractor().ExtractConstructions(pageHTMLBytes)
buildingID, buildingCountdown, researchID, researchCountdown, _, _ := NewExtractor().ExtractConstructions(pageHTMLBytes)
assert.Equal(t, ogame.CrystalMineID, buildingID)
assert.Equal(t, int64(731), buildingCountdown)
assert.Equal(t, ogame.CombustionDriveID, researchID)
Expand Down
18 changes: 13 additions & 5 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 Expand Up @@ -1905,7 +1913,7 @@ func extractFederation(pageHTML []byte) url.Values {
return payload
}

func extractConstructions(pageHTML []byte) (buildingID ogame.ID, buildingCountdown int64, researchID ogame.ID, researchCountdown int64) {
func extractConstructions(pageHTML []byte) (buildingID ogame.ID, buildingCountdown int64, researchID ogame.ID, researchCountdown int64, lfBuildingID ogame.ID, lfBuildingCountdown int64) {
buildingCountdownMatch := regexp.MustCompile(`getElementByIdWithCache\("Countdown"\),(\d+),`).FindSubmatch(pageHTML)
if len(buildingCountdownMatch) > 0 {
buildingCountdown = int64(utils.ToInt(buildingCountdownMatch[1]))
Expand Down
2 changes: 1 addition & 1 deletion pkg/extractor/v7/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (e Extractor) ExtractResourcesDetails(pageHTML []byte) (out ogame.Resources
}

// ExtractConstructions ...
func (e Extractor) ExtractConstructions(pageHTML []byte) (buildingID ogame.ID, buildingCountdown int64, researchID ogame.ID, researchCountdown int64) {
func (e Extractor) ExtractConstructions(pageHTML []byte) (buildingID ogame.ID, buildingCountdown int64, researchID ogame.ID, researchCountdown int64, lfBuildingID ogame.ID, lfBuildingCountdown int64) {
return ExtractConstructions(pageHTML, clockwork.NewRealClock())
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/extractor/v7/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func TestExtractFleetSlot_FleetDispatch(t *testing.T) {
func TestGetConstructionsV7(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("../../../samples/v7/overview_supplies_in_construction.html")
clock := clockwork.NewFakeClockAt(time.Date(2019, 11, 12, 9, 6, 43, 0, time.UTC))
buildingID, buildingCountdown, researchID, researchCountdown := ExtractConstructions(pageHTMLBytes, clock)
buildingID, buildingCountdown, researchID, researchCountdown, _, _ := ExtractConstructions(pageHTMLBytes, clock)
assert.Equal(t, ogame.MetalMineID, buildingID)
assert.Equal(t, int64(62), buildingCountdown)
assert.Equal(t, ogame.EnergyTechnologyID, researchID)
Expand Down
22 changes: 11 additions & 11 deletions pkg/extractor/v7/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@ func extractPremiumToken(pageHTML []byte, days int64) (token string, err error)

func extractResourcesDetailsFromFullPageFromDoc(doc *goquery.Document) ogame.ResourcesDetails {
out := ogame.ResourcesDetails{}
out.Metal.Available = utils.ParseInt(strings.Split(doc.Find("span#resources_metal").AttrOr("data-raw", "0"), ".")[0])
out.Crystal.Available = utils.ParseInt(strings.Split(doc.Find("span#resources_crystal").AttrOr("data-raw", "0"), ".")[0])
out.Deuterium.Available = utils.ParseInt(strings.Split(doc.Find("span#resources_deuterium").AttrOr("data-raw", "0"), ".")[0])
out.Energy.Available = utils.ParseInt(doc.Find("span#resources_energy").Text())
out.Darkmatter.Available = utils.ParseInt(doc.Find("span#resources_darkmatter").Text())
metalDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(doc.Find("li#metal_box").AttrOr("title", "")))
crystalDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(doc.Find("li#crystal_box").AttrOr("title", "")))
deuteriumDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(doc.Find("li#deuterium_box").AttrOr("title", "")))
energyDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(doc.Find("li#energy_box").AttrOr("title", "")))
darkmatterDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(doc.Find("li#darkmatter_box").AttrOr("title", "")))
metalDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(doc.Find("div#metal_box").AttrOr("title", "")))
crystalDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(doc.Find("div#crystal_box").AttrOr("title", "")))
deuteriumDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(doc.Find("div#deuterium_box").AttrOr("title", "")))
energyDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(doc.Find("div#energy_box").AttrOr("title", "")))
darkmatterDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(doc.Find("div#darkmatter_box").AttrOr("title", "")))
out.Metal.Available = utils.ParseInt(metalDoc.Find("table tr").Eq(0).Find("td").Eq(0).Text())
out.Metal.StorageCapacity = utils.ParseInt(metalDoc.Find("table tr").Eq(1).Find("td").Eq(0).Text())
out.Metal.CurrentProduction = utils.ParseInt(metalDoc.Find("table tr").Eq(2).Find("td").Eq(0).Text())
out.Crystal.Available = utils.ParseInt(crystalDoc.Find("table tr").Eq(0).Find("td").Eq(0).Text())
out.Crystal.StorageCapacity = utils.ParseInt(crystalDoc.Find("table tr").Eq(1).Find("td").Eq(0).Text())
out.Crystal.CurrentProduction = utils.ParseInt(crystalDoc.Find("table tr").Eq(2).Find("td").Eq(0).Text())
out.Deuterium.Available = utils.ParseInt(deuteriumDoc.Find("table tr").Eq(0).Find("td").Eq(0).Text())
out.Deuterium.StorageCapacity = utils.ParseInt(deuteriumDoc.Find("table tr").Eq(1).Find("td").Eq(0).Text())
out.Deuterium.CurrentProduction = utils.ParseInt(deuteriumDoc.Find("table tr").Eq(2).Find("td").Eq(0).Text())
out.Energy.Available = utils.ParseInt(energyDoc.Find("table tr").Eq(0).Find("td").Eq(0).Text())
out.Energy.CurrentProduction = utils.ParseInt(energyDoc.Find("table tr").Eq(1).Find("td").Eq(0).Text())
out.Energy.Consumption = utils.ParseInt(energyDoc.Find("table tr").Eq(2).Find("td").Eq(0).Text())
out.Darkmatter.Available = utils.ParseInt(darkmatterDoc.Find("table tr").Eq(0).Find("td").Eq(0).Text())
out.Darkmatter.Purchased = utils.ParseInt(darkmatterDoc.Find("table tr").Eq(1).Find("td").Eq(0).Text())
out.Darkmatter.Found = utils.ParseInt(darkmatterDoc.Find("table tr").Eq(2).Find("td").Eq(0).Text())
return out
Expand Down Expand Up @@ -220,7 +220,7 @@ func extractResourcesDetails(pageHTML []byte) (out ogame.ResourcesDetails, err e
return
}

func ExtractConstructions(pageHTML []byte, clock clockwork.Clock) (buildingID ogame.ID, buildingCountdown int64, researchID ogame.ID, researchCountdown int64) {
func ExtractConstructions(pageHTML []byte, clock clockwork.Clock) (buildingID ogame.ID, buildingCountdown int64, researchID ogame.ID, researchCountdown int64, lfBuildingID ogame.ID, lfBuildingCountdown int64) {
buildingCountdownMatch := regexp.MustCompile(`var restTimebuilding = (\d+) -`).FindSubmatch(pageHTML)
if len(buildingCountdownMatch) > 0 {
buildingCountdown = int64(utils.ToInt(buildingCountdownMatch[1])) - clock.Now().Unix()
Expand Down
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
Loading

0 comments on commit c841c2f

Please sign in to comment.