Assigns teams to subscribers (used for Nov 2020 & Mar 2021) https://twitch.tv/bertiebaggio
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

teampicker.py 6.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. 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. if "donkey" in name:
  126. name = name.split(' ', 1)[1]
  127. print("Adding {} at {},{}".format(name,
  128. TEAMCOLS[col],
  129. TEAMROWS[row]))
  130. TEAMS.append(Team(name,
  131. TEAMROWS[row], TEAMCOLS[col]))
  132. def setup_subs():
  133. """Check positioning, not actually used"""
  134. for i in range(len(SUBS)):
  135. # print("drawing {} at {},{}".format(SUBS[i], 20, SUBROWS[i]))
  136. SUBS[i].y = SUBROWS[i]
  137. # Prep
  138. for line in open(SUBSFILE, "r"):
  139. sub = line.strip()
  140. SUBS.append(Sub(sub))
  141. print(SUBS)
  142. random.shuffle(SUBS)
  143. setup_team_board()
  144. random.shuffle(TEAMS)
  145. setup_subs()
  146. HIGHLIGHTER = Highlighter()
  147. while True:
  148. for event in pygame.event.get():
  149. if event.type == pygame.QUIT:
  150. sys.exit()
  151. elif event.type == pygame.KEYDOWN:
  152. if event.key == 113: # Q
  153. pygame.quit()
  154. sys.exit()
  155. elif event.key == 115:
  156. TEAMS[5].highlight()
  157. elif event.key == 100: # D for debug
  158. for sub in SUBS:
  159. if sub.team == "":
  160. sub.team = TEAMS.pop().team
  161. if sub._sub == "":
  162. sub._sub = sub.sub
  163. elif event.key == 32:
  164. # Display who we're choosing for
  165. for sub in SUBS:
  166. if sub._sub == "":
  167. sub._sub = sub.sub
  168. break
  169. # Start the "who's it going to pick?" animation
  170. pygame.time.set_timer(HIGHLIGHTEVENT, 100) # every 100ms
  171. print(event.key)
  172. elif event.type == HIGHLIGHTEVENT:
  173. HIGHLIGHTER.highlight()
  174. elif event.type == CLEAREVENT:
  175. for team in TEAMS:
  176. team.unhighlight()
  177. elif event.type == CHOOSEEVENT:
  178. for sub in SUBS:
  179. if sub.team == "":
  180. sub.team = TEAMS.pop().team
  181. pygame.time.set_timer(CLEAREVENT, 100, True)
  182. break
  183. screen.blit(BGIMG, (0, 0))
  184. for team in TEAMS:
  185. team.draw()
  186. for sub in SUBS:
  187. sub.draw()
  188. pygame.display.update()