annotate plugins/plugin_misc_tarot.py @ 91:39c672544593

Tarot: bidding phase - quick_app: command line is now parsed, "profile" option allow to select it - xml_tools: list-single is now managed - plugin tarot: method and signal to manage contract (contrat): tarotChooseContrat & tarotGameContratChoosed - wix: Q&D Form hack to manage more generic form (not only registration), used to show contract selection form
author Goffi <goffi@goffi.org>
date Thu, 27 May 2010 19:26:19 +0930
parents 4020931569b8
children 2503de7fb4c7
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
Goffi <goffi@goffi.org>
parents:
diff changeset
62 class Tarot():
Goffi <goffi@goffi.org>
parents:
diff changeset
63
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def __init__(self, host):
Goffi <goffi@goffi.org>
parents:
diff changeset
65 info(_("Plugin Tarot initialization"))
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.host = host
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.games={}
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
68 self.contrats = [_('Passe'), _('Petite'), _('Garde'), _('Garde Sans'), _('Garde Contre')]
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
69 host.bridge.addMethod("tarotGameCreate", ".communication", in_sign='sass', out_sign='', method=self.createGame) #args: room_jid, players, profile
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
70 host.bridge.addMethod("tarotGameReady", ".communication", in_sign='sss', out_sign='', method=self.newPlayerReady) #args: user, referee, profile
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
71 host.bridge.addMethod("tarotGameContratChoosed", ".communication", in_sign='ssss', out_sign='', method=self.contratChoosed) #args: user, referee, contrat, profile
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
72 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
73 host.bridge.addSignal("tarotGameNew", ".communication", signature='sa(ss)s') #args: room_jid, hand, profile
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
74 host.bridge.addSignal("tarotChooseContrat", ".communication", signature='sss') #args: room_jid, xml_data, profile
88
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self.deck_ordered = []
Goffi <goffi@goffi.org>
parents:
diff changeset
76 for value in map(str,range(1,22))+['excuse']:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self.deck_ordered.append(("atout",value))
Goffi <goffi@goffi.org>
parents:
diff changeset
78 for family in ["pique", "coeur", "carreau", "trefle"]:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 self.deck_ordered.append((family, value))
Goffi <goffi@goffi.org>
parents:
diff changeset
81
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
82 def createGameElt(self, to_jid):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
83 elt = domish.Element(('jabber:client','message'))
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
84 elt["to"] = to_jid.full()
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
85 elt.addElement((NS_CG, CG_TAG))
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
86 return elt
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
87
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
88 def __hand_to_xml(self, hand):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
89 """Convert a hand (list of tuples) to domish element"""
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
90 hand_elt = domish.Element(('','hand'))
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
91 for family, value in hand:
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
92 card_elt = domish.Element(('','card'))
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
93 card_elt['family'] = family
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
94 card_elt['value'] = value
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
95 hand_elt.addChild(card_elt)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
96 return hand_elt
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
97
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
98 def __xml_to_hand(self, hand_elt):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
99 """Convert a hand domish element to a list of tuples"""
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
100 hand = []
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
101 assert (hand_elt.name == 'hand')
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
102 for card in hand_elt.elements():
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
103 hand.append((card['family'], card['value']))
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
104 return hand
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
105
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
106 def __create_started_elt(self, players):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
107 """Create a game_started domish element"""
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
108 started_elt = domish.Element(('','started'))
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
109 idx = 0
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
110 for player in players:
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
111 player_elt = domish.Element(('','player'))
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
112 player_elt.addContent(player)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
113 player_elt['index'] = str(idx)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
114 idx+=1
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
115 started_elt.addChild(player_elt)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
116 return started_elt
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
117
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
118 def __ask_contrat(self):
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
119 """Create a element for asking contrat"""
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
120 contrat_elt = domish.Element(('','contrat'))
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
121 form = data_form.Form('form', title=_('contrat selection'))
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
122 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
123 form.addField(field)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
124 contrat_elt.addChild(form.toElement())
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
125 return contrat_elt
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
126
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
127
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
128
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
129
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
130 def __next_player(self, game_data):
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
131 """It's next player turn
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
132 Increment player number & return player name"""
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
133 pl_idx = game_data['current_player'] = (game_data['current_player'] + 1) % len(game_data['players'])
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
134 return game_data['players'][pl_idx]
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
135
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
136 def createGame(self, room_jid_param, players, profile_key='@DEFAULT@'):
88
Goffi <goffi@goffi.org>
parents:
diff changeset
137 """Create a new game"""
Goffi <goffi@goffi.org>
parents:
diff changeset
138 debug (_("Creating Tarot game"))
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
139 room_jid = jid.JID(room_jid_param)
88
Goffi <goffi@goffi.org>
parents:
diff changeset
140 profile = self.host.memory.getProfileName(profile_key)
Goffi <goffi@goffi.org>
parents:
diff changeset
141 if not profile:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 error (_("profile %s is unknown") % profile_key)
Goffi <goffi@goffi.org>
parents:
diff changeset
143 return
Goffi <goffi@goffi.org>
parents:
diff changeset
144 if False: #gof: self.games.has_key(room_jid):
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
145 warning (_("Tarot game already started in room %s") % room_jid.userhost())
88
Goffi <goffi@goffi.org>
parents:
diff changeset
146 else:
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
147 status = {}
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
148 players_data = {}
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
149 for player in players:
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
150 players_data[player] = {}
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
151 status[player] = "init"
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
152 self.games[room_jid.userhost()] = {'players':players, 'status':status, 'players_data':players_data, 'referee_profile':profile, 'hand_size':18, 'init_player':0, 'current_player': None}
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
153 for player in players:
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
154 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player))
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
155 mess.firstChildElement().addChild(self.__create_started_elt(players))
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
156 self.host.profiles[profile].xmlstream.send(mess)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
157
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
158 def newPlayerReady(self, user, referee, profile_key='@DEFAULT@'):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
159 """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
160 profile = self.host.memory.getProfileName(profile_key)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
161 if not profile:
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
162 error (_("profile %s is unknown") % profile_key)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
163 return
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
164 debug ('new player ready: %s' % profile)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
165 mess = self.createGameElt(jid.JID(referee))
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
166 ready_elt = mess.firstChildElement().addElement('player_ready')
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
167 ready_elt['user'] = user
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
168 self.host.profiles[profile].xmlstream.send(mess)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
169
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
170 def contratChoosed(self, user, referee, contrat, profile_key='@DEFAULT@'):
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
171 """Must be call by player when the contrat is selected
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
172 @param user: player's name
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
173 @param referee: arbiter jid
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
174 @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
175 @profile_key: profile
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
176 """
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
177 profile = self.host.memory.getProfileName(profile_key)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
178 if not profile:
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
179 error (_("profile %s is unknown") % profile_key)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
180 return
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
181 debug (_('contrat [%(contrat)s] choosed by %(profile)s') % {'contrat':contrat, 'profile':profile})
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
182 mess = self.createGameElt(jid.JID(referee))
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
183 contrat_elt = mess.firstChildElement().addElement(('','contrat_choosed'), content=contrat)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
184 contrat_elt['user'] = user
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
185 self.host.profiles[profile].xmlstream.send(mess)
88
Goffi <goffi@goffi.org>
parents:
diff changeset
186
Goffi <goffi@goffi.org>
parents:
diff changeset
187
Goffi <goffi@goffi.org>
parents:
diff changeset
188 def newGame(self, room_jid):
Goffi <goffi@goffi.org>
parents:
diff changeset
189 """Launch a new round"""
Goffi <goffi@goffi.org>
parents:
diff changeset
190 debug (_('new Tarot game'))
Goffi <goffi@goffi.org>
parents:
diff changeset
191 deck = self.deck_ordered[:]
Goffi <goffi@goffi.org>
parents:
diff changeset
192 random.shuffle(deck)
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
193 game_data = self.games[room_jid.userhost()]
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
194 referee_profile = game_data['referee_profile']
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
195 players = game_data['players']
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
196 players_data = game_data['players_data']
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
197 current_player = game_data['current_player']
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
198 hand = game_data['hand'] = {}
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
199 hand_size = game_data['hand_size']
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
200 chien = game_data['chien'] = []
88
Goffi <goffi@goffi.org>
parents:
diff changeset
201 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
202 hand[players[i]] = deck[0:hand_size]
Goffi <goffi@goffi.org>
parents:
diff changeset
203 del deck[0:hand_size]
Goffi <goffi@goffi.org>
parents:
diff changeset
204 chien = deck[:]
Goffi <goffi@goffi.org>
parents:
diff changeset
205 del(deck[:])
Goffi <goffi@goffi.org>
parents:
diff changeset
206
Goffi <goffi@goffi.org>
parents:
diff changeset
207 for player in players:
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
208 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof:
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
209 mess = self.createGameElt(to_jid)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
210 mess.firstChildElement().addChild(self.__hand_to_xml(hand[player]))
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
211 self.host.profiles[referee_profile].xmlstream.send(mess)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
212 players_data[player]['contrat'] = None
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
213
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
214 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
215 player = players[pl_idx]
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
216 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof:
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
217 mess = self.createGameElt(to_jid)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
218 mess.firstChildElement().addChild(self.__ask_contrat())
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
219 self.host.profiles[referee_profile].xmlstream.send(mess)
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
220
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
221
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
222 def card_game_cmd(self, mess_elt, profile):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
223 print "\n\nCARD GAME command received (profile=%s): %s" % (profile, mess_elt.toXml())
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
224 room_jid = jid.JID(mess_elt['from'])
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
225 game_elt = mess_elt.firstChildElement()
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
226 for elt in game_elt.elements(): #new game created
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
227
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
228 if elt.name == 'started':
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
229 players = []
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
230 for player in elt.elements():
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
231 players.append(unicode(player))
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
232 self.host.bridge.tarotGameStarted(room_jid.userhost(), room_jid.full(), players, profile)
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
233
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
234 elif elt.name == 'player_ready':
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
235 player = elt['user']
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
236 status = self.games[room_jid.userhost()]['status']
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
237 nb_players = len(self.games[room_jid.userhost()]['players'])
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
238 status[player] = 'ready'
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
239 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
240 if status.values().count('ready') == nb_players: #everybody is ready, we can start the game
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
241 self.newGame(room_jid)
88
Goffi <goffi@goffi.org>
parents:
diff changeset
242
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
243 elif elt.name == 'hand': #a new hand has been received
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
244 self.host.bridge.tarotGameNew(room_jid.userhost(), self.__xml_to_hand(elt), profile)
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
245
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
246 elif elt.name == 'contrat': #it's time to choose contrat
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
247 form = data_form.Form.fromElement(elt.firstChildElement())
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
248 xml_data = XMLTools.dataForm2xml(form)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
249 self.host.bridge.tarotChooseContrat(room_jid.userhost(), xml_data, profile)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
250
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
251 elif elt.name == 'contrat_choosed': #the player has chooser a contrat
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
252 #TODO: check we receive the contrat from the right person
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
253 #TODO: user proper XEP-0004 way for answering form
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
254 user = elt['user']
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
255 game_data = self.games[room_jid.userhost()]
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
256 players_data = game_data['players_data']
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
257 players_data[user]['contrat'] = unicode(elt)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
258 contrats = [players_data[player]['contrat'] for player in game_data['players']]
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
259 if contrats.count(None):
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
260 #not everybody has choosed his contrat, it's next one turn
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
261 player = self.__next_player(game_data)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
262 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof:
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
263 mess = self.createGameElt(to_jid)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
264 mess.firstChildElement().addChild(self.__ask_contrat())
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
265 self.host.profiles[game_data['referee_profile']].xmlstream.send(mess)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
266 else:
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
267 #TODO: manage "everybody pass" case
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
268 best_contrat = [None, "Passe"]
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
269 for player in game_data['players']:
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
270 contrat = players_data[player]['contrat']
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
271 idx_best = self.contrats.index(best_contrat[1])
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
272 idx_pl = self.contrats.index(contrat)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
273 if idx_pl > idx_best:
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
274 best_contrat[0] = player
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
275 best_contrat[1] = contrat
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
276 debug (_("%(player)s win the bid with %(contrat)s") % {'player':best_contrat[0],'contrat':best_contrat[1]})
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
277
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 90
diff changeset
278
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
279
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
280 def getHandler(self, profile):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
281 return CardGameHandler(self)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
282
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
283
88
Goffi <goffi@goffi.org>
parents:
diff changeset
284
90
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
285 class CardGameHandler (XMPPHandler):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
286 implements(iwokkel.IDisco)
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
287
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
288 def __init__(self, plugin_parent):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
289 self.plugin_parent = plugin_parent
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
290 self.host = plugin_parent.host
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
291
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
292 def connectionInitialized(self):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
293 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
294
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
295 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
296 return [disco.DiscoFeature(NS_CB)]
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
297
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
298 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
299 return []
4020931569b8 Tarot Game: session initialization
Goffi <goffi@goffi.org>
parents: 89
diff changeset
300