Skip to content

Commit

Permalink
more abstract interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Aug 20, 2022
1 parent b821c0c commit 4f9cad9
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 12 deletions.
12 changes: 8 additions & 4 deletions pkg/ogame/baseBuilding.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ func (b BaseBuilding) DeconstructionPrice(level int64, techs Researches) Resourc
}
}

// ConstructionTime returns the duration it takes to build given level. Deconstruction time is the same function.
func (b BaseBuilding) ConstructionTime(level, universeSpeed int64, facilities Facilities, hasTechnocrat, isDiscoverer bool) time.Duration {
func (b BaseBuilding) BuildingConstructionTime(level, universeSpeed int64, acc BuildingAccelerators) time.Duration {
price := b.GetPrice(level)
metalCost := float64(price.Metal)
crystalCost := float64(price.Crystal)
roboticLvl := float64(facilities.RoboticsFactory)
naniteLvl := float64(facilities.NaniteFactory)
roboticLvl := float64(acc.GetRoboticsFactory())
naniteLvl := float64(acc.GetNaniteFactory())
hours := (metalCost + crystalCost) / (2500 * (1 + roboticLvl) * float64(universeSpeed) * math.Pow(2, naniteLvl))
secs := hours * 3600
if b.ID != NaniteFactoryID && (level-1) < 5 {
Expand All @@ -39,6 +38,11 @@ func (b BaseBuilding) ConstructionTime(level, universeSpeed int64, facilities Fa
return time.Duration(int64(math.Floor(secs))) * time.Second
}

// ConstructionTime returns the duration it takes to build given level. Deconstruction time is the same function.
func (b BaseBuilding) ConstructionTime(level, universeSpeed int64, facilities BuildAccelerators, _, _ bool) time.Duration {
return b.BuildingConstructionTime(level, universeSpeed, facilities)
}

// GetLevel returns current level of a building
func (b BaseBuilding) GetLevel(lazyResourcesBuildings LazyResourcesBuildings, lazyFacilities LazyFacilities, _ LazyResearches) int64 {
if b.ID.IsResourceBuilding() {
Expand Down
13 changes: 9 additions & 4 deletions pkg/ogame/baseDefender.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,20 @@ func (b BaseDefender) GetRapidfireAgainst() map[ID]int64 {
return b.RapidfireAgainst
}

// ConstructionTime returns the duration it takes to build nbr defender units
func (b BaseDefender) ConstructionTime(nbr, universeSpeed int64, facilities Facilities, hasTechnocrat, isDiscoverer bool) time.Duration {
shipyardLvl := float64(facilities.Shipyard)
naniteLvl := float64(facilities.NaniteFactory)
// DefenderConstructionTime returns the duration it takes to build nbr defender units
func (b BaseDefender) DefenderConstructionTime(nbr, universeSpeed int64, acc DefenseAccelerators) time.Duration {
shipyardLvl := float64(acc.GetShipyard())
naniteLvl := float64(acc.GetNaniteFactory())
hours := float64(b.StructuralIntegrity) / (2500 * (1 + shipyardLvl) * float64(universeSpeed) * math.Pow(2, naniteLvl))
secs := math.Max(1, hours*3600)
return time.Duration(int64(math.Floor(secs))*nbr) * time.Second
}

// ConstructionTime same as DefenderConstructionTime, needed for BaseOgameObj implementation
func (b BaseDefender) ConstructionTime(nbr, universeSpeed int64, acc BuildAccelerators, _, _ bool) time.Duration {
return b.DefenderConstructionTime(nbr, universeSpeed, acc)
}

// GetPrice returns the price of nbr defender units
func (b BaseDefender) GetPrice(nbr int64) Resources {
return b.Price.Mul(nbr)
Expand Down
11 changes: 8 additions & 3 deletions pkg/ogame/baseTechnology.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ type BaseTechnology struct {
BaseLevelable
}

// ConstructionTime returns the duration it takes to build given technology
func (b BaseTechnology) ConstructionTime(level, universeSpeed int64, facilities Facilities, hasTechnocrat, isDiscoverer bool) time.Duration {
// TechnologyConstructionTime returns the duration it takes to build given technology
func (b BaseTechnology) TechnologyConstructionTime(level, universeSpeed int64, acc TechAccelerators, hasTechnocrat, isDiscoverer bool) time.Duration {
price := b.GetPrice(level)
metalCost := float64(price.Metal)
crystalCost := float64(price.Crystal)
researchLabLvl := float64(facilities.ResearchLab)
researchLabLvl := float64(acc.GetResearchLab())
hours := (metalCost + crystalCost) / (1000 * (1 + researchLabLvl) * float64(universeSpeed))
if hasTechnocrat {
hours -= 0.25 * hours
Expand All @@ -27,6 +27,11 @@ func (b BaseTechnology) ConstructionTime(level, universeSpeed int64, facilities
return time.Duration(int64(math.Floor(secs))) * time.Second
}

// ConstructionTime same as TechnologyConstructionTime, needed for BaseOgameObj implementation
func (b BaseTechnology) ConstructionTime(level, universeSpeed int64, facilities BuildAccelerators, hasTechnocrat, isDiscoverer bool) time.Duration {
return b.TechnologyConstructionTime(level, universeSpeed, facilities, hasTechnocrat, isDiscoverer)
}

// GetLevel returns current level of a technology
func (b BaseTechnology) GetLevel(_ LazyResourcesBuildings, _ LazyFacilities, lazyResearches LazyResearches) int64 {
return lazyResearches().ByID(b.ID)
Expand Down
5 changes: 5 additions & 0 deletions pkg/ogame/facilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ type Facilities struct {
JumpGate int64 // 43
}

func (f Facilities) GetNaniteFactory() int64 { return f.NaniteFactory }
func (f Facilities) GetRoboticsFactory() int64 { return f.RoboticsFactory }
func (f Facilities) GetResearchLab() int64 { return f.ResearchLab }
func (f Facilities) GetShipyard() int64 { return f.Shipyard }

// Lazy returns a function that return self
func (f Facilities) Lazy() LazyFacilities {
return func() Facilities { return f }
Expand Down
25 changes: 24 additions & 1 deletion pkg/ogame/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Celestial interface {

// BaseOgameObj base interface for all ogame objects (buildings, technologies, ships, defenses)
type BaseOgameObj interface {
ConstructionTime(nbr, universeSpeed int64, facilities Facilities, hasTechnocrat, isDiscoverer bool) time.Duration
ConstructionTime(nbr, universeSpeed int64, acc BuildAccelerators, hasTechnocrat, isDiscoverer bool) time.Duration
GetID() ID
GetName() string
GetPrice(int64) Resources
Expand All @@ -27,6 +27,7 @@ type BaseOgameObj interface {
// DefenderObj base interface for all defensive units (ships, defenses)
type DefenderObj interface {
BaseOgameObj
DefenderConstructionTime(nbr, universeSpeed int64, acc DefenseAccelerators) time.Duration
GetRapidfireAgainst() map[ID]int64
GetRapidfireFrom() map[ID]int64
GetShieldPower(Researches) int64
Expand Down Expand Up @@ -56,10 +57,32 @@ type Levelable interface {
// Technology interface that all technologies implement
type Technology interface {
Levelable
TechnologyConstructionTime(nbr, universeSpeed int64, acc TechAccelerators, hasTechnocrat, isDiscoverer bool) time.Duration
}

// Building interface that all buildings implement
type Building interface {
Levelable
BuildingConstructionTime(nbr, universeSpeed int64, acc BuildingAccelerators) time.Duration
DeconstructionPrice(lvl int64, techs Researches) Resources
}

type BuildAccelerators interface {
TechAccelerators
BuildingAccelerators
DefenseAccelerators
}

type TechAccelerators interface {
GetResearchLab() int64
}

type DefenseAccelerators interface {
GetNaniteFactory() int64
GetShipyard() int64
}

type BuildingAccelerators interface {
GetNaniteFactory() int64
GetRoboticsFactory() int64
}
5 changes: 5 additions & 0 deletions pkg/ogame/solarSatellite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ogame

import (
"math"
"time"
)

type solarSatellite struct {
Expand Down Expand Up @@ -44,3 +45,7 @@ func (s *solarSatellite) GetLevel(LazyResourcesBuildings, LazyFacilities, LazyRe
func (s *solarSatellite) DeconstructionPrice(level int64, techs Researches) Resources {
return Resources{}
}

func (s *solarSatellite) BuildingConstructionTime(_, _ int64, _ BuildingAccelerators) time.Duration {
panic("Solar satellite should not be a building")
}

0 comments on commit 4f9cad9

Please sign in to comment.