2015-05-31 216 views
-1

在随机表决我的问题并提示我使用拼接之前,可能实际上读取了我正在尝试执行的操作。谢谢。删除数组的元素

我正在写一个node.js聊天机器人和我需要删除数组的特定元素之一的命令。

这是命令如何工作:

中的项目“富”,“酒吧”,“巴兹”被添加到称为raidLoot阵列。对于这些数组元素中的每一个,都会创建一个数组,供用户添加,并为每个数组选择一个随机用户。因此,如果我有raidLoot = [foo]和另一个名为foo = [userA,userB,userC]的数组,则在选择其中一个用户后,应从raidLoot中删除'foo'。

这是相关代码

case 'loot': 
    query(connection, 'SELECT * FROM channel WHERE name = "' + userName + '"').done(function (result) { 
     if (result[0].length !== 0) { 
      if (args[1] === 'clear') { 
       raidLoot = [] 
       send_PRIVGRP_MESSAGE(botId, userName + ' cleared the loot list') 
      } else { 
       raidLoot.push(args.slice(1).join(' ')) 
       send_PRIVGRP_MESSAGE(botId, userName + ' added ' + args.slice(1).join(' ') + ' to slot #' + raidLoot.length + '. Use !add ' + raidLoot.length + ' to join ') 
       lootSlot[raidLoot.length] = [] 
      } 


     } else { 
      send_MESSAGE_PRIVATE(userId, 'You have to join the channel first') 
     } 
    }) 

    break; 

而这正是我的问题是:

exports.flatroll = flatroll = function (userId, args) { 
    if (raidLoot.length === 0) { 
     send_MESSAGE_PRIVATE(userId, 'There is no loot') 
     return 
    } 
    connectdb().done(function (connection) { 
     checkAccess(userId).done(function (result) { 
      userAc = result 
      access_req(connection, 'rem').done(function (result) { 
       if (result[0].length === 0 || result[0].length > 0 && result[0][0].status === 'enabled') { 
        if (result[0].length === 0 || result[0][0].access_req <= userAc) { 
         getUserName(connection, userId).done(function (result) { 
          userName = result[0][0].name 
          if (!args) { 
           winnerList = '<center> <font color=#FFFF00> :::Flatroll Results::: </font> </center> \n' 
           for (loot in raidLoot) { 
            winnerList += '<font color=#00FFFF>Slot #' + (+loot + 1) + '</font> \n' 
            winnerList += 'Item: ' + raidLoot[loot] + '\n' 
            if (lootSlot[+loot + 1].length === 0) { 
             winnerList += 'Winner: No one added \n' 
            } else { 
             winnerList += 'Winner:</font><font color=#00FF00>' + _.sample(lootSlot[+loot + 1]) + '</font> \n' 
             lootSlot[+loot + 1] = [] 
             raidLoot.splice(loot, 1) // This is the line I need a better alternative for. 
            } 

            winnerList += '<img src=tdb://id:GFX_GUI_FRIENDLIST_SPLITTER>\n' 

           } 
           send_PRIVGRP_MESSAGE(botId, blob('Winner List', winnerList)) 
           connection.release() 
          } // else with args 
         }) 
        } else { 
         connection.release() 
         send_MESSAGE_PRIVATE(userId, 'Access Denied') 
        } 
       } else { 
        connection.release() 
        send_MESSAGE_PRIVATE(userId, 'Command Disabled') 
       } 
      }) 
     }) 
    }) 
} 

'raidLoot.splice(抢劫,1)' 这确实工作它应该怎样,但结果不是我所需要的,如果添加了多个项目并且用户只加入其中一个项目,那么显示的列表将包含除最后一个项目以外的所有项目,修改raidLoot的长度以便循环不会到达最后一个项目并且如此多项目,少显示。

我是新节点,做这个项目的方式来学习,所以不要在恶劣的我福利局代码:)

+0

请节点+ HTML4伤害我的眼睛。 – moonwave99

+1

html用于游戏内的窗口,这个bot是MMORPG,没有别的选择,因为它只接受非常基本的html。 – Trax

回答

0

看来,它的作品,如果我在2个独立的回路分开for循环,提取胜利者1名,取消获胜者名单的第二名。

if (!args) { 
    winnerList = '<center> <font color=#FFFF00> :::Flatroll Results::: </font> </center> \n' 
    var rl = raidLoot 
    for (i = 0; i < rl.length; i++) { 
     winnerList += '<font color=#00FFFF>Slot #' + (i + 1) + '</font> \n' 
     winnerList += 'Item: ' + raidLoot[i] + '\n' 
     if (lootSlot[i + 1].length === 0) { 
      winnerList += 'Winner: No one added \n' 
     } else { 
      shuffleUsers = _.shuffle(lootSlot[i + 1]) 
      console.log(shuffleUsers) 
      winnerList += 'Winner:</font><font color=#00FF00>' + _.sample(shuffleUsers) + '</font> \n' 
     } 

     winnerList += '<img src=tdb://id:GFX_GUI_FRIENDLIST_SPLITTER>\n' 

    } 
    send_PRIVGRP_MESSAGE(botId, blob('Winner List', winnerList)) 

    for (i = 0; i < rl.length; i++) { 
     if (lootSlot[i + 1].length > 0) { 
      lootSlot[i + 1] = [] 
      raidLoot = _.without(raidLoot, rl[i]) 
     } 
    } 
    connection.release() 
    }