diff frontends/primitivus/card_game.py @ 144:80661755ea8d

Primitivus: Tarot card game implementation - quick frontend: card_game added - wix: card_game splitted with quick frontend - tools: new game library - primitivus: new card_game widget (not finished yet) - primitivus: SàT XML UI management: first draft
author Goffi <goffi@goffi.org>
date Mon, 26 Jul 2010 19:43:44 +0800
parents
children 63d20bda5754
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/frontends/primitivus/card_game.py	Mon Jul 26 19:43:44 2010 +0800
@@ -0,0 +1,149 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+"""
+Primitivus: a SAT frontend
+Copyright (C) 2009, 2010  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 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 General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+"""
+
+import urwid
+from tools.games import TarotCard
+from quick_frontend.quick_card_game import QuickCardGame
+from xmlui import XMLUI
+
+class Hand(urwid.WidgetWrap):
+    """Used to display several cards, and manage a hand"""
+
+    def __init__(self):
+        self.columns = urwid.Columns([])
+        urwid.WidgetWrap.__init__(self, self.columns)
+
+    def update(self, hand):
+        """Update the hand displayed in this widget"""
+        del self.columns.widget_list[:]
+        del self.columns.column_types[:]
+        for card in hand:
+            self.columns.widget_list.append(urwid.Text(card.getAttrText()))
+            self.columns.column_types.append(('weight',1))
+            
+
+        
+
+class Card(TarotCard):
+    """This class is used to represent a card, logically
+    and give a text representation with attributes"""
+
+    def __init__(self, suit, value):
+        """@param file: path of the PNG file"""
+        TarotCard.__init__(self, (suit, value))
+
+    def getAttrText(self):
+        """return text representation of the card with attributes"""
+        try:
+            value = "%02i" % int(self.value)
+        except ValueError:
+            value = self.value[0].upper()+self.value[1]
+        if self.suit == "atout":
+            if self.value == "excuse":
+                suit = 'c'
+            else:
+                suit = 'A'
+            color = 'neutral'
+        elif self.suit == "pique":
+            suit = u'♠'
+            color = 'black'
+        elif self.suit == "trefle":
+            suit = u'♣'
+            color = 'black'
+        elif self.suit == "coeur":
+            suit = u'♥'
+            color = 'red'
+        elif self.suit == "carreau":
+            suit = u'♦'
+            color = 'red'
+        if self.bout:
+            color = 'special'
+        return ('card_%s' % color,u"%s%s" % (value,suit))
+
+class CardGame(QuickCardGame,urwid.WidgetWrap):
+    """Widget for card games"""
+    
+    def __init__(self, parent, referee, players, player_nick):
+        QuickCardGame.__init__(self, parent, referee, players, player_nick)
+        self.loadCards()
+        self.top = urwid.Pile([urwid.Padding(urwid.Text(self.top_nick), 'center')])
+        self.top_card_wid = urwid.Text('') 
+        self.center_cards_wid = urwid.Text(' - ') 
+        self.bottom_card_wid = urwid.Text('') 
+        center = urwid.Pile([urwid.Padding(self.top_card_wid,'center'),
+                             urwid.Columns([('fixed',len(self.left_nick),urwid.Text(self.left_nick)),
+                                            urwid.Padding(self.center_cards_wid,'center'),
+                                            ('fixed',len(self.right_nick),urwid.Text(self.right_nick))
+                                           ]),
+                             urwid.Padding(self.bottom_card_wid,'center')
+                             ])
+        body = urwid.Filler(center)
+        self.hand_wid = Hand()
+        self.main_frame = urwid.Frame(body,header=self.top, footer=self.hand_wid)
+        urwid.WidgetWrap.__init__(self,self.main_frame)
+        self.parent.host.bridge.tarotGameReady(player_nick, referee, profile_key = self.parent.host.profile)
+
+    def loadCards(self):
+        """Load all the cards in memory"""
+        QuickCardGame.loadCards(self)
+        for value in map(str,range(1,22))+['excuse']:
+            card = Card('atout',value)
+            self.cards[card.suit, card.value]=card
+            self.deck.append(card)
+        for suit in ["pique", "coeur", "carreau", "trefle"]:
+            for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
+                card = Card(suit,value)
+                self.cards[card.suit, card.value]=card
+                self.deck.append(card)
+
+    def newGame(self, hand):
+        """Start a new game, with given hand"""
+        QuickCardGame.newGame(self, hand)
+        self.hand_wid.update(self.hand)
+        self.parent.host.redraw()
+    
+    def contratSelected(self, data):
+        """Called when the contrat has been choosed
+        @param data: form result"""
+        contrat = data[0][1]
+        QuickCardGame.contratSelected(self, contrat)
+    
+    def chooseContrat(self, xml_data):
+        """Called when the player as to select his contrat
+        @param xml_data: SàT xml representation of the form"""
+        misc = {'callback': self.contratSelected}
+        form = XMLUI(self.parent.host, xml_data, title = _('Please choose your contrat'), options = ['NO_CANCEL'], misc = misc)
+        form.show()
+
+    """def selectable(self):
+        return True
+    
+    def keypress(self, size, key):
+        return key
+        
+    def render(self, size, focus=False):
+        return self.display_widget(size, focus).render(size, focus)
+
+    def display_widget(self, size, focus):
+        (maxcol,maxrow) = size
+
+        return self.main_frame"""
+