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.

237 regels
6.0 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. pygame.display.set_caption("Sub Crown Nov 2020 Team Picker")
  38. # Set up team text positioning references
  39. #
  40. # 26 teams so 3 cols, 9 rows
  41. COLSTART = int(WIDTH/3)
  42. COLWIDTH = int((WIDTH-COLSTART)/3)
  43. ROWSTART = int(HEIGHT/12)
  44. ROWHEIGHT = int((HEIGHT-ROWSTART)/10)
  45. TEAMSFILE = "teams.txt"
  46. TEAMNUMCOLS = 3
  47. TEAMNUMROWS = 9
  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 = 8
  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. TEAMS[-1].choose()
  108. pygame.time.set_timer(HIGHLIGHTEVENT, 0) # Disable highlight
  109. pygame.time.set_timer(CHOOSEEVENT, 5000, True) # make choice
  110. self.iterations = 0
  111. else:
  112. self.lastchoice = random.randrange(len(TEAMS))
  113. TEAMS[self.lastchoice].highlight()
  114. def draw_text(text, font, text_col, x, y):
  115. img = font.render(text, True, text_col)
  116. screen.blit(img, (x, y))
  117. def setup_team_board():
  118. """Grab teams from text file and set their positions"""
  119. with open(TEAMSFILE, "r") as fh:
  120. for row in range(TEAMNUMROWS):
  121. for col in range(TEAMNUMCOLS):
  122. name = fh.readline().strip()
  123. if name == "":
  124. continue
  125. print("Adding {} at {},{}".format(name,
  126. TEAMCOLS[col],
  127. TEAMROWS[row]))
  128. TEAMS.append(Team(name,
  129. TEAMROWS[row], TEAMCOLS[col]))
  130. def setup_subs():
  131. """Check positioning, not actually used"""
  132. for i in range(len(SUBS)):
  133. # print("drawing {} at {},{}".format(SUBS[i], 20, SUBROWS[i]))
  134. SUBS[i].y = SUBROWS[i]
  135. # Prep
  136. for line in open(SUBSFILE, "r"):
  137. sub = line.strip()
  138. SUBS.append(Sub(sub))
  139. print(SUBS)
  140. random.shuffle(SUBS)
  141. setup_team_board()
  142. random.shuffle(TEAMS)
  143. setup_subs()
  144. HIGHLIGHTER = Highlighter()
  145. while True:
  146. for event in pygame.event.get():
  147. if event.type == pygame.QUIT:
  148. sys.exit()
  149. elif event.type == pygame.KEYDOWN:
  150. if event.key == 113: # Q
  151. pygame.quit()
  152. sys.exit()
  153. elif event.key == 115:
  154. TEAMS[5].highlight()
  155. elif event.key == 100: # D for debug
  156. for sub in SUBS:
  157. if sub.team == "":
  158. sub.team = TEAMS.pop().team
  159. if sub._sub == "":
  160. sub._sub = sub.sub
  161. elif event.key == 32:
  162. # Display who we're choosing for
  163. for sub in SUBS:
  164. if sub._sub == "":
  165. sub._sub = sub.sub
  166. break
  167. # Start the "who's it going to pick?" animation
  168. pygame.time.set_timer(HIGHLIGHTEVENT, 100) # every 100ms
  169. print(event.key)
  170. elif event.type == HIGHLIGHTEVENT:
  171. HIGHLIGHTER.highlight()
  172. elif event.type == CLEAREVENT:
  173. for team in TEAMS:
  174. team.unhighlight()
  175. elif event.type == CHOOSEEVENT:
  176. for sub in SUBS:
  177. if sub.team == "":
  178. sub.team = TEAMS.pop().team
  179. pygame.time.set_timer(CLEAREVENT, 100, True)
  180. break
  181. screen.blit(BGIMG, (0, 0))
  182. for team in TEAMS:
  183. team.draw()
  184. for sub in SUBS:
  185. sub.draw()
  186. pygame.display.update()