Mercurial > libervia-backend
diff src/plugins/plugin_misc_quiz.py @ 361:141eeb7cd9e6
Quizz game: first draft
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 12 Jun 2011 16:28:33 +0200 |
parents | |
children | 208107419b17 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/plugins/plugin_misc_quiz.py Sun Jun 12 16:28:33 2011 +0200 @@ -0,0 +1,312 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +""" +SAT plugin for managing Quiz game +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/>. +""" + +from logging import debug, info, warning, error +from twisted.words.xish import domish +from twisted.internet import protocol, defer, threads, reactor +from twisted.words.protocols.jabber import client, jid, xmlstream +from twisted.words.protocols.jabber import error as jab_error +from twisted.words.protocols.jabber.xmlstream import IQ +import random + +from zope.interface import implements + +from wokkel import disco, iwokkel, data_form +from sat.tools.xml_tools import dataForm2xml +from sat.tools.games import TarotCard + +from time import time + +try: + from twisted.words.protocols.xmlstream import XMPPHandler +except ImportError: + from wokkel.subprotocols import XMPPHandler + +MESSAGE = '/message' +NS_QG = 'http://www.goffi.org/protocol/quiz' +QG_TAG = 'quiz' +QG_REQUEST = MESSAGE + '/' + QG_TAG + '[@xmlns="' + NS_QG + '"]' + +PLUGIN_INFO = { +"name": "Quiz game plugin", +"import_name": "Quiz", +"type": "Game", +"protocols": [], +"dependencies": ["XEP-0045", "XEP-0249"], +"main": "Quiz", +"handler": "yes", +"description": _("""Implementation of Quiz game""") +} + + +class Quiz(): + + def __init__(self, host): + info(_("Plugin Quiz initialization")) + self.host = host + self.games={} + self.waiting_inv = {} #Invitation waiting for people to join to launch a game + host.bridge.addMethod("quizGameLaunch", ".communication", in_sign='ass', out_sign='', method=self.quizGameLaunch) #args: room_jid, players, profile + host.bridge.addMethod("quizGameCreate", ".communication", in_sign='sass', out_sign='', method=self.quizGameCreate) #args: room_jid, players, profile + host.bridge.addMethod("quizGameReady", ".communication", in_sign='sss', out_sign='', method=self.newPlayerReady) #args: player, referee, profile + host.bridge.addSignal("quizGameStarted", ".communication", signature='ssass') #args: room_jid, referee, players, profile + host.bridge.addSignal("quizGameNew", ".communication", + signature='sa{ss}s', + doc = { 'summary': 'Start a new game', + 'param_0': "jid of game's room", + 'param_1': "data of the game", + 'param_2': '%(doc_profile)s'}) + host.bridge.addSignal("quizGameQuestion", ".communication", + signature = 'sssis', + doc = { 'summary': "Send the current question", + 'param_0': "jid of game's room", + 'param_1': "question id", + 'param_2': "question to ask", + 'param_3': "timer", + 'param_4': '%(doc_profile)s'}) + host.trigger.add("MUC user joined", self.userJoinedTrigger) + + def createGameElt(self, to_jid, type="normal"): + type = "normal" if to_jid.resource else "groupchat" + elt = domish.Element(('jabber:client','message')) + elt["to"] = to_jid.full() + elt["type"] = type + elt.addElement((NS_QG, QG_TAG)) + return elt + + def __game_data_to_xml(self, game_data): + """Convert a game data dict to domish element""" + game_data_elt = domish.Element(('','game_data')) + for data in game_data: + data_elt = domish.Element(('',data)) + data_elt.addContent(game_data[data]) + game_data_elt.addChild(data_elt) + return game_data_elt + + def __xml_to_game_data(self, game_data_elt): + """Convert a domish element with game_data to a dict""" + game_data = {} + for data_elt in game_data_elt.elements(): + game_data[data_elt.name] = unicode(data_elt) + return game_data + + def __create_started_elt(self, players): + """Create a game_started domish element""" + started_elt = domish.Element(('','started')) + idx = 0 + for player in players: + player_elt = domish.Element(('','player')) + player_elt.addContent(player) + player_elt['index'] = str(idx) + idx+=1 + started_elt.addChild(player_elt) + return started_elt + + def __ask_question(self, question_id, question, timer=30): + """Create a element for asking a question""" + question_elt = domish.Element(('','question')) + question_elt['id'] = question_id + question_elt['timer'] = str(timer) + question_elt.addContent(question) + return question_elt + + def __start_play(self, room_jid, game_data, profile): + """Start the game (tell to the first player after dealer to play""" + game_data['stage'] = "play" + next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) #the player after the dealer start + game_data['first_player'] = next_player = game_data['players'][next_player_idx] + to_jid = jid.JID(room_jid.userhost()+"/"+next_player) #FIXME: gof: + mess = self.createGameElt(to_jid) + yourturn_elt = mess.firstChildElement().addElement('your_turn') + self.host.profiles[profile].xmlstream.send(mess) + + + def userJoinedTrigger(self, room, user, profile): + """This trigger is used to check if we are waiting people in this room, + and to create a game if everybody is here""" + _room_jid = room.occupantJID.userhostJID() + if _room_jid in self.waiting_inv and len(room.roster) == 4: + #When we have 4 people in the room, we create the game + #TODO: check people identity + players = room.roster.keys() + del self.waiting_inv[_room_jid] + self.quizGameCreate(_room_jid.userhost(), players, profile_key=profile) + return True + + def quizGameLaunch(self, players, profile_key='@DEFAULT@'): + """Launch a game: helper method to create a room, invite players, and create the quiz game + @param players: list for players jid""" + debug(_('Launching quiz game')) + profile = self.host.memory.getProfileName(profile_key) + if not profile: + error(_("Unknown profile")) + return + + def quizRoomJoined(room): + _room = room.occupantJID.userhostJID() + for player in players: + self.host.plugins["XEP-0249"].invite(jid.JID(player), room.occupantJID.userhostJID(), {"game":"Quiz"}, profile) + self.waiting_inv[_room] = (time(), players) #TODO: remove invitation waiting for too long, using the time data + + def after_init(ignore): + room_name = "sat_quiz_%s" % self.host.plugins["XEP-0045"].getUniqueName(profile_key) + print "\n\n===> room_name:", room_name + muc_service = None + for service in self.host.memory.getServerServiceEntities("conference", "text", profile): + if not ".irc." in service.userhost(): + #FIXME: + #This awfull ugly hack is here to avoid an issue with openfire: the irc gateway + #use "conference/text" identity (instead of "conference/irc"), there is certainly a better way + #to manage this, but this hack fill do it for test purpose + muc_service = service + break + if not muc_service: + error(_("Can't find a MUC service")) + return + + _jid, xmlstream = self.host.getJidNStream(profile) + d = self.host.plugins["XEP-0045"].join(muc_service.userhost(), room_name, _jid.user, profile).addCallback(quizRoomJoined) + + client = self.host.getClient(profile) + if not client: + error(_('No client for this profile key: %s') % profile_key) + return + client.client_initialized.addCallback(after_init) + + def quizGameCreate(self, room_jid_param, players, profile_key='@DEFAULT@'): + """Create a new game + @param room_jid_param: jid of the room + @param players: list of players nick (nick must exist in the room) + @param profile_key: %(doc_profile_key)s""" + debug (_("Creating Quiz game")) + room_jid = jid.JID(room_jid_param) + profile = self.host.memory.getProfileName(profile_key) + if not profile: + error (_("profile %s is unknown") % profile_key) + return + if self.games.has_key(room_jid): + warning (_("Quiz game already started in room %s") % room_jid.userhost()) + else: + room_nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid.userhost(), profile) + if not room_nick: + error ('Internal error') + return + referee = room_jid.userhost() + '/' + room_nick + status = {} + players_data = {} + for player in players: + players_data[player] = {'score':0} + status[player] = "init" + self.games[room_jid.userhost()] = {'referee':referee, 'players':players, 'status':status, 'players_data':players_data, 'stage': None} + for player in players: + mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player)) + mess.firstChildElement().addChild(self.__create_started_elt(players)) + self.host.profiles[profile].xmlstream.send(mess) + + def newPlayerReady(self, player, referee, profile_key='@DEFAULT@'): + """Must be called when player is ready to start a new game""" + profile = self.host.memory.getProfileName(profile_key) + if not profile: + error (_("profile %s is unknown") % profile_key) + return + debug ('new player ready: %s' % profile) + mess = self.createGameElt(jid.JID(referee)) + ready_elt = mess.firstChildElement().addElement('player_ready') + ready_elt['player'] = player + self.host.profiles[profile].xmlstream.send(mess) + + def askQuestion(self, room_jid, profile): + mess = self.createGameElt(room_jid) + mess.firstChildElement().addChild(self.__ask_question("1", u"Quel est l'âge du capitaine ?")) + self.host.profiles[profile].xmlstream.send(mess) + + def newGame(self, room_jid, profile): + """Launch a new round""" + debug (_('new Quiz game')) + game_data = self.games[room_jid.userhost()] + players = game_data['players'] + players_data = game_data['players_data'] + game_data['stage'] = "init" + + for player in players: + players_data[player]['game_score'] = 0 + + new_game_data = {"instructions": _(u"""Bienvenue dans cette partie rapide de quizz, le premier à atteindre le score de 9 remporte le jeu + +Attention, tu es prêt ?""")} + + mess = self.createGameElt(room_jid) + mess.firstChildElement().addChild(self.__game_data_to_xml(new_game_data)) + self.host.profiles[profile].xmlstream.send(mess) + reactor.callLater(10, self.askQuestion, room_jid, profile) + + def quiz_game_cmd(self, mess_elt, profile): + from_jid = jid.JID(mess_elt['from']) + room_jid = jid.JID(from_jid.userhost()) + game_elt = mess_elt.firstChildElement() + game_data = self.games[room_jid.userhost()] + players_data = game_data['players_data'] + + for elt in game_elt.elements(): + + if elt.name == 'started': #new game created + players = [] + for player in elt.elements(): + players.append(unicode(player)) + self.host.bridge.quizGameStarted(room_jid.userhost(), from_jid.full(), players, profile) + + elif elt.name == 'player_ready': #ready to play + player = elt['player'] + status = self.games[room_jid.userhost()]['status'] + nb_players = len(self.games[room_jid.userhost()]['players']) + status[player] = 'ready' + debug (_('Player %(player)s is ready to start [status: %(status)s]') % {'player':player, 'status':status}) + if status.values().count('ready') == nb_players: #everybody is ready, we can start the game + self.newGame(room_jid, profile) + + elif elt.name == 'game_data': + self.host.bridge.quizGameNew(room_jid.userhost(), self.__xml_to_game_data(elt), profile) + + elif elt.name == 'question': #A question is asked + self.host.bridge.quizGameQuestion(room_jid.userhost(), elt["id"], unicode(elt), int(elt["timer"]), profile ) + + else: + error (_('Unmanaged game element: %s') % elt.name) + + def getHandler(self, profile): + return QuizGameHandler(self) + +class QuizGameHandler (XMPPHandler): + implements(iwokkel.IDisco) + + def __init__(self, plugin_parent): + self.plugin_parent = plugin_parent + self.host = plugin_parent.host + + def connectionInitialized(self): + self.xmlstream.addObserver(QG_REQUEST, self.plugin_parent.quiz_game_cmd, profile = self.parent.profile) + + def getDiscoInfo(self, requestor, target, nodeIdentifier=''): + return [disco.DiscoFeature(NS_QG)] + + def getDiscoItems(self, requestor, target, nodeIdentifier=''): + return [] +