Class: CEML::Cast
- Inherits:
-
Object
- Object
- CEML::Cast
- Extended by:
- Forwardable
- Defined in:
- lib/ceml/models/cast.rb
Overview
this contains the logic of building and approving a valid cast for an await statement, starting with a seed first_guy
Instance Attribute Summary collapse
-
#castings ⇒ Object
readonly
Returns the value of attribute castings.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #=~(c) ⇒ Object
- #affinity(role, guy) ⇒ Object
- #cast(guy) ⇒ Object
- #circle ⇒ Object
- #closes_at ⇒ Object
- #folks ⇒ Object
- #included?(guy) ⇒ Boolean
-
#initialize(castable, first_guy) ⇒ Cast
constructor
A new instance of Cast.
- #launchable? ⇒ Boolean
- #matchings ⇒ Object
- #player_ids ⇒ Object
- #room(role) ⇒ Object
- #star ⇒ Object
- #tagspecs ⇒ Object
Constructor Details
Instance Attribute Details
#castings ⇒ Object (readonly)
Returns the value of attribute castings.
8 9 10 |
# File 'lib/ceml/models/cast.rb', line 8 def castings @castings end |
Instance Method Details
#<=>(other) ⇒ Object
31 32 33 |
# File 'lib/ceml/models/cast.rb', line 31 def <=>(other) castings <=> other.castings end |
#=~(c) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/ceml/models/cast.rb', line 88 def =~(c) (!closes_at or closes_at < Time.unix) and (!circle or circle.contains?(c[:lat], c[:lng])) and (matchings.each{ |k,v| c[:matchables][k] == v }) and (.any?{ |ts| ts =~ c }) end |
#affinity(role, guy) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/ceml/models/cast.rb', line 22 def affinity role, guy casted_count = @castings[role.name].size needed = [role.range.min - casted_count, 0].max allowed = [role.range.max - casted_count, 0].max return [-1, -1, -1 ] unless role. =~ guy and allowed > 0 [ role..with.size, -needed, -allowed ] end |
#cast(guy) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/ceml/models/cast.rb', line 35 def cast guy return if included? guy or not self =~ guy best_role = roles.max_by{ |role| affinity(role, guy) } return if affinity(best_role, guy)[0] == -1 @castings[best_role.name] << guy guy[:roles] = best_role.name.to_sym end |
#circle ⇒ Object
78 79 80 81 82 |
# File 'lib/ceml/models/cast.rb', line 78 def circle return nil unless star && radius llr = star[:lat] && star[:lng] && [star[:lat], star[:lng], radius] ll && Circle.new(*llr) end |
#closes_at ⇒ Object
68 69 70 71 |
# File 'lib/ceml/models/cast.rb', line 68 def closes_at return nil unless star && timewindow star[:ts] + timewindow end |
#folks ⇒ Object
56 57 58 |
# File 'lib/ceml/models/cast.rb', line 56 def folks castings.values.map(&:to_a).flatten end |
#included?(guy) ⇒ Boolean
43 44 45 |
# File 'lib/ceml/models/cast.rb', line 43 def included? guy folks.any?{ |fellow| fellow[:id] == guy[:id] } end |
#launchable? ⇒ Boolean
47 48 49 50 51 52 53 54 |
# File 'lib/ceml/models/cast.rb', line 47 def launchable? case type when :accept roles.any?{ |role| castings[role.name].size >= 1 } when :await roles.all?{ |role| castings[role.name].size >= role.range.min } end end |
#matchings ⇒ Object
73 74 75 76 |
# File 'lib/ceml/models/cast.rb', line 73 def matchings return {} unless star Hash[ *matching.map{ |k| [k, star[:matchables][k]] } ] end |
#player_ids ⇒ Object
60 61 62 |
# File 'lib/ceml/models/cast.rb', line 60 def player_ids folks.map{ |p| p[:id] } end |
#room(role) ⇒ Object
17 18 19 20 |
# File 'lib/ceml/models/cast.rb', line 17 def room(role) casted_count = @castings[role.name].size [role.range.max - casted_count, 0].max end |
#star ⇒ Object
64 65 66 |
# File 'lib/ceml/models/cast.rb', line 64 def star folks.first end |
#tagspecs ⇒ Object
84 85 86 |
# File 'lib/ceml/models/cast.rb', line 84 def roles.map(&:tagspec).uniq end |