Mercurial > libervia-backend
annotate frontends/src/wix/quiz_game.py @ 555:1d58758689e9
primitivus, wix: fixed bad defaults values for XMLUI class
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 09 Dec 2012 23:25:51 +0100 |
parents | 2a072735e459 |
children | ca13633d3b6b |
rev | line source |
---|---|
361 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 wix: a SAT frontend | |
459 | 6 Copyright (C) 2009, 2010, 2011, 2012 Jérôme Poisson (goffi@goffi.org) |
361 | 7 |
8 This program is free software: you can redistribute it and/or modify | |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
9 it under the terms of the GNU Affero General Public License as published by |
361 | 10 the Free Software Foundation, either version 3 of the License, or |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
16 GNU Affero General Public License for more details. |
361 | 17 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
18 You should have received a copy of the GNU Affero General Public License |
361 | 19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 """ | |
21 | |
22 | |
23 | |
24 import wx | |
25 import os.path, glob | |
26 import pdb | |
27 from logging import debug, info, error | |
28 from sat.tools.jid import JID | |
29 from time import time | |
30 from math import sin, cos, pi | |
31 | |
32 CARD_WIDTH = 74 | |
33 CARD_HEIGHT = 136 | |
34 WIDTH = 800 | |
35 HEIGHT = 600 | |
36 | |
37 class GraphicElement(): | |
38 """This class is used to represent a card, graphically and logically""" | |
39 | |
40 def __init__(self, file, x=0, y=0, zindex=10, transparent=True): | |
41 """ Image used to build the game visual | |
42 @param file: path of the PNG file | |
43 @param zindex: layer of the element (0=background; the bigger, the more in the foreground)""" | |
44 self.bitmap = wx.Image(file).ConvertToBitmap() | |
45 self.x = x | |
46 self.y = y | |
47 self.zindex = zindex | |
48 self.transparent = transparent | |
49 | |
50 def __cmp__(self, other): | |
51 return self.zindex.__cmp__(other.zindex) | |
52 | |
53 def draw(self, dc, x=None, y=None): | |
54 """Draw the card on the device context | |
55 @param dc: device context | |
56 @param x: abscissa | |
57 @param y: ordinate""" | |
58 dc.DrawBitmap(self.bitmap, x or self.x, y or self.y, self.transparent) | |
59 | |
60 class BaseWindow(wx.Window): | |
61 """This is the panel where the game is drawed, under the other widgets""" | |
62 | |
63 def __init__(self, parent): | |
64 wx.Window.__init__(self, parent, pos=(0,0), size=(WIDTH, HEIGHT)) | |
65 self.parent = parent | |
66 self.SetMinSize(wx.Size(WIDTH, HEIGHT)) | |
67 self.Bind(wx.EVT_PAINT, self.onPaint) | |
68 self.graphic_elts = {} | |
368 | 69 self.loadImages(os.path.join(parent.parent.host.media_dir, 'games/quiz/')) |
361 | 70 |
71 def loadImages(self, dir): | |
72 """Load all the images needed for the game | |
73 @param dir: directory where the PNG files are""" | |
362
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
74 x_player = 24 |
368 | 75 for name, sub_dir, filename, x, y, zindex, transparent in [("background", "background", "blue_background.png", 0, 0, 0, False), |
76 ("joueur0", "characters", "zombie.png", x_player+0*184, 170, 5, True), | |
77 ("joueur1", "characters", "nerd.png", x_player+1*184, 170, 5, True), | |
78 ("joueur2", "characters", "zombie.png", x_player+2*184, 170, 5, True), | |
79 ("joueur3", "characters", "zombie.png", x_player+3*184, 170, 5, True), | |
361 | 80 ("foreground", "foreground", "foreground.png", 0, 0, 10, True)]: |
81 self.graphic_elts[name] = GraphicElement(os.path.join(dir, sub_dir, filename), x = x, y = y, zindex=zindex, transparent=transparent) | |
82 | |
368 | 83 self.right_image = wx.Image(os.path.join(dir, "foreground", "right.png")).ConvertToBitmap() |
84 self.wrong_image = wx.Image(os.path.join(dir, "foreground", "wrong.png")).ConvertToBitmap() | |
362
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
85 |
361 | 86 def fullPaint(self, device_context): |
87 """Paint all the game on the given dc | |
88 @param device_context: wx.DC""" | |
89 elements = self.graphic_elts.values() | |
90 elements.sort() | |
91 for elem in elements: | |
92 elem.draw(device_context) | |
93 | |
94 _font = wx.Font(65, wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL) | |
95 device_context.SetFont(_font) | |
96 device_context.SetTextForeground(wx.BLACK) | |
97 | |
362
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
98 for i in range(4): |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
99 answer = self.parent.players_data[i]["answer"] |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
100 score = self.parent.players_data[i]["score"] |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
101 if answer == None: |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
102 device_context.DrawText("%d" % score, 100 + i*184, 355) |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
103 else: |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
104 device_context.DrawBitmap(self.right_image if answer else self.wrong_image, 39+i*184, 348, True) |
361 | 105 |
106 | |
107 if self.parent.time_origin: | |
108 device_context.SetPen(wx.BLACK_PEN) | |
109 radius = 20 | |
110 center_x = 760 | |
111 center_y = 147 | |
112 origin = self.parent.time_origin | |
362
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
113 current = self.parent.time_pause or time() |
361 | 114 limit = self.parent.time_limit |
115 total = limit - origin | |
116 left = self.parent.time_left = max(0,limit - current) | |
117 device_context.SetBrush(wx.RED_BRUSH if left/total < 1/4.0 else wx.WHITE_BRUSH) | |
118 if left: | |
119 #we now draw the timer | |
120 angle = ((-2*pi)*((total-left)/total) + (pi/2)) | |
121 x = center_x + radius * cos(angle) | |
122 y = center_y - radius * sin(angle) | |
123 device_context.DrawArc(center_x, center_y-radius, x, y, center_x, center_y) | |
124 | |
125 def onPaint(self, event): | |
126 dc = wx.PaintDC(self) | |
127 self.fullPaint(dc) | |
128 | |
129 | |
130 | |
131 class QuizPanel(wx.Panel): | |
132 """This class is used to display the quiz game""" | |
133 | |
134 def __init__(self, parent, referee, players, player_nick): | |
135 wx.Panel.__init__(self, parent) | |
362
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
136 self.referee = referee |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
137 self.player_nick = player_nick |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
138 self.players = players |
361 | 139 self.time_origin = None #set to unix time when the timer start |
140 self.time_limit = None | |
141 self.time_left = None | |
362
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
142 self.time_pause = None |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
143 self.last_answer = None |
361 | 144 self.parent = parent |
145 self.SetMinSize(wx.Size(WIDTH, HEIGHT)) | |
146 self.SetSize(wx.Size(WIDTH, HEIGHT)) | |
147 self.base = BaseWindow(self) | |
362
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
148 self.question = wx.TextCtrl(self, -1, pos=(168,17), size=(613, 94), style=wx.TE_MULTILINE | wx.TE_READONLY) |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
149 self.answer = wx.TextCtrl(self, -1, pos=(410,569), size=(342, 21), style=wx.TE_PROCESS_ENTER) |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
150 self.players_data = [{}, {}, {}, {}] |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
151 for i in range(4): |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
152 self.players_data[i]['bubble'] = wx.TextCtrl(self, -1, pos=(39+i*184, 120), size=(180, 56), style=wx.TE_MULTILINE | wx.TE_READONLY) |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
153 self.players_data[i]['bubble'].Hide() |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
154 self.players_data[i]['answer'] = None #True if the player gave a good answer |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
155 self.players_data[i]['score'] = 0 |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
156 self.answer.Bind(wx.EVT_TEXT_ENTER, self.answered) |
361 | 157 self.parent.host.bridge.quizGameReady(player_nick, referee, profile_key = self.parent.host.profile) |
158 self.state = None | |
362
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
159 |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
160 def answered(self, event): |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
161 """Called when the player gave an answer in the box""" |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
162 self.last_answer = self.answer.GetValue() |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
163 self.answer.Clear() |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
164 if self.last_answer: |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
165 self.parent.host.bridge.quizGameAnswer(self.player_nick, self.referee, self.last_answer, profile_key = self.parent.host.profile) |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
166 |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
167 def quizGameTimerExpired(self): |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
168 """Called when nobody answered the question in time""" |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
169 self.question.SetValue(_(u"Quel dommage, personne n'a trouvé la réponse\n\nAttention, la prochaine question arrive...")) |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
170 |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
171 def quizGameTimerRestarted(self, time_left): |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
172 """Called when nobody answered the question in time""" |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
173 timer_orig = self.time_limit - self.time_origin |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
174 self.time_left = time_left |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
175 self.time_limit = time() + time_left |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
176 self.time_origin = self.time_limit - timer_orig |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
177 self.time_pause = None |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
178 self.__timer_refresh() |
361 | 179 |
180 def startTimer(self, timer=60): | |
181 """Start the timer to answer the question""" | |
182 self.time_left = timer | |
183 self.time_origin = time() | |
184 self.time_limit = self.time_origin + timer | |
362
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
185 self.time_pause = None |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
186 self.__timer_refresh() |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
187 |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
188 def __timer_refresh(self): |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
189 self.Refresh() |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
190 if self.time_left: |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
191 wx.CallLater(1000, self.__timer_refresh) |
361 | 192 |
193 def quizGameNew(self, data): | |
194 """Start a new game, with given hand""" | |
195 if data.has_key('instructions'): | |
196 self.question.ChangeValue(data['instructions']) | |
197 self.Refresh() | |
198 | |
199 def quizGameQuestion(self, question_id, question, timer): | |
200 """Called when a new question is available | |
201 @param question: question to ask""" | |
202 self.question.ChangeValue(question) | |
203 self.startTimer(timer) | |
362
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
204 self.last_answer = None |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
205 self.answer.Clear() |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
206 |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
207 def quizGamePlayerBuzzed(self, player, pause): |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
208 """Called when the player pushed the buzzer |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
209 @param player: player who pushed the buzzer |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
210 @param pause: should we stop the timer ?""" |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
211 if pause: |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
212 self.time_pause = time() |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
213 |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
214 def quizGamePlayerSays(self, player, text, delay): |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
215 """Called when the player says something |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
216 @param player: who is talking |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
217 @param text: what the player says""" |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
218 if player != self.player_nick and self.last_answer: |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
219 #if we are not the player talking, and we have an answer, that mean that our answer has not been validated |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
220 #we can put it again in the answering box |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
221 self.answer.SetValue(self.last_answer) |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
222 idx = self.players.index(player) |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
223 bubble = self.players_data[idx]['bubble'] |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
224 bubble.SetValue(text) |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
225 bubble.Show() |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
226 self.Refresh() |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
227 wx.CallLater(delay * 1000, bubble.Hide) |
361 | 228 |
362
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
229 def quizGameAnswerResult(self, player, good_answer, score): |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
230 """Result of the just given answer |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
231 @param player: who gave the answer |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
232 @good_answer: True if the answer is right |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
233 @score: dict of score""" |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
234 player_idx = self.players.index(player) |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
235 self.players_data[player_idx]['answer'] = good_answer |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
236 for _player in score: |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
237 _idx = self.players.index(_player) |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
238 self.players_data[_idx]['score'] = score[_player] |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
239 def removeAnswer(): |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
240 self.players_data[player_idx]['answer'] = None |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
241 self.Refresh() |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
242 wx.CallLater(2000, removeAnswer) |
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
243 self.Refresh() |