def login(conn, username, password): data = chatlib.join_data([username, password]) cmd, server_msg = build_send_recv_parse( conn, chatlib.PROTOCOL_CLIENT["login_msg"], data) if cmd == chatlib.PROTOCOL_SERVER["login_ok_msg"]: return cmd, False return server_msg, True
def register(conn, username, password): data = chatlib.join_data([username, password]) cmd, server_msg = build_send_recv_parse( conn, chatlib.PROTOCOL_CLIENT["register_msg"], data) if cmd == chatlib.PROTOCOL_SERVER["register_ok_msg"]: return cmd return server_msg
def handle_question_message(conn): cmd = chatlib.PROTOCOL_SERVER["question_msg"] data = get_random_question(conn) if data != ERROR_MSG: build_and_send_message(conn, cmd, chatlib.join_data(data)) else: build_and_send_message(conn, chatlib.PROTOCOL_SERVER["no_questions_msg"], "")
def get_question_answer_print(conn, question_id): answer = input("choice your answer: ") answer = chatlib.join_data([question_id, answer]) code, data = build_send_recv_parse(conn, chatlib.PROTOCOL_CLIENT["send_answer"], answer) if code == chatlib.PROTOCOL_SERVER["error_msg"]: return str("ERROR get question") elif code == chatlib.PROTOCOL_SERVER["correct_answer_msg"]: return str("CORRECT ANSWER!!!") elif code == chatlib.PROTOCOL_SERVER["wrong_answer_msg"]: return str("WRONG ANSWER!!! :( \nThe correct answer is: " + data)
def handle_question_massage(conn): global questions global logged_users flag = False username = logged_users[conn.getpeername()] qarr = users[username].get("questions_asked") qnum = 0 for question in questions: #print(question, '<->', qarr) if question not in qarr: flag =True qnum = question break if flag == False: send_error(conn, str(username + " you answered all questions")) else: ans = questions[qnum].get("answers") strans = chatlib.join_data(ans) arr = [str(qnum), questions[qnum].get("question"), strans] msg = chatlib.join_data(arr) build_and_send_message(conn, 'YOUR_QUESTION', msg)
def login(): global conn, top, root username = top.usernameEntry.get() password = top.passwordEntry.get() data = chatlib.join_data([username, password]) cmd, server_msg = build_send_recv_parse( chatlib.PROTOCOL_CLIENT["login_msg"], data) top.msgLabel.configure(text=server_msg) if cmd == chatlib.PROTOCOL_SERVER["login_ok_msg"]: top.msgLabel.configure(text=cmd) root.destroy() from gui import trivia2 trivia2.vp_start_gui(conn)
def register(): global conn, top username = top.usernameEntry.get() password = top.passwordEntry.get() if len(username) > 2 and len(password) > 2: data = chatlib.join_data([username, password]) cmd, server_msg = build_send_recv_parse( chatlib.PROTOCOL_CLIENT["register_msg"], data) top.msgLabel.configure(text=server_msg) if cmd == chatlib.PROTOCOL_SERVER["register_ok_msg"]: top.msgLabel.configure(text=cmd) else: top.msgLabel.configure( text= "Please enter a username and password of at least 3 characters each" )
def login(conn): while True: username = input("Please enter username: \t") password = input("Please enter password: \t") # Implement code txt = chatlib.join_data([username, password]) build_and_send_message(conn, chatlib.PROTOCOL_CLIENT["login_msg"], txt) # Implement code retcmd, retdata = recv_message_and_parse(conn) print(retcmd) if retcmd == "LOGIN_OK": print("logined sucesfully") break print("login failed, try again")
def create_random_question(username): global users questions_id = list(questions.keys()) questions_id_asked = users[username]["questions_asked"] questions_id_removed_asked = [ question_id for question_id in questions_id if str(question_id) not in questions_id_asked ] if questions_id_removed_asked == []: return None random_key = questions_id_removed_asked[random.randint( 0, len(questions_id_removed_asked) - 1)] question = questions[random_key]["question"] answers = questions[random_key]["answers"] formatted_question = chatlib.join_data([random_key] + [question] + answers) edit_user_value(username, "questions_asked", random_key) print("\n", formatted_question, random_key) return formatted_question
def create_random_question(username): """ The function generates new question specific to the user :param username: player's name :return:question message and question id :rtype: tuple """ global questions global users print_debug(questions) # Search for unasked question stop_flag = False question_id = None while not stop_flag: question_id = random.randint(1, len(questions)) if str(question_id).zfill( QUESTION_ID_LENGTH) not in users[username]["asked_questions"]: stop_flag = True question_frame = chatlib.join_data( [str(question_id).zfill(QUESTION_ID_LENGTH)] + questions[str(question_id).zfill(QUESTION_ID_LENGTH)][0:2]) return question_frame, question_id
def login_try(conn): username = input("Please enter username: \n") password = input("Please enter password: \n") return build_send_recv_parse(conn, chatlib.PROTOCOL_CLIENT["login_msg"], chatlib.join_data([username, password]))[0]
def send_question_answer_get_result(answer, question_id): global question_data answer = chatlib.join_data([question_id, answer]) code, data = build_send_recv_parse(chatlib.PROTOCOL_CLIENT["send_answer"], answer) return code, data