Skip to content

Commit

Permalink
Merge branch 'release/1.1.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Sep 21, 2018
2 parents 891345d + 374a437 commit 16616d9
Show file tree
Hide file tree
Showing 24 changed files with 454 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
dist
cmd/scripts/bindata
cmd/bot1
*.cover
cover.out
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import "os"
import "github.com/alaingilbert/ogame"

func main() {
universe := os.Getenv("UNIVERSE")
username := os.Getenv("USERNAME")
password := os.Getenv("PASSWORD")
language := os.Getenv("LANGUAGE")
universe := os.Getenv("UNIVERSE") // eg: Bellatrix
username := os.Getenv("USERNAME") // eg: [email protected]
password := os.Getenv("PASSWORD") // eg: *****
language := os.Getenv("LANGUAGE") // eg: en
bot, _ := ogame.New(universe, username, password, language)
attacked := bot.IsUnderAttack()
fmt.Println(attacked) // False
Expand Down
35 changes: 35 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ogame

import (
"bytes"
"io/ioutil"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

// RoundTripFunc .
type RoundTripFunc func(req *http.Request) *http.Response

// RoundTrip .
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}

func TestOgameClient_Do(t *testing.T) {
c := ogameClient{UserAgent: "test", Client: http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response {
// Test request parameters
return &http.Response{
StatusCode: 200,
// Send response to be tested
Body: ioutil.NopCloser(bytes.NewBufferString(`OK`)),
// Must be set to non-nil value or it panics
Header: make(http.Header),
}
})}}
req, _ := http.NewRequest("GET", "http://test.com", nil)
_, err := c.Do(req)
assert.Nil(t, err)
assert.Equal(t, "test", req.Header.Get("User-Agent"))
}
52 changes: 50 additions & 2 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,62 @@ import "strconv"
type MissionID int

func (m MissionID) String() string {
return strconv.Itoa(int(m))
switch m {
case Attack:
return "Attack"
case GroupedAttack:
return "GroupedAttack"
case Transport:
return "Transport"
case Park:
return "Park"
case ParkInThatAlly:
return "ParkInThatAlly"
case Spy:
return "Spy"
case Colonize:
return "Colonize"
case RecycleDebrisField:
return "RecycleDebrisField"
case Destroy:
return "Destroy"
case MissileAttack:
return "MissileAttack"
case Expedition:
return "Expedition"
default:
return strconv.Itoa(int(m))
}
}

// Speed represent a fleet speed
type Speed int

func (s Speed) String() string {
return strconv.Itoa(int(s))
switch s {
case TenPercent:
return "10%"
case TwentyPercent:
return "20%"
case ThirtyPercent:
return "30%"
case FourtyPercent:
return "40%"
case FiftyPercent:
return "50%"
case SixtyPercent:
return "60%"
case SeventyPercent:
return "70%"
case EightyPercent:
return "80%"
case NinetyPercent:
return "90%"
case HundredPercent:
return "100%"
default:
return strconv.Itoa(int(s))
}
}

// OGame constants
Expand Down
36 changes: 36 additions & 0 deletions constants_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ogame

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestConstants_Speed_String(t *testing.T) {
assert.Equal(t, "10%", Speed(1).String())
assert.Equal(t, "20%", Speed(2).String())
assert.Equal(t, "30%", Speed(3).String())
assert.Equal(t, "40%", Speed(4).String())
assert.Equal(t, "50%", Speed(5).String())
assert.Equal(t, "60%", Speed(6).String())
assert.Equal(t, "70%", Speed(7).String())
assert.Equal(t, "80%", Speed(8).String())
assert.Equal(t, "90%", Speed(9).String())
assert.Equal(t, "100%", Speed(10).String())
assert.Equal(t, "11", Speed(11).String())
}

func TestConstants_MissionID_String(t *testing.T) {
assert.Equal(t, "Attack", MissionID(1).String())
assert.Equal(t, "GroupedAttack", MissionID(2).String())
assert.Equal(t, "Transport", MissionID(3).String())
assert.Equal(t, "Park", MissionID(4).String())
assert.Equal(t, "ParkInThatAlly", MissionID(5).String())
assert.Equal(t, "Spy", MissionID(6).String())
assert.Equal(t, "Colonize", MissionID(7).String())
assert.Equal(t, "RecycleDebrisField", MissionID(8).String())
assert.Equal(t, "Destroy", MissionID(9).String())
assert.Equal(t, "MissileAttack", MissionID(10).String())
assert.Equal(t, "Expedition", MissionID(15).String())
assert.Equal(t, "16", MissionID(16).String())
}
4 changes: 2 additions & 2 deletions crystalMine.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func (b *crystalMine) EnergyConsumption(level int) int {
}

// Production returns the crystal production of the mine
func (b *crystalMine) Production(universeSpeed int, productionRatio float64, level int) int {
func (b *crystalMine) Production(universeSpeed int, productionRatio float64, plasmaTech, level int) int {
basicIncome := 15.0
levelProduction := 20 * float64(level) * math.Pow(1.1, float64(level)) * float64(universeSpeed)
levelProduction := 20 * float64(universeSpeed) * (1 + float64(plasmaTech)*0.0066) * float64(level) * math.Pow(1.1, float64(level))
production := int(levelProduction*productionRatio + (basicIncome * float64(universeSpeed)))
return production
}
10 changes: 10 additions & 0 deletions crystalMine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ func TestCrystalMineConstructionTime(t *testing.T) {
cm := newCrystalMine()
assert.Equal(t, 75*time.Second, cm.ConstructionTime(5, 6, Facilities{}))
}

func TestCrystalMine_EnergyConsumption(t *testing.T) {
cm := newCrystalMine()
assert.Equal(t, 736, cm.EnergyConsumption(16))
}

func TestCrystalMine_Production(t *testing.T) {
cm := newCrystalMine()
assert.Equal(t, 37921+1752+105, cm.Production(7, 1, 7, 25))
}
34 changes: 34 additions & 0 deletions defence_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ogame

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestDefence_String(t *testing.T) {
r := DefensesInfos{
RocketLauncher: 1,
LightLaser: 2,
HeavyLaser: 3,
GaussCannon: 4,
IonCannon: 5,
PlasmaTurret: 6,
SmallShieldDome: 7,
LargeShieldDome: 8,
AntiBallisticMissiles: 9,
InterplanetaryMissiles: 10,
}
expected := "\n" +
" Rocket Launcher: 1\n" +
" Light Laser: 2\n" +
" Heavy Laser: 3\n" +
" Gauss Cannon: 4\n" +
" Ion Cannon: 5\n" +
" Plasma Turret: 6\n" +
" Small Shield Dome: 7\n" +
" Large Shield Dome: 8\n" +
"Anti Ballistic Missiles: 9\n" +
"Interplanetary Missiles: 10"
assert.Equal(t, expected, r.String())
}
4 changes: 2 additions & 2 deletions deuteriumSynthesizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ func (b *deuteriumSynthesizer) EnergyConsumption(level int) int {
}

// Production returns the deuterium production of the mine
func (b *deuteriumSynthesizer) Production(universeSpeed, maxTemp int, productionRatio float64, level int) int {
return int(math.Round(10 * float64(level) * math.Pow(1.1, float64(level)) * (1.44 - 0.004*float64(maxTemp)) * float64(universeSpeed) * productionRatio))
func (b *deuteriumSynthesizer) Production(universeSpeed, avgTemp int, productionRatio float64, level int) int {
return int(math.Round(10 * float64(level) * math.Pow(1.1, float64(level)) * (-0.004*float64(avgTemp) + 1.36) * float64(universeSpeed) * productionRatio))
}
10 changes: 10 additions & 0 deletions deuteriumSynthesizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ func TestDeuteriumSynthesizerConstructionTime(t *testing.T) {
ds := newDeuteriumSynthesizer()
assert.Equal(t, 1845*time.Second, ds.ConstructionTime(9, 6, Facilities{}))
}

func TestDeuteriumSynthesizer_Production(t *testing.T) {
ds := newDeuteriumSynthesizer()
assert.Equal(t, 29760, ds.Production(7, (-23+17)/2, 1, 26))
}

func TestDeuteriumSynthesizer_EnergyConsumption(t *testing.T) {
ds := newDeuteriumSynthesizer()
assert.Equal(t, 6198, ds.EnergyConsumption(26))
}
6 changes: 6 additions & 0 deletions energyTechnology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ func TestEnergyTechnologyConstructionTime(t *testing.T) {
ct := mm.ConstructionTime(5, 7, Facilities{ResearchLab: 3})
assert.Equal(t, 1645*time.Second, ct)
}

func TestEnergyTechnology_GetLevel(t *testing.T) {
et := newEnergyTechnology()
l := et.GetLevel(ResourcesBuildings{}, Facilities{}, Researches{EnergyTechnology: 4})
assert.Equal(t, 4, l)
}
52 changes: 52 additions & 0 deletions facilities_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ogame

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFacilities_ByID(t *testing.T) {
f := Facilities{
RoboticsFactory: 1,
Shipyard: 2,
ResearchLab: 3,
AllianceDepot: 4,
MissileSilo: 5,
NaniteFactory: 6,
Terraformer: 7,
SpaceDock: 8,
}
assert.Equal(t, 1, f.ByID(RoboticsFactoryID))
assert.Equal(t, 2, f.ByID(ShipyardID))
assert.Equal(t, 3, f.ByID(ResearchLabID))
assert.Equal(t, 4, f.ByID(AllianceDepotID))
assert.Equal(t, 5, f.ByID(MissileSiloID))
assert.Equal(t, 6, f.ByID(NaniteFactoryID))
assert.Equal(t, 7, f.ByID(TerraformerID))
assert.Equal(t, 8, f.ByID(SpaceDockID))
assert.Equal(t, 0, f.ByID(ID(12345)))
}

func TestFacilities_String(t *testing.T) {
f := Facilities{
RoboticsFactory: 1,
Shipyard: 2,
ResearchLab: 3,
AllianceDepot: 4,
MissileSilo: 5,
NaniteFactory: 6,
Terraformer: 7,
SpaceDock: 8,
}
expected := "\n" +
"RoboticsFactory: 1\n" +
" Shipyard: 2\n" +
" Research Lab: 3\n" +
" Alliance Depot: 4\n" +
" Missile Silo: 5\n" +
" Nanite Factory: 6\n" +
" Terraformer: 7\n" +
" Space Dock: 8"
assert.Equal(t, expected, f.String())
}
11 changes: 11 additions & 0 deletions fleetid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ogame

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFleetID_String(t *testing.T) {
assert.Equal(t, "12345", FleetID(12345).String())
}
5 changes: 5 additions & 0 deletions fusionReactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ func TestFusionReactor_IsAvailable(t *testing.T) {
assert.True(t, fr.IsAvailable(ResourcesBuildings{DeuteriumSynthesizer: 5}, Facilities{}, Researches{EnergyTechnology: 3}, 0))
assert.True(t, fr.IsAvailable(ResourcesBuildings{DeuteriumSynthesizer: 6}, Facilities{}, Researches{EnergyTechnology: 4}, 0))
}

func TestFusionReactor_Production(t *testing.T) {
fr := newFusionReactor()
assert.Equal(t, 3002, fr.Production(12, 13))
}
5 changes: 5 additions & 0 deletions id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"github.com/stretchr/testify/assert"
)

func TestID_IsSet(t *testing.T) {
assert.True(t, AllianceDepotID.IsSet())
assert.False(t, ID(0).IsSet())
}

func TestID_Int(t *testing.T) {
assert.Equal(t, 34, AllianceDepotID.Int())
}
Expand Down
6 changes: 3 additions & 3 deletions metalMine.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func (b *metalMine) EnergyConsumption(level int) int {
}

// Production returns the metal production of the mine
func (b *metalMine) Production(universeSpeed int, productionRatio float64, level int) int {
func (b *metalMine) Production(universeSpeed int, productionRatio float64, plasmaTech, level int) int {
basicIncome := 30.0 * float64(universeSpeed)
levelProduction := basicIncome * float64(level) * math.Pow(1.1, float64(level))
production := int(levelProduction*productionRatio + basicIncome)
levelProduction := 30.0 * (1.0 + (float64(plasmaTech) / 100.0)) * float64(universeSpeed) * float64(level) * math.Pow(1.1, float64(level))
production := int((levelProduction * productionRatio) + basicIncome)
return production
}
14 changes: 10 additions & 4 deletions metalMine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (

func TestMetalMineProduction(t *testing.T) {
mm := newMetalMine()
assert.Equal(t, 30, mm.Production(1, 1, 0))
assert.Equal(t, 63, mm.Production(1, 1, 1))
assert.Equal(t, 120, mm.Production(4, 1, 0))
assert.Equal(t, 252, mm.Production(4, 1, 1))
assert.Equal(t, 30, mm.Production(1, 1, 0, 0))
assert.Equal(t, 63, mm.Production(1, 1, 0, 1))
assert.Equal(t, 120, mm.Production(4, 1, 0, 0))
assert.Equal(t, 252, mm.Production(4, 1, 0, 1))
assert.Equal(t, 96606+6762+210, mm.Production(7, 1, 7, 29))
}

func TestMetalMineConstructionTime(t *testing.T) {
Expand All @@ -26,3 +27,8 @@ func TestMetalMine_GetLevel(t *testing.T) {
assert.Equal(t, 0, mm.GetLevel(ResourcesBuildings{}, Facilities{}, Researches{}))
assert.Equal(t, 3, mm.GetLevel(ResourcesBuildings{MetalMine: 3}, Facilities{}, Researches{}))
}

func TestMetalMine_EnergyConsumption(t *testing.T) {
mm := newMetalMine()
assert.Equal(t, 4601, mm.EnergyConsumption(29))
}
Loading

0 comments on commit 16616d9

Please sign in to comment.