annotate plugins/plugin_misc_tarot.py @ 98:dd556233a1b1

Tarot Plugin: Garde Sans and Garde Contre are now managed
author Goffi <goffi@goffi.org>
date Thu, 03 Jun 2010 17:43:49 +0930
parents c8518b9a8025
children 63c9067a1499
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
88
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
Goffi <goffi@goffi.org>
parents:
diff changeset
3
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
Goffi <goffi@goffi.org>
parents:
diff changeset
5 SAT plugin for managing xep-0045
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
Goffi <goffi@goffi.org>
parents:
diff changeset
7
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
Goffi <goffi@goffi.org>
parents:
diff changeset
12
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
Goffi <goffi@goffi.org>
parents:
diff changeset
17
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
Goffi <goffi@goffi.org>
parents:
diff changeset
21
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from logging import debug, info, warning, error
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.xish import domish
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.internet import protocol, defer, threads, reactor
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import client, jid, xmlstream
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber import error as jab_error
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.words.protocols.jabber.xmlstream import IQ
Goffi <goffi@goffi.org>
parents:
diff changeset
28 import os.path
Goffi <goffi@goffi.org>
parents:
diff changeset
29 import pdb
Goffi <goffi@goffi.org>
parents:
diff changeset
30 import random
Goffi <goffi@goffi.org>
parents:
diff changeset
31
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from zope.interface import implements
Goffi <goffi@goffi.org>
parents:
diff changeset
33
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
34 from wokkel import disco, iwokkel, data_form
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
35 from tools.xml_tools import XMLTools
88
Goffi <goffi@goffi.org>
parents:
diff changeset
36
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from base64 import b64decode
Goffi <goffi@goffi.org>
parents:
diff changeset
38 from hashlib import sha1
Goffi <goffi@goffi.org>
parents:
diff changeset
39 from time import sleep
Goffi <goffi@goffi.org>
parents:
diff changeset
40
Goffi <goffi@goffi.org>
parents:
diff changeset
41 try:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 from twisted.words.protocols.xmlstream import XMPPHandler
Goffi <goffi@goffi.org>
parents:
diff changeset
43 except ImportError:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 from wokkel.subprotocols import XMPPHandler
Goffi <goffi@goffi.org>
parents:
diff changeset
45
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
46 MESSAGE = '/message'
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
47 NS_CG = 'http://www.goffi.org/protocol/card_game'
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
48 CG_TAG = 'card_game'
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
49 CG_REQUEST = MESSAGE + '/' + CG_TAG + '[@xmlns="' + NS_CG + '"]'
88
Goffi <goffi@goffi.org>
parents:
diff changeset
50
Goffi <goffi@goffi.org>
parents:
diff changeset
51 PLUGIN_INFO = {
Goffi <goffi@goffi.org>
parents:
diff changeset
52 "name": "Tarot cards plugin",
Goffi <goffi@goffi.org>
parents:
diff changeset
53 "import_name": "Tarot",
Goffi <goffi@goffi.org>
parents:
diff changeset
54 "type": "Misc",
Goffi <goffi@goffi.org>
parents:
diff changeset
55 "protocols": [],
Goffi <goffi@goffi.org>
parents:
diff changeset
56 "dependencies": ["XEP_0045"],
Goffi <goffi@goffi.org>
parents:
diff changeset
57 "main": "Tarot",
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
58 "handler": "yes",
88
Goffi <goffi@goffi.org>
parents:
diff changeset
59 "description": _("""Implementation of Tarot card game""")
Goffi <goffi@goffi.org>
parents:
diff changeset
60 }
Goffi <goffi@goffi.org>
parents:
diff changeset
61
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
62 suits_order = ['pique', 'coeur', 'trefle', 'carreau', 'atout'] #I have swith the usual order 'trefle' and 'carreau' because card are more easy to see if suit colour change (black, red, black, red)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
63 values_order = map(str,range(1,11))+["valet","cavalier","dame","roi"]
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
64
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
65 class Card():
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
66 """This class is used to represent a car logically"""
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
67 #TODO: move this in a library in tools, and share this with frontends (e.g. card_game in wix use the same class)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
68
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
69 def __init__(self, tuple_card):
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
70 """@param tuple_card: tuple (suit, value)"""
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
71 self.suit, self.value = tuple_card
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
72 self.bout = True if self.suit=="atout" and self.value in ["1","21","excuse"] else False
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
73 if self.bout or self.value == "roi":
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
74 self.points = 4.5
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
75 elif self.value == "dame":
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
76 self.points = 3.5
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
77 elif self.value == "cavalier":
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
78 self.points = 2.5
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
79 elif self.value == "valet":
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
80 self.points = 1.5
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
81 else:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
82 self.points = 0.5
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
83
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
84 def get_tuple(self):
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
85 return (self.suit,self.value)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
86
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
87 @staticmethod
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
88 def from_tuples(tuple_list):
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
89 result = []
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
90 for card_tuple in tuple_list:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
91 result.append(Card(card_tuple))
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
92 return result
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
93
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
94 def __cmp__(self, other):
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
95 if other == None:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
96 return 1
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
97 if self.suit != other.suit:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
98 idx1 = suits_order.index(self.suit)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
99 idx2 = suits_order.index(other.suit)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
100 return idx1.__cmp__(idx2)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
101 if self.suit == 'atout':
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
102 if self.value == other.value == 'excuse':
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
103 return 0
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
104 if self.value == 'excuse':
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
105 return -1
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
106 if other.value == 'excuse':
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
107 return 1
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
108 return int(self.value).__cmp__(int(other.value))
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
109 #at this point we have the same suit which is not 'atout'
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
110 idx1 = values_order.index(self.value)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
111 idx2 = values_order.index(other.value)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
112 return idx1.__cmp__(idx2)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
113
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
114 def __str__(self):
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
115 return "[%s,%s]" % (self.suit, self.value)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
116
88
Goffi <goffi@goffi.org>
parents:
diff changeset
117 class Tarot():
Goffi <goffi@goffi.org>
parents:
diff changeset
118
Goffi <goffi@goffi.org>
parents:
diff changeset
119 def __init__(self, host):
Goffi <goffi@goffi.org>
parents:
diff changeset
120 info(_("Plugin Tarot initialization"))
Goffi <goffi@goffi.org>
parents:
diff changeset
121 self.host = host
Goffi <goffi@goffi.org>
parents:
diff changeset
122 self.games={}
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
123 self.contrats = [_('Passe'), _('Petite'), _('Garde'), _('Garde Sans'), _('Garde Contre')]
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
124 host.bridge.addMethod("tarotGameCreate", ".communication", in_sign='sass', out_sign='', method=self.createGame) #args: room_jid, players, profile
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
125 host.bridge.addMethod("tarotGameReady", ".communication", in_sign='sss', out_sign='', method=self.newPlayerReady) #args: player, referee, profile
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
126 host.bridge.addMethod("tarotGameContratChoosed", ".communication", in_sign='ssss', out_sign='', method=self.contratChoosed) #args: player, referee, contrat, profile
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
127 host.bridge.addMethod("tarotGamePlayCards", ".communication", in_sign='ssa(ss)s', out_sign='', method=self.play_cards) #args: player, referee, cards, profile
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
128 host.bridge.addSignal("tarotGameStarted", ".communication", signature='ssass') #args: room_jid, referee, players, profile
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
129 host.bridge.addSignal("tarotGameNew", ".communication", signature='sa(ss)s') #args: room_jid, hand, profile
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
130 host.bridge.addSignal("tarotGameChooseContrat", ".communication", signature='sss') #args: room_jid, xml_data, profile
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
131 host.bridge.addSignal("tarotGameShowCards", ".communication", signature='ssa(ss)a{ss}s') #args: room_jid, type ["chien", "poignée",...], cards, data[dict], profile
93
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
132 host.bridge.addSignal("tarotGameCardsPlayed", ".communication", signature='ssa(ss)s') #args: room_jid, player, type ["chien", "poignée",...], cards, data[dict], profile
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
133 host.bridge.addSignal("tarotGameYourTurn", ".communication", signature='ss') #args: room_jid, profile
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
134 host.bridge.addSignal("tarotGameScore", ".communication", signature='ssasass') #args: room_jid, xml_data, winners (list of nicks), loosers (list of nicks), profile
88
Goffi <goffi@goffi.org>
parents:
diff changeset
135 self.deck_ordered = []
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
136 for value in ['excuse']+map(str,range(1,22)):
98
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
137 self.deck_ordered.append(Card(("atout",value)))
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
138 for suit in ["pique", "coeur", "carreau", "trefle"]:
88
Goffi <goffi@goffi.org>
parents:
diff changeset
139 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
98
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
140 self.deck_ordered.append(Card((suit, value)))
88
Goffi <goffi@goffi.org>
parents:
diff changeset
141
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
142 def createGameElt(self, to_jid, type="normal"):
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
143 type = "normal" if to_jid.resource else "groupchat"
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
144 elt = domish.Element(('jabber:client','message'))
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
145 elt["to"] = to_jid.full()
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
146 elt["type"] = type
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
147 elt.addElement((NS_CG, CG_TAG))
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
148 return elt
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
149
98
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
150 def __card_list_to_xml(self, cards_list, elt_name):
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
151 """Convert a card list to domish element"""
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
152 cards_list_elt = domish.Element(('',elt_name))
98
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
153 for card in cards_list:
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
154 card_elt = domish.Element(('','card'))
98
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
155 card_elt['suit'] = card.suit
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
156 card_elt['value'] = card.value
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
157 cards_list_elt.addChild(card_elt)
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
158 return cards_list_elt
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
159
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
160 def __xml_to_list(self, cards_list_elt):
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
161 """Convert a domish element with cards to a list of tuples"""
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
162 cards_list = []
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
163 for card in cards_list_elt.elements():
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
164 cards_list.append((card['suit'], card['value']))
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
165 return cards_list
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
166
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
167 def __create_started_elt(self, players):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
168 """Create a game_started domish element"""
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
169 started_elt = domish.Element(('','started'))
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
170 idx = 0
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
171 for player in players:
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
172 player_elt = domish.Element(('','player'))
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
173 player_elt.addContent(player)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
174 player_elt['index'] = str(idx)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
175 idx+=1
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
176 started_elt.addChild(player_elt)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
177 return started_elt
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
178
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
179 def __ask_contrat(self):
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
180 """Create a element for asking contrat"""
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
181 contrat_elt = domish.Element(('','contrat'))
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
182 form = data_form.Form('form', title=_('contrat selection'))
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
183 field = data_form.Field('list-single', 'contrat', options=map(data_form.Option, self.contrats), required=True)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
184 form.addField(field)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
185 contrat_elt.addChild(form.toElement())
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
186 return contrat_elt
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
187
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
188 def __give_scores(self, scores, winners, loosers):
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
189 """Create an element to give scores
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
190 @param scores: unicode (can contain line feed)
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
191 @param winners: list of unicode nicks of winners
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
192 @param loosers: list of unicode nicks of loosers"""
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
193
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
194 score_elt = domish.Element(('','score'))
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
195 form = data_form.Form('form', title=_('scores'))
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
196 for line in scores.split('\n'):
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
197 field = data_form.Field('fixed', value = line)
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
198 form.addField(field)
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
199 score_elt.addChild(form.toElement())
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
200 for winner in winners:
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
201 winner_elt = domish.Element(('','winner'))
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
202 winner_elt.addContent(winner)
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
203 score_elt.addChild(winner_elt)
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
204 for looser in loosers:
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
205 looser_elt = domish.Element(('','looser'))
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
206 looser_elt.addContent(looser)
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
207 score_elt.addChild(looser_elt)
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
208 return score_elt
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
209
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
210 def __next_player(self, game_data, next_pl = None):
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
211 """Increment player number & return player name
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
212 @param next_pl: if given, then next_player is forced to this one
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
213 """
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
214 if next_pl:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
215 game_data['current_player'] = game_data['players'].index(next_pl)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
216 return next_pl
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
217 else:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
218 pl_idx = game_data['current_player'] = (game_data['current_player'] + 1) % len(game_data['players'])
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
219 return game_data['players'][pl_idx]
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
220
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
221 def __winner(self, game_data):
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
222 """give the nick of the player who win this trick"""
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
223 players_data = game_data['players_data']
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
224 first = game_data['first_player']
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
225 first_idx = game_data['players'].index(first)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
226 suit_asked = None
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
227 strongest = None
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
228 winner = None
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
229 for idx in [(first_idx + i) % 4 for i in range(4)]:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
230 player = game_data['players'][idx]
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
231 card = players_data[player]['played']
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
232 if card.value == "excuse":
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
233 continue
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
234 if suit_asked == None:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
235 suit_asked = card.suit
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
236 if (card.suit == suit_asked or card.suit == "atout") and card > strongest:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
237 strongest = card
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
238 winner = player
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
239 assert winner
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
240 return winner
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
241
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
242 def __excuse_hack(self, game_data, played, winner):
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
243 """give a low card to other team and keep excuse if trick is lost
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
244 @param game_data: data of the game
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
245 @param played: cards currently on the table
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
246 @param winner: nick of the trick winner"""
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
247 #TODO: manage the case where excuse is played on the last trick (and lost)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
248 #TODO: gof: manage excuse (fool)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
249 players_data = game_data['players_data']
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
250 excuse = Card(("atout","excuse"))
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
251
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
252 #we first check if the Excuse was already player
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
253 #and if somebody is waiting for a card
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
254 for player in game_data['players']:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
255 if players_data[player]['wait_for_low']:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
256 #the excuse owner has to give a card to somebody
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
257 if winner == player:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
258 #the excuse owner win the trick, we check if we have something to give
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
259 for card in played:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
260 if card.points == 0.5:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
261 pl_waiting = players_data[player]['wait_for_low']
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
262 played.remove(card)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
263 players_data[pl_waiting]['levees'].append(card)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
264 debug (_('Player %(excuse_owner)s give %(card_waited)s to %(player_waiting)s for Excuse compensation') % {"excuse_owner":player, "card_waited": card, "player_waiting":pl_waiting})
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
265 return
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
266 return
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
267
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
268 if not excuse in played:
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
269 #the Excuse is not on the table, nothing to do
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
270 return
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
271
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
272 excuse_player = None #Who has played the Excuse ?
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
273 for player in game_data['players']:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
274 if players_data[player]['played'] == excuse:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
275 excuse_player = player
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
276 break
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
277
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
278 if excuse_player == winner:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
279 return #the excuse player win the trick, nothing to do
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
280
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
281 #first we remove the excuse from played cards
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
282 played.remove(excuse)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
283 #then we give it back to the original owner
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
284 owner_levees = players_data[excuse_player]['levees']
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
285 owner_levees.append(excuse)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
286 #finally we give a low card to the trick winner
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
287 low_card = None
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
288 #We look backward in cards won by the Excuse owner to
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
289 #find a low value card
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
290 for card_idx in range(len(owner_levees)-1, -1, -1):
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
291 if owner_levees[card_idx].points == 0.5:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
292 low_card = owner_levees[card_idx]
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
293 del owner_levees[card_idx]
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
294 players_data[winner]['levees'].append(low_card)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
295 debug (_('Player %(excuse_owner)s give %(card_waited)s to %(player_waiting)s for Excuse compensation') % {"excuse_owner":excuse_player, "card_waited": low_card, "player_waiting":winner})
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
296 break
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
297 if not low_card: #The player has no low card yet
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
298 #TODO: manage case when player never win a trick with low card
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
299 players_data[excuse_player]['wait_for_low'] = winner
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
300 debug(_("%(excuse_owner)s keep the Excuse but has not card to give, %(winner)s is waiting for one") % {'excuse_owner':excuse_player, 'winner':winner})
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
301
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
302
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
303 def __calculate_scores(self, game_data):
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
304 """The game is finished, time to know who won :)
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
305 @param game_data: data of the game
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
306 @return: tuple with (string victory message, list of winners, list of loosers)"""
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
307 players_data = game_data['players_data']
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
308 levees = players_data[game_data['attaquant']]['levees']
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
309 score = 0
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
310 nb_bouts = 0
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
311 bouts = []
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
312 for card in levees:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
313 if card.bout:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
314 nb_bouts +=1
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
315 bouts.append(card.value)
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
316 score += card.points
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
317
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
318 #We now check if there is no bug in score calculation
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
319 check_score = 0
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
320 defenseurs = game_data['players'][:]
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
321 defenseurs.remove(game_data['attaquant'])
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
322 for defenseur in defenseurs:
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
323 for card in players_data[defenseur]['levees']:
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
324 check_score+=card.points
98
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
325 if game_data['contrat'] == "Garde Contre":
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
326 for card in game_data['chien']:
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
327 check_score+=card.points
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
328 if ( score + check_score != 91 ):
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
329 pdb.set_trace()
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
330 assert (score + check_score == 91)
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
331
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
332 point_limit = None
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
333 if nb_bouts == 3:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
334 point_limit = 36
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
335 elif nb_bouts == 2:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
336 point_limit = 41
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
337 elif nb_bouts == 1:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
338 point_limit = 51
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
339 else:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
340 point_limit = 56
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
341 if game_data['contrat'] == 'Petite':
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
342 contrat_mult = 1
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
343 elif game_data['contrat'] == 'Garde':
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
344 contrat_mult = 2
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
345 elif game_data['contrat'] == 'Garde Sans':
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
346 contrat_mult = 4
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
347 elif game_data['contrat'] == 'Garde Contre':
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
348 contrat_mult = 6
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
349 else:
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
350 error(_('Internal error: contrat not managed (mispelled ?)'))
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
351
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
352 victory = (score >= point_limit)
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
353 margin = score - point_limit
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
354 points_defenseur = (-margin + 25) * contrat_mult
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
355 winners = []
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
356 loosers = []
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
357 player_score = {}
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
358 for player in game_data['players']:
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
359 #TODO: adjust this for 3 and 5 players variants
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
360 #TODO: manage bonuses (petit au bout, poignée, chelem)
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
361 player_score[player] = points_defenseur if player != game_data['attaquant'] else points_defenseur * -3
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
362 players_data[player]['score'] += player_score[player] #we add score of this game to the global score
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
363 if player_score[player] > 0:
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
364 winners.append(player)
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
365 else:
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
366 loosers.append(player)
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
367
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
368 scores_str = _('The attacker (%(attaquant)s) makes %(points)i and needs to make %(point_limit)i (%(nb_bouts)s oulder%(plural)s: %(bouts)s): he %(victory)s') % {'attaquant':game_data['attaquant'], 'points':score, 'point_limit':point_limit, 'nb_bouts': nb_bouts, 'plural': 's' if nb_bouts>1 else '', 'bouts':','.join(map(str,bouts)), 'victory': 'win' if victory else 'loose'}
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
369 scores_str+='\n'
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
370 for player in game_data['players']:
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
371 scores_str+=_("\n--\n%(player)s:\nscore for this game ==> %(score_game)i\ntotal score ==> %(total_score)i") % {'player':player, 'score_game':player_score[player], 'total_score': players_data[player]['score']}
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
372 debug(scores_str)
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
373
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
374 return (scores_str, winners, loosers)
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
375
98
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
376 def __start_play(self, room_jid, game_data, profile):
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
377 """Start the game (tell to the first player after dealer to play"""
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
378 game_data['stage'] = "play"
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
379 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) #the player after the dealer start
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
380 game_data['first_player'] = next_player = game_data['players'][next_player_idx]
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
381 to_jid = jid.JID(room_jid.userhost()+"/"+next_player) #FIXME: gof:
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
382 mess = self.createGameElt(to_jid)
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
383 yourturn_elt = mess.firstChildElement().addElement('your_turn')
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
384 self.host.profiles[profile].xmlstream.send(mess)
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
385
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
386
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
387 def createGame(self, room_jid_param, players, profile_key='@DEFAULT@'):
88
Goffi <goffi@goffi.org>
parents:
diff changeset
388 """Create a new game"""
Goffi <goffi@goffi.org>
parents:
diff changeset
389 debug (_("Creating Tarot game"))
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
390 room_jid = jid.JID(room_jid_param)
88
Goffi <goffi@goffi.org>
parents:
diff changeset
391 profile = self.host.memory.getProfileName(profile_key)
Goffi <goffi@goffi.org>
parents:
diff changeset
392 if not profile:
Goffi <goffi@goffi.org>
parents:
diff changeset
393 error (_("profile %s is unknown") % profile_key)
Goffi <goffi@goffi.org>
parents:
diff changeset
394 return
Goffi <goffi@goffi.org>
parents:
diff changeset
395 if False: #gof: self.games.has_key(room_jid):
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
396 warning (_("Tarot game already started in room %s") % room_jid.userhost())
88
Goffi <goffi@goffi.org>
parents:
diff changeset
397 else:
93
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
398 room_nick = self.host.plugins["XEP_0045"].getRoomNick(room_jid.userhost(), profile)
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
399 if not room_nick:
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
400 error ('Internal error')
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
401 return
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
402 referee = room_jid.userhost() + '/' + room_nick
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
403 status = {}
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
404 players_data = {}
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
405 for player in players:
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
406 players_data[player] = {'score':0}
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
407 status[player] = "init"
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
408 self.games[room_jid.userhost()] = {'referee':referee, 'players':players, 'status':status, 'players_data':players_data, 'hand_size':18, 'init_player':0, 'current_player': None, 'contrat': None, 'stage': None}
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
409 for player in players:
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
410 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player))
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
411 mess.firstChildElement().addChild(self.__create_started_elt(players))
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
412 self.host.profiles[profile].xmlstream.send(mess)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
413
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
414 def newPlayerReady(self, player, referee, profile_key='@DEFAULT@'):
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
415 """Must be called when player is ready to start a new game"""
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
416 profile = self.host.memory.getProfileName(profile_key)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
417 if not profile:
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
418 error (_("profile %s is unknown") % profile_key)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
419 return
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
420 debug ('new player ready: %s' % profile)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
421 mess = self.createGameElt(jid.JID(referee))
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
422 ready_elt = mess.firstChildElement().addElement('player_ready')
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
423 ready_elt['player'] = player
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
424 self.host.profiles[profile].xmlstream.send(mess)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
425
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
426 def contratChoosed(self, player, referee, contrat, profile_key='@DEFAULT@'):
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
427 """Must be call by player when the contrat is selected
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
428 @param player: player's name
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
429 @param referee: arbiter jid
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
430 @contrat: contrat choosed (must be the exact same string than in the give list options)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
431 @profile_key: profile
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
432 """
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
433 profile = self.host.memory.getProfileName(profile_key)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
434 if not profile:
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
435 error (_("profile %s is unknown") % profile_key)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
436 return
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
437 debug (_('contrat [%(contrat)s] choosed by %(profile)s') % {'contrat':contrat, 'profile':profile})
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
438 mess = self.createGameElt(jid.JID(referee))
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
439 contrat_elt = mess.firstChildElement().addElement(('','contrat_choosed'), content=contrat)
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
440 contrat_elt['player'] = player
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
441 self.host.profiles[profile].xmlstream.send(mess)
88
Goffi <goffi@goffi.org>
parents:
diff changeset
442
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
443 def play_cards(self, player, referee, cards, profile_key='@DEFAULT@'):
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
444 """Must be call by player when the contrat is selected
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
445 @param player: player's name
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
446 @param referee: arbiter jid
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
447 @cards: cards played (list of tuples)
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
448 @profile_key: profile
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
449 """
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
450 profile = self.host.memory.getProfileName(profile_key)
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
451 if not profile:
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
452 error (_("profile %s is unknown") % profile_key)
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
453 return
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
454 debug (_('Cards played by %(profile)s: [%(cards)s]') % {'profile':profile,'cards':cards})
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
455 mess = self.createGameElt(jid.JID(referee))
98
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
456 playcard_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(Card.from_tuples(cards), 'cards_played'))
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
457 playcard_elt['player'] = player
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
458 self.host.profiles[profile].xmlstream.send(mess)
88
Goffi <goffi@goffi.org>
parents:
diff changeset
459
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
460 def newGame(self, room_jid, profile):
88
Goffi <goffi@goffi.org>
parents:
diff changeset
461 """Launch a new round"""
Goffi <goffi@goffi.org>
parents:
diff changeset
462 debug (_('new Tarot game'))
Goffi <goffi@goffi.org>
parents:
diff changeset
463 deck = self.deck_ordered[:]
Goffi <goffi@goffi.org>
parents:
diff changeset
464 random.shuffle(deck)
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
465 game_data = self.games[room_jid.userhost()]
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
466 players = game_data['players']
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
467 players_data = game_data['players_data']
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
468 current_player = game_data['current_player']
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
469 game_data['stage'] = "init"
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
470 game_data['first_player'] = None #first player for the current trick
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
471 game_data['contrat'] = None
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
472 hand = game_data['hand'] = {}
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
473 hand_size = game_data['hand_size']
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
474 chien = game_data['chien'] = []
88
Goffi <goffi@goffi.org>
parents:
diff changeset
475 for i in range(4): #TODO: distribute according to real Tarot rules (3 by 3 counter-clockwise, 1 card at once to chien)
Goffi <goffi@goffi.org>
parents:
diff changeset
476 hand[players[i]] = deck[0:hand_size]
Goffi <goffi@goffi.org>
parents:
diff changeset
477 del deck[0:hand_size]
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
478 chien.extend(deck)
88
Goffi <goffi@goffi.org>
parents:
diff changeset
479 del(deck[:])
Goffi <goffi@goffi.org>
parents:
diff changeset
480
Goffi <goffi@goffi.org>
parents:
diff changeset
481 for player in players:
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
482 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof:
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
483 mess = self.createGameElt(to_jid)
98
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
484 mess.firstChildElement().addChild(self.__card_list_to_xml(hand[player], 'hand'))
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
485 self.host.profiles[profile].xmlstream.send(mess)
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
486 players_data[player]['contrat'] = None
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
487 players_data[player]['levees'] = [] #cards won
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
488 players_data[player]['played'] = None #card on the table
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
489 players_data[player]['wait_for_low'] = None #Used when a player wait for a low card because of excuse
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
490
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
491 pl_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(players) #the player after the dealer start
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
492 player = players[pl_idx]
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
493 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof:
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
494 mess = self.createGameElt(to_jid)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
495 mess.firstChildElement().addChild(self.__ask_contrat())
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
496 self.host.profiles[profile].xmlstream.send(mess)
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
497
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
498
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
499 def card_game_cmd(self, mess_elt, profile):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
500 print "\n\nCARD GAME command received (profile=%s): %s" % (profile, mess_elt.toXml())
93
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
501 from_jid = jid.JID(mess_elt['from'])
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
502 room_jid = jid.JID(from_jid.userhost())
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
503 game_elt = mess_elt.firstChildElement()
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
504 game_data = self.games[room_jid.userhost()]
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
505 players_data = game_data['players_data']
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
506
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
507 for elt in game_elt.elements():
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
508
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
509 if elt.name == 'started': #new game created
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
510 players = []
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
511 for player in elt.elements():
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
512 players.append(unicode(player))
93
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
513 self.host.bridge.tarotGameStarted(room_jid.userhost(), from_jid.full(), players, profile)
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
514
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
515 elif elt.name == 'player_ready': #ready to play
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
516 player = elt['player']
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
517 status = self.games[room_jid.userhost()]['status']
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
518 nb_players = len(self.games[room_jid.userhost()]['players'])
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
519 status[player] = 'ready'
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
520 debug (_('Player %(player)s is ready to start [status: %(status)s]') % {'player':player, 'status':status})
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
521 if status.values().count('ready') == nb_players: #everybody is ready, we can start the game
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
522 self.newGame(room_jid, profile)
88
Goffi <goffi@goffi.org>
parents:
diff changeset
523
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
524 elif elt.name == 'hand': #a new hand has been received
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
525 self.host.bridge.tarotGameNew(room_jid.userhost(), self.__xml_to_list(elt), profile)
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
526
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
527 elif elt.name == 'contrat': #it's time to choose contrat
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
528 form = data_form.Form.fromElement(elt.firstChildElement())
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
529 xml_data = XMLTools.dataForm2xml(form)
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
530 self.host.bridge.tarotGameChooseContrat(room_jid.userhost(), xml_data, profile)
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
531
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
532 elif elt.name == 'contrat_choosed':
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
533 #TODO: check we receive the contrat from the right person
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
534 #TODO: use proper XEP-0004 way for answering form
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
535 player = elt['player']
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
536 players_data[player]['contrat'] = unicode(elt)
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
537 contrats = [players_data[player]['contrat'] for player in game_data['players']]
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
538 if contrats.count(None):
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
539 #not everybody has choosed his contrat, it's next one turn
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
540 player = self.__next_player(game_data)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
541 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof:
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
542 mess = self.createGameElt(to_jid)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
543 mess.firstChildElement().addChild(self.__ask_contrat())
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
544 self.host.profiles[profile].xmlstream.send(mess)
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
545 else:
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
546 #TODO: gof: manage "everybody pass" case
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
547 best_contrat = [None, "Passe"]
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
548 for player in game_data['players']:
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
549 contrat = players_data[player]['contrat']
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
550 idx_best = self.contrats.index(best_contrat[1])
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
551 idx_pl = self.contrats.index(contrat)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
552 if idx_pl > idx_best:
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
553 best_contrat[0] = player
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
554 best_contrat[1] = contrat
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
555 debug (_("%(player)s win the bid with %(contrat)s") % {'player':best_contrat[0],'contrat':best_contrat[1]})
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
556 game_data['contrat'] = best_contrat[1]
98
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
557
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
558 if game_data['contrat'] == "Garde Sans" or game_data['contrat'] == "Garde Contre":
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
559 self.__start_play(room_jid, game_data, profile)
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
560 game_data['attaquant'] = best_contrat[0]
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
561 else:
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
562 #Time to show the chien to everybody
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
563 to_jid = jid.JID(room_jid.userhost()) #FIXME: gof:
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
564 mess = self.createGameElt(to_jid)
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
565 chien_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(game_data['chien'], 'chien'))
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
566 chien_elt['attaquant'] = best_contrat[0]
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
567 self.host.profiles[profile].xmlstream.send(mess)
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
568 #the attacker (attaquant) get the chien
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
569 game_data['hand'][best_contrat[0]].extend(game_data['chien'])
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
570 del game_data['chien'][:]
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
571
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
572 if game_data['contrat'] == "Garde Sans":
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
573 #The chien go into attaquant's (attacker) levees
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
574 players_data[best_contrat[0]]['levees'].extend(game_data['chien'])
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
575 del game_data['chien'][:]
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
576
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
577
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
578 elif elt.name == 'chien': #we have received the chien
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
579 debug (_("tarot: chien received"))
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
580 data = {"attaquant":elt['attaquant']}
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
581 game_data['stage'] = "ecart"
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
582 game_data['attaquant'] = elt['attaquant']
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
583 self.host.bridge.tarotGameShowCards(room_jid.userhost(), "chien", self.__xml_to_list(elt), data, profile)
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
584
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
585 elif elt.name == 'cards_played':
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
586 if game_data['stage'] == "ecart":
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
587 #TODO: check validity of écart (no king, no oulder, cards must be in player hand)
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
588 #TODO: show atouts (trumps) if player put some in écart
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
589 assert (game_data['attaquant'] == elt['player']) #TODO: throw an xml error here
98
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
590 list_cards = Card.from_tuples(self.__xml_to_list(elt))
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
591 #FIXME: gof: manage Garde Sans & Garde Contre cases
98
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
592 players_data[elt['player']]['levees'].extend(list_cards) #we add the chien to attaquant's levées
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
593 for card in list_cards:
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
594 game_data['hand'][elt['player']].remove(card)
98
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
595
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
596 self.__start_play(room_jid, game_data, profile)
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
597
93
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
598 elif game_data['stage'] == "play":
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
599 current_player = game_data['players'][game_data['current_player']]
98
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
600 cards = Card.from_tuples(self.__xml_to_list(elt))
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
601
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
602 if mess_elt['type'] == 'groupchat':
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
603 self.host.bridge.tarotGameCardsPlayed(room_jid.userhost(), elt['player'], self.__xml_to_list(elt), profile)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
604 else:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
605 #TODO: check card validity and send error mess if necessary
93
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
606 #the card played is ok, we forward it to everybody
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
607 #first we remove it from the hand and put in on the table
93
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
608 game_data['hand'][current_player].remove(cards[0])
98
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
609 players_data[current_player]['played'] = cards[0]
93
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
610
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
611 #then we forward the message
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
612 mess = self.createGameElt(room_jid)
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
613 playcard_elt = mess.firstChildElement().addChild(elt)
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
614 self.host.profiles[profile].xmlstream.send(mess)
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
615
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
616 #Did everybody played ?
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
617 played = [players_data[player]['played'] for player in game_data['players']]
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
618 if all(played):
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
619 #everybody has played
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
620 winner = self.__winner(game_data)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
621 debug (_('The winner of this trick is %s') % winner)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
622 #the winner win the trick
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
623 self.__excuse_hack(game_data, played, winner)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
624 players_data[elt['player']]['levees'].extend(played)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
625 #nothing left on the table
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
626 for player in game_data['players']:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
627 players_data[player]['played'] = None
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
628 if len(game_data['hand'][current_player]) == 0:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
629 #no card lef: the game is finished
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
630 to_jid = jid.JID(room_jid.userhost()) #FIXME: gof:
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
631 mess = self.createGameElt(to_jid)
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
632 chien_elt = mess.firstChildElement().addChild(self.__give_scores(*self.__calculate_scores(game_data)))
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
633 self.host.profiles[profile].xmlstream.send(mess)
94
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
634 return
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
635 #next player is the winner
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
636 next_player = game_data['first_player'] = self.__next_player(game_data, winner)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
637 else:
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
638 next_player = self.__next_player(game_data)
1eb5ccead43c Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 93
diff changeset
639
93
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
640 #finally, we tell to the next player to play
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
641 to_jid = jid.JID(room_jid.userhost()+"/"+next_player) #FIXME: gof:
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
642 mess = self.createGameElt(to_jid)
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
643 yourturn_elt = mess.firstChildElement().addElement('your_turn')
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
644 self.host.profiles[profile].xmlstream.send(mess)
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 92
diff changeset
645
92
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
646 elif elt.name == 'your_turn':
2503de7fb4c7 Tarot game: chien/écart stage
Goffi <goffi@goffi.org>
parents: 91
diff changeset
647 self.host.bridge.tarotGameYourTurn(room_jid.userhost(), profile)
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
648
95
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
649 elif elt.name == 'score':
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
650 form_elt = elt.elements(name='x',uri='jabber:x:data').next()
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
651 winners = []
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
652 loosers = []
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
653 for winner in elt.elements(name='winner', uri=''):
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
654 winners.append(unicode(winner))
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
655 for looser in elt.elements(name='looser', uri=''):
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
656 loosers.append(unicode(looser))
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
657 form = data_form.Form.fromElement(form_elt)
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
658 xml_data = XMLTools.dataForm2xml(form)
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
659 self.host.bridge.tarotGameScore(room_jid.userhost(), xml_data, winners, loosers, profile)
be206a3d1a9b Tarot game: score calculation
Goffi <goffi@goffi.org>
parents: 94
diff changeset
660
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
661 def getHandler(self, profile):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
662 return CardGameHandler(self)
88
Goffi <goffi@goffi.org>
parents:
diff changeset
663
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
664 class CardGameHandler (XMPPHandler):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
665 implements(iwokkel.IDisco)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
666
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
667 def __init__(self, plugin_parent):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
668 self.plugin_parent = plugin_parent
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
669 self.host = plugin_parent.host
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
670
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
671 def connectionInitialized(self):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
672 self.xmlstream.addObserver(CG_REQUEST, self.plugin_parent.card_game_cmd, profile = self.parent.profile)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
673
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
674 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
98
dd556233a1b1 Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents: 96
diff changeset
675 return [disco.DiscoFeature(NS_CG)]
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
676
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
677 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
678 return []
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
679