Changeset 103
- Timestamp:
- 10/14/07 13:02:39 (1 year ago)
- Files:
-
- adventure/adventuremuc.py (modified) (4 diffs)
- adventure/cave.xml (modified) (1 diff)
- adventure/player.py (modified) (3 diffs)
- adventure/thing.py (modified) (2 diffs)
- adventure/world.dtd (modified) (4 diffs)
- adventure/world.py (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
adventure/adventuremuc.py
r102 r103 24 24 def add_world(self, filename): 25 25 print 'Adding world from "%s"...' % filename 26 world = World .new_from_file(self, filename)26 world = World(self, filename) 27 27 self.worlds[world.node] = world 28 print ' ' + world. iname28 print ' ' + world.name 29 29 30 30 def send(self, worldnode, worldresource, stanza): … … 86 86 87 87 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()) 89 89 90 90 world = self.worlds[pres.to.node] 91 91 if not world: 92 92 answer = pres.answer 93 answer. type = :error94 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.')) 95 95 self.component.send(answer) 96 96 else: … … 99 99 100 100 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())) 102 102 103 103 world = self.worlds[msg.getTo()] 104 104 if not world: 105 105 answer = msg.buildReply() 106 answer. type = 'error'106 answer.setType('error') 107 107 #answer.add(Jabber::Error.new('item-not-found', 'The world you are trying to reach is currently unavailable.')) 108 108 self.send(answer) … … 116 116 if __name__ == '__main__': 117 117 if len argv != 4: 118 print 'Syntax: ./adventure .rb<JID> <Password> <Host> <Port>'118 print 'Syntax: ./adventuremuc.py <JID> <Password> <Host> <Port>' 119 119 print 'See README for further help' 120 120 muc = AdventureMUC(*argv) adventure/cave.xml
r101 r103 39 39 <commands> 40 40 <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"> 42 42 <use target="bottle" /> 43 43 <narration>%actor% use alcool bottle on %self%</narration> adventure/player.py
r102 r103 1 1 # -*- coding: utf-8 -*- 2 class Player :#(Thing):2 class Player(Thing): 3 3 def __init__(self, world, name, jid): 4 4 self.world = world … … 7 7 self.place = None 8 8 9 def see( place):9 def see(self, place): 10 10 if not place: 11 11 return … … 17 17 send_message(None, 'You can go %s}' % ', '.join(place.exits.keys())) 18 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? 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) 23 24 self.world.send(fromresource, msg) 24 25 adventure/thing.py
r101 r103 1 1 # -*- coding: utf-8 -*- 2 2 3 from player import Player 4 3 5 class Thing: 4 def _init__(self, name, parent): 6 def _init__(self, world, name, parent): 7 self.word = word 5 8 self.name = name 6 9 self.parent = parent 7 10 self.aliases = list() # Noms alternatifs 8 11 self.actions = list() 9 self.presence = ...12 self.presence = None 10 13 self.UID = 0 11 14 self.place = None 12 15 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 13 77 class Action: 14 78 def _init__(self, name, parent): … … 19 83 # un élément par alias ( ex "use bottle on", "use whiskey on" ) 20 84 #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 25 25 respawn CDATA #IMPLIED> 26 26 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?)> 29 29 30 30 <!ELEMENT commands (command+,inherit+)> … … 37 37 enabled CDATA #IMPLIED> 38 38 39 <!ELEMENT command (say?,narration?,presence?,commands?,destroy?,use?,give? )>39 <!ELEMENT command (say?,narration?,presence?,commands?,destroy?,use?,give?,move?)> 40 40 <!ATTLIST command 41 41 name CDATA #REQUIRED 42 object CDATA #IMPLIED>42 objects CDATA #IMPLIED> 43 43 44 44 <!ELEMENT inherit (#EMPTY)> … … 54 54 <!ELEMENT say (#PCDATA)> 55 55 <!ATTLIST say 56 target CDATA #IMPLIED> 56 target CDATA #IMPLIED 57 to CDATA #IMPLIED> 57 58 58 59 <!ELEMENT destroy (#EMPTY)> … … 75 76 place CDATA #REQUIRED> 76 77 78 <!ELEMENT move (#EMPTY)> 79 <!ATTLIST move 80 target CDATA #IMPLIED 81 place CDATA #REQUIRED> 82 77 83 <!ELEMENT narration (#PCDATA)> 78 84 adventure/world.py
r102 r103 4 4 from player import Player 5 5 from xml.dom.minidom import parse as xml_parse 6 6 from xmpp import Message, Presence 7 7 8 8 class World: 9 def __init__(self, muc ):9 def __init__(self, muc, filename): 10 10 self.muc = muc 11 11 self.places = dict() # {'place_name': place, ...} 12 #self.things =12 self.things = dict() 13 13 14 14 doc = xml_parse(filename).documentElement … … 22 22 place_obj = Place(place.getAttribute('name'), 23 23 place.getAttribute('description')) 24 self.places[place_obj.name] = place_obj 24 25 gos = place.getElementsByTagName('go') 25 26 for go in gos: … … 41 42 return None 42 43 43 def each_thing_by_place( placename):44 def each_thing_by_place(self, placename): 44 45 for thing in self.things.values(): 45 46 if thing.place == placename: 46 47 yield(thing) 47 48 48 def move_thing( thing, newplace):49 def move_thing(self, thing, newplace): 49 50 for t in each_thing_by_place(thing.place): 50 51 # Call leave hooks … … 56 57 pres.setType('unavailable') 57 58 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) 59 61 60 62 # Broadcast unavailability presence to all who are here 61 63 if thing.presence: 62 64 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) 66 69 67 70 # Enter new place … … 73 76 if t.presence: 74 77 pres = Jabber::Presence.import(t.presence) 75 pres. to = thing.jid76 se nd(t.iname, pres)78 pres.setTo(thing.jid) 79 self.send(t.name, pres) 77 80 78 81 # Broadcast availability presence to all who are here 79 82 if thing.presence: 80 83 pres = Jabber::Presence.import(thing.presence) 81 pres. to = t.jid82 se nd(thing.iname, pres)84 pres.setTo(t.jid) 85 self.send(thing.name, pres) 83 86 84 87 thing.send_message(None, ' ') 85 88 if newplace: 86 subject = newplace. dup.capitalize()89 subject = newplace.capitalize() 87 90 else: 88 91 subject = ' ' … … 97 100 def handle_presence(pres): 98 101 # 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) 101 104 msg.type = 'normal' 102 105 msg.subject = 'Adventure component help' … … 154 157 player = None 155 158 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: 157 160 player = thing 158 161 159 162 if not player: 160 163 answer = msg.answer 161 answer. type = :error162 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()): 167 170 for thing in each_thing_by_place(player.place): 168 171 thing.send_message(player.iname, msg.body) … … 172 175 player.send_message(None, '(Command) who') 173 176 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']}) 175 178 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)) 178 181 return True 179 182 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 = '' 183 189 if cmd == 'go': 184 190 oldplace = place(player.place) … … 192 198 move_thing(player, newplace) 193 199 else: 194 player.send_message( nil, 'You cannot go there')200 player.send_message(None, 'You cannot go there') 195 201 return True 196 202 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)) 201 207 return True 202 208 else: 203 209 handled = False 204 210 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 210 217 if handled: 211 218 return True 212 219 return False 213 220 214 class Thing(REXML::Element):215 def initialize(world):216 super('thing')217 self.world = world218 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'] = p238 239 def jid():240 return None241 242 def presence():243 xe = nil244 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 xe251 252 def presence(pres):253 delete_elements('presence')254 add(pres)255 256 def see(place):257 pass258 259 def send_message(fromresource, text, subject=None):260 pass261 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.dup279 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 = nil286 if action.name == 'say':287 sender = iname288 if action.attributes['to'] == 'all':289 send_message_to_place(sender, text)290 else:291 source.send_message(sender, text)292
