From: <umg...@us...> - 2007-04-03 13:26:32
|
Revision: 397 http://svn.sourceforge.net/pybridge/?rev=397&view=rev Author: umgangee Date: 2007-04-03 06:26:31 -0700 (Tue, 03 Apr 2007) Log Message: ----------- Fix WindowBridgetable to open/close WindowBidbox; clean up WindowBidbox code. Modified Paths: -------------- trunk/pybridge/pybridge/ui/window_bidbox.py trunk/pybridge/pybridge/ui/window_bridgetable.py Modified: trunk/pybridge/pybridge/ui/window_bidbox.py =================================================================== --- trunk/pybridge/pybridge/ui/window_bidbox.py 2007-04-03 12:00:17 UTC (rev 396) +++ trunk/pybridge/pybridge/ui/window_bidbox.py 2007-04-03 13:26:31 UTC (rev 397) @@ -1,5 +1,5 @@ # PyBridge -- online contract bridge made easy. -# Copyright (C) 2004-2005 PyBridge Project. +# Copyright (C) 2004-2007 PyBridge Project. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -10,7 +10,7 @@ # 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 Street, Fifth Floor, Boston, MA 02110-1301, USA @@ -24,15 +24,14 @@ from pybridge.bridge.call import Bid, Pass, Double, Redouble from pybridge.bridge.call import Level, Strain -CALL_NAMES = {'bid' : Bid, 'pass' : Pass, - 'double' : Double, 'redouble' : Redouble, } +CALL_NAMES = {'bid': Bid, 'pass': Pass, 'double': Double, 'redouble': Redouble} -LEVEL_NAMES = {'1' : Level.One, '2' : Level.Two, '3' : Level.Three, - '4' : Level.Four, '5' : Level.Five, '6' : Level.Six, - '7' : Level.Seven, } +LEVEL_NAMES = {'1': Level.One, '2': Level.Two,'3': Level.Three, + '4': Level.Four, '5': Level.Five, '6': Level.Six, + '7': Level.Seven, } -STRAIN_NAMES = {'club' : Strain.Club, 'diamond' : Strain.Diamond, - 'heart' : Strain.Heart, 'spade' : Strain.Spade, +STRAIN_NAMES = {'club': Strain.Club, 'diamond': Strain.Diamond, + 'heart': Strain.Heart, 'spade': Strain.Spade, 'nt' : Strain.NoTrump, } ALL_CALLS = [Pass(), Double(), Redouble()] + \ @@ -50,34 +49,57 @@ glade_name = 'window_bidbox' - def new(self): + def setUp(self): + self.game = None self.eventHandler = SimpleEventHandler(self) - self.parent.table.game.attach(self.eventHandler) - self.set_available_calls(self.parent.position, self.parent.table.game.bidding) - def set_available_calls(self, position, bidding): - """Enables buttons representing the given calls.""" - if bidding.whoseTurn() == position: - self.window.set_property('sensitive', True) - for call in ALL_CALLS: - button = self.get_button_from_call(call) - button.set_property('sensitive', bidding.isValidCall(call)) - else: - self.window.set_property('sensitive', False) + def tearDown(self): + if self.game: + self.game.detach(self.eventHandler) -# Registered event handlers. + def monitor(self, game, position, callSelected): + """Monitor the state of bidding in game. + + @param game: the BridgeGame for which to observe bidding session. + @param position: if user is playing, their position in the game. + @param callSelected: a handler to invoke when user selects a call. + """ + if self.game: + self.game.detach(self.eventHandler) + self.game = game + self.position = position + self.callSelected = callSelected + self.game.attach(self.eventHandler) + self.enableCalls() + + +# Event handlers. + + def event_makeCall(self, call, position): - self.set_available_calls(self.parent.position, self.parent.table.game.bidding) + self.enableCalls() # Utility methods. - def get_button_from_call(self, call): + def enableCalls(self): + """Enables buttons representing the calls available to player.""" + if self.position == self.game.getTurn(): + self.window.set_property('sensitive', True) + for call in ALL_CALLS: + button = self.getButtonFromCall(call) + isvalid = self.game.bidding.isValidCall(call) + button.set_property('sensitive', isvalid) + else: + self.window.set_property('sensitive', False) + + + def getButtonFromCall(self, call): """Returns a pointer to GtkButton object representing given call.""" callname = [k for k,v in CALL_NAMES.items() if isinstance(call, v)][0] if isinstance(call, Bid): @@ -88,14 +110,14 @@ return getattr(self, 'button_%s' % callname) - def get_call_from_button(self, widget): + def getCallFromButton(self, widget): """Returns an instance of the call represented by given GtkButton.""" text = widget.get_name().split('_') # "button", calltype, level, strain calltype = CALL_NAMES[text[1]] - if calltype == Bid: + if calltype is Bid: level = LEVEL_NAMES[text[2]] strain = STRAIN_NAMES[text[3]] - return calltype(level, strain) + return Bid(level, strain) return calltype() @@ -103,12 +125,8 @@ def on_call_clicked(self, widget, *args): - """Builds a call object and submits.""" - # Do not check validity of call: the server will do that. - # If call is invalid, ignore the resultant errback. - call = self.get_call_from_button(widget) - d = self.parent.player.callRemote('makeCall', call) - d.addErrback(lambda r: True) # Ignore any error. + call = self.getCallFromButton(widget) + self.callSelected(call) # Invoke external handler. def on_window_bidbox_delete_event(self, widget, *args): Modified: trunk/pybridge/pybridge/ui/window_bridgetable.py =================================================================== --- trunk/pybridge/pybridge/ui/window_bridgetable.py 2007-04-03 12:00:17 UTC (rev 396) +++ trunk/pybridge/pybridge/ui/window_bridgetable.py 2007-04-03 13:26:31 UTC (rev 397) @@ -24,6 +24,8 @@ from eventhandler import SimpleEventHandler from manager import WindowManager +from window_bidbox import WindowBidbox + from pybridge.network.error import GameError from pybridge.bridge.call import Bid, Pass, Double, Redouble from pybridge.bridge.symbols import Direction, Level, Strain, Rank, Vulnerable @@ -124,13 +126,13 @@ def tearDown(self): - print "Cleaning up" - self.table = None # Dereference table. # Close all child windows. - for windowname in self.children.keys(): - self.children.close(windowname) + for window in self.children.values(): + self.children.close(window) + self.table = None # Dereference table. + def errback(self, failure): print "Error: %s" % failure.getErrorMessage() @@ -177,8 +179,10 @@ # If user is a player and bidding in progress, open bidding box. if self.player and not self.table.game.bidding.isComplete(): - self.children.open('window_bidbox', parent=self) + bidbox = self.children.open(WindowBidbox, parent=self) + bidbox.monitor(self.table.game, self.position, self.on_call_selected) + # Initialise seat menu and player labels. for position in Direction: player = self.table.players.get(position) # Player name or None. @@ -421,15 +425,17 @@ d = self.player.callRemote('getHand') d.addCallbacks(self.table.game.revealHand, self.errback, callbackKeywords={'position' : self.position}) - self.children.open('window_bidbox', parent=self) + bidbox = self.children.open(WindowBidbox, parent=self) + bidbox.monitor(self.table.game, self.position, self.on_call_selected) def event_makeCall(self, call, position): self.addCall(call, position) self.setTurnIndicator() if self.table.game.bidding.isComplete(): - self.children.close('window_bidbox') # If playing. self.setContract() + if self.children.get(WindowBidbox): # If a player. + self.children.close(self.children[WindowBidbox]) def event_playCard(self, card, position): @@ -507,11 +513,16 @@ # Signal handlers. + def on_call_selected(self, call): + if self.player: + d = self.player.callRemote('makeCall', call) + d.addErrback(self.errback) + + def on_card_clicked(self, card, position): if self.player: d = self.player.callRemote('playCard', card) d.addErrback(self.errback) - #d.addErrback(lambda _: True) # Ignore any error. def on_seat_activated(self, widget, position): @@ -528,7 +539,8 @@ d.addCallbacks(self.table.game.revealHand, self.errback, callbackKeywords={'position' : self.position}) if not self.table.game.bidding.isComplete(): - self.children.open('window_bidbox', parent=self) + bidbox = self.children.open(WindowBidbox, parent=self) + bidbox.monitor(self.table.game, self.position, self.on_call_selected) d = self.table.joinGame(position) d.addCallbacks(success, self.errback) @@ -549,7 +561,8 @@ self.position = None self.takeseat.set_property('sensitive', True) self.leaveseat.set_property('sensitive', False) - self.children.close('window_bidbox') # If open. + if self.children.get(WindowBidbox): + self.children.close(self.children[WindowBidbox]) d = self.table.leaveGame(self.position) d.addCallbacks(success, self.errback) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |