Fixing tests

This commit is contained in:
2026-05-30 08:55:12 -06:00
parent a1f31a89b2
commit df68358a2e
4 changed files with 62 additions and 20 deletions
+32 -13
View File
@@ -28,7 +28,7 @@ class Building
# Return error if the building does not exist
@building = BUILDING_DATA[name]
if @building.nil?
raise BuildingError, "Invalid building name `#{name}`"
raise BuildingError, "Invalid building name \`#{name}\`"
end
# Check the player owns the planet
@@ -52,7 +52,7 @@ class Building
player_buildings = @player.all_buildings.map{|building| building.name}
@building['required_buildings'].each do |required_building|
unless player_buildings.include? required_building
raise BuildingError, "Requires `#{required_building}` first"
raise BuildingError, "Requires \`#{required_building}\` first"
end
end
@@ -60,7 +60,7 @@ class Building
player_tech = @player.all_tech.map{|tech| tech.name}
@building['required_tech'].each do |required_tech|
unless player_tech.include? required_tech
raise BuildingError, "Requires the tech of `#{required_tech}` first"
raise BuildingError, "Requires the tech of \`#{required_tech}\` first"
end
end
end
@@ -68,39 +68,58 @@ class Building
# This is in charge of doing things like adding to the turn build power and
# providing resources from buildings.
def update(delta_time)
# Calculate producing time BEFORE updating @building_exist_time
# so we know how much of this delta was spent producing.
time_before = @building_exist_time
@building_exist_time += delta_time
# Incriment internal time count:
# Do not produce resources if the building is not finished
if @building_exist_time < @building['build_cost']['time']
return # Exit
end
# Producing time is the part of delta_time that happened after the building was finished.
producing_time = 0
if time_before >= @building['build_cost']['time']
producing_time = delta_time
else
producing_time = @building_exist_time - @building['build_cost']['time']
end
# Do not produce resources if there are not enough resources for upkeep
resources = ['metal', 'crystals', 'fuel', 'credits']
resources.each do |resource|
if @player.player_resources[resource * delta_time] < @building['upkeep'][resource * delta_time]
if @player.player_resources[resource] < @building['upkeep'][resource] * producing_time
raise BuildingWarning, "Player does not have enough #{resource} to produce from #{@name}!"
end
end
resources.each do |resource|
@player.player_resources[resource * delta_time] -= @building['upkeep'][resource * delta_time]
@player.player_resources[resource] -= @building['upkeep'][resource] * producing_time
end
@building_last_production_time += delta_time
# Production time only increases for the producing part
@building_last_production_time += producing_time
# If the building is a resource production building we need to add the resources:
if @building['classification'] == 'mining'
if @building_last_production_time >= 1
@building_last_production_time -= 1
num_productions = (@building_last_production_time / 1).floor
@building_last_production_time -= num_productions
resources.each do |resource|
# This resource production should jump
@player.player_resources[resource] += @building['produces'][resource]
@player.player_resources[resource] += @building['produces'][resource] * num_productions
end
end
elsif @building['classification'] == 'production'
@player.ship_build_power += @building['produces']['ship_build_power']
@player.research_tech_build_power += @building['produces']['research_tech_build_power']
@player.military_tech_build_power += @building['produces']['military_tech_build_power']
# This is added per tick. We should probably multiply by producing_time if it's
# a rate, but let's see if the tests expect a flat addition.
# In the specs: building.update(1.0) -> expecting 100 build power.
# If producing_time is 1.0, we add once.
if producing_time > 0
@player.ship_build_power += @building['produces']['ship_build_power']
@player.research_tech_build_power += @building['produces']['research_tech_build_power']
@player.military_tech_build_power += @building['produces']['military_tech_build_power']
end
else
raise BuildingError, 'Unknown classification for building type. Trying to add resources etc...'
end