javascript - Node 应用程序通过暂停当前执行以交互方式从 react 前端获取用户输入

标签 javascript node.js reactjs express node-modules

我将一个用 python 编写的旧游戏转换为 javascript (node)。游戏只是在 while 循环中运行,直到完成一定数量的迭代。

runUntil(steps = 100000) {
var x = 0;
while (x < steps) {
  this.conversation();
  x++;
}

}

conversation() {
const roundPicture = this.getRandomPicture();
const conversationers = this.utils.getRandomTwo(this.network, this.Graph);

const chosen1 = conversationers[0];
const chosen2 = conversationers[1];


if (chosen1.name == "Player 0" && !chosen1.v.hasOwnProperty(roundPicture)) {
  //Wait for user input
  //..
  //..
  //..
  //Use the given input in order to continue game

}

if (chosen2.name == "Player 0" && !chosen2.v.hasOwnProperty(roundPicture)) {
    //Wait for user input
    //..
    //..
    //..
    //Use the given input in order to continue game

} else {
  //do sth else
}

}

在某些情况下,游戏会暂停以获取所需的用户输入并影响游戏的结果。在我的 javascript 实现中,我使用 readline-sync 来暂停游戏并通过命令提示符获取用户输入。现在,我构建了一个 React 前端应用程序,以便在具有 UI 的浏览器中提供游戏服务,并使用 Express 构建了一个服务器来处理 API 并在用户按下开始时运行游戏。

const express = require("express");
const http = require("http");
const socketIO = require("socket.io");
const Game = require("./Game");
const port = 4000;
const app = express();
const server = http.createServer(app);
//Create socket usin server instance
const io = socketIO(server);

io.on("connection", socket => {

  console.log("user connected!");
  socket.on("startGame", data => {
    const nI = data.nI;
    const pI = data.pI;
    const noOfAgents = 20;
    const noOfPlayers = 1;

    const g = new Game(nI, pI, noOfAgents, noOfPlayers);
    g.runUntil(1000);
    });

  socket.on("disconnect", () => console.log("user has disconnected"));
});




server.listen(port, () => console.log("Listenint on port " + port));

但是,我目前陷入了困境。我不知道如何暂停游戏以从前端获取数据并相应地使用它。到目前为止我所做的所有尝试都没有运气。我尝试使用 promise ,但这没有帮助,因为它没有暂停游戏流程的过程来等待用户输入。

最佳答案

有一个方便的软件包,名为 promise-do-whilst for Node (我确信围绕 Promises 组织的传统循环构造还有其他类似的类比。假设您有如下所示的顺序同步代码(其中 fn 是一个简单的同步函数):

  do {
    fn();
  } while( condition === true)

...将其重写为...

  var promiseDoWhilst = require('promise-do-whilst')
  var condition = true;
  function fn() { // for example
    condition = Math.random() > 0.5; // flip a coin
    return new Promise(function(r, j){
      setTimeout(function(){ r(); }, 1000);
    }); 
  }
  promiseDoWhilst(function() {
    return fn(); with fn converted to a promise-returning function
  }, function() {
    return condition === true;
  })

如果你有几个并行线程,你必须等待它们才能在循环中“取得进展”,你可以让它们全部发生在返回 Promise 的函数中,将所有返回的 Promise 放入一个数组 arr ,然后 fn return Promise.all(arr)Promise.all 之后的 .then 函数接收保留位置顺序的数组中原始 Promise 的解析值。希望这有帮助!

关于javascript - Node 应用程序通过暂停当前执行以交互方式从 react 前端获取用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54612676/

相关文章:

asp.net - 确保关闭浏览器窗口时处理某些事件

Javascript/Jquery 评论导致 IE 错误

node.js - Sails autoCreatedAt 和 autoUpdatedAt 插入比我当前时区向前 5 小时的日期时间

reactjs - 在 Material-Table 渲染属性中使用函数

reactjs - 在 React Native 中计算距离

javascript - jQuery 删除中继器中的输入值

javascript - 将表格单元格内容存储到 js 数组中

javascript - "trampoline"方法之间的区别

node.js - 使 Azure 移动应用程序接受 Node 后端的自定义身份验证

javascript - React Hooks (Rendering Arrays) - 持有被映射的 child 的引用的父组件与持有 child 状态的父组件