changeset 87:66d784082930

Tarot game - Tarot plugin: game session start, first draft - wix: Tarot: names of players are now printed - wix: Tarot: game session start first draft - wix : Tarot: card can be compared and sorted
author Goffi <goffi@goffi.org>
date Tue, 11 May 2010 23:58:32 +0930 (2010-05-11)
parents 4b5f2d55b6ac
children 59f181e8433a
files frontends/quick_frontend/quick_app.py frontends/wix/card_game.py frontends/wix/chat.py
diffstat 3 files changed, 92 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/quick_frontend/quick_app.py	Tue May 11 13:06:05 2010 +0930
+++ b/frontends/quick_frontend/quick_app.py	Tue May 11 23:58:32 2010 +0930
@@ -47,6 +47,7 @@
         self.bridge.register("roomUserLeft", self.roomUserLeft)
         self.bridge.register("roomNewSubject", self.roomNewSubject)
         self.bridge.register("tarotGameStarted", self.tarotGameStarted)
+        self.bridge.register("tarotGameNew", self.tarotGameNew)
         self.bridge.register("subscribe", self.subscribe)
         self.bridge.register("paramUpdate", self.paramUpdate)
         self.bridge.register("contactDeleted", self.contactDeleted)
@@ -250,15 +251,23 @@
     def tarotGameStarted(self, room_jid, players, profile):
         if not self.__check_profile(profile):
             return
-        print "Tarot Game Started \o/"
+        debug  (_("Tarot Game Started \o/"))
         if self.chat_wins.has_key(room_jid):
-            self.chat_wins[room_jid].startGame("Tarot")
-            debug (_("new Tarot game started in room [%(room_jid)s]") % {'room_jid':room_jid})
+            self.chat_wins[room_jid].startGame("Tarot", players)
+            debug (_("new Tarot game started in room [%(room_jid)s] with %(players)s") % {'room_jid':room_jid, 'players':[str(player) for player in players]})
+       
+    def tarotGameNew(self, room_jid, hand, profile):
+        if not self.__check_profile(profile):
+            return
+        debug (_("New Tarot Game"))
+        print "hand:", hand
+        if self.chat_wins.has_key(room_jid):
+            self.chat_wins[room_jid].getGame("Tarot").newGame(hand)
 
-        
+
 
     def subscribe(self, type, raw_jid, profile):
-        """Called when a subsciption maangement signal is received"""
+        """Called when a subsciption management signal is received"""
         if not self.__check_profile(profile):
             return
         entity = JID(raw_jid)
--- a/frontends/wix/card_game.py	Tue May 11 13:06:05 2010 +0930
+++ b/frontends/wix/card_game.py	Tue May 11 23:58:32 2010 +0930
@@ -23,7 +23,6 @@
 
 import wx
 import os.path, glob
-import random
 import pdb
 from logging import debug, info, error
 from tools.jid  import JID
@@ -35,6 +34,9 @@
 MIN_WIDTH = 950 #Minimum size of the panel
 MIN_HEIGHT = 500
 
+families_order = ['pique', 'coeur', 'trefle', 'carreau', 'atout'] #I have swith the usual order 'trefle' and 'carreau' because card are more easy to see if couleur change (black, red, black, red)
+values_order = map(str,range(1,11))+["valet","cavalier","dame","roi"]
+
 class Card():
     """This class is used to represent a card, graphically and logically"""
 
@@ -47,6 +49,28 @@
 
         print "Carte:",self.family, self.value, self.bout
 
+    def __cmp__(self, other):
+        if other == None:
+            return 1
+        if self.family != other.family:
+            idx1 = families_order.index(self.family)
+            idx2 = families_order.index(other.family)
+            return idx1.__cmp__(idx2)
+        if self.family == 'atout':
+            if self.value == other.value == 'excuse':
+                return 0
+            if self.value == 'excuse':
+                return -1
+            if other.value == 'excuse':
+                return 1
+            return int(self.value).__cmp__(int(other.value))
+        #at this point we have the same family which is not 'atout'
+        idx1 = values_order.index(self.value)
+        idx2 = values_order.index(other.value)
+        return idx1.__cmp__(idx2)
+
+    def __str__(self):
+        return "[%s,%s]" % (self.family, self.value)
 
     def draw(self, dc, x, y):
         """Draw the card on the device context
@@ -59,14 +83,25 @@
 class CardPanel(wx.Panel):
     """This class is used to display the cards"""
 
-    def __init__(self, parent):
+    def __init__(self, parent, players, user):
         wx.Panel.__init__(self, parent)
+        self.players = players
+        self.user = user
+        self.bottom_nick = self.user
+        idx = self.players.index(self.user)
+        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.SetMinSize(wx.Size(MIN_WIDTH, MIN_HEIGHT))
         self.load_cards("/home/goffi/dev/divers/images/cards/")
         self.selected = None #contain the card to highlight
         self.hand_size = 13 #number of cards in a hand
         self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards
-        self.hand = random.sample(self.deck, self.hand_size)
+        self.hand = []
+        self.my_turn = False
         self.SetBackgroundColour(wx.GREEN)
         self.Bind(wx.EVT_SIZE, self.onResize)
         self.Bind(wx.EVT_PAINT, self.onPaint)
@@ -93,6 +128,14 @@
             for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
                 self.idx_cards.append(self.cards[family, value])"""  #XXX: no need to sort the cards !
 
+    def newGame(self, hand):
+        """Start a new game, with given hand"""
+        assert (len(self.hand) == 0)
+        for family, value in hand:
+            self.hand.append(self.cards[family, value])
+        self.hand.sort()
+        self.my_turn = True
+
     def _is_on_hand(self, pos_x, pos_y):
         """Return True if the coordinate are on the hand cards"""
         if pos_x > self.orig_x and pos_y > self.orig_y \
@@ -112,9 +155,24 @@
 
     def onPaint(self, event):
         dc = wx.PaintDC(self)
+        
+        #We print the names to know who play where TODO: print avatars when available
+        max_x, max_y = self.GetSize()
+        border = 10 #border between nick and end of panel
+        right_y = left_y = 200
+        right_width, right_height = dc.GetTextExtent(self.right_nick)
+        right_x = max_x - right_width - border
+        left_x = border
+        top_width, top_height = dc.GetTextExtent(self.top_nick)
+        top_x = (max_x - top_width) / 2
+        top_y = border
+        dc.DrawText(self.right_nick, right_x, right_y)
+        dc.DrawText(self.top_nick, top_x, top_y)
+        dc.DrawText(self.left_nick, left_x, left_y)
+
         x=self.orig_x
         for card in self.hand:
-            card.draw(dc,x,self.orig_y - 30 if card == self.selected else self.orig_y)
+            card.draw(dc,x,self.orig_y - 30 if self.my_turn and card == self.selected else self.orig_y)
             x+=self.visible_size
 
     def onMouseMove(self, event):
--- a/frontends/wix/chat.py	Tue May 11 13:06:05 2010 +0930
+++ b/frontends/wix/chat.py	Tue May 11 23:58:32 2010 +0930
@@ -108,15 +108,21 @@
             self.__createMenus_O2O()
             self.historyPrint(profile=self.host.profile)
     
-    def startGame(self, game_type):
+    def startGame(self, game_type, players):
         """Configure the chat window to start a game"""
         if game_type=="Tarot":
             debug (_("configure chat window for Tarot game"))
-            tarot_panel = CardPanel(self)
-            self.sizer.Prepend(tarot_panel, 0, flag=wx.EXPAND)
+            self.tarot_panel = CardPanel(self, players, self.nick)
+            self.sizer.Prepend(self.tarot_panel, 0, flag=wx.EXPAND)
             self.sizer.Layout()
             self.Fit()
 
+    def getGame(self, game_type):
+        """Return class managing the game type"""
+        #TODO: check that the game is launched, and manage errors
+        if game_type=="Tarot":
+            return self.tarot_panel 
+
 
     def setPresents(self, nicks):
         """Set the users presents in the contact list for a group chat
@@ -128,8 +134,7 @@
             return
         for nick in nicks:
             self.present_panel.presents.replace(nick)
-            if nick != self.nick:
-                self.occupants.add(nick)
+            self.occupants.add(nick)
     
     
     def replaceUser(self, nick):
@@ -139,8 +144,7 @@
             error (_("[INTERNAL] trying to replace user for a non group chat window"))
             return
         self.present_panel.presents.replace(nick)
-        if nick != self.nick:
-            self.occupants.add(nick)
+        self.occupants.add(nick)
       
     def removeUser(self, nick):
         """Remove a user from the group list"""
@@ -240,4 +244,8 @@
     def onStartTarot(self, e):
         debug (_("Starting Tarot game"))
         warning (_("FIXME: temporary menu, must be changed"))
-        self.host.bridge.createTarotGame(self.id, list(self.occupants), self.host.profile)
+        if len(self.occupants) != 4:
+            err_dlg = wx.MessageDialog(self, _("You need to be exactly 4 peoples in the room to start a Tarot game"), _("Can't start game"), style = wx.OK | wx.ICON_ERROR) #FIXME: gof: temporary only, need to choose the people with who the game has to be started
+            err_dlg.ShowModal()
+        else:
+            self.host.bridge.createTarotGame(self.id, list(self.occupants), self.host.profile)