Quizz game: first draft

This commit is contained in:
Goffi 2011-06-12 16:28:33 +02:00
commit fb41ecbbf5
7 changed files with 539 additions and 2 deletions

View file

@ -176,6 +176,15 @@ class DBusBridgeFrontend(BridgeFrontend):
def tarotGamePlayCards(self, player, referee, cards, profile_key='@DEFAULT@'):
return self.db_comm_iface.tarotGamePlayCards(player, referee, cards, profile_key)
def quizGameLaunch(self, players, profile_key='@DEFAULT@'):
return self.db_comm_iface.quizGameLaunch(players, profile_key)
def quizGameCreate(self, room_jid, players, profile_key='@DEFAULT@'):
return self.db_comm_iface.quizGameCreate(room_jid, players, profile_key)
def quizGameReady(self, player, referee, profile_key='@DEFAULT@'):
return self.db_comm_iface.quizGameReady(player, referee, profile_key)
def sendFile(self, to, path, profile_key='@DEFAULT@'):
return self.db_comm_iface.sendFile(to, path, profile_key)

View file

@ -63,6 +63,9 @@ class QuickApp():
self.bridge.register("tarotGameScore", self.tarotScore)
self.bridge.register("tarotGameCardsPlayed", self.tarotCardsPlayed)
self.bridge.register("tarotGameInvalidCards", self.tarotInvalidCards)
self.bridge.register("quizGameStarted", self.quizGameStarted)
self.bridge.register("quizGameNew", self.quizGameNew)
self.bridge.register("quizGameQuestion", self.quizGameQuestion)
self.bridge.register("subscribe", self.subscribe)
self.bridge.register("paramUpdate", self.paramUpdate)
self.bridge.register("contactDeleted", self.contactDeleted)
@ -372,6 +375,30 @@ class QuickApp():
if self.chat_wins.has_key(room_jid):
self.chat_wins[room_jid].getGame("Tarot").invalidCards(phase, played_cards, invalid_cards)
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):
self.chat_wins[room_jid].startGame("Quiz", referee, players)
debug (_("new Quiz game started by [%(referee)s] in room [%(room_jid)s] with %(players)s") % {'referee':referee, 'room_jid':room_jid, 'players':[str(player) for player in players]})
def quizGameNew(self, room_jid, data, profile):
if not self.check_profile(profile):
return
debug (_("New Quiz Game"))
if self.chat_wins.has_key(room_jid):
self.chat_wins[room_jid].getGame("Quiz").quizGameNew(data)
def quizGameQuestion(self, room_jid, question_id, question, timer, profile):
"""Called when a new question is asked"""
if not self.check_profile(profile):
return
debug (_(u"Quiz: new question: %s") % question)
if self.chat_wins.has_key(room_jid):
self.chat_wins[room_jid].getGame("Quiz").quizGameQuestion(question_id, question, timer)
def _subscribe_cb(self, answer, data):
entity, profile = data
if answer:

View file

@ -30,6 +30,7 @@ from sat.tools.jid import JID
from sat_frontends.quick_frontend.quick_chat import QuickChat
from sat_frontends.wix.contact_list import ContactList
from sat_frontends.wix.card_game import CardPanel
from sat_frontends.wix.quiz_game import QuizPanel
idSEND = 1
@ -118,13 +119,21 @@ class Chat(wx.Frame, QuickChat):
self.sizer.Layout()
self.Fit()
self.splitter.UpdateSize()
elif game_type=="Quiz":
debug (_("configure chat window for Quiz game"))
self.quiz_panel = QuizPanel(self, referee, players, self.nick)
self.sizer.Prepend(self.quiz_panel, 0, flag=wx.EXPAND)
self.sizer.Layout()
self.Fit()
self.splitter.UpdateSize()
def getGame(self, game_type):
"""Return class managing the game type"""
#TODO: check that the game is launched, and manage errors
if game_type=="Tarot":
return self.tarot_panel
elif game_type=="Quiz":
return self.quiz_panel
def setPresents(self, nicks):
"""Set the users presents in the contact list for a group chat

View file

@ -0,0 +1,171 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
wix: a SAT frontend
Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import wx
import os.path, glob
import pdb
from logging import debug, info, error
from sat.tools.jid import JID
from time import time
from math import sin, cos, pi
CARD_WIDTH = 74
CARD_HEIGHT = 136
WIDTH = 800
HEIGHT = 600
class GraphicElement():
"""This class is used to represent a card, graphically and logically"""
def __init__(self, file, x=0, y=0, zindex=10, transparent=True):
""" Image used to build the game visual
@param file: path of the PNG file
@param zindex: layer of the element (0=background; the bigger, the more in the foreground)"""
self.bitmap = wx.Image(file).ConvertToBitmap()
self.x = x
self.y = y
self.zindex = zindex
self.transparent = transparent
def __cmp__(self, other):
return self.zindex.__cmp__(other.zindex)
def draw(self, dc, x=None, y=None):
"""Draw the card on the device context
@param dc: device context
@param x: abscissa
@param y: ordinate"""
dc.DrawBitmap(self.bitmap, x or self.x, y or self.y, self.transparent)
class BaseWindow(wx.Window):
"""This is the panel where the game is drawed, under the other widgets"""
def __init__(self, parent):
wx.Window.__init__(self, parent, pos=(0,0), size=(WIDTH, HEIGHT))
self.parent = parent
self.SetMinSize(wx.Size(WIDTH, HEIGHT))
self.Bind(wx.EVT_PAINT, self.onPaint)
self.graphic_elts = {}
self.loadImages("images/quiz/")
def loadImages(self, dir):
"""Load all the images needed for the game
@param dir: directory where the PNG files are"""
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),
("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)
def fullPaint(self, device_context):
"""Paint all the game on the given dc
@param device_context: wx.DC"""
elements = self.graphic_elts.values()
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
if self.parent.time_origin:
device_context.SetPen(wx.BLACK_PEN)
radius = 20
center_x = 760
center_y = 147
origin = self.parent.time_origin
current = 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)
class QuizPanel(wx.Panel):
"""This class is used to display the quiz game"""
def __init__(self, parent, referee, players, player_nick):
wx.Panel.__init__(self, parent)
self.time_origin = None #set to unix time when the timer start
self.time_limit = None
self.time_left = 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.parent.host.bridge.quizGameReady(player_nick, referee, profile_key = self.parent.host.profile)
self.state = None
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()
def quizGameNew(self, data):
"""Start a new game, with given hand"""
if data.has_key('instructions'):
self.question.ChangeValue(data['instructions'])
self.Refresh()
def quizGameQuestion(self, question_id, question, timer):
"""Called when a new question is available
@param question: question to ask"""
self.question.ChangeValue(question)
self.startTimer(timer)