boreelog

quiz_client

Apr 13th, 2021
670
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # quiz_client
  2.  
  3. import socket, re, sys
  4. # Import the functions from the networking tools module
  5. from fl_networking_tools import get_binary, send_binary
  6.  
  7. def set_up():
  8.     def valid(svr):
  9.         if not(re.match(ip_re,svr) and len(svr) > 0): return False
  10.         splt = svr.split('.')
  11.         if len(splt) != 4: return False
  12.         for x in splt:
  13.             if not (0 <= int(x) < 256): return False
  14.         return True
  15.        
  16.     quiz_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  17.     team = ''
  18.     while team == '':
  19.         team = input('Please enter your team name: ')
  20.        
  21.     ip_re = r'^\d{1,3}[.]\d{1,3}[.]\d{1,3}[.]\d{1,3}$'
  22.     while True:
  23.         server = input('Please enter a valid IP address of the server')
  24.         #server = '127.0.0.1' # just for testing
  25.         if valid(server): break
  26.     quiz_server.connect((server, 2065))
  27.     return team, quiz_server    
  28.        
  29. team, quiz_server = set_up()
  30. messages = dict(Q=['QUES',''],A=['ANS '],J=['JOIN',team],S=['STAT',team])
  31.  
  32. def send_msg(msg_id,a=''):
  33.     # a is the answer to a quiz question, relevant only for Answer messages
  34.     msg = messages[msg_id][:]
  35.     if msg_id == 'A': msg.append(a)
  36.     send_binary(quiz_server, msg)
  37.    
  38. def process_response(response):
  39.         # response is the command/response tuple - response[0] is the code
  40.         r = response[0]
  41.         if r == 1: # The question response
  42.             # response consists of question and options for answer
  43.             q_options = response[1].split('#')
  44.             # Display it to the user.
  45.             print()
  46.             for ln in q_options:
  47.                 print(ln)
  48.             answer_question()
  49.         elif r == 2:
  50.             print('Correct {}. Your score so far is {}'.\
  51.                   format(team,response[1]))
  52.             send_msg('Q')
  53.         elif r == 3:
  54.             print('Sorry {}, that is wrong. Your score so far is {}'.\
  55.                   format(team,response[1]))
  56.             send_msg('Q')
  57.         elif r == 4:
  58.             print('Current status is {}'.format(response[1]))
  59.         elif r == 5:
  60.             print('Welcome to the Amazing Quiz {}'.format(team))
  61.             send_msg('S')  # send STAT msg
  62.         elif r == 6:
  63.             print("Sorry! you can't join the quiz because {}".\
  64.                   format(response[1]))
  65.         elif r == 7:
  66.             print('\nThank you for playing {}. Your score was {}'.\
  67.                   format(team,response[1]))
  68.         elif r == 8:
  69.             score_list = response[1]
  70.             print('\nAll scores')
  71.             print()
  72.             for x in score_list:
  73.                 print('{} scored {}'.format(x[0],x[1]))
  74.  
  75.         elif r == 9:  # quiz is starting/continuing
  76.             print(response[1])
  77.             send_msg('Q')
  78.         elif r == 10:  # questions are available
  79.             print(response[1])
  80.             send_msg('Q')
  81.         elif r == 12:
  82.             print('Sorry {}, but you have been eliminated'.format(team))
  83.         elif r == 13:
  84.             print(response[1])
  85.         elif r == 14:  # the winner
  86.             print('\n{} is declared the winner'.format(response[1]))
  87.             print('\nThe quiz has now finished.')
  88.             print('You are welcome back at any time')
  89.            
  90.            
  91.  
  92. def answer_question():
  93.     print('Answer with the option number of your choice.')
  94.     ans = input('Your answer: ')
  95.     send_msg('A',ans)
  96.    
  97. send_msg('J')
  98. playing = True
  99.  
  100. while playing:
  101.     # The get_binary function returns a list of messages - loop over them
  102.     for response in get_binary(quiz_server):
  103.         process_response(response)
  104.  
  105.        
  106.        
  107.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×