13. MMO Game Server Application Protocol
Tags: zinx, go
Published: 2023-08-21 08:03:29

### [Zinx]

[<1.Building Basic Services with Zinx Framework>](https://dev.to/aceld/1building-basic-services-with-zinx-framework-296e)
[<2. Zinx-V0.2 Simple Connection Encapsulation and Binding with Business>](https://dev.to/aceld/2-zinx-v02-simple-connection-encapsulation-and-binding-with-business-4p9g)
[<3.Design and Implementation of the Zinx Framework's Routing Module>](https://dev.to/aceld/3design-and-implementation-of-the-zinx-frameworks-routing-module-583n)
[<4.Zinx Global Configuration>](https://dev.to/aceld/4zinx-global-configuration-3jam)
[<5.Zinx Message Encapsulation Module Design and Implementation>](https://dev.to/aceld/5zinx-message-encapsulation-module-design-and-implementation-3fmc)
[<6.Design and Implementation of Zinx Multi-Router Mode>](https://dev.to/aceld/6design-and-implementation-of-zinx-multi-router-mode-19nn)
[<7. Building Zinx's Read-Write Separation Model>](https://dev.to/aceld/7-building-zinxs-read-write-separation-model-ihj)
[<8.Zinx Message Queue and Task Worker Pool Design and Implementation>](https://dev.to/aceld/8zinx-message-queue-and-task-worker-pool-design-and-implementation-12gl)
[<9. Zinx Connection Management and Property Setting>](https://dev.to/aceld/9-zinx-connection-management-and-property-setting-28ha)

### [Zinx Application - MMO Game Case Study]

[<10. Application Case Study using the Zinx Framework>](https://dev.to/aceld/10-application-case-study-using-the-zinx-framework-3ich)
[<11. MMO Online Game AOI Algorithm>](https://dev.to/aceld/11-mmo-online-game-aoi-algorithm-l7d)
[<12.Data Transmission Protocol: Protocol Buffers>](https://dev.to/aceld/12data-transmission-protocol-protocol-buffers-1jek)
[<13. MMO Game Server Application Protocol>](https://dev.to/aceld/13-mmo-game-server-application-protocol-5adl)
[<14. Building the Project and User Login>](https://dev.to/aceld/14-building-the-project-and-user-login-b7p)
[<15. World Chat System Implementation>](https://dev.to/aceld/157-world-chat-system-implementation-18li)
[<16. Online Location Information Synchronization>](https://dev.to/aceld/16-online-location-information-synchronization-3cp1)
[<17. Moving position and non-crossing grid AOI broadcasting>](https://dev.to/aceld/17-moving-position-and-non-crossing-grid-aoi-broadcasting-4nb9)
[<18. Player Logout>](https://dev.to/aceld/18-player-logout-3eln)
[<19. Movement and AOI Broadcast Across Grids>](https://dev.to/aceld/19-movement-and-aoi-broadcast-across-grids-46fe)

---
### MMO-GAME Source Code

https://github.com/aceld/zinx/tree/master/zinx_app_demo/mmo_game

---

In the context of the MMO game demo application, the client portion acts as a server, while the upcoming content in this chapter pertains to the server portion of the game. Prior to development, it is necessary to define the protocol between the two parties.

## 13.1 Protocol Definition

Based on the requirements of the client application, it is understood that the client will transmit the following types of application protocol game data to the server, as shown in Table 13-1.

> Table 13-1

| MsgID | Client      | Server      | Description |
| ----- | ----------- | ----------- | ---------------------------------------------------------- |
| 1     | -           | SyncPid     | Synchronize the player's login ID (used for player ID).|
| 2     | Talk        | -           | World chat.                                                   |
| 3     | MovePackege | -           | Movement.                                                       |
| 200   | -           | BroadCast   | Broadcast messages (Tp 1: world chat, 2: coordinates synchronization (spawn point sync), 3: action, 4: updated coordinates after movement). |
| 201   | -           | SyncPid     | Broadcast messages for disconnect/aoi disappearance.|
| 202   | -           | SyncPlayers | Synchronize the position information of nearby players (including oneself).   |

### 13.1.1 Table Explanation

The meaning of each column in the table is as follows:

**MsgID**: The Message ID of the current communication protocol, synonymous with the MsgID defined in Zinx.
**Client**: The name of the protocol for the client, and the current message is initiated by the client.
**Server**: The name of the protocol for the server, and the current message is initiated by the server.
**Description**: The description of the current protocol.


### 13.1.2 Protocol Meanings
In Table 13-1, there are a total of 6 protocols, and their meanings are as follows:

**SyncPID Protocol**: Initiated by the server to the client, Message ID 1. It informs the client of other online users that the current player has come online.

**Talk Protocol**: Initiated by the client to the server, Message ID 2. It represents the client-initiated world chat message, informing the server to broadcast the message.

**Position Protocol**: Initiated by the client to the server, Message ID 3. It carries the coordinates of the client player's movement.

**BroadCast Protocol**: Initiated by the server to the client, Message ID 200. It represents server-initiated broadcast messages to all online player clients. The Tp variable indicates different types of business.

**SyncPID Protocol**: Initiated by the server to the client, Message ID 201. It represents server-initiated broadcast messages for player disconnections or changing viewpoints.

**SyncPlayers Protocol**: Initiated by the server to the client, Message ID 202. It synchronizes the positions of surrounding players, including the player's own position, to other players.

## 13.2 Proto3 Protocol Definitions
This section will define the specific data protocol format for Protobuf based on the protocol definition format provided in Table 13-1.

### 13.2.1 Protocol One (MsgID=1)

The SyncPid protocol is used to synchronize the player's unique ID generated by the server to the client after the player logs in. This ID serves to identify the player.
The Proto protocol definition is as follows:

```proto
message SyncPid {
	int32 Pid=1;
}
```
Relevant Parameters are as Follows:
(1) Pid: Player ID.

### 13.2.2 Protocol Two (MsgID=2)

Talk Protocol, world chat, this message is initiated by the Client client.

Proto protocol definition is as follows:

```proto
message Talk {
	string Content = 1;
}
```
Relevant parameters are as follows:
(1) Content: Chat message.

### 13.2.3 Protocol Three (MsgID=3)

Position Protocol, coordinates of movement, message about the movement coordinates of the client player, initiated by the Client.

Proto protocol definition is as follows:

```proto
message Position {
	float X = 1;
	float Y = 2;
	float Z = 3;
	float V = 4;
}
```
Relevant parameters are as follows:
(1) X: X-axis coordinate in the 3D map.
(2) Y: Height coordinate in the 3D map, not the Y-axis.
(3) Z: Y-axis coordinate in the 3D map.
(4) V: Angle of rotation for objects or tasks (0~360°).

### 13.2.4 Protocol Four (MsgID=200)

BroadCast Protocol, broadcast message, the server broadcasts the message to all online player clients, and the Tp variable indicates different types of businesses.

Proto protocol definition is as follows:
```proto
message BroadCast {
	int32 Pid = 1;
	int32 Tp = 2;
	oneof Data {
        string Content = 3;
        Position P = 4;
	    int32 ActionData = 5;
    }
}
```

Relevant parameters are as follows:

(1) Pid: Player ID.
(2) Tp: Type of broadcast message. 1 for world chat, 2 for coordinates, 3 for actions, and 4 for coordinates update after movement.

(3) Data: Specific message format for transmission. Data is decorated with the "oneof" keyword. Depending on Tp, Data will have different specific data types. Content is for Tp=1, broadcasting world chat data; Position is for Tp=2 or 4, broadcasting coordinate data; ActionData is for Tp=3, broadcasting specific player action data.

### 13.2.5 Protocol Five (MsgID=201)

SyncPid Protocol, broadcast message, a player goes offline or a player's visibility disappears from the AOI region, initiated by the Server.

Proto protocol definition is as follows:
```proto
message SyncPid {
	int32 Pid = 1;
}
```
Relevant parameters are as follows:
(1) Pid: Player ID.

### 13.2.6 Protocol Six (MsgID=202)

SyncPlayers Protocol, synchronizes the positions of nearby players, including the current player, to other players. Initiated by the Server.

Proto protocol definition is as follows:

```proto
message SyncPlayers {
	repeated Player ps = 1;
}

message Player {
	int32 Pid = 1;
	Position P = 2;
}
```
Relevant parameters are as follows:
(1) Player: Collection of players to be synchronized, decorated with the "repeated" keyword.
(2) Pid: Player ID.
(3) Position: Position information.

---
### MMO-GAME Source Code
https://github.com/aceld/zinx/tree/master/zinx_app_demo/mmo_game

---
### [Zinx]

[<1.Building Basic Services with Zinx Framework>](https://dev.to/aceld/1building-basic-services-with-zinx-framework-296e)
[<2. Zinx-V0.2 Simple Connection Encapsulation and Binding with Business>](https://dev.to/aceld/2-zinx-v02-simple-connection-encapsulation-and-binding-with-business-4p9g)
[<3.Design and Implementation of the Zinx Framework's Routing Module>](https://dev.to/aceld/3design-and-implementation-of-the-zinx-frameworks-routing-module-583n)
[<4.Zinx Global Configuration>](https://dev.to/aceld/4zinx-global-configuration-3jam)
[<5.Zinx Message Encapsulation Module Design and Implementation>](https://dev.to/aceld/5zinx-message-encapsulation-module-design-and-implementation-3fmc)
[<6.Design and Implementation of Zinx Multi-Router Mode>](https://dev.to/aceld/6design-and-implementation-of-zinx-multi-router-mode-19nn)
[<7. Building Zinx's Read-Write Separation Model>](https://dev.to/aceld/7-building-zinxs-read-write-separation-model-ihj)
[<8.Zinx Message Queue and Task Worker Pool Design and Implementation>](https://dev.to/aceld/8zinx-message-queue-and-task-worker-pool-design-and-implementation-12gl)
[<9. Zinx Connection Management and Property Setting>](https://dev.to/aceld/9-zinx-connection-management-and-property-setting-28ha)

### [Zinx Application - MMO Game Case Study]

[<10. Application Case Study using the Zinx Framework>](https://dev.to/aceld/10-application-case-study-using-the-zinx-framework-3ich)
[<11. MMO Online Game AOI Algorithm>](https://dev.to/aceld/11-mmo-online-game-aoi-algorithm-l7d)
[<12.Data Transmission Protocol: Protocol Buffers>](https://dev.to/aceld/12data-transmission-protocol-protocol-buffers-1jek)
[<13. MMO Game Server Application Protocol>](https://dev.to/aceld/13-mmo-game-server-application-protocol-5adl)
[<14. Building the Project and User Login>](https://dev.to/aceld/14-building-the-project-and-user-login-b7p)
[<15. World Chat System Implementation>](https://dev.to/aceld/157-world-chat-system-implementation-18li)
[<16. Online Location Information Synchronization>](https://dev.to/aceld/16-online-location-information-synchronization-3cp1)
[<17. Moving position and non-crossing grid AOI broadcasting>](https://dev.to/aceld/17-moving-position-and-non-crossing-grid-aoi-broadcasting-4nb9)
[<18. Player Logout>](https://dev.to/aceld/18-player-logout-3eln)
[<19. Movement and AOI Broadcast Across Grids>](https://dev.to/aceld/19-movement-and-aoi-broadcast-across-grids-46fe)

---
Author:
discord: https://discord.gg/xQ8Xxfyfcz
zinx:   https://github.com/aceld/zinx
github: https://github.com/aceld
aceld's home: https://yuque.com/aceld