Skip to content

Commit

Permalink
Merge branch 'Lifeform' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	pkg/extractor/v71/extracts.go
#	pkg/extractor/v9/extractor.go
#	pkg/extractor/v9/extractor_test.go
#	pkg/extractor/v9/extracts.go
#	pkg/ogame/lfBuildings.go
#	pkg/ogame/resourceSettings.go
#	pkg/parser/lfbuildingsPage.go
#	pkg/wrapper/prioritize.go
  • Loading branch information
alaingilbert committed Sep 24, 2022
2 parents 61f0c48 + 4912636 commit ea420e0
Show file tree
Hide file tree
Showing 26 changed files with 2,658 additions and 68 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
2 changes: 1 addition & 1 deletion pkg/extractor/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,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
2 changes: 1 addition & 1 deletion pkg/extractor/v6/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,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
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
2 changes: 1 addition & 1 deletion pkg/extractor/v6/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -1913,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
28 changes: 28 additions & 0 deletions pkg/extractor/v71/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ type resourcesResp struct {
Amount float64 `json:"amount"`
Tooltip string `json:"tooltip"`
} `json:"darkmatter"`
Population struct {
Amount float64 `json:"amount"`
Tooltip string `json:"tooltip"`
} `json:"population"`
Food struct {
Amount float64 `json:"amount"`
Tooltip string `json:"tooltip"`
} `json:"food"`
} `json:"resources"`
HonorScore int64 `json:"honorScore"`
Techs struct {
Expand Down Expand Up @@ -172,18 +180,38 @@ func extractResourcesDetails(pageHTML []byte) (out ogame.ResourcesDetails, err e
out.Deuterium.StorageCapacity = int64(res.Resources.Deuterium.Storage)
out.Energy.Available = int64(res.Resources.Energy.Amount)
out.Darkmatter.Available = int64(res.Resources.Darkmatter.Amount)
out.Population.Available = int64(res.Resources.Population.Amount)
out.Food.Available = int64(res.Resources.Food.Amount)
metalDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(res.Resources.Metal.Tooltip))
crystalDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(res.Resources.Crystal.Tooltip))
deuteriumDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(res.Resources.Deuterium.Tooltip))
darkmatterDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(res.Resources.Darkmatter.Tooltip))
energyDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(res.Resources.Energy.Tooltip))
populationDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(res.Resources.Population.Tooltip))
foodDoc, _ := goquery.NewDocumentFromReader(strings.NewReader(res.Resources.Food.Tooltip))
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.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.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.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.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())
out.Food.Available = utils.ParseInt(foodDoc.Find("table tr").Eq(0).Find("td").Eq(0).Text())
out.Food.StorageCapacity = utils.ParseInt(foodDoc.Find("table tr").Eq(1).Find("td").Eq(0).Text())
out.Food.Overproduction = utils.ParseInt(foodDoc.Find("table tr").Eq(2).Find("td").Eq(0).Text())
out.Food.ConsumedIn = utils.ParseInt(foodDoc.Find("table tr").Eq(3).Find("td").Eq(0).Text())
out.Food.TimeTillFoodRunsOut = utils.ParseInt(foodDoc.Find("table tr").Eq(4).Find("td").Eq(0).Text())
out.Population.Available = utils.ParseInt(populationDoc.Find("table tr").Eq(0).Find("td").Eq(0).Text())
out.Population.T2Lifeforms = utils.ParseInt(populationDoc.Find("table tr").Eq(1).Find("td").Eq(0).Text())
out.Population.T3Lifeforms = utils.ParseInt(populationDoc.Find("table tr").Eq(2).Find("td").Eq(0).Text())
out.Population.LivingSpace = utils.ParseInt(populationDoc.Find("table tr").Eq(3).Find("td").Eq(0).Text())
out.Population.Satisfied = utils.ParseInt(populationDoc.Find("table tr").Eq(4).Find("td").Eq(0).Text())
out.Population.Hungry, _ = strconv.ParseFloat(populationDoc.Find("table tr").Eq(5).Find("td").Eq(0).Text(), 64)
out.Population.GrowthRate, _ = strconv.ParseFloat(strings.TrimPrefix(populationDoc.Find("table tr").Eq(6).Find("td").Eq(0).Text(), "±"), 64)
out.Population.BunkerSpace = utils.ParseInt(populationDoc.Find("table tr").Eq(7).Find("td").Eq(0).Text())
return
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/extractor/v9/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (e *Extractor) ExtractResourcesDetailsFromFullPageFromDoc(doc *goquery.Docu
}

// 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
12 changes: 10 additions & 2 deletions pkg/extractor/v9/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ func TestExtractResourcesDetailsFromFullPage(t *testing.T) {
assert.Equal(t, int64(8000), res.Darkmatter.Found)
}

func TestExtractResourcesDetailsFromFullPagePopulation(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("../../../samples/v9.0.4/en/lifeform/overview.html")
res := NewExtractor().ExtractResourcesDetailsFromFullPage(pageHTMLBytes)
assert.Equal(t, int64(1974118), res.Population.Available)
assert.Equal(t, 0.233, res.Population.Hungry)
assert.Equal(t, 61.983, res.Population.GrowthRate)
}

func TestExtractResources(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("../../../samples/v9.0.0/en/overview.html")
res := NewExtractor().ExtractResources(pageHTMLBytes)
Expand Down Expand Up @@ -102,7 +110,7 @@ func TestGetConstructions(t *testing.T) {
// Without lifeform
pageHTMLBytes, _ := ioutil.ReadFile("../../../samples/v9.0.2/en/overview_all_queues.html")
clock := clockwork.NewFakeClockAt(time.Date(2022, 8, 20, 12, 43, 11, 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(5413), buildingCountdown)
assert.Equal(t, ogame.ComputerTechnologyID, researchID)
Expand All @@ -111,7 +119,7 @@ func TestGetConstructions(t *testing.T) {
// With lifeform
pageHTMLBytes, _ = ioutil.ReadFile("../../../samples/v9.0.2/en/lifeform/overview_all_queues2.html")
clock = clockwork.NewFakeClockAt(time.Date(2022, 8, 28, 17, 22, 26, 0, time.UTC))
buildingID, buildingCountdown, researchID, researchCountdown = ExtractConstructions(pageHTMLBytes, clock)
buildingID, buildingCountdown, researchID, researchCountdown, _, _ = ExtractConstructions(pageHTMLBytes, clock)
assert.Equal(t, ogame.MetalStorageID, buildingID)
assert.Equal(t, int64(33483), buildingCountdown)
assert.Equal(t, ogame.ComputerTechnologyID, researchID)
Expand Down
Loading

0 comments on commit ea420e0

Please sign in to comment.