Class MpGameService<TGame, TPlayer>
Service managing games for a MpGameModuleBase<TService, TGame, TPlayer>.
Implements
Namespace: Discord.Addons.MpGame
Assembly: Discord.Addons.MpGame.dll
Syntax
public class MpGameService<TGame, TPlayer>
where TGame : GameBase<TPlayer>
where TPlayer : Player
Type Parameters
Name | Description |
---|---|
TGame | The type of game to manage. |
TPlayer | The type of the Player object. |
Constructors
| Improve this Doc View SourceMpGameService(BaseSocketClient, IMpGameServiceConfig, Func<LogMessage, Task>)
Instantiates the MpGameService for the specified Game and Player type.
Declaration
public MpGameService(BaseSocketClient client, IMpGameServiceConfig mpconfig = null, Func<LogMessage, Task> logger = null)
Parameters
Type | Name | Description |
---|---|---|
BaseSocketClient | client | The Discord client. |
Discord.Addons.MpGame.IMpGameServiceConfig | mpconfig | An optional config type. |
Func<LogMessage, Task> | logger | An optional logging method. |
Properties
| Improve this Doc View SourceLogger
Logger function.
Declaration
protected Func<LogMessage, Task> Logger { get; }
Property Value
Type | Description |
---|---|
Func<LogMessage, Task> |
MessageChannelComparer
A cached IEqualityComparer<IMessageChannel> instance to use when instantiating a Dictionary<IMessageChannel, TValue>. This is the same instance as DiscordComparers.ChannelComparer.
Declaration
protected static IEqualityComparer<IMessageChannel> MessageChannelComparer { get; }
Property Value
Type | Description |
---|---|
IEqualityComparer<IMessageChannel> |
Examples
private readonly ConcurrentDictionary<IMessageChannel, MyData> _data
= new ConcurrentDictionary<IMessageChannel, MyData>(MessageChannelComparer);
Methods
| Improve this Doc View SourceAddPlayerAsync(TGame, TPlayer)
Adds a player to an ongoing game.
Declaration
public async Task<bool> AddPlayerAsync(TGame game, TPlayer player)
Parameters
Type | Name | Description |
---|---|---|
TGame | game | The game instance. |
TPlayer | player | The player to add. |
Returns
Type | Description |
---|---|
Task<Boolean> | true if the operation succeeded, otherwise false. |
Examples
if (await GameService.AddPlayerAsync(Game, new MyPlayer(Context.User, Context.Channel)).ConfigureAwait(false))
await ReplyAsync($"**{Context.User.Username}** has joined.").ConfigureAwait(false);
|
Improve this Doc
View Source
AddUserAsync(IMessageChannel, IUser)
Add a user to join an unstarted game.
Declaration
public async Task<bool> AddUserAsync(IMessageChannel channel, IUser user)
Parameters
Type | Name | Description |
---|---|---|
IMessageChannel | channel | Public facing channel of this game. |
IUser | user | The user. |
Returns
Type | Description |
---|---|
Task<Boolean> | true if the operation succeeded, otherwise false. |
CancelGameAsync(IMessageChannel)
Cancel a game that has not yet started.
Declaration
public Task<bool> CancelGameAsync(IMessageChannel channel)
Parameters
Type | Name | Description |
---|---|---|
IMessageChannel | channel | Public facing channel of this game. |
Returns
Type | Description |
---|---|
Task<Boolean> | true if the operation succeeded, otherwise false. |
Dispose()
Declaration
protected virtual void Dispose()
GetGameData(ICommandContext)
Gets the game metadata associated with this context.
Declaration
public MpGameService<TGame, TPlayer>.MpGameData GetGameData(ICommandContext context)
Parameters
Type | Name | Description |
---|---|---|
ICommandContext | context | The CommandContext to fetch metadata for. |
Returns
Type | Description |
---|---|
MpGameService.MpGameData<> | A snapshot of the current game metadata. |
GetGameFromChannel(IMessageChannel)
Retrieve the game instance being played, if any.
Declaration
public TGame GetGameFromChannel(IMessageChannel channel)
Parameters
Type | Name | Description |
---|---|---|
IMessageChannel | channel | A message channel. Can be both the public-facing channel or the DM channel of one of the players. |
Returns
Type | Description |
---|---|
TGame | The |
GetJoinedUsers(IMessageChannel)
Retrieve the users set to join an open game, if any.
Declaration
public IReadOnlyCollection<IUser> GetJoinedUsers(IMessageChannel channel)
Parameters
Type | Name | Description |
---|---|---|
IMessageChannel | channel | A message channel. Can be both the public-facing channel or the DM channel of one of the players. |
Returns
Type | Description |
---|---|
IReadOnlyCollection<IUser> | The users set to join a new game, or an empty collection if there is no data. |
IsOpenToJoin(IMessageChannel)
Retrieve whether a game has been opened and users can join.
Declaration
public bool IsOpenToJoin(IMessageChannel channel)
Parameters
Type | Name | Description |
---|---|---|
IMessageChannel | channel | A message channel. Can be both the public-facing channel or the DM channel of one of the players. |
Returns
Type | Description |
---|---|
Boolean | true if a game has been opened and users can join, otherwise false. |
KickPlayerAsync(TGame, TPlayer)
Kicks a player from an ongoing game.
Declaration
public Task<bool> KickPlayerAsync(TGame game, TPlayer player)
Parameters
Type | Name | Description |
---|---|---|
TGame | game | The game instance. |
TPlayer | player | The player to kick. |
Returns
Type | Description |
---|---|
Task<Boolean> | true if the operation succeeded, otherwise false. |
OpenNewGameAsync(ICommandContext)
Prepare to set up a new game in a specified channel.
Declaration
public async Task<bool> OpenNewGameAsync(ICommandContext context)
Parameters
Type | Name | Description |
---|---|---|
ICommandContext | context | Context of where this game is intended to be opened. |
Returns
Type | Description |
---|---|
Task<Boolean> | true if the operation succeeded, otherwise false. |
RemoveUserAsync(IMessageChannel, IUser)
Remove a user from an unstarted game.
Declaration
public async Task<bool> RemoveUserAsync(IMessageChannel channel, IUser user)
Parameters
Type | Name | Description |
---|---|---|
IMessageChannel | channel | Public facing channel of this game. |
IUser | user | The user. |
Returns
Type | Description |
---|---|
Task<Boolean> | true if the operation succeeded, otherwise false. |
Examples
if (await GameService.RemoveUserAsync(Context.Channel, user).ConfigureAwait(false))
await ReplyAsync($"**{user.Username}** has been kicked.").ConfigureAwait(false);
|
Improve this Doc
View Source
TryAddNewGameAsync(IMessageChannel, TGame)
Add a new game to the list of active games.
Declaration
public async Task<bool> TryAddNewGameAsync(IMessageChannel channel, TGame game)
Parameters
Type | Name | Description |
---|---|---|
IMessageChannel | channel | Public facing channel of this game. |
TGame | game | Instance of the game. |
Returns
Type | Description |
---|---|
Task<Boolean> | true if the operation succeeded, otherwise false. |
TryGetGameChannel(IDMChannel, out IMessageChannel)
Declaration
protected static bool TryGetGameChannel(IDMChannel dmChannel, out IMessageChannel publicChannel)
Parameters
Type | Name | Description |
---|---|---|
IDMChannel | dmChannel | |
IMessageChannel | publicChannel |
Returns
Type | Description |
---|---|
Boolean |
TryUpdateOpenToJoin(IMessageChannel, Boolean)
Updates the flag indicating if a game can be joined or not.
Declaration
public bool TryUpdateOpenToJoin(IMessageChannel channel, bool newValue)
Parameters
Type | Name | Description |
---|---|---|
IMessageChannel | channel | A message channel. Can be both the public-facing channel or the DM channel of one of the players. |
Boolean | newValue | The new value. |
Returns
Type | Description |
---|---|
Boolean | true if the value was updated, otherwise false. |
Explicit Interface Implementations
| Improve this Doc View SourceIDisposable.Dispose()
Declaration
void IDisposable.Dispose()