Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "newGame" event #528

Merged
merged 1 commit into from
Jul 24, 2023
Merged

Add "newGame" event #528

merged 1 commit into from
Jul 24, 2023

Conversation

utarwyn
Copy link
Owner

@utarwyn utarwyn commented Jul 23, 2023

Description

This PR creates a new event in the module called "newGame".
It will be triggered right before a game is launched and enables the developer to check and cancel its creation if needed.

Examples of use:

  • Bet system (take a virtual money before the game starts, and cancel game if player does not have the required amount)
  • Statistics system (number of games played per player, etc.)
  • Whatever you want 😎

This addition will be available for versions 3 and 4 of the module. 🚀

Code example

const toBet = 5;
const bank = { player_id_1: 6, player_id_2: 34 };

bot.on('newGame', data => {
    const realPlayers = data.players.filter(player => player.id !== 'AI');
    const playerNotEnoughMoney = realPlayers.find(player => bank[player.id] < toBet);
    if (playerNotEnoughMoney != null) {
        throw new Error(`${playerNotEnoughMoney.displayName} does not have enough money to play.`);
    }

    realPlayers.forEach(player => (bank[player.id] -= toBet));
});

Example of data object:

{
  "players": [
    {
      "id": "AI",
      "displayName": "the AI"
    },
    {
      "id": "player_id_1",
      "displayName": "Maxime — Utarwyn"
    }
  ]
}

Related Issues

Part of #258

@utarwyn utarwyn added the type: enhancement Features and feature requests. label Jul 23, 2023
@utarwyn utarwyn added this to the 3.x milestone Jul 23, 2023
@utarwyn utarwyn self-assigned this Jul 23, 2023
@sonarcloud
Copy link

sonarcloud bot commented Jul 23, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@utarwyn utarwyn merged commit a751316 into release/3.x Jul 24, 2023
5 checks passed
@utarwyn utarwyn deleted the feature/258-new-game-event branch July 24, 2023 17:48
@utarwyn utarwyn linked an issue Aug 5, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement Features and feature requests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Edit challenge message
1 participant