Ejemplo n.º 1
0
def process_incoming(betslip):

    recv = wallet.getreceivedby(betslip, 0)

    if recv == 0:
        logging.info('Betslip %s: Nothing received' % (betslip))
        return

    # read betslip
    with open(btcs.path('bets/new', betslip), 'r') as f:
        betslip_data = json.loads(f.read())

    # Sum total in betslip
    total = Decimal('0')
    for bets in betslip_data['bets']:
        total += Decimal(bets['amount'])

    total = total / Decimal('1000')

    if recv == total:
        logging.info('Betslip %s: Received %s, moving to received' %
                     (betslip, recv))

        os.rename(btcs.path('bets/new', betslip),
                  btcs.path('bets/received', betslip))

        # attach game data for email
        if 'email_address' in betslip_data:
            for bet in betslip_data['bets']:
                try:
                    with open(btcs.path('games/new', bet['game']), 'r') as f:
                        bet['game_data'] = json.loads(f.read())

                except Exception:
                    # only for email; do not trip over it
                    pass
            notify_email.sendmail('email_betslip.html', betslip_data,
                                  betslip_data['email_address'],
                                  'Betslip payment received')

    else:
        logging.warning(
            'Betslip %s: Invalid amount received: %s, expected %s' %
            (betslip, recv, total))
        logging.warning('Return amount manually')
Ejemplo n.º 2
0
def process_incoming(betslip):

    recv = wallet.getreceivedby(betslip, 0)

    if recv == 0:
        logging.info('Betslip %s: Nothing received' % (betslip))
        return

    # read betslip
    with open(btcs.path('bets/new', betslip),'r') as f:
        betslip_data = json.loads(f.read())
    
    # Sum total in betslip
    total = Decimal('0')
    for bets in betslip_data['bets']:
        total += Decimal(bets['amount'])

    total = total / Decimal('1000')

    if recv == total:
        logging.info('Betslip %s: Received %s, moving to received' % (betslip, recv))
        
        os.rename(btcs.path('bets/new', betslip), btcs.path('bets/received', betslip))

        # attach game data for email
        if 'email_address' in betslip_data:
            for bet in betslip_data['bets']:
                try:
                    with open(btcs.path('games/new', bet['game']), 'r') as f:
                        bet['game_data'] = json.loads(f.read())

                except Exception:
                    # only for email; do not trip over it
                    pass
            notify_email.sendmail('email_betslip.html', betslip_data, 
                betslip_data['email_address'], 'Betslip payment received')

            


    else:
        logging.warning('Betslip %s: Invalid amount received: %s, expected %s' % (betslip, recv, total))
        logging.warning('Return amount manually')
Ejemplo n.º 3
0
def process_outgoing(gameid):

    # read betslip
    with open(btcs.path('games/finished', gameid),'r') as f:
        game = json.loads(f.read())
    
    if game['time'] in ['Abandonded', 'Postponed']:
        result = game['time'] # this will result in everyone is wrong
    elif game['time'] == ['Finished AP']:
        # penalties, only count winner
        result = winner(game['result'])
        logging.info('Finished with penalties; winner=%s' % result)

    elif game['time'] in ['Finished', 'Finished AET']:
        result = game['result']
    else:
        raise Exception('Unknown result type: %s' % game['time'])

    logging.info('Processing game %s; Result is %s', gameid, result)

    # collect all bets for this game
    correctbet = []
    wrongbet   = []
    invalidbet = []

    for betslipid in os.listdir(btcs.path('bets/received', '')):
        with open(btcs.path('bets/received', betslipid),'r') as f:
            betslip = json.loads(f.read())

        gamebets = [bet for bet in betslip['bets'] if bet['game'] == gameid]

        if not gamebets:
            continue

        # find the latest transaction for this slip
        tx = wallet.getlatesttx(betslipid)
        logging.info('TX = %s' % repr(tx))

        # add latest transaction and return_address and email_address to bets 
        for bet in gamebets:
            bet['latesttx'] = tx
            if 'return_address' in betslip:
                bet['return_address'] = betslip['return_address']
            else:
                bet['return_address'] = wallet.findreturnaddress(tx)

            if 'email_address' in betslip:
                bet['email_address'] = betslip['email_address']

            if within_deadline(game, bet):
                if bet['result'] == result or winner(bet['result']) == result:
                    correctbet.append(bet)
                else:
                    wrongbet.append(bet)
            else:
                invalidbet.append(bet)

            wallet.movetodispatch(betslipid, Decimal(bet['amount']) / Decimal('1000'))

    # move to process for atomicity
    if not DRYRUN:
        os.rename(btcs.path('games/finished', gameid),
            btcs.path('games/process', gameid))


    # process payouts
    if invalidbet:
        logging.info('Invalid: ' + repr(invalidbet))

        payout_tx = payout(invalidbet, sum_bets(invalidbet), game, 'invalid')

    if len(correctbet) >0:
        logging.info('Correct: ' + repr(correctbet))
        logging.info('Wrong  : ' + repr(wrongbet))

        total = sum_bets(wrongbet) * (Decimal('1') - btcs.BTCS_FEE) + sum_bets(correctbet)
        logging.info('Total wrong %s, total correct %s payout %s' % (
            repr(sum_bets(wrongbet)), repr(sum_bets(correctbet)), repr(total)))
        payout_tx = payout(correctbet, total, game, 'winnings' )

    elif len(wrongbet) > 0:
        logging.info('Only wrong bets: ' + repr(wrongbet))

        payout_tx = payout(wrongbet, sum_bets(wrongbet), game, 'allwrong' )


    # grab all email_addresses (ignore invalids)
    allbets = wrongbet + correctbet
    allemails = set([ bet['email_address'] for bet in allbets if 'email_address' in bet])

    for email in allemails:
        data = { 
            "email": email,
            "game": game, 
            "txid": payout_tx,
            "bets": [ bet for bet in allbets if 'email_address' in bet and bet['email_address'] == email]
        }

        for bet in data['bets']:
            if not 'txtype' in bet:
                bet['txtype'] = 'lost'
            elif bet['txtype'] == 'allwrong':
                bet['txtype'] = 'refund'
            else:
                bet['txtype'] = 'won'

        subj = 'Result: %s - %s  %s' % (game['home'], game['away'], game['result'])
        notify_email.sendmail('email_result.html', data, email, subj)


    if not DRYRUN:
        os.rename(btcs.path('games/process', gameid),
            btcs.path('games/archive', gameid))
Ejemplo n.º 4
0
def process_outgoing(gameid):

    # read betslip
    with open(btcs.path('games/finished', gameid), 'r') as f:
        game = json.loads(f.read())

    if game['time'] in ['Abandonded', 'Postponed']:
        result = game['time']  # this will result in everyone is wrong
    elif game['time'] == ['Finished AP']:
        # penalties, only count winner
        result = winner(game['result'])
        logging.info('Finished with penalties; winner=%s' % result)

    elif game['time'] in ['Finished', 'Finished AET']:
        result = game['result']
    else:
        raise Exception('Unknown result type: %s' % game['time'])

    logging.info('Processing game %s; Result is %s', gameid, result)

    # collect all bets for this game
    correctbet = []
    wrongbet = []
    invalidbet = []

    for betslipid in os.listdir(btcs.path('bets/received', '')):
        with open(btcs.path('bets/received', betslipid), 'r') as f:
            betslip = json.loads(f.read())

        gamebets = [bet for bet in betslip['bets'] if bet['game'] == gameid]

        if not gamebets:
            continue

        # find the latest transaction for this slip
        tx = wallet.getlatesttx(betslipid)
        logging.info('TX = %s' % repr(tx))

        # add latest transaction and return_address and email_address to bets
        for bet in gamebets:
            bet['latesttx'] = tx
            if 'return_address' in betslip:
                bet['return_address'] = betslip['return_address']
            else:
                bet['return_address'] = wallet.findreturnaddress(tx)

            if 'email_address' in betslip:
                bet['email_address'] = betslip['email_address']

            if within_deadline(game, bet):
                if bet['result'] == result or winner(bet['result']) == result:
                    correctbet.append(bet)
                else:
                    wrongbet.append(bet)
            else:
                invalidbet.append(bet)

            wallet.movetodispatch(betslipid,
                                  Decimal(bet['amount']) / Decimal('1000'))

    # move to process for atomicity
    if not DRYRUN:
        os.rename(btcs.path('games/finished', gameid),
                  btcs.path('games/process', gameid))

    # process payouts
    if invalidbet:
        logging.info('Invalid: ' + repr(invalidbet))

        payout_tx = payout(invalidbet, sum_bets(invalidbet), game, 'invalid')

    if len(correctbet) > 0:
        logging.info('Correct: ' + repr(correctbet))
        logging.info('Wrong  : ' + repr(wrongbet))

        total = sum_bets(wrongbet) * (Decimal('1') -
                                      btcs.BTCS_FEE) + sum_bets(correctbet)
        logging.info('Total wrong %s, total correct %s payout %s' % (repr(
            sum_bets(wrongbet)), repr(sum_bets(correctbet)), repr(total)))
        payout_tx = payout(correctbet, total, game, 'winnings')

    elif len(wrongbet) > 0:
        logging.info('Only wrong bets: ' + repr(wrongbet))

        payout_tx = payout(wrongbet, sum_bets(wrongbet), game, 'allwrong')

    # grab all email_addresses (ignore invalids)
    allbets = wrongbet + correctbet
    allemails = set(
        [bet['email_address'] for bet in allbets if 'email_address' in bet])

    for email in allemails:
        data = {
            "email":
            email,
            "game":
            game,
            "txid":
            payout_tx,
            "bets": [
                bet for bet in allbets
                if 'email_address' in bet and bet['email_address'] == email
            ]
        }

        for bet in data['bets']:
            if not 'txtype' in bet:
                bet['txtype'] = 'lost'
            elif bet['txtype'] == 'allwrong':
                bet['txtype'] = 'refund'
            else:
                bet['txtype'] = 'won'

        subj = 'Result: %s - %s  %s' % (game['home'], game['away'],
                                        game['result'])
        notify_email.sendmail('email_result.html', data, email, subj)

    if not DRYRUN:
        os.rename(btcs.path('games/process', gameid),
                  btcs.path('games/archive', gameid))