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.

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