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.

267 lines
7.1 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 -- 30 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("bertieb")
  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", 45)
  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. pygame.display.set_caption("Sub Crown September 2021 Team Picker")
  38. # Set up team text positioning references
  39. #
  40. # 26 teams so 3 cols, 9 rows
  41. TEAMSFILE = "teams.txt"
  42. TEAMNUMCOLS = 3
  43. TEAMNUMROWS = 12
  44. COLSTART = int(WIDTH/TEAMNUMCOLS)
  45. COLWIDTH = int((WIDTH-COLSTART)/TEAMNUMCOLS)
  46. ROWSTART = int(HEIGHT/(TEAMNUMROWS+2))
  47. ROWHEIGHT = int((HEIGHT-ROWSTART)/TEAMNUMROWS)
  48. TEAMCOLS = []
  49. TEAMROWS = []
  50. for i in range(TEAMNUMCOLS):
  51. print(COLSTART + i*COLWIDTH)
  52. TEAMCOLS.append(COLSTART + i*COLWIDTH)
  53. for i in range(TEAMNUMROWS):
  54. TEAMROWS.append(ROWSTART + i*ROWHEIGHT)
  55. print(ROWSTART + i*ROWHEIGHT)
  56. SUBSFILE = "subs.txt"
  57. SUBS = []
  58. SUBNUMROWS = 9
  59. SUBROWHEIGHT = int((HEIGHT-ROWSTART)/9)
  60. SUBROWS = []
  61. for i in range(SUBNUMROWS):
  62. SUBROWS.append(ROWSTART + i*SUBROWHEIGHT)
  63. TEAMS = [] # List of Team
  64. class Team(object):
  65. """Teams for iteration"""
  66. def __init__(self, team, x, y):
  67. self.team = team
  68. self.x = x
  69. self.y = y
  70. self.colour = OFFWHITE
  71. def highlight(self):
  72. self.colour = HIGHLIGHT
  73. def unhighlight(self):
  74. self.colour = OFFWHITE
  75. def choose(self):
  76. self.colour = RED
  77. def draw(self):
  78. draw_text(self.team, FONT, self.colour, self.y, self.x)
  79. class Sub(object):
  80. """Like Team"""
  81. def __init__(self, sub):
  82. self.sub = sub
  83. self._sub = ""
  84. self.team = ""
  85. self.x = 20
  86. self.y = 0
  87. def draw(self):
  88. if self._sub != "":
  89. draw_text(self.sub, SUBFONT, BLUE, self.x, self.y)
  90. if self.team != "":
  91. draw_text(self.team, SUBFONT, PURPLE,
  92. self.x, int(self.y+SUBFONTHEIGHT))
  93. class Highlighter(object):
  94. """For highlighting teams randomly"""
  95. def __init__(self):
  96. self.team = None
  97. self.iterations = 0
  98. self.max = 70
  99. self.lastchoice = -1
  100. def highlight(self):
  101. if self.lastchoice >= 0:
  102. TEAMS[self.lastchoice].unhighlight()
  103. self.iterations += 1
  104. if self.iterations >= self.max:
  105. # Choosing time
  106. self.lastchoice = -1
  107. # # ** Name-specific choices! ** # #
  108. for tsub in SUBS:
  109. if tsub.team == "":
  110. choosing_sub = tsub
  111. break
  112. for j in range(len(TEAMS)):
  113. if "amy" not in choosing_sub.sub:
  114. if TEAMS[j].team.lower(
  115. ).startswith(choosing_sub.sub[0].lower()):
  116. TEAMS[j].choose()
  117. break
  118. else:
  119. if TEAMS[j].team.lower().startswith("a"):
  120. TEAMS[j].choose()
  121. break
  122. # # End Name-specific stuff # #
  123. # TEAMS[-1].choose()
  124. pygame.time.set_timer(HIGHLIGHTEVENT, 0) # Disable highlight
  125. pygame.time.set_timer(CHOOSEEVENT, 5000, True) # make choice
  126. self.iterations = 0
  127. else:
  128. self.lastchoice = random.randrange(len(TEAMS))
  129. TEAMS[self.lastchoice].highlight()
  130. def draw_text(text, font, text_col, x, y):
  131. img = font.render(text, True, text_col)
  132. screen.blit(img, (x, y))
  133. def setup_team_board(randomise_teams=False):
  134. """Grab teams from text file and set their positions"""
  135. teamlist = []
  136. with open(TEAMSFILE, "r") as fh:
  137. teams = fh.readlines()
  138. teamlist = [team.rstrip() for team in teams if team != ""]
  139. if randomise_teams:
  140. random.shuffle(teamlist)
  141. for row in range(TEAMNUMROWS):
  142. for col in range(TEAMNUMCOLS):
  143. if len(teamlist) == 0:
  144. continue
  145. name = teamlist.pop(0)
  146. print("Adding {} at {},{}".format(name,
  147. TEAMCOLS[col],
  148. TEAMROWS[row]))
  149. TEAMS.append(Team(name,
  150. TEAMROWS[row], TEAMCOLS[col]))
  151. def setup_subs():
  152. """Check positioning, not actually used"""
  153. for i in range(len(SUBS)):
  154. # print("drawing {} at {},{}".format(SUBS[i], 20, SUBROWS[i]))
  155. SUBS[i].y = SUBROWS[i]
  156. # Prep
  157. for line in open(SUBSFILE, "r"):
  158. sub = line.strip()
  159. SUBS.append(Sub(sub))
  160. print(SUBS)
  161. random.shuffle(SUBS)
  162. setup_team_board(randomise_teams=True)
  163. random.shuffle(TEAMS)
  164. setup_subs()
  165. HIGHLIGHTER = Highlighter()
  166. while True:
  167. for event in pygame.event.get():
  168. if event.type == pygame.QUIT:
  169. sys.exit()
  170. elif event.type == pygame.KEYDOWN:
  171. if event.key == 113: # Q
  172. pygame.quit()
  173. sys.exit()
  174. elif event.key == 115:
  175. TEAMS[5].highlight()
  176. elif event.key == 100: # D for debug
  177. for sub in SUBS:
  178. if sub.team == "":
  179. sub.team = TEAMS.pop().team
  180. if sub._sub == "":
  181. sub._sub = sub.sub
  182. elif event.key == 32:
  183. # Display who we're choosing for
  184. for sub in SUBS:
  185. if sub._sub == "":
  186. sub._sub = sub.sub
  187. break
  188. # Start the "who's it going to pick?" animation
  189. pygame.time.set_timer(HIGHLIGHTEVENT, 100) # every 100ms
  190. print(event.key)
  191. elif event.type == HIGHLIGHTEVENT:
  192. HIGHLIGHTER.highlight()
  193. elif event.type == CLEAREVENT:
  194. for team in TEAMS:
  195. team.unhighlight()
  196. elif event.type == CHOOSEEVENT:
  197. for sub in SUBS:
  198. if sub.team == "":
  199. # # Name-specific again # #
  200. for k in range(len(TEAMS)):
  201. if TEAMS[k].colour == RED:
  202. sub.team = TEAMS.pop(k).team
  203. break
  204. # # End name-specific # #
  205. # sub.team = TEAMS.pop().team
  206. pygame.time.set_timer(CLEAREVENT, 100, True)
  207. break
  208. screen.blit(BGIMG, (0, 0))
  209. for team in TEAMS:
  210. team.draw()
  211. for sub in SUBS:
  212. sub.draw()
  213. pygame.display.update()