当前位置: 首页>>代码示例>>Python>>正文


Python Connection.receive_message方法代码示例

本文整理汇总了Python中connection.Connection.receive_message方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.receive_message方法的具体用法?Python Connection.receive_message怎么用?Python Connection.receive_message使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在connection.Connection的用法示例。


在下文中一共展示了Connection.receive_message方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import receive_message [as 别名]
class Game:
    def __init__(self, **kwargs):
        self.index = kwargs.get('index')
        self.game_id = kwargs.get('game_id')
        self.nickname = kwargs.get('nickname', None)
        self.prefix = kwargs.get('prefix', None)

    def start(self):
        self.connection = Connection(self.index, self)
        self.connection.start_game()

    def game_completed(self, results):
        logging.info(
            'Bot %d; Game completed. Nickname: %s. Ranked #%d of %d. %d of %d correct answers' % (
                self.index, self.nickname, results['rank'], results['playerCount'],
                results['correctCount'], len(results['answers'])
            )
        )

        self.connection.stop_ping()

    def get_next_question(self):
        while True:
            result = self.connection.receive_message('/service/player')

            if 'data' in result:
                data = result['data']
            else:
                continue

            if 'id' in data:
                awaiting_question = data['id'] == 1
            else:
                continue

            if 'content' in data:
                content = json.loads(data['content'])
            else:
                continue

            if 'playerCount' in content:
                self.game_completed(content)

                return None

            if ('quizQuestionAnswers' not in content or 'questionIndex' not in content or
                    'answerMap' not in content):
                continue

            quiz_question_answers = content['quizQuestionAnswers']
            question_index = content['questionIndex']
            answers = content['answerMap']

            if question_index >= len(quiz_question_answers) or question_index < 0:
                logging.error(
                    'Bot %d; Invalid question index; %s' % (self.index, str(question_index))
                )

                return question_index >= len(quiz_question_answers)

            number_of_answers = quiz_question_answers[question_index]

            return Question(awaiting_question, question_index, number_of_answers, answers)

    def answer_question(self, answer_index):
        self.connection.send_message('/service/controller', {
            'data': {
                'id': 6,
                'type': 'message',
                'gameid': self.game_id,
                'host': 'kahoot.it',
                'content': json.dumps({
                    'choice': answer_index,
                    'meta': {
                        'lag': 22,
                        'device': {
                            'userAgent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) ' +
                                         'Gecko/20100101 Firefox/48.0',
                            'screen': {
                                'width': 1920,
                                'height': 1080,
                            }
                        }
                    }
                })
            }
        })

        result = self.connection.receive_message('/service/controller')

        if not result['successful']:
            logging.error('Bot %d; Was not able to answer question' % self.index)
开发者ID:TheSimoms,项目名称:Kaloot,代码行数:94,代码来源:game.py

示例2: GameManager

# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import receive_message [as 别名]

#.........这里部分代码省略.........
			text_port = self.find_widget('text_port')
			text_name = self.find_widget('text_name')

			ip_address = text_ip.document.text
			port_num = int(text_port.document.text)
			name = text_name.document.text
		else:
			#debug mode
			text_ip = "127.0.0.1"
			#text_ip = "192.168.254.103"
			text_port = "8080"
			text_name = "DebugX"

			ip_address = text_ip
			port_num = int(text_port)
			name = text_name
			
			
		#attributes
		player_class = choice(Resources.types)
		player_x,player_y = randint(5+34,Resources.window_width-(5+34)),randint(5+34,Resources.window_height-(5+34))
		player_actual_name = name

		#connect to server
		self.my_connection.join_server((ip_address,port_num))

		self.me.set_data(player_class,player_actual_name,"temp")
		self.me.x,self.me.y = player_x,player_y
		
		#register player to server
		print "me:",self.me.get()
		print "Sending player..."
		if self.my_connection.send_message(self.me.get()) is None:
			self.me.name = self.my_connection.receive_message()
			print "Complete!"
		else:
			print "Error!"

		#socket details
		print "IP Address:",ip_address
		print "Port:",port_num
		print "Name:",name

	def set_info_bar(self):
		info_bar = self.find_widget('info_bar')
		info_bar.opacity = 185
		thumbnail = self.find_widget('thumbnail')
		thumbnail.image = Resources.sprites['thumb_'+self.me.type]
		player_name = self.find_label('player_name')
		player_name.text = self.me.actual_name

	#Utilities
	def set_client(self,player):
		self.me = player

	def set_window(self,window):
		self.window = window

	def set_media(self,media):
		self.media = media
		self.media.volume = 0.75
		self.media.eos_action = self.media.EOS_LOOP
		self.media.play()

	def set_focus(self,focus):
		if self.focus:
开发者ID:nmcalabroso,项目名称:Push,代码行数:70,代码来源:manager.py


注:本文中的connection.Connection.receive_message方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。