Skip to content

Commit

Permalink
Merge branch 'release/42.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Dec 6, 2020
2 parents c0fa73e + 65dc4a5 commit a6cca7e
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 17 deletions.
6 changes: 3 additions & 3 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {
Name: "Planet",
Help: "Planet infos",
Func: func(c *ishell.Context) {
p := bot.Planets[0]
p := bot.GetPlanets()[0]
c.Printf("%s [%d:%d:%d]\n",
p.Name, p.Coordinate.Galaxy, p.Coordinate.System, p.Coordinate.Position)
},
Expand All @@ -47,7 +47,7 @@ func main() {
Name: "Planets",
Help: "List planets",
Func: func(c *ishell.Context) {
for _, p := range bot.Planets {
for _, p := range bot.GetPlanets() {
c.Printf("%s (%d) [%d:%d:%d]\n",
p.Name, p.ID, p.Coordinate.Galaxy, p.Coordinate.System, p.Coordinate.Position)
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func main() {
Name: "GetResources",
Help: "GetResources",
Func: func(c *ishell.Context) {
resources, _ := bot.Planets[0].GetResources()
resources, _ := bot.GetPlanets()[0].GetResources()
c.Printf("Metal: %d, Crystal: %d, Deuterium: %d, Energy: %d, Dark Matter: %d\n",
resources.Metal, resources.Crystal, resources.Deuterium, resources.Energy, resources.Darkmatter)
},
Expand Down
21 changes: 21 additions & 0 deletions defence.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ type DefensesInfos struct {
InterplanetaryMissiles int64
}

// HasShipDefense returns either or not at least one defense which can attack ships is present i.e., excluding
// AntiBallisticMissiles
func (d DefensesInfos) HasShipDefense() bool {
return d.CountShipDefenses() > 0
}

// HasMissilesDefense returns either or not AntiBallisticMissiles are present
func (d DefensesInfos) HasMissilesDefense() bool {
return d.AntiBallisticMissiles > 0
}

// CountShipDefenses returns the count of defenses which can attack ships i.e., excluding AntiBallisticMissiles
func (d DefensesInfos) CountShipDefenses() (out int64) {
for _, defense := range Defenses {
if defense != InterplanetaryMissiles && defense != AntiBallisticMissiles {
out += d.ByID(defense.GetID())
}
}
return
}

// AttackableValue returns the value of the defenses that can be attacked
func (d DefensesInfos) AttackableValue() int64 {
val := d.RocketLauncher * RocketLauncher.Price.Total()
Expand Down
18 changes: 18 additions & 0 deletions defence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,21 @@ func TestDefenceSet(t *testing.T) {
assert.Equal(t, int64(9), s.ByID(AntiBallisticMissilesID))
assert.Equal(t, int64(10), s.ByID(InterplanetaryMissilesID))
}

func TestDefence_HasMissilesDefenses(t *testing.T) {
assert.True(t, DefensesInfos{RocketLauncher: 2, PlasmaTurret: 3, InterplanetaryMissiles: 5, AntiBallisticMissiles: 2}.HasMissilesDefense())
assert.False(t, DefensesInfos{}.HasMissilesDefense())
assert.False(t, DefensesInfos{InterplanetaryMissiles: 2}.HasMissilesDefense())
assert.True(t, DefensesInfos{InterplanetaryMissiles: 2, AntiBallisticMissiles: 3}.HasMissilesDefense())
}

func TestDefence_HasShipDefenses(t *testing.T) {
assert.True(t, DefensesInfos{RocketLauncher: 2, PlasmaTurret: 3, InterplanetaryMissiles: 5, AntiBallisticMissiles: 2}.HasShipDefense())
assert.False(t, DefensesInfos{}.HasShipDefense())
assert.False(t, DefensesInfos{InterplanetaryMissiles: 2}.HasShipDefense())
assert.False(t, DefensesInfos{InterplanetaryMissiles: 2, AntiBallisticMissiles: 3}.HasShipDefense())
}

func TestDefence_CountShipDefenses(t *testing.T) {
assert.Equal(t, int64(5), DefensesInfos{RocketLauncher: 2, PlasmaTurret: 3, AntiBallisticMissiles: 4, InterplanetaryMissiles: 5}.CountShipDefenses())
}
10 changes: 10 additions & 0 deletions espionageReport.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,13 @@ func (r EspionageReport) Loot(characterClass CharacterClass) Resources {
Deuterium: int64(float64(r.Deuterium) * plunderRatio),
}
}

// IsDefenceless returns either or not the scanned planet has any defense (either ships or defense) against an attack
// with ships. If no ShipsInfos or DefensesInfos is including in the espionage report due to the lack of enough probes,
// the planet is assumed to be not defenceless.
func (r EspionageReport) IsDefenceless() bool {
return r.HasFleetInformation &&
r.HasDefensesInformation &&
!r.ShipsInfos().HasShips() &&
!r.DefensesInfos().HasShipDefense()
}
10 changes: 10 additions & 0 deletions espionageReport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ func TestEspionageReport_Loot(t *testing.T) {
assert.Equal(t, Resources{Metal: 50}, er.Loot(NoClass))
}

func TestEspionageReport_IsDefenceless(t *testing.T) {
two := int64(2)
assert.True(t, EspionageReport{Resources: Resources{Metal: 100}, HasFleetInformation: true, HasDefensesInformation: true}.IsDefenceless())
assert.False(t, EspionageReport{Resources: Resources{Metal: 100}, HasFleetInformation: true, HasDefensesInformation: true, LightFighter: &two}.IsDefenceless())
assert.False(t, EspionageReport{Resources: Resources{Metal: 100}, HasFleetInformation: true, HasDefensesInformation: true, RocketLauncher: &two}.IsDefenceless())
assert.False(t, EspionageReport{Resources: Resources{Metal: 100}, HasFleetInformation: true, HasDefensesInformation: false}.IsDefenceless())
assert.False(t, EspionageReport{Resources: Resources{Metal: 100}, HasFleetInformation: false, HasDefensesInformation: true}.IsDefenceless())
assert.False(t, EspionageReport{Resources: Resources{Metal: 100}, HasFleetInformation: false, HasDefensesInformation: false}.IsDefenceless())
}

func TestShipsInfos(t *testing.T) {
er := EspionageReport{HasFleetInformation: true, SmallCargo: I64Ptr(3), LightFighter: I64Ptr(5)}
assert.Equal(t, int64(8), er.ShipsInfos().CountShips())
Expand Down
6 changes: 6 additions & 0 deletions extracts_v6.go
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,7 @@ func extractPreferencesFromDocV6(doc *goquery.Document) Preferences {
EconomyNotifications: extractEconomyNotificationsFromDocV6(doc),
ShowActivityMinutes: extractShowActivityMinutesFromDocV6(doc),
PreserveSystemOnPlanetChange: extractPreserveSystemOnPlanetChangeFromDocV6(doc),
UrlaubsModus: extractUrlaubsModus(doc),
}
if prefs.MobileVersion {
prefs.Notifications.BuildList = extractNotifBuildListFromDocV6(doc)
Expand Down Expand Up @@ -1284,6 +1285,11 @@ func extractMobileVersionFromDocV6(doc *goquery.Document) bool {
return exists
}

func extractUrlaubsModus(doc *goquery.Document) bool {
_, exists := doc.Find("input[name=urlaubs_modus]").Attr("checked")
return exists
}

func extractShowOldDropDownsFromDocV6(doc *goquery.Document) bool {
_, exists := doc.Find("input[name=showOldDropDowns]").Attr("checked")
return exists
Expand Down
4 changes: 3 additions & 1 deletion ogame.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ type Preferences struct {
EconomyNotifications bool
ShowActivityMinutes bool
PreserveSystemOnPlanetChange bool
UrlaubsModus bool // Vacation mode

// Mobile only
Notifications struct {
Expand Down Expand Up @@ -2216,7 +2217,8 @@ func calcFuel(ships ShipsInfos, dist, duration int64, universeSpeedFleet, fleetD
return
}

func calcFlightTime(origin, destination Coordinate, universeSize, nbSystems int64, donutGalaxy, donutSystem bool,
// CalcFlightTime ...
func CalcFlightTime(origin, destination Coordinate, universeSize, nbSystems int64, donutGalaxy, donutSystem bool,
fleetDeutSaveFactor, speed float64, universeSpeedFleet int64, ships ShipsInfos, techs Researches, characterClass CharacterClass) (secs, fuel int64) {
if !ships.HasShips() {
return
Expand Down
18 changes: 11 additions & 7 deletions ogame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,7 @@ func TestExtractPreferences(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("samples/preferences.html")
prefs := NewExtractorV6().ExtractPreferences(pageHTMLBytes)
assert.Equal(t, int64(10), prefs.SpioAnz)
assert.False(t, prefs.UrlaubsModus)
assert.False(t, prefs.DisableChatBar)
assert.False(t, prefs.DisableOutlawWarning)
assert.False(t, prefs.MobileVersion)
Expand All @@ -1122,6 +1123,7 @@ func TestExtractPreferences(t *testing.T) {
pageHTMLBytes, _ = ioutil.ReadFile("samples/preferences_reverse.html")
prefs = NewExtractorV6().ExtractPreferences(pageHTMLBytes)
assert.Equal(t, int64(2), prefs.SpioAnz)
assert.False(t, prefs.UrlaubsModus)
assert.True(t, prefs.DisableChatBar)
assert.True(t, prefs.DisableOutlawWarning)
assert.False(t, prefs.MobileVersion)
Expand All @@ -1145,6 +1147,7 @@ func TestExtractPreferences(t *testing.T) {
pageHTMLBytes, _ = ioutil.ReadFile("samples/preferences_mobile.html")
prefs = NewExtractorV6().ExtractPreferences(pageHTMLBytes)
assert.Equal(t, int64(3), prefs.SpioAnz)
assert.False(t, prefs.UrlaubsModus)
assert.False(t, prefs.DisableChatBar) // no mobile
assert.False(t, prefs.DisableOutlawWarning)
assert.True(t, prefs.MobileVersion)
Expand Down Expand Up @@ -1177,6 +1180,7 @@ func TestExtractPreferences(t *testing.T) {
pageHTMLBytes, _ = ioutil.ReadFile("samples/preferences_reverse_mobile.html")
prefs = NewExtractorV6().ExtractPreferences(pageHTMLBytes)
assert.Equal(t, int64(2), prefs.SpioAnz)
assert.False(t, prefs.UrlaubsModus)
assert.False(t, prefs.DisableChatBar) // no mobile
assert.True(t, prefs.DisableOutlawWarning)
assert.True(t, prefs.MobileVersion)
Expand Down Expand Up @@ -2746,25 +2750,25 @@ func TestDistance(t *testing.T) {

func TestCalcFlightTime(t *testing.T) {
// Test from https://ogame.fandom.com/wiki/Talk:Fuel_Consumption
secs, fuel := calcFlightTime(Coordinate{1, 1, 1, PlanetType}, Coordinate{1, 5, 3, PlanetType},
secs, fuel := CalcFlightTime(Coordinate{1, 1, 1, PlanetType}, Coordinate{1, 5, 3, PlanetType},
1, 499, false, false, 1, 0.8, 1, ShipsInfos{LightFighter: 16, HeavyFighter: 8, Cruiser: 4}, Researches{CombustionDrive: 10, ImpulseDrive: 7}, NoClass)
assert.Equal(t, int64(4966), secs)
assert.Equal(t, int64(550), fuel)

// Different fleetDeutSaveFactor
secs, fuel = calcFlightTime(Coordinate{4, 116, 12, PlanetType}, Coordinate{3, 116, 12, PlanetType},
secs, fuel = CalcFlightTime(Coordinate{4, 116, 12, PlanetType}, Coordinate{3, 116, 12, PlanetType},
6, 499, true, true, 0.5, 1, 2, ShipsInfos{LargeCargo: 1931}, Researches{CombustionDrive: 18, ImpulseDrive: 15, HyperspaceDrive: 13}, Discoverer)
assert.Equal(t, int64(5406), secs)
assert.Equal(t, int64(110336), fuel)

// Test with solar satellite
secs, fuel = calcFlightTime(Coordinate{1, 1, 1, PlanetType}, Coordinate{1, 1, 15, PlanetType},
secs, fuel = CalcFlightTime(Coordinate{1, 1, 1, PlanetType}, Coordinate{1, 1, 15, PlanetType},
6, 499, false, false, 1, 1, 4, ShipsInfos{LargeCargo: 100, SolarSatellite: 50}, Researches{CombustionDrive: 16, ImpulseDrive: 13, HyperspaceDrive: 15}, NoClass)
assert.Equal(t, int64(651), secs)
assert.Equal(t, int64(612), fuel)

// General tests
secs, fuel = calcFlightTime(
secs, fuel = CalcFlightTime(
Coordinate{2, 68, 4, MoonType},
Coordinate{1, 313, 9, PlanetType},
5, 499, true, true, 1, 1, 2,
Expand All @@ -2773,7 +2777,7 @@ func TestCalcFlightTime(t *testing.T) {
assert.Equal(t, int64(13427), secs)
assert.Equal(t, int64(3808), fuel)

secs, fuel = calcFlightTime(
secs, fuel = CalcFlightTime(
Coordinate{1, 230, 7, MoonType},
Coordinate{1, 318, 4, MoonType},
5, 499, true, true, 0.5, 1, 6,
Expand All @@ -2782,7 +2786,7 @@ func TestCalcFlightTime(t *testing.T) {
assert.Equal(t, int64(3069), secs)
assert.Equal(t, int64(584), fuel)

secs, fuel = calcFlightTime(
secs, fuel = CalcFlightTime(
Coordinate{1, 230, 7, MoonType},
Coordinate{1, 318, 4, MoonType},
5, 499, true, true, 0.5, 1, 6,
Expand All @@ -2791,7 +2795,7 @@ func TestCalcFlightTime(t *testing.T) {
assert.Equal(t, int64(15), secs)
assert.Equal(t, int64(1), fuel)

secs, fuel = calcFlightTime(
secs, fuel = CalcFlightTime(
Coordinate{1, 230, 7, MoonType},
Coordinate{1, 318, 4, MoonType},
5, 499, true, true, 1, 1, 6,
Expand Down
2 changes: 1 addition & 1 deletion prioritize.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ func (b *Prioritize) FlightTime(origin, destination Coordinate, speed Speed, shi
b.begin("FlightTime")
defer b.done()
researches := b.bot.getCachedResearch()
return calcFlightTime(origin, destination, b.bot.serverData.Galaxies, b.bot.serverData.Systems,
return CalcFlightTime(origin, destination, b.bot.serverData.Galaxies, b.bot.serverData.Systems,
b.bot.serverData.DonutGalaxy, b.bot.serverData.DonutSystem, b.bot.serverData.GlobalDeuteriumSaveFactor,
float64(speed)/10, b.bot.serverData.SpeedFleet, ships, researches, b.bot.characterClass)
}
Expand Down
20 changes: 15 additions & 5 deletions resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package ogame

import (
"fmt"
stdmath "math"

"github.com/dustin/go-humanize"
humanize "github.com/dustin/go-humanize"
"github.com/google/gxui/math"
)

Expand Down Expand Up @@ -61,7 +62,7 @@ type Resources struct {

func (r Resources) String() string {
return fmt.Sprintf("[%s|%s|%s]",
humanize.Comma(int64(r.Metal)), humanize.Comma(int64(r.Crystal)), humanize.Comma(int64(r.Deuterium)))
humanize.Comma(r.Metal), humanize.Comma(r.Crystal), humanize.Comma(r.Deuterium))
}

// Total returns the sum of resources
Expand Down Expand Up @@ -102,7 +103,7 @@ func (r Resources) Mul(scalar int64) Resources {
}

func min64(values ...int64) int64 {
var m int64 = int64(math.MaxInt)
m := int64(math.MaxInt)
for _, v := range values {
if v < m {
m = v
Expand All @@ -123,7 +124,7 @@ func max64(values ...int64) int64 {

// Div finds how many price a res can afford
func (r Resources) Div(price Resources) int64 {
var nb int64 = int64(math.MaxInt)
nb := int64(math.MaxInt)
if price.Metal > 0 {
nb = r.Metal / price.Metal
}
Expand All @@ -133,7 +134,7 @@ func (r Resources) Div(price Resources) int64 {
if price.Deuterium > 0 {
nb = min64(r.Deuterium/price.Deuterium, nb)
}
return int64(nb)
return nb
}

// CanAfford alias to Gte
Expand All @@ -154,3 +155,12 @@ func (r Resources) Lte(val Resources) bool {
r.Crystal <= val.Crystal &&
r.Deuterium <= val.Deuterium
}

// FitsIn get the number of ships required to transport the resource
func (r Resources) FitsIn(ship Ship, techs Researches, probeRaids, isCollector bool) int64 {
cargo := ship.GetCargoCapacity(techs, probeRaids, isCollector)
if cargo == 0 {
return 0
}
return int64(stdmath.Ceil(float64(r.Total()) / float64(cargo)))
}
10 changes: 10 additions & 0 deletions resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,13 @@ func TestResourcesDetails_Available(t *testing.T) {
d.Darkmatter.Available = 5
assert.Equal(t, Resources{1, 2, 3, 4, 5}, d.Available())
}

func TestResources_FitsIn(t *testing.T) {
assert.Equal(t, int64(1), Resources{Metal: 100, Crystal: 200, Deuterium: 300}.FitsIn(SmallCargo, Researches{}, false, false))
assert.Equal(t, int64(2), Resources{Metal: 1001, Crystal: 2000, Deuterium: 2000}.FitsIn(SmallCargo, Researches{}, false, false))
assert.Equal(t, int64(2), Resources{Metal: 1000, Crystal: 2000, Deuterium: 3000}.FitsIn(SmallCargo, Researches{}, false, false))
assert.Equal(t, int64(2), Resources{Metal: 999, Crystal: 4000, Deuterium: 5000}.FitsIn(SmallCargo, Researches{}, false, false))
assert.Equal(t, int64(0), Resources{Metal: 0, Crystal: 0, Deuterium: 0}.FitsIn(SmallCargo, Researches{}, false, false))
assert.Equal(t, int64(0), Resources{Metal: 100, Crystal: 200, Deuterium: 300}.FitsIn(EspionageProbe, Researches{}, false, false))
assert.Equal(t, int64(120), Resources{Metal: 100, Crystal: 200, Deuterium: 300}.FitsIn(EspionageProbe, Researches{}, true, false))
}

0 comments on commit a6cca7e

Please sign in to comment.