Javascript 彻底摧毁一个实例

Javascript 彻底摧毁一个实例,javascript,node.js,Javascript,Node.js,我正在制作一个回合数为100分钟的游戏,或者是totalItemAmount达到500所需的时间 如果“addPlayer”在游戏结束时被触发,我想将该玩家推到下一轮 我希望能够完美地摧毁它/创建新的 第一场比赛总是打得很好 接下来的游戏出错了。总而言之,我觉得第一场比赛结束后并没有被彻底摧毁。很明显,我的代码比你们在这里看到的要大得多,但让我问你们,我的结构有什么问题吗 function Game(duration, playersForThisRound) { console.log

我正在制作一个回合数为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
                },
let game=require(“./lib/games/roundbase.js”);
let test=setInterval(()=>{
对于(变量i=0;i<100;i++){
游戏。添加玩家(游戏{
id:'12345'+i,
尼克:“玩家”+i
},

你错过了关闭
addPlayer
方法。在
end
之前添加
}
@Tushar显然,我的代码比你在下面看到的要大得多……是的,伙计们。忽略输入错误(很抱歉)。问题是我不能在当前游戏中调用“删除”,而游戏=新游戏()工作效率不高。请看。@RobG您还需要什么?