Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.12 KB | None | 0 0
  1. # purpose: draft_menu creates an instance of the standard draft menu. It
  2. #          contains internal functions to create and modify pokemon lists.
  3. def draft_menu():
  4.     # create the popup menu
  5.     d_m = Toplevel()
  6.     d_m.resizable(False, False)
  7.  
  8.     # initialize the state of each pool pokemon (True = unpicked)
  9.     draft_menu.pokemon_state = [True for i in range(15)]
  10.     # initialize the state of drafting (False = unfinished)
  11.     draft_menu.donebool = False
  12.  
  13.  
  14.     # purpose: undo_action will remove the most recent pokemon added. The var x
  15.     #          contains the position of the most recent pokemon to undo and the
  16.     #          var y identifies which team it is on.
  17.     def undo_action(x, y):
  18.         # remove pokemon info from lists (y is team 1 or team 2)
  19.         for i in range(15):
  20.             generate_draft.pokemon_list[y][i].pop()
  21.         # revert icon from picked unpicked
  22.         reverted_img = PhotoImage(file=poke_icon(generate_draft.pokemon_list[2][1], generate_draft.pokemon_list[2][2], x))
  23.         current_img = PhotoImage(file='img\\current_mystery_pokemon_icon.gif')
  24.         mystery_img = PhotoImage(file='img\\mystery_pokemon_icon.gif')
  25.         pool_pokemon[x].config(image=reverted_img)
  26.         pool_pokemon[x].image = reverted_img
  27.  
  28.         # allow the pokemon to be picked again in the pool
  29.         draft_menu.pokemon_state[x] = True
  30.  
  31.         # loop through pokemon 1 ~ 6 on each team
  32.         for i in range(1, 13):
  33.             # find the current pokemon
  34.             if generate_draft.most_recent_pokemon2 == i:
  35.                 # i is odd = remove from team 1
  36.                 if i % 2 == 1:
  37.                     # revert the pokemon icon and current team icon
  38.                     team_pokemon[y][i/2].config(image=current_img)
  39.                     team_pokemon[y][i/2].image = current_img
  40.                     team_pokemon[y+1][i/2].config(image=mystery_img)
  41.                     team_pokemon[y+1][i/2].image = mystery_img
  42.                 # i is even = remove from team 2
  43.                 else:
  44.                     # revert the pokemon icon and current team icon
  45.                     team_pokemon[y][(i-1)/2].config(image=current_img)
  46.                     team_pokemon[y][(i-1)/2].image = current_img
  47.                     if i < 12:
  48.                         # apply only to pokemon 1 ~ 5 (i/2 = 6 out of bounds)
  49.                         team_pokemon[y-1][i/2].config(image=mystery_img)
  50.                         team_pokemon[y-1][i/2].image = mystery_img
  51.                     else:
  52.                         # revert drafting and showdown button states
  53.                         draft_menu.donebool = False
  54.                         showdown2_img = PhotoImage(file='img\\showdown_unavailable.gif')
  55.                         showdown_button.config(image=showdown2_img, command=lambda: None)
  56.                         showdown_button.image = showdown2_img
  57.                         main.draft_session_counter -= 1
  58.  
  59.         # remove pokemon from recent lists
  60.         generate_draft.most_recent_pokemon2 -= 1
  61.         generate_draft.most_recent_pokemon.pop()
  62.  
  63.         # check if there are no more actions to undo
  64.         if (generate_draft.most_recent_pokemon2) == 0:
  65.             # revert undo button state
  66.             undo_img = PhotoImage(file='img\\undo_button_unavailable.gif')
  67.             undo_button.config(image=undo_img, command=lambda: None)
  68.             undo_button.image = undo_img
  69.         else:
  70.             # change undo button to remove the next pokemon in most recent list
  71.             undo_button.config(command=lambda: undo_action(generate_draft.most_recent_pokemon[-1], ((y+1)%2)))
  72.  
  73.  
  74.     # purpose: update_pool_pokemon handles the selection process of the draft.
  75.     #          The var x is the position of the pool pokemon.
  76.     def update_pool_pokemon(x):
  77.         # check if the selected pokemon is unpicked
  78.         if draft_menu.pokemon_state[x]:
  79.             # make the pokemon at position x picked
  80.             draft_menu.pokemon_state[x] = False
  81.             print draft_menu.pokemon_state
  82.             # retrieve the corresponding pokemon icon
  83.             new_img = PhotoImage(file=poke_icon(generate_draft.pokemon_list[2][1], generate_draft.pokemon_list[2][2], x))
  84.             next_img = PhotoImage(file='img\\current_mystery_pokemon_icon.gif')
  85.  
  86.             # check if drafting is not finished, otherwise do nothing
  87.             if draft_menu.donebool == False:
  88.                 # change the pokemon icon in the pool from unpicked to picked
  89.                 picked_img = PhotoImage(file=poke_icon_picked(generate_draft.pokemon_list[2][1], generate_draft.pokemon_list[2][2], x))
  90.                 pool_pokemon[x].config(image=picked_img)
  91.                 pool_pokemon[x].image = picked_img
  92.  
  93.                 # check if current pokemon is not final pokemon
  94.                 y = generate_draft.most_recent_pokemon2
  95.                 if 0 <= y <= 10:
  96.                     # loop through pokemon 1 ~ 11
  97.                     for i in range(0, 11):
  98.                         # find the current pokemon
  99.                         if y == i:
  100.                             # check if currently adding to team 1
  101.                             if y % 2 == 0:
  102.                                 print "adding to team 1..."
  103.                                 print "CURRENT:  [y%2][i/2] = [" + str(y%2) + "][" + str(i/2) + "]"
  104.                                 print "NEXT: [(y+1)%2][i/2] = [" + str((y+1)%2) + "][" + str(i/2) + "]"
  105.                                 # modify team 1 pokemon icons
  106.                                 team_pokemon[y%2][i/2].config(image=new_img)
  107.                                 team_pokemon[y%2][i/2].image = new_img
  108.                                 team_pokemon[(y+1)%2][i/2].config(image=next_img)
  109.                                 team_pokemon[(y+1)%2][i/2].image = next_img
  110.  
  111.                                 # check if currently the first pokemon
  112.                                 if y == 0:
  113.                                     # activate the undo button
  114.                                     undo_button_img2 = PhotoImage(file='img\\undo_button.gif')
  115.                                     undo_button.config(image=undo_button_img2, command=lambda: undo_action(generate_draft.most_recent_pokemon[0], 0))
  116.                                     undo_button.image = undo_button_img2
  117.                                 else:
  118.                                     # modify the undo button
  119.                                     undo_button.config(command=lambda: undo_action(generate_draft.most_recent_pokemon[-1], 0))
  120.                             # check if currently adding to team 2
  121.                             else:
  122.                                 print "adding to team 2..."
  123.                                 print "CURRENT:         [y%2][i/2] = [" + str((y-1)%2) + "][" + str(i/2) + "]"
  124.                                 print "NEXT:    [(y+1)%2][(i+1)/2] = [" + str(y%2) + "][" + str((i+1)/2) + "]"
  125.                                 # modify team 2 pokemon icons and the undo button
  126.                                 team_pokemon[y%2][i/2].config(image=new_img)
  127.                                 team_pokemon[y%2][i/2].image = new_img
  128.                                 team_pokemon[(y+1)%2][(i+1)/2].config(image=next_img)
  129.                                 team_pokemon[(y+1)%2][(i+1)/2].image = next_img
  130.                                 undo_button.config(command=lambda: undo_action(generate_draft.most_recent_pokemon[-1], 1))
  131.                 else:
  132.                     # modify team 2 pokemon 6 icon and the undo button
  133.                     team_pokemon[1][5].config(image=new_img)
  134.                     team_pokemon[1][5].image = new_img
  135.  
  136.                     # modify the undo button and change draft state to finished
  137.                     undo_button.config(command=lambda: undo_action(generate_draft.most_recent_pokemon[-1], 1))
  138.                     draft_menu.donebool = True
  139.  
  140.                     # activate the showdown button
  141.                     showdown2_img = PhotoImage(file='img\\showdown.gif')
  142.                     showdown_button.config(image=showdown2_img, command=draft_showdown)
  143.                     showdown_button.image = showdown2_img
  144.                     main.draft_session_counter += 1
  145.  
  146.                 # add selected pokemon to list of actions and respective team
  147.                 generate_draft.most_recent_pokemon2 +=1
  148.                 # add the pokemon information to each list
  149.                 for i in range(15):
  150.                     generate_draft.pokemon_list[generate_draft.most_recent_pokemon2%2][i].append(generate_draft.pokemon_list[2][i][x])
  151.                 # keep track of the pokemon just added
  152.                 generate_draft.most_recent_pokemon.append(x)
  153.  
  154.  
  155.     # purpose: generate_draft will generate a new pool of pokemon and start
  156.     #          the draft. Calling this function again will reset the draft.
  157.     def generate_draft():
  158.         # initialize 3 sets of lists of pokemon
  159.         generate_draft.pokemon_list = [[[] for i in range(15)] for j in range(3)]
  160.         # initialize a list and counter that will keep track of the picks
  161.         generate_draft.most_recent_pokemon = []
  162.         generate_draft.most_recent_pokemon2 = 0
  163.         # initialize a counter that will keep track of the generated pokemon
  164.         generate_counter = 0
  165.  
  166.         # open the csv file and read the database information
  167.         with open('database\\main_database.csv', 'r') as fileName:
  168.             reader = csv.reader(fileName)
  169.  
  170.             # generate at least 15 pokemon
  171.             while (generate_counter < 15):
  172.                 # pick a random ID
  173.                 newID = randint(2,dblen)
  174.  
  175.                 # check if ID is already picked, otherwise do nothing
  176.                 if newID not in generate_draft.pokemon_list[2][0]:
  177.                     # add new ID to list of picked IDs
  178.                     generate_draft.pokemon_list[2][0].append(newID)
  179.  
  180.                     # loop through each row in the database
  181.                     for row in reader:
  182.                         # check if the current row's ID matches the random ID
  183.                         if int(row[0]) == newID:
  184.                             # check if the current species is already picked
  185.                             if row[2] in generate_draft.pokemon_list[2][2]:
  186.                                 # remove the duplicate species
  187.                                 generate_draft.pokemon_list[2][0].remove(newID)
  188.                             else:
  189.                                 # add the new pokemon to the pool (list 3)
  190.                                 for i in range (1,15):
  191.                                     generate_draft.pokemon_list[2][i].append(row[i])
  192.                                 generate_counter += 1
  193.                     # return to the beginning of the file
  194.                     fileName.seek(0)
  195.  
  196.         # reset the icons for the pool of pokemon
  197.         pool_pokemon_img = [None for i in range(15)]
  198.         for i in range(15):
  199.             pool_pokemon_img[i] = PhotoImage(file=poke_icon(generate_draft.pokemon_list[2][1], generate_draft.pokemon_list[2][2], i))
  200.             pool_pokemon[i].config(image=pool_pokemon_img[i], command=lambda i=i: update_pool_pokemon(i))
  201.             pool_pokemon[i].image = pool_pokemon_img[i]
  202.  
  203.         # reset the icons for both teams
  204.         reset_teams_img = PhotoImage(file='img\\mystery_pokemon_icon.gif')
  205.         current_pokemon_img = PhotoImage(file='img\\current_mystery_pokemon_icon.gif')
  206.         for i in range(2):
  207.             for j in range(6):
  208.                 if i == 0 and j == 0:
  209.                     team_pokemon[i][j].config(image=current_pokemon_img)
  210.                     team_pokemon[i][j].image = current_pokemon_img
  211.                 else:
  212.                     team_pokemon[i][j].config(image=reset_teams_img)
  213.                     team_pokemon[i][j].image = reset_teams_img
  214.  
  215.         # reset the pool pokemon pick states
  216.         for i in range(15):
  217.             draft_menu.pokemon_state[i] = True
  218.  
  219.         # reset state of drafting and deactivate showdown button
  220.         draft_menu.donebool = False
  221.         showdown_img = PhotoImage(file='img\\showdown_unavailable.gif')
  222.         showdown_button.config(image=showdown_img, command=lambda: None)
  223.         showdown_button.image = showdown_img
  224.  
  225.  
  226.     # purpose: showdown will copy the showdown information for both teams to
  227.     #          the clipboard and create a log file of the current session.
  228.     def draft_showdown():
  229.         cboard = showdown("draft", main.draft_session_counter, 15, generate_draft.pokemon_list, 6)
  230.         # copy team 1 and team 2 information to the clipboard
  231.         d_m.clipboard_clear()
  232.         d_m.clipboard_append(cboard)
  233.  
  234.  
  235.     # initialize the draft type image at the top of the menu
  236.     bar_img = PhotoImage(file='img\\draft_menu_bar.gif')
  237.     mbar = Label(d_m, image=bar_img)
  238.     mbar.image = bar_img
  239.     mbar.pack()
  240.  
  241.     # initialize the 3-frame-layered (top-down) frame for the pool
  242.     pool_frame = [None for i in range(3)]
  243.     for i in range(3):
  244.         pool_frame[i] = Frame(d_m)
  245.         pool_frame[i].pack()
  246.  
  247.     # initialize the mystery icon for each pool pokemon (5 per row)
  248.     mystery_img = PhotoImage(file='img\\mystery_pokemon_icon.gif')
  249.     pool_pokemon = [None for i in range(15)]
  250.     for i in range(15):
  251.         if 0 <= i <= 4:
  252.             pool_pokemon[i] = Button(pool_frame[0], image=mystery_img, bd=0.1)
  253.         elif 5 <= i <= 9:
  254.             pool_pokemon[i] = Button(pool_frame[1], image=mystery_img, bd=0.1)
  255.         else:
  256.             pool_pokemon[i] = Button(pool_frame[2], image=mystery_img, bd=0.1)
  257.         pool_pokemon[i].image = mystery_img
  258.         pool_pokemon[i].pack(side=LEFT, padx=5, pady=5)
  259.  
  260.     # initialize the team frames below the pool frame
  261.     team_frame = [None for i in range(2)]
  262.     for i in range(2):
  263.         team_frame[i] = LabelFrame(d_m, text="TEAM " + str(i+1))
  264.         team_frame[i].pack(padx=5, pady=10)
  265.  
  266.     # initialize the mystery icon for each pokemon (6 per team)
  267.     team_pokemon = [[None for i in range(6)] for j in range(2)]
  268.     for i in range(2):
  269.         for j in range(6):
  270.             team_pokemon[i][j] = Label(team_frame[i], image=mystery_img, bd=0.1)
  271.             team_pokemon[i][j].image = mystery_img
  272.             team_pokemon[i][j].pack(side=LEFT, padx=3, pady=5)
  273.  
  274.     # initialize the frame for the drafting buttons below the team frames
  275.     buttonsframe = Frame(d_m)
  276.     buttonsframe.pack(pady=10)
  277.  
  278.     # initialize the start button on the left side
  279.     generate_img = PhotoImage(file='img\\generate.gif')
  280.     generate_button = Button(buttonsframe, image=generate_img, bd=0.1, command=generate_draft)
  281.     generate_button.image = generate_img
  282.     generate_button.pack(side=LEFT, padx=7)
  283.  
  284.     # initialize the undo button in the middle
  285.     undo_img = PhotoImage(file='img\\undo_button_unavailable.gif')
  286.     undo_button = Button(buttonsframe, image=undo_img, bd=0.1, command=lambda: None)
  287.     undo_button.image = undo_img
  288.     undo_button.pack(side=LEFT)
  289.  
  290.     # initialize the showdown button on the right side
  291.     showdown_img = PhotoImage(file='img\\showdown_unavailable.gif')
  292.     showdown_button = Button(buttonsframe, image=showdown_img, bd=0.1, command=lambda: None)
  293.     showdown_button.image = showdown_img
  294.     showdown_button.pack(side=LEFT, padx=7)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement