thiiiiings

This commit is contained in:
Jonas Zeunert
2020-02-13 19:05:55 +01:00
parent 82f1ad6ed8
commit 7414734dad
719 changed files with 41551 additions and 227 deletions

23
Game.py Normal file
View File

@@ -0,0 +1,23 @@
import asyncio
import signal
class Game:
def __init__(self, input_handler, player_state, game_state):
self.input_handler = input_handler
self.player_state = player_state
self.game_state = game_state
signal.signal(signal.SIGINT, self.interrupt)
self.is_running = asyncio.Condition()
pass
def interrupt(self):
print('Received SIGINT. Stopping Game :-)')
self.is_running.release()
def run(self):
self.is_running.acquire()
asyncio.run(self.input_handler.handleInputs())
await self.is_running.wait()
pass

17
GameState.py Normal file
View File

@@ -0,0 +1,17 @@
class GameState:
def __init__(self, players, highscore, currentPlayer = 1):
self.players = players
self.currentPlayer = currentPlayer
self.highscore = highscore
def currentPlayer(self):
return self.players[0]
def setCurrentPlayer(self, playerId):
if(playerId > len(self.players)):
pass # todo throw error
savedPlayer = self.players[0]
self.players[0] = filter(lambda x: playerId == x.id, self.players)
self.players.remove(savedPlayer)
self.players.append(savedPlayer)

17
InputHandler.py Normal file
View File

@@ -0,0 +1,17 @@
import asyncio
from networking.Networking import Networking
class InputHandler:
def __init__(self, targets):
self.isRunning = True
self.targets = targets
async def handleInputs(self):
self.isRunning = True
while self.isRunning:
event = await Networking().getInputEvent()
self.targets[event].hit() #Todo
def stop(self):
self.isRunning = False

View File

@@ -15,15 +15,15 @@ class PlayerState:
def addPoints(self, points): def addPoints(self, points):
self.__points += points self.__points += points
self.display.write_score(self.__points) self.display.printScore(self.__points)
def addBall(self): def addBall(self):
self.ballsLeft += 1 self.ballsLeft += 1
self.specialDisplay.write_ballsToPlay(self.ballsLeft) self.specialDisplay.printBallsToPlay(self.ballsLeft)
def removeBall(self): def removeBall(self):
self.ballsLeft -= 1 self.ballsLeft -= 1
self.specialDisplay.write_ballsToPlay(self.ballsLeft) self.specialDisplay.printBallsToPlay(self.ballsLeft)
if(self.ballsLeft == 0): if(self.ballsLeft == 0):
# todo emit endgame Event # todo emit endgame Event
pass pass
@@ -39,7 +39,7 @@ class PlayerState:
self.timer = Timer(self.upperPlayfieldTime, self.stopUpperPlayfieldTimer) self.timer = Timer(self.upperPlayfieldTime, self.stopUpperPlayfieldTimer)
def stopUpperPlayfieldTimer(self): def stopUpperPlayfieldTimer(self):
if(self.timer == None): if not self.timer:
return return
self.timer.cancel() self.timer.cancel()

View File

@@ -1,5 +1,5 @@
from src.networking.Networking import Networking from networking.Networking import Networking
from src.utils.util import normalizeName from utils.util import normalizeName
class Solenoid: class Solenoid:
def __init__(self, name): def __init__(self, name):
@@ -11,4 +11,4 @@ class Solenoid:
self.name = normalizeName(solenoidName) self.name = normalizeName(solenoidName)
def trigger(self): def trigger(self):
Networking.triggerSolenoid(self.name) Networking().triggerSolenoid(self.name)

19
__main__.py Normal file
View File

@@ -0,0 +1,19 @@
from factories.NetworkFactory import NetworkFactory
from factories.GameFactory import GameFactory
def main():
args = parseCommandLine()
game = createGame(args)
game.run()
def parseCommandLine():
return
def createGame(args):
NetworkFactory.createNetwork(args['OutputServerAddress'], args['InputServerAddress'])
gameFactory = GameFactory()
game = gameFactory.createGame()
return game
if __name__ == '__main__':
main()

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

9
displays/Display.py Normal file
View File

@@ -0,0 +1,9 @@
from networking.Networking import Networking
class Display:
def __init__(self, name):
self.name = name
def printScore(self, score):
Networking().write(self.name, score)

View File

@@ -1,4 +1,4 @@
from src.displays.Display import Display from displays.Display import Display
class PlayerDisplay(Display): class PlayerDisplay(Display):
def __init__(self, name): def __init__(self, name):

View File

@@ -1,4 +1,4 @@
from src.displays.Display import Display from displays.Display import Display
class SpecialDisplay(Display): class SpecialDisplay(Display):
def __init__(self, name): def __init__(self, name):
@@ -7,19 +7,19 @@ class SpecialDisplay(Display):
self.gameTimeBonus = 0 self.gameTimeBonus = 0
self.ballsToPlay = 0 self.ballsToPlay = 0
def write_score(self): def printScore(self):
super.write_score(int(str(self.credits).zfill(2) super.write_score(int(str(self.credits).zfill(2)
+ str(self.gameTimeBonus).zfill(2) + str(self.gameTimeBonus).zfill(2)
+ str(self.ballsToPlay).zfill(2))) + str(self.ballsToPlay).zfill(2)))
def write_ballsToPlay(self, ballsToPlay): def printBallsToPlay(self, ballsToPlay):
self.ballsToPlay = ballsToPlay self.ballsToPlay = ballsToPlay
self.write_score() self.printScore()
def write_gameTimeBonus(self, gameTimeBonus): def printGameTimeBonus(self, gameTimeBonus):
self.gameTimeBonus = gameTimeBonus self.gameTimeBonus = gameTimeBonus
self.write_score() self.printScore()
def write_credits(self, credits): def printCredits(self, credits):
self.credits = credits self.credits = credits
self.write_score() self.printScore()

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -3,4 +3,4 @@ class BonusTimeEvent:
self.playerState = playerState self.playerState = playerState
def trigger(self): def trigger(self):
self.playerState.addBonusTimeSecond() self.playerState().addBonusTimeSecond()

View File

@@ -3,4 +3,4 @@ class EndOfBallEvent:
self.playerState = playerState self.playerState = playerState
def trigger(self): def trigger(self):
self.playerState.removeBall() self.playerState().removeBall()

View File

@@ -5,4 +5,4 @@ class EnterUpperPlayfieldEvent:
def trigger(self): def trigger(self):
self.flipper.activate() self.flipper.activate()
self.playerState.startUpperPlayfieldTimer() self.playerState().startUpperPlayfieldTimer()

6
events/FlapEvent.py Normal file
View File

@@ -0,0 +1,6 @@
class FlapEvent:
def __init__(self, flapSolenoid):
self.flapSolenoid = flapSolenoid
def trigger(self):
self.flapSolenoid.trigger()

View File

@@ -5,4 +5,4 @@ class LeaveUpperPlayfieldEvent:
def trigger(self): def trigger(self):
self.flipper.deactivate() self.flipper.deactivate()
self.playerState.stopUpperPlayfieldTimer() self.playerState().stopUpperPlayfieldTimer()

7
events/LeftFlapEvent.py Normal file
View File

@@ -0,0 +1,7 @@
from events.FlapEvent import FlapEvent
from solenoids.LeftFlapSolenoid import LeftFlapSolenoid
class LeftFlapEvent(FlapEvent):
def __init__(self):
super(LeftFlapSolenoid())

View File

@@ -3,4 +3,4 @@ class PointEvent:
self.playerState = playerState self.playerState = playerState
def trigger(self, target): def trigger(self, target):
self.playerState.addPoints(target.points) self.playerState().addPoints(target.points)

7
events/RightFlapEvent.py Normal file
View File

@@ -0,0 +1,7 @@
from events.FlapEvent import FlapEvent
from solenoids.RightFlapSolenoid import RightFlapSolenoid
class RightFlapEvent(FlapEvent):
def __init__(self):
super(RightFlapSolenoid())

View File

@@ -0,0 +1,9 @@
from solenoids.RightKickerSolenoid import RightKickerSolenoid
class RightKickerEvent:
def __init__(self):
self.rightKickerSolenoid = RightKickerSolenoid()
def trigger(self):
self.rightKickerSolenoid.trigger()

10
events/StartGameEvent.py Normal file
View File

@@ -0,0 +1,10 @@
from solenoids.OutHoleSolenoid import OutHoleSolenoid
class StartGameEvent:
def __init__(self, flipper):
self.flipper = flipper
self.outHoleSolenoid = OutHoleSolenoid()
def trigger(self):
self.flipper.activate()
self.outHoleSolenoid.trigger()

View File

@@ -3,4 +3,4 @@ class UpperPlayfieldTimeEvent:
self.playerState = playerState self.playerState = playerState
def trigger(self): def trigger(self):
self.playerState.addUpperPlayfieldTime(1) self.playerState().addUpperPlayfieldTime(1)

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,14 @@
from displays.PlayerDisplay import PlayerDisplay
from displays.SpecialDisplay import SpecialDisplay
class DisplayFactory:
def __init__(self, displayNames):
self.displayNames = displayNames
def createSpecialDisplay(self):
name = filter(lambda displayName: "Special" in displayName, self.displayNames)
return SpecialDisplay(name)
def createPlayerDisplay(self, id):
name = filter(lambda displayName: "Player" in displayName and str(id) in displayName, self.displayNames)
return PlayerDisplay(name)

53
factories/EventFactory.py Normal file
View File

@@ -0,0 +1,53 @@
from events.PointEvent import PointEvent
from events.EndOfBallEvent import EndOfBallEvent
from events.RightFlapEvent import RightFlapEvent
from events.LeftFlapEvent import LeftFlapEvent
from events.RightKickerEvent import RightKickerEvent
from events.EnterUpperPlayfieldEvent import EnterUpperPlayfieldEvent
from events.StartGameEvent import StartGameEvent
class EventFactory:
def __init__(self, targets, currentPlayer, upperPlayfieldFlippers, flipper):
self.targets = targets
self.currentPlayer = currentPlayer
self.upperPlayfieldFlippers = upperPlayfieldFlippers
self.flipper = flipper
def createPointEvent(self):
event = PointEvent(self.currentPlayer)
for target in self.targets:
self.__registerEventToTarget(event, target)
return event
def createEndOfBallEvent(self):
event = EndOfBallEvent(self.currentPlayer)
self.__registerEventToTarget(event, self.targets['OutholeTarget'])
return event
def createRightFlapEvent(self):
event = RightFlapEvent()
self.__registerEventToTarget(event, self.targets['RightSlingshotTarget'])
return event
def createLeftFlapEvent(self):
event = LeftFlapEvent()
self.__registerEventToTarget(event, self.targets['LeftSlingshotTarget'])
return event
def createRightKickerEvent(self):
event = RightKickerEvent()
self.__registerEventToTarget(event, self.targets['RightOutlaneKickerTarget'])
return event
def createEnterUpperPlayfieldEvent(self):
event = EnterUpperPlayfieldEvent(self.currentPlayer, self.upperPlayfieldFlippers)
self.__registerEventToTarget(event, self.targets['UpperPlayfieldRollUnderTarget'])
return event
def createStartGameEvent(self):
event = StartGameEvent(self.flipper)
self.__registerEventToTarget(event, self.targets['CreditTarget'])
return event
def __registerEventToTarget(self, event, target):
target.on(target.hit_key, event.trigger)

47
factories/GameFactory.py Normal file
View File

@@ -0,0 +1,47 @@
from config import *
from networking.Networking import Networking
from factories.PlayerStateFactory import PlayerStateFactory
from factories.DisplayFactory import DisplayFactory
from factories.TargetFactory import TargetFactory
from factories.EventFactory import EventFactory
from InputHandler import InputHandler
from GameState import GameState
class GameFactory:
def __init__(self):
displayNames = Networking().getDisplays()
self.displayFactory = DisplayFactory(displayNames)
specialDisplay = self.displayFactory.createSpecialDisplay()
self.playerCount = len(displayNames) - 1
self.playerStateFactory = PlayerStateFactory(specialDisplay)
def createGame(self):
targets = TargetFactory.createAllTargets()
inputHandler = InputHandler(targets)
players = self.createPlayers()
gameState = GameState(players, self.getHighScore(), players[0].id)
eventFactory = EventFactory(targets, gameState.currentPlayer)
pointEvent = eventFactory.createPointEvent()
def createPlayers(self):
players = []
for id in range(self.playerCount):
display = self.displayFactory.createPlayerDisplay(id)
player = self.playerStateFactory.createPlayerState(display, id)
players.append(player)
return players
def getHighScore(self): # todo
return 0

View File

@@ -0,0 +1,7 @@
from networking.Networking import Networking
class NetworkFactory:
@staticmethod
def createNetwork(output_server_address, input_server_address):
Networking(output_server_address, input_server_address)

View File

@@ -0,0 +1,9 @@
from config import *
from PlayerState import PlayerState
class PlayerStateFactory:
def __init__(self, specialDisplay):
self.specialDisplay = specialDisplay
def createPlayerState(self, display, id):
return PlayerState(display, self.specialDisplay, id, BALLS_PER_GAME, BEGINNING_UPPER_PLAYFIELD_TIME, BEGINNING_BONUS_TIME)

View File

@@ -0,0 +1,31 @@
from targets import *
from targets.BankTarget import BankTarget
class TargetFactory:
def createAllTargets(self):
result = dict()
glo = globals() # Save globals so the dict does not change during execution of the script
for key in glo:
if(type(target) != 'class'):
continue
target = glo[key]
if 'Target' in key:
result[key] = target()
elif 'Bank' in key and 'Target' not in key:
targets = self.createBankTarget(target, key)
result = {**result, **targets}
# todo check with driver?
return result
def createBankTarget(self, target, key):
bankTarget = target()
targets = dict()
targets[key] = bankTarget
for target in bankTarget.targets:
name = type(target).__name__
targets[name] = target
return targets

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,6 +1,6 @@
import requests_unixsocket as req import requests_unixsocket as req
import socket import socket
from src.utils.Singleton import Singleton from utils.Singleton import Singleton
class Networking(metaclass=Singleton): class Networking(metaclass=Singleton):
@@ -47,7 +47,7 @@ class Networking(metaclass=Singleton):
def writeDisplayScore(self, display, score): def writeDisplayScore(self, display, score):
self.get("/displays/" + display + "/write_score/" + str(score)) self.get("/displays/" + display + "/write_score/" + str(score))
def getInputEvent(self): async def getInputEvent(self):
header = list() header = list()
while b'\x02' not in header: while b'\x02' not in header:
byte = self.input_socket.recv(1) byte = self.input_socket.recv(1)

Binary file not shown.

View File

@@ -1,4 +1,4 @@
from src.Solenoid import Solenoid from Solenoid import Solenoid
class BottomLeftBankSolenoid(Solenoid): class BottomLeftBankSolenoid(Solenoid):
pass pass

View File

@@ -1,4 +1,4 @@
from src.Solenoid import Solenoid from Solenoid import Solenoid
class BottomRightBankSolenoid(Solenoid): class BottomRightBankSolenoid(Solenoid):
pass pass

View File

@@ -1,4 +1,4 @@
from src.Solenoid import Solenoid from Solenoid import Solenoid
class BottomRightPopSolenoid(Solenoid): class BottomRightPopSolenoid(Solenoid):
pass pass

View File

@@ -1,4 +1,4 @@
from src.Solenoid import Solenoid from Solenoid import Solenoid
class CoinMechanismCoilSolenoid(Solenoid): class CoinMechanismCoilSolenoid(Solenoid):
pass pass

View File

@@ -1,4 +1,4 @@
from src.Solenoid import Solenoid from Solenoid import Solenoid
class KnockerSolenoid(Solenoid): class KnockerSolenoid(Solenoid):
pass pass

View File

@@ -1,4 +1,4 @@
from src.Solenoid import Solenoid from Solenoid import Solenoid
class LeftFlapSolenoid(Solenoid): class LeftFlapSolenoid(Solenoid):
pass pass

View File

@@ -1,4 +1,4 @@
from src.Solenoid import Solenoid from Solenoid import Solenoid
class OutHoleSolenoid(Solenoid): class OutHoleSolenoid(Solenoid):
pass pass

View File

@@ -1,4 +1,4 @@
from src.Solenoid import Solenoid from Solenoid import Solenoid
class RightFlapSolenoid(Solenoid): class RightFlapSolenoid(Solenoid):
pass pass

View File

@@ -1,4 +1,4 @@
from src.Solenoid import Solenoid from Solenoid import Solenoid
class RightKickerSolenoid(Solenoid): class RightKickerSolenoid(Solenoid):
pass pass

View File

@@ -1,4 +1,4 @@
from src.Solenoid import Solenoid from Solenoid import Solenoid
class TopCentralBankSolenoid(Solenoid): class TopCentralBankSolenoid(Solenoid):
pass pass

View File

@@ -1,4 +1,4 @@
from src.Solenoid import Solenoid from Solenoid import Solenoid
class TopLeftPopSolenoid(Solenoid): class TopLeftPopSolenoid(Solenoid):
pass pass

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,3 +0,0 @@
class GameState:
def __init__(self, players):
self.players = players

View File

@@ -1,17 +0,0 @@
import asyncio
from src.networking.Networking import Networking
class EventHandler:
def __init__(self, targets):
self.isRunning = True
self.targets = targets
async def handleEvents(self):
self.isRunning = True
while(self.isRunning):
event = Networking.getInputEvent()
self.targets[event].hit()
def stop(self):
self.isRunning = False

View File

@@ -1,9 +0,0 @@
from src.networking.Networking import Networking
class Display:
def __init__(self, name):
self.name = name
def write_score(self, score):
Networking.write(self.name, score)

View File

@@ -1,7 +0,0 @@
from src.events.FlapEvent import FlapEvent
from src.solenoids.RightFlapSolenoid import RightFlapSolenoid
class RightFlapEvent(FlapEvent):
def __init__(self):
super(RightFlapSolenoid())

View File

@@ -1,6 +0,0 @@
class StartGameEvent:
def __init__(self, flipper):
self.flipper = flipper
def trigger(self):
flipper.activate()

View File

@@ -1,51 +0,0 @@
from src.config import *
from src.networking.Networking import Networking
from src.PlayerState import PlayerState
from src.displays.PlayerDisplay import PlayerDisplay
from src.displays.SpecialDisplay import SpecialDisplay
from src.EventHandler import EventHandler
from src.targets import *
from src.events.PointEvent import PointEvent
class GameFactory:
def createGame(self):
targets = self.createAllTargets()
eventHandler = EventHandler(targets)
displayNames = Networking.getDisplays()
playerDisplays = self.createPlayerDisplays(displayNames)
playerState = PlayerState()
pointEvent = PointEvent()
for target in self.targets:
target.on(target.hit_key,
pass
def createPlayerDisplays(self, displayNames):
result = []
for displayName in displayNames:
if("Player" in displayName):
display = PlayerDisplay(displayName)
result.append(display)
return result
def createPlayerStates(self, playerDisplays, specialDisplay, ballsPerGame = BALLS_PER_GAME, beginningUpperPlayfieldTime = BEGINNING_UPPER_PLAYFIELD_TIME, beginningBonusTime = BEGINNING_BONUS_TIME):
result = []
for i in range(len(playerDisplays)):
playerState = PlayerState(playerDisplays[i], specialDisplay, i, ballsPerGame, beginningUpperPlayfieldTime, beginningBonusTime)
result.append(playerState)
return result
def createAllTargets(self):
result = dict
glo = globals() # Save globals so the dict does not change during execution of the script
for key in glo:
target = glo[key]
if(key.endswith('Target') and type(target) == 'class'):
result[key] = target()
# todo check with driver?
return result

View File

@@ -1,46 +0,0 @@
output_server_address = ''
input_server_address = ''
#network = Networking(output_server_address, input_server_address)
network = ''
class EventHandler():
def __init__(self, network):
# brace yourselves, python incoming
self.subclasses = {subcls.__name__: subcls for subcls in self.__class__.__subclasses__()}
self.network = network
def handle(self, name):
self.subclasses[name](network)
class LeftFlapEventHandler(EventHandler):
def __init__(self, network):
print('Left Flap Event gets handled')
self.handle()
def handle(self):
# do stuff...
pass
class GameState:
def __init__(self, targets):
self.score = 0
self.targets = targets
for target in self.targets:
target.on(target.hit_key, self.__target_hit__)
def __target_hit__(self, target):
pass
def snake_to_camel(word):
return ''.join(x.capitalize() or '' for x in word.split('_'))
handler = EventHandler(network)
handler.handle(snake_to_camel('left_flap') + 'EventHandler')
# while True:
# event_name = snake_to_camel(network.getInputEvent())
# handler.handle(event_name + 'EventHandler')

View File

@@ -1,12 +0,0 @@
from src.targets.Bank import Bank
from src.targets.right_bank import RightBankLeft
from src.targets.right_bank import RightBankMiddle
from src.targets.right_bank import RightBankRight
class RightBank(Bank):
def __init__(self):
targets = [RightBankLeft(), RightBankMiddle(), RightBankRight]
super(0, "Right Bank", targets)
def hit(self):
super.hit()

View File

@@ -1,4 +1,4 @@
from src.targets.Target import Target from targets.Target import Target
class Bank(Target): class Bank(Target):
def __init__(self, points, name, targets): def __init__(self, points, name, targets):

View File

@@ -1,4 +1,4 @@
from src.targets.Target import Target from targets.Target import Target
class BankTarget(Target): class BankTarget(Target):
def __init__(self, points): def __init__(self, points):

View File

@@ -1,4 +1,4 @@
from src.targets.Target import Target from targets.Target import Target
class ReboundContactTarget(Target): class ReboundContactTarget(Target):
def __init__(self): def __init__(self):

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,4 +1,4 @@
from src.targets.Target import Target from targets.Target import Target
class LeftInlaneTarget(Target): class LeftInlaneTarget(Target):
def __init__(self): def __init__(self):

View File

@@ -1,4 +1,4 @@
from src.targets.Target import Target from targets.Target import Target
class LeftOutlaneTarget(Target): class LeftOutlaneTarget(Target):
def __init__(self): def __init__(self):

View File

@@ -1,4 +1,4 @@
from src.targets.Target import Target from targets.Target import Target
class RightInlaneTarget(Target): class RightInlaneTarget(Target):

View File

@@ -1,4 +1,4 @@
from src.targets.Target import Target from targets.Target import Target
class RightOutlaneKickerTarget(Target): class RightOutlaneKickerTarget(Target):

Some files were not shown because too many files have changed in this diff Show More