[pypy-svn] r29412 - in pypy/dist/pypy/translator/js: . demo/jsdemo tools

fijal at codespeak.net fijal at codespeak.net
Tue Jun 27 19:56:06 CEST 2006


Author: fijal
Date: Tue Jun 27 19:56:03 2006
New Revision: 29412

Modified:
   pypy/dist/pypy/translator/js/demo/jsdemo/bnb.py
   pypy/dist/pypy/translator/js/jts.py
   pypy/dist/pypy/translator/js/tools/start_bnb.py
Log:
Added some way of handling full frame, but it's not perfect. Especially we need to cache pictures client-side. Needs some caching anyway and reverse (?) order of rendering frames. Fixed session issues, so it's now multiplayer in some sort of way.



Modified: pypy/dist/pypy/translator/js/demo/jsdemo/bnb.py
==============================================================================
--- pypy/dist/pypy/translator/js/demo/jsdemo/bnb.py	(original)
+++ pypy/dist/pypy/translator/js/demo/jsdemo/bnb.py	Tue Jun 27 19:56:03 2006
@@ -22,6 +22,8 @@
 
 
 class SpriteManager(object):
+    FULL_FRAME = 100
+    
     def __init__(self):
         self.sprite_sets = {}
         self.positions = {}
@@ -29,10 +31,18 @@
         self.next_pos = {}
         self.last_seen = set()
         self.seen = set()
+        self.num_frame = 0
     
     def def_icon(self, icon_code):
         self.sprite_sets[icon_code] = []
     
+    def get_frame_number(self):
+        self.num_frame += 1
+        if self.num_frame >= self.FULL_FRAME:
+            self.num_frame = 0
+            return True
+        return False
+    
     def get_sprite(self, icon_code, x, y):
         try:
             to_ret = self.positions[(icon_code, x, y)]
@@ -73,8 +83,9 @@
 # and semi-transparent communication proxy
 class BnbRoot(Root, BasicExternal):
     _serverMessage = {}
+    _spriteManagers = {}
 
-    host = 'snake'
+    host = 'localhost'
     try:
         port = re.findall('value=".*"', urllib.urlopen('http://%s:8000' % host).read())[0]
     except IOError:
@@ -90,6 +101,7 @@
         'add_player'   : MethodDesc( [('player_id', 0), ('callback', (lambda : None))] , {'aa':[{'aa':'bb'}]}),
         'remove_player': MethodDesc( [('player_id', 0), ('callback', (lambda : None))] , {'aa':[{'aa':'bb'}]}),
         'key'          : MethodDesc( [('player_id', 0), ('keynum', '0'), ('callback', (lambda : None))] , {'aa':[{'aa':'bb'}]}),
+        'initialize_session' : MethodDesc( [('callback', (lambda : None))], {'aa':'bb'}),
         
 #        'add_player0'   : MethodDesc( [('callback', (lambda : None))] , {'aa':[{'aa':'bb'}]}),
 #        'remove_player0': MethodDesc( [('callback', (lambda : None))] , {'aa':[{'aa':'bb'}]}),
@@ -126,15 +138,13 @@
             #XXX todo: session.socket.close() after a timeout
         return sm.socket
 
+    def get_sprite_manager(self):
+        sessionid = session['_id']
+        return self._spriteManagers[sessionid]
+
     @turbogears.expose(html="jsdemo.templates.bnb")
     def index(self):
         import time
-        self._close()
-        #force new session id to restart a game!
-        session['_id'] = md5.md5(str(random.random())).hexdigest()
-        self._serverMessage[session['_id']] = ServerMessage('static/images/')
-        self.new_sprites = 0
-        self.sm = SpriteManager()
         return dict(now=time.ctime(), onload=self.jsname, code=self.jssource)
     
     @turbogears.expose(format='json')
@@ -206,6 +216,16 @@
             del self._serverMessage[sessionid]
 
     @turbogears.expose(format="json")
+    def initialize_session(self):
+        self._close()
+        #force new session id to restart a game!
+        session['_id'] = md5.md5(str(random.random())).hexdigest()
+        sessionid = session['_id']
+        self._serverMessage[sessionid] = ServerMessage('static/images/')
+        self._spriteManagers[sessionid] = SpriteManager()
+        return dict()
+
+    @turbogears.expose(format="json")
     def get_message(self):
         #XXX hangs if not first sending CMSG_PING!
         sm   = self.serverMessage()
@@ -246,25 +266,44 @@
         #    log('MESSAGES:lenbefore=%d, inline_frames=%s, lenafter=%d' % (
         #        len_before, inline_frames, len(messages)))
         to_append = []
+        sprite_manager = self.get_sprite_manager()
+        
+        def get_full_frame(next):
+            new_sprite, s_num = sprite_manager.get_sprite(*next)
+            to_append.append({'type':'show_sprite', 's':s_num, 'icon_code':str(next[0]), 'x':str(next[1]), 'y':str(next[2])})
+        
+        def get_partial_frame(next):
+            new_sprite, s_num = sprite_manager.get_sprite(*next)
+            if new_sprite == 'new':
+                to_append.append({'type':'ns', 's':s_num, 'icon_code':str(next[0]), 'x':str(next[1]), 'y':str(next[2])})
+            elif new_sprite == 'move':
+                to_append.append({'type':'sm', 's':str(s_num), 'x':str(next[1]), 'y':str(next[2])})
+
+        if sprite_manager.get_frame_number():
+            full_frame = True
+            get_frame = get_full_frame
+            to_append.append({'type':'begin_clean_sprites'})
+        else:
+            full_frame = False
+            get_frame = get_partial_frame
+            
         for i, msg in enumerate(messages):
             if msg['type'] == PMSG_INLINE_FRAME:
                 for next in msg['sprites']:
                     #to_append.append({'type':'ns', 's':self.num, 'icon_code':str(next[0]), 'x':str(next[1]), 'y':str(next[2])})
                     #self.num += 1
-                    new_sprite, s_num = self.sm.get_sprite(*next)
-                    if new_sprite == 'new':
-                        to_append.append({'type':'ns', 's':s_num, 'icon_code':str(next[0]), 'x':str(next[1]), 'y':str(next[2])})
-                    elif new_sprite == 'move':
-                        to_append.append({'type':'sm', 's':str(s_num), 'x':str(next[1]), 'y':str(next[2])})
+                    get_frame(next)
                 del messages[i]
 
         empty_frame = False
-        if self.sm.seen == set([]):
+        if sprite_manager.seen == set([]):
             empty_frame = True
         
-        if not empty_frame:
-            for i in self.sm.end_frame():
+        if not empty_frame and not full_frame:
+            for i in sprite_manager.end_frame():
                 to_append.append({'type':'ds', 's':str(i)})
+        if full_frame:
+            to_append.append({'type':'clean_sprites'})
         messages += to_append
         #messages.append(to_append[0])
         #print len(messages)

Modified: pypy/dist/pypy/translator/js/jts.py
==============================================================================
--- pypy/dist/pypy/translator/js/jts.py	(original)
+++ pypy/dist/pypy/translator/js/jts.py	Tue Jun 27 19:56:03 2006
@@ -46,6 +46,8 @@
             return '""'
         elif isinstance(t, ootype.Dict):
             return "Object"
+        elif isinstance(t, ootype.DictItemsIterator):
+            return "Object"
         #return "var"
         raise NotImplementedError("Type %r" % (t,))
     

Modified: pypy/dist/pypy/translator/js/tools/start_bnb.py
==============================================================================
--- pypy/dist/pypy/translator/js/tools/start_bnb.py	(original)
+++ pypy/dist/pypy/translator/js/tools/start_bnb.py	Tue Jun 27 19:56:03 2006
@@ -68,6 +68,7 @@
     def __init__(self):
         self.sprites = {}
         self.filenames = {}
+        self.all_sprites = {}
 
     def add_icon(self, icon_code, filename):
         self.filenames[icon_code] = filename
@@ -99,6 +100,23 @@
         i.style.visibility = "hidden"
         #pass
     
+    def start_clean_sprites(self):
+        self.all_sprites = {}
+    
+    def show_sprite(self, s, icon_code, x, y):
+        self.all_sprites[s] = 1
+        try:
+            self.move_sprite(s, x, y)
+        except KeyError:
+            self.add_sprite(s, icon_code, x, y)
+    
+    def end_clean_sprites(self):
+        for i in self.sprites:
+            try:
+                self.all_sprites[i]
+            except KeyError:
+                self.hide_sprite(i)
+    
     #def show_sprite(self, s):
     #    i = self.sprites[s]
     #    i.style.visibility = "visible"
@@ -128,6 +146,13 @@
         sm.move_sprite(msg['s'], msg['x'], msg['y'])
     elif msg['type'] == 'ds':
         sm.hide_sprite(msg['s'])
+    elif msg['type'] == 'begin_clean_sprites':
+        sm.start_clean_sprites()
+    elif msg['type'] == 'clean_sprites':
+        sm.end_clean_sprites()
+    elif msg['type'] == 'show_sprite':
+        sm.show_sprite(msg['s'], msg['icon_code'], msg['x'], msg['y'])
+        
     #elif msg['type'] == 'ss':
     #    sm.show_sprite(msg['s'])
 
@@ -187,12 +212,16 @@
     get_document().title = str(stats.n_sprites) + " sprites " + str(stats.fps)
     #sc.revive()
     
+def session_dispatcher(msgs):
+    log("Something...")
+    BnbRootInstance.get_message(bnb_dispatcher)
+
 def run_bnb():
     def bnb():
         #get_document().
         createLoggingPane(True)
         log("keys: <a>dd player, <r>emove player and <e><s><d><x> to walk around")
-        BnbRootInstance.get_message(bnb_dispatcher)
+        BnbRootInstance.initialize_session(session_dispatcher)
         set_on_keydown(keydown)
         set_on_keyup(keyup)
     



More information about the Pypy-commit mailing list