/// returns [ {playId: ?, user: ?, amount: ? }, ...]
function calcBonuses(input) {
    // first, lets sum the bets..

    function sortCashOuts(input) {
        function r(c) {
            return c.stoppedAt ? -c.stoppedAt : null;
        }

        return _.sortBy(input, r);
    }

    // slides fn across array, providing [listRecords, stoppedAt, totalBetAmount]
    function slideSameStoppedAt(arr, fn) {
        var i = 0;
        while (i < arr.length) {
            var tmp = [];
            var betAmount = 0;
            var sa = arr[i].stoppedAt;
            for (; i < arr.length && arr[i].stoppedAt === sa; ++i) {
                betAmount += arr[i].bet;
                tmp.push(arr[i]);
            }
            assert(tmp.length >= 1);
            fn(tmp, sa, betAmount);
        }
    }

    var results = [];

    var sorted = sortCashOuts(input);

    if (sorted.length  === 0)
        return results;

    var bonusPool = 0;
    var largestBet = 0;

    for (var i = 0; i < sorted.length; ++i) {
        var record = sorted[i];

        assert(record.status === 'CASHED_OUT' || record.status === 'PLAYING');
        assert(record.playId);
        var bet = record.bet;
        assert(lib.isInt(bet));

        bonusPool += bet / 100;
        assert(lib.isInt(bonusPool));

        largestBet = Math.max(largestBet, bet);
    }

    var maxWinRatio = bonusPool / largestBet;

    slideSameStoppedAt(sorted,
        function(listOfRecords, cashOutAmount, totalBetAmount) {
            if (bonusPool <= 0)
                return;

            var toAllocAll = Math.min(totalBetAmount * maxWinRatio, bonusPool);

            for (var i = 0; i < listOfRecords.length; ++i) {
                var toAlloc = Math.round((listOfRecords[i].bet / totalBetAmount) * toAllocAll);

                if (toAlloc <= 0)
                    continue;

                bonusPool -= toAlloc;

                var playId = listOfRecords[i].playId;
                assert(lib.isInt(playId));
                var user = listOfRecords[i].user;
                assert(user);

                results.push({
                    playId: playId,
                    user: user,
                    amount: toAlloc
                });
            }
        }
    );

    return results;
}
Exemple #2
0
 dm.info(url).done(function() {
   assert(arguments.length);
   done();
 });
function readKeyPem(filename) {
    var raw_key = fs.readFileSync(filename, "utf8");
    var pemType = identifyPemType(raw_key);
    assert(typeof pemType === "string"); // must have a valid pem type
    return raw_key;
}
Exemple #4
0
 bench.add('Variant.encode', function () {
     assert(_.isFunction(Variant.prototype._schema.encode));
     test_iteration(variant, stream, Variant.prototype._schema.encode);
 })
Exemple #5
0
    function perform_benchmark(done) {

        var bench = new Benchmarker();

        var length = 1024;
        var sampleArray = new Float32Array(length);
        for (var i = 0; i < length; i++) {
            sampleArray[i] = 1.0 / (i + 1);
        }

        var stream = new BinaryStream(length * 4 + 30);
        var variant = new Variant({
            dataType: DataType.Float,
            arrayType: VariantArrayType.Array,
            value: sampleArray
        });
        assert(variant.value.buffer instanceof ArrayBuffer);

        stream.rewind();
        var r = [test_1, test_2, test_3, test_4, test_5, test_6].map(function (fct) {
            stream.rewind();
            fct(stream, variant.value);
            var reference_buf = stream._buffer.slice(0, stream._buffer.length);
            return reference_buf.toString("hex");
        });
        r[0].should.eql(r[1]);
        r[0].should.eql(r[2]);
        r[0].should.eql(r[3]);
        r[0].should.eql(r[4]);
        r[0].should.eql(r[5]);

        bench
            .add('test1', function () {
                test_iteration(variant, stream, test_1);
            })
            .add('test2', function () {
                test_iteration(variant, stream, test_2);
            })
            .add('test3', function () {
                test_iteration(variant, stream, test_3);
            })
            .add('test4', function () {
                test_iteration(variant, stream, test_4);
            })
            .add('test5', function () {
                test_iteration(variant, stream, test_5);
            })
            .add('test6', function () {
                test_iteration(variant, stream, test_6);
            })
            .on('cycle', function (message) {
                console.log(message);
            })
            .on('complete', function () {

                console.log(' slowest is ' + this.slowest.name);
                console.log(' Fastest is ' + this.fastest.name);
                console.log(' Speed Up : x', this.speedUp);
                // xxthis.fastest.name.should.eql("test4");
                done();
            })
            .run({max_time: 0.1});

    }
Exemple #6
0
 it('cannot initialize multiple plugins with the same role', function () {
     system.initialize(examples['bare-role-1']);
     assert(!system.isInitializable(examples['bare-role-2']));
 });
Exemple #7
0
 'test': function (name, value) {
     assert(name === 'test-hook');
     assert(value === true);
     done();
 }
Exemple #8
0
 return function isDecimalString(str) {
   assert(_.isString(str) || _.isNumber(str));
   return re.test(str);
 };
Exemple #9
0
Validator.addMethod('notMatch', function(regexp, tip) {
  assert(_.isString(this.val()));
  assert(_.isRegExp(regexp));
  this.checkNotPred(val => regexp.test(val), tip);
  return this;
});
Exemple #10
0
Validator.addMethod('uniq', function() {
  assert(_.isArray(this.val()));
  this.tap(_.uniq);
  return this;
});
Exemple #11
0
Validator.addMethod('trim', function() {
  assert(_.isString(this.val()));
  this.tap(x => x.trim());
  return this;
});
Exemple #12
0
Validator.addMethod('lte', function(otherVal, tip) {
  assert(_.isNumber(this.val()));
  assert(_.isNumber(otherVal));
  this.checkPred(val => val <= otherVal, tip);
  return this;
});
Exemple #13
0
Validator.addMethod('isNotIn', function(arr, tip) {
  assert(_.isArray(arr));
  this.checkNotPred(val => _.includes(arr, val), tip);
  return this;
});
Exemple #14
0
Validator.addMethod('checkNotPred', function(pred, tip) {
  assert(_.isFunction(pred));
  this.checkNot(pred.call(this.ctx, this.val()), tip);
  return this;
});
Exemple #15
0
 it('can load plugins with different names', function () {
     system.initialize(examples['bare']);
     system.initialize(examples['bare-2']);
     assert(system.hasPlugin('bare'));
     assert(system.hasPlugin('bare-2'));
 });
Exemple #16
0
 init: function (_context, imports) {
     assert(imports.role === true);
     done();
     return {};
 },
Exemple #17
0
 it('of a module with a role', function () {
     system.initialize(examples['bare-role-1']);
     assert(system.hasPlugin('bare-role-1'));
     assert(system.hasRole('bare'));
 });
Exemple #18
0
 it('hooks into every loaded module', function () {
     system.initialize(examples['test-hook']);
     assert(spy.calledOnce);
     assert(spy.calledWith('test-hook', true));
 });
Exemple #19
0
 init: function (context, imports) {
     assert(imports.exports === true);
     return {};
 },
Exemple #20
0
 it('skips plugins not using the hook', function () {
     system.initialize(examples['bare']);
     assert(!spy.called);
 });
Exemple #21
0
 client.performMessageTransaction(request,function(err,response){
     // RegisterServerResponse
     assert(response instanceof RegisterServerResponse);
     disconnect(callback);
 });
Exemple #22
0
 it('for plugins', function () {
     system.initialize(examples['exports']);
     assert(system.pluginExportsOf('exports') === true);
 });
Exemple #23
0
 .add('Variant.old_encode', function () {
     assert(_.isFunction(old_encode));
     test_iteration(variant, stream, old_encode);
 })
Exemple #24
0
 it('of a bare module', function () {
     system.initialize(examples['bare']);
     assert(system.hasPlugin('bare'));
     assert(equal(system.loadedNames(), ['bare']));
 });
Exemple #25
0
 it('Should return id of a dailymotion clip', function(done){
   var de = new DailyMotionExtractor(config);
   var id = de.getId(url);
   assert(id === 'xb64yc_movie-countdown-google-videos-2_shortfilms');
   done();
 });
Exemple #26
0
 init: function (_context) {
     assert(context === _context);
     return {};
 },
Exemple #27
0
 streamAssert(vidStream, file, function(err, equals) {
   assert(equals);
   assert(file instanceof Stream);
   done();
 });
Exemple #28
0
 it('cannot initialize the same module name twice', function () {
     system.initialize(examples['bare']);
     assert(!system.isInitializable(examples['bare']));
 });
function readCertificate(filename) {

    assert(typeof(filename) === "string");
    var raw_key = fs.readFileSync(filename);
    return readPEM(raw_key);
}
Game.prototype.endGame = function(forced) {
    var self = this;

    var gameId = self.gameId;

    assert(self.crashPoint == 0 || self.crashPoint >= 100);

    var bonuses = [];

    if (self.crashPoint !== 0) {
        bonuses = calcBonuses(self.players);

        var givenOut = 0;
        Object.keys(self.players).forEach(function(player) {
            var record = self.players[player];

            givenOut += record.bet * 0.01;
            if (record.status === 'CASHED_OUT') {
                var given = (record.stoppedAt/100) * record.bet;
                assert(typeof given === 'number' && given > 0);
                givenOut += given;
            }
        });

        self.bankroll -= givenOut;
    }

    var playerInfo = self.getInfo().player_info;
    var bonusJson = {};
    bonuses.forEach(function(entry) {
        bonusJson[entry.user.username] = entry.amount;
        playerInfo[entry.user.username].bonus = entry.amount;
    });

    self.lastHash = self.hash;

    // oh noes, we crashed!
    self.emit('game_crash', {
        forced: forced,
        elapsed: self.gameDuration,
        game_crash: self.crashPoint, // We send 0 to client in instant crash
        bonuses: bonusJson,
        hash: self.lastHash
    });

    self.gameHistory.addCompletedGame({
        game_id: gameId,
        game_crash: self.crashPoint,
        created: self.startTime,
        player_info: playerInfo,
        hash: self.lastHash
    });

    self.dbEnding = true;
    db.endGame(gameId, bonuses, function(err) {
      if (err)
         console.log('ERROR could not end game id: ', gameId, ' got err: ', err);

       self.dbEnding = false;
    });

    self.state = 'ENDED';
};