[Sugar-devel] [PATCH Maze] Collaboration: allow multiple users in the same XO

Manuel Kaufmann humitos at gmail.com
Mon Apr 16 23:24:01 EDT 2012


This is a workaround to the problem with the real nick on a jabber server[1].
Now, we are using the unique identifier (mesh.my_handle or event.handle) for
each player in the game plus a "-%d" at the end of the uid. This value
(Player.uid) is added only when the Activity is shared otherwise this
field is None.

This new workaround allow to distinguish between many users (up to 3) for each
XO laptop -using the gamepad keys.

[1] http://dev.laptop.org/ticket/10750

Signed-off-by: Manuel Kaufmann <humitos at gmail.com>
---
 game.py   |   27 ++++++++++++++++++++-------
 player.py |   24 ++++++++++++++----------
 2 files changed, 34 insertions(+), 17 deletions(-)

diff --git a/game.py b/game.py
index b6319b1..85f6d7f 100644
--- a/game.py
+++ b/game.py
@@ -33,7 +33,9 @@ import pygame
 import olpcgames
 
 import logging
+logging.basicConfig()
 log = logging.getLogger('Maze')
+log.setLevel(logging.DEBUG)
 
 import olpcgames.pausescreen as pausescreen
 import olpcgames.mesh as mesh
@@ -195,7 +197,7 @@ class MazeGame:
 
                 if len(self.remoteplayers) > 0:
                     mesh.broadcast("move:%s,%d,%d,%d,%d" % \
-                                   (player.nick,
+                                   (player.uid,
                                     player.position[0],
                                     player.position[1],
                                     player.direction[0],
@@ -245,6 +247,7 @@ class MazeGame:
 
         elif event.type == mesh.CONNECT:
             log.debug("Connected to the mesh")
+
         elif event.type == mesh.PARTICIPANT_ADD:
             log.debug('mesh.PARTICIPANT_ADD')
 
@@ -252,10 +255,19 @@ class MazeGame:
                 if event.handle == mesh.my_handle():
                     log.debug("Me: %s - %s", buddy.props.nick,
                                   buddy.props.color)
+                    # README: this is a workaround to use an unique
+                    # identifier instead the nick of the buddy
+                    # http://dev.laptop.org/ticket/10750
+                    count = ''
+                    for i, player in enumerate(self.localplayers):
+                        if i > 0:
+                            count = '-%d' % i
+                        player.uid = mesh.my_handle() + count
                 else:
                     log.debug("Join: %s - %s", buddy.props.nick,
                                   buddy.props.color)
                     player = Player(buddy)
+                    player.uid = event.handle
                     self.remoteplayers[event.handle] = player
                     self.allplayers.append(player)
                     self.allplayers.extend(player.bonusPlayers())
@@ -269,8 +281,9 @@ class MazeGame:
                                   self.maze.width, self.maze.height))
                     for player in self.localplayers:
                         if not player.hidden:
-                            mesh.send_to(event.handle, "move:%s,%d,%d,%d,%d" % \
-                                         (player.nick,
+                            mesh.send_to(event.handle,
+                                         "move:%s,%d,%d,%d,%d" % \
+                                         (player.uid,
                                           player.position[0],
                                           player.position[1],
                                           player.direction[0],
@@ -367,7 +380,7 @@ class MazeGame:
             return
         if message.startswith("move:"):
             # a player has moved
-            nick, x, y, dx, dy = message[5:].split(",")[:5]
+            uid, x, y, dx, dy = message[5:].split(",")[:5]
 
             # README: this function (player.bonusPlayer) sometimes
             # returns None and the activity doesn't move the players.
@@ -377,7 +390,7 @@ class MazeGame:
             # So, we have set remote users with this kind of name but
             # we receive the reald child's name in the mesh message
 
-            # player = player.bonusPlayer(nick)
+            player = player.bonusPlayer(uid)
             player.hidden = False
 
             self.markPointDirty(player.position)
@@ -400,8 +413,8 @@ class MazeGame:
                 self.reset()
         elif message.startswith("finish:"):
             # someone finished the maze
-            nick, elapsed = message[7:].split(",")[:2]
-            player = player.bonusPlayer(nick)
+            uid, elapsed = message[7:].split(",")[:2]
+            player = player.bonusPlayer(uid)
             player.elapsed = float(elapsed)
             self.markPointDirty(player.position)
         else:
diff --git a/player.py b/player.py
index a6636dc..ee2df37 100644
--- a/player.py
+++ b/player.py
@@ -22,15 +22,12 @@
 #     You should have received a copy of the GNU General Public License
 #     along with Maze.activity.  If not, see <http://www.gnu.org/licenses/>.
 
-from olpcgames.util import get_bundle_path
-bundlepath = get_bundle_path()
-from sugar.graphics.icon import Icon
-from sugar.graphics.xocolor import XoColor
 import pygame
-import re
-import os
 import unicodedata
 
+from olpcgames.util import get_bundle_path
+bundlepath = get_bundle_path()
+
 
 class Player:
     def __init__(self, buddy, shape='circle'):
@@ -39,6 +36,11 @@ class Player:
         self.nick = unicodedata.normalize('NFC',name)
         colors = buddy.props.color.split(",")
 
+        # this field is None when the activity is not shared and when
+        # the user shared it this field will become to
+        # "olpcgames.mesh.my_handle()"
+        self.uid = None
+
         def string2Color(str):
             return (int(str[1:3], 16), int(str[3:5], 16), int(str[5:7], 16))
         self.colors = map(string2Color, colors)
@@ -123,17 +125,19 @@ class Player:
             self.bonusplayers.append(Player(self.buddy, 'square'))
             self.bonusplayers.append(Player(self.buddy, 'triangle'))
 
-            count = 2
+            count = 1
             for player in self.bonusplayers:
                 player.nick = self.nick + "-%d" % count
+                if self.uid is not None:
+                    player.uid = self.uid + "-%d" % count
                 player.hidden = True
                 count += 1
 
         return self.bonusplayers
 
-    def bonusPlayer(self, nick):
-        if nick == self.nick:
+    def bonusPlayer(self, uid):
+        if uid == self.uid:
             return self
         for bonusplayer in self.bonusPlayers():
-            if bonusplayer.nick == nick:
+            if bonusplayer.uid == uid:
                 return bonusplayer
-- 
1.7.9.5



More information about the Sugar-devel mailing list