节点应用程序通过暂停当前执行以交互方式从响应前端获取用户输入 [英] Node application interactively get user input from react front end by pausing the current execution

查看:81
本文介绍了节点应用程序通过暂停当前执行以交互方式从响应前端获取用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I converted an old game which was written in python to javascript (node). The game simply runs in a while loop until a certain amount of iterations complete.

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并在用户每次按下按键时运行游戏的服务器.

On some occasions, the game pauses in order to get user input which is required and affects the outcome of the game. In my javascript implementation, I used readline-sync to pause the game and get user input through command prompt. Now I built a react front end application in order to serve the game in a browser with UI, and I built a server using express to handle the API and run the game whenever user presses start.

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,但这没有帮助,因为它没有暂停游戏流程以等待用户输入.

However, I am currently stuck at this point. I am not sure how I can pause the game to get the data from the front-end and use it accordingly. All tries I made until now have had no luck. I tried using promises but that didn't help because it didn't paused the process of game flow to wait for user input.

推荐答案

有一个名为

There's a convenient package called promise-do-whilst for Node (I'm sure there are other similar analogies for traditional loop constructs organized around Promises. Lets say you have sequential synchronous code that looks like this (where fn is a simple synchronous function):

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

...将其改写为...

... rewrite this as ...

  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的解析值.希望这会有所帮助!

If you have a couple of parallel threads that you have to wait for to 'make progress' in your loop, you can make them all happen in functions that return promises, put all those returned promises into an array arr, and then have fn return Promise.all(arr). A .then function after a Promise.all receives the resolved values of the original promises in an array preserving the positional order. Hope this helps!

这篇关于节点应用程序通过暂停当前执行以交互方式从响应前端获取用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆