Matching frame synchronous Tankedaizhan

Before introducing matching system, first talk about the overall structure of the project.

Project has four main scenarios: start -> home -> choose -> game

file

start_scene: is the beginning of the game scene, this scene is mainly made micro-channel license, obtain a user's nickname, picture and other information. Get the user's openid via cloud function (do not understand the function of the cloud can refer to this article: micro-channel cloud development using the tutorial ). Then the user information and the user's global object Global openid are put inside for later use.

file

file

home_scene: After successful authorization, we jump to the scene.

file

After entering this scenario, you need to initialize the SDK MGOBE framework.

First, the parameter value (gameId, openId, secretKey, server, obtaining from the console) required for the SDK, and placed in the cache.

file

initListen function to do is initialize Listener, and create rooms instance, to add a room to listen. The main code is taken as follows:

file

After initialization is complete, you can choose 1V1,3V3. In fact, I am currently 3V3 setting is 2V2, not to measure the actual effect, but the calculation of the initial position of the tank when putting this situation into consideration, the frame sync again to go into detail.

choose_scene: After selecting 1V1 or 3V3, will enter the scene. In fact, the current situation, choose_scene and home_scene can be merged into one scene. Here, I explain why separate them.

Because, before I imagine that the freedom to choose what type of tank by the players in this scenario, after the selection, click manually by the player matches. At the same time, you can also match the success of the players, play against players all game show information, similar to the scene before the king of glory into the game. This is for the future, expand convenient features. The following example of this:

file

Now omitted these lower-priority sectors. Therefore, after entering choose_scene scene, directly initiate match.

file

Before initiating match, we need to create a matching rule in MGOBE console.

首先,在控制台左侧菜单【房间管理】,找到【匹配规则集】

file

新建规则集,填写规则集名称和规则集内容。名称随意填就好,规则集内容是一个json。它的结构是这样的:

file

我以当前坦克大战的规则集为例,解释一下各个字段的含义。

{
  "version": "V1.0",  //版本号,一般填V1.0就可以
  "teams": [
    {
      "name": "1v1",  // 名称1v1
      "maxPlayers": 1,  //每支队伍的最大玩家数量为1
      "minPlayers": 1,  //每支队伍的最小玩家数量为1
      "number": 2  //总共有两支队伍
    }
  ],
  "playerAttributes": [
    {
      "name": "lv",  //等级,匹配时需要传入此参数
      "type": "number"  //属性的类型,目前暂时仅支持number
    }
  ],
  "rules": [
    {
      "type": "deviation",  //匹配规则的类型:当前为误差规则
      "expression": "teams[i].players.lv", //依据玩家等级组队
      "value": 5, //表示所有玩家之间等级相差不能超过5
      "waitTimeSteps": [
        {
          "waitTimeSeconds": 10, //等待10秒若匹配不上,则放宽规则
          "value": 10 //把等级差放宽为10
        }
      ]
    }
  ],
  "timeout": 15 //全局匹配超时时间
}

更复杂的匹配规则,可以参考官方文档:
https://cloud.tencent.com/document/product/1038/34952

规则集创建完之后,就需要创建匹配code了。左侧菜单【房间管理】,找到【在线匹配】。右侧新建匹配,填写描述信息,匹配规则选择刚才我们创建的那条匹配规则集。

file

点击确定之后,就会分配一个匹配code,这个code就是调用发起匹配API需要传入的一个参数。

现在,可以调用room.matchPlayers方法发起匹配了。匹配超时之后,会弹出超时的对话框,可以选择重新匹配,或者取消匹配退出匹配队列。取消匹配,会返回到home_scene场景。(超时对话框是我从cocos论坛找的UI,结合自己需要改的)

file

至此,整个匹配的主要流程就讲完了。查看完整代码,在公众号回复关键字“帧同步”即可获取。

说明一下:

  1. 目前MGOBE匹配支持两种模式,房间匹配和玩家自由匹配。房间匹配采用的是类似于棋牌游戏,一个玩家开房,然后邀请其他玩家进来游戏,或者其他玩家通过房间匹配进来。所有玩家准备之后,开始进行游戏。而玩家自由匹配,是指所有玩家进行的单人匹配。因此,目前框架不支持类似于王者荣耀那样的组队匹配。之前,有联系过他们的产品,说以后会上线此功能。

  2. 帧同步,所有逻辑都在客户端完成,因此不需要在服务端写额外的代码。这样一来,有好处也有坏处。好处就是我们不用关心服务端的实现,坏处就是灵活性不够强,很多问题,只能通过在客户端想办法来实现。另外,此框架有个需要注意的地方就是,在这种情况下,也需要下载实时服务器的代码(即服务端),然后发布到实时服务器。操作流程可查看官方文档。

  3. All API framework can be found corresponding to the interpretation and use of the MGOBE.t.ds file. So, look at this document will make you more familiar with the framework.

  4. There are a lot of code in the code commented out. Some in order to keep the code before the impact of the current logic, some in the debugging process, in order to compare various implementations and left. Can be ignored.

  5. Code can be downloaded directly cocoscreator 2.0.10 run up temporarily with my server parameters can be.

Guess you like

Origin www.cnblogs.com/starry-skys/p/11980419.html