等待用户响应,然后继续执行脚本

时间:2019-06-24 17:22:10

标签: javascript node.js

基本上,我正在研究基于文本的小型多人rpg,我需要向用户发送不同类型的对话框:文本,菜单或输入。一个菜单完成后,可以在其后启动另一个菜单。我正在使用tcp进行通信。

var name = sendInput(player, "What is your name?");
// sends the packet with "What is your name?" and then waits for a response input packet
var response = sendMenu(player, "Is your name " + name + "?", ["Yes","No"]);
//wait for the menu packet before going next
if(response == "Yes") {
     sendDialog(player, ["test1","test2"]);
     // wait for when they finish the dialog and another menu can happen
     response = sendMenu(player, "Where do you want to start?", ["North","South"]);
} else {
     sendDialog(player, ["goodbye"]);
}

我不确定如何设置它以等待响应,然后再针对不同的玩家进入下一部分。我不希望代码阻止其他任何I / O或操作。

编辑:这是我想出的(只是一个测试),它在某种程度上满足了我的期望。无论如何,是否不使用yield或移动yield以便我可以调用函数?服务器收到数据包“ 1”时,将启动对话框。对话框中将继续发送“ 2”。这类似于大型生产游戏在服务器端处理npc对话框脚本的方式。

module.exports = {
    parse: function(socket, data) {
        data = data.toString();
        switch (data[0]) {
            case "1": 
                socket.dialog = dialog(socket); 
                socket.dialog.next(); 
                break;
            case "2": 
                socket.dialog.next(); 
                break;
        }        
    }
}

function* dialog(socket) {
    sendDialog(socket, ["first message","second message"]);
    yield;
    sendDialog(socket, ["third message"]);
    yield;
    sendDialog(socket, ["fourth message"]);
    yield;        
}

function sendDialog(socket, message) {
    socket.write(JSON.stringify(message));
}

编辑2:这就是我所处的位置,它仅用于单向对话框,但是我想将其设置为我可以在等待用户响应的位置,例如在对话框函数中向他们发送问题。

var name = sendInput(player, "What is your name?");

0 个答案:

没有答案
相关问题