Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/activity.py
blob: b5f091aae5b11ee6974b9d4cbe6a60c4d7837f16 (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
import logging
from gettext import gettext as _

import dbus
import gtk
import telepathy
import telepathy.client

from sugar.activity.activity import Activity, ActivityToolbox
from sugar.presence import presenceservice
import sugar.logger

import boardwidget
from game import SERVICE, abstractBoard, GoGame

from sugar.presence.tubeconn import TubeConnection
from buddiespanel import BuddiesPanel
from infopanel import InfoPanel


logger = logging.getLogger('PlayGO')


class PlayGo(Activity):
    """
    Enter the PlayGo activity.
        1. intialize our parent object
        2. create an empty abstract board
        3. create the graphic objects, boardWidget, buddyPanel, InfoPanel
        4. group them in layout containers
        5. create an ActivityToolBox
        6. open a channel to the presence server
        7.1 if creating the game, make me the only player and go into local play mode
        7.2 if connecting to a neighborhood game, call the activity connection methods
    """
    def __init__(self, handle):
        Activity.__init__(self, handle)
        self._name = handle
        
        self.set_title('PlayGo')

        logger.debug('Starting Playgo activity...')
        
        toolbox = ActivityToolbox(self)
        self.set_toolbox(toolbox)

        board = abstractBoard( 19 )
        self.boardWidget = boardwidget.BoardWidget( board, self )
        self.buddies_panel = BuddiesPanel()
        self.info_panel = InfoPanel()

        #Prepare the main box
        self._main_view = gtk.VBox()
        
        #Prepare the top box
        self.top_view = gtk.HBox(False, 10)
        self.boardWidget.set_size_request(770, 770)
        self.top_view.pack_start(self.boardWidget, False)
        
        self.info_panel = InfoPanel()        

        self.buttons_box = gtk.VBox()
        #Pass button
        self.pass_button = gtk.Button(_('Pass'))
        self.pass_button.connect("clicked",  self.pass_cb)
#        self.buttons_box.pack_start(self.pass_button,  False,  False, 10)
        
        #Undo button
        self.undo_button = gtk.Button(_('Undo'))
        self.pass_button.connect("clicked", self.undo_cb)
#        self.buttons_box.pack_start(self.undo_button, False, False, 10)
        
        self.top_view.pack_start(self.buttons_box,  False)

        #Pack the top box and the InfoPanel into the main box
        self._main_view.pack_start(self.top_view)
        self._main_view.pack_start(self.info_panel)

        #Set canvas and show all
        self.set_canvas(self._main_view)
        self.show_all()

        self.pservice = presenceservice.get_instance()
        owner = self.pservice.get_owner()
        self.owner = owner
        self.shared = False

        # This displays the buddies_panel even if we fail to connect:
        self.buddies_panel.add_watcher(owner)
        self.info_panel.show(_('Place a black stone to play locally. You may share or invite to play remotely'))

        self.initiating = None

        self.game = None

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

        if self._shared_activity:
            # we are joining the activity
            self.buddies_panel.add_watcher(owner)
            self.connect('joined', self._joined_cb)
            self._shared_activity.connect('buddy-joined', self._buddy_joined_cb)
            self._shared_activity.connect('buddy-left', self._buddy_left_cb)
            if self.get_shared():
                # oh, OK, we've already joined
                self._joined_cb()
        else:
            # we are creating the activity
            self.buddies_panel.remove_watcher(owner)
            self.buddies_panel.add_player(owner)
            #self.buddies_panel.set_is_playing(owner)
            #self.buddies_panel.set_count(owner, 69)

        self.connect('key-press-event', self.key_press_cb)

    def pass_cb(self, widget, data=None):
        self.game.change_turn()
        
    def undo_cb(self, widget, data=None):
        pass

    def _get_buddy(self, cs_handle):
        """Get a Buddy from a channel specific handle."""
        # FIXME: After Update.1, design a better API for sugar.presence
        # to track handles of buddies, and use that instead
        logger.debug('Trying to find owner of handle %u...', cs_handle)
        group = self.text_chan[telepathy.CHANNEL_INTERFACE_GROUP]
        my_csh = group.GetSelfHandle()
        logger.debug('My handle in that group is %u', my_csh)
        if my_csh == cs_handle:
            handle = self.conn.GetSelfHandle()
            logger.debug('CS handle %u belongs to me, %u', cs_handle, handle)
        elif group.GetGroupFlags() & telepathy.CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES:
            handle = group.GetHandleOwners([cs_handle])[0]
            logger.debug('CS handle %u belongs to %u', cs_handle, handle)
        else:
            handle = cs_handle
            logger.debug('non-CS handle %u belongs to itself', handle)

            # XXX: deal with failure to get the handle owner
            assert handle != 0

        return self.pservice.get_buddy_by_telepathy_handle(
            self.conn.service_name, self.conn.object_path, handle)

    def key_press_cb(self, widget, event):
        logger.debug('Keypress: %r, %r', widget, event)

        if self.game is not None:
            self.game.key_press_event(widget, event)

    def _shared_cb(self, activity):
        logger.debug('My PlayGo activity was shared')
        self.shared = True
        self.initiating = True
        self._setup()

        for buddy in self._shared_activity.get_joined_buddies():
            self.buddies_panel.add_watcher(buddy)

        self._shared_activity.connect('buddy-joined', self._buddy_joined_cb)
        self._shared_activity.connect('buddy-left', self._buddy_left_cb)

        logger.debug('This is my activity: making a tube...')
        id = self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].OfferDBusTube(
            SERVICE, {})
        self.info_panel.show(_('Waiting for another player to join.'))

    def _setup(self):
        if self._shared_activity is None:
            logger.error('Failed to share or join activity')
            return

        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)

    def _list_tubes_reply_cb(self, tubes):
        for tube_info in tubes:
            self._new_tube_cb(*tube_info)

    def _list_tubes_error_cb(self, e):
        logger.error('ListTubes() failed: %s', e)

    def _joined_cb(self, activity):
        if self.game is not None:
            return

        if not self._shared_activity:
            return

        for buddy in self._shared_activity.get_joined_buddies():
            self.buddies_panel.add_watcher(buddy)

        logger.debug('Joined an existing Connect game')
        self.info_panel.show(_('Joined a game. Waiting for my turn...'))
        self.initiating = False
        self._setup()

        logger.debug('This is not my 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)

    def _new_tube_cb(self, id, initiator, type, service, params, state):
        logger.debug('New tube: ID=%d initator=%d type=%d service=%s '
                     'params=%r state=%d', id, initiator, type, service,
                     params, state)

        if (self.game is None and 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.game = GoGame(tube_conn, self.boardWidget, self.initiating,
                self.buddies_panel, self.info_panel, self.owner,
                self._get_buddy, self)

    def _buddy_joined_cb (self, activity, buddy):
        logger.debug("buddy joined")
        self.buddies_panel.add_watcher(buddy)

    def _buddy_left_cb (self,  activity, buddy):
        logger.debug("buddy left")
        self.buddies_panel.remove_watcher(buddy)