Mercurial > libervia-web
view 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 |
line wrap: on
line source
#!/usr/bin/python # -*- coding: utf-8 -*- """ Libervia: a Salut à Toi frontend Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org) This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """ import pyjd # this is dummy in pyjs from pyjamas.ui.AbsolutePanel import AbsolutePanel from pyjamas.ui.VerticalPanel import VerticalPanel from pyjamas.ui.HorizontalPanel import HorizontalPanel from pyjamas.ui.DockPanel import DockPanel from pyjamas.ui.DialogBox import DialogBox from pyjamas.ui.ListBox import ListBox from pyjamas.ui.Image import Image from pyjamas.ui.Label import Label from pyjamas.ui.Button import Button from pyjamas.ui import HasAlignment from pyjamas.dnd import makeDraggable from pyjamas.ui.DragWidget import DragWidget, DragContainer from jid import JID from tools import html_sanitize from datetime import datetime from time import time from games import TarotCard CARD_WIDTH = 74 CARD_HEIGHT = 136 MIN_WIDTH = 950 #Minimum size of the panel MIN_HEIGHT = 500 class ContratChooser(DialogBox): def __init__(self, parent): """ Dialog asking to choose the contrat """ self._parent = parent DialogBox.__init__(self, modal=False, centered=True) content = VerticalPanel() content.setWidth('100%') self.contrats_list = ListBox() self.contrats_list.setVisibleItemCount(5) self.contrats_list.setWidth("100%") self.contrats_list.setStyleName('contratsChooser') for contrat in ['Passe', 'Petite', 'Garde', 'Garde Sans', 'Garde Contre']: self.contrats_list.addItem(contrat) self.contrats_list.setSelectedIndex(0) content.add(self.contrats_list) button_panel = HorizontalPanel() self.choose_button = Button("Choose", self.onChoose) button_panel.add(self.choose_button) content.add(button_panel) self.setHTML("Please select your contrat") self.setWidget(content) def onChoose(self): self.hide() self._parent.contratSelected(self.contrats_list.getSelectedItemText()[0]) class CardWidget(TarotCard, Image): """This class is used to represent a card, graphically and logically""" def __init__(self, file): """@param file: path of the PNG file""" Image.__init__(self,file) root_name = file[file.rfind("/")+1:-4] suit,value = root_name.split('_') TarotCard.__init__(self, (suit, value)) print "Carte:",suit, value #, self.bout def draw(self, dc, x, y): """Draw the card on the device context @param dc: device context @param x: abscissa @param y: ordinate""" pass #dc.DrawBitmap(self.bitmap, x, y, True) class CardPanel(DockPanel): def __init__(self, parent, referee, players, player_nick): self._parent = parent self._autoplay = None #XXX: use 0 to activate fake play, None else self.referee = referee self.players = players self.played = {} for player in players: self.played[player] = None self.player_nick = player_nick self.bottom_nick = self.player_nick idx = self.players.index(self.player_nick) idx = (idx + 1) % len(self.players) self.right_nick = self.players[idx] idx = (idx + 1) % len(self.players) self.top_nick = self.players[idx] idx = (idx + 1) % len(self.players) self.left_nick = self.players[idx] self.bottom_nick = player_nick self.selected = [] #Card choosed by the player (e.g. during ecart) self.hand_size = 13 #number of cards in a hand self.hand = [] self.to_show = [] self.state = None DockPanel.__init__(self) self.setSize("%spx" % MIN_WIDTH, "%spx" % MIN_HEIGHT) self.setStyleName("cardPanel") _label = Label(self.top_nick) _label.setStyleName('cardGamePlayerNick') self.add(_label, DockPanel.NORTH) self.setCellWidth(_label, '100%') self.setCellHorizontalAlignment(_label, HasAlignment.ALIGN_CENTER) self.hand_panel = AbsolutePanel() self.add(self.hand_panel, DockPanel.SOUTH) self.setCellWidth(self.hand_panel, '100%') self.setCellHorizontalAlignment(self.hand_panel, HasAlignment.ALIGN_CENTER) _label = Label(self.left_nick) _label.setStyleName('cardGamePlayerNick') self.add(_label, DockPanel.WEST) self.setCellHeight(_label, '100%') self.setCellVerticalAlignment(_label, HasAlignment.ALIGN_MIDDLE) _label = Label(self.right_nick) _label.setStyleName('cardGamePlayerNick') self.add(_label, DockPanel.EAST) self.setCellHeight(_label, '100%') self.setCellHorizontalAlignment(_label, HasAlignment.ALIGN_RIGHT) self.setCellVerticalAlignment(_label, HasAlignment.ALIGN_MIDDLE) self.center_panel = DockPanel() self.add(self.center_panel, DockPanel.CENTER) self.setCellWidth(self.center_panel, '100%') """for side in zip(['left', 'top', 'right'], [DockPanel.WEST, DockPanel.NORTH, DockPanel.EAST]): _nick = getattr(self, "%s_nick" % side[0]) _label = Label(_nick) _label.setStyleName('cardGamePlayerNick') self.add(_label, side[1])""" self.loadCards() self.mouse_over_card = None #contain the card to highlight self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards def loadCards(self, dir): """Load all the cards in memory @param dir: directory where the PNG files are""" def _getTarotCardsPathsCb(paths): for file in paths: card = CardWidget(file) self.cards[(card.suit, card.value)]=card self.deck.append(card) self._parent.host.bridge.call('tarotGameReady', None, self.player_nick, self.referee) self.cards={} self.deck=[] self.cards["atout"]={} #As Tarot is a french game, it's more handy & logical to keep french names self.cards["pique"]={} #spade self.cards["coeur"]={} #heart self.cards["carreau"]={} #diamond self.cards["trefle"]={} #club self._parent.host.bridge.call('getTarotCardsPaths', _getTarotCardsPathsCb) def tarotGameNew(self, hand): """Start a new game, with given hand""" for suit, value in hand: self.hand.append(self.cards[(suit, value)]) self.hand.sort() self.state = "init" self.updateHand() def updateHand(self): """Show the cards in the hand in the hand_panel (SOUTH panel)""" self.hand_panel.clear() self.hand_panel.setSize("%spx" % (self.visible_size * (len(self.hand)+1)), "%spx" % (CARD_HEIGHT + 10)) x_pos = 0 y_pos = 0 for card in self.hand: self.hand_panel.add(card, x_pos, y_pos) x_pos+=self.visible_size def tarotGameChooseContrat(self, xml_data): """Called when the player as to select his contrat @param xml_data: SàT xml representation of the form""" #for the moment we cheat a little bit and make our own dialog box #but XMLUI must be user ASAP, as in other frontends contrat_chooser = ContratChooser(self) contrat_chooser.show() def contratSelected(self, contrat): """Must be called when the contrat is selected @param contrat: one of the valid contrat value""" print "Contrat choosed:", contrat 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