Assigns teams to subscribers (used for Nov 2020 & Mar 2021) https://twitch.tv/bertiebaggio
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
5.6 KiB

  1. #!/bin/python3
  2. #
  3. # gamepicker.py - pick games for Novemeber 2020 sub crown
  4. #
  5. # Two files provided:
  6. # - subs.txt -- 8 subscribers
  7. # - teams.txt -- 26 teams
  8. #
  9. # Pseudocode:
  10. # Split screen in 1:3
  11. # - LHS = subscriber+team space
  12. # - RHS = teams space
  13. # Set BG image
  14. # Init seed
  15. # Lay out teams in order (3 cols)
  16. # On keypress, show subscriber name & show team random picker (via highlight)
  17. # Once picked, add subscriber + team to LHS
  18. import sys
  19. import random
  20. import pygame
  21. pygame.init()
  22. random.seed("BERTIE BEEF BAGGIO")
  23. HIGHLIGHTEVENT = pygame.USEREVENT + 1 # user specified event
  24. CLEAREVENT = pygame.USEREVENT + 2 # user specified event
  25. CHOOSEEVENT = pygame.USEREVENT + 3 # user specified event
  26. SIZE = WIDTH, HEIGHT = 1920, 1080
  27. BGIMG = pygame.image.load("sub_crown_bg_2.png")
  28. FONT = pygame.font.SysFont("Fetamont", 50)
  29. SUBFONTHEIGHT = 35
  30. SUBFONT = pygame.font.SysFont("Fetamont", SUBFONTHEIGHT)
  31. OFFWHITE = (224, 224, 224)
  32. HIGHLIGHT = (255, 209, 0)
  33. BLUE = (20, 25, 153)
  34. RED = (186, 20, 31)
  35. PURPLE = (121, 35, 158)
  36. screen = pygame.display.set_mode(SIZE)
  37. # Set up team text positioning references
  38. #
  39. # 26 teams so 3 cols, 9 rows
  40. COLSTART = int(WIDTH/3)
  41. COLWIDTH = int((WIDTH-COLSTART)/3)
  42. ROWSTART = int(HEIGHT/12)
  43. ROWHEIGHT = int((HEIGHT-ROWSTART)/10)
  44. TEAMSFILE = "teams.txt"
  45. TEAMNUMCOLS = 3
  46. TEAMNUMROWS = 9
  47. TEAMCOLS = []
  48. TEAMROWS = []
  49. for i in range(TEAMNUMCOLS):
  50. print(COLSTART + i*COLWIDTH)
  51. TEAMCOLS.append(COLSTART + i*COLWIDTH)
  52. for i in range(TEAMNUMROWS):
  53. TEAMROWS.append(ROWSTART + i*ROWHEIGHT)
  54. print(ROWSTART + i*ROWHEIGHT)
  55. SUBSFILE = "subs.txt"
  56. SUBS = []
  57. SUBNUMROWS = 8
  58. SUBROWHEIGHT = int((HEIGHT-ROWSTART)/9)
  59. SUBROWS = []
  60. for i in range(SUBNUMROWS):
  61. SUBROWS.append(ROWSTART + i*SUBROWHEIGHT)
  62. TEAMS = [] # List of Team
  63. class Team(object):
  64. """Teams for iteration"""
  65. def __init__(self, team, x, y):
  66. self.team = team
  67. self.x = x
  68. self.y = y
  69. self.colour = OFFWHITE
  70. def highlight(self):
  71. self.colour = HIGHLIGHT
  72. def unhighlight(self):
  73. self.colour = OFFWHITE
  74. def choose(self):
  75. self.colour = RED
  76. def draw(self):
  77. draw_text(self.team, FONT, self.colour, self.y, self.x)
  78. class Sub(object):
  79. """Like Team"""
  80. def __init__(self, sub):
  81. self.sub = sub
  82. self.team = ""
  83. self.x = 20
  84. self.y = 0
  85. def draw(self):
  86. draw_text(self.sub, SUBFONT, BLUE, self.x, self.y)
  87. if self.team != "":
  88. draw_text(self.team, SUBFONT, PURPLE,
  89. self.x, int(self.y+SUBFONTHEIGHT))
  90. class Highlighter(object):
  91. """For highlighting teams randomly"""
  92. def __init__(self):
  93. self.team = None
  94. self.iterations = 0
  95. self.max = 70
  96. self.lastchoice = -1
  97. def highlight(self):
  98. if self.lastchoice >= 0:
  99. TEAMS[self.lastchoice].unhighlight()
  100. self.iterations += 1
  101. if self.iterations >= self.max:
  102. # Choosing time
  103. self.lastchoice = -1
  104. TEAMS[-1].choose()
  105. pygame.time.set_timer(HIGHLIGHTEVENT, 0) # Disable highlight
  106. pygame.time.set_timer(CHOOSEEVENT, 5000, True) # make choice
  107. self.iterations = 0
  108. else:
  109. self.lastchoice = random.randrange(len(TEAMS))
  110. TEAMS[self.lastchoice].highlight()
  111. def draw_text(text, font, text_col, x, y):
  112. img = font.render(text, True, text_col)
  113. screen.blit(img, (x, y))
  114. def setup_team_board():
  115. """Grab teams from text file and set their positions"""
  116. with open(TEAMSFILE, "r") as fh:
  117. for row in range(TEAMNUMROWS):
  118. for col in range(TEAMNUMCOLS):
  119. name = fh.readline().strip()
  120. if name == "":
  121. continue
  122. print("Adding {} at {},{}".format(name,
  123. TEAMCOLS[col],
  124. TEAMROWS[row]))
  125. TEAMS.append(Team(name,
  126. TEAMROWS[row], TEAMCOLS[col]))
  127. def setup_subs():
  128. """Check positioning, not actually used"""
  129. for i in range(len(SUBS)):
  130. # print("drawing {} at {},{}".format(SUBS[i], 20, SUBROWS[i]))
  131. SUBS[i].y = SUBROWS[i]
  132. # Prep
  133. for line in open(SUBSFILE, "r"):
  134. sub = line.strip()
  135. SUBS.append(Sub(sub))
  136. print(SUBS)
  137. random.shuffle(SUBS)
  138. setup_team_board()
  139. random.shuffle(TEAMS)
  140. setup_subs()
  141. HIGHLIGHTER = Highlighter()
  142. while True:
  143. for event in pygame.event.get():
  144. if event.type == pygame.QUIT:
  145. sys.exit()
  146. elif event.type == pygame.KEYDOWN:
  147. if event.key == 113: # Q
  148. pygame.quit()
  149. sys.exit()
  150. elif event.key == 115:
  151. TEAMS[5].highlight()
  152. elif event.key == 100: # D for debug
  153. for sub in SUBS:
  154. if sub.team == "":
  155. sub.team = TEAMS.pop().team
  156. elif event.key == 32:
  157. pygame.time.set_timer(HIGHLIGHTEVENT, 100) # every 100ms
  158. print(event.key)
  159. elif event.type == HIGHLIGHTEVENT:
  160. HIGHLIGHTER.highlight()
  161. elif event.type == CLEAREVENT:
  162. for team in TEAMS:
  163. team.unhighlight()
  164. elif event.type == CHOOSEEVENT:
  165. for sub in SUBS:
  166. if sub.team == "":
  167. sub.team = TEAMS.pop().team
  168. pygame.time.set_timer(CLEAREVENT, 100, True)
  169. break
  170. screen.blit(BGIMG, (0, 0))
  171. for team in TEAMS:
  172. team.draw()
  173. for sub in SUBS:
  174. sub.draw()
  175. pygame.display.update()