Adding the player vs. player command to my FPL bot for Reddit
In my last update I laid the groundworks for the entire bot and also added a player vs. team command. With this update I've now added another command that allows users to compare a player against another player (and some other improvements here and there).
https://github.com/amosbastian/FPLbot/pull/4
The new command
The new player vs. player command works the same as the player vs. team command; !fplbot <player_name> vs. <player_name> <optional: number of fixtures>. However, there are some differences, mainly because of the fact that there is too much interesting information available. In the player vs. team table you could show all of this, but when comparing two players, it would be too much to add all this to a Markdown table. Because of this, I've decided to simply compare each player's goals and assists (and their expected values).
The first function that was created was versus_player_handler(). It basically gets all information it needs; the players (using text indexes) and their relevant fixtures (all the fixtures the player has played for their current team). It then zips these fixtures together (since you want to compare the two) and sends it to another function to create the Markdown table.
The table
In the end I created the player_vs_player_table() function, which in itself is a bit big (and maybe messy), but it works. As I mentioned before, it strips the information contained in the player vs. team table and creates something like the following table:
It could also be used on Steemit itself, but honestly I don't think the Markdown tables look anywhere near as good on here (although on Steempeak they do):
Son (£8.9) vs. Mané (£9.6) (last 5 fixtures)
| xA | A | xG | G | MP | Fixture | Fixture | MP | G | xG | A | xA |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0.13 | 0 | 0.42 | 1 | 90 | Spurs 3-1 Leicester | Liverpool 3-0 Bournemouth | 89 | 1 | 0.58 | 0 | 0.12 |
| 0.70 | 0 | 0.19 | 1 | 90 | Spurs 1-0 Newcastle United | West Ham 1-1 Liverpool | 90 | 1 | 0.64 | 0 | 0.00 |
| 0.00 | 0 | 0.20 | 1 | 90 | Spurs 2-1 Watford | Liverpool 1-1 Leicester | 90 | 1 | 0.18 | 0 | 0.10 |
| 0.38 | 0 | 0.05 | 0 | 90 | Spurs 0-1 Man Utd | Liverpool 4-3 Crystal Palace | 90 | 1 | 0.47 | 0 | 0.01 |
| 0.08 | 1 | 0.50 | 1 | 77 | Cardiff 0-3 Spurs | Brighton 0-1 Liverpool | 90 | 0 | 0.10 | 0 | 0.10 |
| 1.29 | 1 | 1.36 | 4 | 437 | 449 | 4 | 1.97 | 0 | 0.34 |
Once this table has been returned to the versus_player_handler() function, the comment is formatted with its header (the Son (£8.9) vs. Mané (£9.6) (last 5 fixtures) bit) and the table as its body, and then the bot replies to the comment that requested it.
Some small improvements
I've also added the player's price to the player vs. team table, as that is a pretty important aspect of the Fantasy Premier League. I've also slightly improved the regex, as I noticed that it wasn't picking up diacritics, which meant that e.g. "son vs. mané" was seen as "son vs. man".
Other than that I've added a separate script for initialising the database. Since it uses text indexes to search for the players in the database, of course they need to be made first. Finally, I also added a simple script for posting the price changes, so I can use that in the cron job instead of bot.py.
Roadmap
I will be making a post about it on Reddit and will see what people want to see, and if they want anything that has already been implemented to change. Hopefully they come up with some good ideas!
Usage & installation
FPLbot uses MongoDB to store players in a database, and so it is required to
have MongoDB installed. Other than that, it uses fpl
to retrieve information from Fantasy Premier League's API, and thus requires
Python 3.6+.
git clone [email protected]:amosbastian/FPLbot.git
cd FPLbot
pip install -r requirements.txt
To initialise the database with text indexes you should do the following:
python FPLbot/init.py
Once this has been done, you should create your own config.json with the correct values (see configuration).
With this filled in, you can run the bot using
python FPLbot/bot.py
As for the price changes, you should schedule a cron job, like this for example:
25 1 * * * /home/amos/FPLbot/venv/bin/python /home/amos/FPLbot/FPLbot/price_changes.py
Contributing
I've created a Discord server for people interested in programming FPL related things, so if you are interested in helping out, or simply want to know more, then don't hesitate to join! Otherwise you can simply create an issue on GitHub.