Skip to content

Commit

Permalink
Merge branch 'release/48.0.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Aug 31, 2022
2 parents 2dfaa05 + 55a82a5 commit cf67599
Show file tree
Hide file tree
Showing 7 changed files with 2,346 additions and 5 deletions.
6 changes: 6 additions & 0 deletions pkg/extractor/v9/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v9
import (
"bytes"
"github.com/PuerkitoBio/goquery"
"github.com/alaingilbert/clockwork"
"github.com/alaingilbert/ogame/pkg/extractor/v874"
"github.com/alaingilbert/ogame/pkg/ogame"
)
Expand Down Expand Up @@ -77,3 +78,8 @@ func (e *Extractor) ExtractResourcesDetailsFromFullPage(pageHTML []byte) ogame.R
func (e *Extractor) ExtractResourcesDetailsFromFullPageFromDoc(doc *goquery.Document) ogame.ResourcesDetails {
return extractResourcesDetailsFromFullPageFromDoc(doc)
}

// ExtractConstructions ...
func (e *Extractor) ExtractConstructions(pageHTML []byte) (buildingID ogame.ID, buildingCountdown int64, researchID ogame.ID, researchCountdown int64) {
return ExtractConstructions(pageHTML, clockwork.NewRealClock())
}
21 changes: 21 additions & 0 deletions pkg/extractor/v9/extractor_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v9

import (
"github.com/alaingilbert/clockwork"
"github.com/alaingilbert/ogame/pkg/ogame"
"github.com/stretchr/testify/assert"
"io/ioutil"
Expand Down Expand Up @@ -95,3 +96,23 @@ func TestCancelBuilding(t *testing.T) {
assert.Equal(t, int64(1), id)
assert.Equal(t, int64(3469488), listID)
}

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)
assert.Equal(t, ogame.MetalMineID, buildingID)
assert.Equal(t, int64(5413), buildingCountdown)
assert.Equal(t, ogame.ComputerTechnologyID, researchID)
assert.Equal(t, int64(7), researchCountdown)

// 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)
assert.Equal(t, ogame.MetalStorageID, buildingID)
assert.Equal(t, int64(33483), buildingCountdown)
assert.Equal(t, ogame.ComputerTechnologyID, researchID)
assert.Equal(t, int64(18355), researchCountdown)
}
17 changes: 17 additions & 0 deletions pkg/extractor/v9/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v9
import (
"errors"
"github.com/PuerkitoBio/goquery"
"github.com/alaingilbert/clockwork"
"github.com/alaingilbert/ogame/pkg/extractor/v6"
v7 "github.com/alaingilbert/ogame/pkg/extractor/v7"
"github.com/alaingilbert/ogame/pkg/extractor/v71"
Expand All @@ -13,6 +14,22 @@ import (
"time"
)

func ExtractConstructions(pageHTML []byte, clock clockwork.Clock) (buildingID ogame.ID, buildingCountdown int64, researchID ogame.ID, researchCountdown int64) {
buildingCountdownMatch := regexp.MustCompile(`var restTimebuilding = (\d+) -`).FindSubmatch(pageHTML)
if len(buildingCountdownMatch) > 0 {
buildingCountdown = int64(utils.ToInt(buildingCountdownMatch[1])) - clock.Now().Unix()
buildingIDInt := utils.ToInt(regexp.MustCompile(`onclick="cancelbuilding\((\d+),`).FindSubmatch(pageHTML)[1])
buildingID = ogame.ID(buildingIDInt)
}
researchCountdownMatch := regexp.MustCompile(`var restTimeresearch = (\d+) -`).FindSubmatch(pageHTML)
if len(researchCountdownMatch) > 0 {
researchCountdown = int64(utils.ToInt(researchCountdownMatch[1])) - clock.Now().Unix()
researchIDInt := utils.ToInt(regexp.MustCompile(`onclick="cancelresearch\((\d+),`).FindSubmatch(pageHTML)[1])
researchID = ogame.ID(researchIDInt)
}
return
}

func extractCancelLfBuildingInfos(pageHTML []byte) (token string, id, listID int64, err error) {
return v7.ExtractCancelInfos(pageHTML, "cancelLinklfbuilding", "cancellfbuilding", 1)
}
Expand Down
38 changes: 37 additions & 1 deletion pkg/ogame/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ const (
IntergalacticResearchNetworkID ID = 123
AstrophysicsID ID = 124
GravitonTechnologyID ID = 199
ResidentialSectorID ID = 11101 // Lifeform
ResidentialSectorID ID = 11101 // Lifeform (humans)
BiosphereFarmID ID = 11102
ResearchCentreID ID = 11103
AcademyOfSciencesID ID = 11104
Expand All @@ -190,6 +190,42 @@ const (
BiotechLabID ID = 11110
MetropolisID ID = 11111
PlanetaryShieldID ID = 11112
MeditationEnclaveID ID = 12101 // Lifeform (rocktal)
CrystalFarmID ID = 12102
RuneTechnologiumID ID = 12103
RuneForgeID ID = 12104
OriktoriumID ID = 12105
MagmaForgeID ID = 12106
DisruptionChamberID ID = 12107
MegalithID ID = 12108
CrystalRefineryID ID = 12109
DeuteriumSynthesiserID ID = 12110
MineralResearchCentreID ID = 12111
MetalRecyclingPlantID ID = 12112
AssemblyLineID ID = 13101 // Lifeform (mechas)
FusionCellFactoryID ID = 13102
RoboticsResearchCentreID ID = 13103
UpdateNetworkID ID = 12304
QuantumComputerCentreID ID = 13105
AutomatisedAssemblyCentreID ID = 13106
HighPerformanceTransformerID ID = 13107
MicrochipAssemblyLineID ID = 13108
ProductionAssemblyHallID ID = 13109
HighPerformanceSynthesiserID ID = 13110
ChipMassProductionID ID = 13111
NanoRepairBotsID ID = 13112
SanctuaryID ID = 14101 // Lifeform (kaelesh)
AntimatterCondenserID ID = 14102
VortexChamberID ID = 14103
HallsOfRealisationID ID = 14104
ForumOfTranscendenceID ID = 14105
AntimatterConvectorID ID = 14106
CloningLaboratoryID ID = 14107
ChrysalisAcceleratorID ID = 14108
BioModifierID ID = 14109
PsionicModulatorID ID = 14110
ShipManufacturingHallID ID = 14111
SupraRefractorID ID = 14112
IntergalacticEnvoysID ID = 11201 // Lifeform tech
HighPerformanceExtractorsID ID = 11202
FusionDrivesID ID = 11203
Expand Down
40 changes: 38 additions & 2 deletions pkg/ogame/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (o ID) IsResourceBuilding() bool {
}

func (o ID) IsLfBuilding() bool {
return o == ResidentialSectorID ||
return o == ResidentialSectorID || // humans
o == BiosphereFarmID ||
o == ResearchCentreID ||
o == AcademyOfSciencesID ||
Expand All @@ -209,7 +209,43 @@ func (o ID) IsLfBuilding() bool {
o == SkyscraperID ||
o == BiotechLabID ||
o == MetropolisID ||
o == PlanetaryShieldID
o == PlanetaryShieldID || // rocktal
o == MeditationEnclaveID ||
o == CrystalFarmID ||
o == RuneTechnologiumID ||
o == RuneForgeID ||
o == OriktoriumID ||
o == MagmaForgeID ||
o == DisruptionChamberID ||
o == MegalithID ||
o == CrystalRefineryID ||
o == DeuteriumSynthesiserID ||
o == MineralResearchCentreID ||
o == MetalRecyclingPlantID ||
o == AssemblyLineID || // mechas
o == FusionCellFactoryID ||
o == RoboticsResearchCentreID ||
o == UpdateNetworkID ||
o == QuantumComputerCentreID ||
o == AutomatisedAssemblyCentreID ||
o == HighPerformanceTransformerID ||
o == MicrochipAssemblyLineID ||
o == ProductionAssemblyHallID ||
o == HighPerformanceSynthesiserID ||
o == ChipMassProductionID ||
o == NanoRepairBotsID ||
o == SanctuaryID || // kaelesh
o == AntimatterCondenserID ||
o == VortexChamberID ||
o == HallsOfRealisationID ||
o == ForumOfTranscendenceID ||
o == AntimatterConvectorID ||
o == CloningLaboratoryID ||
o == ChrysalisAcceleratorID ||
o == BioModifierID ||
o == PsionicModulatorID ||
o == ShipManufacturingHallID ||
o == SupraRefractorID
}

// IsBuilding returns either or not the id is a building (facility, resource building)
Expand Down
5 changes: 3 additions & 2 deletions pkg/wrapper/gameforge.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,8 @@ type ServerData struct {
// GetServerData gets the server data from xml api
func GetServerData(client httpclient.IHttpClient, ctx context.Context, serverNumber int64, serverLang string) (ServerData, error) {
var serverData ServerData
req, err := http.NewRequest(http.MethodGet, "https://s"+utils.FI64(serverNumber)+"-"+serverLang+".ogame.gameforge.com/api/serverData.xml", nil)
serverDataURL := "https://s" + utils.FI64(serverNumber) + "-" + serverLang + ".ogame.gameforge.com/api/serverData.xml"
req, err := http.NewRequest(http.MethodGet, serverDataURL, nil)
if err != nil {
return serverData, err
}
Expand All @@ -576,7 +577,7 @@ func GetServerData(client httpclient.IHttpClient, ctx context.Context, serverNum
return serverData, err
}
if err := xml.Unmarshal(by, &serverData); err != nil {
return serverData, err
return serverData, fmt.Errorf("failed to xml unmarshal %s : %w", serverDataURL, err)
}
return serverData, nil
}
Expand Down
Loading

0 comments on commit cf67599

Please sign in to comment.