...
 
Commits (2)
......@@ -55,4 +55,9 @@ class PongConfig:
minZoneSize = 0.11
reboundZoneShrink = 0.018
# MQTT
doMqtt = True
mqttHost = "mqtt.devlol.org"
mqttBaseTopic = "codepong/"
......@@ -37,6 +37,8 @@ from readereventqueue import ReaderEvent, ReaderEventQueue, EventType
from pygletcanvas import PygletCanvas
import pyglet
import paho.mqtt.client as mqtt
# game core
import gamesim
import gameelements
......@@ -84,6 +86,13 @@ class PongGame:
# setup rendering
self.canvas = PygletCanvas(self.ball, self.gamefield, self.leftPlayer, self.rightPlayer)
# mqtt
self.mqttClient = None
if PongConfig.doMqtt:
self.mqttClient = mqtt.Client()
self.mqttClient.connect(PongConfig.mqttHost, 1883, 60)
def setupControllers(self, numControllers=2):
#FIXME move this to end of method
......@@ -183,12 +192,18 @@ class PongGame:
if self.leftPlayer.lifes <= 0:
self.menuText = "Right Player Wins!"
self.gameState = GameState.STARTGAME
print("RIGHT PLAYER WINS!")
print("RIGHT PLAYER WINS!", flush=True)
if self.mqttClient:
topic = PongConfig.mqttBaseTopic + "win"
self.mqttClient.publish(topic, payload="RIGHT_PLAYER", qos=0, retain=False)
return
if self.rightPlayer.lifes <= 0:
self.menuText = "Left Player Wins!"
self.gameState = GameState.STARTGAME
print("LEFT PLAYER WINS!")
if self.mqttClient:
topic = PongConfig.mqttBaseTopic + "win"
self.mqttClient.publish(topic, payload="LEFT_PLAYER", qos=0, retain=False)
print("LEFT PLAYER WINS!", flush=True)
return
# setup ball
......@@ -238,6 +253,9 @@ class PongGame:
self.ball.velocity *= PongConfig.reboundSpeedUp
if self.gamefield.leftZoneSize > PongConfig.minZoneSize:
self.gamefield.leftZoneSize -= PongConfig.reboundZoneShrink
if self.mqttClient:
topic = PongConfig.mqttBaseTopic + "scan"
self.mqttClient.publish(topic, payload="LEFT_PLAYER", qos=0, retain=False)
# right player
if (ev.reader.name == self.rightPlayer.ctrID and
......@@ -247,6 +265,9 @@ class PongGame:
self.ball.velocity *= PongConfig.reboundSpeedUp
if self.gamefield.rightZoneSize > PongConfig.minZoneSize:
self.gamefield.rightZoneSize -= PongConfig.reboundZoneShrink
if self.mqttClient:
topic = PongConfig.mqttBaseTopic + "scan"
self.mqttClient.publish(topic, payload="RIGHT_PLAYER", qos=0, retain=False)
# a controller was disconnected
if (ev.etype == EventType.DISCONNECT):
......@@ -262,9 +283,15 @@ class PongGame:
if self.gamefield.ballLeftOut(self.ball):
self.leftPlayer.lifes -= 1
self.gameState = GameState.SETUPRALLY
if self.mqttClient:
topic = PongConfig.mqttBaseTopic + "out"
self.mqttClient.publish(topic, payload="LEFT_PLAYER", qos=0, retain=False)
if self.gamefield.ballRightOut(self.ball):
self.rightPlayer.lifes -= 1
self.gameState = GameState.SETUPRALLY
if self.mqttClient:
topic = PongConfig.mqttBaseTopic + "out"
self.mqttClient.publish(topic, payload="RIGHT_PLAYER", qos=0, retain=False)
......