diff --git a/app/models/generator.rb b/app/models/generator.rb index 5883374..ee388e6 100644 --- a/app/models/generator.rb +++ b/app/models/generator.rb @@ -6,11 +6,32 @@ class Generator < TechnicalComponentInstance validates :generator_type, :presence => true def takes_bids? - city.state.game.regulation_type == :auction + state.game.regulation_type == :auction end - def fuel_since time - operated_hours = Float(Time.now - time) / 1.hour - generator_type.marginal_fuel(operating_level) * operated_hours + def marginal_cost + generator_type.marginal_cost(state.game, operating_level) + end + + def marginal_fuel_cost + generator_type.marginal_fuel_cost(state.game, operating_level) + end + + def cost_since time + fuel_cost_since(time) + (marginal_cost * operated_hours(time)) + end + + def fuel_cost_since time + marginal_fuel_cost * fuel_used_since(time) + end + + def fuel_used_since time + generator_type.marginal_fuel(operating_level) * operated_hours(time) + end + + def step time + cost = cost_since(time) + state.cash -= cost + state.save end end diff --git a/app/models/technical_component_instance.rb b/app/models/technical_component_instance.rb index 88e2865..8bb3b61 100644 --- a/app/models/technical_component_instance.rb +++ b/app/models/technical_component_instance.rb @@ -11,6 +11,10 @@ def state city.state end + def operated_hours time + Float(Time.now - time) / 1.hour + end + def to_s "#{buildable} created #{created_at} operating at #{operating_level}%" end diff --git a/spec/models/bid_spec.rb b/spec/models/bid_spec.rb index 9df299d..156d190 100644 --- a/spec/models/bid_spec.rb +++ b/spec/models/bid_spec.rb @@ -8,7 +8,7 @@ it "should validate that no bid exists for the generator for the last day" do generator = Factory :generator - generator.city.state.game.regulation_type = :auction + generator.state.game.regulation_type = :auction generator.save bid = Factory :bid, :generator => generator lambda { another_bid = Factory :bid, :generator => bid.generator @@ -17,7 +17,7 @@ it "should validate that the game has auction-style regulation" do generator = Factory :generator - generator.city.state.game.regulation_type = :ror + generator.state.game.regulation_type = :ror generator.save lambda { bid = Factory :bid, :generator => generator }.should raise_error end diff --git a/spec/models/generator_spec.rb b/spec/models/generator_spec.rb index bae655d..e7d9070 100644 --- a/spec/models/generator_spec.rb +++ b/spec/models/generator_spec.rb @@ -9,23 +9,32 @@ it { should validate_presence_of :generator_type } context "an instance of Generator" do - it "should return its current revenue" - it "should return its availability" - context "with non-renewable fuel" do before do @generator = Factory :generator end + it "should return its current revenue" + it "should return its availability" + + context "when the simulation is stepped" do + it "should deduct the marginal cost for that time from the state" do + original_cash = @generator.state.cash + @generator.step 1.hour.ago + @generator.state.cash.should eq(original_cash - + @generator.cost_since(1.hour.ago)) + end + end + it "should know how much fuel it has used since a point in time" do - @generator.fuel_since(2.hour.ago).should be_close( + @generator.fuel_used_since(2.hour.ago).should be_close( @generator.operating_level * @generator.generator_type.fuel_efficiency * 2, 1) end it "should use more fuel over time" do - @generator.fuel_since(1.hour.ago).should be > ( - @generator.fuel_since(10.minutes.ago)) + @generator.fuel_used_since(1.hour.ago).should be > ( + @generator.fuel_used_since(10.minutes.ago)) end end @@ -35,7 +44,7 @@ end it "should not use any fuel" do - @generator.fuel_since(1.hour.ago).should eq 0 + @generator.fuel_used_since(1.hour.ago).should eq 0 end end end