1
0
Fork 0
mirror of synced 2024-05-20 20:42:27 +12:00

Moved utility functions to the utility file

This commit is contained in:
Phxntxm 2016-12-09 21:25:08 -06:00
parent 532c0078eb
commit e0f7260a70
3 changed files with 81 additions and 60 deletions

View file

@ -3,6 +3,7 @@ import discord
from .utils import config
from .utils import checks
from .utils import utilities
import re
import random
@ -191,7 +192,7 @@ class TicTacToe:
await self.bot.say("{} has won this game of TicTacToe, better luck next time {}".format(winner.display_name,
loser.display_name))
# Handle updating ratings based on the winner and loser
await config.update_records('tictactoe', winner, loser)
await utilities.update_records('tictactoe', winner, loser)
# This game has ended, delete it so another one can be made
del self.boards[ctx.message.server.id]
else:

View file

@ -114,65 +114,6 @@ async def update_cache():
for value in cache.values():
await value.update()
async def update_records(key, winner, loser):
# We're using the Harkness scale to rate
# http://opnetchessclub.wikidot.com/harkness-rating-system
r_filter = lambda row: (row['member_id'] == winner.id) | (row['member_id'] == loser.id)
matches = await get_content(key, r_filter)
winner_stats = {}
loser_stats = {}
try:
for stat in matches:
if stat.get('member_id') == winner.id:
winner_stats = stat
elif stat.get('member_id') == loser.id:
loser_stats = stat
except TypeError:
pass
winner_rating = winner_stats.get('rating') or 1000
loser_rating = loser_stats.get('rating') or 1000
# The scale is based off of increments of 25, increasing the change by 1 for each increment
# That is all this loop does, increment the "change" for every increment of 25
# The change caps off at 300 however, so break once we are over that limit
difference = abs(winner_rating - loser_rating)
rating_change = 0
count = 25
while count <= difference:
if count > 300:
break
rating_change += 1
count += 25
# 16 is the base change, increased or decreased based on whoever has the higher current rating
if winner_rating > loser_rating:
winner_rating += 16 - rating_change
loser_rating -= 16 - rating_change
else:
winner_rating += 16 + rating_change
loser_rating -= 16 + rating_change
# Just increase wins/losses for each person, making sure it's at least 0
winner_wins = winner_stats.get('wins', 0)
winner_losses = winner_stats.get('losses', 0)
loser_wins = loser_stats.get('wins', 0)
loser_losses = loser_stats.get('losses', 0)
winner_wins += 1
loser_losses += 1
# Now save the new wins, losses, and ratings
winner_stats = {'wins': winner_wins, 'losses': winner_losses, 'rating': winner_rating}
loser_stats = {'wins': loser_wins, 'losses': loser_losses, 'rating': loser_rating}
if not await update_content(key, winner_stats, {'member_id': winner.id}):
winner_stats['member_id'] = winner.id
await add_content(key, winner_stats, {'member_id': winner.id})
if not await update_content(key, loser_stats, {'member_id': loser.id}):
loser_stats['member_id'] = loser.id
await add_content(key, loser_stats, {'member_id': loser.id})
def command_prefix(bot, message):
# We do not want to make a query for every message that is sent

View file

@ -1,4 +1,10 @@
import aiohttp
import io
from . import config
def get_all_commands(bot):
"""Returns a list of all command names for the bot"""
# First lets create a set of all the parent names
parent_command_names = set(cmd.qualified_name for cmd in bot.commands.values())
all_commands = []
@ -22,6 +28,7 @@ def _get_all_commands(command):
pass
def find_command(bot, command):
"""Finds a command (be it parent or sub command) based on string given"""
# This method ensures the command given is valid. We need to loop through commands
# As bot.commands only includes parent commands
# So we are splitting the command in parts, looping through the commands
@ -43,3 +50,75 @@ def find_command(bot, command):
break
return cmd
async def download_image(url):
"""Returns a file-like object based on the URL provided"""
headers = {'User-Agent': config.user_agent}
try:
# Simply download the image
with aiohttp.ClientSession(headers=headers) as session:
async with session.get(url) as r:
image_bytes = r.read()
# Then wrap it in a BytesIO object, to be used like an actual file
return io.BytesIO(image_bytes)
except:
return
async def update_records(key, winner, loser):
# We're using the Harkness scale to rate
# http://opnetchessclub.wikidot.com/harkness-rating-system
r_filter = lambda row: (row['member_id'] == winner.id) | (row['member_id'] == loser.id)
matches = await config.get_content(key, r_filter)
winner_stats = {}
loser_stats = {}
try:
for stat in matches:
if stat.get('member_id') == winner.id:
winner_stats = stat
elif stat.get('member_id') == loser.id:
loser_stats = stat
except TypeError:
pass
winner_rating = winner_stats.get('rating') or 1000
loser_rating = loser_stats.get('rating') or 1000
# The scale is based off of increments of 25, increasing the change by 1 for each increment
# That is all this loop does, increment the "change" for every increment of 25
# The change caps off at 300 however, so break once we are over that limit
difference = abs(winner_rating - loser_rating)
rating_change = 0
count = 25
while count <= difference:
if count > 300:
break
rating_change += 1
count += 25
# 16 is the base change, increased or decreased based on whoever has the higher current rating
if winner_rating > loser_rating:
winner_rating += 16 - rating_change
loser_rating -= 16 - rating_change
else:
winner_rating += 16 + rating_change
loser_rating -= 16 + rating_change
# Just increase wins/losses for each person, making sure it's at least 0
winner_wins = winner_stats.get('wins', 0)
winner_losses = winner_stats.get('losses', 0)
loser_wins = loser_stats.get('wins', 0)
loser_losses = loser_stats.get('losses', 0)
winner_wins += 1
loser_losses += 1
# Now save the new wins, losses, and ratings
winner_stats = {'wins': winner_wins, 'losses': winner_losses, 'rating': winner_rating}
loser_stats = {'wins': loser_wins, 'losses': loser_losses, 'rating': loser_rating}
if not await config.update_content(key, winner_stats, {'member_id': winner.id}):
winner_stats['member_id'] = winner.id
await config.add_content(key, winner_stats, {'member_id': winner.id})
if not await config.update_content(key, loser_stats, {'member_id': loser.id}):
loser_stats['member_id'] = loser.id
await config.add_content(key, loser_stats, {'member_id': loser.id})