core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly

This commit is contained in:
Goffi 2017-02-04 17:59:13 +01:00
commit 6467bdb3c3
14 changed files with 54 additions and 58 deletions

View file

@ -22,13 +22,8 @@ from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
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 import jabber
from twisted.words.protocols.jabber.xmlstream import IQ
import random
from wokkel import data_form
from twisted.internet import reactor
from twisted.words.protocols.jabber import client as jabber_client, jid
from time import time
@ -169,35 +164,34 @@ class Quiz(object):
def __start_play(self, room_jid, game_data, profile):
"""Start the game (tell to the first player after dealer to play"""
client = self.host.getClient(profile)
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)
mess = self.createGameElt(to_jid)
yourturn_elt = mess.firstChildElement().addElement('your_turn')
self.host.profiles[profile].xmlstream.send(mess)
mess.firstChildElement().addElement('your_turn')
client.send(mess)
def playerAnswer(self, player, referee, answer, profile_key=C.PROF_KEY_NONE):
"""Called when a player give an answer"""
profile = self.host.memory.getProfileName(profile_key)
if not profile:
log.error(_(u"profile %s is unknown") % profile_key)
return
log.debug(u'new player answer (%(profile)s): %(answer)s' % {'profile': profile, 'answer': answer})
client = self.host.getClient(profile_key)
log.debug(u'new player answer (%(profile)s): %(answer)s' % {'profile': client.profile, 'answer': answer})
mess = self.createGameElt(jid.JID(referee))
answer_elt = mess.firstChildElement().addElement('player_answer')
answer_elt['player'] = player
answer_elt.addContent(answer)
self.host.profiles[profile].xmlstream.send(mess)
client.send(mess)
def timerExpired(self, room_jid, profile):
"""Called when nobody answered the question in time"""
client = self.host.getClient(profile)
game_data = self.games[room_jid]
game_data['stage'] = 'expired'
mess = self.createGameElt(room_jid)
expired_elt = mess.firstChildElement().addElement('timer_expired')
self.host.profiles[profile].xmlstream.send(mess)
reactor.callLater(4, self.askQuestion, room_jid, profile)
mess.firstChildElement().addElement('timer_expired')
client.send(mess)
reactor.callLater(4, self.askQuestion, room_jid, client.profile)
def pauseTimer(self, room_jid):
"""Stop the timer and save the time left"""
@ -210,12 +204,13 @@ class Quiz(object):
def restartTimer(self, room_jid, profile):
"""Restart a timer with the saved time"""
client = self.host.getClient(profile)
game_data = self.games[room_jid]
assert game_data['time_left'] is not None
mess = self.createGameElt(room_jid)
restarted_elt = mess.firstChildElement().addElement('timer_restarted')
restarted_elt["time_left"] = str(game_data['time_left'])
self.host.profiles[profile].xmlstream.send(mess)
mess.firstChildElement().addElement('timer_restarted')
jabber_client.restarted_elt["time_left"] = str(game_data['time_left'])
client.send(mess)
game_data["timer"] = reactor.callLater(game_data['time_left'], self.timerExpired, room_jid, profile)
game_data["time_left"] = None
game_data['stage'] = game_data['previous_stage']
@ -223,18 +218,20 @@ class Quiz(object):
def askQuestion(self, room_jid, profile):
"""Ask a new question"""
client = self.host.getClient(profile)
game_data = self.games[room_jid]
game_data['stage'] = "question"
game_data['question_id'] = "1"
timer = 30
mess = self.createGameElt(room_jid)
mess.firstChildElement().addChild(self.__ask_question(game_data['question_id'], u"Quel est l'âge du capitaine ?", timer))
self.host.profiles[profile].xmlstream.send(mess)
client.send(mess)
game_data["timer"] = reactor.callLater(timer, self.timerExpired, room_jid, profile)
game_data["time_left"] = None
def checkAnswer(self, room_jid, player, answer, profile):
"""Check if the answer given is right"""
client = self.host.getClient(profile)
game_data = self.games[room_jid]
players_data = game_data['players_data']
good_answer = game_data['question_id'] == "1" and answer == "42"
@ -243,7 +240,7 @@ class Quiz(object):
mess = self.createGameElt(room_jid)
mess.firstChildElement().addChild(self.__answer_result(player, good_answer, game_data))
self.host.profiles[profile].xmlstream.send(mess)
client.send(mess)
if good_answer:
reactor.callLater(4, self.askQuestion, room_jid, profile)
@ -261,12 +258,13 @@ Attention, tu es prêt ?""")}
reactor.callLater(10, self.askQuestion, room_jid, profile)
def room_game_cmd(self, mess_elt, profile):
client = self.host.getClient(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]
if 'players_data' in game_data:
players_data = game_data['players_data']
# if 'players_data' in game_data:
#  players_data = game_data['players_data']
for elt in game_elt.elements():
@ -299,7 +297,7 @@ Attention, tu es prêt ?""")}
buzzer_elt = mess.firstChildElement().addElement('player_buzzed')
buzzer_elt['player'] = player
buzzer_elt['pause'] = str(pause)
self.host.profiles[profile].xmlstream.send(mess)
client.send(mess)
if pause:
self.pauseTimer(room_jid)
# and we send the player answer
@ -309,7 +307,7 @@ Attention, tu es prêt ?""")}
say_elt['player'] = player
say_elt.addContent(_answer)
say_elt['delay'] = "3"
reactor.callLater(2, self.host.profiles[profile].xmlstream.send, mess)
reactor.callLater(2, client.send, mess)
reactor.callLater(6, self.checkAnswer, room_jid, player, _answer, profile=profile)
elif elt.name == 'player_buzzed':