def handle_win(number, sections, loser_submit=False): # check that both players exist in system winner = get_object(Player, phone_number=number) if not winner: return HttpResponse(createSmsResponse("Join Undisputed first")) loser_username = sections[1] loser = get_object(Player, username=loser_username) if not loser: return HttpResponse(createSmsResponse(loser_username + " does not exist. Please try again.")) # check if the result is just a competition competition_name = sections[-1] league = get_object(League, shorthand_name=competition_name) if not league: league = get_object(League, name=competition_name) # if this flag is true, swap if loser_submit: temp = winner winner = loser loser = temp teams = Team.objects.filter(league=league) # check that message was not malformed if len(sections) != 4: return HttpResponse(createSmsResponse('multiperson teams and multiword competitions not implemented yet')) # search for existing teams winning_team = get_object(Team, name=winner.username) losing_team = get_object(Team, name=loser.username) if not winning_team: new_team = Team(league=league,rating=2000,ranking=100, name=winner.username) new_team.members = winner new_team.name = winner.username new_team.save() winning_team = new_team # search for the loser's team if not losing_team: new_l_team = Team(league=league,rating=2000, ranking=100) new_l_team.members = loser new_l_team.name = loser.username new_l_team.save() losing_team = new_l_team # save the result #Todo- add teams to results new_result = Result.objects.create(league=league,time=datetime.now(),winner=winning_team,loser=losing_team) # use Elo's algorithm to calculate the new ratings calculate_elo_update(winning_team, losing_team) # update each team's streaks winning_team.update_streak(True) losing_team.update_streak(False) # use the new ratings to calculate new rankings teams = Team.objects.filter(league=league).order_by("rating").all().reverse() rank = 1 for team in teams: team.ranking = rank team.save() rank += 1 winning_team = Team.objects.get(name=winner.username) losing_team = Team.objects.get(name=loser.username) NOTIFICATIONS = True loser_string = "You were defeated by %s in %s. Your new rating is %s and you are ranked %s" % (winner.username.upper(), league.name, int(losing_team.rating), losing_team.ranking) winner_string = "Congrats on beating %s! Your new rating is %s and you are ranked #%s in %s. A notification was sent to %s." % (loser.username.upper(), int(winning_team.rating), int(winning_team.ranking), winning_team.league.name, loser.username.upper()) if loser_submit: to_msg = winner_string return_msg = loser_string to_phone_number = winner.phone_number else: to_msg = loser_string return_msg = winner_string to_phone_number = loser.phone_number if competition_name == "ksfifa": TWILIO_NUMBER = "+19786730440" elif competition_name == "kspingpong": TWILIO_NUMBER = "+14793163917" if NOTIFICATIONS: client.sms.messages.create( to=str(to_phone_number), from_=TWILIO_NUMBER, body=to_msg) return HttpResponse( createSmsResponse(return_msg ))