Module: CEML::Script

Extended by:
Forwardable
Defined in:
lib/ceml/script.rb

Defined Under Namespace

Classes: DefaultDP

Instance Method Summary collapse

Instance Method Details

#allowed_rolesObject



89
90
91
92
93
# File 'lib/ceml/script.rb', line 89

def allowed_roles
  allowed_roles = [:organizer, :agents, :both, :all, :everyone, :each, :players]
  allowed_roles += cast.rolenames
  allowed_roles.uniq
end

#asks(roles) ⇒ Object



122
123
124
125
# File 'lib/ceml/script.rb', line 122

def asks(roles)
  return [] unless instructions
  instructions.i_asks([*roles])
end

#availabilities(potential_count, committed_count = 0) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ceml/script.rb', line 42

def availabilities(potential_count, committed_count = 0)
  return {} unless script.dramatis_personae
  min        = script.dramatis_personae.min
  needed     = min - committed_count
  possible   = potential_count + committed_count
  yesprob    = 0.7  # just make this up for now
  odds       = likelihood(yesprob, needed, potential_count)

  {
    :odds => odds, :color => color(odds),
    :availability_counts => {
      :total => possible,
      :unknown => potential_count
    },
    :estimated_size => yesprob * potential_count + committed_count,
    :needed => min
  }
end

#bytecodeObject



111
112
113
114
115
116
117
118
119
120
# File 'lib/ceml/script.rb', line 111

def bytecode
  code = [[[:all], :start]]
  if !instructions and title
    code.concat [[[:all], :null_assign], [[:all], :complete_assign]]
  elsif instructions
    instructions.list.each{ |inst| code.concat inst.bytecode } if instructions
  end
  code << [[:all], :finish]
  code
end

#castObject Also known as: dramatis_personae



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

def cast
  return casting_statement if respond_to? :casting_statement
  return DefaultDP.new nil, [:agents], 0, :gather
end

#color(odds) ⇒ Object

likelihoods =



23
24
25
26
27
# File 'lib/ceml/script.rb', line 23

def color(odds)
  return :red    if odds < 0.1
  return :yellow if odds < 0.4
  return :green
end

#concludes_immediately?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/ceml/script.rb', line 137

def concludes_immediately?
  !title and instructions.asks([:agents]).empty?
end

#expand_roles(roles) ⇒ Object



85
86
87
# File 'lib/ceml/script.rb', line 85

def expand_roles(roles)
  roles.map{ |r| r == :agent ? [:agent, :agents] : r }.flatten.concat([:both, :all, :everyone])
end

#instructionsObject

instructions =



100
101
102
103
104
# File 'lib/ceml/script.rb', line 100

def instructions
  return self if Instructions === self
  return super if defined?(super)
  nil
end

#instructions_for(roles) ⇒ Object



106
107
108
109
# File 'lib/ceml/script.rb', line 106

def instructions_for(roles)
  return [] unless instructions
  return instructions.for(expand_roles(roles))
end

#labelObject



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ceml/script.rb', line 185

def label
  return title || begin
    return "unknown" unless simple?
    if q = instructions.asks(:agents).first
      "Question: #{q.text}"
    elsif tell = instructions.tell(:agents)
      "Message: #{tell}" if tell
    else
      "unknown"
    end
  end
end

#likelihood(yesprob, needed, psize) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ceml/script.rb', line 29

def likelihood(yesprob, needed, psize)
  return 1 if needed <= 0
  return 0 if psize < needed
  return (needed..psize).inject(0) do |memo, yes_count|
    memo + begin
      no_count = psize - yes_count
      ways_it_could_happen = psize.choose(yes_count)
      prob_of_each = (yesprob ** yes_count) * ((1 - yesprob) ** no_count)
      ways_it_could_happen * prob_of_each
    end
  end
end

#message?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/ceml/script.rb', line 181

def message?
  type == 'message'
end

#nabs?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/ceml/script.rb', line 14

def nabs?
  cast.type == :nab or !title
end

#nameObject



163
164
165
# File 'lib/ceml/script.rb', line 163

def name
  title || label
end

#paramsObject



167
168
169
170
171
# File 'lib/ceml/script.rb', line 167

def params
  asks(:organizer).map do |ask|
    [ask.var, ask.var.capitalize, ask.text]
  end
end

#roles_to_castObject

casting =



9
10
11
12
# File 'lib/ceml/script.rb', line 9

def roles_to_cast
  return [] unless cast.type == :await
  return cast.roles_to_cast(self)
end

#scriptObject



150
151
152
# File 'lib/ceml/script.rb', line 150

def script
  text_value
end

#simple?Boolean

casting_statement.roles.names

Returns:

  • (Boolean)


81
82
83
# File 'lib/ceml/script.rb', line 81

def simple?
  (roles - [:agents, :organizer]).empty?
end

#tell(roles) ⇒ Object



127
128
129
130
# File 'lib/ceml/script.rb', line 127

def tell(roles)
  return unless instructions
  instructions.i_tell([*roles])
end

#titleObject



154
155
156
157
158
159
160
161
# File 'lib/ceml/script.rb', line 154

def title
  if defined?(super)
    super.title_value
  elsif respond_to? :title_value
    self.title_value
  else nil
  end
end

#to_hash(*fields) ⇒ Object

etc =



146
147
148
# File 'lib/ceml/script.rb', line 146

def to_hash *fields
  fields.inject({}){ |h, s| x = send(s); h[s] = x if x; h }
end

#typeObject



173
174
175
176
177
178
179
# File 'lib/ceml/script.rb', line 173

def type
  return 'mission'  if title
  return 'unknown'  if instructions.empty?
  return 'question' if not instructions.asks(:agents).empty?
  return 'message'  if instructions.tell(:agents)
  return 'unknown'
end

#validate!Object



132
133
134
135
# File 'lib/ceml/script.rb', line 132

def validate!
  return unless instructions
  instructions.validate_instructions!(allowed_roles)
end