This repository has been archived by the owner on Nov 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.rb
81 lines (67 loc) · 3.05 KB
/
game.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'simple'
class Game < ActiveRecord::Base
include SimpleExtensions
TIME_SCALE_FACTOR = [1 / 0.8, 1 / 0.2]
acts_as_limited
has_many :market_prices
has_many :markets, :through => :market_prices
has_many :allowed_generator_types
has_many :allowed_line_types
has_many :allowed_storage_device_types
has_many :generator_types, :through => :allowed_generator_types
has_many :line_types, :through => :allowed_line_types
has_many :storage_device_types, :through => :allowed_storage_device_types
has_many :states
has_many :users, :through => :states
has_many :maps, :through => :states
enum_attr :regulation_type, [:unregulated, :ror, :auction] do
label :ror => "Rate of Return"
end
validates :regulation_type, :presence => true
validates :speed, :presence => true, :percentage => true
validates :max_players, :numericality => {:greater_than_or_equal_to => 1},
:allow_nil => true
validates :max_line_capacity, :presence => true, :numericality => {
:greater_than_or_equal_to => 25, :less_than_or_equal_to => 2000}
validates :technology_cost, :presence => true, :percentage => true
validates :technology_reliability, :presence => true, :percentage => true
validates :power_factor, :presence => true, :percentage => true
validates :frequency, :presence => true, :numericality => {
:greater_than_or_equal_to => 10, :less_than_or_equal_to => 120}
validates :wind_speed, :presence => true, :percentage => true
validates :sunfall, :presence => true, :percentage => true
validates :water_flow, :presence => true, :percentage => true
validates :starting_capital, :presence => true, :numericality => {
:greater_than_or_equal_to => 0, :less_than_or_equal_to => 500000000000}
validates :interest_rate, :presence => true, :percentage => true
validates :reliability_constraint, :presence => true, :numericality => {
:greater_than_or_equal_to => 0, :less_than_or_equal_to => 10}
validates :fuel_cost, :presence => true, :percentage => true
validates :fuel_cost_volatility, :presence => true, :percentage => true
validates :workforce_reliability, :presence => true, :percentage => true
validates :workforce_cost, :presence => true, :percentage => true
validates :unionized, :presence => true
validates :carbon_allowance, :presence => true, :percentage => true
validates :tax_credit, :presence => true, :percentage => true
validates :renewable_requirement, :presence => true, :percentage => true
validates :political_stability, :presence => true, :percentage => true
validates :political_opposition, :presence => true, :percentage => true
validates :public_support, :presence => true, :percentage => true
def current_price market
market.current_price @game
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