dongqian3750 2015-05-12 22:03
浏览 47

PHP如何连接两个玩家

I'm making a website. and one of the features i want it to have is to have a simple game that connects 2 players together. my problem is I don't know how to make it so both player are in the same "room" because on room holds only 2 players. On way i approached this is once one player joined, he gets a "wait for next player" message and waits while sending to the database that one player have joined. how do i make it keep checking for the next 3 minutes if the next player joined?

UPDATE

First here is the code so far:

<html>
<title>SiteName (test)</title>
<head>
<?php
$servername = "localhost";
$u

sername = 
$password = 
$dbname = 

try 
{
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $conn->prepare('SELECT * FROM game');
$stmt->execute(array('gameID' => $gameID));

while($row = $stmt->fetch()) {
    print_r($row);
    echo "<br />
";

 }
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}
?>
<button onclick="myFunction()" id="w">Look for Game</button><br>
<a href="https://www.google.com" id="lbl" class="button"></a>
<script>
function myFunction() {
var elem = document.getElementById("w").innerHTML = "Wait";
var counter = 10;
var label= document.getElementById("lbl");
var counter = 10;
var clabel= document.createElement("p");
clabel.innerHTML = "You can download the file in 10 seconds.";
var id;

label.parentNode.replaceChild(clabel, label);

id = setInterval(function() {
    counter--;
    if(counter < 0) {
        clabel.parentNode.replaceChild(label, clabel);
        clearInterval(id);

    } else {
        clabel.innerHTML = "You can download the file in " + counter.toString() + " seconds.";
    }
}, 1000);
  }
 </script>

<?php 
$conn = null;
?>
</body>
</html>

Am trying to make it so that if the first player joined, he will be waiting (i have it for 10 seconds here as a test) until the other joins. the way am trying to do it is to have a field in the database will know if the a player is in that page and await the next player. I read something about long polling but not sure how to implement it for my case.

Any feed back would be helpful, Thank you

  • 写回答

2条回答 默认 最新

  • duandi1636 2015-05-12 22:12
    关注

    first you will want javascript or some client side code to handle this. as php will execute on the server side then display to the user. if you use ajax with javascript you can get the client side and server side to work together.

    you will want to use a while loop, in this loop you will set a timeout.

    in the while loop you can call the ajax script you want untill you get your result you want. I'm assuming you plan on making this a turn by turn game for the players. you will want a table that sets "true" to if player 1 or player 2 are in the game. if both are turn then the game begins.

    Hope this logic helps

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大