Class: World

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

Class Method Summary collapse

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(muc) ⇒ World

Returns a new instance of World.



4
5
6
7
8
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 4

def initialize(muc)
  super('world')

  @muc = muc
end

Class Method Details

.new_from_file(muc, filename) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 17

def World.new_from_file(muc, filename)
  file = File.new(filename)
  world = World.new(muc)
  world.import(REXML::Document.new(file).root)
  file.close
  world
end

Instance Method Details

#add(xmlelement) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 25

def add(xmlelement)
  if xmlelement.kind_of?(REXML::Element) && (xmlelement.name == 'place')
    super(Place.new.import(xmlelement))
  elsif xmlelement.kind_of?(REXML::Element) && (xmlelement.name == 'thing') && !xmlelement.kind_of?(Player)
    super(Thing.new(self).import(xmlelement))
  else
    super(xmlelement)
  end
end

#command(player, text) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 205

def command(player, text)
  if text == '?'
    player.send_message(nil, "(Command) who")
    place(player.place).each_element('go') { |go|
      player.send_message(nil, "(Command) go #{go.attributes['spec']}")
    }
    each_thing_by_place(player.place) { |thing|
      thing.each_element('on-command') { |c|
        player.send_message(nil, "(Command) #{c.attributes['command']} #{thing.command_name}")
      }
    }
    return(true)
  else
    words = text.split(/ /)
    cmd = words.shift
    what = words.shift || ""
    if cmd == 'go'
      oldplace = place(player.place)
      newplace = nil

      oldplace.each_element('go') { |go|
        if go.attributes['spec'] == what
          newplace = go.attributes['place']
        end
      }

      if newplace.nil?
        player.send_message(nil, 'You cannot go there')
      else
        move_thing(player, newplace)
      end
      return(true)
    elsif cmd == 'who'
      player.send_message(nil, "Players in \"#{iname}\":")
      each_element('thing') { |thing|
        if thing.kind_of?(Player)
          player.send_message(nil, "#{thing.iname} is at/in #{thing.place}")
        end
      }
      return(true)
    else
      handled = false
      each_thing_by_place(player.place) { |thing|
        if what.downcase == thing.command_name.downcase
          thing.each_element('on-command') { |c|
            if c.attributes['command'] == cmd
              thing.command(player, c, words)
              handled = true
            end
          }
        end
      }
      return(true) if handled
    end
  end
  false
end

#each_thing_by_place(place, &block) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 53

def each_thing_by_place(place, &block)
  each_element('thing') { |t|
    if t.place == place
      yield(t)
    end
  }
end

#handle_message(msg) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 182

def handle_message(msg)
  player = nil
  each_element('thing') { |thing|
    if thing.kind_of?(Player) && msg.to.resource == nil && msg.from == thing.jid
      player = thing
    end
  }

  if player.nil?
    answer = msg.answer
    answer.type = :error
    answer.add(Jabber::ErrorResponse.new('forbidden'))
    send(msg.to.resource, answer)
    return(true)
  end

  unless command(player, msg.body)
    each_thing_by_place(player.place) { |thing|
      thing.send_message(player.iname, msg.body)
    }
  end
end

#handle_presence(pres) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 116

def handle_presence(pres)
  # A help for the irritated first:
  if pres.type == :subscribe
    msg = Jabber::Message.new(pres.from)
    msg.type = :normal
    msg.subject = "Adventure component help"
    msg.body = "You don't need to subscribe to my presence. Simply use your Jabber client to join the MUC or conference at #{pres.to.strip}"
    send(nil, msg)
    return(true)
  end

  # Look if player is already known
  player = nil
  each_element('thing') { |thing|
    if thing.kind_of?(Player) && pres.to.resource == thing.iname
      player = thing
    end

    # Disallow nick changes
    if thing.kind_of?(Player) && (pres.from == thing.jid) && (player != thing)
      answer = pres.answer(false)
      answer.type = :error
      answer.add(Jabber::ErrorResponse.new('not-acceptable', 'Nickchange not allowed'))
      send(thing.iname, answer)
      return(true)
    end
  }

  # Either nick-collission or empty nick
  unless (player.nil? || pres.from == player.jid) && (pres.to.resource.to_s.size > 1)
    answer = pres.answer
    answer.type = :error
    if (pres.to.resource.to_s.size > 1)
      answer.add(Jabber::ErrorResponse.new('conflict', 'Nickname already used'))
    else
      answer.add(Jabber::ErrorResponse.new('not-acceptable', 'Please use a nickname'))
    end
    send(nil, answer)
    return(true)
  end

  # Add the valid player
  if player.nil?
    player = add(Player.new(self, pres.to.resource, pres.from))
    player.presence = pres
    move_thing(player, attributes['start'])
    player.send_message('Help!', 'Send "?" to get a list of available commands any time.')
  # Or broadcast updated presence
  else
    player.presence = pres

    each_thing_by_place(player.place) { |t|
      # Broadcast presence to all who are here
      pres = Jabber::Presence.import(player.presence)
      pres.to = t.jid
      send(player.iname, pres)
    }
  end

  # Remove the player instantly
  if pres.type == :error || pres.type == :unavailable
    move_thing(player, nil)
    delete_element(player)
  end
end

#inameObject



39
40
41
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 39

def iname
  attributes['name']
end

#move_thing(thing, newplace) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 61

def move_thing(thing, newplace)
  each_thing_by_place(thing.place) { |t|
    # Call leave hooks
    t.on_leave(thing, newplace)

    # Broadcast unavailability presence to leaver
    unless t.presence.nil?
      pres = Jabber::Presence.import(t.presence)
      pres.type = :unavailable
      pres.to = thing.jid
      send(t.iname, pres) unless t.jid == thing.jid
    end

    # Broadcast unavailability presence to all who are here
    unless thing.presence.nil?
      pres = Jabber::Presence.import(thing.presence)
      pres.type = :unavailable
      pres.to = t.jid
      send(thing.iname, pres) unless thing.jid == t.jid
    end
  }

  # Enter new place
  oldplace = thing.place
  thing.place = newplace

  each_thing_by_place(thing.place) { |t|
    # Broadcast availability presence to enterer
    unless t.presence.nil?
      pres = Jabber::Presence.import(t.presence)
      pres.to = thing.jid
      send(t.iname, pres)
    end

    # Broadcast availability presence to all who are here
    unless thing.presence.nil?
      pres = Jabber::Presence.import(thing.presence)
      pres.to = t.jid
      send(thing.iname, pres)
    end
  }

  thing.send_message(nil, " ")
  subject = newplace.nil? ? " " : newplace.dup
  subject[0] = subject[0,1].upcase
  thing.send_message(nil, "Entering #{newplace}", subject)
  thing.send_message(nil, " ")
  thing.see(place(newplace))

  each_thing_by_place(thing.place) { |t|
    # Call enter hooks
    t.on_enter(thing, oldplace)
  }
end

#nodeObject



35
36
37
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 35

def node
  attributes['node']
end

#place(placename) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 43

def place(placename)
  pl = nil
  each_element('place') { |place|
    if place.iname == placename
      pl = place
    end
  }
  pl
end

#send(resource, stanza) ⇒ Object



10
11
12
13
14
15
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/adventure/world.rb', line 10

def send(resource, stanza)
  # Avoid sending to things without JID
  if stanza.to != nil
    @muc.send(node, resource, stanza)
  end
end