88
|
1 #!/usr/bin/python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 """ |
|
5 SAT plugin for managing xep-0045 |
|
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) |
|
7 |
|
8 This program is free software: you can redistribute it and/or modify |
|
9 it under the terms of the GNU General Public License as published by |
|
10 the Free Software Foundation, either version 3 of the License, or |
|
11 (at your option) any later version. |
|
12 |
|
13 This program is distributed in the hope that it will be useful, |
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 GNU General Public License for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20 """ |
|
21 |
|
22 from logging import debug, info, warning, error |
|
23 from twisted.words.xish import domish |
|
24 from twisted.internet import protocol, defer, threads, reactor |
|
25 from twisted.words.protocols.jabber import client, jid, xmlstream |
|
26 from twisted.words.protocols.jabber import error as jab_error |
|
27 from twisted.words.protocols.jabber.xmlstream import IQ |
|
28 import os.path |
|
29 import pdb |
|
30 import random |
|
31 |
|
32 from zope.interface import implements |
|
33 |
|
34 from wokkel import disco, iwokkel, muc |
|
35 |
|
36 from base64 import b64decode |
|
37 from hashlib import sha1 |
|
38 from time import sleep |
|
39 |
|
40 try: |
|
41 from twisted.words.protocols.xmlstream import XMPPHandler |
|
42 except ImportError: |
|
43 from wokkel.subprotocols import XMPPHandler |
|
44 |
|
45 |
|
46 PLUGIN_INFO = { |
|
47 "name": "Tarot cards plugin", |
|
48 "import_name": "Tarot", |
|
49 "type": "Misc", |
|
50 "protocols": [], |
|
51 "dependencies": ["XEP_0045"], |
|
52 "main": "Tarot", |
|
53 "handler": "no", |
|
54 "description": _("""Implementation of Tarot card game""") |
|
55 } |
|
56 |
|
57 class Tarot(): |
|
58 |
|
59 def __init__(self, host): |
|
60 info(_("Plugin Tarot initialization")) |
|
61 self.host = host |
|
62 self.games={} |
|
63 host.bridge.addMethod("createTarotGame", ".communication", in_sign='sass', out_sign='', method=self.createGame) |
|
64 host.bridge.addSignal("tarotGameStarted", ".communication", signature='sass') #args: room_id, players, profile |
|
65 host.bridge.addSignal("tarotGameNew", ".communication", signature='sa(ss)s') #args: room_id, hand, profile |
|
66 self.deck_ordered = [] |
|
67 for value in map(str,range(1,22))+['excuse']: |
|
68 self.deck_ordered.append(("atout",value)) |
|
69 for family in ["pique", "coeur", "carreau", "trefle"]: |
|
70 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: |
|
71 self.deck_ordered.append((family, value)) |
|
72 |
|
73 def createGame(self, room_jid, players, profile_key='@DEFAULT@'): |
|
74 """Create a new game""" |
|
75 debug (_("Creating Tarot game")) |
|
76 profile = self.host.memory.getProfileName(profile_key) |
|
77 if not profile: |
|
78 error (_("profile %s is unknown") % profile_key) |
|
79 return |
|
80 if False: #gof: self.games.has_key(room_jid): |
|
81 warning (_("Tarot game already started in room %s") % room_jid) |
|
82 else: |
|
83 self.games[room_jid] = {'players':players, 'profile':profile, 'hand_size':18, 'player_start':0} |
|
84 self.host.bridge.tarotGameStarted(room_jid, players, profile) |
|
85 self.newGame(room_jid) |
|
86 |
|
87 |
|
88 def newGame(self, room_jid): |
|
89 """Launch a new round""" |
|
90 debug (_('new Tarot game')) |
|
91 deck = self.deck_ordered[:] |
|
92 random.shuffle(deck) |
|
93 profile = self.games[room_jid]['profile'] |
|
94 players = self.games[room_jid]['players'] |
|
95 hand = self.games[room_jid]['hand'] = {} |
|
96 hand_size = self.games[room_jid]['hand_size'] |
|
97 chien = self.games[room_jid]['chien'] = [] |
|
98 for i in range(4): #TODO: distribute according to real Tarot rules (3 by 3 counter-clockwise, 1 card at once to chien) |
|
99 hand[players[i]] = deck[0:hand_size] |
|
100 del deck[0:hand_size] |
|
101 chien = deck[:] |
|
102 del(deck[:]) |
|
103 |
|
104 for player in players: |
|
105 print "envoi de main a",player |
|
106 self.host.sendMessage(room_jid+"/"+player, "/hand: %s" % str(hand[player])) |
|
107 |
|
108 self.host.bridge.tarotGameNew(room_jid, hand[players[0]], profile) |
|
109 |
|
110 |