annotate src/plugins/plugin_misc_quiz.py @ 688:f7878ad3c846

tools: renamed tools.jid.JID attribute "short" to "bare"
author souliane <souliane@mailoo.org>
date Tue, 29 Oct 2013 16:26:55 +0100
parents 75e4f5e2cc65
children f610864eb7a5
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
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
4 # SAT plugin for managing Quiz game
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
10 # (at your option) any later version.
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
15 # GNU Affero General Public License for more details.
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from logging import debug, info, warning, error
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from twisted.words.xish import domish
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.internet import protocol, defer, threads, reactor
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber import client, jid, xmlstream
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.protocols.jabber import error as jab_error
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber.xmlstream import IQ
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import random
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from zope.interface import implements
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 wokkel import disco, iwokkel, data_form
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 609
diff changeset
31 from sat.tools.xml_tools import dataForm2XML
682
2805fa3f4bdf tools: moved src/tools/games.py to src/tools/frontends/
souliane <souliane@mailoo.org>
parents: 660
diff changeset
32 from sat.tools.frontends.games import TarotCard
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 682
diff changeset
33 from sat.tools.plugins.games import RoomGame
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from time import time
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 try:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from twisted.words.protocols.xmlstream import XMPPHandler
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 except ImportError:
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 from wokkel.subprotocols import XMPPHandler
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 MESSAGE = '/message'
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 NS_QG = 'http://www.goffi.org/protocol/quiz'
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 QG_TAG = 'quiz'
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 QG_REQUEST = MESSAGE + '/' + QG_TAG + '[@xmlns="' + NS_QG + '"]'
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 PLUGIN_INFO = {
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
47 "name": "Quiz game plugin",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
48 "import_name": "Quiz",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
49 "type": "Game",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
50 "protocols": [],
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
51 "dependencies": ["XEP-0045", "XEP-0249"],
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
52 "main": "Quiz",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
53 "handler": "yes",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
54 "description": _("""Implementation of Quiz game""")
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 }
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 682
diff changeset
58 class Quiz(RoomGame):
361
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 def __init__(self, host):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 info(_("Plugin Quiz initialization"))
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 682
diff changeset
62 RoomGame.__init__(self, host, PLUGIN_INFO, (NS_QG, QG_TAG), player_init_data={'score': 0},
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 682
diff changeset
63 options={'stage': None})
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 682
diff changeset
64 host.bridge.addMethod("quizGameLaunch", ".plugin", in_sign='ass', out_sign='', method=self.prepareRoom) # args: players, profile
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 682
diff changeset
65 host.bridge.addMethod("quizGameCreate", ".plugin", in_sign='sass', out_sign='', method=self.createGame) # args: room_jid, players, profile
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 682
diff changeset
66 host.bridge.addMethod("quizGameReady", ".plugin", in_sign='sss', out_sign='', method=self.playerReady) # args: player, referee, profile
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 362
diff changeset
67 host.bridge.addMethod("quizGameAnswer", ".plugin", in_sign='ssss', out_sign='', method=self.playerAnswer)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
68 host.bridge.addSignal("quizGameStarted", ".plugin", signature='ssass') # args: room_jid, referee, players, profile
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 362
diff changeset
69 host.bridge.addSignal("quizGameNew", ".plugin",
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 signature='sa{ss}s',
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
71 doc={'summary': 'Start a new game',
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
72 'param_0': "room_jid: jid of game's room",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
73 'param_1': "game_data: data of the game",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
74 'param_2': '%(doc_profile)s'})
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 362
diff changeset
75 host.bridge.addSignal("quizGameQuestion", ".plugin",
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
76 signature='sssis',
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
77 doc={'summary': "Send the current question",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
78 'param_0': "room_jid: jid of game's room",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
79 'param_1': "question_id: question id",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
80 'param_2': "question: question to ask",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
81 'param_3': "timer: timer",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
82 'param_4': '%(doc_profile)s'})
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 362
diff changeset
83 host.bridge.addSignal("quizGamePlayerBuzzed", ".plugin",
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
84 signature='ssbs',
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
85 doc={'summary': "A player just pressed the buzzer",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
86 'param_0': "room_jid: jid of game's room",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
87 'param_1': "player: player who pushed the buzzer",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
88 'param_2': "pause: should the game be paused ?",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
89 'param_3': '%(doc_profile)s'})
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 362
diff changeset
90 host.bridge.addSignal("quizGamePlayerSays", ".plugin",
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
91 signature='sssis',
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
92 doc={'summary': "A player just pressed the buzzer",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
93 'param_0': "room_jid: jid of game's room",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
94 'param_1': "player: player who pushed the buzzer",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
95 'param_2': "text: what the player say",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
96 'param_3': "delay: how long, in seconds, the text must appear",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
97 'param_4': '%(doc_profile)s'})
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 362
diff changeset
98 host.bridge.addSignal("quizGameAnswerResult", ".plugin",
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
99 signature='ssba{si}s',
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
100 doc={'summary': "Result of the just given answer",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
101 'param_0': "room_jid: jid of game's room",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
102 'param_1': "player: player who gave the answer",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
103 'param_2': "good_answer: True if the answer is right",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
104 'param_3': "score: dict of score with player as key",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
105 'param_4': '%(doc_profile)s'})
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 362
diff changeset
106 host.bridge.addSignal("quizGameTimerExpired", ".plugin",
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
107 signature='ss',
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
108 doc={'summary': "Nobody answered the question in time",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
109 'param_0': "room_jid: jid of game's room",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
110 'param_1': '%(doc_profile)s'})
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 362
diff changeset
111 host.bridge.addSignal("quizGameTimerRestarted", ".plugin",
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
112 signature='sis',
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
113 doc={'summary': "Nobody answered the question in time",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
114 'param_0': "room_jid: jid of game's room",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
115 'param_1': "time_left: time left before timer expiration",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
116 'param_2': '%(doc_profile)s'})
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
117 host.trigger.add("MUC user joined", self.userJoinedTrigger)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
118
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
119 def __game_data_to_xml(self, game_data):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
120 """Convert a game data dict to domish element"""
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
121 game_data_elt = domish.Element((None, 'game_data'))
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
122 for data in game_data:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
123 data_elt = domish.Element((None, data))
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
124 data_elt.addContent(game_data[data])
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
125 game_data_elt.addChild(data_elt)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
126 return game_data_elt
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
127
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128 def __xml_to_game_data(self, game_data_elt):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
129 """Convert a domish element with game_data to a dict"""
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
130 game_data = {}
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
131 for data_elt in game_data_elt.elements():
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
132 game_data[data_elt.name] = unicode(data_elt)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
133 return game_data
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
134
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
135 def __answer_result_to_signal_args(self, answer_result_elt):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
136 """Parse answer result element and return a tuple of signal arguments
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
137 @param answer_result_elt: answer result element
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
138 @return: (player, good_answer, score)"""
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
139 score = {}
660
69a8bfd266a5 core, plugins: fixed bad use of children instead of elements() for domish.Element instances.
Goffi <goffi@goffi.org>
parents: 630
diff changeset
140 for score_elt in answer_result_elt.elements():
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
141 score[score_elt['player']] = int(score_elt['score'])
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
142 return (answer_result_elt['player'], answer_result_elt['good_answer'] == str(True), score)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
143
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
144 def __answer_result(self, player_answering, good_answer, game_data):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
145 """Convert a domish an answer_result element
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
146 @param player_answering: player who gave the answer
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
147 @param good_answer: True is the answer is right
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
148 @param game_data: data of the game"""
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
149 players_data = game_data['players_data']
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
150 score = {}
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
151 for player in game_data['players']:
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
152 score[player] = players_data[player]['score']
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
153
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
154 answer_result_elt = domish.Element((None, 'answer_result'))
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
155 answer_result_elt['player'] = player_answering
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
156 answer_result_elt['good_answer'] = str(good_answer)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
157
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
158 for player in score:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
159 score_elt = domish.Element((None, "score"))
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
160 score_elt['player'] = player
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
161 score_elt['score'] = str(score[player])
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
162 answer_result_elt.addChild(score_elt)
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
163
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
164 return answer_result_elt
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
165
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
166 def __ask_question(self, question_id, question, timer):
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
167 """Create a element for asking a question"""
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
168 question_elt = domish.Element((None, 'question'))
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
169 question_elt['id'] = question_id
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
170 question_elt['timer'] = str(timer)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
171 question_elt.addContent(question)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
172 return question_elt
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
173
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
174 def __start_play(self, room_jid, game_data, profile):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
175 """Start the game (tell to the first player after dealer to play"""
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
176 game_data['stage'] = "play"
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
177 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # the player after the dealer start
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
178 game_data['first_player'] = next_player = game_data['players'][next_player_idx]
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
179 to_jid = jid.JID(room_jid.userhost() + "/" + next_player)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
180 mess = self.createGameElt(to_jid)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
181 yourturn_elt = mess.firstChildElement().addElement('your_turn')
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
182 self.host.profiles[profile].xmlstream.send(mess)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
183
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
184 def playerAnswer(self, player, referee, answer, profile_key='@DEFAULT@'):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
185 """Called when a player give an answer"""
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
186 profile = self.host.memory.getProfileName(profile_key)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
187 if not profile:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
188 error(_("profile %s is unknown") % profile_key)
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
189 return
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
190 debug('new player answer (%(profile)s): %(answer)s' % {'profile': profile, 'answer': answer})
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
191 mess = self.createGameElt(jid.JID(referee))
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
192 answer_elt = mess.firstChildElement().addElement('player_answer')
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
193 answer_elt['player'] = player
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
194 answer_elt.addContent(answer)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
195 self.host.profiles[profile].xmlstream.send(mess)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
196
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
197 def timerExpired(self, room_jid, profile):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
198 """Called when nobody answered the question in time"""
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
199 game_data = self.games[room_jid.userhost()]
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
200 game_data['stage'] = 'expired'
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
201 mess = self.createGameElt(room_jid)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
202 expired_elt = mess.firstChildElement().addElement('timer_expired')
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
203 self.host.profiles[profile].xmlstream.send(mess)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
204 reactor.callLater(4, self.askQuestion, room_jid, profile)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
205
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
206 def pauseTimer(self, room_jid):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
207 """Stop the timer and save the time left"""
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
208 game_data = self.games[room_jid.userhost()]
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
209 left = max(0, game_data["timer"].getTime() - time())
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
210 game_data['timer'].cancel()
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
211 game_data['time_left'] = int(left)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
212 game_data['previous_stage'] = game_data['stage']
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
213 game_data['stage'] = "paused"
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
214
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
215 def restartTimer(self, room_jid, profile):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
216 """Restart a timer with the saved time"""
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
217 game_data = self.games[room_jid.userhost()]
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
218 assert game_data['time_left'] is not None
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
219 mess = self.createGameElt(room_jid)
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
220 restarted_elt = mess.firstChildElement().addElement('timer_restarted')
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
221 restarted_elt["time_left"] = str(game_data['time_left'])
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
222 self.host.profiles[profile].xmlstream.send(mess)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
223 game_data["timer"] = reactor.callLater(game_data['time_left'], self.timerExpired, room_jid, profile)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
224 game_data["time_left"] = None
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
225 game_data['stage'] = game_data['previous_stage']
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
226 del game_data['previous_stage']
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
227
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
228 def askQuestion(self, room_jid, profile):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
229 """Ask a new question"""
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
230 game_data = self.games[room_jid.userhost()]
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
231 game_data['stage'] = "question"
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
232 game_data['question_id'] = "1"
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
233 timer = 30
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
234 mess = self.createGameElt(room_jid)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
235 mess.firstChildElement().addChild(self.__ask_question(game_data['question_id'], u"Quel est l'âge du capitaine ?", timer))
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
236 self.host.profiles[profile].xmlstream.send(mess)
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
237 game_data["timer"] = reactor.callLater(timer, self.timerExpired, room_jid, profile)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
238 game_data["time_left"] = None
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
239
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
240 def checkAnswer(self, room_jid, player, answer, profile):
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
241 """Check if the answer given is right"""
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
242 game_data = self.games[room_jid.userhost()]
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
243 players_data = game_data['players_data']
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
244 good_answer = game_data['question_id'] == "1" and answer == "42"
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
245 players_data[player]['score'] += 1 if good_answer else -1
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
246 players_data[player]['score'] = min(9, max(0, players_data[player]['score']))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
247
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
248 mess = self.createGameElt(room_jid)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
249 mess.firstChildElement().addChild(self.__answer_result(player, good_answer, game_data))
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
250 self.host.profiles[profile].xmlstream.send(mess)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
251
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
252 if good_answer:
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
253 reactor.callLater(4, self.askQuestion, room_jid, profile)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
254 else:
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
255 reactor.callLater(4, self.restartTimer, room_jid, profile)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
256
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
257 def newGame(self, room_jid, profile):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
258 """Launch a new round"""
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 682
diff changeset
259 common_data = {'game_score': 0}
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
260 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
261
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
262 Attention, tu es prêt ?""")}
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 682
diff changeset
263 msg_elts = self.__game_data_to_xml(new_game_data)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 682
diff changeset
264 RoomGame.newRound(self, room_jid, (common_data, msg_elts), profile)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
265 reactor.callLater(10, self.askQuestion, room_jid, profile)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
266
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
267 def quiz_game_cmd(self, mess_elt, profile):
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
268 from_jid = jid.JID(mess_elt['from'])
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
269 room_jid = jid.JID(from_jid.userhost())
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
270 game_elt = mess_elt.firstChildElement()
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
271 game_data = self.games[room_jid.userhost()]
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
272 players_data = game_data['players_data']
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
273
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
274 for elt in game_elt.elements():
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
275
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
276 if elt.name == 'started': # new game created
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
277 players = []
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
278 for player in elt.elements():
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
279 players.append(unicode(player))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
280 self.host.bridge.quizGameStarted(room_jid.userhost(), from_jid.full(), players, profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
281
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
282 elif elt.name == 'player_ready': # ready to play
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
283 player = elt['player']
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
284 status = self.games[room_jid.userhost()]['status']
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
285 nb_players = len(self.games[room_jid.userhost()]['players'])
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
286 status[player] = 'ready'
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
287 debug(_('Player %(player)s is ready to start [status: %(status)s]') % {'player': player, 'status': status})
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
288 if status.values().count('ready') == nb_players: # everybody is ready, we can start the game
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
289 self.newGame(room_jid, 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 elif elt.name == 'game_data':
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
292 self.host.bridge.quizGameNew(room_jid.userhost(), self.__xml_to_game_data(elt), profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
293
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
294 elif elt.name == 'question': # A question is asked
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
295 self.host.bridge.quizGameQuestion(room_jid.userhost(), elt["id"], unicode(elt), int(elt["timer"]), profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
296
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
297 elif elt.name == 'player_answer':
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
298 player = elt['player']
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
299 pause = game_data['stage'] == 'question' # we pause the game only if we are have a question at the moment
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
300 #we first send a buzzer message
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
301 mess = self.createGameElt(room_jid)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
302 buzzer_elt = mess.firstChildElement().addElement('player_buzzed')
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
303 buzzer_elt['player'] = player
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
304 buzzer_elt['pause'] = str(pause)
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
305 self.host.profiles[profile].xmlstream.send(mess)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
306 if pause:
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
307 self.pauseTimer(room_jid)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
308 #and we send the player answer
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
309 mess = self.createGameElt(room_jid)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
310 _answer = unicode(elt)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
311 say_elt = mess.firstChildElement().addElement('player_says')
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
312 say_elt['player'] = player
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
313 say_elt.addContent(_answer)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
314 say_elt['delay'] = "3"
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
315 reactor.callLater(2, self.host.profiles[profile].xmlstream.send, mess)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
316 reactor.callLater(6, self.checkAnswer, room_jid, player, _answer, profile=profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
317
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
318 elif elt.name == 'player_buzzed':
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
319 self.host.bridge.quizGamePlayerBuzzed(room_jid.userhost(), elt["player"], elt['pause'] == str(True), profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
320
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
321 elif elt.name == 'player_says':
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
322 self.host.bridge.quizGamePlayerSays(room_jid.userhost(), elt["player"], unicode(elt), int(elt["delay"]), profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
323
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
324 elif elt.name == 'answer_result':
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
325 player, good_answer, score = self.__answer_result_to_signal_args(elt)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
326 self.host.bridge.quizGameAnswerResult(room_jid.userhost(), player, good_answer, score, profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
327
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
328 elif elt.name == 'timer_expired':
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
329 self.host.bridge.quizGameTimerExpired(room_jid.userhost(), profile)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
330
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
331 elif elt.name == 'timer_restarted':
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
332 self.host.bridge.quizGameTimerRestarted(room_jid.userhost(), int(elt['time_left']), profile)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
333
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
334 else:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
335 error(_('Unmanaged game element: %s') % elt.name)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
336
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
337 def getHandler(self, profile):
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
338 return QuizGameHandler(self)
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
339
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
340
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
341 class QuizGameHandler (XMPPHandler):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
342 implements(iwokkel.IDisco)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
343
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
344 def __init__(self, plugin_parent):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
345 self.plugin_parent = plugin_parent
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
346 self.host = plugin_parent.host
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
347
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
348 def connectionInitialized(self):
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
349 self.xmlstream.addObserver(QG_REQUEST, self.plugin_parent.quiz_game_cmd, profile=self.parent.profile)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
350
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
351 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
352 return [disco.DiscoFeature(NS_QG)]
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
353
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
354 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
355 return []