最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

在socket.io中完成游戏后,从房间里获得所有用户的分数

IT培训 admin 1浏览 0评论

在socket.io中完成游戏后,从房间里获得所有用户的分数

我正在开发一个多人游戏,其中将有四个房间,每个房间将有多个客户在其中玩游戏。完成游戏后,我想将所有用户的分数发送到服务器,将其存储在数组中,进行比较并想要返回获胜者的名字。那么,在完成游戏后,如何从房间中获得所有用户的分数。

var delayInMilliseconds = 2000;
var hit = 0;

$('#hit').click(function() 
{
  hit++;
});

var modal = document.getElementById('myModal');
var btn = document.getElementsByClassName("common");
var span = document.getElementsByClassName("close")[0];

setTimeout(function() 
{
    var seconds_left = 15;
    var interval = setInterval(function() 
    {
        document.getElementById('timer').innerHTML = --seconds_left;
        if (seconds_left <= 0)
        {
            // document.getElementById('timer').innerHTML = "Time's up!";
            clearInterval(interval);
          console.log(hit);
          $('#hit').attr("disabled", true);
          $('#score').html(hit);
          modal.style.display = "block";

        }
    }, 1000);
}, delayInMilliseconds);

以上是游戏的客户端代码,游戏将在15秒后完成,所以我的想法是我可以在if(seconds_left <= 0)发送数据,但我怎么能从房间里得到所有客户的分数。

回答如下:

首先,你必须以某种方式识别用户。让我们将它们保存到具有关键userName和值得分的称为用户的对象。就像那样:var users = { user1: 12, user2: 9 };。 我们必须将此数据发送到服务器。我们可以像官方socket.io documentation那样做

<script>
  var socket = io('http://localhost');
  socket.on('connection', function () {
    console.log('Socket connected');
  });

  // here is your code to handle users actions. 
  // When the game is finished, you do it like this
  socket.emit('game finished', users);
</script>

在服务器端,您可以使用要解析的数据监听game finished事件。 此外,您可以使用ajax向服务器发出处理分数数据的请求,您可以阅读有关jQuery implementation或native JS的信息。

在socket.io中完成游戏后,从房间里获得所有用户的分数

我正在开发一个多人游戏,其中将有四个房间,每个房间将有多个客户在其中玩游戏。完成游戏后,我想将所有用户的分数发送到服务器,将其存储在数组中,进行比较并想要返回获胜者的名字。那么,在完成游戏后,如何从房间中获得所有用户的分数。

var delayInMilliseconds = 2000;
var hit = 0;

$('#hit').click(function() 
{
  hit++;
});

var modal = document.getElementById('myModal');
var btn = document.getElementsByClassName("common");
var span = document.getElementsByClassName("close")[0];

setTimeout(function() 
{
    var seconds_left = 15;
    var interval = setInterval(function() 
    {
        document.getElementById('timer').innerHTML = --seconds_left;
        if (seconds_left <= 0)
        {
            // document.getElementById('timer').innerHTML = "Time's up!";
            clearInterval(interval);
          console.log(hit);
          $('#hit').attr("disabled", true);
          $('#score').html(hit);
          modal.style.display = "block";

        }
    }, 1000);
}, delayInMilliseconds);

以上是游戏的客户端代码,游戏将在15秒后完成,所以我的想法是我可以在if(seconds_left <= 0)发送数据,但我怎么能从房间里得到所有客户的分数。

回答如下:

首先,你必须以某种方式识别用户。让我们将它们保存到具有关键userName和值得分的称为用户的对象。就像那样:var users = { user1: 12, user2: 9 };。 我们必须将此数据发送到服务器。我们可以像官方socket.io documentation那样做

<script>
  var socket = io('http://localhost');
  socket.on('connection', function () {
    console.log('Socket connected');
  });

  // here is your code to handle users actions. 
  // When the game is finished, you do it like this
  socket.emit('game finished', users);
</script>

在服务器端,您可以使用要解析的数据监听game finished事件。 此外,您可以使用ajax向服务器发出处理分数数据的请求,您可以阅读有关jQuery implementation或native JS的信息。

发布评论

评论列表 (0)

  1. 暂无评论