Quiz game: buzzer, timer, answer management

This commit is contained in:
Goffi 2011-06-12 22:34:15 +02:00
commit bb255b7a3d
3 changed files with 322 additions and 37 deletions

View file

@ -185,6 +185,9 @@ class DBusBridgeFrontend(BridgeFrontend):
def quizGameReady(self, player, referee, profile_key='@DEFAULT@'):
return self.db_comm_iface.quizGameReady(player, referee, profile_key)
def quizGameAnswer(self, player, referee, answer, profile_key='@DEFAULT@'):
return self.db_comm_iface.quizGameAnswer(player, referee, answer, profile_key)
def sendFile(self, to, path, profile_key='@DEFAULT@'):
return self.db_comm_iface.sendFile(to, path, profile_key)

View file

@ -66,6 +66,11 @@ class QuickApp():
self.bridge.register("quizGameStarted", self.quizGameStarted)
self.bridge.register("quizGameNew", self.quizGameNew)
self.bridge.register("quizGameQuestion", self.quizGameQuestion)
self.bridge.register("quizGamePlayerBuzzed", self.quizGamePlayerBuzzed)
self.bridge.register("quizGamePlayerSays", self.quizGamePlayerSays)
self.bridge.register("quizGameAnswerResult", self.quizGameAnswerResult)
self.bridge.register("quizGameTimerExpired", self.quizGameTimerExpired)
self.bridge.register("quizGameTimerRestarted", self.quizGameTimerRestarted)
self.bridge.register("subscribe", self.subscribe)
self.bridge.register("paramUpdate", self.paramUpdate)
self.bridge.register("contactDeleted", self.contactDeleted)
@ -377,7 +382,6 @@ class QuickApp():
def quizGameStarted(self, room_jid, referee, players, profile):
if not self.check_profile(profile):
print "gof: NOT CHECK PROFILE", profile
return
debug (_("Quiz Game Started \o/"))
if self.chat_wins.has_key(room_jid):
@ -399,6 +403,41 @@ class QuickApp():
if self.chat_wins.has_key(room_jid):
self.chat_wins[room_jid].getGame("Quiz").quizGameQuestion(question_id, question, timer)
def quizGamePlayerBuzzed(self, room_jid, player, pause, profile):
"""Called when a player pushed the buzzer"""
if not self.check_profile(profile):
return
if self.chat_wins.has_key(room_jid):
self.chat_wins[room_jid].getGame("Quiz").quizGamePlayerBuzzed(player, pause)
def quizGamePlayerSays(self, room_jid, player, text, delay, profile):
"""Called when a player say something"""
if not self.check_profile(profile):
return
if self.chat_wins.has_key(room_jid):
self.chat_wins[room_jid].getGame("Quiz").quizGamePlayerSays(player, text, delay)
def quizGameAnswerResult(self, room_jid, player, good_answer, score, profile):
"""Called when a player say something"""
if not self.check_profile(profile):
return
if self.chat_wins.has_key(room_jid):
self.chat_wins[room_jid].getGame("Quiz").quizGameAnswerResult(player, good_answer, score)
def quizGameTimerExpired(self, room_jid, profile):
"""Called when nobody answered the question in time"""
if not self.check_profile(profile):
return
if self.chat_wins.has_key(room_jid):
self.chat_wins[room_jid].getGame("Quiz").quizGameTimerExpired()
def quizGameTimerRestarted(self, room_jid, time_left, profile):
"""Called when the question is not answered, and we still have time"""
if not self.check_profile(profile):
return
if self.chat_wins.has_key(room_jid):
self.chat_wins[room_jid].getGame("Quiz").quizGameTimerRestarted(time_left)
def _subscribe_cb(self, answer, data):
entity, profile = data
if answer:

View file

@ -71,14 +71,18 @@ class BaseWindow(wx.Window):
def loadImages(self, dir):
"""Load all the images needed for the game
@param dir: directory where the PNG files are"""
x_player = 24
for name, sub_dir, filename, x, y, zindex, transparent in [("background", "backgrounds", "blue_background.png", 0, 0, 0, False),
("joueur0", "characters/zombie", "zombie.png", 24, 170, 5, True),
("joueur1", "characters/nerd", "nerd2.png", 209, 170, 5, True),
("joueur2", "characters/zombie", "zombie.png", 392, 170, 5, True),
("joueur3", "characters/zombie", "zombie.png", 578, 170, 5, True),
("joueur0", "characters/zombie", "zombie.png", x_player+0*184, 170, 5, True),
("joueur1", "characters/nerd", "nerd2.png", x_player+1*184, 170, 5, True),
("joueur2", "characters/zombie", "zombie.png", x_player+2*184, 170, 5, True),
("joueur3", "characters/zombie", "zombie.png", x_player+3*184, 170, 5, True),
("foreground", "foreground", "foreground.png", 0, 0, 10, True)]:
self.graphic_elts[name] = GraphicElement(os.path.join(dir, sub_dir, filename), x = x, y = y, zindex=zindex, transparent=transparent)
self.right_image = wx.Image(os.path.join(dir, "right.png")).ConvertToBitmap()
self.wrong_image = wx.Image(os.path.join(dir, "wrong.png")).ConvertToBitmap()
def fullPaint(self, device_context):
"""Paint all the game on the given dc
@param device_context: wx.DC"""
@ -86,16 +90,18 @@ class BaseWindow(wx.Window):
elements.sort()
for elem in elements:
elem.draw(device_context)
#device_context.DrawArc(39,127, 39, 127,
_font = wx.Font(65, wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
device_context.SetFont(_font)
device_context.SetTextForeground(wx.BLACK)
x = 100
for score in [0,1,4,9]:
device_context.DrawText("%d" % score, x, 355)
x+=184
for i in range(4):
answer = self.parent.players_data[i]["answer"]
score = self.parent.players_data[i]["score"]
if answer == None:
device_context.DrawText("%d" % score, 100 + i*184, 355)
else:
device_context.DrawBitmap(self.right_image if answer else self.wrong_image, 39+i*184, 348, True)
if self.parent.time_origin:
@ -104,25 +110,18 @@ class BaseWindow(wx.Window):
center_x = 760
center_y = 147
origin = self.parent.time_origin
current = time()
current = self.parent.time_pause or time()
limit = self.parent.time_limit
print "limit:", limit
total = limit - origin
left = self.parent.time_left = max(0,limit - current)
device_context.SetBrush(wx.RED_BRUSH if left/total < 1/4.0 else wx.WHITE_BRUSH)
print "left:",left
if left:
#we now draw the timer
print "total - left:", total - left
angle = ((-2*pi)*((total-left)/total) + (pi/2))
print "angle:", angle*57.3
x = center_x + radius * cos(angle)
y = center_y - radius * sin(angle)
print "x: %s, y:%s" % (x, y)
device_context.DrawArc(center_x, center_y-radius, x, y, center_x, center_y)
def onPaint(self, event):
dc = wx.PaintDC(self)
self.fullPaint(dc)
@ -134,28 +133,62 @@ class QuizPanel(wx.Panel):
def __init__(self, parent, referee, players, player_nick):
wx.Panel.__init__(self, parent)
self.referee = referee
self.player_nick = player_nick
self.players = players
self.time_origin = None #set to unix time when the timer start
self.time_limit = None
self.time_left = None
self.time_pause = None
self.last_answer = None
self.parent = parent
self.SetMinSize(wx.Size(WIDTH, HEIGHT))
self.SetSize(wx.Size(WIDTH, HEIGHT))
self.base = BaseWindow(self)
self.question = wx.TextCtrl(self.base, -1, "", pos=(168,17), size=(613, 94), style=wx.TE_MULTILINE | wx.TE_READONLY)
self.reponse = wx.TextCtrl(self.base, -1, pos=(410,569), size=(342, 21), style=wx.TE_PROCESS_ENTER)
self.question = wx.TextCtrl(self, -1, pos=(168,17), size=(613, 94), style=wx.TE_MULTILINE | wx.TE_READONLY)
self.answer = wx.TextCtrl(self, -1, pos=(410,569), size=(342, 21), style=wx.TE_PROCESS_ENTER)
self.players_data = [{}, {}, {}, {}]
for i in range(4):
self.players_data[i]['bubble'] = wx.TextCtrl(self, -1, pos=(39+i*184, 120), size=(180, 56), style=wx.TE_MULTILINE | wx.TE_READONLY)
self.players_data[i]['bubble'].Hide()
self.players_data[i]['answer'] = None #True if the player gave a good answer
self.players_data[i]['score'] = 0
self.answer.Bind(wx.EVT_TEXT_ENTER, self.answered)
self.parent.host.bridge.quizGameReady(player_nick, referee, profile_key = self.parent.host.profile)
self.state = None
def answered(self, event):
"""Called when the player gave an answer in the box"""
self.last_answer = self.answer.GetValue()
self.answer.Clear()
if self.last_answer:
self.parent.host.bridge.quizGameAnswer(self.player_nick, self.referee, self.last_answer, profile_key = self.parent.host.profile)
def quizGameTimerExpired(self):
"""Called when nobody answered the question in time"""
self.question.SetValue(_(u"Quel dommage, personne n'a trouvé la réponse\n\nAttention, la prochaine question arrive..."))
def quizGameTimerRestarted(self, time_left):
"""Called when nobody answered the question in time"""
timer_orig = self.time_limit - self.time_origin
self.time_left = time_left
self.time_limit = time() + time_left
self.time_origin = self.time_limit - timer_orig
self.time_pause = None
self.__timer_refresh()
def startTimer(self, timer=60):
"""Start the timer to answer the question"""
def _refresh():
self.Refresh()
if self.time_left:
wx.CallLater(1000, _refresh)
self.time_left = timer
self.time_origin = time()
self.time_limit = self.time_origin + timer
_refresh()
self.time_pause = None
self.__timer_refresh()
def __timer_refresh(self):
self.Refresh()
if self.time_left:
wx.CallLater(1000, self.__timer_refresh)
def quizGameNew(self, data):
"""Start a new game, with given hand"""
@ -168,4 +201,43 @@ class QuizPanel(wx.Panel):
@param question: question to ask"""
self.question.ChangeValue(question)
self.startTimer(timer)
self.last_answer = None
self.answer.Clear()
def quizGamePlayerBuzzed(self, player, pause):
"""Called when the player pushed the buzzer
@param player: player who pushed the buzzer
@param pause: should we stop the timer ?"""
if pause:
self.time_pause = time()
def quizGamePlayerSays(self, player, text, delay):
"""Called when the player says something
@param player: who is talking
@param text: what the player says"""
if player != self.player_nick and self.last_answer:
#if we are not the player talking, and we have an answer, that mean that our answer has not been validated
#we can put it again in the answering box
self.answer.SetValue(self.last_answer)
idx = self.players.index(player)
bubble = self.players_data[idx]['bubble']
bubble.SetValue(text)
bubble.Show()
self.Refresh()
wx.CallLater(delay * 1000, bubble.Hide)
def quizGameAnswerResult(self, player, good_answer, score):
"""Result of the just given answer
@param player: who gave the answer
@good_answer: True if the answer is right
@score: dict of score"""
player_idx = self.players.index(player)
self.players_data[player_idx]['answer'] = good_answer
for _player in score:
_idx = self.players.index(_player)
self.players_data[_idx]['score'] = score[_player]
def removeAnswer():
self.players_data[player_idx]['answer'] = None
self.Refresh()
wx.CallLater(2000, removeAnswer)
self.Refresh()