Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to Python 3 and Collabwrapper #20

Open
wants to merge 53 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
096e4de
port to python 3
sanjaymaniam Mar 7, 2020
de9993d
StringIO
sanjaymaniam Mar 7, 2020
21f5313
print brackets in sprites.py
sanjaymaniam Mar 7, 2020
843e9aa
uncomment telepathy & dbus
sanjaymaniam Mar 7, 2020
525a449
TelepathyGLib
sanjaymaniam Mar 7, 2020
a6f8509
remove dbus, TelepathyGLib
sanjaymaniam Mar 8, 2020
a711ff3
import collabwrapper
sanjaymaniam Mar 8, 2020
7fa4dd9
import collabwrapper
sanjaymaniam Mar 8, 2020
549117c
_message_cb
sanjaymaniam Mar 8, 2020
7838949
TypeError for sprite.py
sanjaymaniam Mar 8, 2020
3c9582f
test
sanjaymaniam Mar 8, 2020
5446a57
test
sanjaymaniam Mar 8, 2020
63de53c
test
sanjaymaniam Mar 8, 2020
7c326e4
test
sanjaymaniam Mar 8, 2020
81f6f84
test
sanjaymaniam Mar 8, 2020
461b69a
test
sanjaymaniam Mar 8, 2020
574a72e
comments
sanjaymaniam Mar 9, 2020
9608ed9
syncing
sanjaymaniam Mar 9, 2020
21bdb26
syncing
sanjaymaniam Mar 9, 2020
64c94b0
syncing
sanjaymaniam Mar 10, 2020
b93f829
porting
sanjaymaniam Mar 10, 2020
4eb1c1c
collabwrapper
sanjaymaniam Mar 10, 2020
92bb1be
collabwrapper
sanjaymaniam Mar 11, 2020
8316a1b
collabwrapper
sanjaymaniam Mar 11, 2020
9c042de
sync
sanjaymaniam Mar 11, 2020
4c2cca3
collabwrapper
sanjaymaniam Mar 11, 2020
7298d09
collabwrapper
sanjaymaniam Mar 11, 2020
0f7b421
collabwrapper
sanjaymaniam Mar 11, 2020
1cef68f
collabwrapper
sanjaymaniam Mar 11, 2020
9986051
collabwrapper
sanjaymaniam Mar 12, 2020
bbcc963
collabwrapper
sanjaymaniam Mar 12, 2020
c97b921
collabwrapper
sanjaymaniam Mar 12, 2020
d69b2f1
collabwrapper
sanjaymaniam Mar 12, 2020
90abf08
collabwrapper
sanjaymaniam Mar 12, 2020
23fb0ae
collabwrapper
sanjaymaniam Mar 12, 2020
72f2491
collabwrapper
sanjaymaniam Mar 12, 2020
080b0e8
collabwrapper
sanjaymaniam Mar 12, 2020
1d5cf29
collabwrapper
sanjaymaniam Mar 12, 2020
51719b3
collabwrapper
sanjaymaniam Mar 12, 2020
66368a5
collabwrapper
sanjaymaniam Mar 12, 2020
a73daf9
collabwrapper
sanjaymaniam Mar 12, 2020
f4523b1
collabwrapper
sanjaymaniam Mar 12, 2020
8a6669c
collabwrapper
sanjaymaniam Mar 12, 2020
114054e
collabwrapper
sanjaymaniam Mar 12, 2020
8c8af2d
collabwrapper
sanjaymaniam Mar 12, 2020
afe40f9
remove telepathy imports
sanjaymaniam Mar 13, 2020
598c5ae
self._game.restore_game(dot_list) indent
sanjaymaniam Mar 14, 2020
ffcb600
neighbours
sanjaymaniam Mar 16, 2020
67ff729
string encoding for pixbuf
sanjaymaniam Mar 21, 2020
564c36c
remove comments
sanjaymaniam Mar 21, 2020
e18b3bf
remove comments and prints
sanjaymaniam Mar 21, 2020
9c3fe47
remove prints
sanjaymaniam Mar 21, 2020
d550018
remove label 'X' for blank sprints
sanjaymaniam Mar 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 35 additions & 120 deletions SearchActivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,14 @@
from toolbar_utils import button_factory, label_factory, separator_factory
from utils import json_load, json_dump, convert_seconds_to_minutes

import telepathy
import dbus
from dbus.service import signal
from dbus.gobject_service import ExportedGObject
from sugar3.presence import presenceservice
from sugar3.presence.tubeconn import TubeConnection

from collabwrapper import CollabWrapper
from gettext import gettext as _

import json
from json import load as jload
from json import dump as jdump
from StringIO import StringIO

from io import StringIO

from game import Game

Expand All @@ -44,17 +39,14 @@

SERVICE = 'org.sugarlabs.CookieSearchActivity'
IFACE = SERVICE

PATH = '/org/sugarlabs/CookieSearchActivity'

class SearchActivity(activity.Activity):
""" Searching strategy game """

def __init__(self, handle):
""" Initialize the toolbars and the game board """
try:
super(SearchActivity, self).__init__(handle)
except dbus.exceptions.DBusException as e:
_logger.error(str(e))
super(SearchActivity, self).__init__(handle)

self.path = activity.get_bundle_path()
self.all_scores = []
Expand All @@ -78,7 +70,14 @@ def __init__(self, handle):

self._game = Game(canvas, parent=self, path=self.path,
colors=self.colors)
self._setup_presence_service()

self.connect('shared', self._shared_cb)
self.connect('joined', self._joined_cb)

self._collab = CollabWrapper(self)
self._collab.connect('message', self._message_cb)
self._collab.connect('joined', self._joined_cb)
self._collab.setup()

if 'dotlist' in self.metadata:
self._restore()
Expand Down Expand Up @@ -126,6 +125,7 @@ def _new_game_cb(self, button=None):
''' Start a new game. '''
self._game.new_game()


def write_file(self, file_path):
""" Write the grid status to the Journal """
dot_list = self._game.save_game()
Expand All @@ -134,6 +134,7 @@ def write_file(self, file_path):
self.metadata['dotlist'] += str(dot)
if dot_list.index(dot) < len(dot_list) - 1:
self.metadata['dotlist'] += ' '

self.metadata['all_scores'] = \
self._data_dumper(self.all_scores)
self.metadata['current_gametime'] = self._game._game_time_seconds
Expand Down Expand Up @@ -180,147 +181,61 @@ def _write_scores_to_clipboard(self, button=None):
_logger.debug(self.all_scores)
scores = ''
for i, s in enumerate(self.all_scores):
scores += '%s: %s\n' % (str(i + 1), s)
scores += '{}: {}\n'.format(str(i + 1), s)
Gtk.Clipboard().set_text(scores)


# Collaboration-related methods

def set_data(self, data):
pass

def _setup_presence_service(self):
""" Setup the Presence Service. """
self.pservice = presenceservice.get_instance()
self.initiating = None # sharing (True) or joining (False)

owner = self.pservice.get_owner()
self.owner = owner
self._share = ""
self.connect('shared', self._shared_cb)
self.connect('joined', self._joined_cb)
def get_data(self):
return None

def _shared_cb(self, activity):
""" Either set up initial share..."""
self._new_tube_common(True)
self.after_share_join(True)

def _joined_cb(self, activity):
""" ...or join an exisiting share. """
self._new_tube_common(False)
self.after_share_join(False)

def _new_tube_common(self, sharer):
""" Joining and sharing are mostly the same... """
if self._shared_activity is None:
_logger.debug("Error: Failed to share or join activity ... \
_shared_activity is null in _shared_cb()")
return

self.initiating = sharer
def after_share_join(self, sharer):
self.waiting_for_hand = not sharer

self.conn = self._shared_activity.telepathy_conn
self.tubes_chan = self._shared_activity.telepathy_tubes_chan
self.text_chan = self._shared_activity.telepathy_text_chan

self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].connect_to_signal(
'NewTube', self._new_tube_cb)

if sharer:
_logger.debug('This is my activity: making a tube...')
id = self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].OfferDBusTube(
SERVICE, {})
else:
_logger.debug('I am joining an activity: waiting for a tube...')
self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].ListTubes(
reply_handler=self._list_tubes_reply_cb,
error_handler=self._list_tubes_error_cb)
self._game.set_sharing(True)

def _list_tubes_reply_cb(self, tubes):
""" Reply to a list request. """
for tube_info in tubes:
self._new_tube_cb(*tube_info)

def _list_tubes_error_cb(self, e):
""" Log errors. """
_logger.debug('Error: ListTubes() failed: %s' % (e))

def _new_tube_cb(self, id, initiator, type, service, params, state):
""" Create a new tube. """
_logger.debug('New tube: ID=%d initator=%d type=%d service=%s \
params=%r state=%d' % (id, initiator, type, service, params, state))

if (type == telepathy.TUBE_TYPE_DBUS and service == SERVICE):
if state == telepathy.TUBE_STATE_LOCAL_PENDING:
self.tubes_chan[
telepathy.CHANNEL_TYPE_TUBES].AcceptDBusTube(id)

tube_conn = TubeConnection(
self.conn, self.tubes_chan[
telepathy.CHANNEL_TYPE_TUBES], id,
group_iface=self.text_chan[
telepathy.CHANNEL_INTERFACE_GROUP])

self.chattube = ChatTube(tube_conn, self.initiating,
self.event_received_cb)

def _setup_dispatch_table(self):
''' Associate tokens with commands. '''
self._processing_methods = {
'n': [self._receive_new_game, 'get a new game grid'],
'p': [self._receive_dot_click, 'get a dot click'],
}

def event_received_cb(self, event_message):
def _message_cb(self, collab, buddy, msg):
''' Data from a tube has arrived. '''
if len(event_message) == 0:
return
try:
command, payload = event_message.split('|', 2)
except ValueError:
_logger.debug('Could not split event message %s' % (event_message))
return
command = msg.get('command')
payload = msg.get('payload')
self._processing_methods[command][0](payload)

def send_new_game(self):
''' Send a new grid to all players '''
self.send_event('n|%s' % (json_dump(self._game.save_game())))
self.send_event('n', self._game.save_game())

def _receive_new_game(self, payload):
''' Sharer can start a new game. '''
dot_list = json_load(payload)
dot_list = payload
self._game.restore_game(dot_list)

def send_dot_click(self, dot, color):
''' Send a dot click to all the players '''
self.send_event('p|%s' % (json_dump([dot, color])))

self.send_event('p', json_dump([dot, color]))
def _receive_dot_click(self, payload):
''' When a dot is clicked, everyone should change its color. '''
''' When a dot is clicked, everyone should change their color. '''
(dot, color) = json_load(payload)
self._game.remote_button_press(dot, color)

def send_event(self, entry):
def send_event(self, command, payload):
""" Send event through the tube. """
if hasattr(self, 'chattube') and self.chattube is not None:
self.chattube.SendText(entry)


class ChatTube(ExportedGObject):
""" Class for setting up tube for sharing """

def __init__(self, tube, is_initiator, stack_received_cb):
super(ChatTube, self).__init__(tube, PATH)
self.tube = tube
self.is_initiator = is_initiator # Are we sharing or joining activity?
self.stack_received_cb = stack_received_cb
self.stack = ''

self.tube.add_signal_receiver(self.send_stack_cb, 'SendText', IFACE,
path=PATH, sender_keyword='sender')

def send_stack_cb(self, text, sender=None):
if sender == self.tube.get_unique_name():
return
self.stack = text
self.stack_received_cb(text)

@signal(dbus_interface=IFACE, signature='s')
def SendText(self, text):
self.stack = text
self._collab.post({'command': command, 'payload': payload})
2 changes: 1 addition & 1 deletion activity/activity.info
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = CookieSearch
activity_version = 10
license = GPLv3+;MIT
bundle_id = org.sugarlabs.CookieSearchActivity
exec = sugar-activity SearchActivity.SearchActivity
exec = sugar-activity3 SearchActivity.SearchActivity
icon = activity-search
show_launcher = yes
summary = Search for cookies hidden behind a hungry turtle.
Expand Down
Loading