Cocos creator 3.x uses the official frame synchronization service to develop a real-time battle WeChat mini-game tutorial (1)

Real-time battle is the core of online games. The official service of WeChat Mini Games has its own frame synchronization service , as well as supporting room service and game matching functions. Currently, these functions are free. For individual developers who are distressed about server resources It is a good choice, but there are too many pitfalls in the documentation. I have been debugging for a long time before I can run through cocos creator, so write an article to record it.

The main process of this series of articles is: players click the online matching button to match, start the game after the number of people is met, the frame synchronization service starts to synchronize player operations, end the frame synchronization when the game end condition is met, and enter the settlement page.

The following link is the official description of the frame synchronization service: https://developers.weixin.qq.com/minigame/dev/guide/open-ability/lock-step.html, the article uses the method of creating rooms in the background to create matching rules, In other words, you must have your own server.
insert image description here
The general process is as follows:
1. Create a matching rule in the background (such as 1V1, or 2V2, etc., the official frame synchronization service currently supports a maximum of 5V5), save the matchId
2. The player clicks the match button to call his own back-end server to get The matchId generated in step 1, use this matchId to call the mini game service
3. When multiple players use the same matchId to match, the WeChat server will automatically create a room and put these players into the room, all (according to the setting of the matching rules) After the player confirms to start the game, the frame synchronization is officially started, and the frame synchronization service will send the game frame according to the cycle set in the matching rules

There is an official small example written in JS (https://github.com/wechat-miniprogram/minigame-lockstep-demo). The more valuable one is the GameServer script. The official packaged some methods for everyone. If it is developed with TS For small games, you can convert the script into TS (some syntax needs to be replaced), and import it into the project for direct use.

Let's start to detail the three steps in the process

1. Create a matching rule in the background (such as 1V1, or 2V2, etc., the official frame synchronization service currently supports a maximum of 5V5), save the matchId

Interface description (https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/gamematch/gamematch.createMatchRule.html)

What is matchId (matching rule)?

Developers who are in contact with it for the first time may wonder, what is this matching rule, what is its relationship with the room, is there any relationship between createMatchRule and createGameRoom? We can understand this matching rule as the matching mode of the game, such as 1V1, 2V2, 5V5. If your game supports these three matching modes, then only three matchIds are needed, and the room is only created based on this matchId.
matchId can be generated and saved in the database before the game goes online, and then you don’t need to manage it. Normally, you don’t need to change it or refresh it regularly

Method to get matchId

POST https://api.weixin.qq.com/wxa/business/gamematch/creatematchrule?access_token=ACCESS_TOKEN
parameter description:

    team_count = 2;//队伍的数量,一般是2队,混战的话就按队伍数量来
    team_member_count = 2;//每队的人数,如果是5V5就写5
    need_room_service_info = 1;//是否需要房间服务,1表示开启,自动匹配开始游戏的话建议开启,这样满足匹配条件后会自动创建房间,省去了很多步骤
    game_room_info={}//开启房间服务后需要设置的参数
    
    //game_room_info的参数
    game_tick = 33;//下发的周期,越小就越快
    start_percent = 100;//玩家确认开始的比例,100表示房间内所有玩家都确认开始后才会开启帧同步,如果设置50表示50%,即1V1的话只要有一个玩家确认开始,另一个玩家没确认,帧同步也会自动开启
    udp_reliability_strategy = 3;//不懂,默认值吧
    need_user_info = false;
    game_last_time = 180; //游戏时间,到达3分钟后自动结束游戏,不填的话默认20分钟,对局中设置了倒计时功能的话这边可以填,建议设置的长一点,这个参数设置了并不是说一定要打满3分钟,只是最大时间而已
    need_game_seed = false;

After sending to the WeChat server, you will receive the following results

{
    
    
    "errcode": 0,
    "errmsg":"ok",
    "match_id":"FD0PT4rKguEdK-L83RaJgdbchUCW8wjhSwgCku4CLQk"
}

It should be noted that getting the matchId is not directly usable, you need to call the setMatchIdOpenState interface to set the open_state state of the matchId to 1 to use it! ! ! (https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/gamematch/gamematch.setMatchIdOpenState.html)

After setting the state, save the matchId in the database, and when the player matches 2V2, return the matchId to the front end.

Then apply for the matchId of other matching modes such as 1V1, 5V5, etc., set the open_state to 1, record it in the database, and return whichever matchId the front end matches. At this point, the creation of the matching rule is completed.

The next chapter will show how to use this matchId to match in the mini-game developed by cocos creator 3.x.

Simple online example:
WeChat mini-game "Elite Tanker" 1V1 matchmaking

Guess you like

Origin blog.csdn.net/hangsky1990/article/details/131341834
Recommended