Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Aug 8, 2018
1 parent 1d5224e commit ab5ad85
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 139 deletions.
10 changes: 5 additions & 5 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ func (b BaseShip) GetRequirements() map[ID]int {

// IsAvailable ...
func (b BaseShip) IsAvailable(_ ResourcesBuildings, facilities Facilities, researches Researches, _ int) bool {
for ogameID, levelNeeded := range b.Requirements {
if ogameID.IsFacility() {
if facilities.ByOGameID(ogameID) < levelNeeded {
for id, levelNeeded := range b.Requirements {
if id.IsFacility() {
if facilities.ByID(id) < levelNeeded {
return false
}
} else if ogameID.IsTech() {
if researches.ByOGameID(ogameID) < levelNeeded {
} else if id.IsTech() {
if researches.ByID(id) < levelNeeded {
return false
}
}
Expand Down
18 changes: 9 additions & 9 deletions baseBuilding.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,26 @@ func (b BaseBuilding) ConstructionTime(level, universeSpeed int, facilities Faci
// GetLevel ...
func (b BaseBuilding) GetLevel(resourcesBuildings ResourcesBuildings, facilities Facilities, researches Researches) int {
if b.ID.IsResourceBuilding() {
return resourcesBuildings.ByOGameID(b.ID)
return resourcesBuildings.ByID(b.ID)
} else if b.ID.IsFacility() {
return facilities.ByOGameID(b.ID)
return facilities.ByID(b.ID)
}
return 0
}

// IsAvailable ...
func (b BaseBuilding) IsAvailable(resourcesBuildings ResourcesBuildings, facilities Facilities, researches Researches, _ int) bool {
for ogameID, levelNeeded := range b.Requirements {
if ogameID.IsResourceBuilding() {
if resourcesBuildings.ByOGameID(ogameID) < levelNeeded {
for id, levelNeeded := range b.Requirements {
if id.IsResourceBuilding() {
if resourcesBuildings.ByID(id) < levelNeeded {
return false
}
} else if ogameID.IsFacility() {
if facilities.ByOGameID(ogameID) < levelNeeded {
} else if id.IsFacility() {
if facilities.ByID(id) < levelNeeded {
return false
}
} else if ogameID.IsTech() {
if researches.ByOGameID(ogameID) < levelNeeded {
} else if id.IsTech() {
if researches.ByID(id) < levelNeeded {
return false
}
}
Expand Down
14 changes: 7 additions & 7 deletions baseDefense.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ func (b BaseDefense) GetRequirements() map[ID]int {

// IsAvailable ...
func (b BaseDefense) IsAvailable(resourcesBuildings ResourcesBuildings, facilities Facilities, researches Researches, _ int) bool {
for ogameID, levelNeeded := range b.Requirements {
if ogameID.IsResourceBuilding() {
if resourcesBuildings.ByOGameID(ogameID) < levelNeeded {
for id, levelNeeded := range b.Requirements {
if id.IsResourceBuilding() {
if resourcesBuildings.ByID(id) < levelNeeded {
return false
}
} else if ogameID.IsFacility() {
if facilities.ByOGameID(ogameID) < levelNeeded {
} else if id.IsFacility() {
if facilities.ByID(id) < levelNeeded {
return false
}
} else if ogameID.IsTech() {
if researches.ByOGameID(ogameID) < levelNeeded {
} else if id.IsTech() {
if researches.ByID(id) < levelNeeded {
return false
}
}
Expand Down
16 changes: 8 additions & 8 deletions baseTechnology.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ func (b BaseTechnology) ConstructionTime(level, universeSpeed int, facilities Fa

// GetLevel ...
func (b BaseTechnology) GetLevel(resourcesBuildings ResourcesBuildings, facilities Facilities, researches Researches) int {
return researches.ByOGameID(b.ID)
return researches.ByID(b.ID)
}

// IsAvailable ...
func (b BaseTechnology) IsAvailable(resourcesBuildings ResourcesBuildings, facilities Facilities,
researches Researches, _ int) bool {
for ogameID, levelNeeded := range b.Requirements {
if ogameID.IsResourceBuilding() {
if resourcesBuildings.ByOGameID(ogameID) < levelNeeded {
for id, levelNeeded := range b.Requirements {
if id.IsResourceBuilding() {
if resourcesBuildings.ByID(id) < levelNeeded {
return false
}
} else if ogameID.IsFacility() {
if facilities.ByOGameID(ogameID) < levelNeeded {
} else if id.IsFacility() {
if facilities.ByID(id) < levelNeeded {
return false
}
} else if ogameID.IsTech() {
if researches.ByOGameID(ogameID) < levelNeeded {
} else if id.IsTech() {
if researches.ByID(id) < levelNeeded {
return false
}
}
Expand Down
20 changes: 10 additions & 10 deletions facilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ type Facilities struct {
SpaceDock int
}

// ByOGameID ...
func (f Facilities) ByOGameID(ogameID ID) int {
if ogameID == RoboticsFactory.ID {
// ByID ...
func (f Facilities) ByID(id ID) int {
if id == RoboticsFactory.ID {
return f.RoboticsFactory
} else if ogameID == Shipyard.ID {
} else if id == Shipyard.ID {
return f.Shipyard
} else if ogameID == ResearchLab.ID {
} else if id == ResearchLab.ID {
return f.ResearchLab
} else if ogameID == AllianceDepot.ID {
} else if id == AllianceDepot.ID {
return f.AllianceDepot
} else if ogameID == MissileSilo.ID {
} else if id == MissileSilo.ID {
return f.MissileSilo
} else if ogameID == NaniteFactory.ID {
} else if id == NaniteFactory.ID {
return f.NaniteFactory
} else if ogameID == Terraformer.ID {
} else if id == Terraformer.ID {
return f.Terraformer
} else if ogameID == SpaceDock.ID {
} else if id == SpaceDock.ID {
return f.SpaceDock
}
return 0
Expand Down
14 changes: 7 additions & 7 deletions gravitonTechnology.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ func (b gravitonTechnology) IsAvailable(resourcesBuildings ResourcesBuildings, f
if energy < 300000 {
return false
}
for ogameID, levelNeeded := range b.Requirements {
if ogameID.IsResourceBuilding() {
if resourcesBuildings.ByOGameID(ogameID) < levelNeeded {
for id, levelNeeded := range b.Requirements {
if id.IsResourceBuilding() {
if resourcesBuildings.ByID(id) < levelNeeded {
return false
}
} else if ogameID.IsFacility() {
if facilities.ByOGameID(ogameID) < levelNeeded {
} else if id.IsFacility() {
if facilities.ByID(id) < levelNeeded {
return false
}
} else if ogameID.IsTech() {
if researches.ByOGameID(ogameID) < levelNeeded {
} else if id.IsTech() {
if researches.ByID(id) < levelNeeded {
return false
}
}
Expand Down
62 changes: 31 additions & 31 deletions ogame.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ type Wrapper interface {
GetDefense(PlanetID) (Defenses, error)
GetShips(PlanetID) (ShipsInfos, error)
GetFacilities(PlanetID) (Facilities, error)
Build(planetID PlanetID, ogameID ID, nbr int) error
Build(planetID PlanetID, id ID, nbr int) error
BuildCancelable(PlanetID, ID) error
BuildProduction(planetID PlanetID, ogameID ID, nbr int) error
BuildProduction(planetID PlanetID, id ID, nbr int) error
BuildBuilding(planetID PlanetID, buildingID ID) error
BuildTechnology(planetID PlanetID, technologyID ID) error
BuildDefense(planetID PlanetID, defenseID ID, nbr int) error
Expand Down Expand Up @@ -1664,15 +1664,15 @@ func extractProduction(pageHTML string) ([]Quantifiable, error) {
if len(m) == 0 {
return []Quantifiable{}, nil
}
ogameIDint, _ := strconv.Atoi(m[1])
activeOgameID := ID(ogameIDint)
idInt, _ := strconv.Atoi(m[1])
activeID := ID(idInt)
activeNbr, _ := strconv.Atoi(active.Find("div.shipSumCount").Text())
res = append(res, Quantifiable{ID: activeOgameID, Nbr: activeNbr})
res = append(res, Quantifiable{ID: activeID, Nbr: activeNbr})
doc.Find("div#pqueue ul li").Each(func(i int, s *goquery.Selection) {
itemOgameIDstr, _ := s.Find("a").Attr("ref")
itemOgameID, _ := strconv.Atoi(itemOgameIDstr)
itemIDstr, _ := s.Find("a").Attr("ref")
itemID, _ := strconv.Atoi(itemIDstr)
itemNbr := parseInt(s.Find("span.number").Text())
res = append(res, Quantifiable{ID: ID(itemOgameID), Nbr: itemNbr})
res = append(res, Quantifiable{ID: ID(itemID), Nbr: itemNbr})
})
return res, nil
}
Expand Down Expand Up @@ -1712,27 +1712,27 @@ func (b *OGame) getResearch() Researches {
return extractResearch(pageHTML)
}

func (b *OGame) build(planetID PlanetID, ogameID ID, nbr int) error {
func (b *OGame) build(planetID PlanetID, id ID, nbr int) error {
var page string
if ogameID.IsDefense() {
if id.IsDefense() {
page = "defense"
} else if ogameID.IsShip() {
} else if id.IsShip() {
page = "shipyard"
} else if ogameID.IsBuilding() {
} else if id.IsBuilding() {
page = "resources"
} else if ogameID.IsTech() {
} else if id.IsTech() {
page = "research"
} else {
return errors.New("invalid id " + ogameID.String())
return errors.New("invalid id " + id.String())
}
planetIDStr := planetID.String()
payload := url.Values{
"modus": {"1"},
"type": {strconv.Itoa(int(ogameID))},
"type": {strconv.Itoa(int(id))},
}

// Techs don't have a token
if !ogameID.IsTech() {
if !id.IsTech() {
pageHTML := b.getPageContent(url.Values{"page": {page}, "cp": {planetIDStr}})
doc, err := goquery.NewDocumentFromReader(strings.NewReader(pageHTML))
if err != nil {
Expand All @@ -1745,7 +1745,7 @@ func (b *OGame) build(planetID PlanetID, ogameID ID, nbr int) error {
payload.Add("token", token)
}

if ogameID.IsDefense() || ogameID.IsShip() {
if id.IsDefense() || id.IsShip() {
payload.Add("menge", strconv.Itoa(nbr))
}

Expand All @@ -1758,18 +1758,18 @@ func (b *OGame) build(planetID PlanetID, ogameID ID, nbr int) error {
return nil
}

func (b *OGame) buildCancelable(planetID PlanetID, ogameID ID) error {
if !ogameID.IsBuilding() && !ogameID.IsTech() {
return errors.New("invalid id " + ogameID.String())
func (b *OGame) buildCancelable(planetID PlanetID, id ID) error {
if !id.IsBuilding() && !id.IsTech() {
return errors.New("invalid id " + id.String())
}
return b.build(planetID, ogameID, 0)
return b.build(planetID, id, 0)
}

func (b *OGame) buildProduction(planetID PlanetID, ogameID ID, nbr int) error {
if !ogameID.IsDefense() && !ogameID.IsShip() {
return errors.New("invalid id " + ogameID.String())
func (b *OGame) buildProduction(planetID PlanetID, id ID, nbr int) error {
if !id.IsDefense() && !id.IsShip() {
return errors.New("invalid id " + id.String())
}
return b.build(planetID, ogameID, nbr)
return b.build(planetID, id, nbr)
}

func (b *OGame) buildBuilding(planetID PlanetID, buildingID ID) error {
Expand Down Expand Up @@ -2613,24 +2613,24 @@ func (b *OGame) GetResearch() Researches {
}

// Build ...
func (b *OGame) Build(planetID PlanetID, ogameID ID, nbr int) error {
func (b *OGame) Build(planetID PlanetID, id ID, nbr int) error {
b.Lock()
defer b.Unlock()
return b.build(planetID, ogameID, nbr)
return b.build(planetID, id, nbr)
}

// BuildCancelable ...
func (b *OGame) BuildCancelable(planetID PlanetID, ogameID ID) error {
func (b *OGame) BuildCancelable(planetID PlanetID, id ID) error {
b.Lock()
defer b.Unlock()
return b.buildCancelable(planetID, ogameID)
return b.buildCancelable(planetID, id)
}

// BuildProduction ...
func (b *OGame) BuildProduction(planetID PlanetID, ogameID ID, nbr int) error {
func (b *OGame) BuildProduction(planetID PlanetID, id ID, nbr int) error {
b.Lock()
defer b.Unlock()
return b.buildProduction(planetID, ogameID, nbr)
return b.buildProduction(planetID, id, nbr)
}

// BuildBuilding ...
Expand Down
4 changes: 2 additions & 2 deletions planet.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (p *Planet) GetFacilities() (Facilities, error) {
}

// BuildCancelable ...
func (p *Planet) BuildCancelable(ogameID ID) error {
return p.ogame.BuildCancelable(p.ID, ogameID)
func (p *Planet) BuildCancelable(id ID) error {
return p.ogame.BuildCancelable(p.ID, id)
}

// BuildBuilding ...
Expand Down
36 changes: 18 additions & 18 deletions researches.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,39 @@ type Researches struct {
ArmourTechnology int
}

// ByOGameID ...
func (s Researches) ByOGameID(ogameID ID) int {
if ogameID == EnergyTechnology.ID {
// ByID ...
func (s Researches) ByID(id ID) int {
if id == EnergyTechnology.ID {
return s.EnergyTechnology
} else if ogameID == LaserTechnology.ID {
} else if id == LaserTechnology.ID {
return s.LaserTechnology
} else if ogameID == IonTechnology.ID {
} else if id == IonTechnology.ID {
return s.IonTechnology
} else if ogameID == HyperspaceTechnology.ID {
} else if id == HyperspaceTechnology.ID {
return s.HyperspaceTechnology
} else if ogameID == PlasmaTechnology.ID {
} else if id == PlasmaTechnology.ID {
return s.PlasmaTechnology
} else if ogameID == CombustionDrive.ID {
} else if id == CombustionDrive.ID {
return s.CombustionDrive
} else if ogameID == ImpulseDrive.ID {
} else if id == ImpulseDrive.ID {
return s.ImpulseDrive
} else if ogameID == HyperspaceDrive.ID {
} else if id == HyperspaceDrive.ID {
return s.HyperspaceDrive
} else if ogameID == EspionageTechnology.ID {
} else if id == EspionageTechnology.ID {
return s.EspionageTechnology
} else if ogameID == ComputerTechnology.ID {
} else if id == ComputerTechnology.ID {
return s.ComputerTechnology
} else if ogameID == Astrophysics.ID {
} else if id == Astrophysics.ID {
return s.Astrophysics
} else if ogameID == IntergalacticResearchNetwork.ID {
} else if id == IntergalacticResearchNetwork.ID {
return s.IntergalacticResearchNetwork
} else if ogameID == GravitonTechnology.ID {
} else if id == GravitonTechnology.ID {
return s.GravitonTechnology
} else if ogameID == WeaponsTechnology.ID {
} else if id == WeaponsTechnology.ID {
return s.WeaponsTechnology
} else if ogameID == ShieldingTechnology.ID {
} else if id == ShieldingTechnology.ID {
return s.ShieldingTechnology
} else if ogameID == ArmourTechnology.ID {
} else if id == ArmourTechnology.ID {
return s.ArmourTechnology
}
return 0
Expand Down
Loading

0 comments on commit ab5ad85

Please sign in to comment.