diff options
author | Simon Schampijer <simon@schampijer.de> | 2007-07-02 21:09:26 (GMT) |
---|---|---|
committer | Simon Schampijer <simon@schampijer.de> | 2007-07-02 21:09:26 (GMT) |
commit | 73859e45443e762bdbae8a1ce9120bf4828873f6 (patch) | |
tree | dcb448e318409adb3586767ec224feb41ca777f7 | |
parent | ea34d78e90d00587d1036d8847d966e2a759099e (diff) |
First networking stuff is working. Getting closer to a first release. Code to change tiles back when not the same must be added now.
-rw-r--r-- | game.py | 79 | ||||
-rw-r--r-- | games/drumgit/drumgit.mson | 6 | ||||
-rwxr-xr-x | memosonoactivity.py | 55 | ||||
-rw-r--r-- | model.py | 18 |
4 files changed, 89 insertions, 69 deletions
@@ -1,6 +1,7 @@ import logging import gtk +import os from dbus import Interface from dbus.service import method, signal @@ -33,7 +34,10 @@ class ConnectGame(ExportedGObject): self._get_buddy = get_buddy self.activity = activity - self.turn = None + self.active_player = 1 + self.count = 0 + self.points = {} + # list indexed by player ID # 0, 1 are players 0, 1 # 2+ are the spectator queue, 2 is to play next @@ -75,6 +79,7 @@ class ConnectGame(ExportedGObject): self.add_hello_handler() self.ordered_bus_names = [self.tube.get_unique_name()] self.player_id = 0 + self.points[self.player_id] = 0 self.buddies_panel.add_player(self.owner) else: _logger.debug('Hello, everyone! What did I miss?') @@ -87,7 +92,7 @@ class ConnectGame(ExportedGObject): up to date with the game state. """ - @method(dbus_interface=IFACE, in_signature='a(nn)as', out_signature='') + @method(dbus_interface=IFACE, in_signature='aanas', out_signature='') def Welcome(self, grid, bus_names): """To be called on the incoming player by the other players to inform them of the game state. @@ -100,10 +105,12 @@ class ConnectGame(ExportedGObject): if self.player_id is None: _logger.debug('Welcomed to the game. Player bus names are %r', bus_names) + _logger.debug('Received the grid: %s', str(grid)) self.model.grid = grid self.ordered_bus_names = bus_names self.player_id = bus_names.index(self.tube.get_unique_name()) + self.points[self.player_id] = 0 # OK, now I'm synched with the game, I can welcome others self.add_hello_handler() @@ -112,7 +119,7 @@ class ConnectGame(ExportedGObject): buddy = self._get_buddy(self.tube.bus_name_to_handle[bus_names[1]]) self.buddies_panel.add_player(buddy) - if self.get_active_player() == self.player_id: + if self.active_player == self.player_id: _logger.debug("It's my turn already!") self.change_turn() else: @@ -135,9 +142,11 @@ class ConnectGame(ExportedGObject): self.buddies_panel.add_player(buddy) _logger.debug('Bus names are now: %r', self.ordered_bus_names) _logger.debug('Welcoming newcomer and sending them the game state') + grid = 0 self.tube.get_object(sender, PATH).Welcome(self.model.grid, self.ordered_bus_names, dbus_interface=IFACE) + _logger.debug('--- After welcome') if (self.player_id == 0 and len(self.ordered_bus_names) == 2): _logger.debug("This is my game and an opponent has joined. " "I go first") @@ -148,56 +157,54 @@ class ConnectGame(ExportedGObject): _logger.debug('Flipped tile(%d) from %s', tilenum, sender) self.pv.flip(tilenum, obj, color) - - if winner is not None: - _logger.debug('Player with handle %d wins', handle) - self.info_panel.show('The other player wins!') - return - self.change_turn() + self.count+=1 + if self.count == 1: + self.comp = tilenum + if self.count == 2: + self.count = 0 + # evaluate + if( self.model.same(tilenum, self.comp) == 1): + _logger.debug('Tile(%d) and (%d) are the same', tilenum, self.comp) + buddy = self._get_buddy(handle) + self.points[self.active_player]+=1 + self.buddies_panel.set_count(buddy, self.points[self.active_player]) + self.info_panel.show('Open another one') + else: + _logger.debug('Tile(%d) and (%d) are NOT the same', tilenum, self.comp) + # next player + self.change_turn() def change_turn(self): - pass - ''' + self.set_active_player() try: - bus_name = self.ordered_bus_names[self.get_active_player()] + bus_name = self.ordered_bus_names[self.active_player] buddy = self._get_buddy(self.tube.bus_name_to_handle[bus_name]) self.buddies_panel.set_is_playing(buddy) except: _logger.error('argh!', exc_info=1) raise - if self.get_active_player() == self.player_id: + if self.active_player == self.player_id: _logger.debug('It\'s my turn now') self.info_panel.show('Your turn') self.activity.grab_focus() else: _logger.debug('It\'s not my turn') + self.info_panel.show('Other player\'s turn') + - - def get_active_player(self): - count = {} - - for row in self.grid.grid: - for player in row: - if player > -1: - count[player] = count.get(player, 0) + 1 - - if count.get(0, 0) > count.get(1, 0): - return 1 + def set_active_player(self): + if self.active_player == 0: + self.active_player = 1 else: - return 0 - ''' + self.active_player = 0 + def _button_press_cb(self, tile, event, tilenum=None): - if self.turn is None: + if self.active_player != self.player_id: _logger.debug('Ignoring flip - not my turn') - return - - _logger.debug('selected tile=%s'%str(tilenum)) - pairkey, moch = self.grid[tilenum] - obj = os.path.join(IMAGES_PATH, self.model.pairs[pairkey][moch]) - color = self.model.pairs[pairkey][2] - logger.debug('obj=%s color=%s'%(obj, color)) - - self.Flip(tilenum, obj, color) + else: + _logger.debug('selected tile=%s'%str(tilenum)) + obj, color = self.model.gettile(tilenum) + self.Flip(tilenum, obj, color) diff --git a/games/drumgit/drumgit.mson b/games/drumgit/drumgit.mson index d5b4cfc..f508fa1 100644 --- a/games/drumgit/drumgit.mson +++ b/games/drumgit/drumgit.mson @@ -1,8 +1,8 @@ <?xml version="1.0"?> <memosono name="drumgit"> - <pair id="1" mother="guitar12_2.jpg" child="guitar12_2.jpg" color="12345" /> - <pair id="2" mother="drumkit2_b.jpg" child="drumkit2_b.jpg" color="234567" /> - <pair id="3" mother="guitar8_2.jpg" child="guitar8_2.jpg" color="23456767" /> + <pair id="1" mother="guitar12_2.jpg" child="guitar12_2.jpg" color="100" /> + <pair id="2" mother="drumkit2_b.jpg" child="drumkit2_b.jpg" color="100" /> + <pair id="3" mother="guitar8_2.jpg" child="guitar8_2.jpg" color="100" /> </memosono> <!-- diff --git a/memosonoactivity.py b/memosonoactivity.py index 92c0dd4..416c3aa 100755 --- a/memosonoactivity.py +++ b/memosonoactivity.py @@ -41,7 +41,6 @@ from game import ConnectGame GAME_PATH = os.path.join(os.path.dirname(__file__),'games/drumgit') -IMAGES_PATH = os.path.join(os.path.dirname(__file__),'games/drumgit/images') class MemosonoActivity(Activity): def __init__(self, handle): @@ -119,6 +118,30 @@ class MemosonoActivity(Activity): #self.buddies_panel.set_is_playing(owner) #self.buddies_panel.set_count(owner, 69) + def _get_buddy(self, cs_handle): + """Get a Buddy from a channel specific handle.""" + logging.debug('Trying to find owner of handle %u...', cs_handle) + group = self.text_chan[telepathy.CHANNEL_INTERFACE_GROUP] + my_csh = group.GetSelfHandle() + logging.debug('My handle in that group is %u', my_csh) + if my_csh == cs_handle: + handle = self.conn.GetSelfHandle() + logging.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] + logging.debug('CS handle %u belongs to %u', cs_handle, handle) + else: + handle = cs_handle + logging.debug('non-CS handle %u belongs to itself', handle) + + # XXX: deal with failure to get the handle owner + assert handle != 0 + + # XXX: we're assuming that we have Buddy objects for all contacts - + # this might break when the server becomes scalable. + return self.pservice.get_buddy_by_telepathy_handle(self.tp_conn_name, + self.tp_conn_path, handle) + def _shared_cb(self, activity): logging.debug('My Memosono activity was shared') self.initiating = True @@ -134,7 +157,7 @@ class MemosonoActivity(Activity): id = self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].OfferTube( telepathy.TUBE_TYPE_DBUS, 'org.fredektop.Telepathy.Tube.Memosono', {}) logging.debug('Tube address: %s', self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].GetDBusServerAddress(id)) - logging.debug('Waiting for another player to join') + self.info_panel.show('Waiting for another player to join') # FIXME: presence service should be tubes-aware and give us more help # with this @@ -200,8 +223,8 @@ class MemosonoActivity(Activity): for buddy in self._shared_activity.get_joined_buddies(): self.buddies_panel.add_watcher(buddy) - logging.debug('Joined an existing Connect game') - logging.debug('Joined a game. Waiting for my turn...') + logging.debug('Joined an existing Memosono game') + self.info_panel.show('Joined a game. Waiting for my turn...') self.initiating = False self._setup() @@ -210,30 +233,6 @@ class MemosonoActivity(Activity): reply_handler=self._list_tubes_reply_cb, error_handler=self._list_tubes_error_cb) - def _get_buddy(self, cs_handle): - """Get a Buddy from a channel specific handle.""" - logging.debug('Trying to find owner of handle %u...', cs_handle) - group = self.text_chan[telepathy.CHANNEL_INTERFACE_GROUP] - my_csh = group.GetSelfHandle() - logging.debug('My handle in that group is %u', my_csh) - if my_csh == cs_handle: - handle = self.conn.GetSelfHandle() - logging.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] - logging.debug('CS handle %u belongs to %u', cs_handle, handle) - else: - handle = cs_handle - logging.debug('non-CS handle %u belongs to itself', handle) - - # XXX: deal with failure to get the handle owner - assert handle != 0 - - # XXX: we're assuming that we have Buddy objects for all contacts - - # this might break when the server becomes scalable. - return self.pservice.get_buddy_by_telepathy_handle(self.tp_conn_name, - self.tp_conn_path, handle) - def _new_tube_cb(self, id, initiator, type, service, params, state): logging.debug('New tube: ID=%d initator=%d type=%d service=%s ' 'params=%r state=%d', id, initiator, type, service, @@ -3,6 +3,8 @@ import os import logging import random +IMAGES_PATH = os.path.join(os.path.dirname(__file__),'games/drumgit/images') + class Model(object): def __init__(self, gamepath, dtdpath, name='noname'): self.name = name @@ -31,12 +33,12 @@ class Model(object): for elem in res: attributes = elem.get_properties() pair = [] - idpair = '' + idpair = 0 for attribute in attributes: if(attribute.name == 'text'): pass if(attribute.name == 'id'): - idpair = attribute.content + idpair = int(attribute.content) if(attribute.name == 'mother'): pair.append(attribute.content) if(attribute.name == 'child'): @@ -82,6 +84,18 @@ class Model(object): random.shuffle(self.grid) print 'self.grid after shufle: %s'%self.grid + def gettile(self, tilenum): + pairkey, moch = self.grid[tilenum] + obj = os.path.join(IMAGES_PATH, self.pairs[pairkey][moch]) + color = self.pairs[pairkey][2] + # logger.debug('obj=%s color=%s'%(obj, color)) + return (obj, color) + + def same(self, a, b): + pairkeya, moch = self.grid[a] + pairkeyb, moch = self.grid[b] + return (pairkeya == pairkeyb) + if __name__ == '__main__': print "[Read game from file] " |