Class MpGameModuleBase<TService, TGame, TPlayer>
Base class to manage a game between Discord users.
Inherited Members
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 SourceMpGameModuleBase(TService)
Initializes the MpGameModuleBase<TService, TGame, TPlayer> base class.
Declaration
protected MpGameModuleBase(TService gameService)
Parameters
Type | Name | Description |
---|---|---|
TService | gameService | The |
Properties
| Improve this Doc View SourceGame
The instance of the game being played (if active).
Declaration
protected TGame Game { get; }
Property Value
Type | Description |
---|---|
TGame |
GameInProgress
Determines if a game in the current channel is in progress or not.
Declaration
protected CurrentlyPlaying GameInProgress { get; }
Property Value
Type | Description |
---|---|
CurrentlyPlaying |
GameService
The GameService instance.
Declaration
protected TService GameService { get; }
Property Value
Type | Description |
---|---|
TService |
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.
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 |
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 |
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 SourceBeforeExecute(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
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
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);
}
}
}
}