Class: CEML::Incident

Inherits:
Object
  • Object
show all
Defined in:
lib/ceml/models/incident.rb

Constant Summary collapse

INTERPOLATE_REGEX =
/\|(\w+)\.?(\w+)?\|/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seq, id) ⇒ Incident

Returns a new instance of Incident.



6
# File 'lib/ceml/models/incident.rb', line 6

def initialize(seq, id); @id = id; @seq = seq; end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/ceml/models/incident.rb', line 5

def id
  @id
end

#seqObject (readonly)

Returns the value of attribute seq.



5
6
7
# File 'lib/ceml/models/incident.rb', line 5

def seq
  @seq
end

#thisObject (readonly)

Returns the value of attribute this.



5
6
7
# File 'lib/ceml/models/incident.rb', line 5

def this
  @this
end

Instance Method Details

#answered_q(q) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/ceml/models/incident.rb', line 169

def answered_q q
  got or return false
  this[:last_answer] = qs_answers[q[:key]] = got
  this[:last_answer_recognized] = recognized
  handled!
  true
end

#ask_q(q) ⇒ Object



162
163
164
165
166
167
# File 'lib/ceml/models/incident.rb', line 162

def ask_q q
  text = interpolate(q[:text]) or return false
  handled!
  say :ask, :q => text
  true
end

#assign(a) ⇒ Object



194
195
196
197
198
# File 'lib/ceml/models/incident.rb', line 194

def assign a
  text = interpolate(a[:text]) or return false
  say :assignment, :msg => text
  true
end

#cb(*stuff) ⇒ Object



19
20
21
# File 'lib/ceml/models/incident.rb', line 19

def cb *stuff
  @callback.call this, *stuff if @callback
end

#complete_assign(a = nil) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/ceml/models/incident.rb', line 200

def complete_assign a = nil
  got or return false
  if recognized == :done
    cb :did_complete
    handled!
    say :ok if pc == seq.size - 2
    true
  else
    cb :did_report
    handled!
    false
  end
end

#complete_delayObject



151
152
153
154
155
# File 'lib/ceml/models/incident.rb', line 151

def complete_delay
  return false unless CEML.clock >= this[:continue_at]
  this.delete(:continue_at)
  true
end

#expand(role, var) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ceml/models/incident.rb', line 101

def expand(role, var)
  case role
  when 'his', 'her', 'their', 'my', 'its', 'your';    return qs_answers[var]
  when 'world', 'game', 'exercise', 'group';  return (cb :world, var)
  when 'somebody', 'someone', 'buddy', 'teammate';  role = nil
  end
  role = role.to_sym if role
  players_with_role(role).each do |p|
    value = (p[:qs_answers]||{})[var] and return value
  end
  nil
end

#expectation(type, value) ⇒ Object



221
222
223
# File 'lib/ceml/models/incident.rb', line 221

def expectation(type, value)
  if type == :if then last_answer_match?(value) else !last_answer_match?(value) end
end

#finishObject



144
# File 'lib/ceml/models/incident.rb', line 144

def finish; true; end

#gotObject



9
# File 'lib/ceml/models/incident.rb', line 9

def got;        this[:received];   end

#handled!Object



14
15
16
17
# File 'lib/ceml/models/incident.rb', line 14

def handled!
  this.delete :received
  this.delete :recognized
end

#interpolate(text) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/ceml/models/incident.rb', line 116

def interpolate(text)
  text =~ INTERPOLATE_REGEX or return text
  text.gsub(INTERPOLATE_REGEX) do |m|
    var, role = *[$2, $1].compact
    expand(role, var) or return false
  end
end

#is_transient?Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
# File 'lib/ceml/models/incident.rb', line 85

def is_transient?
  seq.none? do |roles, opcode, arg|
    case opcode
    when :start_delay, :ask_q, :assign, :null_assign then true
    end
  end
end

#last_answer_match?(value) ⇒ Boolean

Returns:

  • (Boolean)


214
215
216
217
218
219
# File 'lib/ceml/models/incident.rb', line 214

def last_answer_match?(value)
  case value
  when :yes; this[:last_answer_recognized] == :yes
  when :no;  this[:last_answer_recognized] == :no
  end
end

#log(state) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/ceml/models/incident.rb', line 28

def log state
  p = @this
  instr = seq[pc]
  guyroles = roles.to_a - [:everyone, :players, :them, :all, :either, :each, :agents, :both]
  instr ||= []

  # CEML.log 3, "#{p[:id]}: #{state} -- #{instr[1]}/#{instr[0]} -- #{instr[2].inspect} -- #{id}##{pc}(#{guyroles})"
end

#null_assignObject



238
239
240
241
# File 'lib/ceml/models/incident.rb', line 238

def null_assign
  say :proceed
  true
end

#pcObject



11
# File 'lib/ceml/models/incident.rb', line 11

def pc;         this[:pc] ||= 0;   end

#pick(q) ⇒ Object



182
183
184
185
186
# File 'lib/ceml/models/incident.rb', line 182

def pick q
  choices = q[:value].split(/\s+\-\s+/)
  qs_answers[q[:key]] ||= choices.sort_by{ rand }.first
  true
end

#players_with_role(role) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/ceml/models/incident.rb', line 93

def players_with_role(role)
  if role
    @players.select{ |p| p[:roles].include? role }
  else
    @players.reject{ |p| p == this }
  end
end

#qs_answersObject



12
# File 'lib/ceml/models/incident.rb', line 12

def qs_answers; this[:qs_answers] ||= Hash.new; end

#recognizedObject



10
# File 'lib/ceml/models/incident.rb', line 10

def recognized; this[:recognized]; end

#release(x) ⇒ Object



225
226
227
228
229
230
# File 'lib/ceml/models/incident.rb', line 225

def release x
  return true if x[:cond] and not expectation(*x[:cond])
  @players.delete(this)
  cb :released, x[:tag]
  true
end

#replace(x) ⇒ Object



232
233
234
235
236
# File 'lib/ceml/models/incident.rb', line 232

def replace x
  return true if x[:cond] and not expectation(*x[:cond])
  # TODO: implement replace
  false
end

#role_infoObject



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

def role_info
  { :is_transient => is_transient? }
end

#rolematch(specified_roles) ⇒ Object



23
24
25
26
# File 'lib/ceml/models/incident.rb', line 23

def rolematch(specified_roles)
  expanded = roles.map{ |r| r == :agent ? [:agent, :agents] : r }.flatten.concat([:both, :all, :everyone, :them, :either])
  not (expanded & [*specified_roles]).empty?
end

#rolesObject



8
# File 'lib/ceml/models/incident.rb', line 8

def roles;      this[:roles] ||= Set.new; end

#run(players, &blk) ⇒ Object



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
# File 'lib/ceml/models/incident.rb', line 37

def run(players, &blk)
  @players = players
  @callback = blk
  was_blocked = {}
  # CEML.log 1, "running players: #{players.inspect}"

  loop do
    players = @players
    advanced = false
    players.each do |p|
      @this = p
      instr = seq[pc]
      # log "running: #{pc}: #{instr.inspect}"
      unless instr = seq[pc]
        @players.delete(p)
        next
      end
      instr = instr.dup
      rolespec = instr.shift
      if not rolematch(rolespec)
        # log "skipping[#{rolespec}]"
        this[:pc]+=1
        advanced = true
      else
        instr << role_info if instr.first == :start  #tmp hack
        if send(*instr)
          log 'completed'
          was_blocked[p] = false
        else
          log 'blocked' unless was_blocked[p]
          was_blocked[p] = true
          next
        end
        cb(*instr)
        this[:pc]+=1
        advanced = true
      end
    end
    break unless advanced
  end

  @callback = @players = nil
end

#say(x, params = {}) ⇒ Object

basic flow =



129
130
131
# File 'lib/ceml/models/incident.rb', line 129

def say x, params = {}
  cb :said, params.merge(:said => x)
end

#seed(x) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/ceml/models/incident.rb', line 133

def seed x
  x[:rolemap].each do |pair|
    if rolematch(pair[:from].to_sym)
      cb :seeded, :target => x[:target], :role => pair[:to]
      break
    end
  end
  true
end

#send_msg(a) ⇒ Object



188
189
190
191
192
# File 'lib/ceml/models/incident.rb', line 188

def send_msg a
  text = interpolate(a[:text]) or return false
  say :message, :msg => text
  true
end

#set(q) ⇒ Object



177
178
179
180
# File 'lib/ceml/models/incident.rb', line 177

def set q
  qs_answers[q[:key]] = q[:value]
  true
end

#start(x) ⇒ Object



143
# File 'lib/ceml/models/incident.rb', line 143

def start(x); true; end

#start_delay(seconds) ⇒ Object



146
147
148
149
# File 'lib/ceml/models/incident.rb', line 146

def start_delay seconds
  this[:continue_at] = CEML.clock + seconds
  true
end

#sync(q) ⇒ Object



157
158
159
160
# File 'lib/ceml/models/incident.rb', line 157

def sync q
  this[:synced] = pc
  return true if players_with_role(q[:role]).all?{ |p| p[:synced] == pc }
end