Compare commits
25 Commits
master
...
feature/ba
Author | SHA1 | Date |
---|---|---|
informaniac | 77cb3516ca | 7 years ago |
informaniac | 16e895e390 | 7 years ago |
informaniac | b778375165 | 7 years ago |
informaniac | 90e1c5c9b8 | 7 years ago |
informaniac | b904fcc449 | 7 years ago |
informaniac | c805f6c71b | 7 years ago |
informaniac | 278f080e23 | 7 years ago |
informaniac | 397e4c323a | 7 years ago |
informaniac | 81de8409f2 | 7 years ago |
informaniac | 9bce3e7916 | 7 years ago |
informaniac | 8cb7470d22 | 7 years ago |
informaniac | ca848e9ba1 | 7 years ago |
informaniac | 5cad7825f3 | 7 years ago |
informaniac | 1e974a74d6 | 7 years ago |
informaniac | a5ae8985ce | 7 years ago |
informaniac | 29d0cfcb31 | 7 years ago |
informaniac | 038f33df2c | 7 years ago |
informaniac | 026f05c958 | 7 years ago |
informaniac | 16c4dcc8d6 | 7 years ago |
informaniac | 075e8c5c03 | 7 years ago |
informaniac | e34f76b22d | 7 years ago |
informaniac | 6da1b8cf13 | 7 years ago |
informaniac | 9c2d3cb33a | 7 years ago |
informaniac | 2b0bde627f | 7 years ago |
informaniac | 02cb0e8348 | 7 years ago |
4 changed files with 197 additions and 98 deletions
@ -0,0 +1,130 @@
@@ -0,0 +1,130 @@
|
||||
""" |
||||
this file is part of blup. it provides an easy way |
||||
to determine the direction the player is leaning |
||||
""" |
||||
from enum import Enum |
||||
import struct |
||||
|
||||
Direction = Enum('BalanceDirection', |
||||
['LEFT', 'RIGHT', 'UP', 'DOWN', |
||||
'CENTER', 'QUIT']) |
||||
|
||||
|
||||
def sanitize_threshold(threshold): |
||||
""" |
||||
function to make sure the threshold is positive |
||||
threshold -- the given (untrusted) treshold |
||||
""" |
||||
if threshold < 0: |
||||
threshold = abs(threshold) |
||||
return threshold |
||||
|
||||
|
||||
class BalanceSocket(object): |
||||
def __init__(self, balance_server_socket): |
||||
self.socket = balance_server_socket |
||||
|
||||
def close(self): |
||||
self.socket.close() |
||||
|
||||
def _get_raw_data(self): |
||||
self.socket.send(b'a') |
||||
data = self.socket.recv(4) |
||||
p0x, p0y, p1x, p1y = struct.unpack('bbbb', data) |
||||
return p0x, p0y, p1x, p1y |
||||
|
||||
|
||||
class BalanceUtil(BalanceSocket): |
||||
def __init__(self, balance_server_socket, player_id): |
||||
""" |
||||
BalanceUtil object |
||||
balance_socket -- (ip, port) of the balance server |
||||
player_id -- id of the player |
||||
""" |
||||
super(BalanceUtil, self).__init__(balance_server_socket) |
||||
self.player_id = player_id |
||||
|
||||
def _get_raw_player_data(self): |
||||
self.socket.send(b'a') |
||||
data = self.socket.recv(4) |
||||
p0x, p0y, p1x, p1y = struct.unpack('bbbb', data) |
||||
if self.player_id == 0: |
||||
return p0x, p0y |
||||
else: |
||||
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): |
||||
""" |
||||
Get raw 2-directional data from the y axis of the board. |
||||
e.g. determine how fast the paddle is supposed to move |
||||
in balance pong |
||||
:return: raw y axis balance value |
||||
""" |
||||
x_bal, y_bal = self._get_raw_player_data() |
||||
return y_bal |
||||
|
||||
def get_2dir_y(self, threshold): |
||||
threshold = sanitize_threshold(threshold) |
||||
x_bal, y_bal = self._get_raw_player_data() |
||||
if x_bal == -128 or y_bal == -128: |
||||
return Direction.QUIT |
||||
elif abs(y_bal) < threshold: |
||||
return Direction.CENTER |
||||
elif y_bal > 0: |
||||
return Direction.UP |
||||
else: |
||||
return Direction.DOWN |
||||
|
||||
def get_raw_2dir_x(self): |
||||
x_bal, y_bal = self._get_raw_player_data() |
||||
return x_bal |
||||
|
||||
def get_2dir_x(self, threshold): |
||||
threshold = sanitize_threshold(threshold) |
||||
x_bal, y_bal = self._get_raw_player_data() |
||||
if x_bal == -128 or y_bal == -128: |
||||
return Direction.QUIT |
||||
elif abs(x_bal) < threshold: |
||||
return Direction.CENTER |
||||
elif x_bal > 0: |
||||
return Direction.RIGHT |
||||
else: |
||||
return Direction.LEFT |
||||
|
||||
def get_raw_4dir(self): |
||||
return self._get_raw_player_data() |
||||
|
||||
def get_4dir(self, threshold): |
||||
""" |
||||
Get evaluated 4-directional data of the board |
||||
e.g. determine the direction and rotation or drop move |
||||
in balance tetris |
||||
|
||||
threshold -- deadzone of the board where nothing is |
||||
supposed to happen |
||||
:return: Enum Direction value of direction or event |
||||
""" |
||||
threshold = sanitize_threshold(threshold) |
||||
x_bal, y_bal = self._get_raw_player_data() |
||||
if x_bal == -128 or y_bal == -128: |
||||
return Direction.QUIT |
||||
elif abs(x_bal) < threshold and abs(y_bal) < threshold: |
||||
return Direction.CENTER |
||||
elif abs(x_bal) < abs(y_bal): |
||||
if y_bal > 0: |
||||
return Direction.UP |
||||
else: |
||||
return Direction.DOWN |
||||
else: |
||||
if x_bal > 0: |
||||
return Direction.RIGHT |
||||
else: |
||||
return Direction.LEFT |
Loading…
Reference in new issue