def threaded(conn, addr): try: with conn: num_wins = 0 for i in range(NUM_GAMES): # TODO: Doc game = Game(SemiRandomAgent, HumanAgent) try: try: winner = Game.play_game(game, conn, addr) if winner >= 0: num_wins += 1 print('{}:{} : {}/{}'.format(addr[0], addr[1], num_wins, NUM_GAMES)) conn.sendall(encoded('{}/{}'.format(num_wins, i + 1))) except InvalidActionError: print('{}:{} : Invalid action'.format( addr[0], addr[1])) conn.sendall(encoded('Invalid action !')) break except ConnectionResetError: print('{}:{} : Connection Reset'.format(addr[0], addr[1])) break if num_wins > WIN_FLAG: conn.sendall(encoded(FLAG)) else: conn.sendall(encoded('No flag for you !')) except socket.timeout: print('{}:{} : Socket timeout'.format(addr[0], addr[1])) except: e = sys.exc_info()[0] print(e, 'happened') exit()
def threaded(conn, addr, path): try: with conn: quiz = Quiz(path) conn.sendall(encoded(quiz.intro)) has_flag = True while not quiz.is_done(): question = quiz.get_question() conn.sendall(encoded(question)) answer = decoded(conn.recv(1024)) print('{}:{} : Q{}, A {}'.format(addr[0], addr[1], quiz.current_question + 1, answer)) if not quiz.answer_question(answer): conn.sendall(encoded(quiz.wrong)) has_flag = False break conn.sendall(encoded(quiz.right)) if has_flag: conn.sendall(encoded(FLAG + '\n')) except socket.timeout: print('{}:{} : Socket timeout'.format(addr[0], addr[1])) except ConnectionResetError: print('{}:{} : Connection Reset'.format(addr[0], addr[1])) except: e = sys.exc_info()[0] print(e, 'happened') print(sys.exc_info()[1]) exit()
def threaded(conn, addr, path): try: with conn: with open('{}_{}.png'.format(addr[0], addr[1]), 'wb') as img: while True: data = conn.recv(1024) if not data: break img.write(data) has_flag = False recv_img = imread('{}_{}.png'.format(addr[0], addr[1])).astype(float) img = imread(path).astype(float) has_flag = compare_images(recv_img, img) < 100 if has_flag: conn.sendall(encoded(FLAG + '\n')) else: conn.sendall(encoded('NO FLAG FOR YOU !')) except socket.timeout: print('{}:{} : Socket timeout'.format(addr[0], addr[1])) except ConnectionResetError: print('{}:{} : Connection Reset'.format(addr[0], addr[1])) except: e = sys.exc_info()[0] print(e, 'happened') print(sys.exc_info()[1]) exit()
def play_game(game, connection, locality): # TODO: Doc while True: action = game.player1.get_action(game.state, connection) try: finished, score = game.step(game.player1.type, action) except AssertionError: raise InvalidActionError() if finished and score < 0: print('{}:{} : Computer wins'.format(locality[0], locality[1])) return score elif finished and score == 0: print('{}:{} : A draw'.format(locality[0], locality[1])) return score time.sleep(0.1) connection.sendall(encoded(game.state.printable_board())) action = game.player2.get_action(game.state, connection) try: finished, score = game.step(game.player2.type, action) except AssertionError: raise InvalidActionError() if finished and score > 0: print('{}:{} : Player wins'.format(locality[0], locality[1])) return score elif finished and score == 0: print('{}:{} : A draw'.format(locality[0], locality[1])) return score