完全破坏一个实例

时间:2017-06-22 07:44:30

标签: javascript node.js

我正在制作一个持续100分钟或者totalItemAmount达到500所需时间的游戏。

如果" addPlayer"在游戏结束时被触发,我想将该玩家推到下一轮。

我希望能够完美地摧毁它/创造新的。

第一场比赛总是运转良好。

以下游戏出错了。总而言之,我觉得第一场比赛结束后并没有被正确销毁。显然,我的代码比你在这里看到的要大得多,但是让我问你,我的结构有什么问题吗?

function Game(duration, playersForThisRound) {
    console.log('NEW GAME')
    this.playersForNextRound = [];
    this.id = Math.random();
    this.finished = 0;
    this.players = {};
    this.duration = 100;
    this.totalItemAmount = 0;
    this.endTimeout = setTimeout(() => {
        this.end(this);
        return
    }, this.duration * 60 * 1000);
    if (playersForThisRound && playersForThisRound.length > 0) {
        Object.keys(this.playersForNextRound).forEach(function(key) {
            this.addPlayer(this, key.player, key.items)
        });
    }

    return
}

Game.prototype.addPlayer = function(game, player) {
    if (this.finished || this.totalItemAmount >= 500) {
        var playerIsInNextRound = 0;
        Object.keys(this.playersForNextRound).forEach(function(key) {
            if (key.id == player.id) {
                playerIsInNextRound = 1
                key.items.push(items)
            }
        })
        if (!playerIsInNextRound)
            this.playersForNextRound.push({
                "id": player.id,
                "player": player,
                "items": items
            });

        if (game.totalItemAmount >= 500) {
            clearTimeout(game.endTimeout)
            this.end(this);
        }
        return
    }

    if (!game.players[player.id]) {
        game.players[player.id] = new Player(game, player, items);

        game.players[player.id].addItems(game, items, function(added) {
            game.jackpot += added;
            Object.keys(game.players).forEach(function(player) {
                game.players[player].chance = game.players[player].getChance(game.jackpot)
            });

        })

    }

    Game.prototype.end = function(game) {
        game.finished = 1;
        clearTimeout(game.endTimeout)
        setTimeout(() => {
            game = new Game(10, this.playersForNextRound);
        }, 1000 * 60 * 10);
    }
    //To start first game
    var game = new Game();
    module.exports = game;

现在在另一个档案中:

let game = require("./lib/games/roundbased.js");
   let test = setInterval(() => {
        for (var i = 0; i < 100; i++) {
           game.addPlayer(game, {
                id: '12345' + i,
                nick: "player" + i
                },

0 个答案:

没有答案