lib/emulator_market.js
function placeBets() {
        // check input bets list
        var error;
        for ( var i = 0; i < req.request.bets.length; ++i) {
            var desc = req.request.bets[i];
            error = checkPlaceBetItem(self, desc);
            // console.log('EMU bet', desc, "error", error);
            if (error)
                break;
        }

        // It is very strange, but Betfair returns 'OK'
        // when bet size or price is invalid
        res.response.errorCode = 'OK';
        res.response.betResults = [];
        if (error) {
            // Prepare response, no bets placed
            for ( var i = 0; i < req.request.bets.length; ++i) {
                var resItem = {
                    averagePriceMatched : '0.0',
                    betId : '0',
                    resultCode : error,
                    sizeMatched : '0.0',
                    success : 'false'
                };
                res.response.betResults.push(resItem);
            }
            cb(null, res);
            return;
        }

        // Create bets
        var betIds = [];
        for ( var i = 0; i < req.request.bets.length; ++i) {
            var desc = req.request.bets[i];
            var bet = new EmulatorBet(desc.marketId, desc.selectionId, desc.betType,
                    desc.price, desc.size);
            betIds.push(bet.betId);
            self.bets[bet.betId] = bet;
        }

        // Try to match bets using price matching
        var bets = betIds.map(function(id) {
            return self.bets[id];
        });
        matchBetsUsingPrices(self, bets);

        // Prepare response
        for ( var id in betIds) {
            var betId = betIds[id];
            var bet = self.bets[betId];
            var resItem = {
                averagePriceMatched : bet.averageMatchedPrice(),
                betId : bet.betId,
                resultCode : 'OK',
                sizeMatched : bet.matchedSize(),
                success : 'true'
            };

            res.response.betResults.push(resItem);
        }

        // placeBets was OK
        cb(null, res);
    }
function placeBets() {
        // check input bets list
        var error;
        for ( var i = 0; i < req.request.bets.length; ++i) {
            var desc = req.request.bets[i];
            error = checkPlaceBetItem(self, desc);
            // console.log('EMU bet', desc, "error", error);
            if (error)
                break;
        }

        // It is very strange, but Betfair returns 'OK'
        // when bet size or price is invalid
        res.response.errorCode = 'OK';
        res.response.betResults = [];
        if (error) {
            // Prepare response, no bets placed
            for ( var i = 0; i < req.request.bets.length; ++i) {
                var resItem = {
                    averagePriceMatched : '0.0',
                    betId : '0',
                    resultCode : error,
                    sizeMatched : '0.0',
                    success : 'false'
                };
                res.response.betResults.push(resItem);
            }
            cb(null, res);
            return;
        }

        // Create bets
        var betIds = [];
        for ( var i = 0; i < req.request.bets.length; ++i) {
            var desc = req.request.bets[i];
            var bet = new EmulatorBet(desc.marketId, desc.selectionId, desc.betType,
                    desc.price, desc.size);
            betIds.push(bet.betId);
            self.bets[bet.betId] = bet;
        }

        // Try to match bets using price matching
        var bets = betIds.map(function(id) {
            return self.bets[id];
        });
        matchBetsUsingPrices(self, bets);

        // Prepare response
        for ( var id in betIds) {
            var betId = betIds[id];
            var bet = self.bets[betId];
            var resItem = {
                averagePriceMatched : bet.averageMatchedPrice(),
                betId : bet.betId,
                resultCode : 'OK',
                sizeMatched : bet.matchedSize(),
                success : 'true'
            };

            res.response.betResults.push(resItem);
        }

        // placeBets was OK
        cb(null, res);
    }
function placeBets ( ) { // check input bets list var error ; for ( var i = 0 ; i < req . request . bets . length ; ++ i ) { var desc = req . request . bets [ i ] ; error = checkPlaceBetItem ( self , desc ) ; // console.log('EMU bet', desc, "error", error); if ( error ) break ; } // It is very strange, but Betfair returns 'OK' // when bet size or price is invalid res . response . errorCode = 'OK' ; res . response . betResults = [ ] ; if ( error ) { // Prepare response, no bets placed for ( var i = 0 ; i < req . request . bets . length ; ++ i ) { var resItem = { averagePriceMatched : '0.0' , betId : '0' , resultCode : error , sizeMatched : '0.0' , success : 'false' } ; res . response . betResults . push ( resItem ) ; } cb ( null , res ) ; return ; } // Create bets var betIds = [ ] ; for ( var i = 0 ; i < req . request . bets . length ; ++ i ) { var desc = req . request . bets [ i ] ; var bet = new EmulatorBet ( desc . marketId , desc . selectionId , desc . betType , desc . price , desc . size ) ; betIds . push ( bet . betId ) ; self . bets [ bet . betId ] = bet ; } // Try to match bets using price matching var bets = betIds . map ( function ( id ) { return self . bets [ id ] ; } ) ; matchBetsUsingPrices ( self , bets ) ; // Prepare response for ( var id in betIds ) { var betId = betIds [ id ] ; var bet = self . bets [ betId ] ; var resItem = { averagePriceMatched : bet . averageMatchedPrice ( ) , betId : bet . betId , resultCode : 'OK' , sizeMatched : bet . matchedSize ( ) , success : 'true' } ; res . response . betResults . push ( resItem ) ; } // placeBets was OK cb ( null , res ) ; }
place bets, the function is delayed to simulate slow network
place bets the function is delayed to simulate slow network