def handle_heartbeat(message): match = match_driver.get_match(current_user.user_id) msg_len = match.get_message_queue_length(current_user.user_id) if msg_len > 30: return "QUE FULL" match.send_message_from(current_user.user_id, message['msg_type'], message['msg_data']) return "OK"
def handle_chat(message): match = match_driver.get_match(current_user.user_id) if match.is_over: return {'result': False} msg_type = message['msg_type'] msg_data = message['msg_data'] match.send_message_from(current_user.user_id, msg_type, msg_data) return {'result': True}
def force_to_stop(): match = match_driver.get_match(current_user.user_id) if match.is_over: return {'result': False} msg_type = MSG_TYPE_MATCHEND msg_data = {'winner': match.player_color.value, 'reason': 'offline'} for player_uid in match.player_uids: match.send_message_from(player_uid, msg_type, msg_data) return {'result': True}
def view_match(): try: match = match_driver.get_match(current_user.user_id) except exceptions.NoMatchFoundException: return redirect(url_for('site.index')) match_msg_form = forms.MessageForm() return render_template('site/play.html', match=match, OFFLINE_TTL=settings.OFFLINE_TTL, match_msg_form=match_msg_form)
def handle_s2c(): try: match = match_driver.get_match(current_user.user_id) except exceptions.NoMatchFoundException: return None message = match.receive_message_to(current_user.user_id) handlers = {MSG_TYPE_MATCHEND: handle_matchend} msg_handler = handlers.get(message['msg_type']) if msg_handler: msg_handler(message) print("handle_s2c to {}: {}".format(current_user.user_id, message)) return message
def handle_reply_draw_request(message): msg_data = json.loads(message['msg_data']) match = match_driver.get_match(current_user.user_id) if not msg_data or not msg_data.get('approved'): msg_data = {'approved': False} if msg_data['approved']: msg_type = MSG_TYPE_MATCHEND msg_data = {'winner': None, 'reason': 'draw by agreement'} for player_uid in match.player_uids: match.send_message_from(player_uid, msg_type, msg_data) else: match.send_message_from(current_user.user_id, MSG_TYPE_REPLYDRAWREQ, msg_data) return msg_data
def handle_leave(message): match = match_driver.get_match(current_user.user_id) if match.is_over: return {'result': False} msg_type = MSG_TYPE_MATCHEND msg_data = { 'winner': [color.value for color in ChessColor if color != match.player_color][0], 'reason': 'abort' } for player_uid in match.player_uids: match.send_message_from(player_uid, msg_type, msg_data) return {'result': True}
def handle_chessmove(message): msg_type = message['msg_type'] msg_data = json.loads(message['msg_data']) match = match_driver.get_match(current_user.user_id) if match.is_over: killed_chessman = None else: killed_chessman = match.move(msg_data['src'], msg_data['dst']) match.send_message_from(current_user.user_id, msg_type, msg_data) rval = {'chessboard': str(match.chessboard)} if killed_chessman and killed_chessman.role == ChessRole.SHUAI: msg_type = MSG_TYPE_MATCHEND msg_data = {'winner': match.player_color.value, 'reason': 'checkmate'} for player_uid in match.player_uids: match.send_message_from(player_uid, msg_type, msg_data) return rval
def handle_draw_request(message): match = match_driver.get_match(current_user.user_id) if match.is_over: return {'result': False} # If your draw request within the past 60s was not approved, by the ohter # player, then you can't request again. can_request = MatchDB.set('drawreq', "{}-{}".format(current_user.user_id, match.match_id), 'requesting', nx=True, ex=60) if not can_request: return {'result': False} msg_type = message['msg_type'] msg_data = message['msg_data'] match.send_message_from(current_user.user_id, msg_type, msg_data) return {'result': True}
def handle_reply_undo_request(message): msg_data = json.loads(message['msg_data']) match = match_driver.get_match(current_user.user_id) if not msg_data or not msg_data.get('approved'): msg_data = {'approved': False} match.send_message_from(current_user.user_id, MSG_TYPE_REPLYUNDOREQ, msg_data) return msg_data try: lock, chessboard = match.lock_and_get_chessboard() if not chessboard: match.send_message_from(current_user.user_id, MSG_TYPE_REPLYUNDOREQ, msg_data) return msg_data step = chessboard.undo() if not step: match.send_message_from(current_user.user_id, MSG_TYPE_REPLYUNDOREQ, msg_data) return msg_data match.chessboard = chessboard step['undone_color'] = Chessman.id2color(step['chess_id']).value kill_chess_id = step['kill_chess_id'] if kill_chess_id is not None: killed_color = Chessman.id2color(kill_chess_id) step['killed_color'] = killed_color.value step['killed_char'] = Chessman.role2char( Chessman.id2role(kill_chess_id), color=killed_color) step['killed_pic'] = Chessman.role2pic( Chessman.id2role(kill_chess_id), color=killed_color) msg_data = {'approved': True, 'step': step} match.send_message_from(current_user.user_id, MSG_TYPE_REPLYUNDOREQ, msg_data) return msg_data finally: MatchDB.delete( 'undoreq', "{}-{}".format(match.another_player_uid, match.match_id)) lock.release()