From ea3da634343ea35fd804b3b6f8488bc29006aaea Mon Sep 17 00:00:00 2001 From: Christopher Peplin Date: Wed, 20 Oct 2010 17:11:33 -0400 Subject: [PATCH] Add scaled time calculation. Fixed #39. --- app/models/game.rb | 21 ++++++++++++++++----- db/schema.rb | 2 +- lib/simple.rb | 6 ++++++ spec/models/game_spec.rb | 17 +++++++++++------ 4 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 lib/simple.rb diff --git a/app/models/game.rb b/app/models/game.rb index a44884f..b6dd6b7 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -1,5 +1,9 @@ +require 'simple' + class Game < ActiveRecord::Base - TIME_SCALE_FACTOR = 1 / 0.2 + include SimpleExtensions + + TIME_SCALE_FACTOR = [1 / 0.8, 1 / 0.2] acts_as_limited has_many :market_prices @@ -56,15 +60,22 @@ def current_price market market.current_price @game end - def time_since time - TIME_SCALE_FACTOR * speed * (Time.now - time) - end - def in_progress? not ended end + def time_since_update + time_since updated_at + end + def to_s "#{states.count} confirmed players, #{started ? "started #{started}" : "not started"}" end + + private + + def time_since time + range_map(speed, 0, 100, TIME_SCALE_FACTOR[0], + TIME_SCALE_FACTOR[1]) * (Time.now - time) + end end diff --git a/db/schema.rb b/db/schema.rb index 93f4180..3d7255f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -47,7 +47,7 @@ t.integer "x", :null => false t.integer "y", :null => false t.integer "elevation", :default => 0, :null => false - t.string "type", :null => false + t.string "block_type", :null => false t.float "wind_speed", :default => 0.0, :null => false t.integer "water_flow", :default => 0, :null => false t.integer "sunfall", :default => 5125, :null => false diff --git a/lib/simple.rb b/lib/simple.rb new file mode 100644 index 0000000..e3fcfea --- /dev/null +++ b/lib/simple.rb @@ -0,0 +1,6 @@ +module SimpleExtensions + def range_map(value, istart, istop, ostart, ostop) + ostart + (ostop - ostart) * ( + Float((value - istart)) / Float((istop - istart))) + end +end diff --git a/spec/models/game_spec.rb b/spec/models/game_spec.rb index 0335815..ed7f0ad 100644 --- a/spec/models/game_spec.rb +++ b/spec/models/game_spec.rb @@ -73,21 +73,26 @@ (@game.current_price @market).should eq(@market.current_price @game) end - context "should know how much game-relative time has passed" do + context "that has been updated" do before do @game.updated_at = Time.now - 60 @now = Time.now + Time.stubs(:now).returns(@now) end - it "with a real-time speed" do + it "should scale time passed based on game speed" do @game.speed = 0 - assert_equal @game.time_since(@now), @now - @game.updated_at + real_time = @game.time_since_update + @game.speed = 1 + fast_time = @game.time_since_update + real_time.should be > (@now - @game.updated_at) + fast_time.should be > (@now - @game.updated_at) + real_time.should be < fast_time end - it "with maxiumum speed" do + it "should know how much time has passed with maxiumum speed" do @game.speed = 1 - assert_equal @game.time_since(@now), - Game::TIME_SCALE_FACTOR * (@now - @game.updated_at) + @game.time_since_update.should be > (@now - @game.updated_at) end end end