Changeset 103

Show
Ignore:
Timestamp:
10/14/07 13:02:39 (3 years ago)
Author:
elghinn
Message:

ThibG and me: some stuff

Location:
adventure
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • adventure/adventuremuc.py

    r102 r103  
    2424    def add_world(self, filename): 
    2525        print 'Adding world from "%s"...' % filename 
    26         world = World.new_from_file(self, filename) 
     26        world = World(self, filename) 
    2727        self.worlds[world.node] = world 
    28         print ' ' + world.iname 
     28        print ' ' + world.name 
    2929 
    3030    def send(self, worldnode, worldresource, stanza): 
     
    8686 
    8787    def handle_presence(self, pres): 
    88         #print "presence: from #{pres.from} type #{pres.type} to #{pres.to}" 
     88        print 'presence: from %s type %s to %s' % (pres.getFrom(), pres.getType(), pres.getTo()) 
    8989     
    9090        world = self.worlds[pres.to.node] 
    9191        if not world: 
    9292            answer = pres.answer 
    93             answer.type = :error 
    94             answer.add(Jabber::Error.new('item-not-found', 'The world you are trying to reach is currently unavailable.')) 
     93            answer.setType('error') 
     94            #answer.add(Jabber::Error.new('item-not-found', 'The world you are trying to reach is currently unavailable.')) 
    9595            self.component.send(answer) 
    9696        else: 
     
    9999 
    100100    def handle_message(msg): 
    101         print "message: from #{msg.from} type #{msg.type} to #{msg.to}: #{msg.body.inspect}" 
     101        print 'message: from %s type %s to %s: %s' % (msg.getFrom(), msg.getType(), msg.getTo(), repr(msg.getBody())) 
    102102 
    103103        world = self.worlds[msg.getTo()] 
    104104        if not world: 
    105105            answer = msg.buildReply() 
    106             answer.type = 'error' 
     106            answer.setType('error') 
    107107            #answer.add(Jabber::Error.new('item-not-found', 'The world you are trying to reach is currently unavailable.')) 
    108108            self.send(answer) 
     
    116116if __name__ == '__main__': 
    117117    if len argv != 4: 
    118         print 'Syntax: ./adventure.rb <JID> <Password> <Host> <Port>' 
     118        print 'Syntax: ./adventuremuc.py <JID> <Password> <Host> <Port>' 
    119119        print 'See README for further help' 
    120120    muc = AdventureMUC(*argv) 
  • adventure/cave.xml

    r101 r103  
    3939        <commands> 
    4040            <command name="look at"><narration>An ugly rat</narration></command> 
    41             <command name="use %s on" object="bottle"> 
     41            <command name="use %s on" objects="bottle"> 
    4242                <use target="bottle" /> 
    4343                <narration>%actor% use alcool bottle on %self%</narration> 
  • adventure/player.py

    r102 r103  
    11# -*- coding: utf-8 -*- 
    2 class Player:#(Thing): 
     2class Player(Thing): 
    33    def __init__(self, world, name, jid): 
    44        self.world = world 
     
    77        self.place = None 
    88   
    9     def see(place): 
     9    def see(self, place): 
    1010        if not place: 
    1111            return 
     
    1717        send_message(None, 'You can go %s}' % ', '.join(place.exits.keys())) 
    1818 
    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? 
     19    def send_message(self, fromresource, text, subject=None): 
     20        msg = Message(jid, text) 
     21        msg.setType('groupchat') 
     22        if subject: 
     23            msg.setSubject(subject) 
    2324        self.world.send(fromresource, msg) 
    2425 
  • adventure/thing.py

    r101 r103  
    11# -*- coding: utf-8 -*- 
    22 
     3from player import Player 
     4 
    35class Thing: 
    4     def _init__(self, name, parent): 
     6    def _init__(self, world, name, parent): 
     7        self.word = word 
    58        self.name = name 
    69        self.parent = parent 
    710        self.aliases = list() # Noms alternatifs 
    811        self.actions = list() 
    9         self.presence = ... 
     12        self.presence = None 
    1013        self.UID = 0 
    1114        self.place = None 
    1215         
     16    def command_name(self): 
     17        if attributes['command-name']: 
     18            return attributes['command-name'] 
     19        return self.iname() 
     20 
     21    def jid(): 
     22        return None 
     23 
     24    def presence(): 
     25        xe = None 
     26        #for pres in each_element('presence'): 
     27        #    xe = Jabber::Presence.import(pres) 
     28        #if isinstance(self, Player): 
     29        #    xe.add(Jabber::MUC::XMUCUser.new).add(Jabber::MUC::XMUCUserItem.new('none', 'participant')) 
     30        #else: 
     31        #    xe.add(Jabber::MUC::XMUCUser.new).add(Jabber::MUC::XMUCUserItem.new('owner', 'moderator')) 
     32        return xe 
     33 
     34    def see(place): 
     35        pass 
     36 
     37    def send_message(fromresource, text, subject=None): 
     38        pass 
     39 
     40    def send_message_to_place(fromresource, text): 
     41        for thing in self.world.each_element('thing'): 
     42            if thing.place == place: 
     43                thing.send_message(fromresource, text) 
     44   
     45    def on_enter(thing, from): 
     46        for c in each_element('on-enter'): 
     47            command(thing, c, [from]) 
     48 
     49    def on_leave(thing, to): 
     50        for c in each_element('on-leave'): 
     51            command(thing, c, [to]) 
     52 
     53    def command(source, command, arguments): 
     54            if command.action[1]: 
     55                text = command.action[1] 
     56            else: 
     57                text = '' 
     58            if command.action[2]: 
     59                target = .... # élément de nom command.action[2] avec le même place que self 
     60            else: 
     61                target = self 
     62            text.replace('%self%', target.name) 
     63            text.replace('%actor%', source.name) 
     64            text.replace('%place%', place) 
     65            if command.action[0] == 'say' or command.action[0] == 'narration': 
     66                sender = None 
     67                if command.action[0] == 'say': 
     68                    sender = self.name 
     69                if command.action[3] == 'all': 
     70                    send_message_to_place(sender, text) 
     71                else: 
     72                    source.send_message(sender, text) 
     73 
     74 
     75 
     76 
    1377class Action: 
    1478    def _init__(self, name, parent): 
     
    1983                                  # un élément par alias ( ex "use bottle on", "use whiskey on" ) 
    2084        #self.xmlelement = None # Élément XML à traiter si les objets sont dans l'inventaire 
    21         self.action = list() 
     85        self.action = list() # (("say","coucou",None,None),("destroy",None)) par exemple 
     86                             # à respecter : premier élément : nom de la commande, deuxième : texte, troisième : target 
  • adventure/world.dtd

    r101 r103  
    2525 respawn CDATA #IMPLIED> 
    2626  
    27 <!ELEMENT on-enter (say?,narration?,destroy?,use?,give)?> 
    28 <!ELEMENT on-leave (say?,narration?,destroy?,use?,give)?> 
     27<!ELEMENT on-enter (say?,narration?,destroy?,use?,give?,move?)> 
     28<!ELEMENT on-leave (say?,narration?,destroy?,use?,give?,move?)> 
    2929 
    3030<!ELEMENT commands (command+,inherit+)> 
     
    3737 enabled CDATA #IMPLIED> 
    3838 
    39 <!ELEMENT command (say?,narration?,presence?,commands?,destroy?,use?,give?)> 
     39<!ELEMENT command (say?,narration?,presence?,commands?,destroy?,use?,give?,move?)> 
    4040<!ATTLIST command 
    4141 name CDATA #REQUIRED 
    42  object CDATA #IMPLIED> 
     42 objects CDATA #IMPLIED> 
    4343  
    4444<!ELEMENT inherit (#EMPTY)> 
     
    5454<!ELEMENT say (#PCDATA)> 
    5555<!ATTLIST say 
    56  target CDATA #IMPLIED> 
     56 target CDATA #IMPLIED 
     57 to CDATA #IMPLIED> 
    5758  
    5859<!ELEMENT destroy (#EMPTY)> 
     
    7576 place CDATA #REQUIRED> 
    7677  
     78<!ELEMENT move (#EMPTY)> 
     79<!ATTLIST move 
     80 target CDATA #IMPLIED 
     81 place CDATA #REQUIRED> 
     82  
    7783<!ELEMENT narration (#PCDATA)>  
    7884 
  • adventure/world.py

    r102 r103  
    44from player import Player 
    55from xml.dom.minidom import parse as xml_parse 
    6  
     6from xmpp import Message, Presence 
    77 
    88class World: 
    9     def __init__(self, muc): 
     9    def __init__(self, muc, filename): 
    1010        self.muc = muc 
    1111        self.places = dict() # {'place_name': place, ...} 
    12         #self.things =  
     12        self.things = dict()  
    1313 
    1414        doc = xml_parse(filename).documentElement 
     
    2222            place_obj = Place(place.getAttribute('name'), 
    2323                              place.getAttribute('description')) 
     24            self.places[place_obj.name] = place_obj 
    2425            gos = place.getElementsByTagName('go') 
    2526            for go in gos: 
     
    4142        return None 
    4243 
    43     def each_thing_by_place(placename): 
     44    def each_thing_by_place(self, placename): 
    4445        for thing in self.things.values(): 
    4546            if thing.place == placename: 
    4647                yield(thing) 
    4748 
    48     def move_thing(thing, newplace): 
     49    def move_thing(self, thing, newplace): 
    4950        for t in each_thing_by_place(thing.place): 
    5051            # Call leave hooks 
     
    5657                pres.setType('unavailable') 
    5758                pres.SetTo(thing.jid) 
    58                 send(t.iname, pres) unless t.jid == thing.jid 
     59                if t.jid != thing.jid: 
     60                    self.send(t.name, pres) 
    5961 
    6062            # Broadcast unavailability presence to all who are here 
    6163            if thing.presence: 
    6264                pres = Jabber::Presence.import(thing.presence) 
    63                 pres.type = 'unavailable' 
    64                 pres.to = t.jid 
    65                 send(thing.iname, pres) unless thing.jid == t.jid 
     65                pres.setType('unavailable') 
     66                pres.setTo(t.jid) 
     67                if thing.jid != t.jid: 
     68                    self.send(thing.name, pres) 
    6669 
    6770        # Enter new place 
     
    7376            if t.presence: 
    7477                pres = Jabber::Presence.import(t.presence) 
    75                 pres.to = thing.jid 
    76                 send(t.iname, pres) 
     78                pres.setTo(thing.jid) 
     79                self.send(t.name, pres) 
    7780 
    7881            # Broadcast availability presence to all who are here 
    7982            if thing.presence: 
    8083                pres = Jabber::Presence.import(thing.presence) 
    81                 pres.to = t.jid 
    82                 send(thing.iname, pres) 
     84                pres.setTo(t.jid) 
     85                self.send(thing.name, pres) 
    8386 
    8487        thing.send_message(None, ' ') 
    8588        if newplace: 
    86             subject = newplace.dup.capitalize() 
     89            subject = newplace.capitalize() 
    8790        else: 
    8891            subject = ' ' 
     
    97100    def handle_presence(pres): 
    98101        # A help for the irritated first: 
    99         if pres.type == 'subscribe': 
    100             msg = Jabber::Message.new(pres.from) 
     102        if pres.getType() == 'subscribe': 
     103            msg = Message(pres.from) 
    101104            msg.type = 'normal' 
    102105            msg.subject = 'Adventure component help' 
     
    154157        player = None 
    155158        for thing in self.things.values(): 
    156             if thing.kind_of?(Player) && msg.to.resource == None && msg.from == thing.jid: 
     159            if isinstance(thing, Player) and msg.getTo().getResource() == None && msg.getFrom() == thing.jid: 
    157160                player = thing 
    158161 
    159162        if not player: 
    160163            answer = msg.answer 
    161             answer.type = :error 
    162             answer.add(Jabber::Error::new('forbidden')) 
    163             send(msg.to.resource, answer) 
    164             return True 
    165  
    166         if not command(player, msg.body): 
     164            answer.setType('error') 
     165            #answer.add(Jabber::Error::new('forbidden')) 
     166            send(msg.getTo().getResource(), answer) 
     167            return True 
     168 
     169        if not command(player, msg.getBody()): 
    167170            for thing in each_thing_by_place(player.place): 
    168171                thing.send_message(player.iname, msg.body) 
     
    172175            player.send_message(None, '(Command) who') 
    173176            for go in place(player.place).each_element('go'): 
    174                 player.send_message(None, '(Command) go #{go.attributes['spec']}') 
     177                player.send_message(None, '(Command) go %s' % #{go.attributes['spec']}) 
    175178            for thing in foreach_thing_by_place(player.place): 
    176                 for c in thing.each_element('on-command'): 
    177                     player.send_message(None, '(Command) #{c.attributes['command']} #{thing.command_name}') 
     179                for c in thing.actions: 
     180                    player.send_message(None, '(Command) %s %s' % (c.expressions[0], thing.name)) 
    178181            return True 
    179182        else: 
    180             words = text.split(/ /) 
    181             cmd = words.shift 
    182             what = words.shift or '' 
     183            words = text.split(' ') 
     184            cmd = words.pop(0) 
     185            if len(words): 
     186                what = words.pop(0) 
     187            else: 
     188                what = '' 
    183189            if cmd == 'go': 
    184190                oldplace = place(player.place) 
     
    192198                    move_thing(player, newplace) 
    193199                else: 
    194                     player.send_message(nil, 'You cannot go there') 
     200                    player.send_message(None, 'You cannot go there') 
    195201                return True 
    196202            elif cmd == 'who': 
    197                 player.send_message(None, "Players in \"#{iname}\":") 
    198                 for thing in each_element('thing'): 
    199                     if thing.kind_of?(Player): 
    200                         player.send_message(None, "#{thing.iname} is at/in #{thing.place}") 
     203                player.send_message(None, 'Players in "%s":' % self.name) 
     204                for thing in self.things.values(): 
     205                    if isinstance(thing, Player): 
     206                        player.send_message(None, '%s is at/in %s' % (thing.name, thing.place)) 
    201207                return True 
    202208            else: 
    203209                handled = False 
    204210                for thing in each_thing_by_place(player.place): 
    205                     if what.downcase == thing.command_name.downcase: 
    206                         for c in thing.each_element('on-command'): 
    207                             if c.attributes['command'] == cmd 
    208                             thing.command(player, c, words) 
    209                             handled = True 
     211                    if what.lower() in thing.aliases: 
     212                        for action in thing.actions: 
     213                            for c in action.expressions 
     214                                if c == cmd: 
     215                                    thing.command(player, action, words) 
     216                                    handled = True 
    210217                if handled: 
    211218                    return True 
    212219        return False 
    213220 
    214 class Thing(REXML::Element): 
    215     def initialize(world): 
    216         super('thing') 
    217         self.world = world 
    218  
    219     def add(xmlelement): 
    220         if xmlelement.kind_of?(REXML::Element) && (xmlelement.name == 'presence'): 
    221             super(Jabber::Presence.import(xmlelement)) 
    222         else: 
    223             super(xmlelement) 
    224  
    225     def iname(self): 
    226         return attributes['name'] 
    227  
    228     def command_name(self): 
    229         if attributes['command-name']: 
    230             return attributes['command-name'] 
    231         return self.iname() 
    232  
    233     def place(): 
    234         return attributes['place'] 
    235  
    236     def place(p): 
    237         attributes['place'] = p 
    238  
    239     def jid(): 
    240         return None 
    241  
    242     def presence(): 
    243         xe = nil 
    244         for pres in each_element('presence'): 
    245             xe = Jabber::Presence.import(pres) 
    246         if self.kind_of?(Player): 
    247             xe.add(Jabber::MUC::XMUCUser.new).add(Jabber::MUC::XMUCUserItem.new(:none, :participant)) 
    248         else: 
    249             xe.add(Jabber::MUC::XMUCUser.new).add(Jabber::MUC::XMUCUserItem.new(:owner, :moderator)) 
    250         return xe 
    251  
    252     def presence(pres): 
    253         delete_elements('presence') 
    254         add(pres) 
    255  
    256     def see(place): 
    257         pass 
    258  
    259     def send_message(fromresource, text, subject=None): 
    260         pass 
    261  
    262     def send_message_to_place(fromresource, text): 
    263         for thing in self.world.each_element('thing'): 
    264             if thing.place == place: 
    265                 thing.send_message(fromresource, text) 
    266    
    267     def on_enter(thing, from): 
    268         for c in each_element('on-enter'): 
    269             command(thing, c, [from]) 
    270  
    271     def on_leave(thing, to): 
    272         for c in each_element('on-leave'): 
    273             command(thing, c, [to]) 
    274  
    275     def command(source, command, arguments): 
    276         for action in command.each_element: 
    277             if action.text: 
    278                 text = action.text.dup 
    279             else: 
    280                 text = '' 
    281             text.gsub!('%self%', iname) 
    282             text.gsub!('%actor%', source.iname) 
    283             text.gsub!('%place%', place) 
    284             if action.name == 'say' or action.name == 'tell': 
    285                 sender = nil 
    286                 if action.name == 'say': 
    287                     sender = iname 
    288                 if action.attributes['to'] == 'all': 
    289                     send_message_to_place(sender, text) 
    290                 else: 
    291                     source.send_message(sender, text) 
    292