Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

Commit

Permalink
Add step method to generator to deduct cash from state.
Browse files Browse the repository at this point in the history
Fixed #71.
  • Loading branch information
Christopher Peplin committed Oct 22, 2010
1 parent e61362e commit 4e7f893
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
29 changes: 25 additions & 4 deletions app/models/generator.rb
Expand Up @@ -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
4 changes: 4 additions & 0 deletions app/models/technical_component_instance.rb
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/models/bid_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
23 changes: 16 additions & 7 deletions spec/models/generator_spec.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 4e7f893

Please sign in to comment.