Skip to content

Commit

Permalink
Add lifeform stuff - Base on FaunX PR
Browse files Browse the repository at this point in the history
Based on FaunX PR:
Add lifeform stuff to support build of lifeform buildings (techs TODO)

Also convert "Lifeform" to "Population"
Add few function to get Population, Food, LF buildings level, Constructionbeingbuild, etc

I'm noob in GO, feel free to improve
  • Loading branch information
YOLOP0wn committed Sep 22, 2022
1 parent 61da082 commit eb0923a
Show file tree
Hide file tree
Showing 28 changed files with 1,209 additions and 108 deletions.
4 changes: 4 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.GetLFRequirementsHandler)
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/LFbuildings", 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 All @@ -313,6 +315,7 @@ func start(c *cli.Context) error {
e.POST("/bot/planets/:planetID/teardown/:ogameID", wrapper.TeardownHandler)
e.GET("/bot/planets/:planetID/production", wrapper.GetProductionHandler)
e.GET("/bot/planets/:planetID/constructions", wrapper.ConstructionsBeingBuiltHandler)
e.GET("/bot/planets/:planetID/LFconstructions", wrapper.LFConstructionsBeingBuiltHandler)
e.POST("/bot/planets/:planetID/cancel-building", wrapper.CancelBuildingHandler)
e.POST("/bot/planets/:planetID/cancel-research", wrapper.CancelResearchHandler)
e.GET("/bot/planets/:planetID/resources", wrapper.GetResourcesHandler)
Expand All @@ -321,6 +324,7 @@ func start(c *cli.Context) error {
e.GET("/bot/moons/:moonID/phalanx/:galaxy/:system/:position", wrapper.PhalanxHandler)
e.POST("/bot/moons/:moonID/jump-gate", wrapper.JumpGateHandler)
e.GET("/game/allianceInfo.php", wrapper.GetAlliancePageContentHandler) // Example: //game/allianceInfo.php?allianceId=500127


// Get/Post Page Content
e.GET("/game/index.php", wrapper.GetFromGameHandler)
Expand Down
14 changes: 11 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 @@ -75,6 +76,7 @@ type OverviewExtractorBytes interface {
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)
ExtractLFConstructions(pageHTML []byte) (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 +357,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 +412,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: 17 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 @@ -812,6 +813,10 @@ func (e *Extractor) ExtractConstructions(pageHTML []byte) (buildingID ogame.ID,
return extractConstructions(pageHTML)
}

func (e *Extractor) ExtractLFConstructions(pageHTML []byte) (LFbuildingID ogame.ID, LFresearchCountdown int64) {
return extractLFConstructions(pageHTML)
}

// ExtractFleetDeutSaveFactor extract fleet deut save factor
func (e *Extractor) ExtractFleetDeutSaveFactor(pageHTML []byte) float64 {
return extractFleetDeutSaveFactor(pageHTML)
Expand Down Expand Up @@ -882,3 +887,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")
}
10 changes: 10 additions & 0 deletions pkg/extractor/v6/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -1921,6 +1921,16 @@ func extractConstructions(pageHTML []byte) (buildingID ogame.ID, buildingCountdo
return
}

func extractLFConstructions(pageHTML []byte) (LFbuildingID ogame.ID, LFbuildingCountdown int64) {
LFbuildingCountdownMatch := regexp.MustCompile(`getElementByIdWithCache\("lfbuildingCountdown"\),(\d+),`).FindSubmatch(pageHTML)
if len(LFbuildingCountdownMatch) > 0 {
LFbuildingCountdown = int64(utils.ToInt(LFbuildingCountdownMatch[1]))
LFbuildingIDInt := utils.ToInt(regexp.MustCompile(`onclick="cancellfbuilding\((\d+),`).FindSubmatch(pageHTML)[1])
LFbuildingID = ogame.ID(LFbuildingIDInt)
}
return
}

func extractFleetDeutSaveFactor(pageHTML []byte) float64 {
factor := 1.0
m := regexp.MustCompile(`var fleetDeutSaveFactor=([+-]?([0-9]*[.])?[0-9]+);`).FindSubmatch(pageHTML)
Expand Down
4 changes: 4 additions & 0 deletions pkg/extractor/v7/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func (e Extractor) ExtractConstructions(pageHTML []byte) (buildingID ogame.ID, b
return ExtractConstructions(pageHTML, clockwork.NewRealClock())
}

func (e Extractor) ExtractLFConstructions(pageHTML []byte) (LFbuildingID ogame.ID, LFbuildingCountdown int64) {
return ExtractLFConstructions(pageHTML, clockwork.NewRealClock())
}

// ExtractFleet1Ships ...
func (e Extractor) ExtractFleet1Ships(pageHTML []byte) ogame.ShipsInfos {
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(pageHTML))
Expand Down
53 changes: 31 additions & 22 deletions pkg/extractor/v7/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,29 @@ 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", "")))
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 := ogame.ResourcesDetails{}
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
}

func ExtractFacilitiesFromDoc(doc *goquery.Document) (ogame.Facilities, error) {
res := ogame.Facilities{}
res.RoboticsFactory = GetNbr(doc, "roboticsFactory")
Expand Down Expand Up @@ -236,6 +235,16 @@ func ExtractConstructions(pageHTML []byte, clock clockwork.Clock) (buildingID og
return
}

func ExtractLFConstructions(pageHTML []byte, clock clockwork.Clock) (LFbuildingID ogame.ID, LFbuildingCountdown int64) {
LFbuildingCountdownMatch := regexp.MustCompile(`var restTimelfbuilding = (\d+) -`).FindSubmatch(pageHTML)
if len(LFbuildingCountdownMatch) > 0 {
LFbuildingCountdown = int64(utils.ToInt(LFbuildingCountdownMatch[1])) - clock.Now().Unix()
LFbuildingIDInt := utils.ToInt(regexp.MustCompile(`onclick="cancellfbuilding\((\d+),`).FindSubmatch(pageHTML)[1])
LFbuildingID = ogame.ID(LFbuildingIDInt)
}
return
}

func extractIPMFromDoc(doc *goquery.Document) (duration, max int64, token string) {
duration = utils.DoParseI64(doc.Find("span#timer").AttrOr("data-duration", "0"))
max = utils.DoParseI64(doc.Find("input[name=missileCount]").AttrOr("data-max", "0"))
Expand Down
7 changes: 4 additions & 3 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 Expand Up @@ -173,4 +174,4 @@ func (e *Extractor) ExtractIsMobile(pageHTML []byte) bool {
// ExtractIsMobileFromDoc ...
func (e *Extractor) ExtractIsMobileFromDoc(doc *goquery.Document) bool {
return extractIsMobileFromDoc(doc)
}
}
Loading

0 comments on commit eb0923a

Please sign in to comment.