ERC-7566 Multiplayer game communication

Draft · Other · created 2023-11-28

A standard that lets a single smart contract gather players into rooms and process actions via messages, enabling the creation of on-chain multiplayer games without a central server.

Why it is needed

Common multiplayer games usually run on centralized servers. Players have no way to know if data is being manipulated or if cheating is occurring within the server. The owner of the game server can match players arbitrarily, alter scores and levels, or even shut down or halt the game at will. Conversely, if all player actions happen on chain, every message emitted from the chain becomes evidence of the player's instructions and actions, guaranteeing fairness of the game. However, without a common structure for this, each developer has to design their own way of grouping players and changing state from scratch, and it also becomes difficult to query or verify game data externally. This standard reduces these problems by providing a basic framework that groups players into rooms and handles game logic through messages.

What to implement

In depth

Two core components: rooms and messages

The principle of this standard is to change the state of different groups of players using the same game logic. A Room is a container that holds players, used for matching players and looking up connected players. A game can only proceed after players have entered a room. A Message is an action between players, used to perform game actions and change a player's state within a room. Vertical scaling to accommodate more players is achieved by adding rooms, and horizontal scaling to implement on-chain game logic is achieved through custom messages.

Unique ID assignment rules

The interface has three ID assignment obligations. A room entity created by createRoom MUST be assigned a unique ID, a member created by joinRoom MUST also be assigned a unique ID, and a message created by sendMessage MUST also be assigned a unique ID. Rooms have a hierarchical data structure, and each member receives a new ID per room. This structure is meant to make it easier for developers to manage player state. A member's ID within a room can be found from an address using getMemberId, and the list of rooms a member has entered can be found using getRoomIds.

sendMessage as a state transition function

Game state refers to changes in a player's data within the game, and sendMessage actually serves as the state transition function. Developers define game logic inside sendMessage and process a member's state based on the message content. The message content _message is encoded with abi.encode, and an array of the content's data types is sent together as _messageTypes. The list of types is defined in the Types.sol library, which holds the Solidity types used by the interface as an enum. Since _to is an array of member IDs, one-to-many sending to multiple members at once is possible. The proposal is flexible, so depending on the game logic, data can be defined either internal to the room or global, outside the room.

Reasons for room-based design and initialization

Because rooms are independent of each other and a player receives a new ID each time they enter a room, a single game session, a single game quest, or a single game event can each be modeled as one room. Player data initialization can be done in either createRoom or joinRoom.

How to handle departed players

A method of recording a member's last sendMessage timestamp using block.timestamp or block.number can be used. Additionally, a message type can be added to sendMessage so that other players can use it to report that a given member is offline and apply a penalty to that member.

Suitable game genres

This standard is a multiplayer on-chain game standard, not a multiplayer real-time game standard. The possible game genres depend on the network the contract is deployed to. Some layer 2 networks process blocks very quickly, making more near-real-time games possible. In general, it is better suited to strategy, trading card, turn-based, chess, sandbox, and settlement-type games.

Note on proposal status

This proposal is still in Draft status. Its content may change, and the Security Considerations section of the source text has not been filled in and merely states that discussion is still needed. Before implementing, it is advisable to check the latest source text and the reference implementation (MultiplayerOnchainGame.sol).

Original ERC · ← ERCs