Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- import helperfunctions
- from helperfunctions import ROOT_DIR, DBNAME, USERNAME,PASSWORD
- cursor, conn = helperfunctions.connect_to_database('localhost',DBNAME,USERNAME,PASSWORD)
- def DamageDealt(level, move,attacker_att,defender_def, physical, attacker_sp_att, defender_sp_def):
- hitFactor = randint(1, 100)
- if hitFactor > move['accuracy']:
- return 0
- if physical == 1:
- return int(round(((((2*level)/5)+2)*move['power']*(attacker_att/defender_def))/50 + 2))
- else:
- return int(round(((((2*level)/5)+2)*move['power']*(attacker_sp_att/defender_sp_def))/50 + 2))
- def findEffect(attack_type, def_type_1, def_type_2):
- s = cursor.mogrify("select effect from effectiveness where att = %s and def = %s;",(attack_type, def_type_1))
- cursor.execute(s)
- try:
- type1Effect = cursor.fetchall()[0]
- except:
- pass
- s = cursor.mogrify("select effect from effectiveness where att = %s and def = %s;",(attack_type, def_type_2))
- cursor.execute(s)
- try:
- type2Effect = cursor.fetchall()[0]
- except:
- totalEffect = type1Effect[0]
- else:
- if type2Effect:
- totalEffect = type1Effect[0]
- else:
- totalEffect = type1Effect[0] * type2Effect[0]
- return totalEffect
- def findBestMoveAgainst(moves, attacker, defender):
- c = 0
- type_1 = defender['type_1']
- type_2 = defender['type_2']
- allMoves = {}
- for current in moves:
- query = cursor.mogrify("select * from moves where name = %s;", [current])
- cursor.execute(query)
- try:
- m = cursor.fetchall()
- moveDetails = m[0]
- except:
- continue
- allMoves[c] = {'name':moveDetails[0],'power': moveDetails[2], 'accuracy': moveDetails[1] , 'type': moveDetails[3],'physical':moveDetails[4] }
- effectivenessAgainstOpp = findEffect(allMoves[c]['type'], type_1, type_2)
- damageDealt = DamageDealt(LEVEL,allMoves[c],attacker['base_stats'][2],defender['base_stats'][3],allMoves[c]['physical'], attacker['base_stats'][4], defender['base_stats'][5])
- allMoves[c]['effectivenessAgainstOpp'] = effectivenessAgainstOpp
- allMoves[c]['TotalEffectivenessAgainstOpp'] = allMoves[c]['accuracy'] * effectivenessAgainstOpp * damageDealt
- c += 1
- orderedMoves = sorted(allMoves.items(), key=lambda x: x[1]['TotalEffectivenessAgainstOpp'])
- if len(orderedMoves) == 0:
- return {}
- else:
- length = len(orderedMoves) - 1
- return orderedMoves[length][1]
- ## Velja 50 bestu pókemona úr fyrri tilraunum
- 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)"
- cursor.execute(query)
- try:
- pokemons = cursor.fetchall()
- if len(pokemons) == 0:
- print('ÞÚ ÞARFT FYRST AÐ RUNNA simulateFights_physical.py og simulateFights_special.py')
- exit()
- except:
- print('Engir pókemonar fundust, ertu búinn að gera allt samkvæmt readme.txt?')
- LEVEL = 100
- LENGTH = len(pokemons)
- c = 1
- for pokemon in pokemons:
- pokemon1 = {}
- pokemon1['id'] = pokemon[0]
- pokemon1['name'] = pokemon[1]
- pokemon1['type_1'] = pokemon[3]
- pokemon1['type_2'] = pokemon[4]
- moves = pokemon[5]
- pokemon1['moves'] = moves.split(',')
- query = cursor.mogrify("select * from base_stats where id = %s order by id asc;", [int(pokemon1['id'])])
- cursor.execute(query)
- try:
- base_stats = cursor.fetchall()
- pokemon1['base_stats'] = base_stats[0]
- except:
- print('no records')
- # Increment PROGRESS BAR
- helperfunctions.printProgressBar(c, LENGTH, prefix = 'Progress', suffix = '\n', length = 50)
- c += 1
- # ---
- for second_pokemon in pokemons[c:]:
- pokemon2 = {}
- pokemon2['id'] = second_pokemon[0]
- pokemon2['name'] = second_pokemon[1]
- pokemon2['type_1'] = second_pokemon[3]
- pokemon2['type_2'] = second_pokemon[4]
- moves = second_pokemon[5]
- pokemon2['moves'] = moves.split(',')
- query = cursor.mogrify("select * from base_stats where id = %s;", [int(pokemon2['id'])])
- cursor.execute(query)
- try:
- base_stats = cursor.fetchall()
- pokemon2['base_stats'] = base_stats[0]
- except:
- print('no records')
- # print("{} is fighting {}".format(pokemon1['name'], pokemon2['name']))
- pokemon1['currentHP'] = (((2*pokemon1['base_stats'][1]+100) * LEVEL)/100)+10
- pokemon2['currentHP'] = (((2*pokemon2['base_stats'][1]+100) * LEVEL)/100)+10
- if pokemon1['base_stats'][6] > pokemon2['base_stats'][6]:
- attackFirst = pokemon1
- attackSecond = pokemon2
- else:
- attackFirst = pokemon2
- attackSecond = pokemon1
- rounds = 0
- while attackFirst['currentHP'] > 0 and attackSecond['currentHP'] > 0:
- rounds += 1
- ## Attack first velur bragð
- currentMove = findBestMoveAgainst(attackFirst['moves'], attackFirst, attackSecond)
- if len(currentMove) == 0:
- print(attackFirst['name'])
- break
- ## Gerir árás með bragði
- damageDealt = currentMove['effectivenessAgainstOpp'] * DamageDealt(LEVEL,currentMove,attackFirst['base_stats'][2],attackSecond['base_stats'][3],currentMove['physical'], attackFirst['base_stats'][4], attackSecond['base_stats'][5])
- # print('{} deals {} damage against {} with {}'.format(attackFirst['name'], damageDealt, attackSecond['name'], currentMove['name']))
- ## currentHp Defender minnkar
- attackSecond['currentHP'] = attackSecond['currentHP'] - damageDealt
- ## Re-evaluete while condition
- if attackSecond['currentHP'] <= 0:
- break
- ## attackSecond tekur við og velur bragð
- currentMove = findBestMoveAgainst(attackSecond['moves'], attackSecond, attackFirst)
- if len(currentMove) == 0:
- print(attackSecond['name'])
- break
- damageDealt = currentMove['effectivenessAgainstOpp'] * DamageDealt(LEVEL,currentMove,attackSecond['base_stats'][2],attackFirst['base_stats'][3],currentMove['physical'], attackSecond['base_stats'][4], attackFirst['base_stats'][5])
- # print('{} deals {} damage against {} with {}'.format(attackSecond['name'], damageDealt, attackFirst['name'], currentMove['name']))
- attackFirst['currentHP'] = attackFirst['currentHP'] - damageDealt
- ## loggum niðurstöðu
- if attackFirst['currentHP'] > attackSecond['currentHP']:
- winner = attackFirst
- loser = attackSecond
- else:
- winner = attackSecond
- loser = attackFirst
- print('\r{} won {} with {} HP left. It took {} rounds'.format(winner['name'], loser['name'], int(winner['currentHP']), rounds), end = '\r')
- 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'))
- cursor.execute(insertString)
- conn.commit()
- cursor.close()
- conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement