annotate browser_side/card_game.py @ 36:1d406077b49b

Tarot Game: first draft
author Goffi <goffi@goffi.org>
date Tue, 17 May 2011 01:33:12 +0200
parents
children b306aa090438
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Libervia: a Salut à Toi frontend
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU Affero General Public License as published by
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU Affero General Public License for more details.
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU Affero General Public License
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import pyjd # this is dummy in pyjs
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from pyjamas.ui.AbsolutePanel import AbsolutePanel
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from pyjamas.ui.Image import Image
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from pyjamas.dnd import makeDraggable
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from pyjamas.ui.DragWidget import DragWidget, DragContainer
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from jid import JID
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from tools import html_sanitize
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from datetime import datetime
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from time import time
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from games import TarotCard
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 CARD_WIDTH = 74
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 CARD_HEIGHT = 136
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 MIN_WIDTH = 950 #Minimum size of the panel
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 MIN_HEIGHT = 500
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 class CardWidget(TarotCard, Image):
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 """This class is used to represent a card, graphically and logically"""
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 def __init__(self, file):
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 """@param file: path of the PNG file"""
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 Image.__init__(self,file)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 root_name = file[file.rfind("/")+1:-4]
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 suit,value = root_name.split('_')
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 TarotCard.__init__(self, (suit, value))
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 print "Carte:",suit, value #, self.bout
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 def draw(self, dc, x, y):
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 """Draw the card on the device context
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 @param dc: device context
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 @param x: abscissa
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 @param y: ordinate"""
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 pass
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 #dc.DrawBitmap(self.bitmap, x, y, True)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 class CardPanel(AbsolutePanel):
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 def __init__(self, parent, referee, players, player_nick):
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self._parent = parent
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 self._autoplay = None #XXX: use 0 to activate fake play, None else
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.referee = referee
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.players = players
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self.played = {}
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 for player in players:
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 self.played[player] = None
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 self.player_nick = player_nick
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 self.bottom_nick = self.player_nick
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 idx = self.players.index(self.player_nick)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74 idx = (idx + 1) % len(self.players)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self.right_nick = self.players[idx]
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76 idx = (idx + 1) % len(self.players)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self.top_nick = self.players[idx]
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 idx = (idx + 1) % len(self.players)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 self.left_nick = self.players[idx]
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80 self.bottom_nick = player_nick
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81 self.selected = [] #Card choosed by the player (e.g. during ecart)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 self.hand_size = 13 #number of cards in a hand
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 self.hand = []
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84 self.to_show = []
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 self.state = None
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 AbsolutePanel.__init__(self)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 self.setSize("%spx" % MIN_WIDTH, "%spx" % MIN_HEIGHT)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 self.setStyleName("cardPanel")
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89 self.loadCards()
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 self.mouse_over_card = None #contain the card to highlight
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91 self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93 def _getTarotCardsPathsCb(self, paths):
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94 for file in paths:
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95 card = CardWidget(file)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 self.cards[(card.suit, card.value)]=card
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97 self.deck.append(card)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
99 def loadCards(self, dir):
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100 """Load all the cards in memory
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101 @param dir: directory where the PNG files are"""
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102 self.cards={}
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
103 self.deck=[]
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104 self.cards["atout"]={} #As Tarot is a french game, it's more handy & logical to keep french names
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
105 self.cards["pique"]={} #spade
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
106 self.cards["coeur"]={} #heart
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
107 self.cards["carreau"]={} #diamond
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
108 self.cards["trefle"]={} #club
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109 self._parent.host.bridge.call('getTarotCardsPaths', self._getTarotCardsPathsCb)
1d406077b49b Tarot Game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110