Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Schampijer <simon@schampijer.de>2007-06-30 16:56:03 (GMT)
committer Simon Schampijer <simon@schampijer.de>2007-06-30 16:56:03 (GMT)
commitd421e4b20f4a392bbb711da832740a3baa641592 (patch)
tree2aa17987b89f92f25b94354a4184ceee831e62f3
parent89a8709e6fbcb3ac29c0ccedc58aa6c5bab14f82 (diff)
- moved model in the ConnectionGame
- clean
-rw-r--r--game.py104
-rwxr-xr-xmemosonoactivity.py79
-rw-r--r--model.py16
-rw-r--r--toolbar.py73
4 files changed, 87 insertions, 185 deletions
diff --git a/game.py b/game.py
index f151199..2f32652 100644
--- a/game.py
+++ b/game.py
@@ -18,11 +18,12 @@ _logger = logging.getLogger('memosono-activity.game')
class ConnectGame(ExportedGObject):
- def __init__(self, tube, grid, is_initiator, buddies_panel, info_panel,
+ def __init__(self, tube, playview, model, is_initiator, buddies_panel, info_panel,
owner, get_buddy, activity):
super(ConnectGame, self).__init__(tube, PATH)
self.tube = tube
- self.grid = grid
+ self.pv = playview
+ self.model = model
self.is_initiator = is_initiator
self.entered = False
self.player_id = None
@@ -37,8 +38,10 @@ class ConnectGame(ExportedGObject):
# 2+ are the spectator queue, 2 is to play next
self.ordered_bus_names = []
+ for tile in self.pv.tiles:
+ tile.connect('button-press-event', self._button_press_cb, self.pv.tiles.index(tile))
+
self.tube.watch_participants(self.participant_change_cb)
- self.grid.connect('insert-requested', self.insert_requested_cb)
def participant_change_cb(self, added, removed):
# Initiator is player 0, other player is player 1.
@@ -64,7 +67,7 @@ class ConnectGame(ExportedGObject):
pass
if not self.entered:
- self.tube.add_signal_receiver(self.insert_cb, 'Insert', IFACE,
+ self.tube.add_signal_receiver(self.flip_cb, 'Flip', IFACE,
path=PATH, sender_keyword='sender')
if self.is_initiator:
_logger.debug('I am the initiator, so making myself player 0')
@@ -83,7 +86,7 @@ class ConnectGame(ExportedGObject):
up to date with the game state.
"""
- @method(dbus_interface=IFACE, in_signature='aanas', out_signature='')
+ @method(dbus_interface=IFACE, in_signature='a(nn)', 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.
@@ -96,8 +99,8 @@ class ConnectGame(ExportedGObject):
if self.player_id is None:
_logger.debug('Welcomed to the game. Player bus names are %r',
bus_names)
- self.grid.grid = grid
- dump_grid(grid)
+ self.model.grid = grid
+
self.ordered_bus_names = bus_names
self.player_id = bus_names.index(self.tube.get_unique_name())
# OK, now I'm synched with the game, I can welcome others
@@ -111,8 +114,6 @@ class ConnectGame(ExportedGObject):
if self.get_active_player() == self.player_id:
_logger.debug("It's my turn already!")
self.change_turn()
-
- redraw(self.grid)
else:
_logger.debug("I've already been welcomed, doing nothing")
@@ -120,11 +121,9 @@ class ConnectGame(ExportedGObject):
self.tube.add_signal_receiver(self.hello_cb, 'Hello', IFACE,
path=PATH, sender_keyword='sender')
- @signal(dbus_interface=IFACE, signature='n')
- def Insert(self, column):
- """Signal that the local player has placed a disc."""
- assert column >= 0
- assert column < 7
+ @signal(dbus_interface=IFACE, signature='nsn')
+ def Flip(self, tilenum):
+ """Signal that the local player has flipped a tile."""
def hello_cb(self, sender=None):
"""Tell the newcomer what's going on."""
@@ -135,7 +134,7 @@ 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')
- self.tube.get_object(sender, PATH).Welcome(self.grid.grid,
+ self.tube.get_object(sender, PATH).Welcome(self.model.grid,
self.ordered_bus_names,
dbus_interface=IFACE)
if (self.player_id == 0 and len(self.ordered_bus_names) == 2):
@@ -143,31 +142,22 @@ class ConnectGame(ExportedGObject):
"I go first")
self.change_turn()
- def insert_cb(self, column, sender=None):
- # Someone placed a disc
+ def flip_cb(self, tilenum, sender=None):
handle = self.tube.bus_name_to_handle[sender]
- _logger.debug('Insert(%d) from %s', column, sender)
-
- if self.tube.self_handle == handle:
- _logger.debug('Ignoring Insert signal from myself: %d', column)
- return
-
- try:
- winner = self.grid.insert(column, self.get_active_player())
- except ValueError:
- return
-
- dump_grid(self.grid.grid)
+ _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!')
- redraw(self.grid)
return
self.change_turn()
def change_turn(self):
+ pass
+'''
try:
bus_name = self.ordered_bus_names[self.get_active_player()]
buddy = self._get_buddy(self.tube.bus_name_to_handle[bus_name])
@@ -179,13 +169,10 @@ class ConnectGame(ExportedGObject):
if self.get_active_player() == self.player_id:
_logger.debug('It\'s my turn now')
self.info_panel.show('Your turn')
- self.grid.selected_column = 3
self.activity.grab_focus()
else:
_logger.debug('It\'s not my turn')
- self.grid.selected_column = None
- redraw(self.grid)
def get_active_player(self):
count = {}
@@ -199,42 +186,17 @@ class ConnectGame(ExportedGObject):
return 1
else:
return 0
-
- def key_press_event(self, widget, event):
- if self.grid.selected_column is None:
- _logger.debug('Ignoring keypress - not my turn')
- return
-
- _logger.debug('Keypress: keyval %s', event.keyval)
-
- if event.keyval in (gtk.keysyms.Left,):
- _logger.debug('<--')
- if self.grid.selected_column > 0:
- self.grid.selected_column -= 1
- redraw(self.grid)
- elif event.keyval in (gtk.keysyms.Right,):
- _logger.debug('-->')
- if self.grid.selected_column < 6:
- self.grid.selected_column += 1
- redraw(self.grid)
- elif event.keyval in (gtk.keysyms.Down, gtk.keysyms.space):
- _logger.debug('v')
- self.insert_requested_cb(self.grid, self.grid.selected_column)
-
- def insert_requested_cb(self, grid, col):
- winner = grid.insert(col, self.player_id)
- if winner == -1:
+'''
+ def _button_press_cb(self, tile, event, tilenum=None):
+ if self.turn is None:
+ _logger.debug('Ignoring flip - not my turn')
return
-
- _logger.debug('Inserting at %d', col)
- dump_grid(grid.grid)
- redraw(grid)
- self.Insert(col)
-
- self.change_turn()
-
- if winner is not None:
- print "I win"
- self.info_panel.show('You win!')
- else:
- self.info_panel.show('Other player\'s turn')
+
+ _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.Flipp(tilenum, obj, color)
+
diff --git a/memosonoactivity.py b/memosonoactivity.py
index f02e9b0..e82e6b5 100755
--- a/memosonoactivity.py
+++ b/memosonoactivity.py
@@ -31,31 +31,52 @@ from sugar.activity.activity import Activity
from sugar.activity.activity import ActivityToolbox
from sugar.presence import presenceservice
-from csound.csoundserver import CsoundServer
from playview import PlayView
-#from toolbar import PlayToolbar
-from model import Game
+from buddiespanel import BuddiesPanel
+from infopanel import InfoPanel
+from model import Model
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):
Activity.__init__(self, handle)
- self.set_title ("Memosono")
- self.pv = None
- toolbox = ActivityToolbox(self)
- self.set_toolbox(toolbox)
- toolbox.show()
-
- self.games = {}
+ logger.debug('Starting Memosono activity...')
- os.path.walk(os.path.join(os.path.dirname(__file__), 'games'), self._find_games, None)
+ self.set_title(_('Memsosono Activity'))
- gamelist = self.games.keys()
- gamelist.sort()
- logging.debug(gamelist)
- self.pv = PlayView(None, self.games[gamelist[0]].pairs)
+ self.model = Model(GAME_PATH, os.path.dirname(__file__))
+ self.model.read('drumgit.mson')
+ self.model.def_grid()
+
+ self.pv = PlayView( len(self.model.grid) )
self.pv.show()
+
+ self.buddies_panel = BuddiesPanel()
+
+ self.info_panel = InfoPanel()
+
+ vbox = hippo.CanvasBox(spacing=4,
+ orientation=hippo.ORIENTATION_VERTICAL)
+
+ hbox = hippo.CanvasBox(spacing=4,
+ orientation=hippo.ORIENTATION_HORIZONTAL)
+
+ hbox.append(self.buddies_panel)
+ hbox.append(self.pv, hippo.PACK_EXPAND)
+
+ vbox.append(hbox, hippo.PACK_EXPAND)
+ vbox.append(self.info_panel, hippo.PACK_END)
+
+ canvas = hippo.Canvas()
+ canvas.set_root(vbox)
+ self.set_canvas(canvas)
+ self.show_all()
+
self.pservice = presenceservice.get_instance()
self.owner = self.pservice.get_owner()
@@ -188,7 +209,7 @@ class MemosonoActivity(Activity):
tube_conn = TubeConnection(self.conn,
self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES],
id, group_iface=self.text_chan[telepathy.CHANNEL_INTERFACE_GROUP])
- self.game = ConnectGame(tube_conn, self.grid, self.initiating,
+ self.game = ConnectGame(tube_conn, self.pv, self.model, self.initiating,
self.pv.buddies_panel, self.info_panel, self.owner,
self._get_buddy, self)
@@ -200,27 +221,5 @@ class MemosonoActivity(Activity):
logging.debug('buddy left')
self.pv.buddies_panel.remove_watcher(buddy)
-
- def _find_games(self, arg, dirname, names):
- for name in names:
- if name.endswith('.mson'):
- game = Game(dirname, os.path.dirname(__file__))
- game.read(name)
- self.games[name.split('.mson')[0]] = game
-
- def _cleanup_cb(self, data=None):
- pass
- #self.controler.oscapi.send(('127.0.0.1', 6783), "/CSOUND/quit", [])
- #self.controler.oscapi.iosock.close()
- #self.server.oscapi.iosock.close()
- #logging.debug(" Closed OSC sockets ")
-
- def _focus_in(self, event, data=None):
- pass
- #logging.debug(" Memosono is visible: Connect to the Csound-Server. ")
- #self.controler.oscapi.send(('127.0.0.1', 6783), "/CSOUND/connect", [])
-
- def _focus_out(self, event, data=None):
- pass
- #logging.debug(" Memosono is invisible: Close the connection to the Csound-Server. ")
- #self.controler.oscapi.send(('127.0.0.1', 6783), "/CSOUND/disconnect", [])
+
+
diff --git a/model.py b/model.py
index 6d2811d..3545c0b 100644
--- a/model.py
+++ b/model.py
@@ -1,11 +1,13 @@
import libxml2
import os
import logging
+import random
class Model(object):
def __init__(self, gamepath, dtdpath, name='noname'):
self.name = name
self.pairs = {}
+ self.grid = []
self.gamepath = gamepath
self.dtdpath = dtdpath
@@ -67,7 +69,19 @@ class Model(object):
if doc.validateDtd(self.ctxt, self.dtd):
doc.saveFormatFile(filename, 1)
doc.freeDoc()
-
+
+ def def_grid(self):
+ print 'pairs: %s' %self.model.pairs
+ ### create grid from pairs information
+ for key in self.pairs.iterkeys():
+ self.grid.append((key, 0))
+ self.grid.append((key, 1))
+ print 'self.grid: %s'%self.grid
+
+ ### shuffle the grid tiles
+ random.shuffle(self.grid)
+ print 'self.grid after shufle: %s'%self.grid
+
if __name__ == '__main__':
print "[Read game from file] "
diff --git a/toolbar.py b/toolbar.py
deleted file mode 100644
index 1e79185..0000000
--- a/toolbar.py
+++ /dev/null
@@ -1,73 +0,0 @@
-# Copyright (C) 2007, Simon Schampijer
-#
-# 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
-
-from gettext import gettext as _
-import logging
-
-import gtk
-
-from sugar.graphics.combobox import ComboBox
-from sugar.graphics.toolbutton import ToolButton
-
-
-class PlayToolbar(gtk.Toolbar):
- def __init__(self, activity):
- gtk.Toolbar.__init__(self)
-
- self._activity = activity
-
- self._image = ToolButton('go-previous')
- self._image_id = self._image.connect('clicked', self._back_cb)
- self.insert(self._image, -1)
- self._image.show()
-
- separator = gtk.SeparatorToolItem()
- separator.set_draw(True)
- self.insert(separator, -1)
-
- self._num_players_combo = ComboBox()
- self._num_players = ['1', '2', '3', '4', '5', '6', '7', '8']
- self._num_players_combo.connect('changed', self._num_players_changed_cb)
- for i, s in enumerate(self._num_players):
- self._num_players_combo.append_item(i, s, None)
- if s == '1':
- self._num_players_combo.set_active(i)
- self._add_widget(self._num_players_combo)
-
- #self.close = ToolButton('window-close')
- #self.close.connect('clicked', self._close_clicked_cb)
- #self.insert(self.close, -1)
- #self.close.show()
-
- #def _close_clicked_cb(self, button):
- # self._activity.close()
-
- def _add_widget(self, widget, expand=False):
- tool_item = gtk.ToolItem()
- tool_item.set_expand(expand)
-
- tool_item.add(widget)
- widget.show()
-
- self.insert(tool_item, -1)
- tool_item.show()
-
- def _num_players_changed_cb(self, num_players_combo):
- logging.debug('num_players=' + self._num_players[self._num_players_combo.get_active()] )
-
- def _back_cb(self, button):
- pass
-