Skip to content

Commit

Permalink
Merge branch 'release/1.0.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Sep 14, 2018
2 parents c20ada1 + 99535d3 commit d945c16
Show file tree
Hide file tree
Showing 105 changed files with 1,125 additions and 1,205 deletions.
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ build: bindata-prod
build-linux: bindata-prod
GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -X main.version=0.0.0" -o bot cmd/scripts/main.go

.PHONY: bindata-dev bindata-prod build build-linux serve test lint
cover:
@mkdir -p ./coverage
@for pkg in $(PKGS) ; do \
go test \
-coverpkg=$$(go list -f '{{ join .Deps "\n" }}' $$pkg | grep '^$(PACKAGE)/' | grep -v '^$(PACKAGE)/vendor/' | tr '\n' ',')$$pkg \
-coverprofile="./coverage/`echo $$pkg | tr "/" "-"`.cover" $$pkg ;\
done
@gocovmerge ./coverage/*.cover > cover.out
@go tool cover -html=cover.out

.PHONY: bindata-dev bindata-prod build build-linux serve test lint cover
2 changes: 1 addition & 1 deletion allianceDepot.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ogame

// AllianceDepot ...
type allianceDepot struct {
BaseBuilding
}
Expand All @@ -11,5 +10,6 @@ func newAllianceDepot() *allianceDepot {
b.ID = AllianceDepotID
b.IncreaseFactor = 2.0
b.BaseCost = Resources{Metal: 20000, Crystal: 40000}

return b
}
1 change: 0 additions & 1 deletion antiBallisticMissiles.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ogame

// AntiBallisticMissiles ...
type antiBallisticMissiles struct {
BaseDefense
}
Expand Down
1 change: 0 additions & 1 deletion armourTechnology.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ogame

// ArmourTechnology ...
type armourTechnology struct {
BaseTechnology
}
Expand Down
1 change: 0 additions & 1 deletion astrophysics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ogame

// Astrophysics ...
type astrophysics struct {
BaseTechnology
}
Expand Down
2 changes: 1 addition & 1 deletion attackEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"
)

// AttackEvent ...
// AttackEvent all information available about an enemy attack
type AttackEvent struct {
MissionType MissionID
Origin Coordinate
Expand Down
28 changes: 28 additions & 0 deletions attackEvent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ogame

import (
"testing"
"time"

"github.com/magiconair/properties/assert"
)

func TestAttackEvent_String(t *testing.T) {
a := AttackEvent{
MissionType: 3,
Origin: Coordinate{1, 2, 3},
Destination: Coordinate{4, 5, 6},
ArrivalTime: time.Date(2018, 9, 11, 1, 2, 3, 4, time.UTC),
AttackerID: 456,
Missiles: 0,
Ships: &ShipsInfos{LargeCargo: 10},
}
expected := "" +
"Mission Type: 3\n" +
" Origin: [1:2:3]\n" +
" Destination: [4:5:6]\n" +
" ArrivalTime: 2018-09-11 01:02:03.000000004 +0000 UTC\n" +
" AttackerID: 456\n" +
" Missiles: 0"
assert.Equal(t, expected, a.String())
}
128 changes: 18 additions & 110 deletions base.go
Original file line number Diff line number Diff line change
@@ -1,127 +1,35 @@
package ogame

import (
"math"
"time"
)

// BaseShip ...
type BaseShip struct {
ID ID
Name string
StructuralIntegrity int
ShieldPower int
WeaponPower int
CargoCapacity int
BaseSpeed int
FuelConsumption int
RapidfireFrom map[ID]int
RapidfireAgainst map[ID]int
Requirements map[ID]int
Price Resources
// Base struct for all ogame objects
type Base struct {
ID ID
Name string
Requirements map[ID]int
}

// GetID ...
func (b BaseShip) GetID() ID {
// GetID returns the ogame id of the object
func (b Base) GetID() ID {
return b.ID
}

// GetName ...
func (b BaseShip) GetName() string {
// GetName returns the printable name of the object
func (b Base) GetName() string {
return b.Name
}

// GetStructuralIntegrity ...
func (b BaseShip) GetStructuralIntegrity(researches Researches) int {
return int(float64(b.StructuralIntegrity) * (1 + float64(researches.ArmourTechnology)*0.1))
}

// GetShieldPower ...
func (b BaseShip) GetShieldPower(researches Researches) int {
return int(float64(b.ShieldPower) * (1 + float64(researches.ShieldingTechnology)*0.1))
}

// GetWeaponPower ...
func (b BaseShip) GetWeaponPower(researches Researches) int {
return int(float64(b.WeaponPower) * (1 + float64(researches.WeaponsTechnology)*0.1))
}

// GetCargoCapacity ...
func (b BaseShip) GetCargoCapacity() int {
return b.CargoCapacity
}

// GetBaseSpeed ...
func (b BaseShip) GetBaseSpeed() int {
return b.BaseSpeed
}

// GetSpeed ...
func (b BaseShip) GetSpeed(techs Researches) int {
techDriveLvl := 0
if b.ID == SmallCargoID && techs.ImpulseDrive >= 5 {
return int(float64(b.BaseSpeed) + (float64(b.BaseSpeed)*0.2)*float64(techs.ImpulseDrive))
}
if minLvl, ok := b.Requirements[CombustionDrive.ID]; ok {
techDriveLvl = techs.CombustionDrive
if techDriveLvl < minLvl {
techDriveLvl = minLvl
}
return int(float64(b.BaseSpeed) + (float64(b.BaseSpeed)*0.1)*float64(techDriveLvl))
} else if minLvl, ok := b.Requirements[ImpulseDrive.ID]; ok {
techDriveLvl = techs.ImpulseDrive
if techDriveLvl < minLvl {
techDriveLvl = minLvl
}
return int(float64(b.BaseSpeed) + (float64(b.BaseSpeed)*0.2)*float64(techDriveLvl))
} else if minLvl, ok := b.Requirements[HyperspaceDrive.ID]; ok {
techDriveLvl = techs.HyperspaceDrive
if techDriveLvl < minLvl {
techDriveLvl = minLvl
}
return int(float64(b.BaseSpeed) + (float64(b.BaseSpeed)*0.3)*float64(techDriveLvl))
}
return int(float64(b.BaseSpeed) + (float64(b.BaseSpeed)*0.2)*float64(techDriveLvl))
}

// GetFuelConsumption ...
func (b BaseShip) GetFuelConsumption() int {
return b.FuelConsumption
}

// GetRapidfireFrom ...
func (b BaseShip) GetRapidfireFrom() map[ID]int {
return b.RapidfireFrom
}

// GetRapidfireAgainst ...
func (b BaseShip) GetRapidfireAgainst() map[ID]int {
return b.RapidfireAgainst
}

// GetPrice ...
func (b BaseShip) GetPrice(nbr int) Resources {
return b.Price.Mul(nbr)
}

// ConstructionTime ...
func (b BaseShip) ConstructionTime(nbr, universeSpeed int, facilities Facilities) time.Duration {
shipyardLvl := float64(facilities.Shipyard)
naniteLvl := float64(facilities.NaniteFactory)
hours := float64(b.StructuralIntegrity) / (2500 * (1 + shipyardLvl) * float64(universeSpeed) * math.Pow(2, naniteLvl))
secs := hours * 3600
return time.Duration(int(math.Floor(secs))*nbr) * time.Second
}

// GetRequirements ...
func (b BaseShip) GetRequirements() map[ID]int {
// GetRequirements returns the requirements to have this object available
func (b Base) GetRequirements() map[ID]int {
return b.Requirements
}

// IsAvailable ...
func (b BaseShip) IsAvailable(_ ResourcesBuildings, facilities Facilities, researches Researches, _ int) bool {
// IsAvailable returns either or not the object is available to us
func (b Base) IsAvailable(resourcesBuildings ResourcesBuildings, facilities Facilities, researches Researches, _ int) bool {
for id, levelNeeded := range b.Requirements {
if id.IsFacility() {
if id.IsResourceBuilding() {
if resourcesBuildings.ByID(id) < levelNeeded {
return false
}
} else if id.IsFacility() {
if facilities.ByID(id) < levelNeeded {
return false
}
Expand Down
61 changes: 4 additions & 57 deletions baseBuilding.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,12 @@ import (
"time"
)

// BaseBuilding ...
// BaseBuilding base struct for buildings
type BaseBuilding struct {
ID ID
Name string
BaseCost Resources
IncreaseFactor float64
Requirements map[ID]int
BaseLevelable
}

// GetID ...
func (b BaseBuilding) GetID() ID {
return b.ID
}

// GetName ...
func (b BaseBuilding) GetName() string {
return b.Name
}

// GetRequirements ...
func (b BaseBuilding) GetRequirements() map[ID]int {
return b.Requirements
}

// GetPrice ...
func (b BaseBuilding) GetPrice(level int) Resources {
return Resources{
Metal: buildingCost(b.BaseCost.Metal, b.IncreaseFactor, level),
Crystal: buildingCost(b.BaseCost.Crystal, b.IncreaseFactor, level),
Deuterium: buildingCost(b.BaseCost.Deuterium, b.IncreaseFactor, level),
Energy: buildingCost(b.BaseCost.Energy, b.IncreaseFactor, level),
}
}

// ConstructionTime ...
// ConstructionTime returns the duration it takes to build given level
func (b BaseBuilding) ConstructionTime(level, universeSpeed int, facilities Facilities) time.Duration {
price := b.GetPrice(level)
metalCost := float64(price.Metal)
Expand All @@ -54,7 +25,7 @@ func (b BaseBuilding) ConstructionTime(level, universeSpeed int, facilities Faci
return time.Duration(int(math.Floor(secs))) * time.Second
}

// GetLevel ...
// GetLevel returns current level of a building
func (b BaseBuilding) GetLevel(resourcesBuildings ResourcesBuildings, facilities Facilities, researches Researches) int {
if b.ID.IsResourceBuilding() {
return resourcesBuildings.ByID(b.ID)
Expand All @@ -63,27 +34,3 @@ func (b BaseBuilding) GetLevel(resourcesBuildings ResourcesBuildings, facilities
}
return 0
}

// IsAvailable ...
func (b BaseBuilding) IsAvailable(resourcesBuildings ResourcesBuildings, facilities Facilities, researches Researches, _ int) bool {
for id, levelNeeded := range b.Requirements {
if id.IsResourceBuilding() {
if resourcesBuildings.ByID(id) < levelNeeded {
return false
}
} else if id.IsFacility() {
if facilities.ByID(id) < levelNeeded {
return false
}
} else if id.IsTech() {
if researches.ByID(id) < levelNeeded {
return false
}
}
}
return true
}

func buildingCost(baseCost int, increaseFactor float64, level int) int {
return int(float64(baseCost) * math.Pow(increaseFactor, float64(level-1)))
}
15 changes: 15 additions & 0 deletions baseBuilding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ogame

import (
"testing"

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

func TestBaseBuilding_GetLevel(t *testing.T) {
var bb struct {
BaseBuilding
}
bb.ID = ID(123456)
assert.Equal(t, 0, bb.GetLevel(ResourcesBuildings{}, Facilities{}, Researches{}))
}
50 changes: 50 additions & 0 deletions baseDefender.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ogame

import (
"math"
"time"
)

// BaseDefender base for defender units (ships, defenses)
type BaseDefender struct {
Base
StructuralIntegrity int
ShieldPower int
WeaponPower int
RapidfireFrom map[ID]int
Price Resources
}

// GetStructuralIntegrity returns structural integrity of a defender unit
func (b BaseDefender) GetStructuralIntegrity(researches Researches) int {
return int(float64(b.StructuralIntegrity) * (1 + float64(researches.ArmourTechnology)*0.1))
}

// GetShieldPower returns shield power of a defender unit
func (b BaseDefender) GetShieldPower(researches Researches) int {
return int(float64(b.ShieldPower) * (1 + float64(researches.ShieldingTechnology)*0.1))
}

// GetWeaponPower returns weapon power of a defender unit
func (b BaseDefender) GetWeaponPower(researches Researches) int {
return int(float64(b.WeaponPower) * (1 + float64(researches.WeaponsTechnology)*0.1))
}

// GetRapidfireFrom returns which ships have rapid fire against the defender unit
func (b BaseDefender) GetRapidfireFrom() map[ID]int {
return b.RapidfireFrom
}

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

// GetPrice returns the price of nbr defender units
func (b BaseDefender) GetPrice(nbr int) Resources {
return b.Price.Mul(nbr)
}
Loading

0 comments on commit d945c16

Please sign in to comment.