comparison browser_side/card_game.py @ 37:b306aa090438

Tarot game: game launching (first hand showed), and contract selection
author Goffi <goffi@goffi.org>
date Wed, 18 May 2011 01:45:28 +0200
parents 1d406077b49b
children 7bea2ae0c4fb
comparison
equal deleted inserted replaced
36:1d406077b49b 37:b306aa090438
19 along with this program. If not, see <http://www.gnu.org/licenses/>. 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """ 20 """
21 21
22 import pyjd # this is dummy in pyjs 22 import pyjd # this is dummy in pyjs
23 from pyjamas.ui.AbsolutePanel import AbsolutePanel 23 from pyjamas.ui.AbsolutePanel import AbsolutePanel
24 from pyjamas.ui.VerticalPanel import VerticalPanel
25 from pyjamas.ui.HorizontalPanel import HorizontalPanel
26 from pyjamas.ui.DockPanel import DockPanel
27 from pyjamas.ui.DialogBox import DialogBox
28 from pyjamas.ui.ListBox import ListBox
24 from pyjamas.ui.Image import Image 29 from pyjamas.ui.Image import Image
30 from pyjamas.ui.Label import Label
31 from pyjamas.ui.Button import Button
32 from pyjamas.ui import HasAlignment
25 33
26 from pyjamas.dnd import makeDraggable 34 from pyjamas.dnd import makeDraggable
27 from pyjamas.ui.DragWidget import DragWidget, DragContainer 35 from pyjamas.ui.DragWidget import DragWidget, DragContainer
28 from jid import JID 36 from jid import JID
29 from tools import html_sanitize 37 from tools import html_sanitize
36 CARD_WIDTH = 74 44 CARD_WIDTH = 74
37 CARD_HEIGHT = 136 45 CARD_HEIGHT = 136
38 MIN_WIDTH = 950 #Minimum size of the panel 46 MIN_WIDTH = 950 #Minimum size of the panel
39 MIN_HEIGHT = 500 47 MIN_HEIGHT = 500
40 48
49 class ContratChooser(DialogBox):
50
51 def __init__(self, parent):
52 """
53 Dialog asking to choose the contrat
54 """
55 self._parent = parent
56 DialogBox.__init__(self, modal=False, centered=True)
57
58 content = VerticalPanel()
59 content.setWidth('100%')
60 self.contrats_list = ListBox()
61 self.contrats_list.setVisibleItemCount(5)
62 self.contrats_list.setWidth("100%")
63 self.contrats_list.setStyleName('contratsChooser')
64 for contrat in ['Passe', 'Petite', 'Garde', 'Garde Sans', 'Garde Contre']:
65 self.contrats_list.addItem(contrat)
66 self.contrats_list.setSelectedIndex(0)
67 content.add(self.contrats_list)
68 button_panel = HorizontalPanel()
69 self.choose_button = Button("Choose", self.onChoose)
70 button_panel.add(self.choose_button)
71 content.add(button_panel)
72 self.setHTML("Please select your contrat")
73 self.setWidget(content)
74
75 def onChoose(self):
76 self.hide()
77 self._parent.contratSelected(self.contrats_list.getSelectedItemText()[0])
41 78
42 class CardWidget(TarotCard, Image): 79 class CardWidget(TarotCard, Image):
43 """This class is used to represent a card, graphically and logically""" 80 """This class is used to represent a card, graphically and logically"""
44 81
45 def __init__(self, file): 82 def __init__(self, file):
56 @param x: abscissa 93 @param x: abscissa
57 @param y: ordinate""" 94 @param y: ordinate"""
58 pass 95 pass
59 #dc.DrawBitmap(self.bitmap, x, y, True) 96 #dc.DrawBitmap(self.bitmap, x, y, True)
60 97
61 class CardPanel(AbsolutePanel): 98 class CardPanel(DockPanel):
62 99
63 def __init__(self, parent, referee, players, player_nick): 100 def __init__(self, parent, referee, players, player_nick):
64 self._parent = parent 101 self._parent = parent
65 self._autoplay = None #XXX: use 0 to activate fake play, None else 102 self._autoplay = None #XXX: use 0 to activate fake play, None else
66 self.referee = referee 103 self.referee = referee
81 self.selected = [] #Card choosed by the player (e.g. during ecart) 118 self.selected = [] #Card choosed by the player (e.g. during ecart)
82 self.hand_size = 13 #number of cards in a hand 119 self.hand_size = 13 #number of cards in a hand
83 self.hand = [] 120 self.hand = []
84 self.to_show = [] 121 self.to_show = []
85 self.state = None 122 self.state = None
86 AbsolutePanel.__init__(self) 123 DockPanel.__init__(self)
87 self.setSize("%spx" % MIN_WIDTH, "%spx" % MIN_HEIGHT) 124 self.setSize("%spx" % MIN_WIDTH, "%spx" % MIN_HEIGHT)
88 self.setStyleName("cardPanel") 125 self.setStyleName("cardPanel")
126
127 _label = Label(self.top_nick)
128 _label.setStyleName('cardGamePlayerNick')
129 self.add(_label, DockPanel.NORTH)
130 self.setCellWidth(_label, '100%')
131 self.setCellHorizontalAlignment(_label, HasAlignment.ALIGN_CENTER)
132
133 self.hand_panel = AbsolutePanel()
134 self.add(self.hand_panel, DockPanel.SOUTH)
135 self.setCellWidth(self.hand_panel, '100%')
136 self.setCellHorizontalAlignment(self.hand_panel, HasAlignment.ALIGN_CENTER)
137
138
139 _label = Label(self.left_nick)
140 _label.setStyleName('cardGamePlayerNick')
141 self.add(_label, DockPanel.WEST)
142 self.setCellHeight(_label, '100%')
143 self.setCellVerticalAlignment(_label, HasAlignment.ALIGN_MIDDLE)
144
145 _label = Label(self.right_nick)
146 _label.setStyleName('cardGamePlayerNick')
147 self.add(_label, DockPanel.EAST)
148 self.setCellHeight(_label, '100%')
149 self.setCellHorizontalAlignment(_label, HasAlignment.ALIGN_RIGHT)
150 self.setCellVerticalAlignment(_label, HasAlignment.ALIGN_MIDDLE)
151
152 self.center_panel = DockPanel()
153 self.add(self.center_panel, DockPanel.CENTER)
154 self.setCellWidth(self.center_panel, '100%')
155
156
157 """for side in zip(['left', 'top', 'right'],
158 [DockPanel.WEST, DockPanel.NORTH, DockPanel.EAST]):
159 _nick = getattr(self, "%s_nick" % side[0])
160 _label = Label(_nick)
161 _label.setStyleName('cardGamePlayerNick')
162 self.add(_label, side[1])"""
89 self.loadCards() 163 self.loadCards()
90 self.mouse_over_card = None #contain the card to highlight 164 self.mouse_over_card = None #contain the card to highlight
91 self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards 165 self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards
92 166
93 def _getTarotCardsPathsCb(self, paths):
94 for file in paths:
95 card = CardWidget(file)
96 self.cards[(card.suit, card.value)]=card
97 self.deck.append(card)
98 167
99 def loadCards(self, dir): 168 def loadCards(self, dir):
100 """Load all the cards in memory 169 """Load all the cards in memory
101 @param dir: directory where the PNG files are""" 170 @param dir: directory where the PNG files are"""
171 def _getTarotCardsPathsCb(paths):
172 for file in paths:
173 card = CardWidget(file)
174 self.cards[(card.suit, card.value)]=card
175 self.deck.append(card)
176 self._parent.host.bridge.call('tarotGameReady', None, self.player_nick, self.referee)
102 self.cards={} 177 self.cards={}
103 self.deck=[] 178 self.deck=[]
104 self.cards["atout"]={} #As Tarot is a french game, it's more handy & logical to keep french names 179 self.cards["atout"]={} #As Tarot is a french game, it's more handy & logical to keep french names
105 self.cards["pique"]={} #spade 180 self.cards["pique"]={} #spade
106 self.cards["coeur"]={} #heart 181 self.cards["coeur"]={} #heart
107 self.cards["carreau"]={} #diamond 182 self.cards["carreau"]={} #diamond
108 self.cards["trefle"]={} #club 183 self.cards["trefle"]={} #club
109 self._parent.host.bridge.call('getTarotCardsPaths', self._getTarotCardsPathsCb) 184 self._parent.host.bridge.call('getTarotCardsPaths', _getTarotCardsPathsCb)
110 185
186 def tarotGameNew(self, hand):
187 """Start a new game, with given hand"""
188 for suit, value in hand:
189 self.hand.append(self.cards[(suit, value)])
190 self.hand.sort()
191 self.state = "init"
192 self.updateHand()
193
194 def updateHand(self):
195 """Show the cards in the hand in the hand_panel (SOUTH panel)"""
196 self.hand_panel.clear()
197 self.hand_panel.setSize("%spx" % (self.visible_size * (len(self.hand)+1)), "%spx" % (CARD_HEIGHT + 10))
198 x_pos = 0
199 y_pos = 0
200 for card in self.hand:
201 self.hand_panel.add(card, x_pos, y_pos)
202 x_pos+=self.visible_size
203
204 def tarotGameChooseContrat(self, xml_data):
205 """Called when the player as to select his contrat
206 @param xml_data: SàT xml representation of the form"""
207 #for the moment we cheat a little bit and make our own dialog box
208 #but XMLUI must be user ASAP, as in other frontends
209 contrat_chooser = ContratChooser(self)
210 contrat_chooser.show()
211
212 def contratSelected(self, contrat):
213 """Must be called when the contrat is selected
214 @param contrat: one of the valid contrat value"""
215 print "Contrat choosed:", contrat
216 self._parent.host.bridge.call('tarotGameContratChoosed', None, self.player_nick, self.referee, contrat or 'Passe') #FIXME: must use roomID ! cf quick_card_game for same issue
217