def list_matches_by_date(): """Return a list of match list group by date""" matches_by_date = {} for match in Match.fetch_all(): d = date.fromtimestamp(time.mktime(match.date.timetuple())) if not d in matches_by_date: matches_by_date[d] = [] matches_by_date[d].append(match.to_dict()) return json_response(sorted(matches_by_date.values(), key=lambda list: list[0]['date']))
def bestbet(): user = users.get_current_user() if not user: abort(401) else: show_known_user = is_known_user(user.email()) final_results = memcache.get('[BestBet]'+str(show_known_user)) if final_results is None: if show_known_user: matches = Match.fetch_all() finished_matches = [] for match in matches: if match.score_a is not None and match.score_b is not None: finished_matches.append(match) finished_matches = sorted(finished_matches, cmp=lambda x, y:cmp(x.date,y.date)) results = {} #key:useremail/known_name, value:rightAboutScore/rightAboutWin slipped_award = 0.0 for match in finished_matches: bets = Bet.fetch_by_matchid(match.matchid) #collect slipped award award_pool = {'total_amount':len(known_users)*3.0+slipped_award,'bingo_count':0,'bingo_user':[]} slipped_award = 0.0 for bet in bets: #only process known users if not is_known_user(bet.useremail) or bet.useremail=='*****@*****.**' or bet.useremail=='*****@*****.**': logging.info('%s is not known user, skip it.' % str(bet.useremail)) continue #replace with known name bet.useremail = known_user_name(bet.useremail) #initialize results for this user if bet.useremail not in results: results[bet.useremail] = {'rightAboutScore':0,'rightAboutWin':0,'points':0} #Bingo! if match.score_a == bet.score_a and match.score_b == bet.score_b and match.extra_a == bet.extra_a and match.extra_b == bet.extra_b: results[bet.useremail]['rightAboutScore']+=1 award_pool['bingo_count']+=1 award_pool['bingo_user'].append(bet.useremail) if cmp(match.score_a, match.score_b) == cmp(bet.score_a, bet.score_b): if match.extra_a is not None and match.extra_b is not None: if bet.extra_a is not None and bet.extra_b is not None: if cmp(match.extra_a, match.extra_b) == cmp(bet.extra_a, bet.extra_b): results[bet.useremail]['rightAboutWin']+=1 else: results[bet.useremail]['rightAboutWin']+=1 #no one bingo, the pool goes to next match if award_pool['bingo_count'] == 0: slipped_award = award_pool['total_amount'] logging.info('Award %d of match between %s and %s slipped to next match' % (slipped_award, match.team_a, match.team_b)) else: award = award_pool['total_amount']/award_pool['bingo_count'] logging.info('Award %d of match between %s and %s got distributed to following users, %d for each: %s' % (award_pool['total_amount'], match.team_a, match.team_b, award, ','.join(award_pool['bingo_user']))) for awarded_user in award_pool['bingo_user']: results[awarded_user]['points']+=award #sort by points/rightAboutScore/rightAboutWin for output final_results = sorted(results.iteritems(), reverse=True, cmp=lambda x, y: cmp(x[1]['points'], y[1]['points']) or cmp(x[1]['rightAboutScore'], y[1]['rightAboutScore']) or cmp(x[1]['rightAboutWin'],y[1]['rightAboutWin'])) final_results = {"slipped_award":slipped_award, "results":final_results} else: #not known user: bets = Bet.fetch_all() results = {} for bet in bets: #only process bets of finished matches match = Match.fetch_by_id(bet.bet_match_id) if match.score_a is None or match.score_b is None: continue #statistic if bet.useremail not in results: results[bet.useremail] = {'rightAboutScore':0,'rightAboutWin':0} result = results[bet.useremail] #Bingo! if match.score_a == bet.score_a and match.score_b == bet.score_b and match.extra_a == bet.extra_a and match.extra_b == bet.extra_b: results[bet.useremail]['rightAboutScore']+=1 if cmp(match.score_a, match.score_b) == cmp(bet.score_a, bet.score_b): if match.extra_a is not None and match.extra_b is not None: if bet.extra_a is not None and bet.extra_b is not None: if cmp(match.extra_a, match.extra_b) == cmp(bet.extra_a, bet.extra_b): results[bet.useremail]['rightAboutWin']+=1 else: results[bet.useremail]['rightAboutWin']+=1 final_results = sorted(results.iteritems(), reverse=True, cmp=lambda x, y: cmp(x[1]['rightAboutScore'], y[1]['rightAboutScore']) or cmp(x[1]['rightAboutWin'],y[1]['rightAboutWin'])) final_results = {"slipped_award":0, "results":final_results} # expire in 5 minutes memcache.set('[BestBet]'+str(show_known_user), final_results, 300) return json_response(final_results)