Module: CEML::Script

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

Defined Under Namespace

Classes: DefaultDP

Instance Method Summary collapse

Instance Method Details

#allowed_rolesObject



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

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

#availabilities(potential_count, committed_count = 0) ⇒ Object



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

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



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ceml/lang/script.rb', line 119

def bytecode
  code = [[[:all], :start]]
  # puts "instructions: #{instructions.inspect}"
  if !instructions and title
    code.concat [[[:all], :null_assign], [[:all], :complete_assign]]
  elsif instructions
    code.concat(instructions.bytecode.flatten(1))
  end
  code << [[:all], :finish]
  code
end

#castObject Also known as: dramatis_personae



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

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

#castableObject

casting =



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

def castable
  return nil unless cast.live_casting?
  args = cast.casting_spec + [bytecode]
  Castable.new(*args)
end

#color(odds) ⇒ Object

likelihoods =



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

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

#concludes_immediately?Boolean

Returns:

  • (Boolean)


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

def concludes_immediately?
  !title and bytecode.none?{ |line| [:assign, :ask_q].include?(line[1]) }
end

#expand_roles(roles) ⇒ Object



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

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

#instructionsObject

instructions =



101
102
103
104
105
106
# File 'lib/ceml/lang/script.rb', line 101

def instructions
  elements.first.instructions if elements.first.respond_to? :instructions
  # return self if Instructions === self
  # return super if defined?(super)
  # nil
end

#labelObject



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/ceml/lang/script.rb', line 195

def label
  return title || begin
    return "unknown" unless simple?
    if simple_question
      "Question: #{simple_question}"
    elsif simple_message
      "Message: #{simple_message}"
    else
      "unknown"
    end
  end
end

#likelihood(yesprob, needed, psize) ⇒ Object



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

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)


191
192
193
# File 'lib/ceml/lang/script.rb', line 191

def message?
  type == 'message'
end

#nabs?Boolean

Returns:

  • (Boolean)


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

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

#nameObject



153
154
155
# File 'lib/ceml/lang/script.rb', line 153

def name
  title || label
end

#paramsObject



157
158
159
160
161
162
163
# File 'lib/ceml/lang/script.rb', line 157

def params
  bytecode.map do |line|
    next unless line[0].include?(:organizer)
    next unless line[1] == :ask_q
    [line[2][:q], line[2][:q].capitalize, line[2][:text]]
  end.compact
end

#scriptObject



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

def script
  text_value
end

#simple?Boolean

casting_statement.roles.names

Returns:

  • (Boolean)


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

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

#simple_messageObject



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

def simple_message
  # puts "bytecode size: #{bytecode.size}"
  # p bytecode
  return unless bytecode.size == 3
  # puts "bytecode core: #{bytecode[1][1]}"
  return unless bytecode[1][1] == :send_msg
  return bytecode[1][2][:text]
end

#simple_questionObject



182
183
184
185
186
187
188
189
# File 'lib/ceml/lang/script.rb', line 182

def simple_question
  # puts "bytecode size: #{bytecode.size}"
  # p bytecode
  return unless bytecode.size == 4
  # puts "bytecode core: #{bytecode[1][1]}"
  return unless bytecode[1][1] == :ask_q
  return bytecode[1][2][:text]
end

#titleObject



108
109
110
111
112
113
114
115
116
# File 'lib/ceml/lang/script.rb', line 108

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

#to_hash(*fields) ⇒ Object

etc =



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

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

#typeObject



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

def type
  return 'mission'  if title
  return 'unknown'  if instructions.empty?
  return 'question' if simple_question
  return 'message'  if simple_message
  return 'unknown'
end

#validate!Object



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

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