Skip to content

Commit

Permalink
Spy report to have nil values instead of 0 when infos is unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Aug 24, 2018
1 parent bbc4dfc commit efd0e9e
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 33 deletions.
79 changes: 67 additions & 12 deletions ogame.go
Original file line number Diff line number Diff line change
Expand Up @@ -2130,14 +2130,65 @@ func (b *OGame) getEspionageReportMessages() ([]EspionageReportSummary, error) {
// EspionageReport ...
type EspionageReport struct {
Resources
ResourcesBuildings
Facilities
Researches
ShipsInfos
Defenses
Coordinate Coordinate
Type EspionageReportType
Date time.Time
MetalMine *int // ResourcesBuildings
CrystalMine *int
DeuteriumSynthesizer *int
SolarPlant *int
FusionReactor *int
SolarSatellite *int
MetalStorage *int
CrystalStorage *int
DeuteriumTank *int
RoboticsFactory *int // Facilities
Shipyard *int
ResearchLab *int
AllianceDepot *int
MissileSilo *int
NaniteFactory *int
Terraformer *int
SpaceDock *int
EnergyTechnology *int // Researches
LaserTechnology *int
IonTechnology *int
HyperspaceTechnology *int
PlasmaTechnology *int
CombustionDrive *int
ImpulseDrive *int
HyperspaceDrive *int
EspionageTechnology *int
ComputerTechnology *int
Astrophysics *int
IntergalacticResearchNetwork *int
GravitonTechnology *int
WeaponsTechnology *int
ShieldingTechnology *int
ArmourTechnology *int
RocketLauncher *int // Defenses
LightLaser *int
HeavyLaser *int
GaussCannon *int
IonCannon *int
PlasmaTurret *int
SmallShieldDome *int
LargeShieldDome *int
AntiBallisticMissiles *int
InterplanetaryMissiles *int
LightFighter *int // Fleets
HeavyFighter *int
Cruiser *int
Battleship *int
Battlecruiser *int
Bomber *int
Destroyer *int
Deathstar *int
SmallCargo *int
LargeCargo *int
ColonyShip *int
Recycler *int
EspionageProbe *int
Coordinate Coordinate
Type EspionageReportType
Date time.Time
}

func extractEspionageReport(pageHTML string, location *time.Location) (EspionageReport, error) {
Expand Down Expand Up @@ -2169,7 +2220,8 @@ func extractEspionageReport(pageHTML string, location *time.Location) (Espionage
imgClass := s2.Find("img").AttrOr("class", "")
r := regexp.MustCompile(`building(\d+)`)
buildingID, _ := strconv.Atoi(r.FindStringSubmatch(imgClass)[1])
level, _ := strconv.Atoi(s2.Find("span.fright").Text())
l, _ := strconv.Atoi(s2.Find("span.fright").Text())
level := &l
switch ID(buildingID) {
case MetalMine.ID:
report.MetalMine = level
Expand Down Expand Up @@ -2210,7 +2262,8 @@ func extractEspionageReport(pageHTML string, location *time.Location) (Espionage
imgClass := s2.Find("img").AttrOr("class", "")
r := regexp.MustCompile(`research(\d+)`)
researchID, _ := strconv.Atoi(r.FindStringSubmatch(imgClass)[1])
level, _ := strconv.Atoi(s2.Find("span.fright").Text())
l, _ := strconv.Atoi(s2.Find("span.fright").Text())
level := &l
switch ID(researchID) {
case EspionageTechnology.ID:
report.EspionageTechnology = level
Expand Down Expand Up @@ -2251,7 +2304,8 @@ func extractEspionageReport(pageHTML string, location *time.Location) (Espionage
imgClass := s2.Find("img").AttrOr("class", "")
r := regexp.MustCompile(`tech(\d+)`)
shipID, _ := strconv.Atoi(r.FindStringSubmatch(imgClass)[1])
level, _ := strconv.Atoi(s2.Find("span.fright").Text())
l, _ := strconv.Atoi(s2.Find("span.fright").Text())
level := &l
switch ID(shipID) {
case SmallCargo.ID:
report.SmallCargo = level
Expand Down Expand Up @@ -2288,7 +2342,8 @@ func extractEspionageReport(pageHTML string, location *time.Location) (Espionage
imgClass := s2.Find("img").AttrOr("class", "")
r := regexp.MustCompile(`defense(\d+)`)
defenceID, _ := strconv.Atoi(r.FindStringSubmatch(imgClass)[1])
level, _ := strconv.Atoi(s2.Find("span.fright").Text())
l, _ := strconv.Atoi(s2.Find("span.fright").Text())
level := &l
switch ID(defenceID) {
case RocketLauncher.ID:
report.RocketLauncher = level
Expand Down
57 changes: 36 additions & 21 deletions ogame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,37 +309,52 @@ func TestExtractEspionageReport(t *testing.T) {
assert.Equal(t, 146970, infos.Crystal)
assert.Equal(t, 24751, infos.Deuterium)
assert.Equal(t, 2324, infos.Energy)
assert.Equal(t, 20, infos.MetalMine)
assert.Equal(t, 14, infos.CrystalMine)
assert.Equal(t, 8, infos.DeuteriumSynthesizer)
assert.Equal(t, 19, infos.SolarPlant)
assert.Equal(t, 5, infos.RoboticsFactory)
assert.Equal(t, 2, infos.Shipyard)
assert.Equal(t, 5, infos.MetalStorage)
assert.Equal(t, 5, infos.CrystalStorage)
assert.Equal(t, 2, infos.DeuteriumTank)
assert.Equal(t, 3, infos.ResearchLab)
assert.Equal(t, 2, infos.EspionageTechnology)
assert.Equal(t, 1, infos.ComputerTechnology)
assert.Equal(t, 1, infos.ArmourTechnology)
assert.Equal(t, 1, infos.EnergyTechnology)
assert.Equal(t, 7, infos.CombustionDrive)
assert.Equal(t, 2, infos.ImpulseDrive)
assert.Equal(t, 20, *infos.MetalMine)
assert.Equal(t, 14, *infos.CrystalMine)
assert.Equal(t, 8, *infos.DeuteriumSynthesizer)
assert.Equal(t, 19, *infos.SolarPlant)
assert.Equal(t, 5, *infos.RoboticsFactory)
assert.Equal(t, 2, *infos.Shipyard)
assert.Equal(t, 5, *infos.MetalStorage)
assert.Equal(t, 5, *infos.CrystalStorage)
assert.Equal(t, 2, *infos.DeuteriumTank)
assert.Equal(t, 3, *infos.ResearchLab)
assert.Equal(t, 2, *infos.EspionageTechnology)
assert.Equal(t, 1, *infos.ComputerTechnology)
assert.Equal(t, 1, *infos.ArmourTechnology)
assert.Equal(t, 1, *infos.EnergyTechnology)
assert.Equal(t, 7, *infos.CombustionDrive)
assert.Equal(t, 2, *infos.ImpulseDrive)
assert.Nil(t, infos.LightFighter)
assert.Nil(t, infos.HeavyFighter)
assert.Nil(t, infos.Cruiser)
assert.Nil(t, infos.Battleship)
assert.Nil(t, infos.Battlecruiser)
assert.Nil(t, infos.Bomber)
assert.Nil(t, infos.Destroyer)
assert.Nil(t, infos.Deathstar)
assert.Nil(t, infos.SmallCargo)
assert.Nil(t, infos.LargeCargo)
assert.Nil(t, infos.ColonyShip)
assert.Nil(t, infos.Recycler)
assert.Nil(t, infos.EspionageProbe)
assert.Nil(t, infos.SolarSatellite)
}

func TestExtractEspionageReport1(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("samples/spy_report_res_buildings_researches_fleet.html")
infos, _ := extractEspionageReport(string(pageHTMLBytes), time.FixedZone("OGT", 3600))
assert.Equal(t, 2, infos.Battleship)
assert.Equal(t, 1, infos.Bomber)
assert.Equal(t, 2, *infos.Battleship)
assert.Equal(t, 1, *infos.Bomber)
}

func TestExtractEspionageReport_defence(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("samples/spy_report_res_fleet_defences.html")
infos, _ := extractEspionageReport(string(pageHTMLBytes), time.FixedZone("OGT", 3600))
assert.Equal(t, 57, infos.RocketLauncher)
assert.Equal(t, 57, infos.LightLaser)
assert.Equal(t, 61, infos.HeavyLaser)
assert.Equal(t, 57, *infos.RocketLauncher)
assert.Equal(t, 57, *infos.LightLaser)
assert.Equal(t, 61, *infos.HeavyLaser)
assert.Nil(t, infos.GaussCannon)
}

func TestGalaxyDistance(t *testing.T) {
Expand Down

0 comments on commit efd0e9e

Please sign in to comment.