view plugins/plugin_misc_tarot.py @ 89:23caf1051099

multi-profile/subscription misc fixes
author Goffi <goffi@goffi.org>
date Thu, 13 May 2010 16:27:48 +0930
parents 59f181e8433a
children 4020931569b8
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
SAT plugin for managing xep-0045
Copyright (C) 2009, 2010  Jérôme Poisson (goffi@goffi.org)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from logging import debug, info, warning, error
from twisted.words.xish import domish
from twisted.internet import protocol, defer, threads, reactor
from twisted.words.protocols.jabber import client, jid, xmlstream
from twisted.words.protocols.jabber import error as jab_error
from twisted.words.protocols.jabber.xmlstream import IQ
import os.path
import pdb
import random

from zope.interface import implements

from wokkel import disco, iwokkel, muc

from base64 import b64decode
from hashlib import sha1
from time import sleep

try:
    from twisted.words.protocols.xmlstream import XMPPHandler
except ImportError:
    from wokkel.subprotocols import XMPPHandler


PLUGIN_INFO = {
"name": "Tarot cards plugin",
"import_name": "Tarot",
"type": "Misc",
"protocols": [],
"dependencies": ["XEP_0045"],
"main": "Tarot",
"handler": "no",
"description": _("""Implementation of Tarot card game""")
}

class Tarot():

    def __init__(self, host):
        info(_("Plugin Tarot initialization"))
        self.host = host
        self.games={}
        host.bridge.addMethod("createTarotGame", ".communication", in_sign='sass', out_sign='', method=self.createGame)
        host.bridge.addSignal("tarotGameStarted", ".communication", signature='sass') #args: room_id, players, profile
        host.bridge.addSignal("tarotGameNew", ".communication", signature='sa(ss)s') #args: room_id, hand, profile
        self.deck_ordered = []
        for value in map(str,range(1,22))+['excuse']:
            self.deck_ordered.append(("atout",value))
        for family in ["pique", "coeur", "carreau", "trefle"]:
            for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
                self.deck_ordered.append((family, value))

    def createGame(self, room_jid, players, profile_key='@DEFAULT@'):
        """Create a new game"""
        debug (_("Creating Tarot game"))
        profile = self.host.memory.getProfileName(profile_key)
        if not profile:
            error (_("profile %s is unknown") % profile_key)
            return
        if False: #gof: self.games.has_key(room_jid):
            warning (_("Tarot game already started in room %s") % room_jid)
        else:
            self.games[room_jid] = {'players':players, 'profile':profile, 'hand_size':18, 'player_start':0}
            self.host.bridge.tarotGameStarted(room_jid, players, profile)
            self.newGame(room_jid)


    def newGame(self, room_jid):
        """Launch a new round"""
        debug (_('new Tarot game'))
        deck = self.deck_ordered[:]
        random.shuffle(deck)
        profile = self.games[room_jid]['profile']
        players = self.games[room_jid]['players']
        hand = self.games[room_jid]['hand'] = {}
        hand_size = self.games[room_jid]['hand_size']
        chien = self.games[room_jid]['chien'] = []
        for i in range(4): #TODO: distribute according to real Tarot rules (3 by 3 counter-clockwise, 1 card at once to chien)
            hand[players[i]] = deck[0:hand_size]
            del deck[0:hand_size]
        chien = deck[:]
        del(deck[:])

        for player in players:
            self.host.sendMessage(room_jid+"/"+player, "/hand: %s" % str(hand[player]))
            
        self.host.bridge.tarotGameNew(room_jid, hand[players[0]], profile)