annotate src/plugins/plugin_misc_quiz.py @ 361:141eeb7cd9e6

Quizz game: first draft
author Goffi <goffi@goffi.org>
date Sun, 12 Jun 2011 16:28:33 +0200
parents
children 208107419b17
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 SAT plugin for managing Quiz game
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from logging import debug, info, warning, error
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.xish import domish
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.internet import protocol, defer, threads, reactor
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import client, jid, xmlstream
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber import error as jab_error
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.words.protocols.jabber.xmlstream import IQ
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 import random
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from zope.interface import implements
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from wokkel import disco, iwokkel, data_form
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from sat.tools.xml_tools import dataForm2xml
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from sat.tools.games import TarotCard
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from time import time
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 try:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 from twisted.words.protocols.xmlstream import XMPPHandler
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 except ImportError:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 from wokkel.subprotocols import XMPPHandler
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 MESSAGE = '/message'
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 NS_QG = 'http://www.goffi.org/protocol/quiz'
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 QG_TAG = 'quiz'
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 QG_REQUEST = MESSAGE + '/' + QG_TAG + '[@xmlns="' + NS_QG + '"]'
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 PLUGIN_INFO = {
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 "name": "Quiz game plugin",
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 "import_name": "Quiz",
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 "type": "Game",
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 "protocols": [],
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 "dependencies": ["XEP-0045", "XEP-0249"],
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 "main": "Quiz",
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 "handler": "yes",
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 "description": _("""Implementation of Quiz game""")
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 }
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 class Quiz():
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 def __init__(self, host):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 info(_("Plugin Quiz initialization"))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self.host = host
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 self.games={}
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.waiting_inv = {} #Invitation waiting for people to join to launch a game
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 host.bridge.addMethod("quizGameLaunch", ".communication", in_sign='ass', out_sign='', method=self.quizGameLaunch) #args: room_jid, players, profile
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68 host.bridge.addMethod("quizGameCreate", ".communication", in_sign='sass', out_sign='', method=self.quizGameCreate) #args: room_jid, players, profile
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 host.bridge.addMethod("quizGameReady", ".communication", in_sign='sss', out_sign='', method=self.newPlayerReady) #args: player, referee, profile
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 host.bridge.addSignal("quizGameStarted", ".communication", signature='ssass') #args: room_jid, referee, players, profile
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 host.bridge.addSignal("quizGameNew", ".communication",
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 signature='sa{ss}s',
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 doc = { 'summary': 'Start a new game',
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74 'param_0': "jid of game's room",
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 'param_1': "data of the game",
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76 'param_2': '%(doc_profile)s'})
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 host.bridge.addSignal("quizGameQuestion", ".communication",
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 signature = 'sssis',
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 doc = { 'summary': "Send the current question",
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80 'param_0': "jid of game's room",
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81 'param_1': "question id",
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 'param_2': "question to ask",
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 'param_3': "timer",
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84 'param_4': '%(doc_profile)s'})
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 host.trigger.add("MUC user joined", self.userJoinedTrigger)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 def createGameElt(self, to_jid, type="normal"):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 type = "normal" if to_jid.resource else "groupchat"
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89 elt = domish.Element(('jabber:client','message'))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 elt["to"] = to_jid.full()
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91 elt["type"] = type
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92 elt.addElement((NS_QG, QG_TAG))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93 return elt
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95 def __game_data_to_xml(self, game_data):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 """Convert a game data dict to domish element"""
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97 game_data_elt = domish.Element(('','game_data'))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98 for data in game_data:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
99 data_elt = domish.Element(('',data))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100 data_elt.addContent(game_data[data])
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101 game_data_elt.addChild(data_elt)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102 return game_data_elt
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
103
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104 def __xml_to_game_data(self, game_data_elt):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
105 """Convert a domish element with game_data to a dict"""
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
106 game_data = {}
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
107 for data_elt in game_data_elt.elements():
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
108 game_data[data_elt.name] = unicode(data_elt)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109 return game_data
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
111 def __create_started_elt(self, players):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
112 """Create a game_started domish element"""
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
113 started_elt = domish.Element(('','started'))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
114 idx = 0
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
115 for player in players:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
116 player_elt = domish.Element(('','player'))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
117 player_elt.addContent(player)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
118 player_elt['index'] = str(idx)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
119 idx+=1
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
120 started_elt.addChild(player_elt)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
121 return started_elt
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
122
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
123 def __ask_question(self, question_id, question, timer=30):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
124 """Create a element for asking a question"""
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
125 question_elt = domish.Element(('','question'))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
126 question_elt['id'] = question_id
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
127 question_elt['timer'] = str(timer)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128 question_elt.addContent(question)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
129 return question_elt
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
130
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
131 def __start_play(self, room_jid, game_data, profile):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
132 """Start the game (tell to the first player after dealer to play"""
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
133 game_data['stage'] = "play"
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
134 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) #the player after the dealer start
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
135 game_data['first_player'] = next_player = game_data['players'][next_player_idx]
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
136 to_jid = jid.JID(room_jid.userhost()+"/"+next_player) #FIXME: gof:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
137 mess = self.createGameElt(to_jid)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
138 yourturn_elt = mess.firstChildElement().addElement('your_turn')
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
139 self.host.profiles[profile].xmlstream.send(mess)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
140
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
141
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
142 def userJoinedTrigger(self, room, user, profile):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
143 """This trigger is used to check if we are waiting people in this room,
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
144 and to create a game if everybody is here"""
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
145 _room_jid = room.occupantJID.userhostJID()
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
146 if _room_jid in self.waiting_inv and len(room.roster) == 4:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
147 #When we have 4 people in the room, we create the game
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
148 #TODO: check people identity
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
149 players = room.roster.keys()
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
150 del self.waiting_inv[_room_jid]
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
151 self.quizGameCreate(_room_jid.userhost(), players, profile_key=profile)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
152 return True
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
153
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
154 def quizGameLaunch(self, players, profile_key='@DEFAULT@'):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
155 """Launch a game: helper method to create a room, invite players, and create the quiz game
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
156 @param players: list for players jid"""
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
157 debug(_('Launching quiz game'))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
158 profile = self.host.memory.getProfileName(profile_key)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
159 if not profile:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
160 error(_("Unknown profile"))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
161 return
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
162
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
163 def quizRoomJoined(room):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
164 _room = room.occupantJID.userhostJID()
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
165 for player in players:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
166 self.host.plugins["XEP-0249"].invite(jid.JID(player), room.occupantJID.userhostJID(), {"game":"Quiz"}, profile)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
167 self.waiting_inv[_room] = (time(), players) #TODO: remove invitation waiting for too long, using the time data
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
168
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
169 def after_init(ignore):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
170 room_name = "sat_quiz_%s" % self.host.plugins["XEP-0045"].getUniqueName(profile_key)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
171 print "\n\n===> room_name:", room_name
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
172 muc_service = None
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
173 for service in self.host.memory.getServerServiceEntities("conference", "text", profile):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
174 if not ".irc." in service.userhost():
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
175 #FIXME:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
176 #This awfull ugly hack is here to avoid an issue with openfire: the irc gateway
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
177 #use "conference/text" identity (instead of "conference/irc"), there is certainly a better way
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
178 #to manage this, but this hack fill do it for test purpose
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
179 muc_service = service
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
180 break
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
181 if not muc_service:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
182 error(_("Can't find a MUC service"))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
183 return
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
184
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
185 _jid, xmlstream = self.host.getJidNStream(profile)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
186 d = self.host.plugins["XEP-0045"].join(muc_service.userhost(), room_name, _jid.user, profile).addCallback(quizRoomJoined)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
187
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
188 client = self.host.getClient(profile)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
189 if not client:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
190 error(_('No client for this profile key: %s') % profile_key)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
191 return
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
192 client.client_initialized.addCallback(after_init)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
193
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
194 def quizGameCreate(self, room_jid_param, players, profile_key='@DEFAULT@'):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
195 """Create a new game
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
196 @param room_jid_param: jid of the room
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
197 @param players: list of players nick (nick must exist in the room)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
198 @param profile_key: %(doc_profile_key)s"""
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
199 debug (_("Creating Quiz game"))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
200 room_jid = jid.JID(room_jid_param)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
201 profile = self.host.memory.getProfileName(profile_key)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
202 if not profile:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
203 error (_("profile %s is unknown") % profile_key)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
204 return
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
205 if self.games.has_key(room_jid):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
206 warning (_("Quiz game already started in room %s") % room_jid.userhost())
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
207 else:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
208 room_nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid.userhost(), profile)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
209 if not room_nick:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
210 error ('Internal error')
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
211 return
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
212 referee = room_jid.userhost() + '/' + room_nick
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
213 status = {}
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
214 players_data = {}
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
215 for player in players:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
216 players_data[player] = {'score':0}
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
217 status[player] = "init"
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
218 self.games[room_jid.userhost()] = {'referee':referee, 'players':players, 'status':status, 'players_data':players_data, 'stage': None}
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
219 for player in players:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
220 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
221 mess.firstChildElement().addChild(self.__create_started_elt(players))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
222 self.host.profiles[profile].xmlstream.send(mess)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
223
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
224 def newPlayerReady(self, player, referee, profile_key='@DEFAULT@'):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
225 """Must be called when player is ready to start a new game"""
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
226 profile = self.host.memory.getProfileName(profile_key)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
227 if not profile:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
228 error (_("profile %s is unknown") % profile_key)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
229 return
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
230 debug ('new player ready: %s' % profile)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
231 mess = self.createGameElt(jid.JID(referee))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
232 ready_elt = mess.firstChildElement().addElement('player_ready')
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
233 ready_elt['player'] = player
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
234 self.host.profiles[profile].xmlstream.send(mess)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
235
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
236 def askQuestion(self, room_jid, profile):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
237 mess = self.createGameElt(room_jid)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
238 mess.firstChildElement().addChild(self.__ask_question("1", u"Quel est l'âge du capitaine ?"))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
239 self.host.profiles[profile].xmlstream.send(mess)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
240
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
241 def newGame(self, room_jid, profile):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
242 """Launch a new round"""
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
243 debug (_('new Quiz game'))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
244 game_data = self.games[room_jid.userhost()]
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
245 players = game_data['players']
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
246 players_data = game_data['players_data']
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
247 game_data['stage'] = "init"
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
248
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
249 for player in players:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
250 players_data[player]['game_score'] = 0
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
251
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
252 new_game_data = {"instructions": _(u"""Bienvenue dans cette partie rapide de quizz, le premier à atteindre le score de 9 remporte le jeu
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
253
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
254 Attention, tu es prêt ?""")}
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
255
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
256 mess = self.createGameElt(room_jid)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
257 mess.firstChildElement().addChild(self.__game_data_to_xml(new_game_data))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
258 self.host.profiles[profile].xmlstream.send(mess)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
259 reactor.callLater(10, self.askQuestion, room_jid, profile)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
260
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
261 def quiz_game_cmd(self, mess_elt, profile):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
262 from_jid = jid.JID(mess_elt['from'])
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
263 room_jid = jid.JID(from_jid.userhost())
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
264 game_elt = mess_elt.firstChildElement()
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
265 game_data = self.games[room_jid.userhost()]
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
266 players_data = game_data['players_data']
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
267
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
268 for elt in game_elt.elements():
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
269
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
270 if elt.name == 'started': #new game created
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
271 players = []
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
272 for player in elt.elements():
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
273 players.append(unicode(player))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
274 self.host.bridge.quizGameStarted(room_jid.userhost(), from_jid.full(), players, profile)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
275
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
276 elif elt.name == 'player_ready': #ready to play
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
277 player = elt['player']
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
278 status = self.games[room_jid.userhost()]['status']
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
279 nb_players = len(self.games[room_jid.userhost()]['players'])
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
280 status[player] = 'ready'
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
281 debug (_('Player %(player)s is ready to start [status: %(status)s]') % {'player':player, 'status':status})
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
282 if status.values().count('ready') == nb_players: #everybody is ready, we can start the game
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
283 self.newGame(room_jid, profile)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
284
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
285 elif elt.name == 'game_data':
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
286 self.host.bridge.quizGameNew(room_jid.userhost(), self.__xml_to_game_data(elt), profile)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
287
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
288 elif elt.name == 'question': #A question is asked
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
289 self.host.bridge.quizGameQuestion(room_jid.userhost(), elt["id"], unicode(elt), int(elt["timer"]), profile )
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
290
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
291 else:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
292 error (_('Unmanaged game element: %s') % elt.name)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
293
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
294 def getHandler(self, profile):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
295 return QuizGameHandler(self)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
296
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
297 class QuizGameHandler (XMPPHandler):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
298 implements(iwokkel.IDisco)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
299
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
300 def __init__(self, plugin_parent):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
301 self.plugin_parent = plugin_parent
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
302 self.host = plugin_parent.host
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
303
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
304 def connectionInitialized(self):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
305 self.xmlstream.addObserver(QG_REQUEST, self.plugin_parent.quiz_game_cmd, profile = self.parent.profile)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
306
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
307 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
308 return [disco.DiscoFeature(NS_QG)]
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
309
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
310 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
311 return []
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
312