Created
February 9, 2024 05:39
-
-
Save cole-wilson/5d5c56865cb1cab7b77b8b261e75b874 to your computer and use it in GitHub Desktop.
Combat Robotics Challonge Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import challonge | |
import time | |
class Tournament(): | |
_id = None | |
participants = [] | |
matches = [] | |
printed = [] | |
def __init__(self, _id: str): | |
"""set up our Tournament class with id""" | |
self._id = _id | |
self.update() | |
self.printed = [False for _ in self.matches] | |
def update(self): | |
"""fetch the new data""" | |
self.matches = challonge.matches.index(self._id) | |
self.participants = challonge.participants.index(self._id) | |
def printMatch(self, matchNum, match): | |
"""function will consume match number and print the details""" | |
self.printed[matchNum] = True | |
print(f"Match {matchNum+1}\n") | |
player1 = self.participants.filter(lambda r: r["id"] == match["player1_id"])[0]["name"] | |
player2 = self.participants.filter(lambda r: r["id"] == match["player2_id"])[0]["name"] | |
printer = open('/dev/usb/lp0', 'w') | |
printer.write(f"Match {matchNum+1}\n") | |
printer.write(f"{player1} vs {player2}\n") | |
printer.write("\n\n\n") | |
printer.flush() | |
printer.close() | |
def checkAndPrint(self): | |
"""print all finalized matches and update""" | |
for matchIndex, match in enumerate(self.matches): | |
if match["player1_id"] and match["player2_id"] and not self.printed[matchIndex]: | |
self.printMatch(matchIndex, match) | |
self.update() | |
return all(self.printed) | |
if __name__ == "__main__": | |
import config | |
challonge.set_credentials(config.api_usr, config.api_key) | |
tournament1 = Tournament("sn1rdt6a") | |
tournament2 = Tournament("some other id...") | |
oneDone = twoDone = False | |
while not (oneDone and twoDone): | |
oneDone = tournament1.checkAndPrint() | |
twoDone = tournament2.checkAndPrint() | |
time.sleep(10) | |
print("all labels printed!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment