annotate plugins/plugin_misc_tarot.py @ 88:59f181e8433a

Tarot SàT: added forgotten file Tarot plugin plugin Tarot: distribution first draft
author Goffi <goffi@goffi.org>
date Wed, 12 May 2010 11:55:18 +0930
parents
children 23caf1051099
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
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from wokkel import disco, iwokkel, muc
Goffi <goffi@goffi.org>
parents:
diff changeset
35
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from base64 import b64decode
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from hashlib import sha1
Goffi <goffi@goffi.org>
parents:
diff changeset
38 from time import sleep
Goffi <goffi@goffi.org>
parents:
diff changeset
39
Goffi <goffi@goffi.org>
parents:
diff changeset
40 try:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 from twisted.words.protocols.xmlstream import XMPPHandler
Goffi <goffi@goffi.org>
parents:
diff changeset
42 except ImportError:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 from wokkel.subprotocols import XMPPHandler
Goffi <goffi@goffi.org>
parents:
diff changeset
44
Goffi <goffi@goffi.org>
parents:
diff changeset
45
Goffi <goffi@goffi.org>
parents:
diff changeset
46 PLUGIN_INFO = {
Goffi <goffi@goffi.org>
parents:
diff changeset
47 "name": "Tarot cards plugin",
Goffi <goffi@goffi.org>
parents:
diff changeset
48 "import_name": "Tarot",
Goffi <goffi@goffi.org>
parents:
diff changeset
49 "type": "Misc",
Goffi <goffi@goffi.org>
parents:
diff changeset
50 "protocols": [],
Goffi <goffi@goffi.org>
parents:
diff changeset
51 "dependencies": ["XEP_0045"],
Goffi <goffi@goffi.org>
parents:
diff changeset
52 "main": "Tarot",
Goffi <goffi@goffi.org>
parents:
diff changeset
53 "handler": "no",
Goffi <goffi@goffi.org>
parents:
diff changeset
54 "description": _("""Implementation of Tarot card game""")
Goffi <goffi@goffi.org>
parents:
diff changeset
55 }
Goffi <goffi@goffi.org>
parents:
diff changeset
56
Goffi <goffi@goffi.org>
parents:
diff changeset
57 class Tarot():
Goffi <goffi@goffi.org>
parents:
diff changeset
58
Goffi <goffi@goffi.org>
parents:
diff changeset
59 def __init__(self, host):
Goffi <goffi@goffi.org>
parents:
diff changeset
60 info(_("Plugin Tarot initialization"))
Goffi <goffi@goffi.org>
parents:
diff changeset
61 self.host = host
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self.games={}
Goffi <goffi@goffi.org>
parents:
diff changeset
63 host.bridge.addMethod("createTarotGame", ".communication", in_sign='sass', out_sign='', method=self.createGame)
Goffi <goffi@goffi.org>
parents:
diff changeset
64 host.bridge.addSignal("tarotGameStarted", ".communication", signature='sass') #args: room_id, players, profile
Goffi <goffi@goffi.org>
parents:
diff changeset
65 host.bridge.addSignal("tarotGameNew", ".communication", signature='sa(ss)s') #args: room_id, hand, profile
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.deck_ordered = []
Goffi <goffi@goffi.org>
parents:
diff changeset
67 for value in map(str,range(1,22))+['excuse']:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self.deck_ordered.append(("atout",value))
Goffi <goffi@goffi.org>
parents:
diff changeset
69 for family in ["pique", "coeur", "carreau", "trefle"]:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 self.deck_ordered.append((family, value))
Goffi <goffi@goffi.org>
parents:
diff changeset
72
Goffi <goffi@goffi.org>
parents:
diff changeset
73 def createGame(self, room_jid, players, profile_key='@DEFAULT@'):
Goffi <goffi@goffi.org>
parents:
diff changeset
74 """Create a new game"""
Goffi <goffi@goffi.org>
parents:
diff changeset
75 debug (_("Creating Tarot game"))
Goffi <goffi@goffi.org>
parents:
diff changeset
76 profile = self.host.memory.getProfileName(profile_key)
Goffi <goffi@goffi.org>
parents:
diff changeset
77 if not profile:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 error (_("profile %s is unknown") % profile_key)
Goffi <goffi@goffi.org>
parents:
diff changeset
79 return
Goffi <goffi@goffi.org>
parents:
diff changeset
80 if False: #gof: self.games.has_key(room_jid):
Goffi <goffi@goffi.org>
parents:
diff changeset
81 warning (_("Tarot game already started in room %s") % room_jid)
Goffi <goffi@goffi.org>
parents:
diff changeset
82 else:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 self.games[room_jid] = {'players':players, 'profile':profile, 'hand_size':18, 'player_start':0}
Goffi <goffi@goffi.org>
parents:
diff changeset
84 self.host.bridge.tarotGameStarted(room_jid, players, profile)
Goffi <goffi@goffi.org>
parents:
diff changeset
85 self.newGame(room_jid)
Goffi <goffi@goffi.org>
parents:
diff changeset
86
Goffi <goffi@goffi.org>
parents:
diff changeset
87
Goffi <goffi@goffi.org>
parents:
diff changeset
88 def newGame(self, room_jid):
Goffi <goffi@goffi.org>
parents:
diff changeset
89 """Launch a new round"""
Goffi <goffi@goffi.org>
parents:
diff changeset
90 debug (_('new Tarot game'))
Goffi <goffi@goffi.org>
parents:
diff changeset
91 deck = self.deck_ordered[:]
Goffi <goffi@goffi.org>
parents:
diff changeset
92 random.shuffle(deck)
Goffi <goffi@goffi.org>
parents:
diff changeset
93 profile = self.games[room_jid]['profile']
Goffi <goffi@goffi.org>
parents:
diff changeset
94 players = self.games[room_jid]['players']
Goffi <goffi@goffi.org>
parents:
diff changeset
95 hand = self.games[room_jid]['hand'] = {}
Goffi <goffi@goffi.org>
parents:
diff changeset
96 hand_size = self.games[room_jid]['hand_size']
Goffi <goffi@goffi.org>
parents:
diff changeset
97 chien = self.games[room_jid]['chien'] = []
Goffi <goffi@goffi.org>
parents:
diff changeset
98 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
99 hand[players[i]] = deck[0:hand_size]
Goffi <goffi@goffi.org>
parents:
diff changeset
100 del deck[0:hand_size]
Goffi <goffi@goffi.org>
parents:
diff changeset
101 chien = deck[:]
Goffi <goffi@goffi.org>
parents:
diff changeset
102 del(deck[:])
Goffi <goffi@goffi.org>
parents:
diff changeset
103
Goffi <goffi@goffi.org>
parents:
diff changeset
104 for player in players:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 print "envoi de main a",player
Goffi <goffi@goffi.org>
parents:
diff changeset
106 self.host.sendMessage(room_jid+"/"+player, "/hand: %s" % str(hand[player]))
Goffi <goffi@goffi.org>
parents:
diff changeset
107
Goffi <goffi@goffi.org>
parents:
diff changeset
108 self.host.bridge.tarotGameNew(room_jid, hand[players[0]], profile)
Goffi <goffi@goffi.org>
parents:
diff changeset
109
Goffi <goffi@goffi.org>
parents:
diff changeset
110