将事件从服务器发送到客户端以运行函数

时间:2015-11-16 10:20:53

标签: javascript node.js

我查看了各种答案,但我无法找到一种方法来实现这一目标。

我有一个函数(在node.js中)从池中选择一个胜利者,当它选择胜利者时,我需要它将一个事件发送到客户端,在那里它运行一个带数据的函数。数据将是获胜者的数组索引。

选择获胜者:

var endRound = function() {
    ref.child('currentJackpot').once('value', function(data) {
        var currentJackpot = data.val();
        var winnerArray = [];
        var winnerObj = {};
        winnerObj.items = [];
        for (var i = 0; i < currentJackpot.players.length; i++) {
            winnerObj.items = winnerObj.items.concat(currentJackpot.players[i].items);
            var playerValue = currentJackpot.players[i].itemsValue * 100;
            currentJackpot.players[i].chance = ((currentJackpot.players[i].itemsValue / currentJackpot.jackpotValue) * 100).toFixed(2);
            for (var j = 0; j < playerValue; j++) {
                winnerArray.push(i);
            }
        }
        var formatted = currentJackpot.roundHash.replace(/[.#$/]/g, "");
        sgRef.child(formatted).once('value', function(data) {
            var sgData = data.val();
            salt = sgData.salt;
            rngStr = sgData.rngStr;
            console.log('ROUND ENDED! hash: ', hash, ' salt: ', salt, ' rngStr: ', rngStr);
            currentJackpot.tickets = currentJackpot.jackpotValue * 100;
            currentJackpot.winningTicket = Math.floor((parseFloat(rngStr, 2) * currentJackpot.tickets));
            currentJackpot.winningNumber = (parseFloat(rngStr, 2) * 100).toFixed(2) + "%";
            currentJackpot.winner = currentJackpot.players[winnerArray[currentJackpot.winningTicket]];
            currentJackpot.salt = salt;
            currentJackpot.rngStr = rngStr;
            winnerObj.jackpotValue = currentJackpot.jackpotValue;
            currentJackpot.jackpotValue = currentJackpot.jackpotValue.toFixed(2);
            winnerObj.winner = currentJackpot.winner;
            winnerObj.tradeToken = currentJackpot.winner.tradeToken;
            ref.child('endedJackpots').push(currentJackpot);
            bcrypt.genSalt(10, function(err, data) {
                salt = data;
                rngStr = JSON.stringify(rng());
                bcrypt.hash(rngStr, salt, function(err, data) {
                    hash = data;
                    ref.child('currentJackpot').set({
                        itemsCount: 0,
                        jackpotValue: 0,
                        roundHash: hash,
                    }, function() {
                        console.log('NEW ROUND! hash: ', hash, 'salt: ', salt, 'rngStr: ', rngStr);
                        var formatted = hash.replace(/[.#$/]/g, "");
                        var sgJackpotRef = sgRef.child(formatted);
                        sgRef.set({}, function() {
                            sgJackpotRef.set({
                                salt: salt,
                                rngStr: rngStr,
                            }, function() {
                                request.post({
                                    url: '*******',
                                    body: winnerObj,
                                    json: true,
                                }, function(error, response, body) {
                                    if (error) {
                                        console.log(error);
                                        setPollTimer(10000);
                                    } else {
                                        usersRef.child(winnerObj.winner.id).once('value', function(data) {
                                            var userData = data.val();
                                            if (data.child('won').exists()) {
                                                userData.won = (Math.floor(parseFloat(userData.won, 2)) + Math.floor(parseFloat(winnerObj.jackpotValue, 2))).toFixed(2);
                                            } else {
                                                userData.won = (Math.floor(parseFloat(winnerObj.jackpotValue, 2))).toFixed(2);
                                            }
                                            usersRef.child(winnerObj.winner.id).update({
                                                won: userData.won
                                            }, function() {
                                                console.log('Added winnings to user data');
                                            });
                                        });
                                        console.log('Making a withdraw request now to bot');
                                        setPollTimer(10000);
                                    }
                                });
                            });
                        });
                    });
                });
            });
        });
    });
};

它应该运行客户端的功能:

  function slotMachine(winnerIndex) {
    var params = {
      active: 3,
      randomize: function(activeElementIndex){
            return 1;
          }
    };
    var machine = $('.slot').slotMachine( params );

    $("#slotMachineButton").click(function(){
      machine.shuffle(3, function(){
        $(this).text("Index: " + this.active);
      });
    });
  }

我完全陷入困境并强调,这不是我的代码,而且很多代码我无法掌握从哪里开始这样做。有什么帮助吗?

0 个答案:

没有答案