Exemple #1
0
def popularity(match_id=None):
    if match_id is None:
        abort(400)
    match = Match.fetch_by_id(match_id)
    final_result = {
        "team_a":calcPopularity(match.team_a),
        "team_b":calcPopularity(match.team_b),
        "match":match.to_dict()
    }
    return json_response(final_result)
Exemple #2
0
def mybet(match_id=None):    
    user = users.get_current_user()
    if not user:
        abort(401)
    else:
        bets = None
        if match_id is not None:
            bets = Bet.query(ndb.AND(Bet.userid==user.user_id(), Bet.bet_match_id==int(match_id))).fetch()
        else:
            bets = Bet.query(Bet.userid==user.user_id()).fetch()
        results = []
        for bet in bets:
            result = bet.to_dict()
            match = Match.fetch_by_id(bet.bet_match_id)
            result['match'] = match.to_dict()
            results.append(result)
        return json_response(results)
Exemple #3
0
def report_match(match_id=None):
    user = users.get_current_user()
    if match_id is None or not user:
        abort(401)
    bets = Bet.query(Bet.bet_match_id==int(match_id)).fetch()
    match = Match.fetch_by_id(match_id)

    # No report page until 10 minutes prior to the beginning of the match
    if datetime.utcnow()+timedelta(minutes=10) <= match.date:
        abort(400)

    show_known_user = is_known_user(user.email())
    bet_results = []
    for bet in bets:
        result = bet.to_dict()
        result['useremail'] = known_user_name(result['useremail'])
        result['match'] = match.to_dict()
        bet_results.append(result)
    return json_response(bet_results)
Exemple #4
0
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)
Exemple #5
0
def bet(match_id, bet_amount=1):
    """Put down a bet"""
    user = users.get_current_user()
    if not user:
        abort(401)
        #return redirect(users.create_login_url(url_for('main')))
    else:
        match = Match.fetch_by_id(match_id)

        # Shutdown bet channel 10 minutes prior to the beginning of the match
        if datetime.utcnow()+timedelta(minutes=10) > match.date:
            abort(400)

        logging.info('betting on %s' % str(match))
        # Don't bet on played matches
        if match.score_a is not None or match.score_b is not None:
            abort(400)
        bets = Bet.query(ndb.AND(Bet.userid==user.user_id(), Bet.bet_match_id==int(match_id))).fetch()
        result = {}
        score_a = request.args.get('sa',None)
        score_b = request.args.get('sb',None)
        extra_a = request.args.get('ea',None)
        extra_b = request.args.get('eb',None)
        penalty_a = request.args.get('pa',None)
        penalty_b = request.args.get('pb',None)

        #Sanity check
        #score_a and score_b are mandtary.
        if score_a is None or score_b is None:
            logging.warning('missing one of the scores')
            abort(400)
        else:
            #extra_a/extra_b has to appear in pair.
            if extra_a is not None and extra_b is None:
                logging.warning('missing extra_b')
                abort(400)
            elif extra_a is None and extra_b is not None:
                logging.warning('missing extra_a')
                abort(400)
            #ok, as we have both extra_a/extra_b...
            elif extra_a is not None and extra_b is not None:
                # it has to be a tie prediction to involve extra scores
                if score_a != score_b:
                    logging.warning('not a tie score but extra presents [%d:%d]'%(len(extra_a),len(extra_b)))
                    abort(400)
                #penalty_a/penalty_b has to appear in pair.
                elif penalty_a is not None and penalty_b is None:
                    logging.warning('missing penalty_b')
                    abort(400)
                elif penalty_a is None and penalty_b is not None:
                    logging.warning('missing penalty_a')
                    abort(400)
                elif penalty_a is not None and penalty_b is not None:
                    # it has to be a tie prediction to involve penalty scores
                    if extra_a != extra_a:
                        logging.warning('not a tie extra but penalty presents')
                        abort(400)
            #no extra_a/extra_b, no penalty_a/penalty_b allowed
            else:
                if penalty_a is not None or penalty_b is not None:
                    logging.warning('no extra but penalty presents')
                    abort(400)

        if len(bets)==0:
            bet = Bet(userid=user.user_id(),
                      useremail=user.email(),
                      bet_match_id=int(match_id),
                      bet_amount=int(bet_amount),
                      score_a=int(score_a or 0) if score_a else None,
                      score_b=int(score_b or 0) if score_b else None,
                      extra_a=int(extra_a or 0) if extra_a else None,
                      extra_b=int(extra_b or 0) if extra_b else None,
                      penalty_a=int(penalty_a or 0) if penalty_a else None,
                      penalty_b=int(penalty_b or 0) if penalty_b else None
                      )
            logging.info('betting on %s' % str(bet))
            bet.put()
            result = bet.to_dict()
        else:
            bet = bets[0]
            bet.useremail=user.email()
            bet.bet_amount=int(bet_amount)
            if score_a:
                bet.score_a = int(score_a)
            if score_b:
                bet.score_b = int(score_b)
            if extra_a:
                bet.extra_a = int(extra_a)
            if extra_b:
                bet.extra_b = int(extra_b)
            if penalty_a:
                bet.penalty_a = int(penalty_a)
            if penalty_b:
                bet.penalty_b = int(penalty_b)
            bet.put()
            result = bet.to_dict()
        result['match'] = match.to_dict()
        return json_response(result)