add calc.go Growths in time, Mines costs. Applied to game

This commit is contained in:
arnaucube
2019-06-15 12:23:10 +02:00
parent eec7019497
commit 80fe8883b1
7 changed files with 265 additions and 78 deletions

View File

@@ -107,7 +107,7 @@ func (u *User) GetResources() (*Resources, error) {
return nil, err
}
// calculate Delta time = currentTime - u.LastUpdated
delta := time.Since(u.LastUpdated)
delta := time.Since(u.LastUpdated).Seconds()
// get planets
planets, err := u.GetPlanets()
@@ -119,11 +119,10 @@ func (u *User) GetResources() (*Resources, error) {
// and calculate growth = ResourcePlant.Level for each planet
var metalGrowth, crystalGrowth, deuteriumGrowth, energyGrowth int64
for _, planet := range planets {
// TODO find correct formulas
metalGrowth = metalGrowth + ((1 + planet.Buildings["metalmine"]) * int64(delta))
crystalGrowth = crystalGrowth + ((1 + planet.Buildings["crystalmine"]) * int64(delta))
deuteriumGrowth = deuteriumGrowth + ((1 + planet.Buildings["deuteriummine"]) * int64(delta))
energyGrowth = energyGrowth + ((1 + planet.Buildings["energymine"]) * int64(delta))
metalGrowth = metalGrowth + MetalGrowth(planet.Buildings["metalmine"], int64(delta))
crystalGrowth = crystalGrowth + MetalGrowth(planet.Buildings["crystalmine"], int64(delta))
deuteriumGrowth = deuteriumGrowth + MetalGrowth(planet.Buildings["deuteriummine"], int64(delta))
energyGrowth = energyGrowth + MetalGrowth(planet.Buildings["energymine"], int64(delta))
}
// calculate newAmount = oldAmount + growth
u.Resources.Metal = u.Resources.Metal + metalGrowth
@@ -171,3 +170,21 @@ func (u *User) SpendResources(r Resources) error {
}
return nil
}
func (u *User) GetBuildingCost(planet Planet, building string) (Resources, error) {
switch building {
case "metalmine":
return MetalMineCost(planet.Buildings["metalmine"] + 1), nil
case "crystalmine":
return CrystalMineCost(planet.Buildings["crystalmine"] + 1), nil
case "deuteriummine":
return DeuteriumMineCost(planet.Buildings["deuteriummine"] + 1), nil
case "energymine":
return EnergyMineCost(planet.Buildings["energymine"] + 1), nil
case "ressearchlab":
return RessearchLabCost(planet.Buildings["ressearchlab"] + 1), nil
default:
return Resources{}, errors.New("building not found")
}
}