• Guides
  • Api Documentation
Show / Hide Table of Contents
  • Discord.Addons.MpGame
    • CurrentlyPlaying
    • GameBase<TPlayer>
    • IMpGameData
    • MpGameModuleBase<TService, TGame, TPlayer>
    • MpGameService<TGame, TPlayer>
    • MpGameService<TGame, TPlayer>.MpGameData
    • Player
  • Discord.Addons.MpGame.Collections
    • CircularLinkedList<T>
    • Hand<T>
    • IWrapper<T>
    • Node<T>
    • Pile<T>
    • WrappingPile<T, TWrapper>
  • Discord.Addons.Preconditions
    • Measure
    • MinimumOnlineUsersAttribute
    • RatelimitAttribute
    • RatelimitFlags
    • RequireLowerHierarchyAttribute
    • RequireRoleAttribute
    • UserMustBeInVoiceAttribute

Class MpGameModuleBase<TService, TGame, TPlayer>

Base class to manage a game between Discord users.


Inheritance
Object
ModuleBase<SocketCommandContext>
MpGameModuleBase<TService, TGame, TPlayer>
Inherited Members
Discord.Commands.ModuleBase<Discord.Commands.SocketCommandContext>.ReplyAsync(System.String, System.Boolean, Discord.Embed, Discord.RequestOptions)
ModuleBase<SocketCommandContext>.AfterExecute(CommandInfo)
ModuleBase<SocketCommandContext>.Context
Namespace: Discord.Addons.MpGame
Assembly: Discord.Addons.MpGame.dll
Syntax
public abstract class MpGameModuleBase<TService, TGame, TPlayer> : ModuleBase<SocketCommandContext>
    where TService : MpGameService<TGame, TPlayer>
    where TGame    : GameBase<TPlayer>
    where TPlayer  : Player
Type Parameters
Name Description
TService

The type of the service managing longer lived objects.

TGame

The type of game to manage.

TPlayer

The type of the Player object.

Constructors

| Improve this Doc View Source

MpGameModuleBase(TService)

Initializes the MpGameModuleBase<TService, TGame, TPlayer> base class.

Declaration
protected MpGameModuleBase(TService gameService)
Parameters
Type Name Description
TService gameService

The TService instance.

Properties

| Improve this Doc View Source

Game

The instance of the game being played (if active).

Declaration
protected TGame Game { get; }
Property Value
Type Description
TGame
| Improve this Doc View Source

GameInProgress

Determines if a game in the current channel is in progress or not.

Declaration
protected CurrentlyPlaying GameInProgress { get; }
Property Value
Type Description
CurrentlyPlaying
| Improve this Doc View Source

GameService

The GameService instance.

Declaration
protected TService GameService { get; }
Property Value
Type Description
TService
| Improve this Doc View Source

JoinedUsers

The list of users ready to play.

Declaration
protected IReadOnlyCollection<IUser> JoinedUsers { get; }
Property Value
Type Description
IReadOnlyCollection<IUser>
Remarks
note

This is an immutable snapshot; it is not updated until the next command invocation. Alternatively, you can manually call GetGameData(ICommandContext) and see its JoinedUsers property to obtain a refreshed snapshot right away.

| Improve this Doc View Source

OpenToJoin

Determines if a game in the current channel is open to join or not.

Declaration
protected bool OpenToJoin { get; }
Property Value
Type Description
Boolean
| Improve this Doc View Source

Player

The player object that wraps the user executing this command (if a game is active AND the user is a player in that game).

Declaration
protected TPlayer Player { get; }
Property Value
Type Description
TPlayer
| Improve this Doc View Source

RegisterPlayerTypeReader

Override this to return false if you don't want to register a type reader for the TPlayer type.

Declaration
protected virtual bool RegisterPlayerTypeReader { get; }
Property Value
Type Description
Boolean

Methods

| Improve this Doc View Source

BeforeExecute(CommandInfo)

Initialize fields whose values come from the TService's Dictionaries.

Declaration
protected override void BeforeExecute(CommandInfo command)
Parameters
Type Name Description
CommandInfo command
Overrides
Discord.Commands.ModuleBase<Discord.Commands.SocketCommandContext>.BeforeExecute(Discord.Commands.CommandInfo)
| Improve this Doc View Source

CancelGameCmd()

Command to cancel a game before it started.

Declaration
public abstract Task CancelGameCmd()
Returns
Type Description
Task
Examples
  [Command("cancel")]
  public override async Task CancelGameCmd()
  {
    if (Game != null)
    {
      await ReplyAsync("Cannot cancel a game already in progress.").ConfigureAwait(false);
    }
    else if (!OpenToJoin)
    {
      await ReplyAsync("No game open to cancel.").ConfigureAwait(false);
    }
    else
    {
      if (await GameService.CancelGameAsync(Context.Channel))
      {
        await ReplyAsync("Game was canceled.").ConfigureAwait(false);
      }
    }
  }
| Improve this Doc View Source

EndGameCmd()

Command to end a game in progress early.

Declaration
public abstract Task EndGameCmd()
Returns
Type Description
Task
Examples
  [Command("end")] //Should be restricted to mods/admins to prevent abuse
  public override Task EndGameCmd()
    => (Game != null)
      ? Game.EndGame("Game ended early by moderator.")
      : GameInProgress == CurrentlyPlaying.DifferentGame
        ? ReplyAsync("Different game in progress.")
        : ReplyAsync("No game in progress.");
| Improve this Doc View Source

GameStateCmd()

Command to display the current state of the game.

Declaration
public abstract Task GameStateCmd()
Returns
Type Description
Task
Examples
  [Command("state")]
  public override Task GameStateCmd()
    => (Game != null)
      ? ReplyAsync(Game.GetGameState())
      // Alternatively: ReplyAsync("", embed: Game.GetGameStateEmbed())
      : (GameInProgress == CurrentlyPlaying.DifferentGame)
        ? ReplyAsync("Different game in progress.")
        : ReplyAsync("No game in progress.");
| Improve this Doc View Source

JoinGameCmd()

Command to join a game that is open.

Declaration
public abstract Task JoinGameCmd()
Returns
Type Description
Task
Examples
  [Command("join")]
  public override async Task JoinGameCmd()
  {
    if (Game != null)
    {
      await ReplyAsync("Cannot join a game already in progress.").ConfigureAwait(false);
    }
    else if (!OpenToJoin)
    {
      await ReplyAsync("No game open to join.").ConfigureAwait(false);
    }
    else
    {
      if (await GameService.AddUserAsync(Context.Channel, Context.User).ConfigureAwait(false))
      {
        await ReplyAsync($"**{Context.User.Username}** has joined.").ConfigureAwait(false);
      }
    }
  }
| Improve this Doc View Source

LeaveGameCmd()

Command to leave a game that is not yet started.

Declaration
public abstract Task LeaveGameCmd()
Returns
Type Description
Task
Examples
  [Command("leave")]
  public override async Task LeaveGameCmd()
  {
    if (Game != null)
    {
      await ReplyAsync("Cannot leave a game already in progress.").ConfigureAwait(false);
    }
    else if (!OpenToJoin)
    {
      await ReplyAsync("No game open to leave.").ConfigureAwait(false);
    }
    else
    {
      if (await GameService.RemoveUserAsync(Context.Channel, Context.User))
      {
          await ReplyAsync($"**{Context.User.Username}** has left.").ConfigureAwait(false);
      }
    }
  }
| Improve this Doc View Source

NextTurnCmd()

Command to advance to the next turn (if applicable).

Declaration
public abstract Task NextTurnCmd()
Returns
Type Description
Task
Examples
  [Command("turn")]
  public override Task NextTurnCmd()
    => (Game != null)
      ? Game.NextTurn()
      : (GameInProgress == CurrentlyPlaying.DifferentGame)
        ? ReplyAsync("Different game in progress.")
        : ReplyAsync("No game in progress.");
| Improve this Doc View Source

OnModuleBuilding(CommandService, ModuleBuilder)

Declaration
protected override void OnModuleBuilding(CommandService commandService, ModuleBuilder builder)
Parameters
Type Name Description
CommandService commandService
ModuleBuilder builder
Overrides
Discord.Commands.ModuleBase<Discord.Commands.SocketCommandContext>.OnModuleBuilding(Discord.Commands.CommandService, Discord.Commands.Builders.ModuleBuilder)
| Improve this Doc View Source

OpenGameCmd()

Command to open a game for others to join.

Declaration
public abstract Task OpenGameCmd()
Returns
Type Description
Task
Examples
  [Command("opengame")]
  public override async Task OpenGameCmd()
  {
    if (OpenToJoin)
    {
      await ReplyAsync("There is already a game open to join.").ConfigureAwait(false);
    }
    else if (GameInProgress != CurrentlyPlaying.None)
    {
      await ReplyAsync("Another game already in progress.").ConfigureAwait(false);
    }
    else
    {
      if (await GameService.OpenNewGame(Context).ConfigureAwait(false))
      {
        await ReplyAsync("Opening for a game.").ConfigureAwait(false);
      }
    }
  }
| Improve this Doc View Source

ResendUnsentDMsAsync()

Convinience method to resend the stored messages to someone who had their DMs disabled.

Declaration
protected Task ResendUnsentDMsAsync()
Returns
Type Description
Task
Examples
  [Command("resend")]
  public override Task ResendCmd() => ResendUnsentDMsAsync();
| Improve this Doc View Source

StartGameCmd()

Command to start a game with the players who joined.

Declaration
public abstract Task StartGameCmd()
Returns
Type Description
Task
Examples
  [Command("start")]
  public override async Task StartGameCmd()
  {
    if (Game != null)
    {
      await ReplyAsync("Another game already in progress.").ConfigureAwait(false);
    }
    else if (!OpenToJoin)
    {
      await ReplyAsync("No game has been opened at this time.").ConfigureAwait(false);
    }
    else if (JoinedUsers.Count < 2) // Example value if a game has a minimum player requirement
    {
      await ReplyAsync("Not enough players have joined.").ConfigureAwait(false);
    }
    else
    {
      if (GameService.TryUpdateOpenToJoin(Context.Channel,
          newValue: false, comparisonValue: true))
      {
        // Tip: Shuffle the players before projecting them
        var players = JoinedUsers.Select(u => new ExamplePlayer(u, Context.Channel));
        // The Player class can also be extended for additional properties
        var game = new ExampleGame(Context.Channel, players);
        if (await GameService.TryAddNewGameAsync(Context.Channel, game).ConfigureAwait(false))
        {
          await game.SetupGame().ConfigureAwait(false);
          await game.StartGame().ConfigureAwait(false);
        }
      }
    }
  }
  • Improve this Doc
  • View Source
Back to top Generated by DocFX