Calculating a League Player’s Win Percentage in Python

Introduction to using the Riot API in Python Part 5, Finishing Our Mini Project and Future Steps

TheConnoisseur
4 min readOct 14, 2023

This is part 5 (The Last Part!) of a tutorial series where I walkthrough creating a mini project using the Riot API in Python.

Here is a link to the first part of the series if you want to go from the beginning: Using The Riot API with Python. Part 1, Defining our mini project and… | by TheConnoisseur | Sep, 2023 | Medium

We’re here at last, the final part of this tutorial series. Congrats for making it this far! And if this is you haven’t seen any other parts of this tutorial series already, then I highly recommend checking them out.

In this part, we’ll finish of our mini project by calculating our player’s win percentage from their last 20 games, using our previously defined functions, and talk about some possible future steps/improvements.

Calculating the Player’s Win Ratio

If you remember the goal of this mini project was to calculate the player’s win ratio from their last 20 played games. And now, we have all the pieces in place to do this.

Let’s write a function for this:

def win_percent_of_last_20_games(summoner_name, region=settings.DEFAULT_REGION, region_code=settings.DEFAULT_REGION_CODE):
summoner = get_summoner_info(summoner_name, region_code)
matches = get_match_ids_by_summoner_puuid(summoner['puuid'], 20, region)

wins = 0
for match in matches:
if did_player_win_match(summoner['puuid'], match):
wins += 1

return (wins/len(matches))*100

This function utilises get_summoner_info, get_match_ids_by_summoner_puuid and did_player_win_match, all of our previously written functions, to calculate the win ratio for our player.

Our pre-existing functions do most of the heavy lifting so there isn’t much new logic to explain here. All we simply do is loop through the last 20 matches our player has played and count how many of those they won.

Then we use a simple calculation to calculate their win percentage: (number of wins/number of games) * 100

Let’s test out our new function by adding this bit of code to __main__.py:

So now our __main__.py looks like this:

from helpers import get_summoner_info, get_match_ids_by_summoner_puuid,\
did_player_win_match, win_percent_of_last_20_games

summoner_name = "Conoisseur"

summoner = get_summoner_info(summoner_name)
print(summoner)
print(summoner['name'])

summoner_match_ids = get_match_ids_by_summoner_puuid(summoner['puuid'], 20)
print(summoner_match_ids)

win = did_player_win_match(summoner['puuid'],summoner_match_ids[0])
print(win)

win_ratio = win_percent_of_last_20_games(summoner_name)
print(win_ratio)

Where the last line prints out the player’s win percentage of their last 20 games.

Upon running this code you should see something like this:

{'id': 'hGqlM71dcf2ftm2-3cIo8kzQafLAiQkRRzXkICH6OlznpUzdpS79yid0pQ', 'accountId': 'gzQspn1TfngAEAVHCDs7lBfVd0lmXmE0GfpRx03r55C2BpTSRLBD8VhH', 'puuid': 'qJMwrXcOhpCXLgnKGJsGRr1Ks4TLc_Pq92LR1wIpHqvP_uOU9jLca3sDk7dUQ8mASPweplXVHrtVcQ', 'name': 'Conoisseur', 'profileIconId': 5973, 'revisionDate': 1697218713425, 'summonerLevel': 56}
Conoisseur
['EUW1_6630219537', 'EUW1_6630138418', 'EUW1_6627617339', 'EUW1_6626784353', 'EUW1_6626717896', 'EUW1_6626707560', 'EUW1_6626605619', 'EUW1_6626441424', 'EUW1_6623931070', 'EUW1_6623866310', 'EUW1_6623814426', 'EUW1_6622616730', 'EUW1_6622561776', 'EUW1_6622504403', 'EUW1_6622481817', 'EUW1_6622445474', 'EUW1_6621140188', 'EUW1_6620130161', 'EUW1_6618818548', 'EUW1_6618778227']
False
35.0

Process finished with exit code 0

As you can see, our player has a 35% win rate from their last 20 played game. Not great, but at least they know now!

And with that our project is complete!

So What Now?

Well, if you want, you could call this project right here and go and work on something else using your newfound skills.

But, if you want some ideas to extend or improve what we currently have, then here’s a few ideas:

Possible Improvements

  • This code is in no way foolproof, and can fail at many different steps. If you want to extend this project, it would be beneficial to make sure you catch any errors thrown when interacting with the API and make sure the program doesn’t crash.
  • Remove all existing hard code. This would mean refactoring out things like the win_percent_of_last_20_games function and replacing it with something that can calculate the win percent of any given amount of games.
  • General code quality improvements. These are up to you.

Possible Extensions

  • Ability to calculate the win percentage of a certain type of game, I.e. our user may only care about their win percentage in ranked games.
  • Ability to calculate win percentage on a certain champion.
  • Ability to compare win ratios of different players.

These are all possible improvements and extensions you can make but feel free to be creative with it and add whatever you want.

That's all for this mini series!

I know a script to calculate win percentage isn’t the most exciting or innovative thing to exist, but the skills needed to code it out form the building blocks of being able to do much more exciting things with the Riot API. The possibilities are limitless!

A big thanks for reading this far and I hope you enjoyed! If there’s anything else you want me to cover or add then let me know through a comment!

Thanks for reading!
If you enjoyed this article then feel free to clap it and check out some of my other articles :)

--

--

TheConnoisseur

Avid Computer Enjoyer, I write about topics I'm interested in, mainly programming.