Advertisement
Guest User

Simplified Pokemon Battle Simluator

a guest
Dec 17th, 2017
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.59 KB | None | 0 0
  1. from random import randint
  2. import helperfunctions
  3. from helperfunctions import ROOT_DIR, DBNAME, USERNAME,PASSWORD
  4.  
  5. cursor, conn = helperfunctions.connect_to_database('localhost',DBNAME,USERNAME,PASSWORD)
  6.  
  7. def DamageDealt(level, move,attacker_att,defender_def, physical, attacker_sp_att, defender_sp_def):
  8.     hitFactor = randint(1, 100)
  9.     if hitFactor > move['accuracy']:
  10.         return 0
  11.  
  12.     if physical == 1:
  13.         return int(round(((((2*level)/5)+2)*move['power']*(attacker_att/defender_def))/50 + 2))
  14.     else:
  15.         return int(round(((((2*level)/5)+2)*move['power']*(attacker_sp_att/defender_sp_def))/50 + 2))
  16.  
  17. def findEffect(attack_type, def_type_1, def_type_2):
  18.     s = cursor.mogrify("select effect from effectiveness where att = %s and def = %s;",(attack_type, def_type_1))
  19.     cursor.execute(s)
  20.     try:
  21.         type1Effect = cursor.fetchall()[0]
  22.     except:
  23.         pass
  24.  
  25.     s = cursor.mogrify("select effect from effectiveness where att = %s and def = %s;",(attack_type, def_type_2))
  26.     cursor.execute(s)
  27.  
  28.     try:
  29.         type2Effect = cursor.fetchall()[0]
  30.     except:
  31.         totalEffect = type1Effect[0]
  32.     else:
  33.         if type2Effect:
  34.             totalEffect = type1Effect[0]
  35.         else:
  36.             totalEffect = type1Effect[0] * type2Effect[0]
  37.  
  38.     return totalEffect
  39.  
  40. def findBestMoveAgainst(moves, attacker, defender):
  41.     c = 0
  42.     type_1 = defender['type_1']
  43.     type_2 = defender['type_2']
  44.     allMoves = {}
  45.     for current in moves:
  46.         query = cursor.mogrify("select * from moves where name = %s;", [current])
  47.         cursor.execute(query)
  48.         try:
  49.             m = cursor.fetchall()
  50.             moveDetails = m[0]
  51.         except:
  52.             continue
  53.  
  54.         allMoves[c] = {'name':moveDetails[0],'power': moveDetails[2], 'accuracy': moveDetails[1] , 'type': moveDetails[3],'physical':moveDetails[4] }
  55.         effectivenessAgainstOpp = findEffect(allMoves[c]['type'], type_1, type_2)
  56.         damageDealt = DamageDealt(LEVEL,allMoves[c],attacker['base_stats'][2],defender['base_stats'][3],allMoves[c]['physical'], attacker['base_stats'][4], defender['base_stats'][5])
  57.         allMoves[c]['effectivenessAgainstOpp'] = effectivenessAgainstOpp
  58.         allMoves[c]['TotalEffectivenessAgainstOpp'] = allMoves[c]['accuracy'] * effectivenessAgainstOpp * damageDealt
  59.         c += 1
  60.  
  61.     orderedMoves = sorted(allMoves.items(), key=lambda x: x[1]['TotalEffectivenessAgainstOpp'])
  62.     if len(orderedMoves) == 0:
  63.         return {}
  64.     else:
  65.         length = len(orderedMoves) - 1
  66.         return orderedMoves[length][1]
  67.  
  68. ## Velja 50 bestu pókemona úr fyrri tilraunum
  69. query = "select * from pokemons p where p.id in (select b.winner from battles b GROUP BY b.winner order by count(b.winner) DESC limit 50)"
  70. cursor.execute(query)
  71.  
  72. try:
  73.     pokemons = cursor.fetchall()
  74.     if len(pokemons) == 0:
  75.         print('ÞÚ ÞARFT FYRST AÐ RUNNA simulateFights_physical.py og simulateFights_special.py')
  76.         exit()
  77. except:
  78.     print('Engir pókemonar fundust, ertu búinn að gera allt samkvæmt readme.txt?')
  79.  
  80. LEVEL = 100
  81. LENGTH = len(pokemons)
  82. c = 1
  83. for pokemon in pokemons:
  84.     pokemon1 = {}
  85.     pokemon1['id'] = pokemon[0]
  86.     pokemon1['name'] = pokemon[1]
  87.     pokemon1['type_1'] = pokemon[3]
  88.     pokemon1['type_2'] = pokemon[4]
  89.     moves = pokemon[5]
  90.     pokemon1['moves'] = moves.split(',')
  91.     query = cursor.mogrify("select * from base_stats where id = %s order by id asc;", [int(pokemon1['id'])])
  92.     cursor.execute(query)
  93.  
  94.     try:
  95.         base_stats = cursor.fetchall()
  96.         pokemon1['base_stats'] = base_stats[0]
  97.     except:
  98.         print('no records')
  99.  
  100.     # Increment PROGRESS BAR
  101.     helperfunctions.printProgressBar(c, LENGTH, prefix = 'Progress', suffix = '\n', length = 50)
  102.     c += 1
  103.     # ---
  104.  
  105.     for second_pokemon in pokemons[c:]:
  106.         pokemon2 = {}
  107.         pokemon2['id'] = second_pokemon[0]
  108.         pokemon2['name'] = second_pokemon[1]
  109.         pokemon2['type_1'] = second_pokemon[3]
  110.         pokemon2['type_2'] = second_pokemon[4]
  111.         moves = second_pokemon[5]
  112.         pokemon2['moves'] = moves.split(',')
  113.  
  114.         query = cursor.mogrify("select * from base_stats where id = %s;", [int(pokemon2['id'])])
  115.         cursor.execute(query)
  116.  
  117.         try:
  118.             base_stats = cursor.fetchall()
  119.             pokemon2['base_stats'] = base_stats[0]
  120.         except:
  121.             print('no records')
  122.  
  123.         # print("{} is fighting {}".format(pokemon1['name'], pokemon2['name']))
  124.  
  125.         pokemon1['currentHP'] = (((2*pokemon1['base_stats'][1]+100) * LEVEL)/100)+10
  126.         pokemon2['currentHP'] = (((2*pokemon2['base_stats'][1]+100) * LEVEL)/100)+10
  127.  
  128.         if pokemon1['base_stats'][6] > pokemon2['base_stats'][6]:
  129.             attackFirst = pokemon1
  130.             attackSecond = pokemon2
  131.         else:
  132.             attackFirst = pokemon2
  133.             attackSecond = pokemon1
  134.  
  135.         rounds = 0
  136.         while attackFirst['currentHP'] > 0 and attackSecond['currentHP'] > 0:
  137.             rounds += 1
  138.             ## Attack first velur bragð
  139.             currentMove = findBestMoveAgainst(attackFirst['moves'], attackFirst, attackSecond)
  140.             if len(currentMove) == 0:
  141.                 print(attackFirst['name'])
  142.                 break
  143.             ## Gerir árás með bragði
  144.             damageDealt = currentMove['effectivenessAgainstOpp'] * DamageDealt(LEVEL,currentMove,attackFirst['base_stats'][2],attackSecond['base_stats'][3],currentMove['physical'], attackFirst['base_stats'][4], attackSecond['base_stats'][5])
  145.             # print('{} deals {} damage against {} with {}'.format(attackFirst['name'], damageDealt, attackSecond['name'], currentMove['name']))
  146.             ## currentHp Defender minnkar
  147.             attackSecond['currentHP'] = attackSecond['currentHP'] - damageDealt
  148.             ## Re-evaluete while condition
  149.             if attackSecond['currentHP'] <= 0:
  150.                 break
  151.             ## attackSecond tekur við og velur bragð
  152.             currentMove = findBestMoveAgainst(attackSecond['moves'], attackSecond, attackFirst)
  153.             if len(currentMove) == 0:
  154.                 print(attackSecond['name'])
  155.                 break
  156.             damageDealt = currentMove['effectivenessAgainstOpp'] * DamageDealt(LEVEL,currentMove,attackSecond['base_stats'][2],attackFirst['base_stats'][3],currentMove['physical'], attackSecond['base_stats'][4], attackFirst['base_stats'][5])
  157.             # print('{} deals {} damage against {} with {}'.format(attackSecond['name'], damageDealt, attackFirst['name'], currentMove['name']))
  158.             attackFirst['currentHP'] = attackFirst['currentHP'] - damageDealt
  159.  
  160.         ## loggum niðurstöðu
  161.         if attackFirst['currentHP'] > attackSecond['currentHP']:
  162.             winner = attackFirst
  163.             loser = attackSecond
  164.         else:
  165.             winner = attackSecond
  166.             loser = attackFirst
  167.  
  168.         print('\r{} won {} with {} HP left. It took {} rounds'.format(winner['name'], loser['name'], int(winner['currentHP']), rounds), end = '\r')
  169.  
  170.         insertString = cursor.mogrify("insert into battles (winner, loser, rounds, winner_hp_left, first_attack, version) values (%s,%s,%s,%s,%s,%s)", (int(winner['id']), int(loser['id']), rounds, int(winner['currentHP']), int(attackFirst['id']),'best_move_against_opp_top50'))
  171.         cursor.execute(insertString)
  172.  
  173. conn.commit()
  174. cursor.close()
  175. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement