def test_loseGameReturnsTrueWhenThereIsAnOpponentInGame(self): file = open('gunid', 'r+') file.truncate() file.write( "# User Identification File.\n# Change gunid and username.\n# Rename file to gunid.\ngunid=3\nusername=Benjamin\n" ) file.close() gun = Gun() gun.readIDFile() file = open('gunid', 'r+') file.truncate() file.write( "# User Identification File.\n# Change gunid and username.\n# Rename file to gunid.\ngunid=4\nusername=Thomas\n" ) file.close() gun2 = Gun() gun2.readIDFile() mydb = connect.connect() cursor = mydb.cursor() sql = "INSERT INTO Games (current_state, winner, game_date) VALUES (2, 0, (NOW() - INTERVAL 4 HOUR + INTERVAL 11 MINUTE - INTERVAL 22 SECOND))" cursor.execute(sql) mydb.commit() gun.joinGame() gun2.joinGame() self.assertTrue(gun2.loseGame()) mydb.close()
def test_fireShotCorrectlyIncrementsGunShotsFiredInDatabase(self): file = open('gunid', 'r+') file.truncate() file.write( "# User Identification File.\n# Change gunid and username.\n# Rename file to gunid.\ngunid=3\nusername=Benjamin\n" ) file.close() gun = Gun() gun.readIDFile() mydb = connect.connect() cursor = mydb.cursor() sql = ("SELECT * FROM Guns " "WHERE gun='{}'").format(gun.id) cursor.execute(sql) myresult = cursor.fetchall() oldShots = myresult[0][3] gun.fireShot() sql = ("SELECT * FROM Guns " "WHERE gun='{}'").format(gun.id) cursor.execute(sql) myresult = cursor.fetchall() newShots = myresult[0][3] mydb.close() self.assertEqual((oldShots + 1), newShots)
def test_joinGameCorrectlyAddsUserIntoGame(self): file = open('gunid', 'r+') file.truncate() file.write( "# User Identification File.\n# Change gunid and username.\n# Rename file to gunid.\ngunid=3\nusername=Benjamin\n" ) file.close() gun = Gun() gun.readIDFile() mydb = connect.connect() cursor = mydb.cursor() sql = "INSERT INTO Games (current_state, winner, game_date) VALUES (1, 0, (NOW() - INTERVAL 4 HOUR + INTERVAL 11 MINUTE - INTERVAL 22 SECOND))" cursor.execute(sql) mydb.commit() gun.joinGame() sql = "SELECT * FROM (Games INNER JOIN Game_Users ON Games.id = Game_Users.game_id) WHERE Games.current_state = 1" cursor.execute(sql) myresult = cursor.fetchall() self.assertEqual(len(myresult), 1) #only one row added to database self.assertEqual(myresult[0][5], 3) #gunid is 3 self.assertEqual(myresult[0][6], "Benjamin") #username is Benjamin sql = "UPDATE Games SET current_state=0 WHERE current_state=1" cursor.execute(sql) mydb.commit() mydb.close()
def test_checkGameReturnsFalseWhenThereIsNoActiveGame(self): file = open('gunid', 'r+') file.truncate() file.write( "# User Identification File.\n# Change gunid and username.\n# Rename file to gunid.\ngunid=3\nusername=Benjamin\n" ) file.close() gun = Gun() gun.readIDFile() self.assertFalse(gun.checkGame())
def test_readIDFileAssignsCorrectValuesFromProperlyFormattedFile(self): file = open('gunid', 'r+') file.truncate() file.write( "# User Identification File.\n# Change gunid and username.\n# Rename file to gunid.\ngunid=3\nusername=Benjamin\n" ) file.close() gun = Gun() gun.readIDFile() self.assertEqual(gun.id, 3) self.assertEqual(gun.username, "Benjamin")
def test_loseGameCorrectlyUpdatesWinnerStats(self): file = open('gunid', 'r+') file.truncate() file.write( "# User Identification File.\n# Change gunid and username.\n# Rename file to gunid.\ngunid=3\nusername=Benjamin\n" ) file.close() gun = Gun() gun.readIDFile() file = open('gunid', 'r+') file.truncate() file.write( "# User Identification File.\n# Change gunid and username.\n# Rename file to gunid.\ngunid=4\nusername=Thomas\n" ) file.close() gun2 = Gun() gun2.readIDFile() mydb = connect.connect() cursor = mydb.cursor() sql = "INSERT INTO Games (current_state, winner, game_date) VALUES (2, 0, (NOW() - INTERVAL 4 HOUR + INTERVAL 11 MINUTE - INTERVAL 22 SECOND))" cursor.execute(sql) mydb.commit() sql = "SELECT * FROM Guns WHERE gun=3" cursor.execute(sql) myresult = cursor.fetchall() oldGunWins = myresult[0][1] sql = "SELECT * FROM Players WHERE username='******'" cursor.execute(sql) myresult = cursor.fetchall() oldPlayerWins = myresult[0][1] gun.joinGame() gun2.joinGame() gun2.loseGame() sql = "SELECT * FROM Guns WHERE gun=3" cursor.execute(sql) myresult = cursor.fetchall() newGunWins = myresult[0][1] sql = "SELECT * FROM Players WHERE username='******'" cursor.execute(sql) myresult = cursor.fetchall() newPlayerWins = myresult[0][1] self.assertEqual((oldGunWins + 1), newGunWins) self.assertEqual((oldPlayerWins + 1), newPlayerWins) mydb.close()
class Trigger: """ The Trigger class handles all trigger events for the gun hardware. Establishing which gun has which id and username, "shooting" an ir shot, and adding or deleting a trigger for event detection (for a gun). """ def __init__(self): """ Constructor for the Trigger class. Postconditions: The gunid and user name will be assigned to a "gun" (to hardware). """ self.TRIGGER = 19 GPIO.setmode(GPIO.BCM) GPIO.setup(self.TRIGGER, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) self.gun = Gun() self.gun.readIDFile() self.shotID = "Shot" + str(self.gun.id).zfill(2) def __del__(self): GPIO.cleanup() # GPIO.remove_event_detect(self.TRIGGER) def shoot(self, pin): """ shoot Preconditions: The constructor has been called and a game has been started. Parameter pin is a GPIO input pin on the raspberry pi zero board. Postconditions: A shot will be registered to the database via the fireShot definition in the gun class. """ if GPIO.input(pin) == GPIO.HIGH: print(str(datetime.datetime.now()), "Shot") call(["irsend", "SEND_ONCE", "laserpi", self.shotID]) self.gun.fireShot() return True def addTrigger(self): """ addTrigger Preconditions: The constructor has been called. Postconditions: Event detection for a trigger will be established. """ GPIO.add_event_detect(self.TRIGGER, GPIO.RISING, callback=self.shoot, bouncetime=50)
def test_readIDFileReturnsFalseFromPoorlyFormattedFile(self): file = open('gunid', 'r+') file.truncate() file.write("not a good file") file.close() gun = Gun() self.assertFalse(gun.readIDFile())
def test_validateReturnsTrueWhenInputIsProperlyFormatted(self): file = open('gunid', 'r+') file.truncate() file.write( "# User Identification File.\n# Change gunid and username.\n# Rename file to gunid.\ngunid=3\nusername=Benjamin\n" ) file.close() gun = Gun() self.assertTrue(gun.readIDFile())
def test_validateReturnsFalseWhenPlayerIsNotRegistered(self): file = open('gunid', 'r+') file.truncate() file.write( "# User Identification File.\n# Change gunid and username.\n# Rename file to gunid.\ngunid=3\nusername=None\n" ) file.close() gun = Gun() self.assertFalse(gun.readIDFile())
def test_validateReturnsFalseWhenIdIs0(self): file = open('gunid', 'r+') file.truncate() file.write( "# User Identification File.\n# Change gunid and username.\n# Rename file to gunid.\ngunid=0\nusername=Benjamin\n" ) file.close() gun = Gun() self.assertFalse(gun.readIDFile())
def test_checkGameReturnsTrueWhenThereIsAJoiningGame(self): file = open('gunid', 'r+') file.truncate() file.write( "# User Identification File.\n# Change gunid and username.\n# Rename file to gunid.\ngunid=3\nusername=Benjamin\n" ) file.close() gun = Gun() gun.readIDFile() mydb = connect.connect() cursor = mydb.cursor() sql = "INSERT INTO Games (current_state, winner, game_date) VALUES (1, 0, (NOW() - INTERVAL 4 HOUR + INTERVAL 11 MINUTE - INTERVAL 22 SECOND))" cursor.execute(sql) mydb.commit() self.assertTrue(gun.checkGame()) sql = "UPDATE Games SET current_state=0 WHERE current_state=1" cursor.execute(sql) mydb.commit() mydb.close()
from trigger import Trigger from led import LED import lirc from time import sleep #prog defined in ~/.lircrc prog = "guns" try: sockid = lirc.init(prog, blocking=False) trigger = Trigger() trigger.addTrigger() gun = Gun() led = LED() hp = 3 if (not gun.readIDFile()): quit() inGame = False while True: while not inGame: joined = gun.joinGame() if joined: inGame = True hp = 3 led.toggleLED('red') sleep(0.5) while inGame: if hp == 3: led.toggleLED('green') led.setLED('red', 'off') elif hp == 2: led.toggleLED('green')