Class: Thing

Inherits:
REXML::Element show all
Defined in:
lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/world.rb

Direct Known Subclasses

Player

Instance Method Summary collapse

Methods inherited from REXML::Element

#==, #delete_elements, #first_element, #first_element_text, #import, import, #replace_element_text, #typed_add

Constructor Details

#initialize(world) ⇒ Thing

Returns a new instance of Thing.



275
276
277
278
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 275

def initialize(world)
  super('thing')
  @world = world
end

Instance Method Details

#add(xmlelement) ⇒ Object



280
281
282
283
284
285
286
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 280

def add(xmlelement)
  if xmlelement.kind_of?(REXML::Element) && (xmlelement.name == 'presence')
    super(Jabber::Presence.import(xmlelement))
  else
    super(xmlelement)
  end
end

#command(source, command, arguments) ⇒ Object



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 352

def command(source, command, arguments)
  command.each_element { |action|
    text = action.text.nil? ? "" : action.text.dup
    text.gsub!('%self%', iname)
    text.gsub!('%actor%', source.iname)
    text.gsub!('%place%', place)
    if action.name == 'say' || action.name == 'tell'
      sender = nil
      sender = iname if action.name == 'say'
      if action.attributes['to'] == 'all'
        send_message_to_place(sender, text)
      else
        source.send_message(sender, text)
      end
    end
  }
end

#command_nameObject



292
293
294
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 292

def command_name
  attributes['command-name'].nil? ? iname : attributes['command-name']
end

#inameObject



288
289
290
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 288

def iname
  attributes['name']
end

#jidObject



304
305
306
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 304

def jid
  nil
end

#on_enter(thing, from) ⇒ Object



340
341
342
343
344
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 340

def on_enter(thing, from)
  each_element('on-enter') { |c|
    command(thing, c, [from])
  }
end

#on_leave(thing, to) ⇒ Object



346
347
348
349
350
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 346

def on_leave(thing, to)
  each_element('on-leave') { |c|
    command(thing, c, [to])
  }
end

#placeObject



296
297
298
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 296

def place
  attributes['place']
end

#place=(p) ⇒ Object



300
301
302
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 300

def place=(p)
  attributes['place'] = p
end

#presenceObject



308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 308

def presence
  xe = nil
  each_element('presence') { |pres|
    xe = Jabber::Presence.import(pres)
  }
  if self.kind_of?(Player)
    xe.add(Jabber::MUC::XMUCUser.new).add(Jabber::MUC::XMUCUserItem.new(:none, :participant))
  else
    xe.add(Jabber::MUC::XMUCUser.new).add(Jabber::MUC::XMUCUserItem.new(:owner, :moderator))
  end
  xe
end

#presence=(pres) ⇒ Object



321
322
323
324
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 321

def presence=(pres)
  delete_elements('presence')
  add(pres)
end

#see(place) ⇒ Object



326
327
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 326

def see(place)
end

#send_message(fromresource, text, subject = nil) ⇒ Object



329
330
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 329

def send_message(fromresource, text, subject=nil)
end

#send_message_to_place(fromresource, text) ⇒ Object



332
333
334
335
336
337
338
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 332

def send_message_to_place(fromresource, text)
  @world.each_element('thing') { |thing|
    if thing.place == place
      thing.send_message(fromresource, text)
    end
  }
end