Skip to content

Commit

Permalink
Merge branch 'release/48.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Aug 20, 2022
2 parents ca24fe1 + 595feb7 commit 6844b1e
Show file tree
Hide file tree
Showing 29 changed files with 4,196 additions and 1,783 deletions.
4 changes: 4 additions & 0 deletions pkg/extractor/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ type Extractor interface {
SetLanguage(lang string)
GetLocation() *time.Location
SetLocation(loc *time.Location)
GetLifeformEnabled() bool
SetLifeformEnabled(lifeformEnabled bool)
ExtractActiveItems(pageHTML []byte) ([]ogame.ActiveItem, error)
ExtractLifeformEnabled(pageHTML []byte) bool
ExtractAdmiral(pageHTML []byte) bool
ExtractAjaxChatToken(pageHTML []byte) (string, error)
ExtractAllResources(pageHTML []byte) (map[ogame.CelestialID]ogame.Resources, error)
ExtractAttacks(pageHTML []byte, ownCoords []ogame.Coordinate) ([]ogame.AttackEvent, error)
ExtractAuction(pageHTML []byte) (ogame.Auction, error)
ExtractBuffActivation(pageHTML []byte) (string, []ogame.Item, error)
ExtractCancelBuildingInfos(pageHTML []byte) (token string, techID, listID int64, err error)
ExtractCancelLfBuildingInfos(pageHTML []byte) (token string, id, listID int64, err error)
ExtractCancelFleetToken(pageHTML []byte, fleetID ogame.FleetID) (string, error)
ExtractCancelResearchInfos(pageHTML []byte) (token string, techID, listID int64, err error)
ExtractCelestial(pageHTML []byte, v any) (ogame.Celestial, error)
Expand Down
28 changes: 24 additions & 4 deletions pkg/extractor/v6/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"github.com/alaingilbert/ogame/pkg/ogame"
"net/url"
"regexp"
"time"

"github.com/PuerkitoBio/goquery"
Expand All @@ -13,8 +14,9 @@ import (

// Extractor ...
type Extractor struct {
loc *time.Location
lang string
loc *time.Location
lang string
lifeformEnabled bool
}

// NewExtractor ...
Expand All @@ -24,8 +26,10 @@ func NewExtractor() *Extractor {
return &Extractor{}
}

func (e *Extractor) SetLocation(loc *time.Location) { e.loc = loc }
func (e *Extractor) SetLanguage(lang string) { e.lang = lang }
func (e *Extractor) SetLocation(loc *time.Location) { e.loc = loc }
func (e *Extractor) SetLanguage(lang string) { e.lang = lang }
func (e *Extractor) SetLifeformEnabled(lifeformEnabled bool) { e.lifeformEnabled = lifeformEnabled }
func (e *Extractor) GetLifeformEnabled() bool { return e.lifeformEnabled }
func (e *Extractor) GetLocation() *time.Location {
if e.loc == nil {
return time.UTC
Expand All @@ -39,6 +43,10 @@ func (e *Extractor) GetLanguage() string {
return e.lang
}

func (e *Extractor) ExtractCancelLfBuildingInfos(pageHTML []byte) (token string, id, listID int64, err error) {
panic("implement me")
}

// ExtractActiveItems ...
func (e *Extractor) ExtractActiveItems(pageHTML []byte) ([]ogame.ActiveItem, error) {
panic("implement me")
Expand Down Expand Up @@ -79,6 +87,18 @@ func (e *Extractor) ExtractExpeditionMessagesFromDoc(doc *goquery.Document) ([]o
panic("implement me")
}

// ExtractLifeformEnabled ...
func (e *Extractor) ExtractLifeformEnabled(pageHTML []byte) bool {
lifeformEnabledMatch := regexp.MustCompile(`"lifeformEnabled":(\btrue\b|\bfalse\b)`).FindSubmatch(pageHTML)
if len(lifeformEnabledMatch) < 2 {
return false
}
if bytes.Equal(lifeformEnabledMatch[1], []byte("true")) {
return true
}
return false
}

// ExtractIsInVacation ...
func (e *Extractor) ExtractIsInVacation(pageHTML []byte) bool {
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(pageHTML))
Expand Down
11 changes: 11 additions & 0 deletions pkg/extractor/v6/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,14 @@ func TestExtractAttacks1(t *testing.T) {
assert.Equal(t, int64(1), attacks[0].Missiles)
assert.Nil(t, attacks[0].Ships)
}

func TestExtractLifeformEnabled(t *testing.T) {
pageHTML, _ := ioutil.ReadFile("../../../samples/unversioned/overview_active.html")
assert.False(t, NewExtractor().ExtractLifeformEnabled(pageHTML))

pageHTML, _ = ioutil.ReadFile("../../../samples/v9.0.2/en/overview_all_queues.html")
assert.False(t, NewExtractor().ExtractLifeformEnabled(pageHTML))

pageHTML, _ = ioutil.ReadFile("../../../samples/v9.0.2/en/lifeform/overview_all_queues.html")
assert.True(t, NewExtractor().ExtractLifeformEnabled(pageHTML))
}
34 changes: 11 additions & 23 deletions pkg/extractor/v7/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,44 +600,32 @@ func extractEspionageReportFromDocV7(doc *goquery.Document, location *time.Locat
return report, nil
}

func extractCancelBuildingInfosV7(pageHTML []byte) (token string, techID, listID int64, err error) {
r1 := regexp.MustCompile(`cancelLinkbuilding[^?]+\?page=ingame&component=overview&modus=2&token=(\w+)&action=cancel`)
func ExtractCancelInfos(pageHTML []byte, linkVarName, fnName string, tableIdx int) (token string, id, listID int64, err error) {
r1 := regexp.MustCompile(linkVarName + `[^?]+\?page=ingame&component=overview&modus=2&token=(\w+)&action=cancel`)
m1 := r1.FindSubmatch(pageHTML)
if len(m1) < 2 {
return "", 0, 0, errors.New("unable to find token")
}
token = string(m1[1])
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(pageHTML))
t := doc.Find("table.construction").Eq(0)
t := doc.Find("table.construction").Eq(tableIdx)
a, _ := t.Find("a.abortNow").Attr("onclick")
r := regexp.MustCompile(`cancelbuilding\((\d+),\s?(\d+),`)
r := regexp.MustCompile(fnName + `\((\d+),\s?(\d+),`)
m := r.FindStringSubmatch(a)
if len(m) < 3 {
return "", 0, 0, errors.New("unable to find techid/listid")
return "", 0, 0, errors.New("unable to find id/listid")
}
techID = utils.DoParseI64(m[1])
id = utils.DoParseI64(m[1])
listID = utils.DoParseI64(m[2])
return
}

func extractCancelBuildingInfosV7(pageHTML []byte) (token string, techID, listID int64, err error) {
return ExtractCancelInfos(pageHTML, "cancelLinkbuilding", "cancelbuilding", 0)
}

func extractCancelResearchInfosV7(pageHTML []byte) (token string, techID, listID int64, err error) {
r1 := regexp.MustCompile(`cancelLinkresearch[^?]+\?page=ingame&component=overview&modus=2&token=(\w+)&action=cancel`)
m1 := r1.FindSubmatch(pageHTML)
if len(m1) < 2 {
return "", 0, 0, errors.New("unable to find token")
}
token = string(m1[1])
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(pageHTML))
t := doc.Find("table.construction").Eq(1)
a, _ := t.Find("a.abortNow").Attr("onclick")
r := regexp.MustCompile(`cancelresearch\((\d+),\s?(\d+),`)
m := r.FindStringSubmatch(a)
if len(m) < 3 {
return "", 0, 0, errors.New("unable to find techid/listid")
}
techID = utils.DoParseI64(m[1])
listID = utils.DoParseI64(m[2])
return
return ExtractCancelInfos(pageHTML, "cancelLinkresearch", "cancelresearch", 1)
}

func extractResourceSettingsFromDocV7(doc *goquery.Document) (ogame.ResourceSettings, error) {
Expand Down
10 changes: 10 additions & 0 deletions pkg/extractor/v9/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ func NewExtractor() *Extractor {
return &Extractor{}
}

// ExtractCancelLfBuildingInfos ...
func (e *Extractor) ExtractCancelLfBuildingInfos(pageHTML []byte) (token string, id, listID int64, err error) {
return extractCancelLfBuildingInfos(pageHTML)
}

// ExtractCancelResearchInfos ...
func (e *Extractor) ExtractCancelResearchInfos(pageHTML []byte) (token string, id, listID int64, err error) {
return extractCancelResearchInfos(pageHTML, e.GetLifeformEnabled())
}

// ExtractEmpire ...
func (e *Extractor) ExtractEmpire(pageHTML []byte) ([]ogame.EmpireCelestial, error) {
return extractEmpireV9(pageHTML)
Expand Down
13 changes: 13 additions & 0 deletions pkg/extractor/v9/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"github.com/PuerkitoBio/goquery"
"github.com/alaingilbert/ogame/pkg/extractor/v6"
v7 "github.com/alaingilbert/ogame/pkg/extractor/v7"
"github.com/alaingilbert/ogame/pkg/extractor/v71"
"github.com/alaingilbert/ogame/pkg/ogame"
"github.com/alaingilbert/ogame/pkg/utils"
Expand All @@ -12,6 +13,18 @@ import (
"time"
)

func extractCancelLfBuildingInfos(pageHTML []byte) (token string, id, listID int64, err error) {
return v7.ExtractCancelInfos(pageHTML, "cancelLinklfbuilding", "cancellfbuilding", 1)
}

func extractCancelResearchInfos(pageHTML []byte, lifeformEnabled bool) (token string, techID, listID int64, err error) {
tableIdx := 1
if lifeformEnabled {
tableIdx = 2
}
return v7.ExtractCancelInfos(pageHTML, "cancelLinkresearch", "cancelresearch", tableIdx)
}

func extractEmpireV9(pageHTML []byte) ([]ogame.EmpireCelestial, error) {
var out []ogame.EmpireCelestial
raw, err := v6.ExtractEmpireJSON(pageHTML)
Expand Down
160 changes: 95 additions & 65 deletions pkg/ogame/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,71 +113,101 @@ const (
MoonType CelestialType = 3

//Buildings
MetalMineID ID = 1
CrystalMineID ID = 2
DeuteriumSynthesizerID ID = 3
SolarPlantID ID = 4
FusionReactorID ID = 12
MetalStorageID ID = 22
CrystalStorageID ID = 23
DeuteriumTankID ID = 24
ShieldedMetalDenID ID = 25
UndergroundCrystalDenID ID = 26
SeabedDeuteriumDenID ID = 27
AllianceDepotID ID = 34 // Facilities
RoboticsFactoryID ID = 14
ShipyardID ID = 21
ResearchLabID ID = 31
MissileSiloID ID = 44
NaniteFactoryID ID = 15
TerraformerID ID = 33
SpaceDockID ID = 36
LunarBaseID ID = 41 // Moon facilities
SensorPhalanxID ID = 42
JumpGateID ID = 43
RocketLauncherID ID = 401 // Defense
LightLaserID ID = 402
HeavyLaserID ID = 403
GaussCannonID ID = 404
IonCannonID ID = 405
PlasmaTurretID ID = 406
SmallShieldDomeID ID = 407
LargeShieldDomeID ID = 408
AntiBallisticMissilesID ID = 502
InterplanetaryMissilesID ID = 503
SmallCargoID ID = 202 // Ships
LargeCargoID ID = 203
LightFighterID ID = 204
HeavyFighterID ID = 205
CruiserID ID = 206
BattleshipID ID = 207
ColonyShipID ID = 208
RecyclerID ID = 209
EspionageProbeID ID = 210
BomberID ID = 211
SolarSatelliteID ID = 212
DestroyerID ID = 213
DeathstarID ID = 214
BattlecruiserID ID = 215
CrawlerID ID = 217
ReaperID ID = 218
PathfinderID ID = 219
EspionageTechnologyID ID = 106 // Research
ComputerTechnologyID ID = 108
WeaponsTechnologyID ID = 109
ShieldingTechnologyID ID = 110
ArmourTechnologyID ID = 111
EnergyTechnologyID ID = 113
HyperspaceTechnologyID ID = 114
CombustionDriveID ID = 115
ImpulseDriveID ID = 117
HyperspaceDriveID ID = 118
LaserTechnologyID ID = 120
IonTechnologyID ID = 121
PlasmaTechnologyID ID = 122
IntergalacticResearchNetworkID ID = 123
AstrophysicsID ID = 124
GravitonTechnologyID ID = 199
MetalMineID ID = 1
CrystalMineID ID = 2
DeuteriumSynthesizerID ID = 3
SolarPlantID ID = 4
FusionReactorID ID = 12
MetalStorageID ID = 22
CrystalStorageID ID = 23
DeuteriumTankID ID = 24
ShieldedMetalDenID ID = 25
UndergroundCrystalDenID ID = 26
SeabedDeuteriumDenID ID = 27
AllianceDepotID ID = 34 // Facilities
RoboticsFactoryID ID = 14
ShipyardID ID = 21
ResearchLabID ID = 31
MissileSiloID ID = 44
NaniteFactoryID ID = 15
TerraformerID ID = 33
SpaceDockID ID = 36
LunarBaseID ID = 41 // Moon facilities
SensorPhalanxID ID = 42
JumpGateID ID = 43
RocketLauncherID ID = 401 // Defense
LightLaserID ID = 402
HeavyLaserID ID = 403
GaussCannonID ID = 404
IonCannonID ID = 405
PlasmaTurretID ID = 406
SmallShieldDomeID ID = 407
LargeShieldDomeID ID = 408
AntiBallisticMissilesID ID = 502
InterplanetaryMissilesID ID = 503
SmallCargoID ID = 202 // Ships
LargeCargoID ID = 203
LightFighterID ID = 204
HeavyFighterID ID = 205
CruiserID ID = 206
BattleshipID ID = 207
ColonyShipID ID = 208
RecyclerID ID = 209
EspionageProbeID ID = 210
BomberID ID = 211
SolarSatelliteID ID = 212
DestroyerID ID = 213
DeathstarID ID = 214
BattlecruiserID ID = 215
CrawlerID ID = 217
ReaperID ID = 218
PathfinderID ID = 219
EspionageTechnologyID ID = 106 // Research
ComputerTechnologyID ID = 108
WeaponsTechnologyID ID = 109
ShieldingTechnologyID ID = 110
ArmourTechnologyID ID = 111
EnergyTechnologyID ID = 113
HyperspaceTechnologyID ID = 114
CombustionDriveID ID = 115
ImpulseDriveID ID = 117
HyperspaceDriveID ID = 118
LaserTechnologyID ID = 120
IonTechnologyID ID = 121
PlasmaTechnologyID ID = 122
IntergalacticResearchNetworkID ID = 123
AstrophysicsID ID = 124
GravitonTechnologyID ID = 199
ResidentialSectorID ID = 11101 // Lifeform
BiosphereFarmID ID = 11102
ResearchCentreID ID = 11103
AcademyOfSciencesID ID = 11104
NeuroCalibrationCentreID ID = 11105
HighEnergySmeltingID ID = 11106
FoodSiloID ID = 11107
FusionPoweredProductionID ID = 11108
SkyscraperID ID = 11109
BiotechLabID ID = 11110
MetropolisID ID = 11111
PlanetaryShieldID ID = 11112
IntergalacticEnvoysID ID = 11201 // Lifeform tech
HighPerformanceExtractorsID ID = 11202
FusionDrivesID ID = 11203
StealthFieldGeneratorID ID = 11204
OrbitalDenID ID = 11205
ResearchAIID ID = 11206
HighPerformanceTerraformerID ID = 11207
EnhancedProductionTechnologiesID ID = 11208
LightFighterMkIIID ID = 11209
CruiserMkIIID ID = 11210
ImprovedLabTechnologyID ID = 11211
PlasmaTerraformerID ID = 11212
LowTemperatureDrivesID ID = 11213
BomberMkIIID ID = 11214
DestroyerMkIIID ID = 11215
BattlecruiserMkIIID ID = 11216
RobotAssistantsID ID = 11217
SupercomputerID ID = 11218

// Missions
Attack MissionID = 1
Expand Down
17 changes: 16 additions & 1 deletion pkg/ogame/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,24 @@ func (o ID) IsResourceBuilding() bool {
o == SeabedDeuteriumDenID
}

func (o ID) IsLfBuilding() bool {
return o == ResidentialSectorID ||
o == BiosphereFarmID ||
o == ResearchCentreID ||
o == AcademyOfSciencesID ||
o == NeuroCalibrationCentreID ||
o == HighEnergySmeltingID ||
o == FoodSiloID ||
o == FusionPoweredProductionID ||
o == SkyscraperID ||
o == BiotechLabID ||
o == MetropolisID ||
o == PlanetaryShieldID
}

// IsBuilding returns either or not the id is a building (facility, resource building)
func (o ID) IsBuilding() bool {
return o.IsResourceBuilding() || o.IsFacility()
return o.IsResourceBuilding() || o.IsLfBuilding() || o.IsFacility()
}

// IsTech returns either or not the id is a technology
Expand Down
Loading

0 comments on commit 6844b1e

Please sign in to comment.