Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/JigsawPuzzleActivity.py
blob: ee2a2e3d71ba9929fd330244cb70646207912e2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Copyright 2007 World Wide Workshop Foundation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# If you find this activity useful or end up using parts of it in one of your
# own creations we would love to hear from you at info@WorldWideWorkshop.org !
#

# init gthreads before using abiword
import gobject
gobject.threads_init()

from sugar.activity.activity import Activity, ActivityToolbox, get_bundle_path
from gettext import gettext as _
import logging, os, sys
import time
import zlib
from cStringIO import StringIO
from mamamedia_modules import json

from JigsawPuzzleUI import JigsawPuzzleUI
from mamamedia_modules import TubeHelper
from mamamedia_modules import GAME_IDLE, GAME_STARTED, GAME_FINISHED, GAME_QUIT

logger = logging.getLogger('jigsawpuzzle-activity')

from dbus.service import method, signal
from dbus.gobject_service import ExportedGObject

SERVICE = "org.worldwideworkshop.olpc.JigsawPuzzle.Tube"
IFACE = SERVICE
PATH = "/org/worldwideworkshop/olpc/JigsawPuzzle/Tube"

class GameTube (ExportedGObject):
    """ Manage the communication between cooperating activities """
    def __init__(self, tube, is_initiator, activity):
        super(GameTube, self).__init__(tube, PATH)
        self.tube = tube
        self.activity = activity
        self.add_status_update_handler()
        self.get_buddy = activity._get_buddy
        self.syncd_once = False
        if is_initiator:
            self.add_hello_handler()
            self.add_request_image_handler()
            #self.add_need_image_handler()
            #self.activity.ui.connect('game-state-changed', self.game_state_cb)
        else:
            self.add_game_update_handler()
            #self.add_re_sync_handler()
            self.Hello()
        self.add_piece_picked_handler()
        self.add_piece_placed_handler()
        self.add_piece_dropped_handler()
        self.tube.watch_participants(self.participant_change_cb)

    def participant_change_cb(self, added, removed):
        logger.debug('Adding participants: %r', added)
        logger.debug('Removing participants: %r', removed)


    ###############
    # Signals
    
    @signal(dbus_interface=IFACE, signature='')
    def Hello(self):
        """Request that this player's Welcome method is called to bring it
        up to date with the game state.
        """

    @signal(dbus_interface=IFACE, signature='s')
    def GameUpdate(self, game_state):
        """ When an image is chosen by the initiator, this method gets called."""

    @signal(dbus_interface=IFACE, signature='su')
    def StatusUpdate (self, status, ellapsed_time):
        """ signal a reshufle, possibly with a new image """

    @signal(dbus_interface=IFACE, signature='')
    def RequestImage (self):
        """ Request that the game image be sent to us. """

    @signal(dbus_interface=IFACE, signature='i')
    def PiecePicked (self, index):
        """ Signals a piece picked in the correct board position """
        
    @signal(dbus_interface=IFACE, signature='i')
    def PiecePlaced (self, index):
        """ Signals a piece placed in the correct board position """
        
    @signal(dbus_interface=IFACE, signature='i(dd)')
    def PieceDropped (self, index, position):
        """ Signals a piece that has been moved around and dropped """

    ###############
    # Callbacks

    def add_hello_handler(self):
        self.tube.add_signal_receiver(self.hello_cb, 'Hello', IFACE,
                                                                    path=PATH, sender_keyword='sender')

    def hello_cb(self, obj=None, sender=None):
        """Tell the newcomer what's going on."""
        logger.debug('Newcomer %s has joined', sender)
#       if self.activity.ui.get_game_state() < GAME_STARTED:
#           f = {}
#       else:
#           f = self.activity.ui._freeze(journal=False)
        self.tube.get_object(sender, PATH).Welcome(self.activity.ui.get_game_state()[1], dbus_interface=IFACE)

    def add_game_update_handler (self):
        self.tube.add_signal_receiver(self.game_update_cb, 'GameUpdate', IFACE,
                                                                    path=PATH, sender_keyword='sender')

    def game_update_cb (self, game_state, sender=None):
        logger.debug('GameUpdate: %s' % game_state)
        if game_state == GAME_STARTED[1]:
            self.activity.ui.set_game_state(GAME_STARTED)
            self.RequestImage()
        #self.activity.ui.set_game_state(str(game_state))
        #self.activity.ui._thaw(json.read(str(state)))

    def add_request_image_handler (self):
        self.tube.add_signal_receiver(self.request_image_cb, 'RequestImage', IFACE,
                                                                    path=PATH, sender_keyword='sender')

    def request_image_cb (self, sender=None):
        logger.debug('Sending image to %s', sender)
        img = self.activity.ui.game.board.cutboard.get_image_as_png()
        t = time.time()
        compressed = zlib.compress(img, 9)
        # We will be sending the image, 24K at a time (my tests put the high water at 48K)
        logger.debug("was %d, is %d. compressed to %d%% in %0.4f seconds" % (len(img), len(compressed), len(compressed)*100/len(img), time.time() - t))
        part_size = 24*1024
        parts = len(compressed) / part_size
        for i in range(parts+1):
            self.tube.get_object(sender, PATH).ImageSync(compressed[i*part_size:(i+1)*part_size], i+1,
                                                         dbus_interface=IFACE)
        self.tube.get_object(sender, PATH).ImageDetailsSync(json.write(self.activity.ui._freeze(journal=False)), dbus_interface=IFACE)


    def add_piece_picked_handler (self):
        self.tube.add_signal_receiver(self.piece_picked_cb, 'PiecePicked', IFACE,
                                                                    path=PATH, sender_keyword='sender')

    def piece_picked_cb (self, index, sender=None):
        if sender != self.activity.get_bus_name():
            self.activity.ui._recv_pick_notification(index)

    def add_piece_placed_handler (self):
        self.tube.add_signal_receiver(self.piece_placed_cb, 'PiecePlaced', IFACE,
                                                                    path=PATH, sender_keyword='sender')

    def piece_placed_cb (self, index, sender=None):
        if sender != self.activity.get_bus_name():
            self.activity.ui._recv_drop_notification(index)
    
    def add_piece_dropped_handler (self):
        self.tube.add_signal_receiver(self.piece_dropped_cb, 'PieceDropped', IFACE,
                                                                    path=PATH, sender_keyword='sender')

    def piece_dropped_cb (self, index, position, sender=None):
        if sender != self.activity.get_bus_name():
            self.activity.ui._recv_drop_notification(index, position)

    def add_status_update_handler(self):
        self.tube.add_signal_receiver(self.status_update_cb, 'StatusUpdate', IFACE,
                                                                    path=PATH, sender_keyword='sender')

    def status_update_cb (self, status, join_time, sender=None):
        buddy = self.get_buddy(self.tube.bus_name_to_handle[sender])
        logger.debug("status_update: %s %s" % (str(sender), str(join_time)))
        nick, stat = self.activity.ui.buddy_panel.update_player(buddy, status, True, int(join_time))
        if buddy != self.activity.owner:
            self.activity.ui.set_message(
                    _("Buddy '%(buddy)s' changed status: %(status)s") % \
                        {'buddy': nick, 'status': stat},
                    frommesh=True)
    
    ##############
    # Methods

    @method(dbus_interface=IFACE, in_signature='s', out_signature='')
    def Welcome(self, game_state):
        """ """
        logger.debug("state: '%s' (%s)" % (game_state, type(game_state)))
        if game_state == GAME_STARTED[1]:
            self.activity.ui.set_game_state(GAME_STARTED)
            self.RequestImage()
        else:
            self.activity.ui.set_game_state(GAME_IDLE)

    @method(dbus_interface=IFACE, in_signature='ayi', out_signature='', byte_arrays=True)
    def ImageSync (self, image_part, part_nr):
        """ """
        logger.debug("Received image part #%d, length %d" % (part_nr, len(image_part)))
        if part_nr == 1:
            self.image = StringIO()
            self.image.write(image_part)
        else:
            self.image.write(image_part)

    @method(dbus_interface=IFACE, in_signature='s', out_signature='', byte_arrays=True)
    def ImageDetailsSync (self, state):
        """ Signals end of image and shares the rest of the needed data to create the image remotely."""
        state = json.read(str(state))
        state['game']['board']['cutboard']['pb'] = zlib.decompress(self.image.getvalue())
        self.activity.ui._thaw(state)
        self.activity.ui._send_status_update()
        

class JigsawPuzzleActivity(Activity, TubeHelper):
    def __init__(self, handle):
        Activity.__init__(self, handle)
        logger.debug('Starting Jigsaw Puzzle activity... %s' % str(get_bundle_path()))
        os.chdir(get_bundle_path())

        self.connect('destroy', self._destroy_cb)
        
        toolbox = ActivityToolbox(self)
        self.set_toolbox(toolbox)
        toolbox.show()

    # Toolbar title size hack
        title_widget = toolbox._activity_toolbar.title
        title_widget.set_size_request(title_widget.get_layout().get_pixel_size()[0] + 30, -1)
        
        self.ui = JigsawPuzzleUI(self)
        self.set_canvas(self.ui)

        self.show_all()

        TubeHelper.__init__(self, tube_class=GameTube, service=SERVICE)

    def _destroy_cb(self, data=None):
        return True

    def new_tube_cb (self):
        self.ui.set_contest_mode(True)

    def shared_cb (self):
        self.ui.buddy_panel.add_player(self.owner)

    def joined_cb (self):
        self.ui.set_readonly()

    def buddy_joined_cb (self, buddy):
        nick = self.ui.buddy_panel.add_player(buddy)
        self.ui.set_message(_("Buddy '%s' joined the game!") % (nick), frommesh=True)

    def buddy_left_cb (self, buddy):
        nick = self.ui.buddy_panel.remove_player(buddy)
        self.ui.set_message(_("Buddy '%s' left the game!") % (nick), frommesh=True)

    def read_file(self, file_path):
        f = open(file_path, 'r')
        try:
            session_data = f.read()
        finally:
            f.close()
        self.ui._thaw(json.read(session_data))
        #import urllib
        #logging.debug('Read session: %s.' % urllib.quote(session_data))
        
    def write_file(self, file_path):
        # First make sure the game is showing, as we need that to get the piece positions
        
        session_data = json.write(self.ui._freeze())
        f = open(file_path, 'w')
        try:
            f.write(session_data)
        finally:
            f.close()
        #import urllib
        #logging.debug('Write session data: %s.' % urllib.quote(session_data))