Game Lobby SC template - MIP-3

Game lobby SC

Overview

This smart contract covers the functionality of a basic game lobby under a set of rules imposed by the owner.

A user can create a new wager game with the following specifications:

  • waiting time

  • minimum number of players

  • maximum number of players

  • wager

and has to pay a fee for each new game.

The owner can:

  • enable/disable the contract for maintenance

  • set the game starting fee amount

  • set the token id for the currency of the SC (used for game starting fee, wager and reward)

  • send rewards or return the wager to the users who participated in a specific game

Each player has to pay the wager amount set by the game creator in the token id set by the owner in order to join the game.

The game is considered invalid until the minimum number of players have joined the game and if the waiting time has passed, no more players can join.

The SC does not have any logic for calculating the winner, so it expects input from the owner with the winners’ addresses and the percentage (*100) of the total reward (sum of wagers) won by each.

The game:

  • If the game is invalid, the wager amount will be returned to the players that have joined the game and the game starting fee will be returned to the creator

  • If the game is valid, but no winners are provided, such in the case of a tie/draw, the contract will send back the wager amount paid by every player who joined

  • If the game is valid and winners are provided, the SC will send the rewards to them, based on the input of the owner.

Endpoints

createGame

#[payable("*")]
#[endpoint(createGame)]
fn create_game(
     &self,
     waiting_time: u64,
     number_of_players_min: u64,
     number_of_players_max: u64,
     wager: BigUint,

Creates a game with a new id using the parameters sent by the caller if the payment is right (payment should be equal to game starting fee).

The SC calculates min and max from the parameters so you don’t have to worry if you placed them wrong.

joinGame

#[payable("*")]
#[endpoint(joinGame)]
fn join_game(&self, game_id: u64)

Caller can join a game with an existing game id if the payment is right (payment should be equal to wager).

sendReward

#[only_owner]
#[endpoint(sendReward)]
fn send_reward(
     &self,
     game_id: u64,
     winners: OptionalValue<MultiValueEncoded<(ManagedAddress, u64)>>,

Owner can send the rewards for the players through this endpoint.

winners:

  • the address of the winner

  • the percentage of the reward pool the winner is entitled to * 100, (e.g: for 12.53%, the owner should send 1253 as parameter)

claimBackWager

#[endpoint(claimBackWager)]
fn claim_back_wager(&self, game_id: u64)

Caller can manually claim back the wager if the game is invalid and the waiting time has passed (in case the owner has not already sent the wager through the sendReward endpoint)

Full implementation can be found here: https://github.com/multiversx/mx-contracts-rs/tree/main/contracts/mvx-game-sc

2 Likes

Why does a game lobby contract need to be a multiversx improvement proposal? Sounds more like something that individual projects would want to use and not something that should be part of the blockchain? How/Why/What game could use a universal game lobby shared by everyone?

MIPs can be a set of Smart Contract Standards as well. Like ERC-20,721 and so forth. By having common contracts, with well defined endpoints and behaviours, anyone building can use these modules, anyone integrating can make those things happen faster.

MIPs are not only protocol/VM related features.

1 Like

Agora is a place, where we can discuss such standards/contracts and make it even better.

4 Likes