Changeset 102
Legend:
- Unmodified
- Added
- Removed
-
adventure/adventuremuc.py
r101 r102 2 2 # -*- coding: utf-8 -*- 3 3 4 from xmpp import Component 4 from xmpp import Component, JID 5 5 from sys import argv 6 6 from world import World … … 8 8 class AdventureMUC(Component): 9 9 def __init__(self, jid, password, server, port=5347): 10 Component.__init__(server, port) 10 11 self.worlds = {} 11 12 12 Component.__init__(server, port)13 13 if not self.connect(server, port): 14 raise Exception, 'Unable to connect to %s:%s' % (server, port) )14 raise Exception, 'Unable to connect to %s:%s' % (server, port) 15 15 if not self.auth(jid, password): 16 16 raise Exception, 'Unable to autenticate as %s' % jid … … 29 29 30 30 def send(self, worldnode, worldresource, stanza): 31 stanza. from = Jabber::JID::new(worldnode, self.component.jid.domain, worldresource)32 self. component.send(stanza)31 stanza.setFrom(JID(node=worldnode, domain=self.jid.domain, resource=worldresource)) 32 self.send(stanza) 33 33 34 34 def handle_iq(self, iq): … … 101 101 print "message: from #{msg.from} type #{msg.type} to #{msg.to}: #{msg.body.inspect}" 102 102 103 world = self.worlds[msg. to.node]104 if world.nil?:105 answer = msg. answer106 answer.type = :error107 answer.add(Jabber::Error.new('item-not-found', 'The world you are trying to reach is currently unavailable.'))108 self. component.send(answer)103 world = self.worlds[msg.getTo()] 104 if not world: 105 answer = msg.buildReply() 106 answer.type = 'error' 107 #answer.add(Jabber::Error.new('item-not-found', 'The world you are trying to reach is currently unavailable.')) 108 self.send(answer) 109 109 else: 110 msg.body=REXML::Text::unnormalize(msg.body)110 #msg.body=REXML::Text::unnormalize(msg.body) 111 111 world.handle_message(msg) 112 112 return True -
adventure/place.py
r101 r102 2 2 3 3 class Place: 4 def _init__(self, name ):4 def _init__(self, name, description): 5 5 self.name = name 6 self.description = description 6 7 self.visitors = list() 7 8 self.participants = list() -
adventure/player.py
r101 r102 1 # -*- coding: utf-8 -*- 2 class Player:#(Thing): 3 def __init__(self, world, name, jid): 4 self.world = world 5 self.name = name 6 self.jid = unicode(jid) 7 self.place = None 8 9 def see(place): 10 if not place: 11 return 12 13 for line in place.description.split('\n'): 14 send_message(None, line.strip()) 15 16 send_message(None, ' ') 17 send_message(None, 'You can go %s}' % ', '.join(place.exits.keys())) 18 19 def send_message(fromresource, text, subject=None): 20 msg = Jabber::Message.new(jid, text) 21 msg.type = :groupchat 22 msg.subject = subject unless subject.nil? 23 self.world.send(fromresource, msg) 24 25 def on_enter(self, thing, from_): 26 if thing != self: 27 if from_: 28 send_message(None, '%s enters %s coming from %s' % (thing.name, place, from_)) 29 else: 30 send_message(None, '%s spawns' % thing.name) 31 32 def on_leave(self, thing, to): 33 if thing != self: 34 if to: 35 send_message(None, '%s leaves %s going to %s' % (thing.name, place, to)) 36 else: 37 send_message(None, '%s disintegrates' % thing.name) 38 -
adventure/world.py
r101 r102 2 2 3 3 from place import Place 4 from player import Player 4 5 from xml.dom.minidom import parse as xml_parse 5 6 … … 12 13 13 14 doc = xml_parse(filename).documentElement 15 doc_getAttribute = doc.documentElement.getAttribute 16 self.node = doc.documentElement.getAttribute('node') 17 self.name = doc.documentElement.getAttribute('name') 18 self.start = doc.documentElement.getAttribute('start') 14 19 get_elements = doc.documentElement.getElementsByTagName 15 16 20 places = get_elements('places') 17 21 for place in places: 18 name = place.getAttribute('name')19 place_obj = Place(name)22 place_obj = Place(place.getAttribute('name'), 23 place.getAttribute('description')) 20 24 gos = place.getElementsByTagName('go') 21 25 for go in gos: … … 27 31 28 32 29 def send( resource, stanza):33 def send(self, resource, stanza): 30 34 # Avoid sending to things without JID 31 if stanza. to!= None:32 self.muc.send( node, resource, stanza)33 34 def place( placename):35 if stanza.getTo() != None: 36 self.muc.send(self.node, resource, stanza) 37 38 def place(self, placename): 35 39 if self.places.has_key(placename): 36 40 return self.places[placename] … … 50 54 if t.presence: 51 55 pres = Jabber::Presence.import(t.presence) 52 pres. type = :unavailable53 pres. to = thing.jid56 pres.setType('unavailable') 57 pres.SetTo(thing.jid) 54 58 send(t.iname, pres) unless t.jid == thing.jid 55 59 … … 57 61 if thing.presence: 58 62 pres = Jabber::Presence.import(thing.presence) 59 pres.type = :unavailable63 pres.type = 'unavailable' 60 64 pres.to = t.jid 61 65 send(thing.iname, pres) unless thing.jid == t.jid … … 78 82 send(thing.iname, pres) 79 83 80 thing.send_message(nil, " ") 81 subject = newplace.nil? ? " " : newplace.dup 82 subject[0] = subject[0,1].upcase 83 thing.send_message(nil, "Entering #{newplace}", subject) 84 thing.send_message(nil, " ") 84 thing.send_message(None, ' ') 85 if newplace: 86 subject = newplace.dup.capitalize() 87 else: 88 subject = ' ' 89 thing.send_message(None, 'Entering %s' % newplace, subject) 90 thing.send_message(None, ' ') 85 91 thing.see(place(newplace)) 86 92 … … 126 132 # Add the valid player 127 133 if not player: 128 player = add(Player .new(self, pres.to.resource, pres.from))134 player = add(Player(self, pres.getTo().getResource(), pres.getFrom())) 129 135 player.presence = pres 130 136 move_thing(player, attributes['start']) … … 285 291 source.send_message(sender, text) 286 292 287 class Player(Thing):288 def initialize(world, iname, jid):289 super(world)290 attributes['name'] = iname291 attributes['jid'] = jid.to_s292 293 def jid():294 if attributes['jid']:295 return Jabber::JID::new(attributes['jid'])296 else:297 return None298 299 def see(place):300 if not place:301 return302 303 for line in place.text.strip.split(/\n/).each:304 send_message(None, line.strip)305 306 send_message(None, " ")307 308 directions = []309 for go in place.each_element('go'):310 directions.push(go.attributes['spec'])311 send_message(None, "You can go #{directions.join(', ')}")312 313 def send_message(fromresource, text, subject=None):314 msg = Jabber::Message.new(jid, text)315 msg.type = :groupchat316 msg.subject = subject unless subject.nil?317 self.world.send(fromresource, msg)318 319 def on_enter(thing, from):320 if thing != self:321 if from:322 send_message(nil, "#{thing.iname} enters #{place} coming from #{from}")323 else:324 send_message(nil, "#{thing.iname} spawns")325 326 def on_leave(thing, to):327 if thing != self:328 if to:329 send_message(nil, "#{thing.iname} leaves #{place} going to #{to}")330 else:331 send_message(nil, "#{thing.iname} disintegrates")332
