6
6
names = ['Mav' , 'Adam' , 'Teddy' , 'Ari' , 'Donovan' , 'Diana' ,
7
7
'Eduardo' , 'Emanuel' , 'Nourya' , 'Ron' , 'Tobias' , 'Vrushank' ]
8
8
9
+ handlers = {
10
+ "handle_get_version" :0 ,
11
+ "handle_get_currently_logged_in_uname" :1 ,
12
+ "handle_login" :2 ,
13
+ "handle_login_admin" :3 ,
14
+ "handle_logout" :4 ,
15
+ "handle_create_user" :5 ,
16
+ "handle_admin_run_cmd" :6 ,
17
+ "handle_place" :7 ,
18
+ "handle_read_board" :8 ,
19
+ "handle_get_winner" :9 ,
20
+ "handle_add_winner" :10 ,
21
+ "handle_set_intro" :11 ,
22
+ "handle_set_outro" :12 ,
23
+ "handle_report_winners" :13 ,
24
+ }
25
+
9
26
def client_error_wrapper (error_msg ):
10
27
print ("*" * len (str (error_msg )))
11
28
print (error_msg )
@@ -24,9 +41,12 @@ def __init__(self, connect_tuple, debug = True):
24
41
25
42
def read_response (self ):
26
43
resp_len = self .server_conn .recv (4 )
27
- if len (resp_len ) != 4 :
44
+ if len (resp_len ) < 4 :
45
+ client_error_wrapper ("unable to read resposne length" )
46
+ raise Exception ("need 4 bytes in length resposne, you have " + str (len (resp_len )))
47
+ elif len (resp_len ) > 4 :
28
48
client_error_wrapper ("unable to read resposne length" )
29
- raise Exception ("need at least 4 bytes in resposne" )
49
+ raise Exception ("need 4 bytes in length resposne, you have " + str ( len ( resp_len )) )
30
50
resp_len = int .from_bytes (resp_len , NET_ORDER )
31
51
if self .debug :
32
52
print ("getting msg resposne len:" , resp_len )
@@ -55,11 +75,13 @@ def place(self, turn):
55
75
turn_enum = 1
56
76
if turn == 'O' :
57
77
turn_enum = 2
58
- packet = turn .encode ("utf-8" ) + user_coords [0 ].encode ("utf-8" ) + user_coords [1 ].encode ("utf-8" )
78
+ packet = int .to_bytes (turn_enum , 4 , NET_ORDER ) + user_coords [0 ].encode ("utf-8" ) + user_coords [1 ].encode ("utf-8" )
79
+ self .server_conn .sendall (int .to_bytes (handlers ["handle_get_winner" ], 4 , NET_ORDER ))
59
80
self .server_conn .sendall (packet ) #send which player, and coordinates
60
81
61
82
#expects a response that is a 9 byte string representing the board
62
83
def read_board (self ):
84
+ self .server_conn .sendall (int .to_bytes (handlers ["handle_get_winner" ], 4 , NET_ORDER ))
63
85
response = self .read_response ()
64
86
65
87
#print the board
@@ -72,6 +94,7 @@ def read_board(self):
72
94
73
95
#expects 4 bytes repr. winner, 0 = no one, 1 = X, 2 = O
74
96
def get_winner (self ):
97
+ self .server_conn .sendall (int .to_bytes (handlers ["handle_get_winner" ], 4 , NET_ORDER ))
75
98
response = self .read_response ()
76
99
return int .from_bytes (response , NET_ORDER )
77
100
@@ -85,6 +108,7 @@ def play(connect_tuple):
85
108
print ("WELCOME TO ZERO TICTACTOE. 100% VULNERABILITY-FREE CODE GUARANTEED" )
86
109
game .read_board ()
87
110
111
+ time .sleep (1 )
88
112
#gameplay
89
113
turn = 'X'
90
114
while game .get_winner == 0 :
0 commit comments