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

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料