Skip to content

Commit

Permalink
Adds an optional Nullable<GameCreationSource> parameter for KookRestC…
Browse files Browse the repository at this point in the history
…lient.GetGamesAsync
  • Loading branch information
gehongyan committed Mar 16, 2023
1 parent fe1e9bc commit 22b799a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/Kook.Net.Core/Entities/Activities/GameCreationSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Kook;

/// <summary>
/// Represents the creation source of a game.
/// </summary>
public enum GameCreationSource
{
/// <summary>
/// Represents that the game was created by myself.
/// </summary>
SelfUser = 1,

/// <summary>
/// Represents that the game was created by the system by default.
/// </summary>
System = 2
}
4 changes: 2 additions & 2 deletions src/Kook.Net.Rest/ClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public static async Task<string> CreateAssetAsync(BaseKookClient client, Stream
return null;
}

public static IAsyncEnumerable<IReadOnlyCollection<RestGame>> GetGamesAsync(BaseKookClient client, RequestOptions options)
public static IAsyncEnumerable<IReadOnlyCollection<RestGame>> GetGamesAsync(BaseKookClient client, GameCreationSource? source, RequestOptions options)
{
return client.ApiClient.GetGamesAsync(options: options)
return client.ApiClient.GetGamesAsync(source, options: options)
.Select(x => x.Select(y => RestGame.Create(client, y)).ToImmutableArray() as IReadOnlyCollection<RestGame>);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Kook.Net.Rest/Entities/Games/RestGame.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Collections.Immutable;
using System.Diagnostics;
using Model = Kook.API.Game;

namespace Kook.Rest;

/// <summary>
/// Represents a game object.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class RestGame : RestEntity<int>, IGame
{
private ImmutableArray<string> _productNames;
Expand Down Expand Up @@ -63,4 +65,6 @@ public async Task DeleteAsync(RequestOptions options = null)
/// <inheritdoc />
async Task<IGame> IGame.ModifyAsync(Action<GameProperties> func, RequestOptions options)
=> await ModifyAsync(func, options);

private string DebuggerDisplay => $"{Name} ({Id}, {GameType.ToString()})";
}
10 changes: 8 additions & 2 deletions src/Kook.Net.Rest/KookRestApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1241,13 +1241,19 @@ public async Task<Stream> GetGuildBadgeAsync(ulong guildId, BadgeStyle style = B

#region Games

public IAsyncEnumerable<IReadOnlyCollection<Game>> GetGamesAsync(int limit = KookConfig.MaxItemsPerBatchByDefault, int fromPage = 1, RequestOptions options = null)
public IAsyncEnumerable<IReadOnlyCollection<Game>> GetGamesAsync(GameCreationSource? source, int limit = KookConfig.MaxItemsPerBatchByDefault, int fromPage = 1, RequestOptions options = null)
{
options = RequestOptions.CreateOrClone(options);

var ids = new BucketIds();
var creationSource = source switch
{
GameCreationSource.SelfUser => 1,
GameCreationSource.System => 2,
_ => 0
};
return SendPagedAsync<Game>(HttpMethod.Get,
(pageSize, page) => $"game?page_size={pageSize}&page={page}",
(pageSize, page) => $"game?type={creationSource}&page_size={pageSize}&page={page}",
ids, clientBucket: ClientBucketType.SendEdit, pageMeta: new PageMeta(page: fromPage, pageSize: limit), options: options);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Kook.Net.Rest/KookRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public Task<string> CreateAssetAsync(Stream stream, string fileName, RequestOpti

#region Games

public IAsyncEnumerable<IReadOnlyCollection<RestGame>> GetGamesAsync(RequestOptions options = null)
=> ClientHelper.GetGamesAsync(this, options);
public IAsyncEnumerable<IReadOnlyCollection<RestGame>> GetGamesAsync(GameCreationSource? source = null, RequestOptions options = null)
=> ClientHelper.GetGamesAsync(this, source, options);
public Task<RestGame> CreateGameAsync(string name, string processName, string iconUrl, RequestOptions options = null)
=> ClientHelper.CreateGameAsync(this, name, processName, iconUrl, options);

Expand Down
3 changes: 3 additions & 0 deletions src/Kook.Net.Rest/Net/DefaultRestClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#if DEBUG_REST
using System.Diagnostics;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
#endif
using System.Globalization;
using System.Net;
Expand Down

0 comments on commit 22b799a

Please sign in to comment.