Changeset 102

Show
Ignore:
Timestamp:
10/14/07 10:08:04 (2 years ago)
Author:
elghinn
Message:

some stuff

Location:
adventure
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • adventure/adventuremuc.py

    r101 r102  
    22# -*- coding: utf-8 -*- 
    33 
    4 from xmpp import Component 
     4from xmpp import Component, JID 
    55from sys import argv 
    66from world import World 
     
    88class AdventureMUC(Component): 
    99    def __init__(self, jid, password, server, port=5347): 
     10        Component.__init__(server, port) 
    1011        self.worlds = {} 
    1112     
    12         Component.__init__(server, port) 
    1313        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) 
    1515        if not self.auth(jid, password): 
    1616            raise Exception, 'Unable to autenticate as %s' % jid 
     
    2929 
    3030    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) 
    3333 
    3434    def handle_iq(self, iq): 
     
    101101        print "message: from #{msg.from} type #{msg.type} to #{msg.to}: #{msg.body.inspect}" 
    102102 
    103         world = self.worlds[msg.to.node] 
    104         if world.nil? : 
    105             answer = msg.answer 
    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.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) 
    109109        else: 
    110             msg.body=REXML::Text::unnormalize(msg.body) 
     110            #msg.body=REXML::Text::unnormalize(msg.body) 
    111111            world.handle_message(msg) 
    112112        return True 
  • adventure/place.py

    r101 r102  
    22 
    33class Place: 
    4     def _init__(self, name): 
     4    def _init__(self, name, description): 
    55        self.name = name 
     6        self.description = description 
    67        self.visitors = list() 
    78        self.participants = list() 
  • adventure/player.py

    r101 r102  
     1# -*- coding: utf-8 -*- 
     2class 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  
    22 
    33from place import Place 
     4from player import Player 
    45from xml.dom.minidom import parse as xml_parse 
    56 
     
    1213 
    1314        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') 
    1419        get_elements = doc.documentElement.getElementsByTagName 
    15  
    1620        places = get_elements('places') 
    1721        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')) 
    2024            gos = place.getElementsByTagName('go') 
    2125            for go in gos: 
     
    2731 
    2832 
    29     def send(resource, stanza): 
     33    def send(self, resource, stanza): 
    3034        # 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): 
    3539        if self.places.has_key(placename): 
    3640            return self.places[placename] 
     
    5054            if t.presence: 
    5155                pres = Jabber::Presence.import(t.presence) 
    52                 pres.type = :unavailable 
    53                 pres.to = thing.jid 
     56                pres.setType('unavailable') 
     57                pres.SetTo(thing.jid) 
    5458                send(t.iname, pres) unless t.jid == thing.jid 
    5559 
     
    5761            if thing.presence: 
    5862                pres = Jabber::Presence.import(thing.presence) 
    59                 pres.type = :unavailable 
     63                pres.type = 'unavailable' 
    6064                pres.to = t.jid 
    6165                send(thing.iname, pres) unless thing.jid == t.jid 
     
    7882                send(thing.iname, pres) 
    7983 
    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, ' ') 
    8591        thing.see(place(newplace)) 
    8692 
     
    126132        # Add the valid player 
    127133        if not player: 
    128             player = add(Player.new(self, pres.to.resource, pres.from)) 
     134            player = add(Player(self, pres.getTo().getResource(), pres.getFrom())) 
    129135            player.presence = pres 
    130136            move_thing(player, attributes['start']) 
     
    285291                    source.send_message(sender, text) 
    286292 
    287 class Player(Thing): 
    288     def initialize(world, iname, jid): 
    289         super(world) 
    290         attributes['name'] = iname 
    291         attributes['jid'] = jid.to_s 
    292    
    293     def jid(): 
    294         if attributes['jid']: 
    295             return Jabber::JID::new(attributes['jid']) 
    296         else: 
    297             return None 
    298  
    299     def see(place): 
    300         if not place: 
    301             return 
    302  
    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 = :groupchat 
    316         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