Corrected syntax errors
This commit is contained in:
parent
f26e04b1a4
commit
8239d66524
|
@ -50,9 +50,10 @@ class Strawpoll:
|
|||
elif poll_id in server_polls.keys():
|
||||
poll = server_polls[poll_id]
|
||||
|
||||
async with self.session.get("{}/{}".format(self.url, poll_id), headers={'User-Agent': 'Bonfire/1.0.0') as response:
|
||||
async with self.session.get("{}/{}".format(self.url, poll_id),
|
||||
headers={'User-Agent': 'Bonfire/1.0.0'}) as response:
|
||||
data = await response.json()
|
||||
|
||||
|
||||
# The response for votes and options is provided as two separate lists
|
||||
# We are enumarting the list of options, to print r (the option) and the votes to match it, based on the index of the option
|
||||
# The rest is simple formatting
|
||||
|
@ -62,8 +63,8 @@ class Strawpoll:
|
|||
created_ago = (pendulum.utcnow() - pendulum.parse(poll['date'])).in_words()
|
||||
link = "https://strawpoll.me/{}".format(poll_id)
|
||||
fmt = "Link: {}\nTitle: {}\nAuthor: {}\nCreated: {} ago\nOptions:\n\t{}".format(link, data['title'],
|
||||
author.display_name,
|
||||
created_ago, fmt_options)
|
||||
author.display_name,
|
||||
created_ago, fmt_options)
|
||||
await self.bot.say("```\n{}```".format(fmt))
|
||||
|
||||
@strawpolls.command(name='create', aliases=['setup', 'add'], pass_context=True)
|
||||
|
@ -97,7 +98,7 @@ class Strawpoll:
|
|||
'options': options}
|
||||
async with self.session.post(self.url, data=json.dumps(payload), headers=self.headers) as response:
|
||||
data = await response.json()
|
||||
|
||||
|
||||
# Save this strawpoll in the list of running strawpolls for a server
|
||||
all_polls = config.get_content('strawpolls') or {}
|
||||
server_polls = all_polls.get(ctx.message.server.id) or {}
|
||||
|
@ -115,7 +116,7 @@ class Strawpoll:
|
|||
|
||||
all_polls = config.get_content('strawpolls') or {}
|
||||
server_polls = all_polls.get(ctx.message.server.id) or {}
|
||||
|
||||
|
||||
# Check if a poll_id was provided, if it is then we can continue, if not print the list of current polls
|
||||
if poll_id:
|
||||
poll = server_polls.get(poll_id)
|
||||
|
|
|
@ -7,31 +7,32 @@ from .utils import checks
|
|||
import re
|
||||
import random
|
||||
|
||||
|
||||
class Board:
|
||||
def __init__(self, player1, player2):
|
||||
self.board = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]
|
||||
|
||||
self.board = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
|
||||
|
||||
# Randomize who goes first when the board is created
|
||||
if random.SystemRandom().randint(0, 1):
|
||||
self.challengers = {'x': player1, 'o': player2}
|
||||
else:
|
||||
self.challengers = {'x': player2, 'o': player1}
|
||||
|
||||
|
||||
# X's always go first
|
||||
self.X_turn = True
|
||||
|
||||
|
||||
def full(self):
|
||||
for row in self.board:
|
||||
if ' ' in row:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def can_play(self, player):
|
||||
if self.X_turn:
|
||||
return player == self.challengers['x']
|
||||
else:
|
||||
return player == self.challengers['o']
|
||||
|
||||
|
||||
def update(self, x, y):
|
||||
letter = 'x' if self.X_turn else 'o'
|
||||
if self.board[x][y] == ' ':
|
||||
|
@ -40,7 +41,7 @@ class Board:
|
|||
return False
|
||||
self.X_turn = not self.X_turn
|
||||
return True
|
||||
|
||||
|
||||
def check(self):
|
||||
# Checking all possiblities will be fun...
|
||||
# First base off the top-left corner, see if any possiblities with that match
|
||||
|
@ -59,7 +60,7 @@ class Board:
|
|||
return self.challengers['x']
|
||||
else:
|
||||
return self.challengers['o']
|
||||
|
||||
|
||||
# Next check the top-right corner, not re-checking the last possiblity that included it
|
||||
if self.board[0][2] == self.board[1][2] and self.board[0][2] == self.board[2][2] and self.board[0][2] != ' ':
|
||||
if self.board[0][2] == 'x':
|
||||
|
@ -71,14 +72,14 @@ class Board:
|
|||
return self.challengers['x']
|
||||
else:
|
||||
return self.challengers['o']
|
||||
|
||||
|
||||
# Next up, bottom-right corner, only one possiblity to check here, other two have been checked
|
||||
if self.board[2][2] == self.board[2][1] and self.board[2][2] == self.board[2][0] and self.board[2][2] != ' ':
|
||||
if self.board[2][2] == 'x':
|
||||
return self.challengers['x']
|
||||
else:
|
||||
return self.challengers['o']
|
||||
|
||||
|
||||
# No need to check the bottom-left, all posiblities have been checked now
|
||||
# Base things off the middle now, as we only need the two 'middle' possiblites that aren't diagonal
|
||||
if self.board[1][1] == self.board[0][1] and self.board[1][1] == self.board[2][1] and self.board[1][1] != ' ':
|
||||
|
@ -91,10 +92,10 @@ class Board:
|
|||
return self.challengers['x']
|
||||
else:
|
||||
return self.challengers['o']
|
||||
|
||||
|
||||
# Otherwise nothing has been found, return None
|
||||
return None
|
||||
|
||||
|
||||
def __str__(self):
|
||||
_board = " {} | {} | {}\n".format(self.board[0][0], self.board[0][1], self.board[0][2])
|
||||
_board += "———————————————\n"
|
||||
|
@ -102,21 +103,20 @@ class Board:
|
|||
_board += "———————————————\n"
|
||||
_board += " {} | {} | {}\n".format(self.board[2][0], self.board[2][1], self.board[2][2])
|
||||
return "```\n{}```".format(_board)
|
||||
|
||||
|
||||
|
||||
|
||||
class TicTacToe:
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
self.boards = {}
|
||||
|
||||
|
||||
def create(self, server_id, player1, player2):
|
||||
self.boards[server_id] = Board(player1, player2)
|
||||
|
||||
|
||||
# Return whoever is x's so that we know who is going first
|
||||
return self.boards[server_id].challengers['x']
|
||||
|
||||
|
||||
def update_records(winner, loser):
|
||||
|
||||
def update_records(self, winner, loser):
|
||||
matches = config.get_content('tictactoe')
|
||||
if matches is None:
|
||||
matches = {winner.id: "1-0", loser.id: "0-1"}
|
||||
|
@ -155,8 +155,8 @@ class TicTacToe:
|
|||
matches[winner.id] = winner_stats
|
||||
matches[loser.id] = loser_stats
|
||||
|
||||
return config.save_content('tictactoe', battles)
|
||||
|
||||
return config.save_content('tictactoe', matches)
|
||||
|
||||
@commands.group(pass_context=True, aliases=['tic', 'tac', 'toe'], no_pm=True, invoke_without_command=True)
|
||||
@checks.custom_perms(send_messages=True)
|
||||
async def tictactoe(self, ctx, *, option: str):
|
||||
|
@ -172,14 +172,14 @@ class TicTacToe:
|
|||
if not board.can_play(player):
|
||||
await self.bot.say("You cannot play right now!")
|
||||
return
|
||||
|
||||
|
||||
# Search for the positions in the option given, the actual match doesn't matter, just need to check if it exists
|
||||
top = re.search('top', option)
|
||||
middle = re.search('middle', option)
|
||||
bottom = re.search('bottom', option)
|
||||
left = re.search('left', option)
|
||||
right = re.search('right', option)
|
||||
|
||||
|
||||
# Check if what was given was valid
|
||||
if top and bottom:
|
||||
await self.bot.say("That is not a valid location! Use some logic, come on!")
|
||||
|
@ -190,7 +190,7 @@ class TicTacToe:
|
|||
if not top and not bottom and not left and not right and not middle:
|
||||
await self.bot.say("Please provide a valid location to play!")
|
||||
return
|
||||
|
||||
|
||||
# Simple assignments
|
||||
if top:
|
||||
x = 0
|
||||
|
@ -207,8 +207,7 @@ class TicTacToe:
|
|||
y = 1
|
||||
elif (left or right) and not (top or bottom):
|
||||
x = 1
|
||||
|
||||
|
||||
|
||||
# If all checks have been made, x and y should now be defined correctly based on the matches, and we can go ahead and:
|
||||
if not board.update(x, y):
|
||||
await self.bot.say("Someone has already played there!")
|
||||
|
@ -216,9 +215,10 @@ class TicTacToe:
|
|||
winner = board.check()
|
||||
if winner:
|
||||
loser = board.challengers['x'] if board.challengers['x'] != winner else board.challengers['o']
|
||||
await self.bot.say("{} has won this game of TicTacToe, better luck next time {}".format(winner.display_name, loser.display_name))
|
||||
|
||||
update_records(winner, loser)
|
||||
await self.bot.say("{} has won this game of TicTacToe, better luck next time {}".format(winner.display_name,
|
||||
loser.display_name))
|
||||
|
||||
self.update_records(winner, loser)
|
||||
del self.boards[ctx.message.server.id]
|
||||
else:
|
||||
if board.full():
|
||||
|
@ -226,20 +226,22 @@ class TicTacToe:
|
|||
del self.boards[ctx.message.server.id]
|
||||
else:
|
||||
await self.bot.say(str(board))
|
||||
|
||||
@tictactoe.command(name='start', aliases= ['challenge','create'], pass_context=True, no_pm=True)
|
||||
|
||||
@tictactoe.command(name='start', aliases=['challenge', 'create'], pass_context=True, no_pm=True)
|
||||
@checks.custom_perms(send_messages=True)
|
||||
async def start_game(self, ctx, player2: discord.Member):
|
||||
"""Starts a game of tictactoe with another player"""
|
||||
player1 = ctx.message.author
|
||||
if self.boards.get(ctx.message.server.id) != None:
|
||||
if self.boards.get(ctx.message.server.id) is not None:
|
||||
await self.bot.say("Sorry but only one Tic-Tac-Toe game can be running per server!")
|
||||
return
|
||||
x_player = self.create(ctx.message.server.id, player1, player2)
|
||||
fmt = "A tictactoe game has just started between {} and {}".format(player1.display_name, player2.display_name)
|
||||
fmt += str(self.boards[ctx.message.server.id])
|
||||
fmt += "I have decided at random, and {} is going to be x's this game. It is your turn first!".format(x_player.display_name)
|
||||
fmt += "I have decided at random, and {} is going to be x's this game. It is your turn first!".format(
|
||||
x_player.display_name)
|
||||
await self.bot.say(fmt)
|
||||
|
||||
|
||||
def setup(bot):
|
||||
bot.add_cog(TicTacToe(bot))
|
||||
|
|
Loading…
Reference in a new issue