2人转换游戏逻辑


2 player turnbased game logic

JS noob此处。。我正在尝试开发一款基于裸机的多人旋转卡牌游戏。根据我目前的编码知识,我可以根据以下伪代码实现。。

我有三个文件,index.html,multiplayer.php,gameStatus.txt.

  index.html:
      player one clicks ready button, which sends 1 to multiplayer.php.  
      player two clicks ready button, which sends 1 to multiplayer.php. 
      every 1 sec, repeater() checks if "twoPlayerFound" is returned from multiplayer.php.
          if "twoPlayerFound" is returned, 
              then console.log("two players are online, game begins..")
          else 
              console.log("waiting for another player to join..")
  multiplayer.php:
      when 1 is posted from index.html, php checks gameStatus.txt
          if gameStatus.txt has 0
              then overwrite gameStatus.txt 0 with 1;
          else if gameStatus.txt has 1
              then overwrite gameStatus.txt 1 with 2;
              send to index.html "twoPlayerFound"
  gameStatus.txt:
      either has 0,1, or 2

Q1.如果玩家不再在线,我如何自动将gameStatus.txt重置为0
Q2.使用这种实现会有什么问题?Q3.这是多人游戏设置的正确思考方式吗

谢谢。。

  • Q3-我不认为这是实现多人游戏设置的好方法,为什么?检查Q2的答案
  • Q2-您的文件无法处理游戏全面实现过程中可能出现的所有复杂场景

例如:"如果玩家不再在线,我如何自动将gameStatus.txt重置为0"(是的,这是你的Q1),为了解决这个问题,你应该为每个玩家保留一个时间戳,并在玩家每次联系服务器时更新时间戳,保持一个循环来关注这些时间戳,消除最近没有联系过你的玩家。要实现这一点,你必须保留一些ID来识别你的玩家等等…

此外,考虑一下如何扩展这种设置,例如:两个人以上同时连接到你的服务器,人们试图与他们喜欢的人配对玩。

你的游戏也会感到网络饥饿。您正在尝试做的叫做AJAX轮询。

我的建议?对于客户端,使用HTML5中的WebSockets,也可以尝试对socket.io进行一些研究。大多数人会推荐nodejs作为后端,但你也可以使用PHP。您还应该选择一种方法来为用户和游戏状态实现会话(这种选择在很大程度上取决于您的后端技术堆栈)。