Compare commits

...

25 Commits

Author SHA1 Message Date
informaniac 77cb3516ca gamemenu: fixed direction evaluation 7 years ago
informaniac 16e895e390 gamemenu now uses balance_util 7 years ago
informaniac b778375165 tetris: fixed socket connection 7 years ago
informaniac 90e1c5c9b8 tetris: fixed player event DOWN to DROP 7 years ago
informaniac b904fcc449 whitespace fix 7 years ago
informaniac c805f6c71b fixed string formatting arguments 7 years ago
informaniac 278f080e23 balancepong now calls the new get_player_ready function. also fixed function call for player 2 7 years ago
informaniac 397e4c323a renamed BalanceUtil._get_raw_data to _get_raw_player_data 7 years ago
informaniac 81de8409f2 balance_util is created and used to create the BalanceTtrsPlayer 7 years ago
informaniac 9bce3e7916 player information is now retrieved via balance_util 7 years ago
informaniac 8cb7470d22 added direction to event evaluation 7 years ago
informaniac ca848e9ba1 removed old balance to event evaluation 7 years ago
informaniac 5cad7825f3 removed old data retrival via socket 7 years ago
informaniac 1e974a74d6 moved creation of the socket into main function 7 years ago
informaniac a5ae8985ce added balance_util import 7 years ago
informaniac 29d0cfcb31 BalanceTtrsPlayer now is being created with a BalanceUtil instead of socket address and player id 7 years ago
informaniac 038f33df2c create BalanceBoardPlayer(s) with the respective balance_utils 7 years ago
informaniac 026f05c958 use the balance_util functions to determine if the player(s) are ready 7 years ago
informaniac 16c4dcc8d6 added balance_utils for player 1 and 2 7 years ago
informaniac 075e8c5c03 player information is now retrieved from the balance_util 7 years ago
informaniac e34f76b22d BalanceBoardPlayer now uses the balance_util for the y-axis data 7 years ago
informaniac 6da1b8cf13 BalanceBoardPlayer now needs a balance_util instead of the wiimote_socket and player_id 7 years ago
informaniac 9c2d3cb33a removed socket retrieval function 7 years ago
informaniac 2b0bde627f imported balance_util 7 years ago
informaniac 02cb0e8348 added blup balance util 7 years ago
  1. 130
      blup/balance_util.py
  2. 43
      games/balancep0ng.py
  3. 51
      games/gamemenu.py
  4. 59
      games/ttrs.py

130
blup/balance_util.py

@ -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

43
games/balancep0ng.py

@ -8,6 +8,7 @@ import os
import configparser import configparser
import socket import socket
import struct import struct
import blup.balance_util
import blup.frame import blup.frame
import blup.output import blup.output
import pong import pong
@ -72,28 +73,14 @@ def convertPixels(dimension, pixels, invert=False):
p.append(row) p.append(row)
return p return p
def get_balance_from_sock(sock, player_id):
print('will balance haben...')
sock.send(b'a')
data = sock.recv(4)
p1x, p1y, p2x, p2y = struct.unpack('bbbb', data)
if player_id == 0:
bal = p1y
elif player_id == 1:
bal = p2y
print('balance id=%s balance=%d' % (player_id, bal))
return bal
class BalanceBoardPlayer(object): class BalanceBoardPlayer(object):
def __init__(self, playground, ownPaddle, wiimote_sock, player_id): def __init__(self, playground, own_paddle, balance_util):
self.__playground = playground self.__playground = playground
self.__ownPaddle = ownPaddle self.__ownPaddle = own_paddle
self.__wiimote = wiimote_sock
self.__ready = False self.__ready = False
self.__playground.addGameTickCallback(self.gametickcb) self.__playground.addGameTickCallback(self.gametickcb)
self.__wiimote_sock = wiimote_sock self.__balance_util = balance_util
self.__player_id = player_id
@property @property
def ownPaddle(self): def ownPaddle(self):
@ -104,11 +91,11 @@ class BalanceBoardPlayer(object):
return true return true
def gametickcb(self): def gametickcb(self):
bal = get_balance_from_sock(self.__wiimote_sock, self.__player_id) bal = self.__balance_util.get_raw_2dir_y()
if bal == -128: if bal == -128:
print("player %d has quit" % (self.__player_id)) print("player %d has quit" % self.__balance_util.player_id)
self.__wiimote_sock.close() self.__balance_util.socket.close()
sys.exit(1) sys.exit(1)
bal = -bal bal = -bal
@ -120,7 +107,7 @@ class BalanceBoardPlayer(object):
bal += MAX_AMPLITUDE bal += MAX_AMPLITUDE
pos = int((bal / (2 * MAX_AMPLITUDE)) * self.__playground.height) pos = int((bal / (2 * MAX_AMPLITUDE)) * self.__playground.height)
print("player %d pos=%d" % (self.__player_id, pos)) print("player %d pos=%d" % (self.__balance_util.player_id, pos))
if self.__ownPaddle.ypos > pos: if self.__ownPaddle.ypos > pos:
self.__ownPaddle.nextMoveUp() self.__ownPaddle.nextMoveUp()
@ -155,24 +142,26 @@ class B4lancePong(object):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(self.__balanceserver) sock.connect(self.__balanceserver)
print('connected!') print('connected!')
balance_util_p1 = blup.balance_util.BalanceUtil(sock, 0)
while get_balance_from_sock(sock, 0) == -128: balance_util_p2 = blup.balance_util.BalanceUtil(sock, 1)
while not balance_util_p1.get_player_ready():
print('waiting for player...') print('waiting for player...')
time.sleep(1) time.sleep(1)
self.__players = [] self.__players = []
self.__players.append(BalanceBoardPlayer(self.__playground, self.__playground.leftPaddle, sock, 0)) self.__players.append(BalanceBoardPlayer(self.__playground, self.__playground.leftPaddle, balance_util_p1))
if self.__numplayers == 2: if self.__numplayers == 2:
self.__players.append(BalanceBoardPlayer(self.__playground, self.__playground.rightPaddle, sock, 1)) self.__players.append(BalanceBoardPlayer(self.__playground, self.__playground.rightPaddle, balance_util_p2))
elif self.__numplayers is None: elif self.__numplayers is None:
for i in range(5): for i in range(5):
frame = mk_logo_frame(self.__dimension, onePlayer) frame = mk_logo_frame(self.__dimension, onePlayer)
self.__out.sendFrame(frame) self.__out.sendFrame(frame)
print('waiting for second player...') print('waiting for second player...')
if get_balance_from_sock(sock, 1) > -128: if balance_util_p2.get_player_ready():
self.__players.append(BalanceBoardPlayer(self.__playground, self.__playground.rightPaddle, sock, 1)) self.__players.append(BalanceBoardPlayer(self.__playground, self.__playground.rightPaddle,
balance_util_p2))
break break
time.sleep(1) time.sleep(1)

51
games/gamemenu.py

@ -8,6 +8,7 @@ import argparse
import subprocess import subprocess
import socket import socket
import struct import struct
import blup.balance_util
import blup.frame import blup.frame
import blup.output import blup.output
@ -210,9 +211,8 @@ class TestInput(AbstractInput):
return None return None
class BalanceInput(AbstractInput, threading.Thread): class BalanceInput(AbstractInput, threading.Thread):
def __init__(self, addr, player_id): def __init__(self, balance_util):
self.addr = addr self.balance_util = balance_util
self.player_id = player_id
self.evt = None self.evt = None
self.lastevt = None self.lastevt = None
self.player_present = False self.player_present = False
@ -221,27 +221,13 @@ class BalanceInput(AbstractInput, threading.Thread):
self.start() self.start()
def run(self): def run(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect(self.addr)
except ConnectionRefusedError:
print('could not connect to balance server', file=sys.stderr)
self.evt = InputEvent.QUIT
return
self.running = True self.running = True
evt = None evt = None
oldevt = None oldevt = None
lastchange = 0 lastchange = 0
while self.running: while self.running:
sock.send(b'a')
data = sock.recv(4)
p0x, p0y, p1x, p1y = struct.unpack('bbbb', data)
if self.player_id == 0:
xbal, ybal = p0x, p0y
elif self.player_id == 1:
xbal, ybal = p1x, p1y
THRESHOLD = 40 THRESHOLD = 40
direction = self.balance_util.get_4dir(THRESHOLD)
MIN_TIMES = { MIN_TIMES = {
InputEvent.LEFT: 0.05, InputEvent.LEFT: 0.05,
InputEvent.RIGHT: 0.05, InputEvent.RIGHT: 0.05,
@ -249,24 +235,21 @@ class BalanceInput(AbstractInput, threading.Thread):
InputEvent.DOWN: 0.05, InputEvent.DOWN: 0.05,
} }
if self.player_present: if self.player_present:
if xbal == -128 or ybal == -128: if direction == blup.balance_util.Direction.QUIT:
self.evt = InputEvent.QUIT self.evt = InputEvent.QUIT
self.player_present = False self.player_present = False
continue continue
elif direction == blup.balance_util.Direction.CENTER:
if abs(xbal) < THRESHOLD and abs(ybal) < THRESHOLD:
evt = None evt = None
else: elif direction == blup.balance_util.Direction.UP:
if abs(xbal) < abs(ybal):
if ybal > 0:
evt = InputEvent.UP evt = InputEvent.UP
else: elif direction == blup.balance_util.Direction.DOWN:
evt = InputEvent.DOWN evt = InputEvent.DOWN
else: elif direction == blup.balance_util.Direction.RIGHT:
if xbal > 0:
evt = InputEvent.RIGHT evt = InputEvent.RIGHT
else: elif direction == blup.balance_util.Direction.LEFT:
evt = InputEvent.LEFT evt = InputEvent.LEFT
if evt != oldevt: if evt != oldevt:
lastchange = time.time() lastchange = time.time()
oldevt = evt oldevt = evt
@ -281,7 +264,7 @@ class BalanceInput(AbstractInput, threading.Thread):
#print('player_id=%d event=%s' % (self.player_id, self.evt)) #print('player_id=%d event=%s' % (self.player_id, self.evt))
time.sleep(0.005) time.sleep(0.005)
else: else:
if xbal != -128 and ybal != -128: if direction != blup.balance_util.Direction.QUIT:
self.evt = InputEvent.STEP_ON self.evt = InputEvent.STEP_ON
self.player_present = True self.player_present = True
continue continue
@ -325,8 +308,14 @@ if __name__ == '__main__':
inp2 = TestInput() inp2 = TestInput()
elif args.balance is not None: elif args.balance is not None:
host, port = args.balance.split(':') host, port = args.balance.split(':')
inp1 = BalanceInput((host, int(port)), 0) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
inp2 = BalanceInput((host, int(port)), 1) try:
sock.connect((host, int(port)))
inp1 = BalanceInput(blup.balance_util.BalanceUtil(sock, 0))
inp2 = BalanceInput(blup.balance_util.BalanceUtil(sock, 1))
except ConnectionRefusedError:
print('could not connect to balance server', file=sys.stderr)
sys.exit(1)
while True: while True:
evt = inp1.get_event() evt = inp1.get_event()

59
games/ttrs.py

@ -8,6 +8,7 @@ import time
import enum import enum
import math import math
import collections import collections
import blup.balance_util
import blup.frame import blup.frame
import blup.output import blup.output
import random import random
@ -233,55 +234,37 @@ class TestTtrsPlayer(TtrsPlayer):
class BalanceTtrsPlayer(TtrsPlayer, threading.Thread): class BalanceTtrsPlayer(TtrsPlayer, threading.Thread):
def __init__(self, playground, addr, player_id): def __init__(self, playground, balance_util):
self.playground = playground self.playground = playground
self.addr = addr self.balance_util = balance_util
self.player_id = player_id
self.evt = None self.evt = None
self.lastevt = None self.lastevt = None
threading.Thread.__init__(self, daemon=True) threading.Thread.__init__(self, daemon=True)
self.start() self.start()
def run(self): def run(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect(self.addr)
except ConnectionRefusedError:
print('could not connect to balance server', file=sys.stderr)
self.evt = PlayerEvent.QUIT
return
self.running = True self.running = True
evt = None evt = None
oldevt = None oldevt = None
lastchange = 0 lastchange = 0
while self.running: while self.running:
sock.send(b'a') THRESHOLD = 40
data = sock.recv(4) direction = self.balance_util.get_4dir(THRESHOLD)
p0x, p0y, p1x, p1y = struct.unpack('bbbb', data)
if self.player_id == 0:
xbal, ybal = p0x, p0y
elif self.player_id == 1:
xbal, ybal = p1x, p1y
#print('player_id=%d xbal=%d ybal=%d' % (self.player_id, xbal, ybal)) if direction == blup.balance_util.Direction.QUIT:
if xbal == -128 or ybal == -128:
self.evt = PlayerEvent.QUIT self.evt = PlayerEvent.QUIT
elif direction == blup.balance_util.Direction.LEFT:
THRESHOLD = 40 evt = PlayerEvent.MOVE_LEFT
if abs(xbal) < THRESHOLD and abs(ybal) < THRESHOLD: elif direction == blup.balance_util.Direction.RIGHT:
evt = None evt = PlayerEvent.MOVE_RIGHT
else: elif direction == blup.balance_util.Direction.UP:
if abs(xbal) < abs(ybal):
if ybal > 0:
evt = PlayerEvent.ROTATE evt = PlayerEvent.ROTATE
else: elif direction == blup.balance_util.Direction.DOWN:
evt = PlayerEvent.DROP evt = PlayerEvent.DROP
else: else:
if xbal > 0: evt = None
evt = PlayerEvent.MOVE_RIGHT
else:
evt = PlayerEvent.MOVE_LEFT
#print('player_id=%d xbal=%d ybal=%d' % (self.player_id, xbal, ybal))
MIN_TIMES = { MIN_TIMES = {
PlayerEvent.MOVE_LEFT: 0.1, PlayerEvent.MOVE_LEFT: 0.1,
PlayerEvent.MOVE_RIGHT: 0.1, PlayerEvent.MOVE_RIGHT: 0.1,
@ -307,7 +290,7 @@ class BalanceTtrsPlayer(TtrsPlayer, threading.Thread):
def get_event(self): def get_event(self):
print('player_id=%d event=%s' % (self.player_id, self.evt)) print('player_id=%d event=%s' % (self.balance_util.player_id, self.evt))
evt = self.evt evt = self.evt
self.evt = None self.evt = None
return evt return evt
@ -481,13 +464,21 @@ if __name__ == '__main__':
seed = random.random() seed = random.random()
games = [] games = []
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host, port = args.balance.split(':')
try:
sock.connect((host, int(port)))
except ConnectionRefusedError:
print('could not connect to balance server', file=sys.stderr)
sys.exit(1)
def start_game(player_id, xpos): def start_game(player_id, xpos):
pg = Playground(10, 16) pg = Playground(10, 16)
balance_util = blup.balance_util.BalanceUtil(sock, player_id)
if args.pygame: if args.pygame:
player = TestTtrsPlayer(pg) player = TestTtrsPlayer(pg)
elif args.balance is not None: elif args.balance is not None:
host, port = args.balance.split(':') player = BalanceTtrsPlayer(pg, balance_util)
player = BalanceTtrsPlayer(pg, (host, int(port)), player_id)
rnd = random.Random(seed) rnd = random.Random(seed)
game = TtrsGame(pg, player, rnd) game = TtrsGame(pg, player, rnd)
painter.add_game(game, xpos, 0) painter.add_game(game, xpos, 0)

Loading…
Cancel
Save