Class: CEML::Cast

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(castable, first_guy) ⇒ Cast

Returns a new instance of Cast.



11
12
13
14
15
# File 'lib/ceml/models/cast.rb', line 11

def initialize(castable, first_guy)
  @castable = castable
  @castings = Hash.new{ |h,k| h[k] = Set.new }
  cast first_guy
end

Instance Attribute Details

#castingsObject (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
  (tagspecs.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.tagspec =~ guy and allowed > 0
  [ role.tagspec.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

#circleObject



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_atObject



68
69
70
71
# File 'lib/ceml/models/cast.rb', line 68

def closes_at
  return nil unless star && timewindow
  star[:ts] + timewindow
end

#folksObject



56
57
58
# File 'lib/ceml/models/cast.rb', line 56

def folks
  castings.values.map(&:to_a).flatten
end

#included?(guy) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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

#matchingsObject



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_idsObject



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

#starObject



64
65
66
# File 'lib/ceml/models/cast.rb', line 64

def star
  folks.first
end

#tagspecsObject



84
85
86
# File 'lib/ceml/models/cast.rb', line 84

def tagspecs
  roles.map(&:tagspec).uniq
end