Class: Player

Inherits:
Thing show all
Defined in:
lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb

Instance Method Summary collapse

Methods inherited from Thing

#add, #command, #command_name, #iname, #place, #place=, #presence, #presence=, #send_message_to_place

Methods inherited from REXML::Element

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

Constructor Details

#initialize(world, iname, jid) ⇒ Player

Returns a new instance of Player.



372
373
374
375
376
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 372

def initialize(world, iname, jid)
  super(world)
  attributes['name'] = iname
  attributes['jid'] = jid.to_s
end

Instance Method Details

#jidObject



378
379
380
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 378

def jid
  attributes['jid'].nil? ? nil : Jabber::JID.new(attributes['jid'])
end

#on_enter(thing, from) ⇒ Object



405
406
407
408
409
410
411
412
413
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 405

def on_enter(thing, from)
  if thing != self
    if from.nil?
      send_message(nil, "#{thing.iname} spawns")
    else
      send_message(nil, "#{thing.iname} enters #{place} coming from #{from}")
    end
  end
end

#on_leave(thing, to) ⇒ Object



415
416
417
418
419
420
421
422
423
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 415

def on_leave(thing, to)
  if thing != self
    if to.nil?
      send_message(nil, "#{thing.iname} disintegrates")
    else
      send_message(nil, "#{thing.iname} leaves #{place} going to #{to}")
    end
  end
end

#see(place) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 382

def see(place)
  return if place.nil?

  place.text.strip.split(/\n/).each do |line|
    send_message(nil, line.strip)
  end

  send_message(nil, " ")

  directions = []
  place.each_element('go') { |go|
    directions.push(go.attributes['spec'])
  }
  send_message(nil, "You can go #{directions.join(', ')}")
end

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



398
399
400
401
402
403
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 398

def send_message(fromresource, text, subject=nil)
  msg = Jabber::Message.new(jid, text)
  msg.type = :groupchat
  msg.subject = subject unless subject.nil?
  @world.send(fromresource, msg)
end