def pick_random_two_people(): # Randomly picks two players from the Players collection to have a go at # each other! creds = object_rocket.get_creds() Database = object_rocket.get_connection(creds) choice1 = random.randrange(1, object_rocket.get_count_Players(Database)) choice2 = random.randrange(1, object_rocket.get_count_Players(Database)) return (Database.Players.find({}, {'_id': 0}).limit(-1).skip(choice1).next(), Database.Players.find({}, {'_id': 0}).limit(-1).skip(choice2).next())
def leaderboard_update(winner, score): # Update the Leaderboard after getting the results of the match creds = object_rocket.get_creds() Database = object_rocket.get_connection(creds) Database.Leaderboard.update({ 'first_name': winner}, {"$inc": {'Wins': 1}}) Database.Leaderboard.update({ 'first_name': winner}, {"$set": {'Best': score}}) Database.Leaderboard.update({'first_name': message1[ 'body']['first_name']}, {"$inc": {'Matches': 1}}) Database.Leaderboard.update({'first_name': message2[ 'body']['first_name']}, {"$inc": {'Matches': 1}})
def get_leaderboard(): # Print Leaderbaord creds = object_rocket.get_creds() Database = object_rocket.get_connection(creds) print "\t\t\t\t\t\t\t\tLeaderBoard\n" table = PrettyTable(["Name", "Matches", "Wins", "Best"]) table.align["Name"] = "l" table.padding_width = 1 # print "\t\t\tName\t\t\tMatches\t\t\tWins\t\t\tBest" for posts in Database.Leaderboard.find().sort("Wins", -1): table.add_row([posts['first_name'].split()[ 0], posts['Matches'], posts['Wins'], posts['Best']]) # print # "\t\t\t{0}\t\t\t{1}\t\t\t{2}\t\t\t{3}".format(posts['first_name'].split()[0],posts['Matches'],posts['Wins'],posts['Best']) print table