|
|
@ -33,22 +33,6 @@ class BalanceSocket(object): |
|
|
|
p0x, p0y, p1x, p1y = struct.unpack('bbbb', data) |
|
|
|
p0x, p0y, p1x, p1y = struct.unpack('bbbb', data) |
|
|
|
return p0x, p0y, p1x, p1y |
|
|
|
return p0x, p0y, p1x, p1y |
|
|
|
|
|
|
|
|
|
|
|
def get_player1_ready(self): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
check if someone is on board 1 |
|
|
|
|
|
|
|
:return: true if someone is on the board |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
p0x, p0y, p1x, p1y = self._get_raw_data() |
|
|
|
|
|
|
|
return p0x > -128 or p0y > -128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_player2_ready(self): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
check if someone is on board 2 |
|
|
|
|
|
|
|
:return: true if someone is on the board |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
p0x, p0y, p1x, p1y = self._get_raw_data() |
|
|
|
|
|
|
|
return p1x > -128 or p1y > -128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BalanceUtil(BalanceSocket): |
|
|
|
class BalanceUtil(BalanceSocket): |
|
|
|
def __init__(self, balance_server_socket, player_id): |
|
|
|
def __init__(self, balance_server_socket, player_id): |
|
|
@ -60,7 +44,7 @@ class BalanceUtil(BalanceSocket): |
|
|
|
super(BalanceUtil, self).__init__(balance_server_socket) |
|
|
|
super(BalanceUtil, self).__init__(balance_server_socket) |
|
|
|
self.player_id = player_id |
|
|
|
self.player_id = player_id |
|
|
|
|
|
|
|
|
|
|
|
def _get_raw_data(self): |
|
|
|
def _get_raw_player_data(self): |
|
|
|
self.socket.send(b'a') |
|
|
|
self.socket.send(b'a') |
|
|
|
data = self.socket.recv(4) |
|
|
|
data = self.socket.recv(4) |
|
|
|
p0x, p0y, p1x, p1y = struct.unpack('bbbb', data) |
|
|
|
p0x, p0y, p1x, p1y = struct.unpack('bbbb', data) |
|
|
@ -69,6 +53,14 @@ class BalanceUtil(BalanceSocket): |
|
|
|
else: |
|
|
|
else: |
|
|
|
return p1x, p1y |
|
|
|
return p1x, p1y |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_player_ready(self): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
check if someone is on the board |
|
|
|
|
|
|
|
:return: true if someone is on the board |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
p_x, p_y = self._get_raw_player_data() |
|
|
|
|
|
|
|
return p_x > -128 or p_y > -128 |
|
|
|
|
|
|
|
|
|
|
|
def get_raw_2dir_y(self): |
|
|
|
def get_raw_2dir_y(self): |
|
|
|
""" |
|
|
|
""" |
|
|
|
Get raw 2-directional data from the y axis of the board. |
|
|
|
Get raw 2-directional data from the y axis of the board. |
|
|
@ -76,12 +68,12 @@ class BalanceUtil(BalanceSocket): |
|
|
|
in balance pong |
|
|
|
in balance pong |
|
|
|
:return: raw y axis balance value |
|
|
|
:return: raw y axis balance value |
|
|
|
""" |
|
|
|
""" |
|
|
|
x_bal, y_bal = self._get_raw_data() |
|
|
|
x_bal, y_bal = self._get_raw_player_data() |
|
|
|
return y_bal |
|
|
|
return y_bal |
|
|
|
|
|
|
|
|
|
|
|
def get_2dir_y(self, threshold): |
|
|
|
def get_2dir_y(self, threshold): |
|
|
|
threshold = sanitize_threshold(threshold) |
|
|
|
threshold = sanitize_threshold(threshold) |
|
|
|
x_bal, y_bal = self._get_raw_data() |
|
|
|
x_bal, y_bal = self._get_raw_player_data() |
|
|
|
if x_bal == -128 or y_bal == -128: |
|
|
|
if x_bal == -128 or y_bal == -128: |
|
|
|
return Direction.QUIT |
|
|
|
return Direction.QUIT |
|
|
|
elif abs(y_bal) < threshold: |
|
|
|
elif abs(y_bal) < threshold: |
|
|
@ -92,12 +84,12 @@ class BalanceUtil(BalanceSocket): |
|
|
|
return Direction.DOWN |
|
|
|
return Direction.DOWN |
|
|
|
|
|
|
|
|
|
|
|
def get_raw_2dir_x(self): |
|
|
|
def get_raw_2dir_x(self): |
|
|
|
x_bal, y_bal = self._get_raw_data() |
|
|
|
x_bal, y_bal = self._get_raw_player_data() |
|
|
|
return x_bal |
|
|
|
return x_bal |
|
|
|
|
|
|
|
|
|
|
|
def get_2dir_x(self, threshold): |
|
|
|
def get_2dir_x(self, threshold): |
|
|
|
threshold = sanitize_threshold(threshold) |
|
|
|
threshold = sanitize_threshold(threshold) |
|
|
|
x_bal, y_bal = self._get_raw_data() |
|
|
|
x_bal, y_bal = self._get_raw_player_data() |
|
|
|
if x_bal == -128 or y_bal == -128: |
|
|
|
if x_bal == -128 or y_bal == -128: |
|
|
|
return Direction.QUIT |
|
|
|
return Direction.QUIT |
|
|
|
elif abs(x_bal) < threshold: |
|
|
|
elif abs(x_bal) < threshold: |
|
|
@ -108,7 +100,7 @@ class BalanceUtil(BalanceSocket): |
|
|
|
return Direction.LEFT |
|
|
|
return Direction.LEFT |
|
|
|
|
|
|
|
|
|
|
|
def get_raw_4dir(self): |
|
|
|
def get_raw_4dir(self): |
|
|
|
return self._get_raw_data() |
|
|
|
return self._get_raw_player_data() |
|
|
|
|
|
|
|
|
|
|
|
def get_4dir(self, threshold): |
|
|
|
def get_4dir(self, threshold): |
|
|
|
""" |
|
|
|
""" |
|
|
@ -121,7 +113,7 @@ class BalanceUtil(BalanceSocket): |
|
|
|
:return: Enum Direction value of direction or event |
|
|
|
:return: Enum Direction value of direction or event |
|
|
|
""" |
|
|
|
""" |
|
|
|
threshold = sanitize_threshold(threshold) |
|
|
|
threshold = sanitize_threshold(threshold) |
|
|
|
x_bal, y_bal = self._get_raw_data() |
|
|
|
x_bal, y_bal = self._get_raw_player_data() |
|
|
|
if x_bal == -128 or y_bal == -128: |
|
|
|
if x_bal == -128 or y_bal == -128: |
|
|
|
return Direction.QUIT |
|
|
|
return Direction.QUIT |
|
|
|
elif abs(x_bal) < threshold and abs(y_bal) < threshold: |
|
|
|
elif abs(x_bal) < threshold and abs(y_bal) < threshold: |
|
|
|