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.

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