def add_match(self, match): """ Called on each participant after a match ends. """ # update cache results with game result if self.cached_results: results = json.loads(self.cached_results) else: results = [] winner = match.winner == self opponent = match.loser if winner else match.winner new_rating = match.winner_rating_after if winner else \ match.loser_rating_after results.append({ 'winner': winner, 'opponent_name': opponent.name, 'played_time': str(match.played_time), 'played_timestamp': get_timestamp(match.played_time) }) self.cached_results = json.dumps(results[-CACHED_RATING_LIMIT:]) # update player with new rating self.update_rating(new_rating, match) # save the player in the database self.save()
def update_rating(self, new_rating, match): """Called to update the player's rating after a match.""" self.rating = new_rating # update cached ratings with rating change if self.cached_rating_changes: rating_changes = json.loads(self.cached_rating_changes) else: rating_changes = [] rating_changes.append({ "rating": new_rating, "played_time": str(match.played_time), "played_timestamp": get_timestamp(match.played_time) }) self.cached_rating_changes = json.dumps( rating_changes[-CACHED_RATING_LIMIT:])
def update_rank(self, new_rank, match): """ Called to update the player's rank. This can be caused by OTHER player's matches in a rank stealing ladder. """ self.rank = new_rank # update cached ranks with rank change if self.cached_rank_changes: rank_changes = json.loads(self.cached_rank_changes) else: rank_changes = [] rank_changes.append({ "rank": new_rank, "played_time": str(match.played_time), "played_timestamp": get_timestamp(match.played_time) }) self.cached_rank_changes = json.dumps( rank_changes[-CACHED_RANK_LIMIT:]) self.save()